feat: add username and password login (#418)

* feat: add username and password login

* fix: make sure cookie is not empty

* chore: go mod tidy

* fix: prevent local config from influencing tests

* fix: small cleanup on error handling

* fix: remove unnecessary trim
This commit is contained in:
Jeffrey Smith II
2022-07-28 10:53:19 -04:00
committed by GitHub
parent 182303e31d
commit f34e6a888f
11 changed files with 313 additions and 4 deletions

View File

@ -66,6 +66,7 @@ https://docs.influxdata.com/influxdb/latest/reference/cli/influx/config/
func newConfigCreateCmd() cli.Command {
var cfg config.Config
var userpass string
return cli.Command{
Name: "create",
Usage: "Create config",
@ -73,6 +74,12 @@ func newConfigCreateCmd() cli.Command {
The influx config create command creates a new InfluxDB connection configuration
and stores it in the configs file (by default, stored at ~/.influxdbv2/configs).
Authentication:
Authentication can be provided by either an api token or username/password, but not both.
When setting the username and password, the password is saved unencrypted in your local config file.
Optionally, you can omit the password and only provide the username.
You will then be prompted for the password each time.
Examples:
# create a config and set it active
influx config create -a -n $CFG_NAME -u $HOST_URL -t $TOKEN -o $ORG_NAME
@ -103,9 +110,13 @@ https://docs.influxdata.com/influxdb/latest/reference/cli/influx/config/create/
&cli.StringFlag{
Name: "token, t",
Usage: "Auth token to use when communicating with the InfluxDB server",
Required: true,
Destination: &cfg.Token,
},
&cli.StringFlag{
Name: "username-password, p",
Usage: "Username (and optionally password) to use for authentication. Only supported in OSS",
Destination: &userpass,
},
&cli.StringFlag{
Name: "org, o",
Usage: "Default organization name to use in the new config",
@ -118,7 +129,13 @@ https://docs.influxdata.com/influxdb/latest/reference/cli/influx/config/create/
},
),
Action: func(ctx *cli.Context) error {
if cfg.Token != "" && userpass != "" {
return fmt.Errorf("cannot specify `--token` and `--username-password` together, please choose one")
}
client := cmd.Client{CLI: getCLI(ctx)}
if userpass != "" {
return client.CreateWithUserPass(cfg, userpass)
}
return client.Create(cfg)
},
}

View File

@ -10,6 +10,7 @@ import (
"github.com/influxdata/influx-cli/v2/api"
"github.com/influxdata/influx-cli/v2/clients"
"github.com/influxdata/influx-cli/v2/clients/signin"
"github.com/influxdata/influx-cli/v2/config"
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
"github.com/influxdata/influx-cli/v2/pkg/signals"
@ -86,8 +87,14 @@ func newApiClient(ctx *cli.Context, configSvc config.Service, injectToken bool)
}
configParams.Host = parsedHost
if injectToken {
if injectToken && cfg.Token != "" {
configParams.Token = &cfg.Token
} else if cfg.Cookie != "" {
cookie, err := signin.GetCookie(getContext(ctx), configParams, cfg.Cookie)
if err != nil {
return nil, fmt.Errorf("error creating session: %w", err)
}
configParams.Cookie = &cookie
}
if ctx.IsSet(traceIdFlagName) {
configParams.TraceId = api.PtrString(ctx.String(traceIdFlagName))

View File

@ -5,6 +5,8 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"path"
"testing"
"github.com/influxdata/influx-cli/v2/pkg/cli/middleware"
@ -135,6 +137,8 @@ func TestApp_HostSpecificErrors(t *testing.T) {
args := []string{
"influx",
"ping",
"--configs-path",
path.Join(os.TempDir(), "configs"),
"--host",
svr.URL,
}