-
-
Notifications
You must be signed in to change notification settings - Fork 153
refactor: Migrate atmos auth to Command Registry with StandardParser #1919
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
osterman
wants to merge
30
commits into
main
Choose a base branch
from
osterman/auth-registry-refactor
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 28 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
af9cf3d
Fix markdown code fence in Nerd Fonts installation instructions (#1917)
osterman f2384ad
Address PR review comments for auth registry refactor
osterman 83e8adf
Fix NoOptDefVal preprocessing for identity flag in auth commands
osterman df871c1
Add documentation for --identity flag placement best practices
osterman 03d98f1
Address PR review comments: constants and security annotations
osterman 50dca0a
Add --identity flag documentation to global flags reference
osterman 3f3ea78
Refactor preprocessing architecture with preprocessArgs orchestrator
osterman d7ff75a
Address PR review comments
osterman 9e12803
Add unit tests for cmd/auth package to improve coverage
osterman 3aeeb0a
Fix cross-platform compatibility for exec tests
osterman c66f3e6
Merge origin/main into osterman/auth-registry-refactor
osterman b352c69
Merge origin/main into osterman/auth-registry-refactor
osterman 5546d2b
test: Add tests for --profile flag in auth commands (fixes #1973)
osterman f45fdc9
docs: Reorganize terraform usage examples into distinct sections
osterman 02a76d7
test: Regenerate auth command golden snapshots
osterman 95e491a
Merge remote-tracking branch 'origin/main' into osterman/auth-registr…
osterman ed6c41e
fix: Windows test failures and website build
osterman 2d79d7e
fix: Add IsExperimental method to AuthCommandProvider
osterman caccbca
[autofix.ci] apply automated fixes
autofix-ci[bot] ec40c5a
[autofix.ci] apply automated fixes (attempt 2/3)
autofix-ci[bot] d56631c
[autofix.ci] apply automated fixes (attempt 3/3)
autofix-ci[bot] b2cf1c2
Merge branch 'main' into osterman/auth-registry-refactor
osterman 2e097bb
[autofix.ci] apply automated fixes
autofix-ci[bot] cfb8fc8
fix: Address CodeRabbit review comments for auth-registry-refactor
osterman ce3ae23
Merge branch 'main' into osterman/auth-registry-refactor
osterman 3bc2495
chore: Regenerate golden snapshots for experimental auth command
osterman e6a589c
Merge branch 'main' into osterman/auth-registry-refactor
osterman 3a95d93
Merge branch 'main' into osterman/auth-registry-refactor
osterman 69f1ab0
fix: Address CodeRabbit review findings in auth commands
osterman d8c0c60
fix: Migrate auth commands from legacy u.Print* to ui/data layer and …
osterman 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 was deleted.
Oops, something went wrong.
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,140 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "github.com/spf13/cobra" | ||
| "github.com/spf13/viper" | ||
|
|
||
| "github.com/cloudposse/atmos/cmd/auth/user" | ||
| "github.com/cloudposse/atmos/cmd/internal" | ||
| cfg "github.com/cloudposse/atmos/pkg/config" | ||
| "github.com/cloudposse/atmos/pkg/flags" | ||
| "github.com/cloudposse/atmos/pkg/flags/compat" | ||
| "github.com/cloudposse/atmos/pkg/perf" | ||
| ) | ||
|
|
||
| const ( | ||
| // IdentityFlagName is the name of the identity flag (from pkg/config/const.go). | ||
| IdentityFlagName = cfg.IdentityFlagName | ||
| // IdentityFlagSelectValue is the sentinel value for interactive selection (from pkg/config/const.go). | ||
| IdentityFlagSelectValue = cfg.IdentityFlagSelectValue | ||
| ) | ||
|
|
||
| // authParser handles persistent flags for auth command. | ||
| var authParser *flags.StandardParser | ||
|
|
||
| // authCmd groups authentication-related subcommands. | ||
| var authCmd = &cobra.Command{ | ||
| Use: "auth", | ||
| Short: "Authenticate with cloud providers and identity services.", | ||
| Long: "Obtain, refresh, and configure credentials from external identity providers such as AWS SSO, Vault, or OIDC. Provides the necessary authentication context for tools like Terraform and Helm to interact with cloud infrastructure.", | ||
| FParseErrWhitelist: struct{ UnknownFlags bool }{UnknownFlags: false}, | ||
| ValidArgsFunction: ComponentsArgCompletion, | ||
| } | ||
|
|
||
| func init() { | ||
| defer perf.Track(nil, "auth.init")() | ||
|
|
||
| // Create parser with persistent identity flag. | ||
| authParser = flags.NewStandardParser( | ||
| flags.WithStringFlag(IdentityFlagName, "i", "", "Specify the target identity to assume. Use without value to interactively select."), | ||
| flags.WithNoOptDefVal(IdentityFlagName, IdentityFlagSelectValue), | ||
| flags.WithEnvVars(IdentityFlagName, "ATMOS_IDENTITY"), | ||
| ) | ||
|
|
||
| // Register as persistent flags (inherited by subcommands). | ||
| authParser.RegisterPersistentFlags(authCmd) | ||
|
|
||
| // Bind to Viper for environment variable support. | ||
| if err := authParser.BindToViper(viper.GetViper()); err != nil { | ||
| panic(err) | ||
| } | ||
|
|
||
| // Add identity completion. | ||
| AddIdentityCompletion(authCmd) | ||
|
|
||
| // Add user subcommand (nested command structure). | ||
| authCmd.AddCommand(user.AuthUserCmd) | ||
|
|
||
| // Register this command with the registry. | ||
| internal.Register(&AuthCommandProvider{}) | ||
| } | ||
|
|
||
| // AuthCommandProvider implements the CommandProvider interface. | ||
| type AuthCommandProvider struct{} | ||
|
|
||
| // GetCommand returns the auth command. | ||
| func (a *AuthCommandProvider) GetCommand() *cobra.Command { | ||
| return authCmd | ||
| } | ||
|
|
||
| // GetName returns the command name. | ||
| func (a *AuthCommandProvider) GetName() string { | ||
| return "auth" | ||
| } | ||
|
|
||
| // GetGroup returns the command group for help organization. | ||
| func (a *AuthCommandProvider) GetGroup() string { | ||
| return "Pro Features" | ||
| } | ||
|
|
||
| // GetFlagsBuilder returns the flags builder for this command. | ||
| func (a *AuthCommandProvider) GetFlagsBuilder() flags.Builder { | ||
| return authParser | ||
| } | ||
|
|
||
| // GetPositionalArgsBuilder returns the positional args builder for this command. | ||
| func (a *AuthCommandProvider) GetPositionalArgsBuilder() *flags.PositionalArgsBuilder { | ||
| return nil | ||
| } | ||
|
|
||
| // GetCompatibilityFlags returns compatibility flags for this command. | ||
| func (a *AuthCommandProvider) GetCompatibilityFlags() map[string]compat.CompatibilityFlag { | ||
| return nil | ||
| } | ||
|
|
||
| // GetAliases returns command aliases. | ||
| func (a *AuthCommandProvider) GetAliases() []internal.CommandAlias { | ||
| return nil | ||
| } | ||
|
|
||
| // IsExperimental returns whether this command is experimental. | ||
| // Auth commands are currently experimental as part of Pro Features. | ||
| func (a *AuthCommandProvider) IsExperimental() bool { | ||
| return true | ||
| } | ||
|
|
||
| // GetIdentityFromFlags retrieves the identity flag value, handling the NoOptDefVal quirk. | ||
| // The identity flag uses NoOptDefVal="__SELECT__" which causes Viper to always return "__SELECT__" | ||
| // after parsing, even when the flag wasn't provided. This function checks the actual flag state | ||
| // to return the correct value. | ||
| // | ||
| // Returns: | ||
| // - Empty string if --identity was not provided (use config/env default). | ||
| // - IdentityFlagSelectValue if --identity was provided without a value (user wants selection). | ||
| // - The actual value if --identity=value was provided. | ||
| func GetIdentityFromFlags(cmd *cobra.Command) string { | ||
| defer perf.Track(nil, "auth.GetIdentityFromFlags")() | ||
|
|
||
| flag := cmd.Flags().Lookup(IdentityFlagName) | ||
| if flag == nil { | ||
| // Check persistent flags if not found in local flags. | ||
| flag = cmd.InheritedFlags().Lookup(IdentityFlagName) | ||
| } | ||
|
|
||
| if flag == nil || !flag.Changed { | ||
| // Flag was not explicitly set on command line. | ||
| // Return empty string to signal "use default from config/env". | ||
| return "" | ||
| } | ||
|
|
||
| // Flag was explicitly set - return its value. | ||
| // This could be: | ||
| // - The actual value (--identity=prod or --identity prod). | ||
| // - IdentityFlagSelectValue (--identity without value). | ||
| return flag.Value.String() | ||
| } | ||
|
|
||
| // GetAuthCmd returns the auth command for subcommand registration. | ||
| func GetAuthCmd() *cobra.Command { | ||
| return authCmd | ||
| } | ||
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,135 @@ | ||
| package auth | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/spf13/cobra" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestAuthCommandProvider(t *testing.T) { | ||
| provider := &AuthCommandProvider{} | ||
|
|
||
| assert.Equal(t, "auth", provider.GetName()) | ||
| assert.Equal(t, "Pro Features", provider.GetGroup()) | ||
| assert.NotNil(t, provider.GetCommand()) | ||
| assert.NotNil(t, provider.GetFlagsBuilder()) | ||
| } | ||
|
|
||
| func TestGetIdentityFromFlags(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| setupCmd func(*cobra.Command) | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "flag not set", | ||
| setupCmd: func(cmd *cobra.Command) { | ||
| cmd.Flags().String(IdentityFlagName, "", "identity") | ||
| }, | ||
| expected: "", | ||
| }, | ||
| { | ||
| name: "flag set with value", | ||
| setupCmd: func(cmd *cobra.Command) { | ||
| cmd.Flags().String(IdentityFlagName, "", "identity") | ||
| _ = cmd.Flags().Set(IdentityFlagName, "prod-admin") | ||
| }, | ||
| expected: "prod-admin", | ||
| }, | ||
| { | ||
| name: "flag set without value (NoOptDefVal)", | ||
| setupCmd: func(cmd *cobra.Command) { | ||
| cmd.Flags().String(IdentityFlagName, "", "identity") | ||
| // Simulate NoOptDefVal behavior by setting the select value. | ||
| _ = cmd.Flags().Set(IdentityFlagName, IdentityFlagSelectValue) | ||
| }, | ||
| expected: IdentityFlagSelectValue, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| cmd := &cobra.Command{Use: "test"} | ||
| tt.setupCmd(cmd) | ||
|
|
||
| result := GetIdentityFromFlags(cmd) | ||
| assert.Equal(t, tt.expected, result) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestAuthCommand_Structure(t *testing.T) { | ||
| assert.Equal(t, "auth", authCmd.Use) | ||
| assert.NotEmpty(t, authCmd.Short) | ||
| assert.NotEmpty(t, authCmd.Long) | ||
|
|
||
| // Auth command should have subcommands. | ||
| subcommands := authCmd.Commands() | ||
| assert.Greater(t, len(subcommands), 0) | ||
|
|
||
| // Get subcommand names. | ||
| subcommandNames := make([]string, len(subcommands)) | ||
| for i, cmd := range subcommands { | ||
| subcommandNames[i] = cmd.Name() | ||
| } | ||
|
|
||
| // Verify expected subcommands exist. | ||
| assert.Contains(t, subcommandNames, "login") | ||
| assert.Contains(t, subcommandNames, "logout") | ||
| assert.Contains(t, subcommandNames, "whoami") | ||
| assert.Contains(t, subcommandNames, "list") | ||
| assert.Contains(t, subcommandNames, "env") | ||
| assert.Contains(t, subcommandNames, "shell") | ||
| assert.Contains(t, subcommandNames, "exec") | ||
| assert.Contains(t, subcommandNames, "console") | ||
| assert.Contains(t, subcommandNames, "validate") | ||
| } | ||
|
|
||
| func TestAuthCommand_IdentityFlag(t *testing.T) { | ||
| // Check identity flag is registered as persistent flag. | ||
| identityFlag := authCmd.PersistentFlags().Lookup(IdentityFlagName) | ||
| assert.NotNil(t, identityFlag) | ||
| assert.Equal(t, "i", identityFlag.Shorthand) | ||
| } | ||
|
|
||
| func TestIdentityFlagName(t *testing.T) { | ||
| assert.Equal(t, "identity", IdentityFlagName) | ||
| } | ||
|
|
||
| func TestIdentityFlagSelectValue(t *testing.T) { | ||
| assert.Equal(t, "__SELECT__", IdentityFlagSelectValue) | ||
| } | ||
|
|
||
| func TestAuthCommand_HasRunE(t *testing.T) { | ||
| // Auth parent command may or may not have RunE. | ||
| // Just verify the command is properly configured. | ||
| assert.Equal(t, "auth", authCmd.Use) | ||
| } | ||
|
|
||
| func TestAuthCommand_PersistentPreRunE(t *testing.T) { | ||
| // Auth command should have a PersistentPreRunE for flag setup. | ||
| // The actual function is set, so verify it's not nil. | ||
| // Note: In the actual implementation, this might be set to nil or a function. | ||
| // Just verify the command is properly initialized. | ||
| assert.NotNil(t, authCmd) | ||
| } | ||
|
|
||
| func TestGetIdentityFromFlags_NoFlag(t *testing.T) { | ||
| // Test with a command that doesn't have the identity flag at all. | ||
| cmd := &cobra.Command{Use: "test"} | ||
|
|
||
| // Should return empty string without panicking. | ||
| result := GetIdentityFromFlags(cmd) | ||
| assert.Equal(t, "", result) | ||
| } | ||
|
|
||
| func TestAddIdentityCompletion(t *testing.T) { | ||
| cmd := &cobra.Command{Use: "test"} | ||
| cmd.Flags().String(IdentityFlagName, "", "identity") | ||
|
|
||
| // Should not panic. | ||
| assert.NotPanics(t, func() { | ||
| AddIdentityCompletion(cmd) | ||
| }) | ||
| } |
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.
🧩 Analysis chain
🏁 Script executed:
Repository: cloudposse/atmos
Length of output: 14180
🏁 Script executed:
Repository: cloudposse/atmos
Length of output: 5327
🏁 Script executed:
Repository: cloudposse/atmos
Length of output: 103
🏁 Script executed:
Repository: cloudposse/atmos
Length of output: 221
🏁 Script executed:
Repository: cloudposse/atmos
Length of output: 2949
Remove undocumented
IDENTITYenv var fallback across three locations.Guidelines require
ATMOS_prefix for Atmos-specific configuration variables.IDENTITYis not a standard/canonical environment variable likeSHELLorGITHUB_TOKEN, so it should not appear as a fallback. Documentation only referencesATMOS_IDENTITY.Update these three locations to remove the
IDENTITYfallback:cmd/auth/auth.go:40cmd/terraform/flags.go(identity flag)pkg/flags/global_registry.go(global identity definition)Changes needed
🤖 Prompt for AI Agents