🚀 Enhancements
fix: Profile and identity flags loading and propagation @aknysh (#1805)
## what- Fixed
--profileCLI flag not loading profile configuration - Fixed
--identityCLI flag not propagating to nested component operations when using--identitywith--profile - CLI flags now correctly take precedence over environment variables
- Profiles specified via
--profileflag load and merge correctly with global configuration - User's explicit
--identitychoice propagates to all nested operations (YAML functions like!terraform.state) when using--identitywith--profile
why
Issue 1: Profile Loading from CLI Flag
When using the --profile CLI flag, profile configuration was not loaded or merged with global configuration, causing authentication failures even when valid profiles were defined. The ATMOS_PROFILE environment variable worked correctly, but the --profile flag did not.
Root Cause: Viper's BindPFlag() creates a binding between Viper key and Cobra flag, but flag values aren't synchronized into Viper immediately when commands execute. Environment variables work because they're read directly into Viper without synchronization delay.
Solution: Implemented dual approach with correct precedence order:
- Check CLI flags FIRST (manual
os.Argsparsing - highest priority) - Fall back to environment variables (direct
os.Getenv()read - lower priority)
This ensures both ATMOS_PROFILE env var and --profile CLI flag work correctly, with CLI flags taking precedence as expected.
Precedence Order (highest to lowest):
- CLI flags (
--profile) - Environment variables (
ATMOS_PROFILE) - Config file
- Defaults
Files Modified:
pkg/config/load.go- AddedparseProfilesFromArgs()andgetProfilesFromFlagsOrEnv()with correct precedencepkg/config/load_profile_test.go(NEW) - Comprehensive test suite with 9 test casespkg/config/load_flags_test.go(NEW) - Tests for precedence and environment variable handling
Issue 2: Identity Flag Not Propagating to Nested Components
After fixing profile loading, a related issue was discovered: when using --profile and --identity flags together, the identity selector still appeared during nested component operations (such as !terraform.state YAML functions).
Root Cause: When YAML template functions need to fetch state from other components, they create component-specific AuthManagers. The original implementation did not inherit the user's explicitly specified identity, always passing empty string which triggered auto-detection. With profiles containing multiple default identities, auto-detection showed the selector prompt.
Solution: Extract the authenticated identity from the parent AuthManager's chain using GetChain() and pass it to nested component AuthManager creation. This ensures the user's --identity choice propagates to all nested operations.
Files Modified:
internal/exec/terraform_nested_auth_helper.go- UpdatedcreateComponentAuthManager()to inherit identity from parent AuthManagerinternal/exec/terraform_nested_auth_helper_test.go- Added comprehensive tests for identity inheritance
Issue 3: Test Isolation (CI Failures)
Initial implementation caused test failures in CI due to Viper caching environment variables.
Root Cause: Viper caches environment variable values on first read. In CI, if ATMOS_PROFILE was set by the environment or previous tests, Viper retained the cached value even after tests cleaned up, causing "profile not found" errors.
Solution: Changed to read ATMOS_PROFILE directly using os.Getenv() instead of Viper. This provides:
- Fresh reads on every call (no caching)
- Proper test isolation (
t.Setenv()cleanup works correctly) - No stale cached values from previous tests
Testing:
All issues were thoroughly tested:
Profile Flag:
- CLI flag syntax:
--profile managers✅ - Environment variable:
ATMOS_PROFILE=managers✅ - Comma-separated profiles:
--profile=managers,staging✅ - CLI flags override environment variables ✅
- Original failing command:
atmos terraform plan --profile managers✅ - All existing tests pass ✅
Identity Flag:
- With
--identityflag: No selector, uses specified identity ✅ - Without
--identityflag: Shows selector once, nested operations inherit selected identity ✅ - Backward compatibility: Auto-detection still works when no parent exists ✅
- YAML functions use inherited identity ✅
Test Isolation:
- Tests pass in CI (no Viper caching issues) ✅
- Proper cleanup with
t.Setenv()✅ - No "profile not found" errors ✅
Success Criteria:
All success criteria met:
- ✅
--profileCLI flag loads profile configuration and merges with global config - ✅
ATMOS_PROFILEenvironment variable continues to work - ✅ CLI flags take precedence over environment variables (correct behavior)
- ✅
--identityflag propagates to nested component operations - ✅ No identity selector appears when identity is explicitly specified
- ✅ User's identity choice is consistent throughout entire command execution
- ✅ Comma-separated profiles work
- ✅ All existing tests continue to pass (including in CI)
- ✅ New tests provide comprehensive coverage
- ✅ Proper test isolation (no cached state issues)
references
- Documentation:
docs/fixes/profile-and-identity-flags-loading.md- Complete technical documentation of all issues and fixes - Related PR: #1786 - Initial work on auth context propagation through nested operations
Summary by CodeRabbit
Release Notes
-
New Features
- Improved CLI flag handling for
--profileand--identityflags with better environment variable support - Identity settings now properly propagate to nested components
- Global authentication configuration is now shared with component-specific auth sections
- Improved CLI flag handling for
-
Bug Fixes
- Fixed profile loading from environment variables and CLI flags in commands with flag parsing disabled
-
Tests
- Added comprehensive test coverage for profile flag parsing and identity inheritance
-
Chores
- Updated dependencies including AWS SDK v2, gRPC, and OpenTelemetry components
✏️ Tip: You can customize this high-level summary in your review settings.