Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth: init recognizes --access-token #1395

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion commands/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,14 @@ To create new contexts, see the help for `+"`"+`doctl auth init`+"`"+`.`, Writer
// XDG_CONFIG_HOME is not set, use $HOME/.config. On Windows use %APPDATA%/doctl/config.
func RunAuthInit(retrieveUserTokenFunc func() (string, error)) func(c *CmdConfig) error {
return func(c *CmdConfig) error {
token := c.getContextAccessToken()
token := viper.GetString(doctl.ArgAccessToken)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this has some unintended consequences. As the top level access-token item in the config file is bound to the flag value, there is no way for us to determine if this is passed on the command line or sourced from the config file for the default context.

So this breaks initializing a new non-default context without passing the flag. If a default context is already configured, it's token just gets used:

$ rm ~/.config/doctl/config.yaml 
$ ./builds/doctl auth init
Please authenticate doctl for use with your DigitalOcean account. You can generate a token in the control panel at https://cloud.digitalocean.com/account/api/tokens

❯ Enter your access token:  ●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●●

Validating token... ✔

$ ./builds/doctl auth init --context test
Using token for context test

Validating token... ✔

I'm not sure if there is a simple solution here. This may need to wait until we can do deeper changes to how contexts work.


// if --access-token is passed, set the context.
if token != "" {
c.setContextAccessToken(token)
}

token = c.getContextAccessToken()
context := strings.ToLower(Context)
if context == "" {
context = strings.ToLower(viper.GetString("context"))
Expand Down
29 changes: 29 additions & 0 deletions integration/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,35 @@ context: default

expect.Contains(string(fileBytes), "access-token: some-magic-token")
})

it("validates and saves an existing token non-interactively to the --context specified", func() {
var testConfigBytes = []byte(`access-token: first-token
context: default
`)

tmpDir := t.TempDir()
testConfig := filepath.Join(tmpDir, "test-config.yml")
expect.NoError(ioutil.WriteFile(testConfig, testConfigBytes, 0644))

cmd := exec.Command(builtBinaryPath,
"-u", server.URL,
"--config", testConfig,
"auth",
"init",
"--access-token", "some-magic-token",
"--context", "new-context",
"--token-validation-server", server.URL,
)

_, err := cmd.CombinedOutput()
expect.NoError(err)

fileBytes, err := ioutil.ReadFile(testConfig)
expect.NoError(err)

expect.Contains(string(fileBytes), "access-token: some-magic-token")
expect.Contains(string(fileBytes), "new-context: some-magic-token")
})
})

when("no custom config is provided", func() {
Expand Down
Loading