-
Notifications
You must be signed in to change notification settings - Fork 17
feat(auth): Add --save-to-file flag for explicit credential file fallback #654
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
Open
mgajda
wants to merge
2
commits into
UpCloudLtd:main
Choose a base branch
from
mgajda:feat/credential-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,62 @@ | ||
| package clierrors | ||
|
|
||
| import "fmt" | ||
| import ( | ||
| "strings" | ||
| ) | ||
|
|
||
| var _ ClientError = MissingCredentialsError{} | ||
|
|
||
| type MissingCredentialsError struct { | ||
| ConfigFile string | ||
| ServiceName string | ||
| ConfigFile string | ||
| ServiceName string | ||
| KeyringError error // New field to track keyring issues | ||
| } | ||
|
|
||
| func (err MissingCredentialsError) ErrorCode() int { | ||
| return MissingCredentials | ||
| } | ||
|
|
||
| func (err MissingCredentialsError) Error() string { | ||
| return fmt.Sprintf("user credentials not found, these must be set in config file (%s) or via environment variables or in a keyring (%s)", err.ConfigFile, err.ServiceName) | ||
| var msg strings.Builder | ||
|
|
||
| msg.WriteString("Authentication credentials not found.\n\n") | ||
|
|
||
| // Check if this is due to keyring issues | ||
| if err.KeyringError != nil && isKeyringError(err.KeyringError) { | ||
| msg.WriteString("System keyring is not accessible. ") | ||
| msg.WriteString("You can:\n") | ||
| msg.WriteString(" 1. Save token to file: upctl account login --with-token --save-to-file\n") | ||
| msg.WriteString(" 2. Set environment variable: export UPCLOUD_TOKEN=your-token\n") | ||
| msg.WriteString(" 3. Add to config file: " + err.ConfigFile + "\n\n") | ||
| msg.WriteString("Original error: " + err.KeyringError.Error()) | ||
| } else { | ||
| msg.WriteString("Please configure authentication using one of these methods:\n") | ||
| msg.WriteString(" 1. Login with token: upctl account login --with-token\n") | ||
| msg.WriteString(" 2. Set environment variable: export UPCLOUD_TOKEN=your-token\n") | ||
| msg.WriteString(" 3. Add to config file: " + err.ConfigFile + "\n") | ||
|
|
||
| if err.ServiceName != "" { | ||
| msg.WriteString("\nNote: System keyring service '" + err.ServiceName + "' may not be available.\n") | ||
| msg.WriteString("Use --save-to-file flag with login command if keyring fails.") | ||
| } | ||
| } | ||
|
|
||
| return msg.String() | ||
| } | ||
|
|
||
| func isKeyringError(err error) bool { | ||
| if err == nil { | ||
| return false | ||
| } | ||
| errStr := strings.ToLower(err.Error()) | ||
| keyringIndicators := []string{ | ||
| "keyring", "secret", "dbus", "collection", | ||
| "unlock", "gnome-keyring", "kwallet", | ||
| } | ||
| for _, indicator := range keyringIndicators { | ||
| if strings.Contains(errStr, indicator) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's look into how similar tools handle this situation and find out if there are some common practices 👀
For example, GitHub CLI, which does authentication very nicely, seems to (based on documentation) fallback to saving the credentials to configuration file, but not sure if this requires using
--insecure-storageflag or is the default when keyring access fails (gh auth login docs) 🤔Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took example of AWS CLI, which uses a separate credentials and configuration files. This allows all to read configuration, but only the user can read credentials.
Saving without explicit override means defaulting to potentially insecure behaviour, which should be discouraged.