Skip to content

Commit

Permalink
Add support for Personal Access Token Authentication (#110)
Browse files Browse the repository at this point in the history
* Added tests on config

Signed-off-by: Rafa Porres Molina <[email protected]>

* moved to 1.15

Signed-off-by: Rafa Porres Molina <[email protected]>

* Upgrade go-jira

Signed-off-by: Rafa Porres Molina <[email protected]>

* Changes to support PAT authentication

Signed-off-by: Rafa Porres Molina <[email protected]>

* addded PAT

Signed-off-by: Rafa Porres Molina <[email protected]>

* Added tests for Personal Access Token config

Signed-off-by: Rafa Porres Molina <[email protected]>

* Removed redundant check from main

Signed-off-by: Rafa Porres Molina <[email protected]>

* Add missing trailing dots in config_test.go

Signed-off-by: Rafa Porres Molina <[email protected]>

* Clarified TODO comment

Signed-off-by: Rafa Porres Molina <[email protected]>

* added trailing dots in config.go

Signed-off-by: Rafa Porres Molina <[email protected]>

* Used anonymous struct and in place test cases

Signed-off-by: Rafa Porres Molina <[email protected]>

* User/Password can be overriden separately

Signed-off-by: Rafa Porres Molina <[email protected]>
  • Loading branch information
rporres authored Mar 15, 2022
1 parent 9d6a5ed commit 0c7c40f
Show file tree
Hide file tree
Showing 8 changed files with 381 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
steps:
- uses: actions/setup-go@v2
with:
go-version: 1.14.x
go-version: 1.15.x
- uses: actions/checkout@v2
- name: Login to quay.io Docker Image Registry
uses: docker/login-action@v1
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/setup-go@v2
with:
go-version: 1.14.x
go-version: 1.15.x
- uses: actions/checkout@v2
- run: make
env:
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.14 AS builder
FROM golang:1.15 AS builder
WORKDIR /go/src/github.com/prometheus-community/jiralert
COPY . /go/src/github.com/prometheus-community/jiralert
RUN GO111MODULE=on GOBIN=/tmp/bin make
Expand Down
18 changes: 14 additions & 4 deletions cmd/jiralert/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,21 @@ func main() {
level.Debug(logger).Log("msg", " matched receiver", "receiver", conf.Name)

// TODO: Consider reusing notifiers or just jira clients to reuse connections.
tp := jira.BasicAuthTransport{
Username: conf.User,
Password: string(conf.Password),
var client *jira.Client
var err error
if conf.User != "" && conf.Password != "" {
tp := jira.BasicAuthTransport{
Username: conf.User,
Password: string(conf.Password),
}
client, err = jira.NewClient(tp.Client(), conf.APIURL)
} else if conf.PersonalAccessToken != "" {
tp := jira.PATAuthTransport{
Token: string(conf.PersonalAccessToken),
}
client, err = jira.NewClient(tp.Client(), conf.APIURL)
}
client, err := jira.NewClient(tp.Client(), conf.APIURL)

if err != nil {
errorHandler(w, http.StatusInternalServerError, err, conf.Name, &data, logger)
return
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
module github.com/prometheus-community/jiralert

go 1.14
go 1.15

require (
github.com/andygrunwald/go-jira v1.11.2-0.20200514151831-146229d2ab58
github.com/fatih/structs v1.1.0 // indirect
github.com/andygrunwald/go-jira v1.15.1
github.com/go-kit/kit v0.10.0
github.com/golang/protobuf v1.4.1 // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.6.0
github.com/prometheus/common v0.10.0 // indirect
Expand Down
20 changes: 12 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuy
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/andygrunwald/go-jira v1.11.2-0.20200514151831-146229d2ab58 h1:mlwwah02q6TPeO31g6qlmVzNJhhomz0YJcdDNvcJaCA=
github.com/andygrunwald/go-jira v1.11.2-0.20200514151831-146229d2ab58/go.mod h1:jYi4kFDbRPZTJdJOVJO4mpMMIwdB+rcZwSO58DzPd2I=
github.com/andygrunwald/go-jira v1.15.1 h1:6J9aYKb9sW8bxv3pBLYBrs0wdsFrmGI5IeTgWSKWKc8=
github.com/andygrunwald/go-jira v1.15.1/go.mod h1:GIYN1sHOIsENWUZ7B4pDeT/nxEtrZpE8l0987O67ZR8=
github.com/apache/thrift v0.12.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/apache/thrift v0.13.0/go.mod h1:cp2SuWMxlEZw2r+iP2GNCdIi4C1qmUzdZFSVb+bacwQ=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down Expand Up @@ -54,7 +54,6 @@ github.com/envoyproxy/go-control-plane v0.6.9/go.mod h1:SBwIajubJHhxtWwsL9s8ss4s
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/structs v1.0.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=
Expand All @@ -76,6 +75,8 @@ github.com/gogo/googleapis v1.1.0/go.mod h1:gf4bu3Q80BeJ6H1S1vYPm8/ELATdvryBaNFG
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.0/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
github.com/golang-jwt/jwt/v4 v4.3.0 h1:kHL1vqdqWNfATmA0FNMdmZNMyZI1U6O31X4rlIPoBog=
github.com/golang-jwt/jwt/v4 v4.3.0/go.mod h1:/xlHOz8bRuivTWchD4jCa+NbatV+wEUSzwAxVc6locg=
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
github.com/golang/groupcache v0.0.0-20160516000752-02826c3e7903/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
Expand All @@ -98,9 +99,11 @@ github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v0.0.0-20170111101155-53e6ce116135/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
Expand Down Expand Up @@ -263,7 +266,6 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/trivago/tgo v1.0.1/go.mod h1:w4dpD+3tzNIIiIfkWWa85w5/B77tlvdZckQ+6PkFnhc=
github.com/trivago/tgo v1.0.7 h1:uaWH/XIy9aWYWpjm2CU3RpcqZXmX2ysQ9/Go+d9gyrM=
github.com/trivago/tgo v1.0.7/go.mod h1:w4dpD+3tzNIIiIfkWWa85w5/B77tlvdZckQ+6PkFnhc=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
Expand All @@ -284,7 +286,6 @@ go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20181029021203-45a5f77698d3/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand Down Expand Up @@ -338,6 +339,9 @@ golang.org/x/sys v0.0.0-20191220142924-d4481acd189f/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f h1:gWF768j/LaZugp8dyS4UwsslYCYz9XgFxvlgsn0n9H8=
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
Expand Down
62 changes: 40 additions & 22 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,29 +88,30 @@ func resolveFilepaths(baseDir string, cfg *Config, logger log.Logger) {
cfg.Template = join(cfg.Template)
}

// ReceiverConfig is the configuration for one receiver. It has a unique name and includes API access fields (URL, user
// and password) and issue fields (required -- e.g. project, issue type -- and optional -- e.g. priority).
// ReceiverConfig is the configuration for one receiver. It has a unique name and includes API access fields (url and
// auth) and issue fields (required -- e.g. project, issue type -- and optional -- e.g. priority).
type ReceiverConfig struct {
Name string `yaml:"name" json:"name"`

// API access fields
APIURL string `yaml:"api_url" json:"api_url"`
User string `yaml:"user" json:"user"`
Password Secret `yaml:"password" json:"password"`
APIURL string `yaml:"api_url" json:"api_url"`
User string `yaml:"user" json:"user"`
Password Secret `yaml:"password" json:"password"`
PersonalAccessToken Secret `yaml:"personal_access_token" json:"personal_access_token"`

// Required issue fields
Project string `yaml:"project" json:"project"`
IssueType string `yaml:"issue_type" json:"issue_type"`
Summary string `yaml:"summary" json:"summary"`
ReopenState string `yaml:"reopen_state" json:"reopen_state"`
Project string `yaml:"project" json:"project"`
IssueType string `yaml:"issue_type" json:"issue_type"`
Summary string `yaml:"summary" json:"summary"`
ReopenState string `yaml:"reopen_state" json:"reopen_state"`
ReopenDuration *Duration `yaml:"reopen_duration" json:"reopen_duration"`

// Optional issue fields
Priority string `yaml:"priority" json:"priority"`
Description string `yaml:"description" json:"description"`
WontFixResolution string `yaml:"wont_fix_resolution" json:"wont_fix_resolution"`
Fields map[string]interface{} `yaml:"fields" json:"fields"`
Components []string `yaml:"components" json:"components"`
ReopenDuration *Duration `yaml:"reopen_duration" json:"reopen_duration"`

// Label copy settings
AddGroupLabels bool `yaml:"add_group_labels" json:"add_group_labels"`
Expand Down Expand Up @@ -158,17 +159,24 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
// We want to set c to the defaults and then overwrite it with the input.
// To make unmarshal fill the plain data struct rather than calling UnmarshalYAML
// again, we have to hide it using a type indirection.

// TODO: This function panics when there are no defaults. This needs to be fixed.

type plain Config
if err := unmarshal((*plain)(c)); err != nil {
return err
}

if (c.Defaults.User != "" || c.Defaults.Password != "") && c.Defaults.PersonalAccessToken != "" {
return fmt.Errorf("bad auth config in defaults section: user/password and PAT authentication are mutually exclusive")
}

for _, rc := range c.Receivers {
if rc.Name == "" {
return fmt.Errorf("missing name for receiver %+v", rc)
}

// Check API access fields
// Check API access fields.
if rc.APIURL == "" {
if c.Defaults.APIURL == "" {
return fmt.Errorf("missing api_url in receiver %q", rc.Name)
Expand All @@ -178,20 +186,30 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
if _, err := url.Parse(rc.APIURL); err != nil {
return fmt.Errorf("invalid api_url %q in receiver %q: %s", rc.APIURL, rc.Name, err)
}
if rc.User == "" {
if c.Defaults.User == "" {
return fmt.Errorf("missing user in receiver %q", rc.Name)
}
rc.User = c.Defaults.User

if (rc.User != "" || rc.Password != "") && rc.PersonalAccessToken != "" {
return fmt.Errorf("bad auth config in receiver %q: user/password and PAT authentication are mutually exclusive", rc.Name)
}
if rc.Password == "" {
if c.Defaults.Password == "" {
return fmt.Errorf("missing password in receiver %q", rc.Name)

if (rc.User == "" || rc.Password == "") && rc.PersonalAccessToken == "" {
if rc.User == "" && c.Defaults.User != "" {
rc.User = c.Defaults.User
}

if rc.Password == "" && c.Defaults.Password != "" {
rc.Password = c.Defaults.Password
}

if rc.User != "" && rc.Password != "" {
// Nothing to do, we're ready to go with basic auth.
} else if c.Defaults.PersonalAccessToken != "" {
rc.PersonalAccessToken = c.Defaults.PersonalAccessToken
} else {
return fmt.Errorf("missing authentication in receiver %q", rc.Name)
}
rc.Password = c.Defaults.Password
}

// Check required issue fields
// Check required issue fields.
if rc.Project == "" {
if c.Defaults.Project == "" {
return fmt.Errorf("missing project in receiver %q", rc.Name)
Expand Down Expand Up @@ -223,7 +241,7 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
rc.ReopenDuration = c.Defaults.ReopenDuration
}

// Populate optional issue fields, where necessary
// Populate optional issue fields, where necessary.
if rc.Priority == "" && c.Defaults.Priority != "" {
rc.Priority = c.Defaults.Priority
}
Expand Down
Loading

0 comments on commit 0c7c40f

Please sign in to comment.