Provide feedback to user in fleetctl login when using env vars (#12371)

Issue reported by @jarodreyes.

The user was not informed that `fleetctl login` was using the PASSWORD
environment variable:

`main`:
```sh
export PASSWORD=wrong

fleetctl login
Log in using the standard Fleet credentials.
Email: a@b.c
Error: Login failed: login received status 401 Authentication failed: Authentication failed
```
And with the changes on this PR:
```
export PASSWORD=wrong

fleetctl login
Log in using the standard Fleet credentials.
Email: a@b.c
Using value of environment variable $PASSWORD as password.
Error: Login failed: login received status 401 Authentication failed: Authentication failed
```
This commit is contained in:
Lucas Manuel Rodriguez
2023-06-16 11:42:04 -03:00
committed by GitHub
parent d676118307
commit bac309c978
2 changed files with 19 additions and 0 deletions
+18
View File
@@ -50,6 +50,16 @@ Trying to login with SSO? First, login to the Fleet UI and retrieve your API tok
return err
}
definedAsEnvOnly := func(flagName, envName string) bool {
cliArgPresent := false
for _, arg := range os.Args {
if arg == flagName {
cliArgPresent = true
}
}
return os.Getenv(envName) != "" && !cliArgPresent
}
// Allow interactive entry to discourage passwords in
// CLI history.
if flEmail == "" {
@@ -59,6 +69,10 @@ Trying to login with SSO? First, login to the Fleet UI and retrieve your API tok
if err != nil {
return fmt.Errorf("error reading email: %w", err)
}
} else {
if definedAsEnvOnly("--email", "EMAIL") {
fmt.Printf("Using value of environment variable $EMAIL as email.\n")
}
}
if flPassword == "" {
fmt.Print("Password: ")
@@ -68,6 +82,10 @@ Trying to login with SSO? First, login to the Fleet UI and retrieve your API tok
}
fmt.Println()
flPassword = string(passBytes)
} else {
if definedAsEnvOnly("--password", "PASSWORD") {
fmt.Printf("Using value of environment variable $PASSWORD as password.\n")
}
}
token, err := fleet.Login(flEmail, flPassword)