[#148] [#149] add support for api token based authentication

This commit is contained in:
Cory Bennett
2018-03-08 10:57:45 -08:00
parent 161a68920d
commit edb06621f8
15 changed files with 125 additions and 45 deletions
+20 -11
View File
@@ -27,6 +27,7 @@
* [Templates](#templates) * [Templates](#templates)
* [Writing/Editing Templates](#writingediting-templates) * [Writing/Editing Templates](#writingediting-templates)
* [Authentication](#authentication) * [Authentication](#authentication)
* [login vs user](#login-vs-user)
* [keyring password source](#keyring-password-source) * [keyring password source](#keyring-password-source)
* [pass password source](#pass-password-source) * [pass password source](#pass-password-source)
* [Usage](#usage) * [Usage](#usage)
@@ -115,7 +116,7 @@ Flags:
``` ```
###### **Incompatible command changes** ###### **Incompatible command changes**
Unfortunately during the rewrite between v0 and v1 there were some changes necessary that broke backwards compatibility with existing commands. Specifically the `dups`, `blocks`, `add worklog` and `add|remove|set labels` commands have had the command word swapped around: Unfortunately during the rewrite between v0 and v1 there were some necessary changes that broke backwards compatibility with existing commands. Specifically the `dups`, `blocks`, `add worklog` and `add|remove|set labels` commands have had the command word swapped around:
* `jira DUPLICATE dups ISSUE` => `jira dup DUPLICATE ISSUE` * `jira DUPLICATE dups ISSUE` => `jira dup DUPLICATE ISSUE`
* `jira BLOCKER blocks ISSUE` => `jira block BLOCKER ISSUE` * `jira BLOCKER blocks ISSUE` => `jira block BLOCKER ISSUE`
* `jira add worklog` => `jira worklog add` * `jira add worklog` => `jira worklog add`
@@ -124,20 +125,17 @@ Unfortunately during the rewrite between v0 and v1 there were some changes neces
* `jira set labels` => `jira labels set` * `jira set labels` => `jira labels set`
###### **Login process change** ###### **Login process change**
We have, once again, changed how login happens for Jira. When authenticating against Atlassian Cloud Jira [API Tokens are now required](https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-basic-auth-and-cookie-based-auth/). Please read [the Authentication section](#authentication) below for more information.
If you use a privately hosted Jira service, you can chose to use the API Token method or continue using the session login api. Please read [the Authentication section](#authentication) below for more information.
Previously `jira` used attempt to get a `JSESSION` cookies by authenticating with the webservice standard GUI login process. This has been especially problematic as users need to authenticate with various credential providers (google auth, etc). We now attempt to authenticate via the [session login api](https://docs.atlassian.com/jira/REST/cloud/#auth/1/session-login). This may be problematic for users if admins have locked down the session-login api, so we might have to bring back the error-prone Basic-Auth approach. For users that are unable to authenticate via `jira` hopefully someone in your organization can provide me with details on a process for you to authenticate and we can try to update `jira`. Previously `jira` used attempt to get a `JSESSION` cookies by authenticating with the webservice standard GUI login process. This has been especially problematic as users need to authenticate with various credential providers (google auth, etc). We now attempt to authenticate via the [session login api](https://docs.atlassian.com/jira/REST/cloud/#auth/1/session-login). This may be problematic for users if admins have locked down the session-login api, so we might have to bring back the error-prone Basic-Auth approach. For users that are unable to authenticate via `jira` hopefully someone in your organization can provide me with details on a process for you to authenticate and we can try to update `jira`.
## Configuration ## Configuration
**go-jira** uses a configuration hierarchy. When loading the configuration from disk it will recursively look through **go-jira** uses a configuration hierarchy. When loading the configuration from disk it will recursively look through all parent directories in your current path looking for a **.jira.d** directory. If your current directory is not a child directory of your homedir, then your homedir will also be inspected for a **.jira.d** directory. From all of **.jira.d** directories discovered **go-jira** will load a **<command>.yml** file (ie for `jira list` it will load `.jira.d/list.yml`) then it will merge in any properties from the **config.yml** if found. The configuration properties found in a file closests to your current working directory will have precedence. Properties overriden with command line options will have final precedence.
all parent directories in your current path looking for a **.jira.d** directory. If your current directory is not
a child directory of your homedir, then your homedir will also be inspected for a **.jira.d** directory. From all of **.jira.d** directories
discovered **go-jira** will load a **<command>.yml** file (ie for `jira list` it will load `.jira.d/list.yml`) then it will merge in any properties from the **config.yml** if found. The configuration properties found in a file closests to your current working directory
will have precedence. Properties overriden with command line options will have final precedence.
The complicated configuration hierarchy is used because **go-jira** attempts to be context aware. For example, if you are working on a "foo" project and The complicated configuration hierarchy is used because **go-jira** attempts to be context aware. For example, if you are working on a "foo" project and you `cd` into your project workspace, wouldn't it be nice if `jira ls` automatically knew to list only issues related to the "foo" project? Likewise when you `cd` to the "bar" project then `jira ls` should only list issues related to "bar" project. You can do this with by creating a configuration under your project workspace at **./.jira.d/config.yml** that looks like:
you `cd` into your project workspace, wouldn't it be nice if `jira ls` automatically knew to list only issues related to the "foo" project? Likewise when you
`cd` to the "bar" project then `jira ls` should only list issues related to "bar" project. You can do this with by creating a configuration under your project
workspace at **./.jira.d/config.yml** that looks like:
``` ```
project: foo project: foo
@@ -370,7 +368,18 @@ jira list -t debug
### Authentication ### Authentication
By default `go-jira` will prompt for a password automatically when get a response header from the Jira service that indicates you do not have an active session (ie the `X-Ausername` header is set to `anonymous`). Then after authentication we cache the `cloud.session.token` cookie returned by the service [session login api](https://docs.atlassian.com/jira/REST/cloud/#auth/1/session-login) and reuse that on subsequent requests. Typically this cookie will be valid for several hours (depending on the service configuration). To automatically securely store your password for easy reuse by jira You can enable a `password-source` via `.jira.d/config.yml` with possible values of `keyring` or `pass`. For Atlassian Cloud hosted Jira [API Tokens are now required](https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-basic-auth-and-cookie-based-auth/). You will automatically be prompted for an API Token if your jira endoint ends in `.atlassian.net`. If you are using a private Jira service, you can force `jira` to use an api-token by setting the `authentication-method: api-token` property in your `$HOME/.jira.d/config.yml` file. The API Token needs to be presented to the Jira service on every request, so it is recommended to store this API Token security within your OS's keyring, or using the `pass` service as documented below so that it can be programatically accessed via `jira` and not prompt you every time. For a less-secure option you can also provide the API token via a `JIRA_API_TOKEN` environment variable. If you are unable to use an api-token for an Atlassian Cloud hosted Jira then you can still force `jira` to use the old session based authentication (until it the hosted system stops accepting it) by setting `authentication-method: session`.
If your Jira service still allows you to use the Session based authention method then `jira` will prompt for a password automatically when get a response header from the Jira service that indicates you do not have an active session (ie the `X-Ausername` header is set to `anonymous`). Then after authentication we cache the `cloud.session.token` cookie returned by the service [session login api](https://docs.atlassian.com/jira/REST/cloud/#auth/1/session-login) and reuse that on subsequent requests. Typically this cookie will be valid for several hours (depending on the service configuration). To automatically securely store your password for easy reuse by jira You can enable a `password-source` via `.jira.d/config.yml` with possible values of `keyring` or `pass`.
#### User vs Login
The Jira service has sometimes differing opinions about how a user is identified. In other words the ID you login with might not be ID that the jira system recognized you as. This matters when trying to identify a user via various Jira REST APIs (like issue assignment). This is especially relevent when trying to authenticate with an API Token where the authentication user is usually an email address, but within the Jira system the user is identified by a user name. To accomodate this `jira` now supports two different properties in the config file. So when authentication using the API Tokens you will likely want something like this in your `$HOME/.jira.d/config.yml` file:
```
user: person
login: person@example.com
```
You can also override these values on the command line with `jira --user person --login person@example.com`. The `login` value will be used only for authentication purposes, the `user` value will be used when a user name is required for any Jira service API calls.
#### keyring password source #### keyring password source
On OSX and Linux there are a few keyring providers that `go-jira` can use (via this [golang module](https://github.com/tmc/keyring)). To integrate `go-jira` with a supported keyring just add this configuration to `$HOME/.jira.d/config.yml`: On OSX and Linux there are a few keyring providers that `go-jira` can use (via this [golang module](https://github.com/tmc/keyring)). To integrate `go-jira` with a supported keyring just add this configuration to `$HOME/.jira.d/config.yml`:
+34 -2
View File
@@ -3,6 +3,7 @@ package jiracli
import ( import (
"bytes" "bytes"
"crypto/tls" "crypto/tls"
"encoding/base64"
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
@@ -27,12 +28,14 @@ type Exit struct {
} }
type GlobalOptions struct { type GlobalOptions struct {
AuthenticationMethod figtree.StringOption `yaml:"authentication-method,omitempty" json:"authentication-method,omitempty"`
Endpoint figtree.StringOption `yaml:"endpoint,omitempty" json:"endpoint,omitempty"` Endpoint figtree.StringOption `yaml:"endpoint,omitempty" json:"endpoint,omitempty"`
Insecure figtree.BoolOption `yaml:"insecure,omitempty" json:"insecure,omitempty"` Insecure figtree.BoolOption `yaml:"insecure,omitempty" json:"insecure,omitempty"`
Login figtree.StringOption `yaml:"login,omitempty" json:"login,omitempty"`
PasswordSource figtree.StringOption `yaml:"password-source,omitempty" json:"password-source,omitempty"` PasswordSource figtree.StringOption `yaml:"password-source,omitempty" json:"password-source,omitempty"`
Quiet figtree.BoolOption `yaml:"quiet,omitempty" json:"quiet,omitempty"` Quiet figtree.BoolOption `yaml:"quiet,omitempty" json:"quiet,omitempty"`
UnixProxy figtree.StringOption `yaml:"unixproxy,omitempty" json:"unixproxy,omitempty"`
SocksProxy figtree.StringOption `yaml:"socksproxy,omitempty" json:"socksproxy,omitempty"` SocksProxy figtree.StringOption `yaml:"socksproxy,omitempty" json:"socksproxy,omitempty"`
UnixProxy figtree.StringOption `yaml:"unixproxy,omitempty" json:"unixproxy,omitempty"`
User figtree.StringOption `yaml:"user,omitempty" json:"user,omitempty"` User figtree.StringOption `yaml:"user,omitempty" json:"user,omitempty"`
} }
@@ -69,19 +72,41 @@ func RegisterCommand(regEntry CommandRegistry) {
globalCommandRegistry = append(globalCommandRegistry, regEntry) globalCommandRegistry = append(globalCommandRegistry, regEntry)
} }
func (o *GlobalOptions) AuthMethod() string {
if strings.Contains(o.Endpoint.Value, ".atlassian.net") && o.AuthenticationMethod.Source == "default" {
return "api-token"
}
return o.AuthenticationMethod.Value
}
func register(app *kingpin.Application, o *oreo.Client, fig *figtree.FigTree) { func register(app *kingpin.Application, o *oreo.Client, fig *figtree.FigTree) {
globals := GlobalOptions{ globals := GlobalOptions{
User: figtree.NewStringOption(os.Getenv("USER")), User: figtree.NewStringOption(os.Getenv("USER")),
AuthenticationMethod: figtree.NewStringOption("session"),
} }
app.Flag("endpoint", "Base URI to use for Jira").Short('e').SetValue(&globals.Endpoint) app.Flag("endpoint", "Base URI to use for Jira").Short('e').SetValue(&globals.Endpoint)
app.Flag("insecure", "Disable TLS certificate verification").Short('k').SetValue(&globals.Insecure) app.Flag("insecure", "Disable TLS certificate verification").Short('k').SetValue(&globals.Insecure)
app.Flag("quiet", "Suppress output to console").Short('Q').SetValue(&globals.Quiet) app.Flag("quiet", "Suppress output to console").Short('Q').SetValue(&globals.Quiet)
app.Flag("unixproxy", "Path for a unix-socket proxy").SetValue(&globals.UnixProxy) app.Flag("unixproxy", "Path for a unix-socket proxy").SetValue(&globals.UnixProxy)
app.Flag("socksproxy", "Address for a socks proxy").SetValue(&globals.SocksProxy) app.Flag("socksproxy", "Address for a socks proxy").SetValue(&globals.SocksProxy)
app.Flag("user", "Login name used for authentication with Jira service").Short('u').SetValue(&globals.User) app.Flag("user", "user name used within the Jira service").Short('u').SetValue(&globals.User)
app.Flag("login", "login name that corresponds to the user used for authentication").SetValue(&globals.Login)
o = o.WithPreCallback(
func(req *http.Request) (*http.Request, error) {
if globals.AuthMethod() == "api-token" {
// need to set basic auth header with user@domain:api-token
token := globals.GetPass()
authHeader := fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", globals.Login.Value, token))))
req.Header.Add("Authorization", authHeader)
}
return req, nil
},
)
o = o.WithPostCallback( o = o.WithPostCallback(
func(req *http.Request, resp *http.Response) (*http.Response, error) { func(req *http.Request, resp *http.Response) (*http.Response, error) {
if globals.AuthMethod() == "session" {
authUser := resp.Header.Get("X-Ausername") authUser := resp.Header.Get("X-Ausername")
if authUser == "" || authUser == "anonymous" { if authUser == "" || authUser == "anonymous" {
// preserve the --quiet value, we need to temporarily disable it so // preserve the --quiet value, we need to temporarily disable it so
@@ -97,6 +122,7 @@ func register(app *kingpin.Application, o *oreo.Client, fig *figtree.FigTree) {
// rerun the original request // rerun the original request
return o.Do(req) return o.Do(req)
} }
}
return resp, nil return resp, nil
}, },
) )
@@ -132,6 +158,12 @@ func register(app *kingpin.Application, o *oreo.Client, fig *figtree.FigTree) {
} else if globals.SocksProxy.Value != "" { } else if globals.SocksProxy.Value != "" {
o = o.WithTransport(socksProxy(globals.SocksProxy.Value)) o = o.WithTransport(socksProxy(globals.SocksProxy.Value))
} }
if globals.AuthMethod() == "api-token" {
o = o.WithCookieFile("")
}
if globals.Login.Value == "" {
globals.Login = globals.User
}
return nil return nil
}) })
+33 -6
View File
@@ -3,6 +3,7 @@ package jiracli
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"os"
"os/exec" "os/exec"
"strings" "strings"
@@ -12,24 +13,36 @@ import (
func (o *GlobalOptions) ProvideAuthParams() *jiradata.AuthParams { func (o *GlobalOptions) ProvideAuthParams() *jiradata.AuthParams {
return &jiradata.AuthParams{ return &jiradata.AuthParams{
Username: o.User.Value, Username: o.Login.Value,
Password: o.GetPass(), Password: o.GetPass(),
} }
} }
func (o *GlobalOptions) keyName() string {
user := o.Login.Value
if o.AuthMethod() == "api-token" {
user = "api-token:" + user
}
if o.PasswordSource.Value == "pass" {
return fmt.Sprintf("GoJira/%s", user)
}
return user
}
func (o *GlobalOptions) GetPass() string { func (o *GlobalOptions) GetPass() string {
passwd := "" passwd := ""
if o.PasswordSource.Value != "" { if o.PasswordSource.Value != "" {
if o.PasswordSource.Value == "keyring" { if o.PasswordSource.Value == "keyring" {
var err error var err error
passwd, err = keyringGet(o.User.Value) passwd, err = keyringGet(o.keyName())
if err != nil { if err != nil {
panic(err) panic(err)
} }
} else if o.PasswordSource.Value == "pass" { } else if o.PasswordSource.Value == "pass" {
if bin, err := exec.LookPath("pass"); err == nil { if bin, err := exec.LookPath("pass"); err == nil {
buf := bytes.NewBufferString("") buf := bytes.NewBufferString("")
cmd := exec.Command(bin, fmt.Sprintf("GoJira/%s", o.User)) cmd := exec.Command(bin, o.keyName())
cmd.Stdout = buf cmd.Stdout = buf
cmd.Stderr = buf cmd.Stderr = buf
if err := cmd.Run(); err == nil { if err := cmd.Run(); err == nil {
@@ -44,9 +57,23 @@ func (o *GlobalOptions) GetPass() string {
if passwd != "" { if passwd != "" {
return passwd return passwd
} }
if passwd = os.Getenv("JIRA_API_TOKEN"); passwd != "" && o.AuthMethod() == "api-token" {
return passwd
}
prompt := fmt.Sprintf("Jira Password [%s]: ", o.Login)
help := ""
if o.AuthMethod() == "api-token" {
prompt = fmt.Sprintf("Jira API-Token [%s]: ", o.Login)
help = "API Tokens may be required by your Jira service endpoint: https://developer.atlassian.com/cloud/jira/platform/deprecation-notice-basic-auth-and-cookie-based-auth/"
}
err := survey.AskOne( err := survey.AskOne(
&survey.Password{ &survey.Password{
Message: fmt.Sprintf("Jira Password [%s]: ", o.User), Message: prompt,
Help: help,
}, },
&passwd, &passwd,
nil, nil,
@@ -62,7 +89,7 @@ func (o *GlobalOptions) GetPass() string {
func (o *GlobalOptions) SetPass(passwd string) error { func (o *GlobalOptions) SetPass(passwd string) error {
if o.PasswordSource.Value == "keyring" { if o.PasswordSource.Value == "keyring" {
// save password in keychain so that it can be used for subsequent http requests // save password in keychain so that it can be used for subsequent http requests
err := keyringSet(o.User.Value, passwd) err := keyringSet(o.keyName(), passwd)
if err != nil { if err != nil {
log.Errorf("Failed to set password in keyring: %s", err) log.Errorf("Failed to set password in keyring: %s", err)
return err return err
@@ -70,7 +97,7 @@ func (o *GlobalOptions) SetPass(passwd string) error {
} else if o.PasswordSource.Value == "pass" { } else if o.PasswordSource.Value == "pass" {
if bin, err := exec.LookPath("pass"); err == nil { if bin, err := exec.LookPath("pass"); err == nil {
log.Debugf("using %s", bin) log.Debugf("using %s", bin)
passName := fmt.Sprintf("GoJira/%s", o.User) passName := o.keyName()
if passwd != "" { if passwd != "" {
in := bytes.NewBufferString(fmt.Sprintf("%s\n%s\n", passwd, passwd)) in := bytes.NewBufferString(fmt.Sprintf("%s\n%s\n", passwd, passwd))
out := bytes.NewBufferString("") out := bytes.NewBufferString("")
+5
View File
@@ -46,6 +46,11 @@ func authCallback(req *http.Request, resp *http.Response) (*http.Response, error
// CmdLogin will attempt to login into jira server // CmdLogin will attempt to login into jira server
func CmdLogin(o *oreo.Client, globals *jiracli.GlobalOptions, opts *jiracli.CommonOptions) error { func CmdLogin(o *oreo.Client, globals *jiracli.GlobalOptions, opts *jiracli.CommonOptions) error {
if globals.AuthMethod() == "api-token" {
log.Noticef("No need to login when using api-token authentication method")
return nil
}
ua := o.WithoutRedirect().WithRetries(0).WithoutCallbacks().WithPostCallback(authCallback) ua := o.WithoutRedirect().WithRetries(0).WithoutCallbacks().WithPostCallback(authCallback)
for { for {
if session, err := jira.GetSession(o, globals.Endpoint.Value); err != nil { if session, err := jira.GetSession(o, globals.Endpoint.Value); err != nil {
+4
View File
@@ -27,6 +27,10 @@ func CmdLogoutRegistry() *jiracli.CommandRegistryEntry {
// CmdLogout will attempt to terminate an active Jira session // CmdLogout will attempt to terminate an active Jira session
func CmdLogout(o *oreo.Client, globals *jiracli.GlobalOptions, opts *jiracli.CommonOptions) error { func CmdLogout(o *oreo.Client, globals *jiracli.GlobalOptions, opts *jiracli.CommonOptions) error {
if globals.AuthMethod() == "api-token" {
log.Noticef("No need to logout when using api-token authentication method")
return nil
}
ua := o.WithoutRedirect().WithRetries(0).WithoutCallbacks() ua := o.WithoutRedirect().WithRetries(0).WithoutCallbacks()
err := jira.DeleteSession(ua, globals.Endpoint.Value) err := jira.DeleteSession(ua, globals.Endpoint.Value)
if err == nil { if err == nil {
+2
View File
@@ -17,7 +17,9 @@ func CmdTakeRegistry() *jiracli.CommandRegistryEntry {
return CmdAssignUsage(cmd, &opts) return CmdAssignUsage(cmd, &opts)
}, },
func(o *oreo.Client, globals *jiracli.GlobalOptions) error { func(o *oreo.Client, globals *jiracli.GlobalOptions) error {
if opts.Assignee == "" {
opts.Assignee = globals.User.Value opts.Assignee = globals.User.Value
}
return CmdAssign(o, globals, &opts) return CmdAssign(o, globals, &opts)
}, },
} }
+1
View File
@@ -3,6 +3,7 @@ config:
password-source: pass password-source: pass
endpoint: https://go-jira.atlassian.net endpoint: https://go-jira.atlassian.net
user: gojira user: gojira
login: gojira@corybennett.org
project: BASIC project: BASIC
+1 -1
View File
@@ -245,7 +245,7 @@ EOF
# reset login for mothra for voting # reset login for mothra for voting
############################################################################### ###############################################################################
jira="$jira --user mothra" jira="$jira --user mothra --login mothra@corybennett.org"
RUNS $jira logout RUNS $jira logout
RUNS $jira login RUNS $jira login
+1 -1
View File
@@ -185,7 +185,7 @@ EOF
# reset login for mothra for voting # reset login for mothra for voting
############################################################################### ###############################################################################
jira="$jira --user mothra" jira="$jira --user mothra --login mothra@corybennett.org"
RUNS $jira logout RUNS $jira logout
echo "mothra123" | RUNS $jira login echo "mothra123" | RUNS $jira login
+1 -1
View File
@@ -185,7 +185,7 @@ EOF
# reset login for mothra for voting # reset login for mothra for voting
############################################################################### ###############################################################################
jira="$jira --user mothra" jira="$jira --user mothra --login mothra@corybennett.org"
RUNS $jira logout RUNS $jira logout
echo "mothra123" | RUNS $jira login echo "mothra123" | RUNS $jira login
+1 -1
View File
@@ -185,7 +185,7 @@ EOF
# reset login for mothra for voting # reset login for mothra for voting
############################################################################### ###############################################################################
jira="$jira --user mothra" jira="$jira --user mothra --login mothra@corybennett.org"
RUNS $jira logout RUNS $jira logout
echo "mothra123" | RUNS $jira login echo "mothra123" | RUNS $jira login
+1 -1
View File
@@ -194,7 +194,7 @@ EOF
# reset login for mothra for voting # reset login for mothra for voting
############################################################################### ###############################################################################
jira="$jira --user mothra" jira="$jira --user mothra --login mothra@corybennett.org"
RUNS $jira logout RUNS $jira logout
echo "mothra123" | RUNS $jira login echo "mothra123" | RUNS $jira login
+1 -1
View File
@@ -187,7 +187,7 @@ EOF
# reset login for mothra for voting # reset login for mothra for voting
############################################################################### ###############################################################################
jira="$jira --user mothra" jira="$jira --user mothra --login mothra@corybennett.org"
RUNS $jira logout RUNS $jira logout
echo "mothra123" | RUNS $jira login echo "mothra123" | RUNS $jira login