-
Notifications
You must be signed in to change notification settings - Fork 154
Auth: resolve positional arg as profile name first #4840
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
simonfaltum
wants to merge
11
commits into
main
Choose a base branch
from
simonfaltum/auth-positional-profile
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 all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
37bf6b0
Add shared resolvePositionalArg for auth commands
simonfaltum 179429a
Refactor resolve tests to table-driven, tighten looksLikeHost check
simonfaltum 2fb8c94
auth login: treat positional arg as profile name first
simonfaltum 796f42e
auth token: use resolvePositionalArg for better error messages
simonfaltum c4eae90
auth logout: use shared resolvePositionalArg, remove local --profile
simonfaltum 3ceec68
Reword login error message, co-locate resolveHostToProfile tests
simonfaltum 9d83701
Fix whitespace and lint in logout.go and resolve.go
simonfaltum 1785a0a
Fix review findings for auth positional profile resolution
simonfaltum eff50c4
Add NEXT_CHANGELOG entry for auth positional profile resolution
simonfaltum 62ee5d5
Merge branch 'main' into simonfaltum/auth-positional-profile
simonfaltum cd7540a
Use [PROFILE] in usage strings, keep host fallback silent
simonfaltum 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
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
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 |
|---|---|---|
|
|
@@ -66,26 +66,37 @@ the profile is an error. | |
| } | ||
|
|
||
| var force bool | ||
| var profileName string | ||
| var deleteProfile bool | ||
| cmd.Flags().BoolVar(&force, "force", false, "Skip confirmation prompt") | ||
| cmd.Flags().StringVar(&profileName, "profile", "", "The profile to log out of") | ||
| cmd.Flags().BoolVar(&deleteProfile, "delete", false, "Delete the profile from the config file") | ||
|
|
||
| cmd.RunE = func(cmd *cobra.Command, args []string) error { | ||
| ctx := cmd.Context() | ||
| profiler := profile.DefaultProfiler | ||
|
|
||
| profileName := "" | ||
| profileFlag := cmd.Flag("profile") | ||
| if profileFlag != nil { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this guard? IIUC, profile is a global flag and having it to nil would highlight a real bug that might be worth panicking over. |
||
| profileName = profileFlag.Value.String() | ||
| } | ||
|
|
||
| // Resolve the positional argument to a profile name. | ||
| if profileName != "" && len(args) == 1 { | ||
| if profileFlag != nil && profileFlag.Changed && len(args) == 1 { | ||
| return errors.New("providing both --profile and a positional argument is not supported") | ||
| } | ||
| if profileName == "" && len(args) == 1 { | ||
| resolved, err := resolveLogoutArg(ctx, args[0], profiler) | ||
| resolvedProfile, resolvedHost, err := resolvePositionalArg(ctx, args[0], profiler) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| profileName = resolved | ||
| if resolvedProfile != "" { | ||
| profileName = resolvedProfile | ||
| } else { | ||
| profileName, err = resolveHostToProfile(ctx, resolvedHost, profiler) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if profileName == "" { | ||
|
|
@@ -289,55 +300,3 @@ func hostCacheKeyAndMatchFn(p profile.Profile) (string, profile.ProfileMatchFunc | |
|
|
||
| return host, profile.WithHost(host) | ||
| } | ||
|
|
||
| // resolveLogoutArg resolves a positional argument to a profile name. It first | ||
| // tries to match the argument as a profile name, then as a host URL. If the | ||
| // host matches multiple profiles in a non-interactive context, it returns an | ||
| // error listing the matching profile names. | ||
| func resolveLogoutArg(ctx context.Context, arg string, profiler profile.Profiler) (string, error) { | ||
| // Try as profile name first. | ||
| candidateProfile, err := loadProfileByName(ctx, arg, profiler) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| if candidateProfile != nil { | ||
| return arg, nil | ||
| } | ||
|
|
||
| // Try as host URL. | ||
| canonicalHost := (&config.Config{Host: arg}).CanonicalHostName() | ||
| hostProfiles, err := profiler.LoadProfiles(ctx, profile.WithHost(canonicalHost)) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| switch len(hostProfiles) { | ||
| case 1: | ||
| return hostProfiles[0].Name, nil | ||
| case 0: | ||
| allProfiles, err := profiler.LoadProfiles(ctx, profile.MatchAllProfiles) | ||
| if err != nil { | ||
| return "", fmt.Errorf("no profile found matching %q", arg) | ||
| } | ||
| names := strings.Join(allProfiles.Names(), ", ") | ||
| return "", fmt.Errorf("no profile found matching %q. Available profiles: %s", arg, names) | ||
| default: | ||
| // Multiple profiles match the host. | ||
| if cmdio.IsPromptSupported(ctx) { | ||
| selected, err := profile.SelectProfile(ctx, profile.SelectConfig{ | ||
| Label: fmt.Sprintf("Multiple profiles found for %q. Select one to log out of", arg), | ||
| Profiles: hostProfiles, | ||
| StartInSearchMode: len(hostProfiles) > 5, | ||
| ActiveTemplate: `▸ {{.PaddedName | bold}}{{if .AccountID}} (account: {{.AccountID}}){{else}} ({{.Host}}){{end}}`, | ||
| InactiveTemplate: ` {{.PaddedName}}{{if .AccountID}} (account: {{.AccountID | faint}}){{else}} ({{.Host | faint}}){{end}}`, | ||
| SelectedTemplate: `{{ "Selected profile" | faint }}: {{ .Name | bold }}`, | ||
| }) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return selected, nil | ||
| } | ||
| names := strings.Join(hostProfiles.Names(), ", ") | ||
| return "", fmt.Errorf("multiple profiles found matching host %q: %s. Please specify the profile name directly", arg, names) | ||
| } | ||
| } | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strconv" | ||
| "strings" | ||
|
|
||
| "github.com/databricks/cli/libs/cmdio" | ||
| "github.com/databricks/cli/libs/databrickscfg/profile" | ||
| "github.com/databricks/databricks-sdk-go/config" | ||
| ) | ||
|
|
||
| // looksLikeHost returns true if the argument looks like a host URL rather than | ||
| // a profile name. Profile names are short identifiers (e.g., "logfood", | ||
| // "DEFAULT"), while host URLs contain dots or start with "http". | ||
| func looksLikeHost(arg string) bool { | ||
| if strings.Contains(arg, ".") || strings.HasPrefix(arg, "http://") || strings.HasPrefix(arg, "https://") { | ||
| return true | ||
| } | ||
| // Match host:port pattern without dots or scheme (e.g., localhost:8080). | ||
| if i := strings.LastIndex(arg, ":"); i > 0 { | ||
| if _, err := strconv.Atoi(arg[i+1:]); err == nil { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| // resolvePositionalArg resolves a positional argument to either a profile name | ||
| // or a host. It tries the argument as a profile name first. If no profile | ||
| // matches and the argument looks like a host URL, it returns it as a host. If | ||
| // no profile matches and the argument does not look like a host, it returns an | ||
| // error. | ||
| func resolvePositionalArg(ctx context.Context, arg string, profiler profile.Profiler) (profileName, host string, err error) { | ||
| candidateProfile, err := loadProfileByName(ctx, arg, profiler) | ||
| if err != nil { | ||
| return "", "", err | ||
| } | ||
| if candidateProfile != nil { | ||
| return arg, "", nil | ||
| } | ||
|
|
||
| if looksLikeHost(arg) { | ||
| return "", arg, nil | ||
| } | ||
|
|
||
| return "", "", fmt.Errorf("no profile named %q found", arg) | ||
| } | ||
|
|
||
| // resolveHostToProfile resolves a host URL to a profile name. If multiple | ||
| // profiles match the host, it prompts the user to select one (or errors in | ||
| // non-interactive mode). If no profiles match, it returns an error. | ||
| func resolveHostToProfile(ctx context.Context, host string, profiler profile.Profiler) (string, error) { | ||
| canonicalHost := (&config.Config{Host: host}).CanonicalHostName() | ||
| hostProfiles, err := profiler.LoadProfiles(ctx, profile.WithHost(canonicalHost)) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| switch len(hostProfiles) { | ||
| case 1: | ||
| return hostProfiles[0].Name, nil | ||
| case 0: | ||
| allProfiles, err := profiler.LoadProfiles(ctx, profile.MatchAllProfiles) | ||
| if err != nil { | ||
| return "", fmt.Errorf("no profile found matching host %q", host) | ||
| } | ||
| names := strings.Join(allProfiles.Names(), ", ") | ||
| return "", fmt.Errorf("no profile found matching host %q. Available profiles: %s", host, names) | ||
| default: | ||
| if cmdio.IsPromptSupported(ctx) { | ||
| selected, err := profile.SelectProfile(ctx, profile.SelectConfig{ | ||
| Label: fmt.Sprintf("Multiple profiles found for %q. Select one to use", host), | ||
| Profiles: hostProfiles, | ||
| StartInSearchMode: len(hostProfiles) > 5, | ||
| ActiveTemplate: "▸ {{.PaddedName | bold}}{{if .AccountID}} (account: {{.AccountID}}){{else}} ({{.Host}}){{end}}", | ||
| InactiveTemplate: " {{.PaddedName}}{{if .AccountID}} (account: {{.AccountID | faint}}){{else}} ({{.Host | faint}}){{end}}", | ||
| SelectedTemplate: `{{ "Selected profile" | faint }}: {{ .Name | bold }}`, | ||
| }) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return selected, nil | ||
| } | ||
| names := strings.Join(hostProfiles.Names(), ", ") | ||
| return "", fmt.Errorf("multiple profiles found matching host %q: %s. Please specify the profile name directly", host, names) | ||
| } | ||
| } |
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.
Should we also error if there is a positional argument and the
--profileflag?