From ad4c5709cc71e849d1990bc20879820f64bb2aaf Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 12:50:17 -0600 Subject: [PATCH 01/25] refactor(terraform): extract the component provider runtime # Conflicts: # internal/exec/terraform_execute_helpers_exec.go --- docs/prd/native-ci/overview.md | 2 +- internal/exec/atmos.go | 2 +- internal/exec/clean_adapter_funcs.go | 5 +- internal/exec/deferred_auth.go | 9 +- internal/exec/deferred_cache_test.go | 7 +- internal/exec/deferred_contexts.go | 24 +- internal/exec/describe_affected.go | 40 +- internal/exec/describe_affected_helpers.go | 89 ++ internal/exec/describe_component.go | 18 +- .../describe_component_auth_override_test.go | 4 +- .../exec/describe_dependents_index_test.go | 12 +- internal/exec/describe_stacks.go | 35 +- .../describe_stacks_component_processor.go | 9 +- internal/exec/helmfile.go | 21 +- internal/exec/helmfile_test.go | 25 +- ...o => mock_terraform_output_getter_test.go} | 7 +- internal/exec/mock_terraform_provider_test.go | 156 +++ ...go => mock_terraform_state_getter_test.go} | 7 +- internal/exec/packer.go | 21 +- internal/exec/packer_test.go | 17 +- internal/exec/path_utils.go | 84 +- internal/exec/path_utils_test.go | 150 -- internal/exec/shell_retry_policy.go | 78 -- internal/exec/shell_retry_policy_test.go | 91 -- internal/exec/shell_utils.go | 272 ++-- internal/exec/starlark_policy_test.go | 21 - internal/exec/template_funcs_component.go | 11 +- internal/exec/terraform.go | 882 +++++++----- .../exec/terraform_adapter_boundary_test.go | 322 +++++ ...est.go => terraform_affected_host_test.go} | 94 -- internal/exec/terraform_auth_context_test.go | 72 - ...erraform_auth_integration_coverage_test.go | 23 +- .../exec/terraform_component_provider_test.go | 153 ++ internal/exec/terraform_execute_helpers.go | 1226 ----------------- .../terraform_execute_helpers_auth_test.go | 20 +- .../exec/terraform_execute_helpers_exec.go | 897 ------------ .../exec/terraform_generate_backend_test.go | 260 ---- .../exec/terraform_generate_varfile_test.go | 234 +--- .../exec/terraform_lookup_helpers_test.go | 19 +- internal/exec/terraform_mocks_test.go | 134 +- .../terraform_output_deferred_auth_test.go | 2 +- .../terraform_plan_diff_preparation_test.go | 61 - internal/exec/terraform_plan_diff_test.go | 4 +- ...y_test.go => terraform_query_auth_test.go} | 150 -- .../terraform_reference_cache_auth_test.go | 22 - .../exec/terraform_reference_cache_test.go | 29 - .../exec/terraform_secret_resolution_test.go | 35 - internal/exec/terraform_verify_plan_test.go | 36 - internal/exec/testmain_test.go | 56 +- internal/exec/yaml_func_aws.go | 5 +- internal/exec/yaml_func_terraform_output.go | 49 +- ..._func_terraform_output_authcontext_test.go | 3 +- internal/exec/yaml_func_terraform_state.go | 29 +- pkg/component/execution_options.go | 235 ++++ pkg/component/execution_options_test.go | 67 + pkg/component/host_contracts.go | 126 ++ pkg/component/provider.go | 6 +- .../component/terraform/affected.go | 159 +-- .../terraform/affected_filter_test.go | 2 +- pkg/component/terraform/affected_test.go | 335 +++++ .../component/terraform/all.go | 56 +- .../terraform/all_closure_scope_test.go | 20 +- .../all_dependency_bootstrap_test.go | 4 +- .../terraform/all_output_cache_test.go | 6 +- .../component/terraform/all_simple_test.go | 4 +- .../component/terraform/all_test.go | 64 +- .../component/terraform/auth_context.go | 2 +- pkg/component/terraform/auth_context_test.go | 273 ++++ .../component/terraform/cli_args_utils.go | 5 +- .../terraform/cli_args_utils_test.go | 2 +- .../component/terraform/configure_rc_test.go | 2 +- .../component/terraform/declared_var_type.go | 6 +- .../terraform/declared_var_type_test.go | 12 +- .../component/terraform/detection.go | 8 +- .../component/terraform/detection_test.go | 4 +- .../exec_metadata_buffer_scoping_test.go | 4 +- .../terraform/exec_metadata_flags_test.go | 2 +- .../component/terraform/exec_metadata_test.go | 2 +- pkg/component/terraform/execute.go | 442 ++++++ pkg/component/terraform/execute_command.go | 465 +++++++ .../terraform/execute_environment.go | 414 ++++++ .../terraform/execute_helpers_args.go | 15 +- .../terraform/execute_helpers_args_test.go | 6 +- .../execute_helpers_coverage_test.go | 26 +- .../execute_helpers_pipeline_test.go | 127 +- .../execute_helpers_recovery_test.go | 211 ++- .../terraform/execute_helpers_test.go | 43 +- .../execute_helpers_workspace_test.go | 117 +- .../execute_helpers_workspace_varfile_test.go | 4 +- pkg/component/terraform/execute_init.go | 594 ++++++++ pkg/component/terraform/execute_pipeline.go | 351 +++++ pkg/component/terraform/execute_prepare.go | 572 ++++++++ .../execute_single_invocation_test.go | 4 +- .../component/terraform/exitcode_test.go | 2 +- .../component/terraform/generate_backend.go | 15 +- .../terraform/generate_backend_test.go | 424 ++++++ .../component/terraform/generate_backends.go | 54 +- .../component/terraform/generate_context.go | 8 +- .../component/terraform/generate_planfile.go | 40 +- .../terraform/generate_required_providers.go | 10 +- .../generate_required_providers_test.go | 4 +- .../component/terraform/generate_varfile.go | 33 +- .../terraform/generate_varfile_test.go | 426 ++++++ .../component/terraform/generate_varfiles.go | 34 +- .../component/terraform/lookup_options.go | 2 +- .../component/terraform/managed_cache_test.go | 2 +- .../terraform/mirror_targets_test.go | 4 +- .../terraform/mock_component_provider_test.go | 156 +++ .../component/terraform/mock_query_test.go | 8 +- pkg/component/terraform/mock_runtime_test.go | 351 +++++ .../component/terraform/mocks.go | 70 +- pkg/component/terraform/mocks_test.go | 241 ++++ .../terraform/nested_auth_cache_test.go | 24 +- .../component/terraform/nested_auth_helper.go | 129 +- .../terraform/nested_auth_helper_test.go | 93 +- .../output_authcontext_wrapper_test.go | 22 +- .../component/terraform/output_getter.go | 29 +- .../component/terraform/output_getter_test.go | 2 +- .../component/terraform/output_utils.go | 95 +- .../component/terraform/output_utils_test.go | 38 +- .../component/terraform/plan_diff.go | 88 +- .../terraform/plan_diff_preparation.go | 25 +- .../terraform/plan_diff_preparation_test.go | 61 +- .../component/terraform/plugin_cache_test.go | 2 +- .../component/terraform/policy.go | 23 +- .../component/terraform/policy_test.go | 66 +- pkg/component/terraform/provider.go | 394 ++++++ pkg/component/terraform/provider_test.go | 506 +++++++ .../terraform/providers_aliases_test.go | 6 +- .../component/terraform/query.go | 29 +- .../component/terraform/rc_merge_test.go | 2 +- .../terraform/remote_state_backend_utils.go | 2 +- .../remote_state_backend_utils_test.go | 2 +- pkg/component/terraform/runtime_host_test.go | 10 + .../component/terraform/shell.go | 57 +- .../component/terraform/shell_test.go | 113 +- .../component/terraform/state_getter.go | 15 +- .../terraform/state_output_coverage_test.go | 28 +- .../component/terraform/state_utils.go | 43 +- .../store_identity_isolation_test.go | 24 +- .../component/terraform/streaming_ui.go | 23 +- .../component/terraform/streaming_ui_test.go | 40 +- .../component/terraform/test_fixtures.go | 19 +- pkg/component/terraform/testmain_test.go | 356 +++++ .../component/terraform/utils.go | 150 +- .../component/terraform/utils_test.go | 99 +- .../component/terraform/verify_plan.go | 22 +- 147 files changed, 9691 insertions(+), 5661 deletions(-) rename internal/exec/{mock_terraform_output_getter.go => mock_terraform_output_getter_test.go} (88%) create mode 100644 internal/exec/mock_terraform_provider_test.go rename internal/exec/{mock_terraform_state_getter.go => mock_terraform_state_getter_test.go} (88%) delete mode 100644 internal/exec/shell_retry_policy.go delete mode 100644 internal/exec/shell_retry_policy_test.go create mode 100644 internal/exec/terraform_adapter_boundary_test.go rename internal/exec/{terraform_affected_test.go => terraform_affected_host_test.go} (83%) delete mode 100644 internal/exec/terraform_auth_context_test.go create mode 100644 internal/exec/terraform_component_provider_test.go delete mode 100644 internal/exec/terraform_execute_helpers.go delete mode 100644 internal/exec/terraform_execute_helpers_exec.go delete mode 100644 internal/exec/terraform_plan_diff_preparation_test.go rename internal/exec/{terraform_query_test.go => terraform_query_auth_test.go} (68%) create mode 100644 pkg/component/execution_options.go create mode 100644 pkg/component/execution_options_test.go create mode 100644 pkg/component/host_contracts.go rename internal/exec/terraform_affected.go => pkg/component/terraform/affected.go (56%) rename internal/exec/terraform_affected_filter_test.go => pkg/component/terraform/affected_filter_test.go (99%) create mode 100644 pkg/component/terraform/affected_test.go rename internal/exec/terraform_all.go => pkg/component/terraform/all.go (87%) rename internal/exec/terraform_all_closure_scope_test.go => pkg/component/terraform/all_closure_scope_test.go (87%) rename internal/exec/terraform_all_dependency_bootstrap_test.go => pkg/component/terraform/all_dependency_bootstrap_test.go (97%) rename internal/exec/terraform_all_output_cache_test.go => pkg/component/terraform/all_output_cache_test.go (96%) rename internal/exec/terraform_all_simple_test.go => pkg/component/terraform/all_simple_test.go (93%) rename internal/exec/terraform_all_test.go => pkg/component/terraform/all_test.go (93%) rename internal/exec/terraform_auth_context.go => pkg/component/terraform/auth_context.go (98%) create mode 100644 pkg/component/terraform/auth_context_test.go rename internal/exec/terraform_cli_args_utils.go => pkg/component/terraform/cli_args_utils.go (99%) rename internal/exec/terraform_cli_args_utils_test.go => pkg/component/terraform/cli_args_utils_test.go (99%) rename internal/exec/terraform_configure_rc_test.go => pkg/component/terraform/configure_rc_test.go (99%) rename internal/exec/terraform_declared_var_type.go => pkg/component/terraform/declared_var_type.go (87%) rename internal/exec/terraform_declared_var_type_test.go => pkg/component/terraform/declared_var_type_test.go (89%) rename internal/exec/terraform_detection.go => pkg/component/terraform/detection.go (97%) rename internal/exec/terraform_detection_test.go => pkg/component/terraform/detection_test.go (99%) rename internal/exec/terraform_exec_metadata_buffer_scoping_test.go => pkg/component/terraform/exec_metadata_buffer_scoping_test.go (96%) rename internal/exec/terraform_exec_metadata_flags_test.go => pkg/component/terraform/exec_metadata_flags_test.go (99%) rename internal/exec/terraform_exec_metadata_test.go => pkg/component/terraform/exec_metadata_test.go (99%) create mode 100644 pkg/component/terraform/execute.go create mode 100644 pkg/component/terraform/execute_command.go create mode 100644 pkg/component/terraform/execute_environment.go rename internal/exec/terraform_execute_helpers_args.go => pkg/component/terraform/execute_helpers_args.go (97%) rename internal/exec/terraform_execute_helpers_args_test.go => pkg/component/terraform/execute_helpers_args_test.go (99%) rename internal/exec/terraform_execute_helpers_coverage_test.go => pkg/component/terraform/execute_helpers_coverage_test.go (96%) rename internal/exec/terraform_execute_helpers_pipeline_test.go => pkg/component/terraform/execute_helpers_pipeline_test.go (85%) rename internal/exec/terraform_execute_helpers_recovery_test.go => pkg/component/terraform/execute_helpers_recovery_test.go (58%) rename internal/exec/terraform_execute_helpers_test.go => pkg/component/terraform/execute_helpers_test.go (95%) rename internal/exec/terraform_execute_helpers_workspace_test.go => pkg/component/terraform/execute_helpers_workspace_test.go (75%) rename internal/exec/terraform_execute_helpers_workspace_varfile_test.go => pkg/component/terraform/execute_helpers_workspace_varfile_test.go (96%) create mode 100644 pkg/component/terraform/execute_init.go create mode 100644 pkg/component/terraform/execute_pipeline.go create mode 100644 pkg/component/terraform/execute_prepare.go rename internal/exec/terraform_execute_single_invocation_test.go => pkg/component/terraform/execute_single_invocation_test.go (97%) rename internal/exec/terraform_exitcode_test.go => pkg/component/terraform/exitcode_test.go (99%) rename internal/exec/terraform_generate_backend.go => pkg/component/terraform/generate_backend.go (86%) create mode 100644 pkg/component/terraform/generate_backend_test.go rename internal/exec/terraform_generate_backends.go => pkg/component/terraform/generate_backends.go (87%) rename internal/exec/terraform_generate_context.go => pkg/component/terraform/generate_context.go (74%) rename internal/exec/terraform_generate_planfile.go => pkg/component/terraform/generate_planfile.go (80%) rename internal/exec/terraform_generate_required_providers.go => pkg/component/terraform/generate_required_providers.go (90%) rename internal/exec/terraform_generate_required_providers_test.go => pkg/component/terraform/generate_required_providers_test.go (98%) rename internal/exec/terraform_generate_varfile.go => pkg/component/terraform/generate_varfile.go (84%) create mode 100644 pkg/component/terraform/generate_varfile_test.go rename internal/exec/terraform_generate_varfiles.go => pkg/component/terraform/generate_varfiles.go (88%) rename internal/exec/terraform_lookup_options.go => pkg/component/terraform/lookup_options.go (93%) rename internal/exec/terraform_managed_cache_test.go => pkg/component/terraform/managed_cache_test.go (99%) rename internal/exec/terraform_mirror_targets_test.go => pkg/component/terraform/mirror_targets_test.go (94%) create mode 100644 pkg/component/terraform/mock_component_provider_test.go rename internal/exec/mock_terraform_query.go => pkg/component/terraform/mock_query_test.go (91%) create mode 100644 pkg/component/terraform/mock_runtime_test.go rename internal/exec/terraform_mocks.go => pkg/component/terraform/mocks.go (84%) create mode 100644 pkg/component/terraform/mocks_test.go rename internal/exec/terraform_nested_auth_cache_test.go => pkg/component/terraform/nested_auth_cache_test.go (92%) rename internal/exec/terraform_nested_auth_helper.go => pkg/component/terraform/nested_auth_helper.go (79%) rename internal/exec/terraform_nested_auth_helper_test.go => pkg/component/terraform/nested_auth_helper_test.go (80%) rename internal/exec/terraform_output_authcontext_wrapper_test.go => pkg/component/terraform/output_authcontext_wrapper_test.go (88%) rename internal/exec/terraform_output_getter.go => pkg/component/terraform/output_getter.go (88%) rename internal/exec/terraform_output_getter_test.go => pkg/component/terraform/output_getter_test.go (99%) rename internal/exec/terraform_output_utils.go => pkg/component/terraform/output_utils.go (69%) rename internal/exec/terraform_output_utils_test.go => pkg/component/terraform/output_utils_test.go (87%) rename internal/exec/terraform_plan_diff.go => pkg/component/terraform/plan_diff.go (70%) rename internal/exec/terraform_plan_diff_preparation.go => pkg/component/terraform/plan_diff_preparation.go (70%) rename internal/exec/terraform_plan_preservation_test.go => pkg/component/terraform/plan_diff_preparation_test.go (51%) rename internal/exec/terraform_plugin_cache_test.go => pkg/component/terraform/plugin_cache_test.go (99%) rename internal/exec/terraform_policy.go => pkg/component/terraform/policy.go (86%) rename internal/exec/terraform_policy_test.go => pkg/component/terraform/policy_test.go (74%) create mode 100644 pkg/component/terraform/provider.go create mode 100644 pkg/component/terraform/provider_test.go rename internal/exec/terraform_providers_aliases_test.go => pkg/component/terraform/providers_aliases_test.go (95%) rename internal/exec/terraform_query.go => pkg/component/terraform/query.go (79%) rename internal/exec/terraform_rc_merge_test.go => pkg/component/terraform/rc_merge_test.go (99%) rename internal/exec/terraform_remote_state_backend_utils.go => pkg/component/terraform/remote_state_backend_utils.go (97%) rename internal/exec/terraform_remote_state_backend_utils_test.go => pkg/component/terraform/remote_state_backend_utils_test.go (99%) create mode 100644 pkg/component/terraform/runtime_host_test.go rename internal/exec/terraform_shell.go => pkg/component/terraform/shell.go (82%) rename internal/exec/terraform_shell_test.go => pkg/component/terraform/shell_test.go (86%) rename internal/exec/terraform_state_getter.go => pkg/component/terraform/state_getter.go (70%) rename internal/exec/terraform_state_output_coverage_test.go => pkg/component/terraform/state_output_coverage_test.go (91%) rename internal/exec/terraform_state_utils.go => pkg/component/terraform/state_utils.go (87%) rename internal/exec/terraform_store_identity_isolation_test.go => pkg/component/terraform/store_identity_isolation_test.go (94%) rename internal/exec/terraform_streaming_ui.go => pkg/component/terraform/streaming_ui.go (90%) rename internal/exec/terraform_streaming_ui_test.go => pkg/component/terraform/streaming_ui_test.go (90%) rename internal/exec/terraform_test_fixtures.go => pkg/component/terraform/test_fixtures.go (61%) create mode 100644 pkg/component/terraform/testmain_test.go rename internal/exec/terraform_utils.go => pkg/component/terraform/utils.go (57%) rename internal/exec/terraform_utils_test.go => pkg/component/terraform/utils_test.go (92%) rename internal/exec/terraform_verify_plan.go => pkg/component/terraform/verify_plan.go (84%) diff --git a/docs/prd/native-ci/overview.md b/docs/prd/native-ci/overview.md index 01e0d349472..4a7f218b0c7 100644 --- a/docs/prd/native-ci/overview.md +++ b/docs/prd/native-ci/overview.md @@ -190,7 +190,7 @@ Users currently using the GitHub Actions can migrate incrementally: - [Existing CI Detection](../../../pkg/telemetry/ci.go) - Detects 24+ CI providers - [Lifecycle Hooks](../../../pkg/hooks/) - Hook system for terraform events -- [Plan-Diff](../../../internal/exec/terraform_plan_diff.go) - Semantic plan comparison (implemented) +- [Plan-Diff](../../../pkg/component/terraform/plan_diff.go) - Semantic plan comparison (implemented) - [Store Registry](../../../pkg/store/registry.go) - Pattern for planfile stores - Terraform Output Package (`pkg/terraform/output/`) - Output formatting and `FlattenMap()` for CI export - [tfcmt](https://github.com/suzuki-shunsuke/tfcmt) - Inspiration for PR comments diff --git a/internal/exec/atmos.go b/internal/exec/atmos.go index 4718f1b46b3..1bb79ac25df 100644 --- a/internal/exec/atmos.go +++ b/internal/exec/atmos.go @@ -181,7 +181,7 @@ func executeTerraformUISelection(atmosConfig *schema.AtmosConfiguration, command // Route it directly to ExecuteTerraformShell to avoid ExecuteTerraform passing // it to the terraform executable. if subcommand == "shell" { - return ExecuteTerraformShell(shellOptionsForUI(component, stack), atmosConfig) + return terraformExecutor.ExecuteTerraformShell(shellOptionsForUI(component, stack), atmosConfig) } configAndStacksInfo := schema.ConfigAndStacksInfo{} diff --git a/internal/exec/clean_adapter_funcs.go b/internal/exec/clean_adapter_funcs.go index 363ffe79e7e..bbf1d51f088 100644 --- a/internal/exec/clean_adapter_funcs.go +++ b/internal/exec/clean_adapter_funcs.go @@ -5,6 +5,7 @@ import ( "path/filepath" errUtils "github.com/cloudposse/atmos/errors" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" "github.com/cloudposse/atmos/pkg/perf" "github.com/cloudposse/atmos/pkg/schema" tfclean "github.com/cloudposse/atmos/pkg/terraform/clean" @@ -85,8 +86,8 @@ func ConstructTerraformComponentVarfilePath(atmosConfig *schema.AtmosConfigurati func ComputeTerraformSecretVarEnv(info *schema.ConfigAndStacksInfo) ([]string, error) { defer perf.Track(nil, "exec.ComputeTerraformSecretVarEnv")() - computeTerraformSecretVarKeys(info) - return secretVarEnv(info) + terraformcomponent.ComputeTerraformSecretVarKeys(info) + return terraformcomponent.SecretVarEnv(info) } // ConstructTerraformComponentPlanfileNameForClean exports the planfile name constructor for clean. diff --git a/internal/exec/deferred_auth.go b/internal/exec/deferred_auth.go index 2d58c419433..ac14502e111 100644 --- a/internal/exec/deferred_auth.go +++ b/internal/exec/deferred_auth.go @@ -5,6 +5,7 @@ import ( "github.com/cloudposse/atmos/pkg/auth" authdeferred "github.com/cloudposse/atmos/pkg/auth/deferred" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/deferred" "github.com/cloudposse/atmos/pkg/schema" @@ -46,8 +47,8 @@ func deferredTargetAuthAndCache(ac *schema.AtmosConfiguration, component, stack } cache := stackdeferred.CacheFor(ac, info) if info.AuthDisabled { - manager := cache.LoadOrStore("auth.disabled", &authContextWrapper{stackInfo: info}) - return manager.(*authContextWrapper), cache, nil + manager := cache.LoadOrStore("auth.disabled", &terraformcomponent.AuthContextWrapper{StackInfo: info}) + return manager.(*terraformcomponent.AuthContextWrapper), cache, nil } manager, _ := info.AuthManager.(auth.AuthManager) return manager, cache, nil @@ -55,12 +56,12 @@ func deferredTargetAuthAndCache(ac *schema.AtmosConfiguration, component, stack func inheritDeferredAuth(section map[string]any, parents []auth.AuthManager) map[string]any { targetAuth, _ := section[cfg.AuthSectionName].(map[string]any) - if hasDefaultIdentity(targetAuth) || len(parents) == 0 || parents[0] == nil { + if terraformcomponent.HasDefaultIdentity(targetAuth) || len(parents) == 0 || parents[0] == nil { return section } if parent := parents[0].GetStackInfo(); parent != nil { parentAuth, _ := parent.ComponentSection[cfg.AuthSectionName].(map[string]any) - if hasDefaultIdentity(parentAuth) { + if terraformcomponent.HasDefaultIdentity(parentAuth) { section = maps.Clone(section) section[cfg.AuthSectionName] = parentAuth } diff --git a/internal/exec/deferred_cache_test.go b/internal/exec/deferred_cache_test.go index d6d184812ab..6a31ef44671 100644 --- a/internal/exec/deferred_cache_test.go +++ b/internal/exec/deferred_cache_test.go @@ -12,6 +12,7 @@ import ( "github.com/cloudposse/atmos/pkg/auth" authdeferred "github.com/cloudposse/atmos/pkg/auth/deferred" "github.com/cloudposse/atmos/pkg/auth/types" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/schema" ) @@ -65,10 +66,10 @@ func TestDeferredTargetDoesNotReuseCallerAuthContext(t *testing.T) { require.Same(t, prior, resolvedTargetAuthContext(ac, nil, prior, false), "eager execution retains its fallback") authdeferred.ConfigureAuth(ac, "") require.Nil(t, resolvedTargetAuthContext(ac, nil, prior, false)) - manager := &authContextWrapper{stackInfo: &schema.ConfigAndStacksInfo{}} + manager := &terraformcomponent.AuthContextWrapper{StackInfo: &schema.ConfigAndStacksInfo{}} require.Nil(t, resolvedTargetAuthContext(ac, manager, prior, false)) resolved := &schema.AuthContext{AWS: &schema.AWSAuthContext{Profile: "resolved-account"}} - manager.stackInfo.AuthContext = resolved + manager.StackInfo.AuthContext = resolved require.Same(t, resolved, resolvedTargetAuthContext(ac, manager, prior, false)) require.Nil(t, resolvedTargetAuthContext(ac, manager, prior, true), "disabled auth must discard all Atmos contexts") } @@ -171,7 +172,7 @@ func TestDeferredStateCacheSkipsFailuresAndSeparatesIdentities(t *testing.T) { ac, factory, path := setupDeferredCacheTarget(t) factory.EXPECT().Create(ac, gomock.Any(), "dev").Return(nil, nil).Times(2) load := func(profile string, skip bool) (any, error) { - var parent auth.AuthManager = &authContextWrapper{stackInfo: deferredCacheParent(profile)} + var parent auth.AuthManager = &terraformcomponent.AuthContextWrapper{StackInfo: deferredCacheParent(profile)} return GetTerraformState(ac, "!terraform.state", "dev", "target", "id", skip, nil, parent) } _, err := load("first", false) diff --git a/internal/exec/deferred_contexts.go b/internal/exec/deferred_contexts.go index c1f90d1a7a5..0bb6b098ab0 100644 --- a/internal/exec/deferred_contexts.go +++ b/internal/exec/deferred_contexts.go @@ -3,31 +3,21 @@ package exec import ( "fmt" + "github.com/cloudposse/atmos/pkg/component" "github.com/cloudposse/atmos/pkg/deferred" m "github.com/cloudposse/atmos/pkg/merge" "github.com/cloudposse/atmos/pkg/perf" "github.com/cloudposse/atmos/pkg/schema" ) -// ComponentDeferredContexts holds the per-section *merge.DeferredMergeContext produced while -// merging a single component's configuration, keyed by section name (vars, settings, env, auth, -// providers, required_providers, hooks, test, generate — see the cfg.*SectionName constants used -// as keys). Each context tracks the deferred YAML functions (!template, !terraform.output, -// !labels, etc.) collected for that section during the raw structural merge; a later, -// per-invocation stage resolves them with a real YAMLFunctionProcessor and deep-merges the result -// against any concrete override at the same path. -type ComponentDeferredContexts map[string]*m.DeferredMergeContext +// ComponentDeferredContexts holds deferred sections for one component. +type ComponentDeferredContexts = component.ComponentDeferredContexts -// StackComponentDeferredContexts holds, for a single stack, every component's -// ComponentDeferredContexts keyed by [componentType][component] (e.g. -// ["terraform"]["vpc"]). It is the shape ProcessStackConfig produces per stack. -type StackComponentDeferredContexts map[string]map[string]ComponentDeferredContexts +// StackComponentDeferredContexts holds deferred sections by type and component. +type StackComponentDeferredContexts = component.StackComponentDeferredContexts -// AllStacksDeferredContexts holds every stack's deferred contexts keyed by -// [stack][componentType][component]. This is the shape threaded from -// ProcessYAMLConfigFiles through FindStacksMap to the per-invocation Stage 3 -// resolver (see resolveDeferredYamlFunctions). -type AllStacksDeferredContexts map[string]StackComponentDeferredContexts +// AllStacksDeferredContexts holds deferred sections by stack. +type AllStacksDeferredContexts = component.AllStacksDeferredContexts // cloneComponentSectionWithOverrides returns a fresh copy of raw (a component's complete merged // section, straight from FindStacksMap) with overrides layered on top. Used by the terraform diff --git a/internal/exec/describe_affected.go b/internal/exec/describe_affected.go index e863493bb41..e4af04b59b2 100644 --- a/internal/exec/describe_affected.go +++ b/internal/exec/describe_affected.go @@ -13,6 +13,7 @@ import ( "github.com/cloudposse/atmos/internal/tui/templates/term" "github.com/cloudposse/atmos/pkg/auth" "github.com/cloudposse/atmos/pkg/ci" + "github.com/cloudposse/atmos/pkg/component" cfg "github.com/cloudposse/atmos/pkg/config" flagsPkg "github.com/cloudposse/atmos/pkg/flags" atmosgit "github.com/cloudposse/atmos/pkg/git" @@ -38,44 +39,7 @@ var ErrRepoPathConflict = errors.New("if the '--repo-path' flag is specified, th type DescribeAffectedExecCreator func(atmosConfig *schema.AtmosConfiguration) DescribeAffectedExec -type DescribeAffectedCmdArgs struct { - CLIConfig *schema.AtmosConfiguration - Base string // Unified base commit (ref or SHA). Takes precedence over Ref/SHA. - CloneTargetRef bool - Format string - IncludeDependents bool - IncludeSettings bool - IncludeSpaceliftAdminStacks bool - OutputFile string - GithubOutputFile string // Output file for $GITHUB_OUTPUT format (key=value). - Ref string - RepoPath string - SHA string - SSHKeyPath string - SSHKeyPassword string - Verbose bool - Upload bool - Stack string - Query string - ProcessTemplates bool - ProcessYamlFunctions bool - Skip []string - ExcludeLocked bool - Tags []string // Keep only components whose `metadata.tags` contain any of these (from --tags). - LabelsRaw string // Raw --labels value (comma-separated key=value pairs). - Labels map[string]string // Parsed --labels: keep only components whose `metadata.labels` contain all of these. - TagsEnvVar string // Environment variable that supplied Tags (for example ATMOS_TAGS); empty when Tags came from the command line or are unset. - LabelsEnvVar string // Environment variable that supplied Labels; empty when Labels came from the command line or are unset. - Flatten bool // Lift every dependent into the top-level list as `affected: dependent` (from --flatten); requires IncludeDependents. - FlattenEnvVar string // Environment variable that supplied Flatten; empty when it came from the command line or is unset. - AuthManager auth.AuthManager // Optional: Auth manager for credential management (from --identity flag). - AuthDisabled bool // True when --identity=false (or alias) explicitly disables authentication; routes stack resolution to ExecuteDescribeStacksWithAuthDisabled. - HeadSHAOverride string // PR head SHA from CI event payload, used for upload correlation with Atmos Pro. - CIEventType string // CI event type (e.g., "pull_request", "push") for upload validation. - TargetBranch string // PR target branch (e.g., "main") used to auto-fetch when refs are missing locally. - ErrorMode string // How to handle recoverable errors: "strict" (default), "warn", or "silent". - Cmd *cobra.Command // The invoking Cobra command, used to derive Flags for the exec-metadata sync capture (proexec.FlagsFromCommand). -} +type DescribeAffectedCmdArgs component.DescribeAffectedCmdArgs //go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=mock_$GOFILE -package=$GOPACKAGE type DescribeAffectedExec interface { diff --git a/internal/exec/describe_affected_helpers.go b/internal/exec/describe_affected_helpers.go index f9131c0621f..c35dce955f7 100644 --- a/internal/exec/describe_affected_helpers.go +++ b/internal/exec/describe_affected_helpers.go @@ -9,6 +9,7 @@ import ( "github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing/transport/ssh" + errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/auth" g "github.com/cloudposse/atmos/pkg/git" log "github.com/cloudposse/atmos/pkg/logger" @@ -437,3 +438,91 @@ func ExecuteDescribeAffectedWithTargetRepoPathWithOptions( return affected, localRepoHead, remoteRepoHead, localRepoInfo.RepoUrl, nil } + +// GetAffectedComponents retrieves the list of affected components based on the +// provided arguments, dispatching to the right git-target resolution mode +// (repo path / clone-and-diff / local ref checkout). Exported so other +// affected-target dispatchers (e.g. `atmos terraform lint --affected`) reuse +// this switch instead of re-declaring it against the same three +// ExecuteDescribeAffectedWith* functions. +func GetAffectedComponents(args *DescribeAffectedCmdArgs) ([]schema.Affected, error) { + defer perf.Track(nil, "exec.GetAffectedComponents")() + + return GetAffectedComponentsWithOptions(args, DescribeStacksErrorOptions{}) +} + +// GetAffectedComponentsWithOptions is GetAffectedComponents plus opt-in graceful degradation +// for recoverable per-value YAML function errors (see DescribeStacksErrorOptions), dispatching +// to the *WithOptions variant of whichever git-target resolution mode applies. +// +//nolint:gocritic // Preserve the existing value-based options API shared by describe entry points. +func GetAffectedComponentsWithOptions(args *DescribeAffectedCmdArgs, errOptions DescribeStacksErrorOptions) ([]schema.Affected, error) { + defer perf.Track(nil, "exec.GetAffectedComponentsWithOptions")() + + if args == nil { + return nil, errUtils.ErrNilParam + } + return dispatchAffectedComponentsWithOptions(args, errOptions) +} + +// dispatchAffectedComponentsWithOptions is GetAffectedComponentsWithOptions's git-target +// dispatch switch, split out so the exported entry point stays a short nil-check + delegate. +// +//nolint:gocritic // Forward the value-based options consistently with the describe entry points below. +func dispatchAffectedComponentsWithOptions(args *DescribeAffectedCmdArgs, errOptions DescribeStacksErrorOptions) ([]schema.Affected, error) { + switch { + case args.RepoPath != "": + affectedList, _, _, _, err := ExecuteDescribeAffectedWithTargetRepoPathWithOptions( + args.CLIConfig, + args.RepoPath, + args.IncludeSpaceliftAdminStacks, + args.IncludeSettings, + args.Stack, + args.ProcessTemplates, + args.ProcessYamlFunctions, + args.Skip, + args.affectedFilter(), + args.AuthManager, + args.AuthDisabled, + errOptions, + ) + return affectedList, err + case args.CloneTargetRef: + affectedList, _, _, _, err := ExecuteDescribeAffectedWithTargetRefCloneWithOptions( + args.CLIConfig, + args.Ref, + args.SHA, + args.SSHKeyPath, + args.SSHKeyPassword, + args.IncludeSpaceliftAdminStacks, + args.IncludeSettings, + args.Stack, + args.ProcessTemplates, + args.ProcessYamlFunctions, + args.Skip, + args.affectedFilter(), + args.AuthManager, + args.AuthDisabled, + errOptions, + ) + return affectedList, err + default: + affectedList, _, _, _, err := ExecuteDescribeAffectedWithTargetRefCheckoutWithOptions( + args.CLIConfig, + args.Ref, + args.SHA, + args.TargetBranch, + args.IncludeSpaceliftAdminStacks, + args.IncludeSettings, + args.Stack, + args.ProcessTemplates, + args.ProcessYamlFunctions, + args.Skip, + args.affectedFilter(), + args.AuthManager, + args.AuthDisabled, + errOptions, + ) + return affectedList, err + } +} diff --git a/internal/exec/describe_component.go b/internal/exec/describe_component.go index aeca408839d..8d221fcd961 100644 --- a/internal/exec/describe_component.go +++ b/internal/exec/describe_component.go @@ -11,6 +11,7 @@ import ( errUtils "github.com/cloudposse/atmos/errors" tuiTerm "github.com/cloudposse/atmos/internal/tui/templates/term" "github.com/cloudposse/atmos/pkg/auth" + "github.com/cloudposse/atmos/pkg/component" "github.com/cloudposse/atmos/pkg/component/typedetect" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/data" @@ -292,22 +293,7 @@ type DescribeComponentResult struct { } // ExecuteDescribeComponentParams contains parameters for ExecuteDescribeComponent. -type ExecuteDescribeComponentParams struct { - // ResolveSecrets retrieves actual secrets for internal value-producing callers. - // The default preserves credential-free, masked inspection. - ResolveSecrets bool - AtmosConfig *schema.AtmosConfiguration // Optional: Use provided config instead of initializing new one. - Component string - Stack string - ComponentType string // Optional: if set, only try this component type. - ProcessTemplates bool - ProcessYamlFunctions bool - UseMocks bool - Skip []string - AuthManager auth.AuthManager - AuthDisabled bool - ErrorOptions DescribeStacksErrorOptions -} +type ExecuteDescribeComponentParams = component.ExecuteDescribeComponentParams // ExecuteDescribeComponent describes component config. func ExecuteDescribeComponent(params *ExecuteDescribeComponentParams) (map[string]any, error) { diff --git a/internal/exec/describe_component_auth_override_test.go b/internal/exec/describe_component_auth_override_test.go index f301ed8c1b3..23301bdb3e6 100644 --- a/internal/exec/describe_component_auth_override_test.go +++ b/internal/exec/describe_component_auth_override_test.go @@ -137,7 +137,7 @@ func TestResolveAuthManagerForNestedComponent(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - resolvedAuthMgr, err := resolveAuthManagerForNestedComponent( + resolvedAuthMgr, err := terraformExecutor.ResolveAuthManagerForNestedComponent( &atmosConfig, tt.component, "test", @@ -252,7 +252,7 @@ func TestAuthOverrideErrorHandling(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - _, err := resolveAuthManagerForNestedComponent( + _, err := terraformExecutor.ResolveAuthManagerForNestedComponent( &atmosConfig, tt.component, tt.stack, diff --git a/internal/exec/describe_dependents_index_test.go b/internal/exec/describe_dependents_index_test.go index cd52ebd9af6..0c688155b37 100644 --- a/internal/exec/describe_dependents_index_test.go +++ b/internal/exec/describe_dependents_index_test.go @@ -39,17 +39,25 @@ func (f *fakeComponentProvider) GenerateArtifacts(_ *comp.ExecutionContext) erro func (f *fakeComponentProvider) GetAvailableCommands() []string { return nil } // registerFakeComponentTypes registers a fake provider for each given type in -// the shared component registry, and restores an empty registry via +// the shared component registry, and restores the original providers via // t.Cleanup. Callers must not call t.Parallel(): the registry is shared, // mutable, global state, so any two callers running concurrently would race. // Every caller is marked //nolint:paralleltest for this reason. func registerFakeComponentTypes(t *testing.T, types ...string) { t.Helper() + original := comp.ListProviders() + t.Cleanup(func() { + comp.Reset() + for _, providers := range original { + for _, provider := range providers { + require.NoError(t, comp.Register(provider)) + } + } + }) comp.Reset() for _, typ := range types { require.NoError(t, comp.Register(&fakeComponentProvider{componentType: typ})) } - t.Cleanup(comp.Reset) } func TestBuildDependencyIndex_Empty(t *testing.T) { diff --git a/internal/exec/describe_stacks.go b/internal/exec/describe_stacks.go index 086aa08fdc5..9d386a73412 100644 --- a/internal/exec/describe_stacks.go +++ b/internal/exec/describe_stacks.go @@ -8,6 +8,7 @@ import ( "github.com/cloudposse/atmos/internal/tui/templates/term" "github.com/cloudposse/atmos/pkg/auth" authdeferred "github.com/cloudposse/atmos/pkg/auth/deferred" + "github.com/cloudposse/atmos/pkg/component" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/degradation" "github.com/cloudposse/atmos/pkg/pager" @@ -140,7 +141,7 @@ func (d *describeStacksExec) Execute(atmosConfig *schema.AtmosConfiguration, arg // OnErrorMode selects how ExecuteDescribeStacksWithOptions handles a recoverable per-value // YAML function error (e.g. a Terraform backend that has not been provisioned yet). -type OnErrorMode string +type OnErrorMode = component.OnErrorMode const ( // OnErrorStrict fails the whole describe-stacks call on the first error. This is the @@ -156,37 +157,7 @@ const ( // DescribeStacksErrorOptions configures how ExecuteDescribeStacksWithOptions handles // recoverable per-value YAML function errors. The zero value is OnErrorStrict, matching // ExecuteDescribeStacks's historical fail-fast behavior. -type DescribeStacksErrorOptions struct { - // StaticOnly inspects merged configuration without auth, expression evaluation, workspace - // synthesis, or evaluated-map cache reuse. Stack name templates allow only Go's pure built-ins. - StaticOnly bool - // EvaluationPaths optionally narrows list columns to fields within sections. - EvaluationPaths [][]string - OnError OnErrorMode - OnWarning func(DegradationWarning) - // OnProgress, when non-nil, is called once per stack file immediately before it's - // processed — stackFile is the file name, index is 0-based, total is the number of - // stack files describe-stacks will visit this call (both counted before filtering by - // includeEmptyStacks, so total is a stable upper bound even though some files are - // skipped). Bundled onto this struct (rather than a new parameter) purely for plumbing - // convenience — it shares the same threading path as OnError/OnWarning. Optional; a nil - // OnProgress is a no-op, matching every existing caller's behavior exactly. - OnProgress func(stackFile string, index, total int) - // StrictAuth, when true, keeps a component-specific auth-manager construction failure - // fatal even under OnErrorWarn (see describeStacksProcessor.resolveComponentAuthManager) -- - // only the recoverable per-value YAML-function errors (e.g. !terraform.state hitting a - // not-yet-provisioned backend) degrade. The zero value (false) preserves the original - // list/describe --error-mode=warn behavior, where a component whose own declared identity - // can't be resolved gracefully falls back to the parent AuthManager instead of aborting -- - // appropriate for merely listing/describing stacks, but not for a Terraform preflight - // about to run real commands with whatever credentials it resolves. - StrictAuth bool - // ExtraComponentTypes names additional component kinds, such as custom kinds declared - // by custom commands, to process after the built-in kinds. Each kind must already be - // registered with the component registry for its stack section to be available. The - // zero value leaves the described component kinds exactly as before. - ExtraComponentTypes []string -} +type DescribeStacksErrorOptions = component.DescribeStacksErrorOptions // ResolveErrorMode determines the effective --error-mode value using the documented // configuration precedence (CLI flags → ENV vars → config files → defaults, see CLAUDE.md): diff --git a/internal/exec/describe_stacks_component_processor.go b/internal/exec/describe_stacks_component_processor.go index faadc3c5e6a..fd7d43078ce 100644 --- a/internal/exec/describe_stacks_component_processor.go +++ b/internal/exec/describe_stacks_component_processor.go @@ -13,6 +13,7 @@ import ( errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/auth" authdeferred "github.com/cloudposse/atmos/pkg/auth/deferred" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/deferred" "github.com/cloudposse/atmos/pkg/function/literalsource" @@ -207,7 +208,7 @@ func newDescribeStacksProcessorWithAuthDisabled( //nolint:revive // argument-lim skip: skip, authManager: authManager, finalStacksMap: make(map[string]any), - componentAuthResolver: createComponentAuthManager, + componentAuthResolver: terraformcomponent.CreateComponentAuthManager, authManagerCache: make(map[string]auth.AuthManager), } } @@ -254,7 +255,7 @@ func (p *describeStacksProcessor) resolveComponentAuthManager( return componentAuthManager, nil } authSection, hasAuth := componentSection[cfg.AuthSectionName].(map[string]any) - if !hasAuth || !hasDefaultIdentity(authSection) { + if !hasAuth || !terraformcomponent.HasDefaultIdentity(authSection) { return componentAuthManager, nil } @@ -270,7 +271,7 @@ func (p *describeStacksProcessor) resolveComponentAuthManager( resolver := p.componentAuthResolver if resolver == nil { - resolver = createComponentAuthManager + resolver = terraformcomponent.CreateComponentAuthManager } resolved, createErr := resolver(p.atmosConfig, componentSection, componentName, stackName, p.authManager) if createErr != nil { @@ -299,7 +300,7 @@ func (p *describeStacksProcessor) resolveComponentAuthManager( // processor and the nested terraform.state path key per-component AuthManagers identically and cannot // drift. See buildComponentAuthCacheKey in terraform_nested_auth_helper.go. func (p *describeStacksProcessor) componentAuthCacheKey(authSection map[string]any) (string, bool) { - return buildComponentAuthCacheKey(p.authManager, authSection) + return terraformcomponent.BuildComponentAuthCacheKey(p.authManager, authSection) } // cacheComponentAuthManager stores a resolved manager, lazily creating the map so struct-literal diff --git a/internal/exec/helmfile.go b/internal/exec/helmfile.go index a0c3acd7a47..46cef15b3bc 100644 --- a/internal/exec/helmfile.go +++ b/internal/exec/helmfile.go @@ -14,6 +14,7 @@ import ( errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/auth" "github.com/cloudposse/atmos/pkg/component" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/dependencies" helmplugin "github.com/cloudposse/atmos/pkg/helm/plugin" @@ -136,7 +137,7 @@ func ExecuteHelmfile(info schema.ConfigAndStacksInfo) (resultErr error) { ) } - authManager, err := SetupComponentAuthForCLI(&atmosConfig, &info) + authManager, err := terraformExecutor.SetupComponentAuthForCLI(&atmosConfig, &info) if err != nil { return err } @@ -511,10 +512,10 @@ func ExecuteHelmfile(info schema.ConfigAndStacksInfo) (resultErr error) { // toolchain-installed helmfile (under the install path, not the system PATH) // is found — mirroring the `version` subcommand above. Falls back to the bare // command name when no toolchain dependency provides it. - err = executeHelmfileCommandWithRetry(&atmosConfig, &info, tenv, retryExecParams{ - allArgsAndFlags: allArgsAndFlags, - componentPath: componentPath, - envVars: envVars, + err = executeHelmfileCommandWithRetry(&atmosConfig, &info, tenv, terraformcomponent.RetryExecParams{ + AllArgsAndFlags: allArgsAndFlags, + ComponentPath: componentPath, + EnvVars: envVars, }, shellOpts...) if info.NodeHooks != nil { if afterErr := info.NodeHooks.After(context.Background(), &info, stdoutBuf.String()+stderrBuf.String(), err); afterErr != nil && err == nil { @@ -542,10 +543,10 @@ func executeHelmfileCommandWithRetry( atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, tenv *dependencies.ToolchainEnvironment, - params retryExecParams, + params terraformcomponent.RetryExecParams, shellOpts ...ShellCommandOption, ) error { - return ExecuteShellCommandWithRetry( + return terraformcomponent.ExecuteShellCommandWithRetry( atmosConfig, info, info.SubCommand, @@ -553,9 +554,9 @@ func executeHelmfileCommandWithRetry( return ExecuteShellCommand( *atmosConfig, tenv.Resolve(info.Command), - params.allArgsAndFlags, - params.componentPath, - params.envVars, + params.AllArgsAndFlags, + params.ComponentPath, + params.EnvVars, info.DryRun, info.RedirectStdErr, o..., diff --git a/internal/exec/helmfile_test.go b/internal/exec/helmfile_test.go index ffec82d8b67..cfa2fe10cad 100644 --- a/internal/exec/helmfile_test.go +++ b/internal/exec/helmfile_test.go @@ -14,6 +14,7 @@ import ( "go.uber.org/mock/gomock" errUtils "github.com/cloudposse/atmos/errors" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" "github.com/cloudposse/atmos/pkg/schema" "github.com/cloudposse/atmos/pkg/terminal" "github.com/cloudposse/atmos/tests" @@ -370,10 +371,10 @@ func TestExecuteHelmfileCommandWithRetry_MatchingError_Retries(t *testing.T) { } atmosConfig := schema.AtmosConfiguration{} - err = executeHelmfileCommandWithRetry(&atmosConfig, info, nil, retryExecParams{ - allArgsAndFlags: []string{"sync"}, - componentPath: t.TempDir(), - envVars: envVars, + err = executeHelmfileCommandWithRetry(&atmosConfig, info, nil, terraformcomponent.RetryExecParams{ + AllArgsAndFlags: []string{"sync"}, + ComponentPath: t.TempDir(), + EnvVars: envVars, }) require.Error(t, err, "all 3 attempts fail in this fixture, so the final error must propagate") @@ -406,10 +407,10 @@ func TestExecuteHelmfileCommandWithRetry_NonMatchingError_FailsFast(t *testing.T } atmosConfig := schema.AtmosConfiguration{} - err = executeHelmfileCommandWithRetry(&atmosConfig, info, nil, retryExecParams{ - allArgsAndFlags: []string{"sync"}, - componentPath: t.TempDir(), - envVars: envVars, + err = executeHelmfileCommandWithRetry(&atmosConfig, info, nil, terraformcomponent.RetryExecParams{ + AllArgsAndFlags: []string{"sync"}, + ComponentPath: t.TempDir(), + EnvVars: envVars, }) require.Error(t, err) @@ -444,10 +445,10 @@ func TestExecuteHelmfileCommandWithRetry_ComposesWithNodeHooksCapture(t *testing shellOpts := []ShellCommandOption{WithStderrCapture(&nodeHooksStderr)} atmosConfig := schema.AtmosConfiguration{} - err = executeHelmfileCommandWithRetry(&atmosConfig, info, nil, retryExecParams{ - allArgsAndFlags: []string{"sync"}, - componentPath: t.TempDir(), - envVars: envVars, + err = executeHelmfileCommandWithRetry(&atmosConfig, info, nil, terraformcomponent.RetryExecParams{ + AllArgsAndFlags: []string{"sync"}, + ComponentPath: t.TempDir(), + EnvVars: envVars, }, shellOpts...) require.Error(t, err) assert.Contains(t, nodeHooksStderr.String(), "502 Bad Gateway", diff --git a/internal/exec/mock_terraform_output_getter.go b/internal/exec/mock_terraform_output_getter_test.go similarity index 88% rename from internal/exec/mock_terraform_output_getter.go rename to internal/exec/mock_terraform_output_getter_test.go index 5ca1b508e0d..625c04d6610 100644 --- a/internal/exec/mock_terraform_output_getter.go +++ b/internal/exec/mock_terraform_output_getter_test.go @@ -1,9 +1,9 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: terraform_output_getter.go +// Source: output_getter.go // // Generated by this command: // -// mockgen -source=terraform_output_getter.go -destination=mock_terraform_output_getter.go -package=exec +// mockgen -source=output_getter.go -destination=../../../internal/exec/mock_terraform_output_getter_test.go -package=exec // // Package exec is a generated GoMock package. @@ -12,6 +12,7 @@ package exec import ( reflect "reflect" + terraform "github.com/cloudposse/atmos/pkg/component/terraform" schema "github.com/cloudposse/atmos/pkg/schema" gomock "go.uber.org/mock/gomock" ) @@ -41,7 +42,7 @@ func (m *MockTerraformOutputGetter) EXPECT() *MockTerraformOutputGetterMockRecor } // GetOutput mocks base method. -func (m *MockTerraformOutputGetter) GetOutput(atmosConfig *schema.AtmosConfiguration, stack, component, output string, skipCache bool, authContext *schema.AuthContext, authManager any, options ...TerraformLookupOptions) (any, bool, error) { +func (m *MockTerraformOutputGetter) GetOutput(atmosConfig *schema.AtmosConfiguration, stack, component, output string, skipCache bool, authContext *schema.AuthContext, authManager any, options ...terraform.TerraformLookupOptions) (any, bool, error) { m.ctrl.T.Helper() varargs := []any{atmosConfig, stack, component, output, skipCache, authContext, authManager} for _, a := range options { diff --git a/internal/exec/mock_terraform_provider_test.go b/internal/exec/mock_terraform_provider_test.go new file mode 100644 index 00000000000..4ef64cbfda3 --- /dev/null +++ b/internal/exec/mock_terraform_provider_test.go @@ -0,0 +1,156 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: pkg/component/provider.go +// +// Generated by this command: +// +// mockgen -source=pkg/component/provider.go -destination=internal/exec/mock_terraform_provider_test.go -package=exec +// + +// Package exec is a generated GoMock package. +package exec + +import ( + context "context" + reflect "reflect" + + component "github.com/cloudposse/atmos/pkg/component" + schema "github.com/cloudposse/atmos/pkg/schema" + gomock "go.uber.org/mock/gomock" +) + +// MockComponentProvider is a mock of ComponentProvider interface. +type MockComponentProvider struct { + ctrl *gomock.Controller + recorder *MockComponentProviderMockRecorder + isgomock struct{} +} + +// MockComponentProviderMockRecorder is the mock recorder for MockComponentProvider. +type MockComponentProviderMockRecorder struct { + mock *MockComponentProvider +} + +// NewMockComponentProvider creates a new mock instance. +func NewMockComponentProvider(ctrl *gomock.Controller) *MockComponentProvider { + mock := &MockComponentProvider{ctrl: ctrl} + mock.recorder = &MockComponentProviderMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockComponentProvider) EXPECT() *MockComponentProviderMockRecorder { + return m.recorder +} + +// Execute mocks base method. +func (m *MockComponentProvider) Execute(ctx *component.ExecutionContext) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Execute", ctx) + ret0, _ := ret[0].(error) + return ret0 +} + +// Execute indicates an expected call of Execute. +func (mr *MockComponentProviderMockRecorder) Execute(ctx any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Execute", reflect.TypeOf((*MockComponentProvider)(nil).Execute), ctx) +} + +// GenerateArtifacts mocks base method. +func (m *MockComponentProvider) GenerateArtifacts(ctx *component.ExecutionContext) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GenerateArtifacts", ctx) + ret0, _ := ret[0].(error) + return ret0 +} + +// GenerateArtifacts indicates an expected call of GenerateArtifacts. +func (mr *MockComponentProviderMockRecorder) GenerateArtifacts(ctx any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GenerateArtifacts", reflect.TypeOf((*MockComponentProvider)(nil).GenerateArtifacts), ctx) +} + +// GetAvailableCommands mocks base method. +func (m *MockComponentProvider) GetAvailableCommands() []string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetAvailableCommands") + ret0, _ := ret[0].([]string) + return ret0 +} + +// GetAvailableCommands indicates an expected call of GetAvailableCommands. +func (mr *MockComponentProviderMockRecorder) GetAvailableCommands() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAvailableCommands", reflect.TypeOf((*MockComponentProvider)(nil).GetAvailableCommands)) +} + +// GetBasePath mocks base method. +func (m *MockComponentProvider) GetBasePath(atmosConfig *schema.AtmosConfiguration) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBasePath", atmosConfig) + ret0, _ := ret[0].(string) + return ret0 +} + +// GetBasePath indicates an expected call of GetBasePath. +func (mr *MockComponentProviderMockRecorder) GetBasePath(atmosConfig any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBasePath", reflect.TypeOf((*MockComponentProvider)(nil).GetBasePath), atmosConfig) +} + +// GetGroup mocks base method. +func (m *MockComponentProvider) GetGroup() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetGroup") + ret0, _ := ret[0].(string) + return ret0 +} + +// GetGroup indicates an expected call of GetGroup. +func (mr *MockComponentProviderMockRecorder) GetGroup() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetGroup", reflect.TypeOf((*MockComponentProvider)(nil).GetGroup)) +} + +// GetType mocks base method. +func (m *MockComponentProvider) GetType() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetType") + ret0, _ := ret[0].(string) + return ret0 +} + +// GetType indicates an expected call of GetType. +func (mr *MockComponentProviderMockRecorder) GetType() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetType", reflect.TypeOf((*MockComponentProvider)(nil).GetType)) +} + +// ListComponents mocks base method. +func (m *MockComponentProvider) ListComponents(ctx context.Context, stack string, stackConfig map[string]any) ([]string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListComponents", ctx, stack, stackConfig) + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListComponents indicates an expected call of ListComponents. +func (mr *MockComponentProviderMockRecorder) ListComponents(ctx, stack, stackConfig any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListComponents", reflect.TypeOf((*MockComponentProvider)(nil).ListComponents), ctx, stack, stackConfig) +} + +// ValidateComponent mocks base method. +func (m *MockComponentProvider) ValidateComponent(config map[string]any) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ValidateComponent", config) + ret0, _ := ret[0].(error) + return ret0 +} + +// ValidateComponent indicates an expected call of ValidateComponent. +func (mr *MockComponentProviderMockRecorder) ValidateComponent(config any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateComponent", reflect.TypeOf((*MockComponentProvider)(nil).ValidateComponent), config) +} diff --git a/internal/exec/mock_terraform_state_getter.go b/internal/exec/mock_terraform_state_getter_test.go similarity index 88% rename from internal/exec/mock_terraform_state_getter.go rename to internal/exec/mock_terraform_state_getter_test.go index c4b5489b1f8..6e55c0c3bf9 100644 --- a/internal/exec/mock_terraform_state_getter.go +++ b/internal/exec/mock_terraform_state_getter_test.go @@ -1,9 +1,9 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: terraform_state_getter.go +// Source: state_getter.go // // Generated by this command: // -// mockgen -source=terraform_state_getter.go -destination=mock_terraform_state_getter.go -package=exec +// mockgen -source=state_getter.go -destination=../../../internal/exec/mock_terraform_state_getter_test.go -package=exec // // Package exec is a generated GoMock package. @@ -12,6 +12,7 @@ package exec import ( reflect "reflect" + terraform "github.com/cloudposse/atmos/pkg/component/terraform" schema "github.com/cloudposse/atmos/pkg/schema" gomock "go.uber.org/mock/gomock" ) @@ -41,7 +42,7 @@ func (m *MockTerraformStateGetter) EXPECT() *MockTerraformStateGetterMockRecorde } // GetState mocks base method. -func (m *MockTerraformStateGetter) GetState(atmosConfig *schema.AtmosConfiguration, yamlFunc, stack, component, output string, skipCache bool, authContext *schema.AuthContext, authManager any, options ...TerraformLookupOptions) (any, error) { +func (m *MockTerraformStateGetter) GetState(atmosConfig *schema.AtmosConfiguration, yamlFunc, stack, component, output string, skipCache bool, authContext *schema.AuthContext, authManager any, options ...terraform.TerraformLookupOptions) (any, error) { m.ctrl.T.Helper() varargs := []any{atmosConfig, yamlFunc, stack, component, output, skipCache, authContext, authManager} for _, a := range options { diff --git a/internal/exec/packer.go b/internal/exec/packer.go index 2c690c4640d..c54b00417fa 100644 --- a/internal/exec/packer.go +++ b/internal/exec/packer.go @@ -11,6 +11,7 @@ import ( errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/component" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/dependencies" log "github.com/cloudposse/atmos/pkg/logger" @@ -96,7 +97,7 @@ func ExecutePacker( // AuthManager, inject the store auth resolver). Mirrors terraform (setupTerraformAuth) // and helmfile (SetupComponentAuthForCLI). Without this, packer ran unauthenticated and // its datasources failed with "No valid credential sources found". - authManager, err := SetupComponentAuthForCLI(&atmosConfig, info) + authManager, err := terraformExecutor.SetupComponentAuthForCLI(&atmosConfig, info) if err != nil { return err } @@ -308,10 +309,10 @@ func ExecutePacker( // "executable file not found in $PATH", because exec.Command resolves the // binary via the process's real PATH at call time, not via the PATH=... // entry later added to envVars. - return executePackerCommandWithRetry(&atmosConfig, info, tenv, retryExecParams{ - allArgsAndFlags: allArgsAndFlags, - componentPath: componentPath, - envVars: envVars, + return executePackerCommandWithRetry(&atmosConfig, info, tenv, terraformcomponent.RetryExecParams{ + AllArgsAndFlags: allArgsAndFlags, + ComponentPath: componentPath, + EnvVars: envVars, }) } @@ -326,9 +327,9 @@ func executePackerCommandWithRetry( atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, tenv *dependencies.ToolchainEnvironment, - params retryExecParams, + params terraformcomponent.RetryExecParams, ) error { - return ExecuteShellCommandWithRetry( + return terraformcomponent.ExecuteShellCommandWithRetry( atmosConfig, info, info.SubCommand, @@ -336,9 +337,9 @@ func executePackerCommandWithRetry( return executePackerShellCommand( *atmosConfig, tenv.Resolve(info.Command), - params.allArgsAndFlags, - params.componentPath, - params.envVars, + params.AllArgsAndFlags, + params.ComponentPath, + params.EnvVars, info.DryRun, info.RedirectStdErr, o..., diff --git a/internal/exec/packer_test.go b/internal/exec/packer_test.go index 02e1cabe77a..8422282afac 100644 --- a/internal/exec/packer_test.go +++ b/internal/exec/packer_test.go @@ -14,6 +14,7 @@ import ( "github.com/stretchr/testify/require" errUtils "github.com/cloudposse/atmos/errors" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" log "github.com/cloudposse/atmos/pkg/logger" "github.com/cloudposse/atmos/pkg/schema" "github.com/cloudposse/atmos/tests" @@ -949,10 +950,10 @@ func TestExecutePackerCommandWithRetry_MatchingError_Retries(t *testing.T) { } atmosConfig := schema.AtmosConfiguration{} - err = executePackerCommandWithRetry(&atmosConfig, info, nil, retryExecParams{ - allArgsAndFlags: []string{"build"}, - componentPath: t.TempDir(), - envVars: info.ComponentEnvList, + err = executePackerCommandWithRetry(&atmosConfig, info, nil, terraformcomponent.RetryExecParams{ + AllArgsAndFlags: []string{"build"}, + ComponentPath: t.TempDir(), + EnvVars: info.ComponentEnvList, }) require.Error(t, err, "all 3 attempts fail in this fixture, so the final error must propagate") @@ -985,10 +986,10 @@ func TestExecutePackerCommandWithRetry_NonMatchingError_FailsFast(t *testing.T) } atmosConfig := schema.AtmosConfiguration{} - err = executePackerCommandWithRetry(&atmosConfig, info, nil, retryExecParams{ - allArgsAndFlags: []string{"build"}, - componentPath: t.TempDir(), - envVars: info.ComponentEnvList, + err = executePackerCommandWithRetry(&atmosConfig, info, nil, terraformcomponent.RetryExecParams{ + AllArgsAndFlags: []string{"build"}, + ComponentPath: t.TempDir(), + EnvVars: info.ComponentEnvList, }) require.Error(t, err) diff --git a/internal/exec/path_utils.go b/internal/exec/path_utils.go index 49fb9fbd58c..9bddc8962e8 100644 --- a/internal/exec/path_utils.go +++ b/internal/exec/path_utils.go @@ -4,100 +4,46 @@ import ( "fmt" "path/filepath" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" log "github.com/cloudposse/atmos/pkg/logger" provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir" "github.com/cloudposse/atmos/pkg/schema" u "github.com/cloudposse/atmos/pkg/utils" ) -// constructTerraformComponentWorkingDir constructs the working dir for a terraform component in a stack. +// constructTerraformComponentWorkingDir delegates to the Terraform component. func constructTerraformComponentWorkingDir(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) string { - // Check if a provisioner (source or workdir) set a workdir path. - // This takes precedence over the standard component path. - // Use filepath.FromSlash to normalize path separators for cross-platform compatibility. - if workdirPath, ok := info.ComponentSection[provWorkdir.WorkdirPathKey].(string); ok && workdirPath != "" { - return filepath.FromSlash(workdirPath) - } - - // If we have a resolved absolute path, use GetComponentPath. - // Otherwise, just construct the path directly (for tests and simple cases). - if atmosConfig.TerraformDirAbsolutePath != "" { - path, err := u.GetComponentPath(atmosConfig, "terraform", info.ComponentFolderPrefix, info.FinalComponent) - if err == nil { - return path - } - // Log error but still try to return something. - log.Debug("Failed to resolve component path, falling back to construction", "error", err) - } - - // Direct path construction without converting to absolute. - // This maintains compatibility with tests that use simple paths. - return filepath.Join( - atmosConfig.BasePath, - atmosConfig.Components.Terraform.BasePath, - info.ComponentFolderPrefix, - info.FinalComponent, - ) + return terraformcomponent.ConstructTerraformComponentWorkingDir(atmosConfig, info) } -// constructTerraformComponentPlanfileName constructs the planfile name for a terraform component in a stack. +// constructTerraformComponentPlanfileName delegates to the Terraform component. func constructTerraformComponentPlanfileName(info *schema.ConfigAndStacksInfo) string { - var planFile string - if len(info.ComponentFolderPrefixReplaced) == 0 { - planFile = fmt.Sprintf("%s-%s.planfile", info.ContextPrefix, info.Component) - } else { - planFile = fmt.Sprintf("%s-%s-%s.planfile", info.ContextPrefix, info.ComponentFolderPrefixReplaced, info.Component) - } - - return planFile + return terraformcomponent.ConstructTerraformComponentPlanfileName(info) } -// constructTerraformComponentVarfileName constructs the varfile name for a terraform component in a stack. +// constructTerraformComponentVarfileName delegates to the Terraform component. func constructTerraformComponentVarfileName(info *schema.ConfigAndStacksInfo) string { - var varFile string - if len(info.ComponentFolderPrefixReplaced) == 0 { - varFile = fmt.Sprintf("%s-%s.terraform.tfvars.json", info.ContextPrefix, info.Component) - } else { - varFile = fmt.Sprintf("%s-%s-%s.terraform.tfvars.json", info.ContextPrefix, info.ComponentFolderPrefixReplaced, info.Component) - } - - return varFile + return terraformcomponent.ConstructTerraformComponentVarfileName(info) } -// constructTerraformComponentTestVarfileName constructs the test varfile name for a terraform component in a stack. +// constructTerraformComponentTestVarfileName delegates to the Terraform component. func constructTerraformComponentTestVarfileName(info *schema.ConfigAndStacksInfo) string { - var varFile string - if len(info.ComponentFolderPrefixReplaced) == 0 { - varFile = fmt.Sprintf("%s-%s.terraform.test.tfvars.json", info.ContextPrefix, info.Component) - } else { - varFile = fmt.Sprintf("%s-%s-%s.terraform.test.tfvars.json", info.ContextPrefix, info.ComponentFolderPrefixReplaced, info.Component) - } - - return varFile + return terraformcomponent.ConstructTerraformComponentTestVarfileName(info) } -// constructTerraformComponentVarfilePath constructs the varfile path for a terraform component in a stack. +// constructTerraformComponentVarfilePath delegates to the Terraform component. func constructTerraformComponentVarfilePath(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) string { - return filepath.Join( - constructTerraformComponentWorkingDir(atmosConfig, info), - constructTerraformComponentVarfileName(info), - ) + return terraformcomponent.ConstructTerraformComponentVarfilePath(atmosConfig, info) } -// constructTerraformComponentTestVarfilePath constructs the test varfile path for a terraform component in a stack. +// constructTerraformComponentTestVarfilePath delegates to the Terraform component. func constructTerraformComponentTestVarfilePath(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) string { - return filepath.Join( - constructTerraformComponentWorkingDir(atmosConfig, info), - constructTerraformComponentTestVarfileName(info), - ) + return terraformcomponent.ConstructTerraformComponentTestVarfilePath(atmosConfig, info) } -// constructTerraformComponentPlanfilePath constructs the planfile path for a terraform component in a stack. +// constructTerraformComponentPlanfilePath delegates to the Terraform component. func constructTerraformComponentPlanfilePath(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) string { - return filepath.Join( - constructTerraformComponentWorkingDir(atmosConfig, info), - constructTerraformComponentPlanfileName(info), - ) + return terraformcomponent.ConstructTerraformComponentPlanfilePath(atmosConfig, info) } // constructHelmfileComponentWorkingDir constructs the working dir for a helmfile component in a stack. diff --git a/internal/exec/path_utils_test.go b/internal/exec/path_utils_test.go index 6105e2871a5..93b0c1d6c68 100644 --- a/internal/exec/path_utils_test.go +++ b/internal/exec/path_utils_test.go @@ -11,49 +11,6 @@ import ( ) // TestConstructTerraformComponentWorkingDir_WithWorkdirPath tests workdir path resolution. -func TestConstructTerraformComponentWorkingDir_WithWorkdirPath(t *testing.T) { - t.Parallel() - - // Test workdir path takes precedence (returned verbatim from config). - workdirPath := filepath.Join("workdir", "terraform", "dev-vpc") - atmosConfig := schema.AtmosConfiguration{ - BasePath: "base", - Components: schema.Components{ - Terraform: schema.Terraform{ - BasePath: filepath.Join("components", "terraform"), - }, - }, - } - info := schema.ConfigAndStacksInfo{ - ComponentFolderPrefix: "", - FinalComponent: "vpc", - ComponentSection: map[string]any{ - provWorkdir.WorkdirPathKey: workdirPath, - }, - } - got := constructTerraformComponentWorkingDir(&atmosConfig, &info) - assert.Equal(t, workdirPath, got) - - // Test empty workdir path falls back to standard path. - info2 := schema.ConfigAndStacksInfo{ - ComponentFolderPrefix: "", - FinalComponent: "vpc", - ComponentSection: map[string]any{ - provWorkdir.WorkdirPathKey: "", - }, - } - got2 := constructTerraformComponentWorkingDir(&atmosConfig, &info2) - assert.Equal(t, filepath.Join("base", "components", "terraform", "vpc"), got2) - - // Test no workdir path uses standard path. - info3 := schema.ConfigAndStacksInfo{ - ComponentFolderPrefix: "", - FinalComponent: "vpc", - ComponentSection: map[string]any{}, - } - got3 := constructTerraformComponentWorkingDir(&atmosConfig, &info3) - assert.Equal(t, filepath.Join("base", "components", "terraform", "vpc"), got3) -} // TestConstructHelmfileComponentWorkingDir_WithWorkdirPath tests workdir path resolution for helmfile. func TestConstructHelmfileComponentWorkingDir_WithWorkdirPath(t *testing.T) { @@ -197,117 +154,10 @@ func TestConstructPackerComponentWorkingDir(t *testing.T) { // TestConstructTerraformComponentVarfilePath_WithWorkdirPath tests varfile path with JIT vendored components. // This test verifies that varfile paths correctly use workdir paths set by JIT provisioning. -func TestConstructTerraformComponentVarfilePath_WithWorkdirPath(t *testing.T) { - t.Parallel() - - // Test varfile path uses workdir path when set (JIT vendored component scenario). - workdirPath := filepath.Join("workdir", "terraform", "dev-vpc") - atmosConfig := schema.AtmosConfiguration{ - BasePath: "base", - Components: schema.Components{ - Terraform: schema.Terraform{ - BasePath: filepath.Join("components", "terraform"), - }, - }, - } - info := schema.ConfigAndStacksInfo{ - ContextPrefix: "tenant1-ue2-dev", - ComponentFolderPrefix: "", - Component: "vpc", - FinalComponent: "vpc", - ComponentSection: map[string]any{ - provWorkdir.WorkdirPathKey: workdirPath, - }, - } - got := constructTerraformComponentVarfilePath(&atmosConfig, &info) - assert.Equal(t, filepath.Join(workdirPath, "tenant1-ue2-dev-vpc.terraform.tfvars.json"), got) - - // Test varfile path uses standard path when no workdir. - info2 := schema.ConfigAndStacksInfo{ - ContextPrefix: "tenant1-ue2-dev", - ComponentFolderPrefix: "", - Component: "vpc", - FinalComponent: "vpc", - ComponentSection: map[string]any{}, - } - got2 := constructTerraformComponentVarfilePath(&atmosConfig, &info2) - assert.Equal(t, filepath.Join("base", "components", "terraform", "vpc", "tenant1-ue2-dev-vpc.terraform.tfvars.json"), got2) -} // TestConstructTerraformComponentWorkingDir_JITVendoredComponent tests working dir for JIT vendored components. // This simulates the scenario where a component is downloaded via JIT provisioning // and the workdir path is set by the source provisioner. -func TestConstructTerraformComponentWorkingDir_JITVendoredComponent(t *testing.T) { - t.Parallel() - - tests := []struct { - name string - workdirPath string - expectedPath string - componentName string - hasSource bool - sourceConfig map[string]any - }{ - { - name: "JIT vendored component with workdir path", - workdirPath: filepath.Join("tmp", "atmos-vendor", "abc123", "modules", "vpc"), - expectedPath: filepath.Join("tmp", "atmos-vendor", "abc123", "modules", "vpc"), - componentName: "vpc", - hasSource: true, - sourceConfig: map[string]any{ - "source": map[string]any{ - "uri": "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=v1.0.0", - }, - }, - }, - { - name: "JIT vendored component with string source", - workdirPath: filepath.Join("tmp", "vendor", "my-component"), - expectedPath: filepath.Join("tmp", "vendor", "my-component"), - componentName: "my-component", - hasSource: true, - sourceConfig: map[string]any{ - "source": "git::https://github.com/org/repo.git?ref=main", - }, - }, - { - name: "Regular component without source (no workdir)", - workdirPath: "", - expectedPath: filepath.Join("base", "components", "terraform", "vpc"), - componentName: "vpc", - hasSource: false, - sourceConfig: map[string]any{}, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - t.Parallel() - atmosConfig := schema.AtmosConfiguration{ - BasePath: "base", - Components: schema.Components{ - Terraform: schema.Terraform{ - BasePath: filepath.Join("components", "terraform"), - }, - }, - } - - componentSection := tt.sourceConfig - if tt.workdirPath != "" { - componentSection[provWorkdir.WorkdirPathKey] = tt.workdirPath - } - - info := schema.ConfigAndStacksInfo{ - ComponentFolderPrefix: "", - FinalComponent: tt.componentName, - ComponentSection: componentSection, - } - - got := constructTerraformComponentWorkingDir(&atmosConfig, &info) - assert.Equal(t, tt.expectedPath, got) - }) - } -} func TestConstructPackerComponentVarfilePath(t *testing.T) { t.Parallel() diff --git a/internal/exec/shell_retry_policy.go b/internal/exec/shell_retry_policy.go deleted file mode 100644 index 05141d2064e..00000000000 --- a/internal/exec/shell_retry_policy.go +++ /dev/null @@ -1,78 +0,0 @@ -package exec - -import ( - "bytes" - "context" - "io" - - "github.com/cloudposse/atmos/pkg/policy/recovery" - "github.com/cloudposse/atmos/pkg/schema" -) - -func executeShellRetryPolicy(info *schema.ConfigAndStacksInfo, phase string, invoke invokeShellCommandFunc, options ...ShellCommandOption) (bool, error) { - componentType, eventCommand := shellErrorEvent(info, phase) - runner, err := recovery.New(info, componentType, eventCommand) - if err != nil || runner == nil { - return err != nil, err - } - capture := errorPolicyCapture(options) - metadataOut, metadataErr := execMetadataOutputCaptureFromOpts(options...) - var detailedResult error - err = runner.Run(capture.ctx, phase, info.ComponentRetrySection, func() recovery.Result { - capture.stdout.Reset() - capture.stderr.Reset() - resetExecMetadataBufs(metadataOut, metadataErr) - err := invoke(capture.options...) - code := resolveExitCode(err) - // A detailed plan result is not an error event. Preserve its exit status. - if componentType == "terraform" && phase == "plan" && code == 2 { - detailedResult = err - return recovery.Result{ExitCode: code} - } - return recovery.Result{Err: err, ExitCode: code, Stdout: capture.stdout.String(), Stderr: capture.stderr.String()} - }) - if err == nil && detailedResult != nil { - return true, detailedResult - } - return true, err -} - -type policyCapture struct { - ctx context.Context - options []ShellCommandOption - stdout, stderr *bytes.Buffer -} - -func errorPolicyCapture(options []ShellCommandOption) policyCapture { - var config shellCommandConfig - for _, option := range options { - option(&config) - } - ctx := config.ctx - if ctx == nil { - ctx = context.Background() - } - stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} - stdoutWriter, stderrWriter := io.Writer(&syncWriter{w: stdout}), io.Writer(&syncWriter{w: stderr}) - if config.stdoutCapture != nil { - stdoutWriter = io.MultiWriter(config.stdoutCapture, stdoutWriter) - } - if config.stderrCapture != nil { - stderrWriter = io.MultiWriter(config.stderrCapture, stderrWriter) - } - capture := append([]ShellCommandOption{}, options...) - capture = append(capture, WithStdoutCapture(stdoutWriter), WithStderrCapture(stderrWriter)) - return policyCapture{ctx: ctx, options: capture, stdout: stdout, stderr: stderr} -} - -func shellErrorEvent(info *schema.ConfigAndStacksInfo, phase string) (string, string) { - componentType := info.ComponentType - if componentType == "" { - componentType = "terraform" - } - eventCommand := phase - if phase == info.SubCommand && info.SubCommand2 != "" { - eventCommand += "." + info.SubCommand2 - } - return componentType, eventCommand -} diff --git a/internal/exec/shell_retry_policy_test.go b/internal/exec/shell_retry_policy_test.go deleted file mode 100644 index 4c7e818af43..00000000000 --- a/internal/exec/shell_retry_policy_test.go +++ /dev/null @@ -1,91 +0,0 @@ -package exec - -import ( - "bytes" - "errors" - "io" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - errUtils "github.com/cloudposse/atmos/errors" - "github.com/cloudposse/atmos/pkg/policy/recovery/recoverytest" - "github.com/cloudposse/atmos/pkg/schema" -) - -func TestShellRetryPolicyActualCommand(t *testing.T) { - for _, tc := range []struct{ kind, phase, parent, nested, event string }{ - {"terraform", "init", "plan", "", "terraform.init.error"}, - {"", "init", "plan", "", "terraform.init.error"}, - {"terraform", "output", "output", "", "terraform.output.error"}, - {"terraform", "workspace-select", "apply", "", "terraform.workspace.select.error"}, - {"terraform", "workspace", "workspace", "select", "terraform.workspace.select.error"}, - {"terraform", "validate", "validate", "", "terraform.*.error"}, - {"helmfile", "sync", "sync", "", "helmfile.apply.error"}, - {"packer", "build", "build", "", "packer.build.error"}, - {"ansible", "playbook", "playbook", "", "ansible.playbook.error"}, - } { - t.Run(tc.kind+"/"+tc.phase, func(t *testing.T) { - recoverytest.Install(t, tc.event, "input.stderr.contains('transient') && input.stdout.contains('attempt')") - info := &schema.ConfigAndStacksInfo{ComponentType: tc.kind, SubCommand: tc.parent, SubCommand2: tc.nested} - var callerCapture, callerStderr bytes.Buffer - attempts := 0 - err := ExecuteShellCommandWithRetry(&schema.AtmosConfiguration{}, info, tc.phase, func(options ...ShellCommandOption) error { - attempts++ - var config shellCommandConfig - for _, option := range options { - option(&config) - } - _, _ = io.WriteString(config.stdoutCapture, "attempt\n") - if attempts < 3 { - _, _ = io.WriteString(config.stderrCapture, "transient\n") - return errors.New("transient") - } - return nil - }, WithStdoutCapture(&callerCapture), WithStderrCapture(&callerStderr)) - require.NoError(t, err) - assert.Equal(t, 3, attempts) - assert.Equal(t, "attempt\nattempt\nattempt\n", callerCapture.String()) - assert.Equal(t, "transient\ntransient\n", callerStderr.String()) - }) - } -} - -func TestShellRetryPolicyPreservesPlanDetailedExitCode(t *testing.T) { - recoverytest.Install(t, "terraform.plan.error", "true") - info := &schema.ConfigAndStacksInfo{ComponentType: "terraform", SubCommand: "plan"} - attempts := 0 - detailed := errUtils.ExitCodeError{Code: 2} - err := ExecuteShellCommandWithRetry(&schema.AtmosConfiguration{}, info, "plan", func(...ShellCommandOption) error { attempts++; return detailed }) - assert.Equal(t, 1, attempts) - assert.Equal(t, 2, resolveExitCode(err)) -} - -func TestShellRetryPolicyDoesNotReusePreviousOutput(t *testing.T) { - recoverytest.Install(t, "terraform.init.error", "input.output.contains('transient')") - info := &schema.ConfigAndStacksInfo{ComponentType: "terraform", SubCommand: "plan"} - attempts := 0 - err := ExecuteShellCommandWithRetry(&schema.AtmosConfiguration{}, info, "init", func(options ...ShellCommandOption) error { - attempts++ - var config shellCommandConfig - for _, option := range options { - option(&config) - } - if attempts == 1 { - _, _ = io.WriteString(config.stderrCapture, "transient") - } - return errors.New("failed") - }) - require.Error(t, err) - assert.Equal(t, 2, attempts) -} - -func TestShellRetryPolicyUnselectedEvent(t *testing.T) { - recoverytest.Install(t, "terraform.init.error", "true") - calls := 0 - failure := errors.New("output failed") - err := ExecuteShellCommandWithRetry(&schema.AtmosConfiguration{}, &schema.ConfigAndStacksInfo{}, "output", func(...ShellCommandOption) error { calls++; return failure }) - require.ErrorIs(t, err, failure) - require.Equal(t, 1, calls) -} diff --git a/internal/exec/shell_utils.go b/internal/exec/shell_utils.go index c0432be0f70..268217dddd8 100644 --- a/internal/exec/shell_utils.go +++ b/internal/exec/shell_utils.go @@ -20,6 +20,7 @@ import ( xterm "golang.org/x/term" errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/component" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/diagnostics" envpkg "github.com/cloudposse/atmos/pkg/env" @@ -39,222 +40,103 @@ import ( const logFieldCommand = "command" -// ShellCommandOption is a functional option for ExecuteShellCommand. -type ShellCommandOption func(*shellCommandConfig) - -// shellCommandConfig holds optional configuration for shell command execution. -type shellCommandConfig struct { - stdoutCapture io.Writer - stderrCapture io.Writer - stdoutOverride io.Writer - streams *process.Streams - ctx context.Context - // processEnv replaces os.Environ() as the process environment. - // When set, ExecuteShellCommand uses this instead of re-reading os.Environ(). - // This is used when auth has already sanitized the environment (e.g., removed IRSA vars). - processEnv []string - // invokingCmd is the Cobra command the user actually invoked (e.g. the - // terraform plan subcommand). Threaded through so ExecuteTerraform can - // report the real Flags actually passed (FR-003b) to the exec-metadata - // sync capture, instead of a pass-through-args collection that cannot - // contain atmos-recognized flags (research.md Decision 14). - invokingCmd *cobra.Command - // execMetadataParser, when set, is called once by captureExecMetadataSync - // (sync-allowlisted commands only) to obtain command-specific structured - // data (FR-006) for the execution record. cmd/terraform supplies this — - // it can safely call pkg/ci/plugins/terraform's output parser, unlike - // internal/exec, which cannot import it without reintroducing a confirmed - // import cycle (research.md Decision 18). The output parameter is the - // scoped exec-metadata output text executeCommandPipeline captured for - // just the main plan/apply/deploy subprocess (FR-006f). - execMetadataParser func(subCommand string, exitCode int, output string) any - - // execMetadataStdoutCapture/execMetadataStderrCapture, when set, additionally - // tee this specific ExecuteShellCommand call's stdout/stderr into these - // writers, alongside (not instead of) stdoutCapture/stderrCapture. Used - // internally by executeCommandPipeline (via withExecMetadataOutputCapture) to - // scope the exec-metadata parser's input to only the final plan/apply/deploy - // subprocess invocation, while stdoutCapture/stderrCapture's buffers keep - // accumulating the whole pipeline's output for other consumers (e.g. CI - // job-summary hooks) that legitimately need it (FR-006f, research.md - // Decision 32). - execMetadataStdoutCapture io.Writer - execMetadataStderrCapture io.Writer - - // metricsCallback, when set, is invoked once after the subprocess exits - // with the subprocess tree's collected resource-usage metrics (nil-safe: - // only called when a non-nil result was collected). Lets a caller (e.g. - // executeMainTerraformCommand) capture this specific shell command's - // subprocess-tree metrics for combining with atmos's own self-usage, for - // both the local per-command display and the exec-metadata sync upload. - metricsCallback func(*metricsprocess.ProcessMetrics) -} +// ShellCommandOption is the compatibility name for component execution options. +type ShellCommandOption = component.ExecutionOption + +type shellCommandConfig = component.ExecutionOptions -// WithInvokingCommand provides the Cobra command the user actually invoked, -// so ExecuteTerraform's exec-metadata sync capture can derive Flags from the -// command's own record of explicitly-set flags (proexec.FlagsFromCommand) -// rather than info.AdditionalArgsAndFlags. +// WithInvokingCommand retains the invoked Cobra command for execution metadata reporting. func WithInvokingCommand(cmd *cobra.Command) ShellCommandOption { defer perf.Track(nil, "exec.WithInvokingCommand")() - return func(c *shellCommandConfig) { - c.invokingCmd = cmd - } + return component.WithInvokingCommand(cmd) } -// invokingCommandFromOpts extracts the invoking *cobra.Command (if any) set -// via WithInvokingCommand among opts. +// invokingCommandFromOpts returns the invoking command supplied through shared execution options. func invokingCommandFromOpts(opts ...ShellCommandOption) *cobra.Command { - var cfg shellCommandConfig - for _, opt := range opts { - opt(&cfg) - } - return cfg.invokingCmd + return component.InvokingCommandFromOptions(opts...) } -// WithExecMetadataParser provides a closure that, given the invoking -// subcommand name and the command's own exit code, returns command-specific -// structured data (FR-006) for the execution record — or nil if there is -// none to report. The exitCode parameter is the terraform/tofu subprocess's -// own exit code (research.md Decision 27), supplied by -// captureExecMetadataSync at call time since it isn't known when this -// closure is created. Cmd/terraform -// supplies this closure so ExecuteTerraform's exec-metadata sync capture can -// obtain parsed terraform plan/apply/deploy output without internal/exec -// itself importing the CI plugin's parser (research.md Decision 18). -func WithExecMetadataParser(fn func(subCommand string, exitCode int, output string) any) ShellCommandOption { +// WithExecMetadataParser supplies the parser for command-specific execution results. +func WithExecMetadataParser(fn func(string, int, string) any) ShellCommandOption { defer perf.Track(nil, "exec.WithExecMetadataParser")() - return func(c *shellCommandConfig) { - c.execMetadataParser = fn - } + return component.WithExecMetadataParser(fn) } -// execMetadataParserFromOpts extracts the parser closure (if any) set via -// WithExecMetadataParser among opts. -func execMetadataParserFromOpts(opts ...ShellCommandOption) func(subCommand string, exitCode int, output string) any { - var cfg shellCommandConfig - for _, opt := range opts { - opt(&cfg) - } - return cfg.execMetadataParser +// execMetadataParserFromOpts returns the command-result parser, or nil when none was supplied. +func execMetadataParserFromOpts(opts ...ShellCommandOption) func(string, int, string) any { + return component.MetadataParserFromOptions(opts...) } -// WithMetricsCallback provides a closure that ExecuteShellCommand invokes -// once, after the subprocess exits, with the subprocess tree's collected -// resource-usage metrics. Used by executeMainTerraformCommand to capture the -// main plan/apply/deploy subprocess's own metrics for combining with atmos's -// self-usage — both for local display and the exec-metadata sync upload. +// WithMetricsCallback supplies the callback that receives subprocess resource-usage metrics. func WithMetricsCallback(fn func(*metricsprocess.ProcessMetrics)) ShellCommandOption { - defer perf.Track(nil, "exec.WithMetricsCallback")() - - return func(c *shellCommandConfig) { - c.metricsCallback = fn - } + return component.WithMetricsCallback(fn) } -// withExecMetadataOutputCapture tees this specific ExecuteShellCommand call's -// stdout/stderr into stdoutW/stderrW, in addition to whatever -// WithStdoutCapture/WithStderrCapture already capture. Used internally by -// executeCommandPipeline to scope the exec-metadata parser's input to only the -// final plan/apply/deploy subprocess invocation (FR-006f), without disturbing -// the broader stdout/stderr buffers other consumers (e.g. cmd/terraform's -// capturedPlanOutput, used by CI job-summary hooks) rely on for the whole -// init+workspace-select+main pipeline. Unexported — an internal -// executeCommandPipeline concern, not part of the ShellCommandOption surface -// cmd/terraform uses. +// withExecMetadataOutputCapture adds output writers scoped to the main command for metadata reporting. func withExecMetadataOutputCapture(stdoutW, stderrW io.Writer) ShellCommandOption { - return func(c *shellCommandConfig) { - c.execMetadataStdoutCapture = stdoutW - c.execMetadataStderrCapture = stderrW - } + return component.WithMetadataOutputCapture(stdoutW, stderrW) } -// execMetadataOutputCaptureFromOpts extracts the exec-metadata stdout/stderr -// tee writers (if any) set via withExecMetadataOutputCapture among opts, so -// callers that retry the underlying invocation (executeShellCommandWithRetry) -// can reset them between attempts. +// execMetadataOutputCaptureFromOpts retrieves the metadata writers so retries can reset their output. func execMetadataOutputCaptureFromOpts(opts ...ShellCommandOption) (io.Writer, io.Writer) { - var cfg shellCommandConfig - for _, opt := range opts { - opt(&cfg) - } - return cfg.execMetadataStdoutCapture, cfg.execMetadataStderrCapture + return component.MetadataOutputCaptureFromOptions(opts...) } -// WithStdoutCapture returns a ShellCommandOption that tees stdout to the provided writer. -// The captured output includes secret masking (post-MaskWriter). +// WithStdoutCapture tees masked subprocess stdout to the supplied writer. func WithStdoutCapture(w io.Writer) ShellCommandOption { - return func(c *shellCommandConfig) { - c.stdoutCapture = w - } + defer perf.Track(nil, "exec.WithStdoutCapture")() + + return component.WithStdoutCapture(w) } -// WithStderrCapture returns a ShellCommandOption that tees stderr to the provided writer. -// The captured output includes secret masking (post-MaskWriter). +// WithStderrCapture tees masked subprocess stderr to the supplied writer. func WithStderrCapture(w io.Writer) ShellCommandOption { - return func(c *shellCommandConfig) { - c.stderrCapture = w - } + defer perf.Track(nil, "exec.WithStderrCapture")() + + return component.WithStderrCapture(w) } -// WithStdoutOverride returns a ShellCommandOption that replaces the default stdout -// (os.Stdout) with a different writer. Used to redirect noisy commands (e.g., -// workspace select) to stderr so they don't pollute data-producing commands like output. +// WithStdoutOverride replaces the default stdout destination for the subprocess. func WithStdoutOverride(w io.Writer) ShellCommandOption { - return func(c *shellCommandConfig) { - c.stdoutOverride = w - } + defer perf.Track(nil, "exec.WithStdoutOverride")() + + return component.WithStdoutOverride(w) } -// WithProcessStreams provides the subprocess standard streams. +// WithProcessStreams supplies stdin, stdout, and stderr through the shared component options. func WithProcessStreams(streams process.Streams) ShellCommandOption { - return func(c *shellCommandConfig) { - c.streams = &streams - } + return component.WithProcessStreams(streams) } -// WithProcessContext provides the context used for subprocess execution. +// WithProcessContext supplies cancellation and deadlines for subprocess execution. func WithProcessContext(ctx context.Context) ShellCommandOption { - return func(c *shellCommandConfig) { - c.ctx = ctx - } + return component.WithProcessContext(ctx) } +// shellCommandContext returns the last configured context, or a background context for legacy callers. func shellCommandContext(opts ...ShellCommandOption) context.Context { - var cfg shellCommandConfig - for _, opt := range opts { - opt(&cfg) - } - if cfg.ctx == nil { - return context.Background() - } - return cfg.ctx + return component.ContextFromOptions(opts...) } +// WithEnvironment supplies a pre-sanitized subprocess environment; nil retains the default environment. +func WithEnvironment(env []string) ShellCommandOption { + defer perf.Track(nil, "exec.WithEnvironment")() + + return component.WithEnvironment(env) +} + +// shellCommandOutputWriters exposes explicitly supplied process writers to provisioning hooks. func shellCommandOutputWriters(opts ...ShellCommandOption) provisioner.OutputWriters { - var cfg shellCommandConfig + var options component.ExecutionOptions for _, opt := range opts { - opt(&cfg) + opt(&options) } - if cfg.streams == nil { + if options.Streams == nil { return provisioner.OutputWriters{} } - return provisioner.OutputWriters{ - Stdout: cfg.streams.Stdout, - Stderr: cfg.streams.Stderr, - } -} - -// WithEnvironment provides a pre-sanitized process environment for subprocess execution. -// When provided, ExecuteShellCommand uses this instead of re-reading os.Environ(). -// Pass nil to fall back to the default os.Environ() behavior. -func WithEnvironment(env []string) ShellCommandOption { - defer perf.Track(nil, "exec.WithEnvironment")() - - return func(c *shellCommandConfig) { - c.processEnv = env - } + return provisioner.OutputWriters{Stdout: options.Streams.Stdout, Stderr: options.Streams.Stderr} } // resolveMaskingDisabled decides whether output masking should be disabled. @@ -307,11 +189,11 @@ func ExecuteShellCommand( } // Build environment: process env + global env (atmos.yaml) + command-specific env. - // When auth has sanitized the environment, cfg.processEnv is used instead of + // When auth has sanitized the environment, cfg.ProcessEnv is used instead of // os.Environ() to avoid reintroducing problematic vars (e.g., IRSA credentials). baseEnv := os.Environ() - if cfg.processEnv != nil { - baseEnv = cfg.processEnv + if cfg.ProcessEnv != nil { + baseEnv = cfg.ProcessEnv } cmdEnv := envpkg.MergeGlobalEnv(baseEnv, atmosConfig.Env) cmdEnv = append(cmdEnv, env...) @@ -327,8 +209,8 @@ func ExecuteShellCommand( } streams := process.OSStreams() - if cfg.streams != nil { - streams = *cfg.streams + if cfg.Streams != nil { + streams = *cfg.Streams if streams.Stdin == nil { streams.Stdin = os.Stdin } @@ -361,8 +243,8 @@ func ExecuteShellCommand( // When stdoutOverride is set, use it instead of os.Stdout (e.g., redirect to stderr // for workspace select so it doesn't pollute data-producing commands like output). var stdoutTarget io.Writer = streams.Stdout - if cfg.stdoutOverride != nil { - stdoutTarget = cfg.stdoutOverride + if cfg.StdoutOverride != nil { + stdoutTarget = cfg.StdoutOverride } stdoutTarget = maybePaceTerminalWriter(stdoutTarget, atmosConfig.Settings.Terminal.Speed, &pacedClosers) maskedStdout := newMasked(stdoutTarget) @@ -370,11 +252,11 @@ func ExecuteShellCommand( if diagnostics.OutputEnabled(diagConfig) { stdoutWriters = append(stdoutWriters, diagnostics.NewOutputWriter(diagConfig, diagID, "stdout")) } - if cfg.stdoutCapture != nil { - stdoutWriters = append(stdoutWriters, cfg.stdoutCapture) + if cfg.StdoutCapture != nil { + stdoutWriters = append(stdoutWriters, cfg.StdoutCapture) } - if cfg.execMetadataStdoutCapture != nil { - stdoutWriters = append(stdoutWriters, cfg.execMetadataStdoutCapture) + if cfg.ExecMetadataStdoutCapture != nil { + stdoutWriters = append(stdoutWriters, cfg.ExecMetadataStdoutCapture) } stdout := io.MultiWriter(stdoutWriters...) @@ -393,21 +275,21 @@ func ExecuteShellCommand( if diagnostics.OutputEnabled(diagConfig) { stderrWriters = append(stderrWriters, diagnostics.NewOutputWriter(diagConfig, diagID, "stderr")) } - if cfg.stderrCapture != nil { - stderrWriters = append(stderrWriters, cfg.stderrCapture) + if cfg.StderrCapture != nil { + stderrWriters = append(stderrWriters, cfg.StderrCapture) } - if cfg.execMetadataStderrCapture != nil { - stderrWriters = append(stderrWriters, cfg.execMetadataStderrCapture) + if cfg.ExecMetadataStderrCapture != nil { + stderrWriters = append(stderrWriters, cfg.ExecMetadataStderrCapture) } stderr = io.MultiWriter(stderrWriters...) } else if redirectStdError == "/dev/stdout" { maskedStderr := newMasked(stdout) extraStderrWriters := []io.Writer{maskedStderr} - if cfg.stderrCapture != nil { - extraStderrWriters = append(extraStderrWriters, cfg.stderrCapture) + if cfg.StderrCapture != nil { + extraStderrWriters = append(extraStderrWriters, cfg.StderrCapture) } - // Note: cfg.execMetadataStderrCapture is intentionally NOT appended here. - // stdout already includes cfg.execMetadataStdoutCapture (see above), and + // Note: cfg.ExecMetadataStderrCapture is intentionally NOT appended here. + // stdout already includes cfg.ExecMetadataStdoutCapture (see above), and // maskedStderr writes through stdout, so also appending // execMetadataStderrCapture would duplicate this stderr content into // combineExecMetadataOutput's stderr buffer alongside the copy already @@ -424,11 +306,11 @@ func ExecuteShellCommand( if diagnostics.OutputEnabled(diagConfig) { stderrWriters = append(stderrWriters, diagnostics.NewOutputWriter(diagConfig, diagID, "stderr")) } - if cfg.stderrCapture != nil { - stderrWriters = append(stderrWriters, cfg.stderrCapture) + if cfg.StderrCapture != nil { + stderrWriters = append(stderrWriters, cfg.StderrCapture) } - if cfg.execMetadataStderrCapture != nil { - stderrWriters = append(stderrWriters, cfg.execMetadataStderrCapture) + if cfg.ExecMetadataStderrCapture != nil { + stderrWriters = append(stderrWriters, cfg.ExecMetadataStderrCapture) } stderr = io.MultiWriter(stderrWriters...) } else { @@ -477,7 +359,7 @@ func ExecuteShellCommand( return closeErr } - ctx := cfg.ctx + ctx := cfg.Ctx if ctx == nil { ctx = context.Background() } @@ -495,8 +377,8 @@ func ExecuteShellCommand( }) flushStreamMaskers(streamMaskers) emitProcessEndDiagnostics(diagConfig, diagID, diagStartedAt, &result) - if cfg.metricsCallback != nil && result.Metrics != nil { - cfg.metricsCallback(result.Metrics) + if cfg.MetricsCallback != nil && result.Metrics != nil { + cfg.MetricsCallback(result.Metrics) } if closeErr := closePacedWriters(pacedClosers); closeErr != nil && result.Err == nil { return closeErr diff --git a/internal/exec/starlark_policy_test.go b/internal/exec/starlark_policy_test.go index 707f9d8f6ec..8c3cb0ad34c 100644 --- a/internal/exec/starlark_policy_test.go +++ b/internal/exec/starlark_policy_test.go @@ -1,36 +1,15 @@ package exec import ( - "encoding/json" "os" "path/filepath" - "strings" "testing" "github.com/stretchr/testify/require" errUtils "github.com/cloudposse/atmos/errors" - "github.com/cloudposse/atmos/pkg/policy" - "github.com/cloudposse/atmos/pkg/schema" ) -func TestStarlarkTerraformPolicyRegistration(t *testing.T) { - directory := t.TempDir() - t.Setenv("ATMOS_POLICY_DIR", directory) - document := strings.Replace(testTerraformPolicyManifest, - `rule: "has(input.resource_changes) && input.resource_changes.exists(r, 'delete' in r.change.actions)"`, - "engine: starlark\n rule: return True", 1) - require.NoError(t, os.WriteFile(filepath.Join(directory, "guard.yaml"), []byte(document), 0o600)) - gate, err := loadTerraformPlanPolicy(&schema.ConfigAndStacksInfo{SubCommand: "plan"}) - require.NoError(t, err) - require.NotNil(t, gate) - decision, err := gate.Engine.Evaluate(t.Context(), &policy.Request{ - Kind: policy.TerraformPlan, Input: json.RawMessage(`{"format_version":"1.2","planned_values":{}}`), - }, gate.Rules) - require.NoError(t, err) - require.ErrorIs(t, policy.EnforceDecision(decision), errUtils.ErrPolicyDenied) -} - func TestStarlarkAffectedPolicyGate(t *testing.T) { basePath, base := affectedPolicyRepo(t, map[string]string{"stacks/prod.yaml": affectedTestManifest}) headPath, head := affectedPolicyRepo(t, map[string]string{"README.md": "removed stack"}) diff --git a/internal/exec/template_funcs_component.go b/internal/exec/template_funcs_component.go index bed683d9049..172580d27da 100644 --- a/internal/exec/template_funcs_component.go +++ b/internal/exec/template_funcs_component.go @@ -12,6 +12,7 @@ import ( errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/auth" authdeferred "github.com/cloudposse/atmos/pkg/auth/deferred" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/deferred" log "github.com/cloudposse/atmos/pkg/logger" @@ -95,12 +96,12 @@ func componentFunc( var valueCache *deferred.ValueCache if authdeferred.IsDeferred(atmosConfig.AuthManager) { var err error - resolvedAuthMgr, valueCache, err = deferredTargetAuthAndCache(atmosConfig, component, stack, &authContextWrapper{stackInfo: configAndStacksInfo}) + resolvedAuthMgr, valueCache, err = deferredTargetAuthAndCache(atmosConfig, component, stack, &terraformcomponent.AuthContextWrapper{StackInfo: configAndStacksInfo}) if err != nil { return nil, err } } else { - resolvedAuthMgr = resolveComponentFuncAuthManager(atmosConfig, configAndStacksInfo, component, stack, resolveAuthManagerForNestedComponent) + resolvedAuthMgr = resolveComponentFuncAuthManager(atmosConfig, configAndStacksInfo, component, stack, terraformExecutor.ResolveAuthManagerForNestedComponent) } kinds := refcache.KindsFor(maskOnly) @@ -286,7 +287,7 @@ func resolveComponentFuncAuthManager( var parentAuthMgr auth.AuthManager if configAndStacksInfo != nil && configAndStacksInfo.AuthContext != nil { - parentAuthMgr = newAuthContextWrapper(configAndStacksInfo.AuthContext) + parentAuthMgr = terraformcomponent.NewAuthContextWrapper(configAndStacksInfo.AuthContext) } if configAndStacksInfo != nil && configAndStacksInfo.AuthDisabled { @@ -297,8 +298,8 @@ func resolveComponentFuncAuthManager( if err != nil { log.Debug( "Auth does not exist for atmos.Component target, using enclosing component's AuthManager", - logKeyComponent, component, - logKeyStack, stack, + terraformcomponent.LogKeyComponent, component, + terraformcomponent.LogKeyStack, stack, "error", err, ) return parentAuthMgr diff --git a/internal/exec/terraform.go b/internal/exec/terraform.go index 5a5afa2c001..eee36a8b1be 100644 --- a/internal/exec/terraform.go +++ b/internal/exec/terraform.go @@ -3,437 +3,565 @@ package exec import ( "context" "fmt" - "maps" - "os" - "github.com/spf13/cobra" - "github.com/spf13/viper" + "github.com/hashicorp/terraform-config-inspect/tfconfig" + cobra "github.com/spf13/cobra" errUtils "github.com/cloudposse/atmos/errors" - "github.com/cloudposse/atmos/pkg/auth/broker" - cfg "github.com/cloudposse/atmos/pkg/config" + auth "github.com/cloudposse/atmos/pkg/auth" + "github.com/cloudposse/atmos/pkg/component" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" "github.com/cloudposse/atmos/pkg/dependencies" - log "github.com/cloudposse/atmos/pkg/logger" - metricsprocess "github.com/cloudposse/atmos/pkg/metrics/process" + "github.com/cloudposse/atmos/pkg/dependency" "github.com/cloudposse/atmos/pkg/perf" - "github.com/cloudposse/atmos/pkg/proexec" - // Import backend provisioner to register S3 provisioner. - _ "github.com/cloudposse/atmos/pkg/provisioner/backend" - "github.com/cloudposse/atmos/pkg/schema" - tfcache "github.com/cloudposse/atmos/pkg/terraform/cache" - tfoutput "github.com/cloudposse/atmos/pkg/terraform/output" - tfplugin "github.com/cloudposse/atmos/pkg/terraform/plugin" + schema "github.com/cloudposse/atmos/pkg/schema" ) -const ( - terraformPluginCacheDirEnv = tfplugin.CacheDirEnv - terraformPluginCacheMayBreakLockFileEnv = tfplugin.CacheMayBreakLockFileEnv - - // BeforeTerraformInitEvent is the hook event name for provisioners that run before terraform init. - // This matches the hook event registered by backend provisioners in pkg/provisioner/backend/backend.go. - // See pkg/hooks/event.go (hooks.BeforeTerraformInit) for the canonical definition. - beforeTerraformInitEvent = "before.terraform.init" - - // AfterTerraformInitEvent is the hook event name for provisioners that run after a - // successful terraform init (e.g. the multi-platform providers-lock hook in - // pkg/provisioner/lock). It is dispatched with a TerraformExecContext so the provisioner - // can run a terraform subcommand against the live env, RC, and working directory. - afterTerraformInitEvent = "after.terraform.init" - - subcommandApply = "apply" - subcommandDeploy = "deploy" - subcommandInit = "init" - subcommandWorkspace = "workspace" - subcommandProvidersLock = "providers-lock" - - autoApproveFlag = "-auto-approve" - outFlag = "-out" - varFileFlag = "-var-file" - skipTerraformLockFileFlag = "--skip-lock-file" - forceFlag = "--force" - everythingFlag = "--everything" - detailedExitCodeFlag = "-detailed-exitcode" - lockTimeoutFlag = "-lock-timeout" - lockFlag = "-lock" - parallelismFlag = "-parallelism" - refreshFlag = "-refresh" - compactWarningsFlag = "-compact-warnings" - // Terraform's -detailed-exitcode documents this as "succeeded, there is a - // diff", distinct from 0 (no changes) and 1 (error). - detailedExitCodeChangesDetected = 2 - logFieldComponent = "component" - dirPermissions = 0o755 -) +// The host wires shared Atmos services into the Terraform component and registers it. +// Terraform owns its lifecycle; this adapter preserves existing host entry points. +func init() { + terraformExecutor = NewTerraformExecutor() -// resolveAndInstallToolchainDeps resolves and installs toolchain dependencies for a terraform component. -// Returns the ToolchainEnvironment for resolving executable paths downstream. -func resolveAndInstallToolchainDeps(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (*dependencies.ToolchainEnvironment, error) { - defer perf.Track(atmosConfig, "exec.resolveAndInstallToolchainDeps")() + stateGetter = terraformExecutor.StateGetter() + outputGetter = terraformExecutor.OutputGetter() - tenv, err := dependencies.ForComponent(atmosConfig, "terraform", info.StackSection, info.ComponentSection) - if err != nil { - return nil, err + if err := component.Register(terraformcomponent.New(terraformExecutor)); err != nil { + panic(err) } +} + +// NewTerraformExecutor binds a new Terraform runtime to the shared Atmos host services. +// Each executor owns its service bindings; creating one does not change the registry. +func NewTerraformExecutor() *terraformcomponent.Executor { + defer perf.Track(nil, "exec.NewTerraformExecutor")() + return terraformcomponent.NewExecutor(terraformcomponent.Services{ + OperationServices: terraformOperationServices{}, + ExecuteDescribeComponent: ExecuteDescribeComponent, + ExecuteDescribeStacksWithAuthDisabled: ExecuteDescribeStacksWithAuthDisabled, + ExecuteDescribeStacksWithAuthDisabledAndMocks: ExecuteDescribeStacksWithAuthDisabledAndMocks, + ExecuteDescribeStacksWithMocks: ExecuteDescribeStacksWithMocks, + ExecuteShellCommand: ExecuteShellCommand, + ExecuteTerraform: ExecuteTerraform, + GenerateFilesForComponent: GenerateFilesForComponent, + GetAffectedComponents: func(args *component.DescribeAffectedCmdArgs) ([]schema.Affected, error) { + return GetAffectedComponents((*DescribeAffectedCmdArgs)(args)) + }, + GetEagerEvaluationSetting: GetEagerEvaluationSetting, + NewDependencyParserWithDelimiter: func(builder *dependency.GraphBuilder, nodeMap, targetStates map[string]string, leftDelim string) terraformcomponent.DependencyParser { + return NewDependencyParserWithDelimiter(builder, nodeMap, targetStates, leftDelim) + }, + ProcessStacks: ProcessStacks, + ValidateComponent: ValidateComponent, + AttachComponentReporting: attachComponentReporting, + CreateAndAuthenticateAuthManager: createAndAuthenticateAuthManager, + DefaultAuthManagerCreator: func(identity string, config *schema.AuthConfig, selectValue string, atmosConfig *schema.AtmosConfiguration, context auth.ReExecContext) (auth.AuthManager, error) { + return defaultAuthManagerCreator(identity, config, selectValue, atmosConfig, context) + }, + DefaultMergedAuthConfigGetter: func(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (*schema.AuthConfig, error) { + return defaultMergedAuthConfigGetter(config, info) + }, + DeferredTargetAuth: deferredTargetAuth, + DeferredTargetAuthAndCache: deferredTargetAuthAndCache, + EnvVarFromList: envVarFromList, + ExecTerraformShellCommand: execTerraformShellCommand, + GetMergedAuthConfig: getMergedAuthConfig, + HasYqDefault: hasYqDefault, + IsComponentEnabled: isComponentEnabled, + MapCIExitCode: mapCIExitCode, + ResolveIdentityConfigError: resolveIdentityConfigError, + ResolvedTargetAuthContext: resolvedTargetAuthContext, + ShouldUploadStatus: shouldUploadStatus, + StoreAutoDetectedIdentity: storeAutoDetectedIdentity, + UploadStatus: uploadStatus, + }) +} - return tenv, nil +// ExecuteTerraform is a compatibility entry point for existing CLI, scheduler, +// plan-diff, and migration callers. All of them now select the registered provider. +// +//nolint:gocritic // Preserve the public value-based API while routing existing callers through the registry. +func ExecuteTerraform(info schema.ConfigAndStacksInfo, opts ...ShellCommandOption) error { + defer perf.Track(nil, "exec.DispatchTerraform")() + + provider, ok := component.GetProvider("terraform") + if !ok { + return fmt.Errorf("%w: terraform", errUtils.ErrComponentProviderNotFound) + } + return provider.Execute(&component.ExecutionContext{ + Context: shellCommandContext(opts...), Options: opts, + ComponentType: "terraform", Component: info.ComponentFromArg, + Stack: info.Stack, Command: info.Command, SubCommand: info.SubCommand, + ComponentConfig: info.ComponentSection, ConfigAndStacksInfo: info, + Args: info.AdditionalArgsAndFlags, + }) } -// startManagedTerraformCache starts the registry cache for this execution and returns -// its cleanup. It returns a no-op cleanup when caching is disabled or when the caller -// owns the cache lifecycle (info.TerraformCacheExternal, e.g. a graph-backed bulk run -// sharing one proxy across components). On a trust failure the proxy is closed before -// returning so it does not leak. -func startManagedTerraformCache(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (*tfcache.Setup, func(), error) { - defer perf.Track(atmosConfig, "exec.startManagedTerraformCache")() +var terraformExecutor *terraformcomponent.Executor - if info.TerraformCacheExternal { - return nil, func() {}, nil - } - setup, cleanup, err := tfcache.StartForExecution(context.Background(), atmosConfig) - if err != nil { - return nil, func() {}, err - } - if setup == nil { - return nil, cleanup, nil - } - info.TerraformCache = setup - return setup, cleanup, nil +var defaultMergedAuthConfigGetter = getMergedAuthConfig + +var stateGetter terraformcomponent.TerraformStateGetter + +var outputGetter terraformcomponent.TerraformOutputGetter + +type terraformOperationServices struct{} + +// ProcessStacks resolves the selected component through the host stack-processing pipeline. +// +//nolint:revive,gocritic // Match the existing shared stack-processing contract. +func (terraformOperationServices) ProcessStacks(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo schema.ConfigAndStacksInfo, checkStack bool, processTemplates bool, processYamlFunctions bool, skip []string, authManager auth.AuthManager) (schema.ConfigAndStacksInfo, error) { + defer perf.Track(atmosConfig, "exec.terraformOperationServices.ProcessStacks")() + + return ProcessStacks(atmosConfig, configAndStacksInfo, checkStack, processTemplates, processYamlFunctions, skip, authManager) } -// ExecuteTerraform executes terraform commands. -// Optional ShellCommandOption values are forwarded to the final ExecuteShellCommand call. +// FindStacksMap returns processed stacks, raw manifests, and their deferred evaluation contexts. // -//nolint:revive,cyclop,funlen,gocritic // Existing pipeline complexity; reporting adds only a deferred snapshot. -func ExecuteTerraform(info schema.ConfigAndStacksInfo, opts ...ShellCommandOption) (resultErr error) { - defer perf.Track(nil, "exec.ExecuteTerraform")() - defer attachComponentReporting(&resultErr, &info, "terraform", info.SubCommand) - - // Captured before any pipeline step can rewrite info.SubCommand (e.g. - // handleDeploySubcommand rewrites "deploy" to "apply" in place so - // downstream terraform invocation logic can treat them uniformly). The - // exec-metadata record must report the command the user actually typed, - // not its internal apply-equivalent rewrite. - originalSubCommand := info.SubCommand - - log.Debug( - "ExecuteTerraform entry", - "SubCommand", info.SubCommand, - "ComponentFromArg", info.ComponentFromArg, - "FinalComponent", info.FinalComponent, - "Stack", info.Stack, - "StackFromArg", info.StackFromArg, - ) - - info.CliArgs = []string{"terraform", info.SubCommand, info.SubCommand2} - planPolicy, err := loadTerraformPlanPolicy(&info) - if err != nil { - return err - } +//nolint:revive // Preserve raw stacks and deferred contexts in the shared contract. +func (terraformOperationServices) FindStacksMap(atmosConfig *schema.AtmosConfiguration, ignoreMissingFiles bool) (map[string]any, map[string]map[string]any, AllStacksDeferredContexts, error) { + defer perf.Track(atmosConfig, "exec.terraformOperationServices.FindStacksMap")() - atmosConfig, err := cfg.InitCliConfig(info, true) - if err != nil { - return err - } + return FindStacksMap(atmosConfig, ignoreMissingFiles) +} - if info.NeedHelp { - return nil - } +// ProcessTmpl renders a template with the host template engine and missing-value policy. +func (terraformOperationServices) ProcessTmpl(atmosConfig *schema.AtmosConfiguration, tmplName string, tmplValue string, tmplData any, ignoreMissingTemplateValues bool) (string, error) { + defer perf.Track(atmosConfig, "exec.terraformOperationServices.ProcessTmpl")() - // Resolve the terraform executable (e.g. "terraform", "tofu", or a custom path). - resolveTerraformCommand(&atmosConfig, &info) + return ProcessTmpl(atmosConfig, tmplName, tmplValue, tmplData, ignoreMissingTemplateValues) +} - // Short-circuit for `terraform version` – no stack processing required. - if info.SubCommand == "version" { - return handleVersionSubcommand(&atmosConfig, &info) - } +// ProcessTmplWithDatasources renders a template with component-scoped data sources and settings. +// +//nolint:revive,gocritic // Match the shared template service contract. +func (terraformOperationServices) ProcessTmplWithDatasources(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo *schema.ConfigAndStacksInfo, settingsSection schema.Settings, tmplName string, tmplValue string, tmplData any, ignoreMissingTemplateValues bool) (string, error) { + defer perf.Track(atmosConfig, "exec.terraformOperationServices.ProcessTmplWithDatasources")() - // Test fixtures may start the emulator that authentication needs. Provision - // their workdir and run their hooks first, retaining per-invocation source state. - fixtureState, err := prepareTerraformTestFixtures(&atmosConfig, &info, opts...) - if err != nil { - return err - } + return ProcessTmplWithDatasources(atmosConfig, configAndStacksInfo, settingsSection, tmplName, tmplValue, tmplData, ignoreMissingTemplateValues) +} - // Set up authentication (merge global + component auth, create AuthManager, inject bridge). - authManager, err := setupTerraformAuth(&atmosConfig, &info) - if err != nil { - return err - } +// ProcessCustomYamlTags evaluates Atmos YAML functions using the selected stack and component context. +func (terraformOperationServices) ProcessCustomYamlTags(atmosConfig *schema.AtmosConfiguration, input schema.AtmosSectionMapType, currentStack string, skip []string, stackInfo *schema.ConfigAndStacksInfo) (schema.AtmosSectionMapType, error) { + defer perf.Track(atmosConfig, "exec.terraformOperationServices.ProcessCustomYamlTags")() - // Process and validate stack configuration. - shouldProcess, shouldCheckStack := shouldProcessStacks(&info) - if shouldProcess { - info, err = ProcessStacks(&atmosConfig, info, shouldCheckStack, info.ProcessTemplates, info.ProcessFunctions, info.Skip, authManager) - if err != nil { - return err - } - } - // Stack processing rebuilds ComponentSection; keep the provisioning markers - // so implicit init cannot re-copy the source over fixture-generated files. - maps.Copy(info.ComponentSection, fixtureState) - if shouldCheckStack && len(info.Stack) < 1 { - return errUtils.ErrMissingStack - } - if !info.ComponentIsEnabled { - log.Info("Component is not enabled and skipped", logFieldComponent, info.ComponentFromArg) - return nil - } - planPolicy, err = bindTerraformPolicy(planPolicy, &info, atmosConfig.Templates.Settings.Delimiters...) - if err != nil { - return err - } + return ProcessCustomYamlTags(atmosConfig, input, currentStack, skip, stackInfo) +} - // Ensure ambient credential brokers (e.g., Atmos Pro github/sts) have provisioned before the - // subprocess environment is built, so terraform's own `git::` module fetches can read private - // repos via the inherited GIT_CONFIG_* rewrites. Process-once and gated (CI + configured). - broker.EnsureCredentials(context.Background(), &atmosConfig) - - // Start the Terraform registry cache (no-op when disabled or caller-owned). The - // ephemeral proxy must outlive the whole pipeline, so its Close is deferred here. - // Env assembly merges its CLI-config contribution into the generated RC. - cacheSetup, closeCache, err := startManagedTerraformCache(&atmosConfig, &info) - if err != nil { - return err - } - if cacheSetup != nil { - defer closeCache() - } +// ProcessCommandLineArgs resolves command flags and passthrough arguments into component execution metadata. +func (terraformOperationServices) ProcessCommandLineArgs(componentType string, cmd *cobra.Command, args []string, additionalArgsAndFlags []string) (schema.ConfigAndStacksInfo, error) { + defer perf.Track(nil, "exec.terraformOperationServices.ProcessCommandLineArgs")() - // Resolve paths, install toolchain, write varfiles, validate, run hooks, and build env. - execCtx, err := prepareComponentExecution(shellCommandContext(opts...), shellCommandOutputWriters(opts...), &atmosConfig, &info, shouldProcess) - if err != nil { - return err - } - execCtx.planPolicy = planPolicy - - // Remove the temporary Terraform CLI config (TF_CLI_CONFIG_FILE) after the whole - // pipeline (init, workspace, plan/apply) completes. Registered here, not inside the - // pipeline, so the file survives every subprocess and is cleaned up on early errors. - if info.RCCleanup != nil { - defer func() { - if cleanupErr := info.RCCleanup(); cleanupErr != nil { - log.Debug("Failed to remove temporary Terraform CLI config", "error", cleanupErr) - } - }() - } + return ProcessCommandLineArgs(componentType, cmd, args, additionalArgsAndFlags) +} - // Persist auth context so PostRunE hooks (e.g. store hooks that read - // terraform outputs) can reuse the credentials established during this - // execution. Without this, hooks create a fresh info with no auth. - SetLastAuthContext(info.AuthContext, info.AuthManager) +// ResolveDeferredYamlFunctions evaluates deferred values in the requested component sections. +// +//nolint:revive // Match the shared deferred-resolution service contract. +func (terraformOperationServices) ResolveDeferredYamlFunctions(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo *schema.ConfigAndStacksInfo, settingsSectionStruct *schema.Settings, componentTemplateContext map[string]any, skip []string, evalSections []string, warnings ...func(DegradationWarning)) error { + defer perf.Track(atmosConfig, "exec.terraformOperationServices.ResolveDeferredYamlFunctions")() - // Test hooks already ran before authentication to bootstrap their fixtures. - if fixtureState == nil { - if err := runPreparedTerraformHooks(&atmosConfig, &info, opts...); err != nil { - return err - } - } + return resolveDeferredYamlFunctions(atmosConfig, configAndStacksInfo, settingsSectionStruct, componentTemplateContext, skip, evalSections, warnings...) +} - // Run the full command pipeline: init, arg build, workspace, execute, cleanup. - // Forward caller-provided options (e.g. CI stdout/stderr capture) alongside the environment option. - opts = append(opts, WithEnvironment(info.SanitizedEnv)) - err = executeCommandPipeline(&atmosConfig, &info, execCtx, opts...) - if err == nil { - // A successful Terraform command can create, change, or remove state. Drop - // any preflight snapshot so a dependent graph node reads the current outputs. - invalidateTerraformStateCache(info.Stack, info.ComponentFromArg) - tfoutput.InvalidateComponentOutputs(info.Stack, info.ComponentFromArg) - } +// PrepareConfigurationValues defers Starlark evaluation and returns the callback that completes resolution. +func (terraformOperationServices) PrepareConfigurationValues(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, skip []string, sections []string) ([]string, func() error) { + defer perf.Track(nil, "exec.terraformOperationServices.PrepareConfigurationValues")() - captureExecMetadataSync(&atmosConfig, originalSubCommand, &info, execMetadataSyncParams{ - Cmd: invokingCommandFromOpts(opts...), - Parser: execMetadataParserFromOpts(opts...), - Err: err, - }) + return prepareConfigurationValues(config, info, skip, sections) +} + +// StackLocalsForComponent retrieves manifest locals with component-type locals applied over root locals. +func (terraformOperationServices) StackLocalsForComponent(rawStackConfigs map[string]map[string]any, stackFile string, componentType string) map[string]any { + defer perf.Track(nil, "exec.terraformOperationServices.StackLocalsForComponent")() + + return stackLocalsForComponent(rawStackConfigs, stackFile, componentType) +} - return err +// CloneComponentSectionWithOverrides copies component configuration before applying resolved overrides. +func (terraformOperationServices) CloneComponentSectionWithOverrides(raw map[string]any, overrides map[string]any) map[string]any { + defer perf.Track(nil, "exec.terraformOperationServices.CloneComponentSectionWithOverrides")() + + return cloneComponentSectionWithOverrides(raw, overrides) +} + +// ContainsStarlark reports whether a configuration value contains Starlark expressions. +func (terraformOperationServices) ContainsStarlark(value any) bool { + defer perf.Track(nil, "exec.terraformOperationServices.ContainsStarlark")() + + return containsStarlark(value) +} + +// GenerateFilesForComponent invokes automatic generation for the prepared component working directory. +func (terraformOperationServices) GenerateFilesForComponent(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { + defer perf.Track(nil, "exec.terraformOperationServices.GenerateFilesForComponent")() + + return GenerateFilesForComponent(config, info, workingDir) } -// captureExecMetadataSync reports an execution record to Atmos Pro for the -// synchronous allowlist (terraform plan/apply/deploy), blocking briefly per -// proexec.CaptureSync's own configurable timeout. No-op for every other -// terraform subcommand. SubCommand must be the subcommand the user actually -// invoked (captured before handleDeploySubcommand's in-place "deploy" -> -// "apply" rewrite), so a `deploy` invocation is reported as `deploy`, not -// misattributed to `apply`. +// BuildTerraformWorkspace resolves the workspace from component metadata and Atmos naming conventions. // -// Multi-component invocations (info.NodeHooks != nil, wired by -// cmd/terraform/utils.go's wirePerComponentHook for --affected/--all/query -// runs) are skipped here: this function fires once per graph node, but -// FR-006a requires exactly one execution record for the whole invocation. -// Cmd/terraform/utils.go's terraformNodeHooks accumulates each node's -// identity/outcome instead and fires a single aggregate CaptureSync call -// after the graph run completes (research.md Decisions 11/17). +//nolint:gocritic // Preserve the existing value-based naming contract. +func (terraformOperationServices) BuildTerraformWorkspace(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo schema.ConfigAndStacksInfo) (string, error) { + defer perf.Track(atmosConfig, "exec.terraformOperationServices.BuildTerraformWorkspace")() + + return BuildTerraformWorkspace(atmosConfig, configAndStacksInfo) +} + +// ExecuteTerraform routes operation-service executions through the registered Terraform provider. // -// Structured plugin.TerraformOutputData enrichment described for User Story 3 -// is obtained via parser, a closure supplied by cmd/terraform through -// WithExecMetadataParser (research.md Decision 18) — internal/exec never -// imports pkg/ci/plugins/terraform directly, since pkg/ci/internal/plugin is -// only importable from within the pkg/ci tree and, independently, -// pkg/ci/plugins/terraform itself imports internal/exec (a confirmed import -// cycle). Parser is nil for callers that don't wire one (e.g. tests), in -// which case data is reported as nil, same as before this data was wired. -// ExecMetadataSyncParams bundles the invoking Cobra command, the optional -// structured-output parser, and the command's own error — the three values -// captureExecMetadataSync needs beyond atmosConfig/subCommand/info, grouped -// to stay under the linter's argument-count limit. -type execMetadataSyncParams struct { - Cmd *cobra.Command - Parser func(subCommand string, exitCode int, output string) any - Err error -} - -func captureExecMetadataSync(atmosConfig *schema.AtmosConfiguration, subCommand string, info *schema.ConfigAndStacksInfo, params execMetadataSyncParams) { - commandPath := "atmos terraform " + subCommand - if !proexec.IsSyncCommand(commandPath) { - return - } +//nolint:gocritic // Preserve the value-based registry dispatch API. +func (terraformOperationServices) ExecuteTerraform(info schema.ConfigAndStacksInfo, opts ...ShellCommandOption) error { + defer perf.Track(nil, "exec.terraformOperationServices.ExecuteTerraform")() - if info.NodeHooks != nil { - log.Debug("Skipping per-node exec-metadata sync capture: part of a multi-component run.", "component", info.ComponentFromArg) - return - } + return ExecuteTerraform(info, opts...) +} - exitCode := errUtils.GetExitCode(params.Err) - if params.Err != nil && exitCode == 0 { - exitCode = 1 - } +var terraformOperations = terraformcomponent.NewOperations(terraformOperationServices{}) - // FR-006e: TerraformExecData.exit_code must report the terraform/tofu subprocess's - // real, pre-CI-remap exit code (info.ExecMetadataRawExitCode, set by - // executeMainTerraformCommand), not the post-remap/neutralized exitCode above — - // that value is reserved for the base envelope's own exit_code (FR-003), which - // is unaffected by this. Falls back to exitCode when the raw field was never - // populated (e.g. the pipeline failed before the main command ran at all, or a - // test invokes captureExecMetadataSync directly without going through - // executeMainTerraformCommand). - rawExitCode := info.ExecMetadataRawExitCode - if rawExitCode == 0 && exitCode != 0 { - rawExitCode = exitCode - } +// ExecuteGenerateBackend delegates to the Terraform component implementation. +func ExecuteGenerateBackend(opts *GenerateBackendOptions, atmosConfig *schema.AtmosConfiguration) error { + defer perf.Track(atmosConfig, "exec.ExecuteGenerateBackend")() - var args []string - if info.ComponentFromArg != "" { - args = []string{info.ComponentFromArg} - } + return terraformOperations.ExecuteGenerateBackend(opts, atmosConfig) +} - // Flags MUST be sourced from the invoking Cobra command's own record of - // explicitly-set flags, not info.AdditionalArgsAndFlags — that field is a - // pass-through-args collection that never contains atmos-recognized flags - // like -s/--stack and has --upload-status stripped out of it before this - // call runs, so it structurally cannot represent "the flags actually - // passed" (research.md Decision 14). - flags := proexec.FlagsFromCommand(params.Cmd) - - var data any - if params.Parser != nil { - data = params.Parser(subCommand, rawExitCode, info.ExecMetadataRawOutput) - } +// ExecuteGeneratePlanfile delegates to the Terraform component implementation. +func ExecuteGeneratePlanfile(opts *PlanfileOptions, atmosConfig *schema.AtmosConfiguration) error { + defer perf.Track(atmosConfig, "exec.ExecuteGeneratePlanfile")() - // info.ExecMetadataRawMetrics is typed `any` (not *process.ProcessMetrics) - // to avoid an import cycle — pkg/metrics/process imports pkg/schema for - // its own settings gate, so pkg/schema cannot import it back. A nil or - // mistyped value here is silently treated as "no override", falling back - // to proexec.buildRecord's own process.SelfUsageSoFar() default. - metrics, _ := info.ExecMetadataRawMetrics.(*metricsprocess.ProcessMetrics) + return terraformOperations.ExecuteGeneratePlanfile(opts, atmosConfig) +} - in := &proexec.ExecRecordInput{Command: "terraform " + subCommand, Args: args, Flags: flags, ExitCode: exitCode, Data: data, Metrics: metrics} - if syncErr := proexec.CaptureSync(atmosConfig, in); syncErr != nil { - log.Debug("Exec-metadata sync capture returned an error.", "error", syncErr) - } +// ExecuteGenerateVarfile delegates to the Terraform component implementation. +func ExecuteGenerateVarfile(opts *VarfileOptions, atmosConfig *schema.AtmosConfiguration) error { + defer perf.Track(atmosConfig, "exec.ExecuteGenerateVarfile")() + + return terraformOperations.ExecuteGenerateVarfile(opts, atmosConfig) } -// configurePluginCache returns environment variables for Terraform plugin caching. -// It checks if the user has already set TF_PLUGIN_CACHE_DIR (via OS env or global env), -// and if not, configures automatic caching based on atmosConfig.Components.Terraform.PluginCache. -func configurePluginCache(atmosConfig *schema.AtmosConfiguration) []string { - override, overrideSet := pluginCacheOverride(atmosConfig) - cache := tfplugin.Resolve(atmosConfig, override, overrideSet) - if !cache.Automatic { - if cache.Directory != "" { - log.Debug("TF_PLUGIN_CACHE_DIR already set, skipping automatic plugin cache configuration") - } - return nil - } - return []string{ - fmt.Sprintf("%s=%s", terraformPluginCacheDirEnv, cache.Directory), - fmt.Sprintf("%s=true", terraformPluginCacheMayBreakLockFileEnv), - } +// ExecuteTerraformGenerateBackendCmd delegates to the Terraform component implementation. +func ExecuteTerraformGenerateBackendCmd(cmd interface{}, args []string) error { + defer perf.Track(nil, "exec.ExecuteTerraformGenerateBackendCmd")() + + return terraformcomponent.ExecuteTerraformGenerateBackendCmd(cmd, args) } -// pluginCacheOverride resolves explicit user configuration with the historical -// command-path precedence: process environment first, then atmos.yaml global env. -func pluginCacheOverride(atmosConfig *schema.AtmosConfiguration) (string, bool) { - if value, ok := os.LookupEnv(terraformPluginCacheDirEnv); ok { - return value, true - } - if atmosConfig != nil { - value, ok := atmosConfig.Env[terraformPluginCacheDirEnv] - return value, ok - } - return "", false +// ExecuteTerraformGenerateBackends delegates to the Terraform component implementation. +func ExecuteTerraformGenerateBackends(atmosConfig *schema.AtmosConfiguration, fileTemplate string, format string, stacks []string, components []string) error { + defer perf.Track(atmosConfig, "exec.ExecuteTerraformGenerateBackends")() + + return terraformOperations.ExecuteTerraformGenerateBackends(atmosConfig, fileTemplate, format, stacks, components) } -// disableTerraformPluginCacheForExecution removes Terraform/OpenTofu plugin-cache -// configuration from this execution. This is intentionally scoped to the current -// subprocess environment and config copy so concurrent graph runs can keep full -// scheduler parallelism without racing on a shared provider cache. -func disableTerraformPluginCacheForExecution(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) { - if atmosConfig == nil || info == nil { - return - } - // Isolated stack lifecycle workers inherit the same cache policy as the - // in-process Terraform scheduler, including during planfile verification. - workerConfig := viper.New() - _ = workerConfig.BindEnv(tfplugin.DisableCacheEnv, tfplugin.DisableCacheEnv) - if workerConfig.GetString(tfplugin.DisableCacheEnv) == "true" { - info.DisablePluginCache = true - } - if !info.DisablePluginCache { - return - } +// ExecuteTerraformGeneratePlanfile delegates to the Terraform component implementation. +func ExecuteTerraformGeneratePlanfile(options *PlanfileOptions, info *schema.ConfigAndStacksInfo) error { + defer perf.Track(nil, "exec.ExecuteTerraformGeneratePlanfile")() - atmosConfig.Components.Terraform.PluginCache = false - atmosConfig.Components.Terraform.PluginCacheDir = "" + return terraformOperations.ExecuteTerraformGeneratePlanfile(options, info) +} - delete(atmosConfig.Env, terraformPluginCacheDirEnv) - delete(atmosConfig.Env, terraformPluginCacheMayBreakLockFileEnv) - delete(info.ComponentEnvSection, terraformPluginCacheDirEnv) - delete(info.ComponentEnvSection, terraformPluginCacheMayBreakLockFileEnv) +// ExecuteTerraformGeneratePlanfileOld delegates to the Terraform component implementation. +func ExecuteTerraformGeneratePlanfileOld(cmd *cobra.Command, args []string) error { + defer perf.Track(nil, "exec.ExecuteTerraformGeneratePlanfileOld")() - baseEnv := info.SanitizedEnv - if baseEnv == nil { - baseEnv = os.Environ() - } - info.SanitizedEnv = removeEnvKeys(baseEnv, terraformPluginCacheDirEnv, terraformPluginCacheMayBreakLockFileEnv) - info.ComponentEnvList = removeEnvKeys(info.ComponentEnvList, terraformPluginCacheDirEnv, terraformPluginCacheMayBreakLockFileEnv) + return terraformOperations.ExecuteTerraformGeneratePlanfileOld(cmd, args) } -func removeEnvKeys(env []string, keys ...string) []string { - if len(env) == 0 || len(keys) == 0 { - return env - } - skip := make(map[string]struct{}, len(keys)) - for _, key := range keys { - skip[key] = struct{}{} - } +// ExecuteTerraformGenerateRequiredProvidersCmd delegates to the Terraform component implementation. +func ExecuteTerraformGenerateRequiredProvidersCmd(cmd *cobra.Command, args []string) error { + defer perf.Track(nil, "exec.ExecuteTerraformGenerateRequiredProvidersCmd")() - filtered := env[:0] - for _, entry := range env { - key := envKey(entry) - if _, ok := skip[key]; ok { - continue - } - filtered = append(filtered, entry) - } - return filtered + return terraformOperations.ExecuteTerraformGenerateRequiredProvidersCmd(cmd, args) } -func envKey(entry string) string { - for i := 0; i < len(entry); i++ { - if entry[i] == '=' { - return entry[:i] - } - } - return entry +// ExecuteTerraformGenerateVarfileCmd delegates to the Terraform component implementation. +func ExecuteTerraformGenerateVarfileCmd(cmd interface{}, args []string) error { + defer perf.Track(nil, "exec.ExecuteTerraformGenerateVarfileCmd")() + + return terraformcomponent.ExecuteTerraformGenerateVarfileCmd(cmd, args) +} + +// ExecuteTerraformGenerateVarfiles delegates to the Terraform component implementation. +func ExecuteTerraformGenerateVarfiles(atmosConfig *schema.AtmosConfiguration, fileTemplate string, format string, stacks []string, components []string) error { + defer perf.Track(atmosConfig, "exec.ExecuteTerraformGenerateVarfiles")() + + return terraformOperations.ExecuteTerraformGenerateVarfiles(atmosConfig, fileTemplate, format, stacks, components) +} + +// TerraformPlanDiff delegates to the Terraform component implementation. +func TerraformPlanDiff(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { + defer perf.Track(atmosConfig, "exec.TerraformPlanDiff")() + + return terraformOperations.TerraformPlanDiff(atmosConfig, info) +} + +// VerifyPlanfile delegates to the Terraform component implementation. +func VerifyPlanfile(info *schema.ConfigAndStacksInfo, storedPlanFile string, mode schema.PlanfileVerifyMode) error { + defer perf.Track(nil, "exec.VerifyPlanfile")() + + return terraformOperations.VerifyPlanfile(info, storedPlanFile, mode) +} + +type ( + backendConfig = terraformcomponent.BackendConfig + + terraformPlanDiffExecutor = terraformcomponent.TerraformPlanDiffExecutor + terraformPlanDiffExecutorFunc = terraformcomponent.TerraformPlanDiffExecutorFunc + PlanFileOptions = terraformcomponent.PlanFileOptions + PlanfileOptions = terraformcomponent.PlanfileOptions +) + +var ( + ErrInvalidFormat = terraformcomponent.ErrInvalidFormat + ErrCreatingTempDirectory = terraformcomponent.ErrCreatingTempDirectory + ErrCreatingIntermediateSubdirectories = terraformcomponent.ErrCreatingIntermediateSubdirectories + ErrGettingJsonForPlanfile = terraformcomponent.ErrGettingJsonForPlanfile + ErrConvertingJsonToGoType = terraformcomponent.ErrConvertingJsonToGoType + ErrNoComponent = terraformcomponent.ErrNoComponent + ErrNoJSONOutput = terraformcomponent.ErrNoJSONOutput +) + +// IsOpenTofu is the compatibility entry point for Terraform tool detection. +func IsOpenTofu(config *schema.AtmosConfiguration, tools *dependencies.ToolchainEnvironment) bool { + defer perf.Track(nil, "exec.IsOpenTofu")() + + return terraformcomponent.IsOpenTofu(config, tools) +} + +// GetTerraformEnvCliArgs parses Terraform's CLI environment arguments. +func GetTerraformEnvCliArgs() []string { + defer perf.Track(nil, "exec.GetTerraformEnvCliArgs")() + + return terraformcomponent.GetTerraformEnvCliArgs() +} + +// GetTerraformEnvCliVars resolves variable assignments from Terraform's CLI environment arguments. +func GetTerraformEnvCliVars() (map[string]any, error) { + defer perf.Track(nil, "exec.GetTerraformEnvCliVars")() + + return terraformcomponent.GetTerraformEnvCliVars() +} + +// SetLastAuthContext records credentials for post-execution hooks. +func SetLastAuthContext(ctx *schema.AuthContext, manager any) { + defer perf.Track(nil, "exec.SetLastAuthContext")() + + terraformcomponent.SetLastAuthContext(ctx, manager) +} + +// GetLastAuthContext retrieves credentials recorded for post-execution hooks. +func GetLastAuthContext() (*schema.AuthContext, any) { + defer perf.Track(nil, "exec.GetLastAuthContext")() + + return terraformcomponent.GetLastAuthContext() +} + +// ClearLastAuthContext clears post-execution credentials. +func ClearLastAuthContext() { + defer perf.Track(nil, "exec.ClearLastAuthContext")() + + terraformcomponent.ClearLastAuthContext() +} + +// TerraformDeclaredVarType returns a variable's declared HCL type. +func TerraformDeclaredVarType(section map[string]any, name string) (string, bool) { + defer perf.Track(nil, "exec.TerraformDeclaredVarType")() + + return terraformcomponent.TerraformDeclaredVarType(section, name) +} + +// GetComponentRemoteStateBackendStaticType returns configured static state outputs. +func GetComponentRemoteStateBackendStaticType(sections *map[string]any) map[string]any { + defer perf.Track(nil, "exec.GetComponentRemoteStateBackendStaticType")() + + return terraformcomponent.GetComponentRemoteStateBackendStaticType(sections) +} + +// ExecuteShellCommandWithRetry delegates to the Terraform component implementation. +func ExecuteShellCommandWithRetry( + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + phase string, + invoke func(opts ...ShellCommandOption) error, + baseOpts ...ShellCommandOption, +) error { + defer perf.Track(nil, "exec.ExecuteShellCommandWithRetry")() + + return terraformcomponent.ExecuteShellCommandWithRetry(atmosConfig, info, phase, invoke, baseOpts...) +} + +type AuthManagerQueryFactory = terraformcomponent.AuthManagerQueryFactory + +// ExecuteTerraformQuery delegates to the Terraform component implementation. +func ExecuteTerraformQuery(info *schema.ConfigAndStacksInfo) error { + defer perf.Track(nil, "exec.ExecuteTerraformQuery")() + + return terraformExecutor.ExecuteTerraformQuery(info) +} + +// ExecuteTerraformQueryWithContext delegates to the Terraform component implementation. +func ExecuteTerraformQueryWithContext(ctx context.Context, info *schema.ConfigAndStacksInfo) error { + defer perf.Track(nil, "exec.ExecuteTerraformQueryWithContext")() + + return terraformExecutor.ExecuteTerraformQueryWithContext(ctx, info) +} + +// ExecuteTerraformShell delegates to the Terraform component implementation. +func ExecuteTerraformShell(opts *ShellOptions, atmosConfig *schema.AtmosConfiguration) error { + defer perf.Track(nil, "exec.ExecuteTerraformShell")() + + return terraformExecutor.ExecuteTerraformShell(opts, atmosConfig) +} + +// FilterTerraformAffected delegates to the Terraform component implementation. +func FilterTerraformAffected(affectedList []schema.Affected) []schema.Affected { + defer perf.Track(nil, "exec.FilterTerraformAffected")() + + return terraformcomponent.FilterTerraformAffected(affectedList) +} + +// ExecuteTerraformAffected delegates to the Terraform component implementation. +func ExecuteTerraformAffected(args *DescribeAffectedCmdArgs, info *schema.ConfigAndStacksInfo) error { + defer perf.Track(nil, "exec.ExecuteTerraformAffected")() + + return terraformExecutor.ExecuteTerraformAffected((*component.DescribeAffectedCmdArgs)(args), info) +} + +// ExecuteTerraformAffectedWithContext delegates to the Terraform component implementation. +func ExecuteTerraformAffectedWithContext(ctx context.Context, args *DescribeAffectedCmdArgs, info *schema.ConfigAndStacksInfo) error { + defer perf.Track(nil, "exec.ExecuteTerraformAffectedWithContext")() + + return terraformExecutor.ExecuteTerraformAffectedWithContext(ctx, (*component.DescribeAffectedCmdArgs)(args), info) +} + +// HookStoreDefaultIdentity delegates to the Terraform component implementation. +func HookStoreDefaultIdentity(authManager auth.AuthManager, info *schema.ConfigAndStacksInfo) string { + defer perf.Track(nil, "exec.HookStoreDefaultIdentity")() + + return terraformExecutor.HookStoreDefaultIdentity(authManager, info) +} + +// SetupTerraformAuthForCLI delegates to the Terraform component implementation. +func SetupTerraformAuthForCLI(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (any, error) { + defer perf.Track(nil, "exec.SetupTerraformAuthForCLI")() + + return terraformExecutor.SetupTerraformAuthForCLI(atmosConfig, info) +} + +// SetupComponentAuthForCLI delegates to the Terraform component implementation. +func SetupComponentAuthForCLI(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (auth.AuthManager, error) { + defer perf.Track(nil, "exec.SetupComponentAuthForCLI")() + + return terraformExecutor.SetupComponentAuthForCLI(atmosConfig, info) +} + +// ResetStateCache delegates to the Terraform component implementation. +func ResetStateCache() { + defer perf.Track(nil, "exec.ResetStateCache")() + terraformcomponent.ResetStateCache() +} + +// GetTerraformState delegates to the Terraform component implementation. +// +//nolint:revive // Preserve the state lookup compatibility signature. +func GetTerraformState( + atmosConfig *schema.AtmosConfiguration, + yamlFunc string, + stack string, + component string, + output string, + skipCache bool, + authContext *schema.AuthContext, + authManager any, + options ...TerraformLookupOptions, +) (any, error) { + defer perf.Track(nil, "exec.GetTerraformState")() + + return terraformExecutor.GetTerraformState(atmosConfig, yamlFunc, stack, component, output, skipCache, authContext, authManager, options...) +} + +type TerraformLookupOptions = terraformcomponent.TerraformLookupOptions + +type TerraformOutputGetter = terraformcomponent.TerraformOutputGetter + +// GetTerraformOutput delegates to the Terraform component implementation. +// +//nolint:revive // Preserve the output lookup compatibility signature. +func GetTerraformOutput( + atmosConfig *schema.AtmosConfiguration, + stack string, + component string, + output string, + skipCache bool, + authContext *schema.AuthContext, + authManager any, +) (any, bool, error) { + defer perf.Track(nil, "exec.GetTerraformOutput")() + + return terraformcomponent.GetTerraformOutput(atmosConfig, stack, component, output, skipCache, authContext, authManager) +} + +// GetAllTerraformOutputs delegates to the Terraform component implementation. +// +//nolint:revive // Preserve the output lookup compatibility signature. +func GetAllTerraformOutputs( + atmosConfig *schema.AtmosConfiguration, + component string, + stack string, + skipInit bool, + authContext *schema.AuthContext, + authManager any, +) (map[string]any, error) { + defer perf.Track(nil, "exec.GetAllTerraformOutputs")() + + return terraformcomponent.GetAllTerraformOutputs(atmosConfig, component, stack, skipInit, authContext, authManager) +} + +// GetStaticRemoteStateOutput delegates to the Terraform component implementation. +func GetStaticRemoteStateOutput( + atmosConfig *schema.AtmosConfiguration, + component string, + stack string, + remoteStateSection map[string]any, + output string, +) (any, bool, error) { + defer perf.Track(nil, "exec.GetStaticRemoteStateOutput")() + + return terraformcomponent.GetStaticRemoteStateOutput(atmosConfig, component, stack, remoteStateSection, output) +} + +type TerraformStateGetter = terraformcomponent.TerraformStateGetter + +// ExecuteTerraformAll delegates to the Terraform component implementation. +func ExecuteTerraformAll(info *schema.ConfigAndStacksInfo) error { + defer perf.Track(nil, "exec.ExecuteTerraformAll")() + + return terraformExecutor.ExecuteTerraformAll(info) +} + +// ExecuteTerraformAllWithContext delegates to the Terraform component implementation. +func ExecuteTerraformAllWithContext(ctx context.Context, info *schema.ConfigAndStacksInfo) error { + defer perf.Track(nil, "exec.ExecuteTerraformAllWithContext")() + + return terraformExecutor.ExecuteTerraformAllWithContext(ctx, info) +} + +// ResetNestedAuthManagerCache delegates to the Terraform component implementation. +func ResetNestedAuthManagerCache() { + defer perf.Track(nil, "exec.ResetNestedAuthManagerCache")() + terraformcomponent.ResetNestedAuthManagerCache() +} + +type ComponentStack = terraformcomponent.ComponentStack + +// ListTerraformComponentTargets delegates to the Terraform component implementation. +func ListTerraformComponentTargets(atmosConfig *schema.AtmosConfiguration, filterStack string, components []string, query string) ([]ComponentStack, error) { + defer perf.Track(nil, "exec.ListTerraformComponentTargets")() + + return terraformExecutor.ListTerraformComponentTargets(atmosConfig, filterStack, components, query) +} + +// allDiagnosticsAreModuleSourceInterpolation identifies module-source interpolation diagnostics that Atmos can tolerate. +func allDiagnosticsAreModuleSourceInterpolation(diagnostics tfconfig.Diagnostics) bool { + return terraformcomponent.AllDiagnosticsAreModuleSourceInterpolation(diagnostics) } diff --git a/internal/exec/terraform_adapter_boundary_test.go b/internal/exec/terraform_adapter_boundary_test.go new file mode 100644 index 00000000000..317b1db7db3 --- /dev/null +++ b/internal/exec/terraform_adapter_boundary_test.go @@ -0,0 +1,322 @@ +package exec + +import ( + bytes "bytes" + context "context" + errors "errors" + url "net/url" + os "os" + filepath "path/filepath" + strings "strings" + testing "testing" + time "time" + + errUtils "github.com/cloudposse/atmos/errors" + auth "github.com/cloudposse/atmos/pkg/auth" + component "github.com/cloudposse/atmos/pkg/component" + cfg "github.com/cloudposse/atmos/pkg/config" + schema "github.com/cloudposse/atmos/pkg/schema" + git "github.com/go-git/go-git/v5" + gitconfig "github.com/go-git/go-git/v5/config" + plumbing "github.com/go-git/go-git/v5/plumbing" + object "github.com/go-git/go-git/v5/plumbing/object" + assert "github.com/stretchr/testify/assert" + require "github.com/stretchr/testify/require" + gomock "go.uber.org/mock/gomock" +) + +// The compatibility entry points must preserve scheduler selection and dispatch +// through the registered provider, including providers that replace the builtin. +func TestTerraformAdapterScheduledRegistryBoundary(t *testing.T) { + for _, mode := range []string{"query", "all", "affected", "affected with context"} { + t.Run(mode, func(t *testing.T) { + args, baseline, _ := affectedDispatchFixture(t) + args.RepoPath = baseline + provider := replaceTerraformProvider(t) + provider.EXPECT().Execute(gomock.Any()).DoAndReturn(func(request *component.ExecutionContext) error { + require.Equal(t, "app", request.Component) + require.Equal(t, "dev", request.Stack) + require.Equal(t, "plan", request.SubCommand) + require.Equal(t, []string{"-lock=false"}, request.Args) + if mode == "affected with context" { + require.Equal(t, "caller", request.Context.Value(adapterContextKey{})) + } + return nil + }).Times(1) + info := &schema.ConfigAndStacksInfo{ + Stack: "dev", SubCommand: "plan", Identity: cfg.IdentityFlagDisabledValue, + AdditionalArgsAndFlags: []string{"-lock=false"}, + } + var err error + switch mode { + case "query": + info.Query = `.vars.size == "large"` + err = ExecuteTerraformQuery(info) + case "all": + err = ExecuteTerraformAll(info) + case "affected": + err = ExecuteTerraformAffected(args, info) + default: + ctx := context.WithValue(t.Context(), adapterContextKey{}, "caller") + err = ExecuteTerraformAffectedWithContext(ctx, args, info) + } + require.NoError(t, err) + }) + } +} + +type adapterContextKey struct{} + +func TestTerraformAdapterTargetsAndShell(t *testing.T) { + args, _, _ := affectedDispatchFixture(t) + targets, err := ListTerraformComponentTargets(args.CLIConfig, "dev", []string{"app"}, `.vars.size == "large"`) + require.NoError(t, err) + require.Equal(t, []ComponentStack{{Component: "app", Stack: "dev"}}, targets) + targets, err = ListTerraformComponentTargets(args.CLIConfig, "dev", []string{"app"}, `.vars.size == "small"`) + require.NoError(t, err) + require.Empty(t, targets) + + // Dry-run exercises stack/auth resolution while requiring no Terraform binary or interactive terminal. + require.NoError(t, ExecuteTerraformShell(&ShellOptions{Component: "app", Stack: "dev", Identity: cfg.IdentityFlagDisabledValue, DryRun: true}, args.CLIConfig)) + _, err = ListTerraformComponentTargets(args.CLIConfig, "dev", nil, `.vars.size == (`) + require.Error(t, err, "invalid query errors must cross the host adapter") +} + +func TestTerraformAdapterAuthErrorContract(t *testing.T) { + for _, mode := range []string{"terraform", "shared component"} { + t.Run(mode, func(t *testing.T) { + config := &schema.AtmosConfiguration{} + info := &schema.ConfigAndStacksInfo{Identity: "selected", ComponentFromArg: "app", Stack: "dev"} + original := defaultAuthManagerCreator + t.Cleanup(func() { defaultAuthManagerCreator = original }) + failure := errors.New("credential provider unavailable") + defaultAuthManagerCreator = func(identity string, _ *schema.AuthConfig, _ string, received *schema.AtmosConfiguration, reexec auth.ReExecContext) (auth.AuthManager, error) { + require.Same(t, config, received) + require.Equal(t, "selected", identity) + require.Equal(t, "app", reexec.Component) + require.Equal(t, "dev", reexec.Stack) + return nil, failure + } + merged := defaultMergedAuthConfigGetter + t.Cleanup(func() { defaultMergedAuthConfigGetter = merged }) + defaultMergedAuthConfigGetter = func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo) (*schema.AuthConfig, error) { + return &schema.AuthConfig{}, nil + } + var err error + if mode == "terraform" { + _, err = SetupTerraformAuthForCLI(config, info) + } else { + _, err = SetupComponentAuthForCLI(config, info) + } + require.ErrorIs(t, err, failure) + require.ErrorIs(t, err, errUtils.ErrFailedToInitializeAuthManager) + require.Nil(t, info.AuthManager) + }) + } +} + +func TestTerraformAdapterRetryPreservesCallerCapture(t *testing.T) { + attempts, delay := 2, time.Millisecond + info := &schema.ConfigAndStacksInfo{ComponentRetrySection: &schema.RetryConfig{ + MaxAttempts: &attempts, InitialDelay: &delay, Conditions: []string{"transient failure"}, + }} + var captured bytes.Buffer + calls := 0 + failure := errors.New("command failed") + err := ExecuteShellCommandWithRetry(&schema.AtmosConfiguration{}, info, "plan", func(opts ...ShellCommandOption) error { + calls++ + var options component.ExecutionOptions + for _, option := range opts { + option(&options) + } + require.Same(t, t.Context(), options.Ctx) + _, err := options.StderrCapture.Write([]byte("transient failure\n")) + require.NoError(t, err) + return failure + }, WithProcessContext(t.Context()), WithStderrCapture(&captured)) + require.ErrorIs(t, err, failure) + require.Equal(t, 2, calls) + require.Equal(t, "transient failure\ntransient failure\n", captured.String()) +} + +func TestTerraformAdapterStaticOutputsAndDeclaredVariables(t *testing.T) { + args, _, _ := affectedDispatchFixture(t) + config := args.CLIConfig + manifestPath := filepath.Join(config.BasePath, "stacks", "dev.yaml") + manifest, err := os.ReadFile(manifestPath) + require.NoError(t, err) + manifest = append(manifest, []byte(" remote_state_backend_type: static\n remote_state_backend:\n static: {endpoint: fixture-endpoint}\n")...) + require.NoError(t, os.WriteFile(manifestPath, manifest, 0o600)) + ClearFindStacksMapCache() + t.Cleanup(ClearFindStacksMapCache) + outputs, err := GetAllTerraformOutputs(config, "app", "dev", true, nil, nil) + require.NoError(t, err) + require.Equal(t, map[string]any{"endpoint": "fixture-endpoint"}, outputs) + value, exists, err := GetStaticRemoteStateOutput(config, "app", "dev", outputs, ".endpoint") + require.NoError(t, err) + require.True(t, exists) + require.Equal(t, "fixture-endpoint", value) + + info, err := ProcessStacks(config, schema.ConfigAndStacksInfo{ + ComponentFromArg: "app", ComponentType: "terraform", Stack: "dev", + }, true, false, false, nil, nil) + require.NoError(t, err) + declared, found := TerraformDeclaredVarType(info.ComponentSection, "size") + require.True(t, found) + require.Equal(t, "string", declared) + _, found = TerraformDeclaredVarType(info.ComponentSection, "not_declared") + require.False(t, found) +} + +func TestShellOptionsForUI(t *testing.T) { + tests := []struct { + name string + component string + stack string + }{ + { + name: "typical values", + component: "vpc", + stack: "dev-us-west-2", + }, + { + name: "empty values", + component: "", + stack: "", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + opts := shellOptionsForUI(tt.component, tt.stack) + assert.Equal(t, tt.component, opts.Component) + assert.Equal(t, tt.stack, opts.Stack) + assert.True(t, opts.ProcessTemplates, "UI path must enable template processing") + assert.True(t, opts.ProcessFunctions, "UI path must enable function processing") + assert.False(t, opts.DryRun, "UI path does not support dry-run") + assert.Empty(t, opts.Identity, "UI path does not support identity selection") + assert.Empty(t, opts.Skip, "UI path does not support skip") + }) + } +} + +func TestShellOptionsForUI_DefaultProcessingOptions(t *testing.T) { + // Verify that UI shell options have correct default processing options. + opts := shellOptionsForUI("vpc", "dev") + + assert.True(t, opts.ProcessTemplates) + assert.True(t, opts.ProcessFunctions) + assert.Empty(t, opts.Skip) +} + +// affectedDispatchFixture uses a local remote so every target mode compares the same +// two commits without network access or rewriting the caller's checkout. +func affectedDispatchFixture(t *testing.T) (*DescribeAffectedCmdArgs, string, string) { + t.Helper() + manifest := `name: dev +components: + terraform: + app: + metadata: + tags: [service] + labels: {owner: platform} + settings: {marker: retained} + vars: {size: small} +` + root, repo := affectedPolicyRepo(t, map[string]string{ + "atmos.yaml": "base_path: .\ncomponents:\n terraform:\n base_path: components/terraform\nstacks:\n base_path: stacks\n included_paths: ['**/*']\n", + "stacks/dev.yaml": manifest, + "components/terraform/app/main.tf": "variable \"size\" { type = string }\n", + }) + remoteURL := (&url.URL{Scheme: "file", Host: "localhost", Path: "/" + strings.TrimPrefix(filepath.ToSlash(root), "/")}).String() + _, err := repo.CreateRemote(&gitconfig.RemoteConfig{Name: "origin", URLs: []string{remoteURL}}) + require.NoError(t, err) + base, err := repo.Head() + require.NoError(t, err) + headDir := t.TempDir() + head, err := git.PlainClone(headDir, false, &git.CloneOptions{URL: remoteURL}) + require.NoError(t, err) + require.NoError(t, head.Storer.SetReference(plumbing.NewHashReference("refs/heads/baseline", base.Hash()))) + require.NoError(t, os.WriteFile(filepath.Join(headDir, "stacks", "dev.yaml"), []byte(strings.ReplaceAll(manifest, "small", "large")), 0o600)) + worktree, err := head.Worktree() + require.NoError(t, err) + _, err = worktree.Add(".") + require.NoError(t, err) + _, err = worktree.Commit("change app", &git.CommitOptions{Author: &object.Signature{Name: "Test", Email: "test@example.com", When: time.Unix(2, 0)}}) + require.NoError(t, err) + t.Chdir(headDir) + t.Setenv("ATMOS_CLI_CONFIG_PATH", headDir) + t.Setenv("ATMOS_BASE_PATH", headDir) + t.Setenv("ATMOS_POLICY_DIR", "") + config, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true) + require.NoError(t, err) + return &DescribeAffectedCmdArgs{CLIConfig: &config, AuthDisabled: true, IncludeSettings: true}, root, base.Hash().String() +} + +func TestGetAffectedComponentsDispatchRealRepositories(t *testing.T) { + for _, mode := range []string{"repo path", "local reference", "clone default", "clone commit"} { + t.Run(mode, func(t *testing.T) { + args, baseline, sha := affectedDispatchFixture(t) + switch mode { + case "repo path": + args.RepoPath = baseline + // An explicit path takes precedence over the other target selectors. + args.CloneTargetRef, args.Ref = true, "refs/heads/missing" + case "local reference": + args.Ref = "refs/heads/baseline" + case "clone default": + args.CloneTargetRef = true + case "clone commit": + args.CloneTargetRef, args.Ref, args.SHA = true, "refs/heads/master", sha + } + progress := 0 + affected, err := GetAffectedComponentsWithOptions(args, DescribeStacksErrorOptions{ + OnProgress: func(file string, index, total int) { + require.NotEmpty(t, file) + require.Less(t, index, total) + progress++ + }, + }) + require.NoError(t, err) + require.Len(t, affected, 1) + require.Equal(t, "app", affected[0].Component) + require.Equal(t, "dev", affected[0].Stack) + require.Equal(t, "retained", affected[0].Settings["marker"]) + require.GreaterOrEqual(t, progress, 2, "options reach both HEAD and BASE stack processing") + + args.Labels = map[string]string{"owner": "other-team"} + affected, err = GetAffectedComponents(args) + require.NoError(t, err) + require.Empty(t, affected, "the dispatcher must forward metadata filters") + }) + } +} + +func TestGetAffectedComponentsDispatchErrors(t *testing.T) { + _, err := GetAffectedComponents(nil) + require.ErrorIs(t, err, errUtils.ErrNilParam) + for _, mode := range []string{"invalid target directory", "unknown local reference", "invalid SSH key"} { + t.Run(mode, func(t *testing.T) { + args, _, _ := affectedDispatchFixture(t) + switch mode { + case "invalid target directory": + args.RepoPath = t.TempDir() + case "unknown local reference": + args.Ref = "refs/heads/does-not-exist" + case "invalid SSH key": + args.CloneTargetRef = true + args.SSHKeyPath = filepath.Join(t.TempDir(), "id_test") + require.NoError(t, os.WriteFile(args.SSHKeyPath, []byte("invalid private key"), 0o600)) + } + affected, err := GetAffectedComponents(args) + require.Error(t, err) + require.Nil(t, affected) + if mode == "invalid target directory" { + require.ErrorIs(t, err, RemoteRepoIsNotGitRepoError) + } + if mode == "invalid SSH key" { + require.ErrorContains(t, err, "no key found") + } + }) + } +} diff --git a/internal/exec/terraform_affected_test.go b/internal/exec/terraform_affected_host_test.go similarity index 83% rename from internal/exec/terraform_affected_test.go rename to internal/exec/terraform_affected_host_test.go index 62e59e8008f..379f178edb2 100644 --- a/internal/exec/terraform_affected_test.go +++ b/internal/exec/terraform_affected_host_test.go @@ -13,34 +13,6 @@ import ( "github.com/cloudposse/atmos/tests" ) -func TestExtractAffectedNodeIDs(t *testing.T) { - t.Run("empty list", func(t *testing.T) { - ids := extractAffectedNodeIDs(nil) - assert.Empty(t, ids) - }) - - t.Run("single affected", func(t *testing.T) { - affected := []schema.Affected{ - {Component: "vpc", Stack: "dev"}, - } - ids := extractAffectedNodeIDs(affected) - assert.Equal(t, []string{"vpc-dev"}, ids) - }) - - t.Run("multiple affected", func(t *testing.T) { - affected := []schema.Affected{ - {Component: "vpc", Stack: "dev"}, - {Component: "rds", Stack: "prod"}, - {Component: "app", Stack: "staging"}, - } - ids := extractAffectedNodeIDs(affected) - assert.Equal(t, 3, len(ids)) - assert.Equal(t, "vpc-dev", ids[0]) - assert.Equal(t, "rds-prod", ids[1]) - assert.Equal(t, "app-staging", ids[2]) - }) -} - func TestGetAffectedComponents(t *testing.T) { tests.SkipIfGomonkeyUnsafe(t, "uses gomonkey.ApplyFunc to mock ExecuteDescribeAffected*") @@ -513,69 +485,3 @@ func BenchmarkGetAffectedComponents(b *testing.B) { _, _ = GetAffectedComponents(args) } } - -// TestAffectedTerraformSelection verifies the selection carries the parsed -// --include-dependents depth from info: a zero DependentDepth means unlimited -// in dependency.Filter encoding, so omitting it silently erased a user-supplied -// depth bound on the --affected path (the closure spec keeps the most -// permissive depth when merging selection and info). -func TestAffectedTerraformSelection(t *testing.T) { - affected := []schema.Affected{{Component: "vpc", Stack: "dev"}} - - tests := []struct { - name string - argsIncludeDependents bool - infoIncludeDependents int - wantInclude bool - wantDepth int - }{ - { - name: "flag off", - argsIncludeDependents: false, - infoIncludeDependents: 0, - wantInclude: false, - wantDepth: 0, - }, - { - name: "bare flag is unlimited", - argsIncludeDependents: true, - infoIncludeDependents: -1, - wantInclude: true, - wantDepth: 0, - }, - { - name: "numeric depth is carried through, not erased to unlimited", - argsIncludeDependents: true, - infoIncludeDependents: 2, - wantInclude: true, - wantDepth: 2, - }, - { - name: "programmatically forced dependents without flag depth stay unlimited", - argsIncludeDependents: true, - infoIncludeDependents: 0, - wantInclude: true, - wantDepth: 0, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - args := &DescribeAffectedCmdArgs{IncludeDependents: tc.argsIncludeDependents} - info := &schema.ConfigAndStacksInfo{IncludeDependents: tc.infoIncludeDependents} - - selection := affectedTerraformSelection(affected, args, info) - - assert.Equal(t, []string{"vpc-dev"}, selection.NodeIDs) - assert.Equal(t, tc.wantInclude, selection.IncludeDependents) - assert.Equal(t, tc.wantDepth, selection.DependentDepth) - }) - } - - t.Run("nil info leaves depth unlimited", func(t *testing.T) { - args := &DescribeAffectedCmdArgs{IncludeDependents: true} - selection := affectedTerraformSelection(affected, args, nil) - assert.True(t, selection.IncludeDependents) - assert.Equal(t, 0, selection.DependentDepth) - }) -} diff --git a/internal/exec/terraform_auth_context_test.go b/internal/exec/terraform_auth_context_test.go deleted file mode 100644 index fa3f019c938..00000000000 --- a/internal/exec/terraform_auth_context_test.go +++ /dev/null @@ -1,72 +0,0 @@ -package exec - -import ( - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/cloudposse/atmos/pkg/schema" -) - -func TestSetLastAuthContext_RoundTrips(t *testing.T) { - t.Cleanup(ClearLastAuthContext) - - authCtx := &schema.AuthContext{ - AWS: &schema.AWSAuthContext{ - Profile: "test-profile", - CredentialsFile: "/tmp/creds", - Region: "us-east-1", - }, - } - authMgr := "mock-manager" - - SetLastAuthContext(authCtx, authMgr) - - gotCtx, gotMgr := GetLastAuthContext() - require.NotNil(t, gotCtx) - assert.Equal(t, "test-profile", gotCtx.AWS.Profile) - assert.Equal(t, "us-east-1", gotCtx.AWS.Region) - assert.Equal(t, "mock-manager", gotMgr) -} - -func TestGetLastAuthContext_ReturnsNilWhenUnset(t *testing.T) { - t.Cleanup(ClearLastAuthContext) - ClearLastAuthContext() - - gotCtx, gotMgr := GetLastAuthContext() - assert.Nil(t, gotCtx) - assert.Nil(t, gotMgr) -} - -func TestClearLastAuthContext_ResetsState(t *testing.T) { - authCtx := &schema.AuthContext{ - AWS: &schema.AWSAuthContext{Profile: "before-clear"}, - } - SetLastAuthContext(authCtx, "mgr") - - ClearLastAuthContext() - - gotCtx, gotMgr := GetLastAuthContext() - assert.Nil(t, gotCtx) - assert.Nil(t, gotMgr) -} - -func TestSetLastAuthContext_OverwritesPrevious(t *testing.T) { - t.Cleanup(ClearLastAuthContext) - - first := &schema.AuthContext{ - AWS: &schema.AWSAuthContext{Profile: "first"}, - } - second := &schema.AuthContext{ - AWS: &schema.AWSAuthContext{Profile: "second"}, - } - - SetLastAuthContext(first, "mgr1") - SetLastAuthContext(second, "mgr2") - - gotCtx, gotMgr := GetLastAuthContext() - require.NotNil(t, gotCtx) - assert.Equal(t, "second", gotCtx.AWS.Profile) - assert.Equal(t, "mgr2", gotMgr) -} diff --git a/internal/exec/terraform_auth_integration_coverage_test.go b/internal/exec/terraform_auth_integration_coverage_test.go index 349c5fcc086..1c70d4cdde6 100644 --- a/internal/exec/terraform_auth_integration_coverage_test.go +++ b/internal/exec/terraform_auth_integration_coverage_test.go @@ -1,11 +1,12 @@ package exec import ( - "testing" + testing "testing" - "github.com/stretchr/testify/assert" - - "github.com/cloudposse/atmos/pkg/schema" + types "github.com/cloudposse/atmos/pkg/auth/types" + schema "github.com/cloudposse/atmos/pkg/schema" + assert "github.com/stretchr/testify/assert" + gomock "go.uber.org/mock/gomock" ) // TestResolveAuthManagerForNestedComponent_Integration tests auth resolution @@ -19,7 +20,7 @@ func TestResolveAuthManagerForNestedComponent_Integration(t *testing.T) { t.Run("component with auth override and default identity", func(t *testing.T) { // Test with a component that has auth config with default identity // This should attempt to create a component-specific AuthManager - authMgr, err := resolveAuthManagerForNestedComponent( + authMgr, err := terraformExecutor.ResolveAuthManagerForNestedComponent( atmosConfig, "auth-override-level2", "test", @@ -39,7 +40,7 @@ func TestResolveAuthManagerForNestedComponent_Integration(t *testing.T) { t.Run("component without auth config inherits parent", func(t *testing.T) { // Test with a component that has NO auth config // This should return the parent AuthManager - authMgr, err := resolveAuthManagerForNestedComponent( + authMgr, err := terraformExecutor.ResolveAuthManagerForNestedComponent( atmosConfig, "mixed-top-level", // This component exists in the fixture "test", @@ -127,3 +128,13 @@ func TestGetTerraformOutput_Integration(t *testing.T) { _ = result }) } + +// newStackInfoAuthManager exposes the resolved caller context without authenticating. +func newStackInfoAuthManager(t *testing.T, stackInfo *schema.ConfigAndStacksInfo) *types.MockAuthManager { + t.Helper() + manager := types.NewMockAuthManager(gomock.NewController(t)) + manager.EXPECT().GetStackInfo().Return(stackInfo).AnyTimes() + return manager +} + +func boolPtr(value bool) *bool { return &value } diff --git a/internal/exec/terraform_component_provider_test.go b/internal/exec/terraform_component_provider_test.go new file mode 100644 index 00000000000..5d3c560b2d8 --- /dev/null +++ b/internal/exec/terraform_component_provider_test.go @@ -0,0 +1,153 @@ +package exec + +//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=../../pkg/component/provider.go -destination=mock_terraform_provider_test.go -package=exec + +import ( + bytes "bytes" + context "context" + os "os" + filepath "path/filepath" + testing "testing" + + errUtils "github.com/cloudposse/atmos/errors" + component "github.com/cloudposse/atmos/pkg/component" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" + schema "github.com/cloudposse/atmos/pkg/schema" + cobra "github.com/spf13/cobra" + assert "github.com/stretchr/testify/assert" + require "github.com/stretchr/testify/require" + gomock "go.uber.org/mock/gomock" +) + +func replaceTerraformProvider(t *testing.T) *MockComponentProvider { + t.Helper() + original, ok := component.GetProvider("terraform") + require.True(t, ok, "the host must register the built-in Terraform provider") + t.Cleanup(func() { require.NoError(t, component.Register(original)) }) + provider := NewMockComponentProvider(gomock.NewController(t)) + provider.EXPECT().GetType().Return("terraform") + require.NoError(t, component.Register(provider)) + return provider +} + +func TestTerraformRegistryDispatch(t *testing.T) { + provider := replaceTerraformProvider(t) + var stdout, stderr bytes.Buffer + invoking := &cobra.Command{Use: "plan"} + info := schema.ConfigAndStacksInfo{ + ComponentFromArg: "dev/app", Stack: "live", SubCommand: "plan", Command: "tofu", + AdditionalArgsAndFlags: []string{"-detailed-exitcode"}, + ComponentSection: map[string]any{"vars": map[string]any{"region": "test"}}, + ComponentEnvList: []string{"AUTH_TOKEN=component"}, SanitizedEnv: []string{}, + } + provider.EXPECT().Execute(gomock.Any()).DoAndReturn(func(request *component.ExecutionContext) error { + require.Equal(t, info, request.ConfigAndStacksInfo) + assert.Equal(t, "terraform", request.ComponentType) + assert.Equal(t, "dev/app", request.Component) + assert.Equal(t, "live", request.Stack) + assert.Equal(t, "tofu", request.Command) + assert.Equal(t, "plan", request.SubCommand) + assert.Equal(t, info.AdditionalArgsAndFlags, request.Args) + require.Same(t, t.Context(), request.Context) + var options component.ExecutionOptions + for _, option := range request.Options { + option(&options) + } + require.Same(t, &stdout, options.StdoutCapture) + require.Same(t, &stderr, options.StderrCapture) + require.NotNil(t, options.ProcessEnv) + assert.Empty(t, options.ProcessEnv) + require.Same(t, invoking, options.InvokingCmd) + assert.Equal(t, "plan:changes", options.ExecMetadataParser("plan", 2, "changes")) + return errUtils.ErrPlanHasDiff + }) + err := ExecuteTerraform(info, WithProcessContext(t.Context()), WithStdoutCapture(&stdout), WithStderrCapture(&stderr), + WithEnvironment([]string{}), WithInvokingCommand(invoking), WithExecMetadataParser(func(command string, _ int, output string) any { return command + ":" + output })) + require.ErrorIs(t, err, errUtils.ErrPlanHasDiff) +} + +func TestRegisteredTerraformCancellation(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + cancel() + // The actual provider must stop before config loading, authentication, or writes. + require.ErrorIs(t, ExecuteTerraform(schema.ConfigAndStacksInfo{}, WithProcessContext(ctx)), context.Canceled) +} + +func TestRegisteredTerraformGeneratesBackend(t *testing.T) { + provider, ok := component.GetProvider("terraform") + require.True(t, ok) + require.IsType(t, &terraformcomponent.Provider{}, provider) + directory := t.TempDir() + config := &schema.AtmosConfiguration{} + config.Components.Terraform.AutoGenerateBackendFile = true + request := &component.ExecutionContext{ + Context: t.Context(), AtmosConfig: config, WorkingDir: directory, + ConfigAndStacksInfo: schema.ConfigAndStacksInfo{ + ComponentBackendType: "local", ComponentBackendSection: map[string]any{"path": "state.tfstate"}, + }, + } + require.NoError(t, provider.GenerateArtifacts(request)) + content, err := os.ReadFile(filepath.Join(directory, "backend.tf.json")) + require.NoError(t, err) + assert.JSONEq(t, `{"terraform":{"backend":{"local":{"path":"state.tfstate"}}}}`, string(content)) + _, err = os.Stat(filepath.Join(directory, ".terraform")) + require.True(t, os.IsNotExist(err), "artifact generation must not initialize Terraform") +} + +// validatePlanfileFormat delegates to the Terraform component implementation. +func validatePlanfileFormat(format *string) error { + return terraformcomponent.ValidatePlanfileFormat(format) +} + +// validateOriginalPlanFile delegates to the Terraform component implementation. +func validateOriginalPlanFile(origPlanFile string, componentPath string) (string, error) { + return terraformcomponent.ValidateOriginalPlanFile(origPlanFile, componentPath) +} + +// validateComponent delegates to the Terraform component implementation. +func validateComponent(component string) error { + return terraformcomponent.ValidateComponent(component) +} + +// runTerraformShowWithExecutor delegates to the Terraform component implementation. +func runTerraformShowWithExecutor(info *schema.ConfigAndStacksInfo, planFile string, executor terraformPlanDiffExecutor) (string, error) { + return terraformOperations.RunTerraformShowWithExecutor(info, planFile, executor) +} + +// parsePlanDiffFlags delegates to the Terraform component implementation. +func parsePlanDiffFlags(arg1 []string) (string, string, error) { + return terraformcomponent.ParsePlanDiffFlags(arg1) +} + +// filterPlanDiffFlags delegates to the Terraform component implementation. +func filterPlanDiffFlags(arg1 []string) []string { return terraformcomponent.FilterPlanDiffFlags(arg1) } + +// extractJSONFromOutput delegates to the Terraform component implementation. +func extractJSONFromOutput(output string) (string, error) { + return terraformcomponent.ExtractJSONFromOutput(output) +} + +// extractBackendConfig delegates to the Terraform component implementation. +func extractBackendConfig(componentSection map[string]any) backendConfig { + return terraformcomponent.ExtractBackendConfig(componentSection) +} + +// ensureTerraformComponentExists delegates to the Terraform component implementation. +func ensureTerraformComponentExists(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { + return terraformcomponent.EnsureTerraformComponentExists(atmosConfig, info) +} + +// copyPlanFileIfNeeded delegates to the Terraform component implementation. +func copyPlanFileIfNeeded(planFile string, componentPath string) (string, func(), error) { + return terraformcomponent.CopyPlanFileIfNeeded(planFile, componentPath) +} + +// comparePlansAndGenerateDiff delegates to the Terraform component implementation. +func comparePlansAndGenerateDiff(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, origPlanFile string, newPlanFile string) error { + return terraformOperations.ComparePlansAndGenerateDiff(atmosConfig, info, componentPath, origPlanFile, newPlanFile) +} + +// checkBackendTypeAfterProcessing delegates to the Terraform component implementation. +func checkBackendTypeAfterProcessing(backendType string) error { + return terraformcomponent.CheckBackendTypeAfterProcessing(backendType) +} diff --git a/internal/exec/terraform_execute_helpers.go b/internal/exec/terraform_execute_helpers.go deleted file mode 100644 index 0c02fca3410..00000000000 --- a/internal/exec/terraform_execute_helpers.go +++ /dev/null @@ -1,1226 +0,0 @@ -package exec - -// terraform_execute_helpers.go contains helper functions extracted from ExecuteTerraform -// to reduce cyclomatic complexity and improve testability. -// Each function handles one discrete responsibility of the terraform execution pipeline. - -import ( - "bytes" - "context" - "errors" - "fmt" - "os" - "path/filepath" - "slices" - "strings" - "time" - - "github.com/hashicorp/terraform-config-inspect/tfconfig" - - errUtils "github.com/cloudposse/atmos/errors" - auth "github.com/cloudposse/atmos/pkg/auth" - "github.com/cloudposse/atmos/pkg/component" - cfg "github.com/cloudposse/atmos/pkg/config" - "github.com/cloudposse/atmos/pkg/degradation" - "github.com/cloudposse/atmos/pkg/dependencies" - atmosio "github.com/cloudposse/atmos/pkg/io" - log "github.com/cloudposse/atmos/pkg/logger" - "github.com/cloudposse/atmos/pkg/perf" - "github.com/cloudposse/atmos/pkg/provisioner" - _ "github.com/cloudposse/atmos/pkg/provisioner/lock" // register after.terraform.init providers-lock provisioner - _ "github.com/cloudposse/atmos/pkg/provisioner/source" // register source provisioner - provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir" - "github.com/cloudposse/atmos/pkg/schema" - "github.com/cloudposse/atmos/pkg/store" - "github.com/cloudposse/atmos/pkg/store/authbridge" - "github.com/cloudposse/atmos/pkg/terraform/autoinit" - tfcache "github.com/cloudposse/atmos/pkg/terraform/cache" - tfgenerate "github.com/cloudposse/atmos/pkg/terraform/generate" - "github.com/cloudposse/atmos/pkg/terraform/rc" - "github.com/cloudposse/atmos/pkg/terraform/tfvars" - u "github.com/cloudposse/atmos/pkg/utils" -) - -// resolveTerraformCommand sets info.Command from atmosConfig if not already set. -// Falls back to the cfg.TerraformComponentType default when neither is configured. -func resolveTerraformCommand(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) { - if info.ComponentCommand != "" { - info.Command = info.ComponentCommand - return - } - if info.Command != "" { - return - } - if atmosConfig.Components.Terraform.Command != "" { - info.Command = atmosConfig.Components.Terraform.Command - } else { - info.Command = cfg.TerraformComponentType - } -} - -// handleVersionSubcommand executes the `terraform version` command and returns the result. -// It resolves the toolchain binary and delegates directly to the shell, bypassing -// full stack processing. -func handleVersionSubcommand(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { - tenv, err := dependencies.ForComponent(atmosConfig, "terraform", nil, nil) - if err != nil { - return err - } - return ExecuteShellCommandWithRetry(atmosConfig, info, "version", func(opts ...ShellCommandOption) error { - return ExecuteShellCommand( - *atmosConfig, - tenv.Resolve(info.Command), - []string{info.SubCommand}, - "", - tenv.EnvVars(), - false, - info.RedirectStdErr, - opts..., - ) - }) -} - -// setupTerraformAuth builds the merged auth config (global + component-specific via -// getMergedAuthConfig), creates and authenticates the AuthManager, stores the resolved -// identity back into info, and injects an auth resolver into the Atmos store registry. -// -// getMergedAuthConfig is the shared helper (utils_auth.go) that handles the -// component config fetch, debug logging on fallback, and the ErrInvalidComponent -// short-circuit. Using it here eliminates duplication and keeps both code paths in sync. -// -// The defaultAuthManagerCreator injectable var (utils_auth.go) is used so tests can -// substitute a fake creator without needing a separate var in this file. -// The defaultMergedAuthConfigGetter injectable var below allows tests to exercise the -// ErrInvalidAuthConfig wrap branch without requiring a real stack or component. - -// defaultMergedAuthConfigGetter is the injectable function for getMergedAuthConfig. -// Overriding it in tests allows exercising error branches that are otherwise only -// reachable via MergeComponentAuthFromConfig failures (hard to trigger in unit tests). -// -// Note: overriding this var also bypasses the defaultComponentConfigFetcher layer -// (utils_auth.go), which is one level deeper. The two vars target different injection -// points: defaultComponentConfigFetcher injects at the component-fetch level (used for -// ErrInvalidComponent), whereas defaultMergedAuthConfigGetter injects at the whole getter -// level (used for ErrInvalidAuthConfig). Do not override both simultaneously — the deeper -// var is shadowed and its effect would be masked. -var defaultMergedAuthConfigGetter = getMergedAuthConfig - -func setupTerraformAuth(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (auth.AuthManager, error) { - // Log the identity-selection decision point for easy debugging. - log.Debug("Resolving auth config for component command", - "stack", info.Stack, "component", info.ComponentFromArg, "subcommand", info.SubCommand) - - // Get merged auth config (global + component-specific if stack/component are set). - // getMergedAuthConfig logs on debug when falling back to global config after an error. - mergedAuthConfig, err := defaultMergedAuthConfigGetter(atmosConfig, info) - if err != nil { - // Propagate ErrInvalidComponent directly — prevents an auth prompt for a nonexistent component. - if errors.Is(err, errUtils.ErrInvalidComponent) { - return nil, err - } - // Wrap unexpected errors (e.g. MergeComponentAuthFromConfig failures) with the sentinel - // to match the behaviour of createAndAuthenticateAuthManagerWithDeps. When the error names - // a missing identity, offer the same profile-selection prompt `atmos auth login` has. - return nil, resolveIdentityConfigError(atmosConfig, info, err, errUtils.ErrInvalidAuthConfig) - } - - // Create and authenticate the AuthManager using the same injectable creator as - // createAndAuthenticateAuthManagerWithDeps to keep injection points unified. Carry - // forward prompted component/stack so a later identity-not-found fallback inside - // Authenticate can re-inject them into a profile-fallback re-exec. - authManager, err := defaultAuthManagerCreator( - info.Identity, mergedAuthConfig, cfg.IdentityFlagSelectValue, atmosConfig, auth.ReExecContext{ - Component: info.ComponentFromArg, - ComponentPrompted: info.ComponentPrompted, - Stack: info.Stack, - StackPrompted: info.StackPrompted, - }, - ) - if err != nil { - if errors.Is(err, errUtils.ErrUserAborted) { - errUtils.Exit(errUtils.ExitCodeSIGINT) - } - // Wrap auth creation failures with the sentinel to match createAndAuthenticateAuthManagerWithDeps. - // When the error names a missing identity, offer the same profile-selection prompt - // `atmos auth login` has. - return nil, resolveIdentityConfigError(atmosConfig, info, err, errUtils.ErrFailedToInitializeAuthManager) - } - - // Store manager for nested YAML functions (e.g. !terraform.state). - info.AuthManager = authManager - - // The manager is created with its own empty stackInfo; thread the target stack - // through so emulator identities can resolve the running emulator's endpoint - // when stores (`!store`, `!secret`, hooks) build their in-process auth context. - // authManager may be nil when no identity/auth is configured. - if authManager != nil { - if si := authManager.GetStackInfo(); si != nil && si.Stack == "" { - si.Stack = info.Stack - } - } - - injectTerraformStoreAuthResolver(atmosConfig, info, authManager) - - return authManager, nil -} - -func injectTerraformStoreAuthResolver(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, authManager auth.AuthManager) { - if authManager == nil { - return - } - - // Persist the auto-detected identity so downstream hooks, nested operations, - // and stores without their own identity use the same authenticated principal. - storeAutoDetectedIdentity(authManager, info) - - resolver := authbridge.NewResolver(authManager, info) - defaultIdentity := resolveStoreDefaultIdentity(info.Identity, authManager) - atmosConfig.Stores.SetAuthContextResolverWithDefaultIdentity(resolver, defaultIdentity) - - // Also expose the resolver to cloud-KMS SOPS providers so `!secret` resolution during terraform - // authenticates KMS decrypt as the component's effective identity (issue #2637). - atmosConfig.SecretsAuth = &store.SecretsAuthContext{ - Resolver: resolver, - DefaultIdentity: defaultIdentity, - } -} - -func storeDefaultIdentity(identity string) string { - switch identity { - case "", cfg.IdentityFlagSelectValue, cfg.IdentityFlagDisabledValue: - return "" - default: - return identity - } -} - -// resolveStoreDefaultIdentity returns the identity that stores without their own -// `identity:` should authenticate as. An explicit `--identity`/`ATMOS_IDENTITY` -// wins; otherwise we fall back to the stack's `default: true` identity so -// in-process store/secret reads use the same principal (and emulator endpoint) as -// the Terraform subprocess. When there is no default identity (or auth is -// disabled), this returns "" — stores then use the AWS/GCP/Azure default -// credential chain exactly as before (back-compat for ambient real-cloud creds). -func resolveStoreDefaultIdentity(identity string, authManager auth.AuthManager) string { - if identity == cfg.IdentityFlagDisabledValue { - return "" - } - if explicit := storeDefaultIdentity(identity); explicit != "" { - return explicit - } - if authManager == nil { - return "" - } - defaultIdentity, err := authManager.GetDefaultIdentity(false) - if err != nil { - return "" - } - return defaultIdentity -} - -// HookStoreDefaultIdentity resolves the identity that identity-less stores invoked by terraform -// lifecycle hooks (e.g. the after-apply `store-outputs` hook) should inherit. It mirrors the main -// terraform path (injectTerraformStoreAuthResolver): it auto-detects the active identity from the -// auth manager's chain when info carries no explicit identity, then normalizes empty/select/disabled -// to "" (meaning "no inheritance — keep ambient/default SDK credentials"). It returns "" when no auth -// manager is present, preserving the prior behavior for runs without Atmos auth. -// -// The hook executes in a freshly loaded config (prepareHookContext), so it must re-derive this from -// the persisted auth manager rather than relying on the apply-phase config. -func HookStoreDefaultIdentity(authManager auth.AuthManager, info *schema.ConfigAndStacksInfo) string { - defer perf.Track(nil, "exec.HookStoreDefaultIdentity")() - - if authManager == nil || info == nil { - return "" - } - storeAutoDetectedIdentity(authManager, info) - return storeDefaultIdentity(info.Identity) -} - -// SetupTerraformAuthForCLI exposes terraform auth setup to command-layer callers -// that need the same merged-auth and explicit-identity behavior as ExecuteTerraform. -func SetupTerraformAuthForCLI(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (any, error) { - defer perf.Track(atmosConfig, "exec.SetupTerraformAuthForCLI")() - - return setupTerraformAuth(atmosConfig, info) -} - -// SetupComponentAuthForCLI exposes the shared component auth setup to non-Terraform -// command layers that still need authenticated YAML functions such as !terraform.state. -func SetupComponentAuthForCLI(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (auth.AuthManager, error) { - defer perf.Track(atmosConfig, "exec.SetupComponentAuthForCLI")() - - return setupTerraformAuth(atmosConfig, info) -} - -// resolveAndProvisionComponentPath resolves the filesystem path for a terraform component, -// optionally auto-generates files, performs JIT source provisioning, and validates -// that the resulting directory actually exists. -// The provision-and-resolve component function is a seam for testing JIT provisioning context propagation. -var provisionAndResolveTerraformComponentPath = component.ProvisionAndResolveComponentPath - -// The before-init provisioner function is a seam for testing context propagation. -var executeBeforeInitProvisioners = provisioner.ExecuteProvisioners - -func resolveAndProvisionComponentPath(ctx context.Context, writers provisioner.OutputWriters, atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (string, error) { - componentPath, err := u.GetComponentPath(atmosConfig, "terraform", info.ComponentFolderPrefix, info.FinalComponent) - if err != nil { - return "", fmt.Errorf("failed to resolve component path: %w", err) - } - - // Provision source before generating files: when provision.workdir.enabled - // is true the resolved path is the workdir, and generated files must land - // there rather than in the base component directory. - if ctx == nil { - ctx = context.Background() - } - ctx, cancel := context.WithTimeout(ctx, 5*time.Minute) - defer cancel() - componentPath, componentPathExists, err := provisionAndResolveTerraformComponentPath( - ctx, writers, atmosConfig, info, cfg.TerraformComponentType, componentPath, - ) - if err != nil { - return "", err - } - - if err = autoGenerateComponentFiles(atmosConfig, info, componentPath); err != nil { - return "", err - } - - if !componentPathExists { - basePath, _ := u.GetComponentBasePath(atmosConfig, cfg.TerraformComponentType) - return "", fmt.Errorf( - "%w: '%s' points to the Terraform component '%s', but it does not exist in '%s'", - errUtils.ErrInvalidTerraformComponent, - info.ComponentFromArg, - info.FinalComponent, - basePath, - ) - } - - return componentPath, nil -} - -// autoGenerateComponentFiles creates the component directory and generates source files -// when AutoGenerateFiles is enabled and a generate section is present. -func autoGenerateComponentFiles(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string) error { - if !atmosConfig.Components.Terraform.AutoGenerateFiles || info.DryRun { - return nil - } - generateSection := tfgenerate.GetGenerateSectionFromComponent(info.ComponentSection) - if generateSection == nil { - return nil - } - if mkdirErr := os.MkdirAll(componentPath, dirPermissions); mkdirErr != nil { - return errors.Join(errUtils.ErrCreateDirectory, fmt.Errorf("auto-generation: %w", mkdirErr)) - } - return GenerateFilesForComponent(atmosConfig, info, componentPath) -} - -// checkComponentRestrictions returns an error when the requested subcommand is not -// permitted for the component due to its metadata (abstract, locked) or the configured -// backend type (HTTP backend does not support workspaces). -func checkComponentRestrictions(info *schema.ConfigAndStacksInfo) error { - // Abstract components cannot be provisioned. - if info.ComponentIsAbstract { - switch info.SubCommand { - case "plan", subcommandApply, subcommandDeploy, subcommandWorkspace: - return fmt.Errorf( - "%w: the component '%s' cannot be provisioned because it's marked as abstract (metadata.type: abstract)", - errUtils.ErrAbstractComponentCantBeProvisioned, - filepath.Join(info.ComponentFolderPrefix, info.Component), - ) - } - } - - // Locked components may not be mutated. - if info.ComponentIsLocked { - switch info.SubCommand { - case subcommandApply, "deploy", "destroy", "import", "state", "taint", "untaint": - return fmt.Errorf( - "%w: component '%s' cannot be modified (metadata.locked: true)", - errUtils.ErrLockedComponentCantBeProvisioned, - filepath.Join(info.ComponentFolderPrefix, info.Component), - ) - } - } - - // HTTP backend does not support workspace commands. - if info.SubCommand == subcommandWorkspace && info.ComponentBackendType == "http" { - return errUtils.ErrHTTPBackendWorkspaces - } - - return nil -} - -// printAndWriteVarFiles logs component variables and, when not using a pre-existing -// plan file, writes them to the varfile on disk (path derived from atmosConfig+info). -// Workspace subcommands do not use varfiles and are skipped, unless init.pass_vars is enabled: the init -// that runs before a workspace command then receives -var-file, so the varfile must exist even for a -// component that was never planned. -func printAndWriteVarFiles(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { - if info.SubCommand == subcommandWorkspace && !atmosConfig.Components.Terraform.Init.PassVars { - return nil - } - - if err := logAndWriteComponentVars(atmosConfig, info); err != nil { - return err - } - if err := writeTerraformTestVars(atmosConfig, info); err != nil { - return err - } - - return logCliVarsOverrides(atmosConfig, info) -} - -// computeTerraformSecretVarKeys records the set of top-level variable keys whose value -// contains a resolved secret, so they can be kept out of the on-disk varfile and injected -// as TF_VAR_ environment variables instead. It must run after secret resolution -// (ProcessStacks) and before auth credentials are registered with the masker, so that the -// varfile-write and env-assembly steps partition the variables identically. -func computeTerraformSecretVarKeys(info *schema.ConfigAndStacksInfo) { - _, secret := tfvars.Partition(info.ComponentVarsSection, atmosio.ContainsSecret) - keys := make(map[string]bool, len(secret)) - for k := range secret { - keys[k] = true - } - - // Terraform's `sensitive = true` is an explicit declaration that an input must - // not be persisted in a generated tfvars file. When masking is disabled, retain - // the established compatibility behavior that keeps typed sensitive values in - // JSON rather than coercing them through TF_VAR_ environment variables. - if atmosio.MaskingEnabled() { - for k := range terraformSensitiveVarKeys(info) { - if _, supplied := info.ComponentVarsSection[k]; supplied { - keys[k] = true - } - } - } - - if len(keys) == 0 { - info.TerraformSecretVarKeys = nil - return - } - info.TerraformSecretVarKeys = keys -} - -// terraformSensitiveVarKeys returns the root-module variable names declared -// `sensitive = true`. Component metadata is populated during stack processing -// with terraform-config-inspect, so this adds no extra filesystem parsing on -// the execution hot path. -func terraformSensitiveVarKeys(info *schema.ConfigAndStacksInfo) map[string]bool { - if info == nil { - return nil - } - componentInfo, ok := info.ComponentSection[componentInfoKey].(map[string]any) - if !ok { - return nil - } - module, ok := componentInfo[terraformConfigKey].(*tfconfig.Module) - if !ok || module == nil { - return nil - } - - keys := make(map[string]bool) - for name, variable := range module.Variables { - if variable != nil && variable.Sensitive { - keys[name] = true - } - } - return keys -} - -// rejectComputedTerraformVars ensures display-only degraded values cannot cross -// into Terraform through either tfvars JSON or TF_VAR_ environment variables. -func rejectComputedTerraformVars(vars map[string]any) error { - for key, value := range vars { - if containsComputedTerraformValue(value) { - return fmt.Errorf("%w: %q", errUtils.ErrUnresolvedComputedTerraformVar, key) - } - } - return nil -} - -func containsComputedTerraformValue(value any) bool { - switch v := value.(type) { - case degradation.AtmosComputedValue: - return true - case *degradation.AtmosComputedValue: - return v != nil - case map[string]any: - for _, child := range v { - if containsComputedTerraformValue(child) { - return true - } - } - case []any: - for _, child := range v { - if containsComputedTerraformValue(child) { - return true - } - } - } - return false -} - -// diskSafeVars returns ComponentVarsSection with secret-bearing top-level keys removed, -// so resolved secrets are never written to the on-disk varfile. Returns the original map -// unchanged when no secret-bearing variables were detected. -func diskSafeVars(info *schema.ConfigAndStacksInfo) map[string]any { - testSecretVars := terraformTestSecretVars(info) - if len(info.TerraformSecretVarKeys) == 0 && len(testSecretVars) == 0 { - return info.ComponentVarsSection - } - safe := make(map[string]any, len(info.ComponentVarsSection)) - for k, v := range info.ComponentVarsSection { - if info.TerraformSecretVarKeys[k] { - continue - } - if _, collidesWithSecretTestVar := testSecretVars[k]; collidesWithSecretTestVar { - continue - } - safe[k] = v - } - return safe -} - -func varsWithoutKeys(vars map[string]any, excluded map[string]bool) map[string]any { - if len(excluded) == 0 { - return vars - } - // `excluded` can include declared inputs that this stack does not supply, so - // use the source size as the allocation hint rather than subtracting it. - safe := make(map[string]any, len(vars)) - for k, v := range vars { - if !excluded[k] { - safe[k] = v - } - } - return safe -} - -// secretVarEnv returns the TF_VAR_ environment entries for the component's -// secret-bearing variables, for injection into the subprocess environment. -func secretVarEnv(info *schema.ConfigAndStacksInfo) ([]string, error) { - if len(info.TerraformSecretVarKeys) == 0 { - return nil, nil - } - secret := make(map[string]any, len(info.TerraformSecretVarKeys)) - for k := range info.TerraformSecretVarKeys { - if v, ok := info.ComponentVarsSection[k]; ok { - secret[k] = v - } - } - return tfvars.SecretEnv(secret) -} - -func terraformTestVars(info *schema.ConfigAndStacksInfo) map[string]any { - if info == nil || info.SubCommand != "test" { - return nil - } - testSection, ok := info.ComponentSection[cfg.TestSectionName].(map[string]any) - if !ok { - return nil - } - vars, ok := testSection[cfg.VarsSectionName].(map[string]any) - if !ok || len(vars) == 0 { - return nil - } - return vars -} - -func hasTerraformTestVars(info *schema.ConfigAndStacksInfo) bool { - return len(terraformTestVars(info)) > 0 -} - -func diskSafeTerraformTestVars(info *schema.ConfigAndStacksInfo) map[string]any { - vars := terraformTestVars(info) - if len(vars) == 0 { - return nil - } - safe, _ := tfvars.Partition(vars, atmosio.ContainsSecret) - return safe -} - -func terraformTestSecretVars(info *schema.ConfigAndStacksInfo) map[string]any { - vars := terraformTestVars(info) - if len(vars) == 0 { - return nil - } - _, secret := tfvars.Partition(vars, atmosio.ContainsSecret) - return secret -} - -func terraformTestSecretVarEnv(info *schema.ConfigAndStacksInfo) ([]string, error) { - secret := terraformTestSecretVars(info) - if len(secret) == 0 { - return nil, nil - } - return tfvars.SecretEnv(secret) -} - -// logAndWriteComponentVars logs component variables and writes the varfile to disk -// when not using a pre-existing plan. Secret-bearing variables are excluded from the -// varfile (they are injected as TF_VAR_ env vars in assembleComponentEnvVars). -func logAndWriteComponentVars(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { - log.Debug("Variables for the component in the stack", logFieldComponent, info.ComponentFromArg, "stack", info.Stack) - if atmosConfig.Logs.Level == u.LogLevelTrace || atmosConfig.Logs.Level == u.LogLevelDebug { - if err := u.PrintAsYAMLToFileDescriptor(atmosConfig, info.ComponentVarsSection); err != nil { - return err - } - } - - if !info.UseTerraformPlan { - varFilePath := constructTerraformComponentVarfilePath(atmosConfig, info) - log.Debug("Writing the variables", "file", varFilePath) - if !info.DryRun { - if err := u.WriteToFileAsJSON(varFilePath, diskSafeVars(info), filePermissions); err != nil { - return err - } - } - } - return nil -} - -func writeTerraformTestVars(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { - if !hasTerraformTestVars(info) { - return nil - } - varFilePath := constructTerraformComponentTestVarfilePath(atmosConfig, info) - log.Debug("Writing Terraform test variables", "file", varFilePath) - if info.DryRun { - return nil - } - return u.WriteToFileAsJSON(varFilePath, diskSafeTerraformTestVars(info), filePermissions) -} - -// logCliVarsOverrides logs CLI variable overrides when present at debug/trace level. -func logCliVarsOverrides(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { - cliVars, ok := info.ComponentSection[cfg.TerraformCliVarsSectionName].(map[string]any) - if !ok || len(cliVars) == 0 { - return nil - } - log.Debug("CLI variables (will override the variables defined in the stack manifests):") - if atmosConfig.Logs.Level == u.LogLevelTrace || atmosConfig.Logs.Level == u.LogLevelDebug { - if err := u.PrintAsYAMLToFileDescriptor(atmosConfig, cliVars); err != nil { - return err - } - } - return nil -} - -// validateTerraformComponent runs OPA/JSON-schema validation policies against the -// component's stack configuration section and returns an error if validation fails. -func validateTerraformComponent(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { - valid, err := ValidateComponent( - atmosConfig, - info.ComponentFromArg, - info.ComponentSection, - "", "", nil, 0, - ) - if err != nil { - return err - } - if !valid { - return fmt.Errorf("%w: the component '%s' did not pass the validation policies", - errUtils.ErrInvalidComponent, info.ComponentFromArg) - } - return nil -} - -// generateConfigFiles writes the backend configuration, generated files, and -// provider overrides for the component into the working directory. -// -// NOTE: GenerateFilesForComponent is also called by autoGenerateComponentFiles -// (inside resolveAndProvisionComponentPath) when AutoGenerateFiles=true. That call -// handles the generate: section from stack config, while this call handles the -// standard backend/provider override files. The two calls serve different purposes -// and both are needed. This is pre-existing behavior from the original terraform.go. -func generateConfigFiles(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { - if err := generateBackendConfig(atmosConfig, info, workingDir); err != nil { - return err - } - if err := GenerateFilesForComponent(atmosConfig, info, workingDir); err != nil { - return err - } - if err := generateProviderOverrides(atmosConfig, info, workingDir); err != nil { - return err - } - // Generate required_providers (terraform_override.tf.json) for version pinning (DEV-3124). - return generateRequiredProviders(atmosConfig, info, workingDir) -} - -// warnOnConflictingEnvVars inspects the current process environment for variables -// that are known to interfere with Atmos's management of Terraform, and emits a -// warning when any are detected. -func warnOnConflictingEnvVars() { - warnOnExactVars := []string{"TF_CLI_ARGS", "TF_WORKSPACE"} - warnOnPrefixVars := []string{"TF_VAR_", "TF_CLI_ARGS_"} - - var problematicVars []string - for _, envVar := range os.Environ() { - parts := strings.SplitN(envVar, "=", 2) - if len(parts) != 2 { - continue - } - if slices.Contains(warnOnExactVars, parts[0]) { - problematicVars = append(problematicVars, parts[0]) - continue - } - for _, prefix := range warnOnPrefixVars { - if strings.HasPrefix(parts[0], prefix) { - problematicVars = append(problematicVars, parts[0]) - break - } - } - } - - if len(problematicVars) > 0 { - log.Warn("Detected environment variables that may interfere with Atmos's control of Terraform", - "variables", problematicVars) - } -} - -// assembleComponentEnvVars builds the complete list of environment variables for -// the terraform subprocess. It combines the component env section, standard Atmos -// variables (ATMOS_CLI_CONFIG_PATH, ATMOS_BASE_PATH, TF_IN_AUTOMATION), the -// TF_APPEND_USER_AGENT value, the plugin-cache env, and any toolchain PATH overrides. -func assembleComponentEnvVars(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, tenv *dependencies.ToolchainEnvironment) error { - // Convert ComponentEnvSection (set by auth hooks and stack config) to a list. - for k, v := range info.ComponentEnvSection { - info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("%s=%v", k, v)) - } - - info.ComponentEnvList = append(info.ComponentEnvList, - fmt.Sprintf("ATMOS_CLI_CONFIG_PATH=%s", atmosConfig.CliConfigPath)) - - basePath, err := filepath.Abs(atmosConfig.BasePath) - if err != nil { - return err - } - info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("ATMOS_BASE_PATH=%s", basePath)) - - // Suppress verbose Terraform instructions in automated environments. - // https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_in_automation - info.ComponentEnvList = append(info.ComponentEnvList, "TF_IN_AUTOMATION=true") - - // Precedence: OS env > atmos.yaml > default (empty/omitted). - appendUserAgent := atmosConfig.Components.Terraform.AppendUserAgent - if envUA, exists := os.LookupEnv("TF_APPEND_USER_AGENT"); exists && envUA != "" { - appendUserAgent = envUA - } - if appendUserAgent != "" { - info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("TF_APPEND_USER_AGENT=%s", appendUserAgent)) - } - - // Plugin cache directory. - info.ComponentEnvList = append(info.ComponentEnvList, configurePluginCache(atmosConfig)...) - - // Terraform CLI configuration (.terraformrc) rendered by Atmos and exposed via - // TF_CLI_CONFIG_FILE / TOFU_CLI_CONFIG_FILE. Also the injection point for the - // registry cache's network_mirror/host directives. The cleanup closer is stashed - // on info and run (deferred) after the whole terraform pipeline so the temp file - // survives every subprocess (init, workspace, plan/apply). - if err := configureTerraformRC(atmosConfig, info); err != nil { - return err - } - - // Toolchain PATH must come last so it takes precedence over all other PATH entries. - if tenv != nil { - info.ComponentEnvList = append(info.ComponentEnvList, tenv.EnvVars()...) - } - - if len(info.ComponentEnvList) > 0 { - log.Debug("Using ENV vars:") - for _, v := range info.ComponentEnvList { - log.Debug(v) - } - } - - // Inject secret-bearing variables as TF_VAR_ so they reach Terraform via the - // (transient) process environment instead of the on-disk varfile. Appended AFTER the - // debug dump above so secret values are never written to logs. - secretEnv, err := secretVarEnv(info) - if err != nil { - return err - } - if len(secretEnv) > 0 { - info.ComponentEnvList = append(info.ComponentEnvList, secretEnv...) - log.Debug("Injecting secret variables as TF_VAR_* environment variables", "count", len(secretEnv)) - } - - testSecretEnv, err := terraformTestSecretVarEnv(info) - if err != nil { - return err - } - if len(testSecretEnv) > 0 { - info.ComponentEnvList = append(info.ComponentEnvList, testSecretEnv...) - log.Debug("Injecting secret Terraform test variables as TF_VAR_* environment variables", "count", len(testSecretEnv)) - } - - return nil -} - -// configureTerraformRC builds the effective Terraform CLI-config map from the -// user's components.terraform.rc plus any registry-cache contribution (network -// mirror + module host overrides), renders it to a temp file, and appends -// TF_CLI_CONFIG_FILE / TOFU_CLI_CONFIG_FILE. The cleanup closer is stashed on info -// and deferred at the ExecuteTerraform level so the file outlives every subprocess. -func configureTerraformRC(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { - rcMap := map[string]any{} - - if rcCfg := atmosConfig.Components.Terraform.RC; rcCfg != nil && rcCfg.Enabled { - for k, v := range rcCfg.Config { - // Deep-copy nested values: mergeCacheContribution mutates the "host" map in - // place, and a shallow copy would leak cache loopback overrides back into - // atmosConfig.Components.Terraform.RC.Config for later components/commands in - // the same process. - rcMap[k] = cloneRCValue(v) - } - } - - if setup, ok := info.TerraformCache.(*tfcache.Setup); ok && setup != nil { - mergeCacheContribution(rcMap, setup.Contribute()) - - // Make the terraform/tofu subprocess trust the proxy's self-signed cert. - trustEnv, err := setup.TrustEnv() - if err != nil { - return err - } - info.ComponentEnvList = append(info.ComponentEnvList, trustEnv...) - } - - if len(rcMap) == 0 { - return nil - } - - rcEnv, rcCleanup, err := rc.Generate(rcMap, atmosConfig, info) - if err != nil { - return err - } - info.ComponentEnvList = append(info.ComponentEnvList, rcEnv...) - info.RCCleanup = rcCleanup - return nil -} - -// mergeCacheContribution merges the registry cache's CLI-config directives into -// rcMap. The cache owns provider_installation (it must be the single network-mirror -// egress); host overrides are merged per-host so user-declared hosts are preserved. -func mergeCacheContribution(rcMap, contribution map[string]any) { - const hostKey = "host" - for key, value := range contribution { - if key != hostKey { - rcMap[key] = value - continue - } - add, _ := value.(map[string]any) - existing, _ := rcMap[hostKey].(map[string]any) - if existing == nil { - rcMap[hostKey] = add - continue - } - for host, services := range add { - existing[host] = services - } - rcMap[hostKey] = existing - } -} - -// cloneRCValue deep-copies a CLI-config value so callers can mutate the result without -// affecting the source. Maps and slices are cloned recursively; scalars are returned -// as-is. This protects atmosConfig.Components.Terraform.RC.Config from in-place mutation -// when the registry cache contribution is merged. -func cloneRCValue(v any) any { - switch val := v.(type) { - case map[string]any: - out := make(map[string]any, len(val)) - for k, item := range val { - out[k] = cloneRCValue(item) - } - return out - case []any: - out := make([]any, len(val)) - for i, item := range val { - out[i] = cloneRCValue(item) - } - return out - default: - return val - } -} - -// shouldRunTerraformInit returns true when a `terraform init` should be executed as a -// pre-step before the main command. Init is skipped when: the subcommand is init -// itself (init runs as the main command), deploy with DeployRunInit=false is configured, -// the caller passed the --skip-init flag, or components.terraform.init.mode is "never". -// Note this only gates whether the smart-init decision point (executeTerraformInitCommand) -// runs at all -- the decision of whether that pre-step actually shells out to `terraform -// init`, and with which flags, is made by autoinit.Decide once it does run. -// -// The init.mode: never check below excludes the workspace subcommand: autoinit.RequestFromInfo -// forces Decide's decision (Force: true) whenever info.SubCommand == "workspace", independent of -// init.mode, because workspace select/new needs a freshly reconfigured backend regardless of -// whether the ordinary pre-command init would otherwise run -- this predates init.mode entirely -// (see buildInitArgs's history). Gating that decision point out here for mode: never would -// silently drop that forced reconfigure and break workspace select/new for anyone who set -// init.mode: never intending only to suppress the ordinary unconditional pre-command init. -func shouldRunTerraformInit(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) bool { - if info.SubCommand == subcommandInit { - return false - } - if info.SubCommand == subcommandDeploy && !atmosConfig.Components.Terraform.DeployRunInit { - return false - } - if info.SkipInit { - log.Debug("Skipping over 'terraform init' due to '--skip-init' flag being passed") - return false - } - if info.SubCommand != subcommandWorkspace && atmosConfig.Components.Terraform.EffectiveInitMode() == schema.TerraformInitModeNever { - log.Debug("Skipping over 'terraform init' due to components.terraform.init.mode: never") - return false - } - return true -} - -// buildInitArgs constructs the argument list for `terraform init` from an already-computed -// autoinit.Decision: `init`, then `-reconfigure` when decision.Reconfigure, then `-upgrade` -// when decision.Upgrade, then `-var-file ` when Init.PassVars is enabled. -// -// This replaces a former workdir-specific carve-out that ignored the legacy -// InitRunReconfigure setting unless the workdir was actually re-provisioned this invocation -// (WorkdirReprovisionedKey) -- adding -reconfigure unconditionally used to make OpenTofu treat -// init as a fresh backend initialization and prompt "Do you want to migrate all workspaces?" -// whenever it saw existing workspace state directories (terraform.tfstate.d/) but no evidence -// the backend itself had moved; decision.Reconfigure now encodes exactly that rule for every -// component (not just workdir ones): auto reconfigure compares the current and last-recorded -// backend fingerprints (see autoinit.Decide), so -reconfigure is added only when the backend -// configuration actually changed, or a caller explicitly forced it (workspace subcommand, a -// re-provisioned workdir, or `init.reconfigure: always`). -// -//nolint:gocritic // autoinit.Decision (88 bytes) is passed by value throughout this call chain and its tests; not worth a pointer for a non-hot-path helper. -func buildInitArgs(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, varFile string, decision autoinit.Decision) []string { - args := []string{subcommandInit} - if decision.Reconfigure { - args = append(args, "-reconfigure") - } - if decision.Upgrade { - args = append(args, "-upgrade") - } - if atmosConfig.Components.Terraform.Init.PassVars { - args = append(args, varFileFlag, varFile) - } - log.Debug("autoinit: init args", logFieldComponent, info.ComponentFromArg, "args", args) - return args -} - -// newAutoInitInputs builds this invocation's fingerprint inputs -- the thin internal/exec-local -// wrapper around autoinit.InputsFromInfo shared by buildInitSubcommandArgs, recordAutoInit, and -// executeTerraformInitForced. -func newAutoInitInputs(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, varFile string) *autoinit.Inputs { - return autoinit.InputsFromInfo(atmosConfig, info, componentPath, varFile) -} - -// decideAutoInit resolves atmos.yaml's init policy plus force into an autoinit.Request and -// returns autoinit.Decide's outcome -- the thin internal/exec-local wrapper around -// autoinit.RequestFromInfo + autoinit.Decide used by buildInitSubcommandArgs for an explicit -// `atmos terraform init`. -func decideAutoInit(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, in *autoinit.Inputs, force bool) autoinit.Decision { - return autoinit.Decide(autoinit.RequestFromInfo(atmosConfig, info, in, force)) -} - -// recordAutoInit best-effort records the init marker after a successful init -- the thin -// internal/exec-local wrapper around autoinit.RecordFromInfo. -func recordAutoInit(in *autoinit.Inputs, initArgs []string) { - autoinit.RecordFromInfo(in, initArgs) -} - -// classifyInitRecovery applies Atmos's init-recovery policy to a failed main command's captured -// output: classify the diagnostic (autoinit.Classify), then decide whether/how to recover -// (autoinit.ShouldRecover) given the component's resolved init policy and opt-out signals. Used -// by recoverFromInitRequired, smart init's plan/apply-time safety net. -func classifyInitRecovery(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, output string) (autoinit.Recovery, error) { - tf := &atmosConfig.Components.Terraform - return autoinit.ShouldRecover( - autoinit.Classify(output), - tf.EffectiveInitMode(), - tf.EffectiveInitReconfigure(), - tf.EffectiveInitUpgrade(), - autoinit.OptedOut(atmosConfig, info), - ) -} - -// executeTerraformInitForced runs `terraform init` unconditionally with exactly the -// -reconfigure/-upgrade flags smart-init's plan/apply-time recovery (recoverFromInitRequired) -// determined were needed, dispatches the after.terraform.init provisioners, and records the -// resulting marker -- exactly as an implicit init would. -// -//nolint:revive // argument-limit: every parameter is load-bearing context from recoverFromInitRequired's forced init/retry. -func executeTerraformInitForced(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, varFile string, reconfigure, upgrade bool, opts ...ShellCommandOption) error { - defer perf.Track(atmosConfig, "exec.executeTerraformInitForced")() - - args := []string{subcommandInit} - if reconfigure { - args = append(args, "-reconfigure") - } - if upgrade { - args = append(args, "-upgrade") - } - if atmosConfig.Components.Terraform.Init.PassVars { - args = append(args, varFileFlag, varFile) - } - - in := newAutoInitInputs(atmosConfig, info, componentPath, varFile) - // Invalidate before attempting: see executeTerraformInitCommand's matching call for why a - // forced init that fails must not leave a stale-but-still-matching marker behind. - autoinit.InvalidateFromInfo(in) - if _, err := runTerraformInitSubprocess(atmosConfig, info, componentPath, args, opts...); err != nil { - return err - } - - dispatchAfterInit(atmosConfig, info, componentPath, opts...) - recordAutoInit(in, args) - return nil -} - -// executeTerraformInitForcedFn is a seam for testing recoverFromInitRequired's forced-init -// recovery path without launching a real subprocess. -var executeTerraformInitForcedFn = executeTerraformInitForced - -// prepareInitExecution performs the pre-init housekeeping: -// 1. Deletes the .terraform/environment file so Terraform doesn't prompt for workspace selection -// (skipped for workdir-enabled components — see note below). -// 2. Executes all provisioners registered for the before.terraform.init hook event. -// 3. Returns the effective component path (which may be overridden by a workdir provisioner). -// -// NOTE on cleanTerraformWorkspace and workdir components: -// cleanTerraformWorkspace was designed to prevent workspace-selection prompts when different -// backends are used for the same component across runs. For workdir-enabled components the -// backend configuration is always consistent (generated fresh from the same stack config), -// so deleting .terraform/environment is not only unnecessary — it is actively harmful: -// when -reconfigure or init_run_reconfigure is also used, OpenTofu sees workspace state -// directories (terraform.tfstate.d/) but no .terraform/environment file and interprets the -// situation as a backend migration, producing the "Do you want to migrate all workspaces?" -// prompt on every apply. Skipping the cleanup for workdir components avoids this. -func prepareInitExecution(ctx context.Context, writers provisioner.OutputWriters, atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string) (string, error) { - _, isWorkdir := info.ComponentSection[provWorkdir.WorkdirPathKey].(string) - if !isWorkdir { - cleanTerraformWorkspace(*atmosConfig, componentPath) - } - - if ctx == nil { - ctx = context.Background() - } - provisionCtx, cancel := context.WithTimeout(ctx, 5*time.Minute) - defer cancel() - - if err := executeBeforeInitProvisioners( - provisionCtx, - provisioner.HookEvent(beforeTerraformInitEvent), - atmosConfig, - info.ComponentSection, - info.AuthContext, - writers, - ); err != nil { - return componentPath, fmt.Errorf("provisioner execution failed: %w", err) - } - - if workdirPath, ok := info.ComponentSection[provWorkdir.WorkdirPathKey].(string); ok && workdirPath != "" { - log.Debug("Using workdir path for terraform command", "workdirPath", workdirPath) - return workdirPath, nil - } - - return componentPath, nil -} - -// executeTerraformInitPhase runs `terraform init` as a pre-step before the main command. -// It prepares the init execution environment, builds the init args, and delegates to -// ExecuteShellCommand. Returns the (possibly updated) component path. -// -// MUTUAL EXCLUSION CONTRACT: this function is called ONLY when shouldRunTerraformInit() -// returns true (i.e. SubCommand ≠ "init"). For the "init" subcommand itself, -// buildInitSubcommandArgs in terraform_execute_helpers_args.go handles the provisioner -// invocation via prepareInitExecution. These two code paths must never both execute -// in the same command invocation or provisioners will run twice. -func executeTerraformInitPhase(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, varFile string, opts ...ShellCommandOption) (string, error) { - newPath, err := prepareInitExecution(shellCommandContext(opts...), shellCommandOutputWriters(opts...), atmosConfig, info, componentPath) - if err != nil { - return componentPath, err - } - - if err = executeTerraformInitCommand(atmosConfig, info, newPath, varFile, opts...); err != nil { - return newPath, err - } - - return newPath, nil -} - -// runTerraformInitSubprocess runs `terraform init` (or a forced re-init retry with recovery -// flags) with args, teeing its stdout+stderr into a buffer so callers can classify the output -// for smart-init recovery diagnostics. Shared by executeTerraformInitCommand's own init run and -// executeCommandPipeline's plan/apply-time forced re-init retry -- the one low-level piece both -// need that pkg/terraform/autoinit itself cannot own, since it talks to ExecuteShellCommandWithRetry -// and executeStreamingOrShell, both internal/exec concerns. -func runTerraformInitSubprocess(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, args []string, opts ...ShellCommandOption) (string, error) { - var buf bytes.Buffer - stdoutW, stderrW := composeRetryCaptureWriters(opts, &buf) - captureOpts := append(slices.Clone(opts), WithStdoutCapture(stdoutW), WithStderrCapture(stderrW)) - - err := ExecuteShellCommandWithRetry( - atmosConfig, - info, - "init", - func(o ...ShellCommandOption) error { - return executeStreamingOrShell(atmosConfig, info, &streamingExecRequest{ - componentPath: componentPath, - args: args, - gatePhase: subcommandInit, - subCommand: subcommandInit, - redirectStdErr: info.RedirectStdErr, - shellOpts: o, - }) - }, - captureOpts..., - ) - return buf.String(), err -} - -// executeTerraformInitCommand is the "smart init" decision point: it fingerprints this -// invocation's init inputs, compares them against the marker recorded by the last successful -// init (autoinit.Decide), and either skips the subprocess entirely or runs it with exactly the -// flags the decision calls for -- recovering once, via autoinit.RecoverInit, if terraform/tofu's -// own output says init needs -upgrade or -reconfigure after all. Unlike executeTerraformInitPhase -// it does NOT run prepareInitExecution — it skips cleanTerraformWorkspace, the -// before.terraform.init provisioners, and workdir-path resolution. Callers that have already -// performed that preparation (e.g. ExecuteTerraformShell, which fires the before.terraform.init -// provisioners itself) use this directly to avoid running the provisioners twice. -func executeTerraformInitCommand(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, varFile string, opts ...ShellCommandOption) error { - defer perf.Track(atmosConfig, "exec.executeTerraformInitCommand")() - - in := autoinit.InputsFromInfo(atmosConfig, info, componentPath, varFile) - decision := autoinit.Decide(autoinit.RequestFromInfo(atmosConfig, info, in, false)) - - if !decision.RunInit { - info.InitSkipped = true - autoinit.AnnounceSkipped(info, decision.Reason) - return nil - } - - tf := &atmosConfig.Components.Terraform - args := autoinit.InitArgs(decision, tf.Init.PassVars, varFile) - // Invalidate before attempting: if this init fails, a stale-but-still-matching marker must - // not survive it -- see autoinit.InvalidateFromInfo's doc comment. RecordFromInfo below - // re-records a fresh marker once (and only once) this attempt or its recovery retry succeeds. - autoinit.InvalidateFromInfo(in) - output, err := runTerraformInitSubprocess(atmosConfig, info, componentPath, args, opts...) - usedArgs := args - - if err != nil { - var rec autoinit.Recovery - rec, err = autoinit.RecoverInit(autoinit.RecoverInitParams{ - Output: output + "\n" + err.Error(), - Err: err, - Mode: tf.EffectiveInitMode(), - Reconfigure: tf.EffectiveInitReconfigure(), - Upgrade: tf.EffectiveInitUpgrade(), - Rerun: func(r autoinit.Recovery) error { - _, rerunErr := runTerraformInitSubprocess(atmosConfig, info, componentPath, autoinit.ApplyRecovery(args, r), opts...) - return rerunErr - }, - Warn: func(msg string, kv ...any) { log.Warn(msg, kv...) }, - }) - if err != nil { - return err - } - usedArgs = autoinit.ApplyRecovery(args, rec) - } - - dispatchAfterInit(atmosConfig, info, componentPath, opts...) - autoinit.RecordFromInfo(in, usedArgs) - - return nil -} - -// dispatchAfterInit fires the after.terraform.init provisioners (e.g. the multi-platform -// providers-lock hook) once init has succeeded. It hands them a TerraformExecContext whose -// runner reuses the same binary, env (incl. TF_CLI_CONFIG_FILE pointing at the live proxy), -// and working directory as init, so a `providers lock` runs against the already-warm cache. -// The runner goes through executeStreamingOrShell (not a raw ExecuteShellCommand) so that, -// under --ui, `providers lock`'s own provider-fetch/checksum output (e.g. when a registry -// cache/mirror forces a cross-platform lock) is captured by the same init spinner instead of -// leaking raw terraform/opentofu output between the "Init … completed" and -// "Selected … workspace" lines. Lock completion is best-effort: a failure is logged, not -// propagated, so it never fails the user's plan/apply. -func dispatchAfterInit(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, opts ...ShellCommandOption) { - execCtx := &provisioner.TerraformExecContext{ - WorkingDir: componentPath, - Run: func(args []string) error { - return executeStreamingOrShell(atmosConfig, info, &streamingExecRequest{ - componentPath: componentPath, - args: args, - gatePhase: subcommandInit, - subCommand: subcommandProvidersLock, - redirectStdErr: info.RedirectStdErr, - shellOpts: opts, - }) - }, - } - - ctx, cancel := context.WithTimeout(shellCommandContext(opts...), 5*time.Minute) - defer cancel() - - if err := provisioner.ExecuteProvisioners( - ctx, - provisioner.HookEvent(afterTerraformInitEvent), - atmosConfig, - info.ComponentSection, - info.AuthContext, - shellCommandOutputWriters(opts...), - execCtx, - ); err != nil { - log.Warn("Failed to complete multi-platform provider lock", "error", err) - } -} - -// handleDeploySubcommand converts `deploy` into `apply` and ensures -auto-approve is -// added when appropriate. When ApplyAutoApprove is set in atmos.yaml, it is also -// applied to plain `apply` subcommands. -func handleDeploySubcommand(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) { - if info.SubCommand == subcommandDeploy { - info.SubCommand = subcommandApply - if !info.UseTerraformPlan && !slices.Contains(info.AdditionalArgsAndFlags, autoApproveFlag) { - info.AdditionalArgsAndFlags = append(info.AdditionalArgsAndFlags, autoApproveFlag) - } - } - - if info.SubCommand == subcommandApply && atmosConfig.Components.Terraform.ApplyAutoApprove && !info.UseTerraformPlan { - if !slices.Contains(info.AdditionalArgsAndFlags, autoApproveFlag) { - info.AdditionalArgsAndFlags = append(info.AdditionalArgsAndFlags, autoApproveFlag) - } - } -} - -// logTerraformContext emits a debug log line with the full execution context -// (executable, command, component, stack, flags, working directory, inheritance chain). -func logTerraformContext(info *schema.ConfigAndStacksInfo, workingDir string) { - command := info.SubCommand - if info.SubCommand2 != "" { - command = fmt.Sprintf("%s %s", info.SubCommand, info.SubCommand2) - } - - var inheritance string - if len(info.ComponentInheritanceChain) > 0 { - inheritance = info.ComponentFromArg + " -> " + strings.Join(info.ComponentInheritanceChain, " -> ") - } - - log.Debug( - "Terraform context", - "executable", info.Command, - "command", command, - logFieldComponent, info.ComponentFromArg, - "stack", info.StackFromArg, - "arguments and flags", info.AdditionalArgsAndFlags, - "terraform component", info.BaseComponentPath, - "inheritance", inheritance, - "working directory", workingDir, - ) -} diff --git a/internal/exec/terraform_execute_helpers_auth_test.go b/internal/exec/terraform_execute_helpers_auth_test.go index 83d893728a9..010609ceef2 100644 --- a/internal/exec/terraform_execute_helpers_auth_test.go +++ b/internal/exec/terraform_execute_helpers_auth_test.go @@ -36,7 +36,7 @@ func TestSetupTerraformAuth_EmptyConfig_NoProviders(t *testing.T) { ComponentFromArg: "", } - authMgr, err := setupTerraformAuth(&atmosConfig, &info) + authMgr, err := terraformExecutor.SetupComponentAuthForCLI(&atmosConfig, &info) require.NoError(t, err) // With no auth providers configured the AuthManager should be nil. assert.Nil(t, authMgr) @@ -59,7 +59,7 @@ func TestSetupTerraformAuth_ErrInvalidComponent(t *testing.T) { ComponentFromArg: "nonexistent", } - _, err := setupTerraformAuth(&atmosConfig, &info) + _, err := terraformExecutor.SetupComponentAuthForCLI(&atmosConfig, &info) require.Error(t, err) assert.True(t, errors.Is(err, errUtils.ErrInvalidComponent), "expected ErrInvalidComponent but got: %v", err) // Must NOT be additionally wrapped — doing so would change the sentinel seen by callers. @@ -79,7 +79,7 @@ func TestSetupTerraformAuth_AuthCreatorError_WrapsWithSentinel(t *testing.T) { atmosConfig := schema.AtmosConfiguration{} info := schema.ConfigAndStacksInfo{} - _, err := setupTerraformAuth(&atmosConfig, &info) + _, err := terraformExecutor.SetupComponentAuthForCLI(&atmosConfig, &info) require.Error(t, err) assert.True(t, errors.Is(err, errUtils.ErrFailedToInitializeAuthManager), "expected ErrFailedToInitializeAuthManager but got: %v", err) } @@ -106,7 +106,7 @@ func TestSetupTerraformAuth_IdentityStoredAndManagerSet(t *testing.T) { atmosConfig := schema.AtmosConfiguration{} info := schema.ConfigAndStacksInfo{Identity: ""} - mgr, err := setupTerraformAuth(&atmosConfig, &info) + mgr, err := terraformExecutor.SetupComponentAuthForCLI(&atmosConfig, &info) require.NoError(t, err) assert.Equal(t, mockMgr, mgr) // storeAutoDetectedIdentity takes the last element of the chain. @@ -128,7 +128,7 @@ func TestSetupTerraformAuth_NilManager_NoAuthBridge(t *testing.T) { info := schema.ConfigAndStacksInfo{} assert.NotPanics(t, func() { - mgr, err := setupTerraformAuth(&atmosConfig, &info) + mgr, err := terraformExecutor.SetupComponentAuthForCLI(&atmosConfig, &info) require.NoError(t, err) assert.Nil(t, mgr) assert.Nil(t, info.AuthManager) @@ -148,7 +148,7 @@ func TestSetupTerraformAuth_MergedConfigError_WrapsWithInvalidAuthConfig(t *test atmosConfig := schema.AtmosConfiguration{} info := schema.ConfigAndStacksInfo{Stack: "dev", ComponentFromArg: "mycomp"} - _, err := setupTerraformAuth(&atmosConfig, &info) + _, err := terraformExecutor.SetupComponentAuthForCLI(&atmosConfig, &info) require.Error(t, err) assert.True(t, errors.Is(err, errUtils.ErrInvalidAuthConfig), "expected ErrInvalidAuthConfig, got: %v", err) assert.False(t, errors.Is(err, errUtils.ErrInvalidComponent)) @@ -221,7 +221,7 @@ func TestSetupTerraformAuth_IdentityFlagPropagatesToAuthCreator(t *testing.T) { SubCommand: "plan", } - _, err := setupTerraformAuth(&atmosConfig, &info) + _, err := terraformExecutor.SetupComponentAuthForCLI(&atmosConfig, &info) require.NoError(t, err) assert.Equal(t, "core-root/admin", capturedIdentity, @@ -256,7 +256,7 @@ func TestSetupTerraformAuth_EmptyIdentity_AllowsAutoDetection(t *testing.T) { atmosConfig := schema.AtmosConfiguration{} info := schema.ConfigAndStacksInfo{Identity: ""} - _, err := setupTerraformAuth(&atmosConfig, &info) + _, err := terraformExecutor.SetupComponentAuthForCLI(&atmosConfig, &info) require.NoError(t, err) assert.Equal(t, "", capturedIdentity, @@ -286,7 +286,7 @@ func TestSetupTerraformAuth_MergedConfigError_IdentityConfigOffersProfileFallbac atmosConfig := schema.AtmosConfiguration{CliConfigPath: tmpDir} info := schema.ConfigAndStacksInfo{Stack: "core-ue2-auto", ComponentFromArg: "vpc"} - _, err := setupTerraformAuth(&atmosConfig, &info) + _, err := terraformExecutor.SetupComponentAuthForCLI(&atmosConfig, &info) require.Error(t, err) assert.True(t, errors.Is(err, errUtils.ErrIdentityNotFound), "expected the profile-fallback's ErrIdentityNotFound, got: %v", err) @@ -312,7 +312,7 @@ func TestSetupTerraformAuth_MergedConfigError_IdentityConfigNoCandidateStillWrap atmosConfig := schema.AtmosConfiguration{CliConfigPath: tmpDir} info := schema.ConfigAndStacksInfo{Stack: "core-ue2-auto", ComponentFromArg: "vpc"} - _, err := setupTerraformAuth(&atmosConfig, &info) + _, err := terraformExecutor.SetupComponentAuthForCLI(&atmosConfig, &info) require.Error(t, err) assert.True(t, errors.Is(err, errUtils.ErrInvalidAuthConfig), "no candidate profile → original wrap must be preserved") diff --git a/internal/exec/terraform_execute_helpers_exec.go b/internal/exec/terraform_execute_helpers_exec.go deleted file mode 100644 index 2f64721b4a7..00000000000 --- a/internal/exec/terraform_execute_helpers_exec.go +++ /dev/null @@ -1,897 +0,0 @@ -package exec - -// terraform_execute_helpers_exec.go contains the high-level execution pipeline and -// the lower-level workspace / TTY / exit-code helpers extracted from ExecuteTerraform. -// -// The two orchestrators here (prepareComponentExecution, executeCommandPipeline) are the -// primary tools for reducing ExecuteTerraform's cyclomatic complexity from ~25 to ~10. - -import ( - "bytes" - "context" - "errors" - "fmt" - "io" - "os" - osexec "os/exec" - "path/filepath" - "slices" - "strings" - "time" - - errUtils "github.com/cloudposse/atmos/errors" - auth "github.com/cloudposse/atmos/pkg/auth" - "github.com/cloudposse/atmos/pkg/ci" - "github.com/cloudposse/atmos/pkg/dependencies" - git "github.com/cloudposse/atmos/pkg/git" - log "github.com/cloudposse/atmos/pkg/logger" - metricsprocess "github.com/cloudposse/atmos/pkg/metrics/process" - "github.com/cloudposse/atmos/pkg/perf" - "github.com/cloudposse/atmos/pkg/policy/planfile" - "github.com/cloudposse/atmos/pkg/pro" - "github.com/cloudposse/atmos/pkg/proexec" - "github.com/cloudposse/atmos/pkg/provisioner" - "github.com/cloudposse/atmos/pkg/retry" - "github.com/cloudposse/atmos/pkg/schema" - "github.com/cloudposse/atmos/pkg/terraform/autoinit" - "github.com/cloudposse/atmos/pkg/ui" -) - -// componentExecContext holds the per-execution state assembled by prepareComponentExecution. -// It is consumed by executeCommandPipeline without further modification. -type componentExecContext struct { - componentPath string - varFile string - testVarFile string - planFile string - workingDir string - tenv *dependencies.ToolchainEnvironment - planPolicy *planfile.Guard -} - -// prepareComponentExecution consolidates all per-component setup into one call: -// path resolution, access checks, toolchain installation, variable file generation, -// OPA/JSON-schema validation, auth pre-hook, config file generation, and env assembly. -// Extracting this reduces ExecuteTerraform's cyclomatic complexity by ~10 decision points. -func prepareComponentExecution( - ctx context.Context, - writers provisioner.OutputWriters, - atmosConfig *schema.AtmosConfiguration, - info *schema.ConfigAndStacksInfo, - shouldProcess bool, -) (*componentExecContext, error) { - if err := checkTerraformConfig(*atmosConfig); err != nil { - return nil, err - } - - componentPath, err := resolveAndProvisionComponentPath(ctx, writers, atmosConfig, info) - if err != nil { - return nil, err - } - - if err = checkComponentRestrictions(info); err != nil { - return nil, err - } - - var tenv *dependencies.ToolchainEnvironment - if shouldProcess { - tenv, err = resolveAndInstallToolchainDeps(atmosConfig, info) - if err != nil { - return nil, err - } - info.Command = tenv.Resolve(info.Command) - } - - varFile := constructTerraformComponentVarfileName(info) - testVarFile := "" - if hasTerraformTestVars(info) { - testVarFile = constructTerraformComponentTestVarfileName(info) - } - planFile := constructTerraformComponentPlanfileName(info) - workingDir := constructTerraformComponentWorkingDir(atmosConfig, info) - - if err = runPreExecutionSteps(atmosConfig, info, workingDir, tenv); err != nil { - return nil, err - } - - return &componentExecContext{ - componentPath: componentPath, - varFile: varFile, - testVarFile: testVarFile, - planFile: planFile, - workingDir: workingDir, - tenv: tenv, - }, nil -} - -// runPreExecutionSteps performs validation, auth pre-hook, config file generation, -// env var warnings, and env var assembly. Extracted from prepareComponentExecution -// to keep cyclomatic complexity within bounds. -func runPreExecutionSteps( - atmosConfig *schema.AtmosConfiguration, - info *schema.ConfigAndStacksInfo, - workingDir string, - tenv *dependencies.ToolchainEnvironment, -) error { - // `(computed)` is an inspection-only degradation marker. Never permit it to - // reach Terraform via a generated tfvars file or TF_VAR_ environment value. - if err := rejectComputedTerraformVars(info.ComponentVarsSection); err != nil { - return err - } - - // Partition variables into disk-safe vs. secret-bearing BEFORE writing the varfile or - // assembling env vars (and before the auth pre-hook registers credentials with the - // masker), so secrets are kept off disk and injected as TF_VAR_* env vars instead. - computeTerraformSecretVarKeys(info) - - if err := printAndWriteVarFiles(atmosConfig, info); err != nil { - return err - } - - if err := validateTerraformComponent(atmosConfig, info); err != nil { - return err - } - - if err := auth.TerraformPreHook(atmosConfig, info); err != nil { - // The error is returned and rendered once by the command boundary, so it is only logged - // at debug level here to avoid printing the same failure twice. - log.Debug("Error executing 'atmos auth terraform pre-hook'", - logFieldComponent, info.ComponentFromArg, "error", err) - // Pre-hook failures terminate execution — this matches the original terraform.go behavior. - // Authentication setup failures must not silently produce unauthenticated terraform commands. - return err - } - disableTerraformPluginCacheForExecution(atmosConfig, info) - - // Generate backend and provider-override files. When AutoGenerateFiles=true, - // GenerateFilesForComponent was already called inside resolveAndProvisionComponentPath - // for generate: section files. This call handles the distinct backend/provider-override - // responsibility. - if err := generateConfigFiles(atmosConfig, info, workingDir); err != nil { - return err - } - - warnOnConflictingEnvVars() - - return assembleComponentEnvVars(atmosConfig, info, tenv) -} - -// executeCommandPipeline runs the full terraform command pipeline after the component -// has been prepared: optional init pre-step, argument construction, workspace setup, -// TTY guard, and final command execution + cleanup. -// Extracting this reduces ExecuteTerraform's cyclomatic complexity by ~7 decision points. -// This builds the CI log-group label for a terraform/tofu phase, -// e.g. "terraform init" or "tofu plan". The command is the resolved executable -// (often an absolute toolchain path), so it is reduced to its base name. -func terraformPhaseLabel(info *schema.ConfigAndStacksInfo, phase string) string { - tool := filepath.Base(strings.TrimSpace(info.Command)) - if tool == "" || tool == "." { - tool = "terraform" - } - return strings.TrimSpace(tool + " " + phase) -} - -func executeCommandPipeline( - atmosConfig *schema.AtmosConfiguration, - info *schema.ConfigAndStacksInfo, - execCtx *componentExecContext, - opts ...ShellCommandOption, -) error { - componentPath := execCtx.componentPath - - if shouldRunTerraformInit(atmosConfig, info) { - // Phase-level CI log grouping (Dimension "phase"): fold the `init` phase - // into its own collapsible group. A no-op unless ci.groups.mode is "auto" - // and this is the outermost Atmos invocation (a terraform run nested inside - // a workflow step stays flat inside the step's group). - err := ci.Group(atmosConfig, ci.DimensionPhase, terraformPhaseLabel(info, subcommandInit), func() error { - var phaseErr error - componentPath, phaseErr = executeTerraformInitPhase(atmosConfig, info, componentPath, execCtx.varFile, opts...) - return phaseErr - }) - if err != nil { - return err - } - } - - handleDeploySubcommand(atmosConfig, info) - logTerraformContext(info, execCtx.workingDir) - addTerraformTestVarfileArg(info, execCtx.testVarFile) - - allArgsAndFlags, uploadStatusFlag, err := buildTerraformCommandArgs(atmosConfig, info, execCtx.varFile, execCtx.planFile, &componentPath, opts...) - if err != nil { - return err - } - - if err = runWorkspaceSetupPhase(atmosConfig, info, componentPath, opts...); err != nil { - return err - } - - if err = checkTTYRequirement(info); err != nil { - return err - } - - addRegionEnvVarForImport(info) - - if shouldRunMainTerraformCommand(info) { - if err = runMainCommandPhase(atmosConfig, info, execCtx, allArgsAndFlags, componentPath, uploadStatusFlag, opts...); err != nil { - return err - } - } - - cleanupTerraformFiles(atmosConfig, info) - return nil -} - -// runMainCommandPhase runs the main plan/apply/destroy/… subcommand under its own CI phase -// group, capturing scoped exec-metadata output (FR-006f), and — on failure — gives smart init -// exactly one chance to recover via recoverFromInitRequired before propagating the error. -func runMainCommandPhase( //nolint:revive // argument-limit: every parameter is load-bearing context passed straight through to recoverFromInitRequired. - atmosConfig *schema.AtmosConfiguration, - info *schema.ConfigAndStacksInfo, - execCtx *componentExecContext, - allArgsAndFlags []string, - componentPath string, - uploadStatusFlag bool, - opts ...ShellCommandOption, -) error { - // FR-006f: capture this invocation's own stdout/stderr into a buffer pair - // scoped to ONLY the main command, in addition to (not instead of) whatever - // WithStdoutCapture/WithStderrCapture already accumulate across the whole - // init+workspace-select+main pipeline for other consumers (e.g. - // cmd/terraform's capturedPlanOutput, used by CI job-summary hooks). Without - // this, incidental init/workspace-select output (e.g. a stray "No changes." - // lookalike) can poison the exec-metadata parser's extraction even though the - // real plan/apply's own output is correct (research.md Decision 32). - var execMetadataStdoutBuf, execMetadataStderrBuf bytes.Buffer - mainOpts := append(slices.Clone(opts), withExecMetadataOutputCapture(&execMetadataStdoutBuf, &execMetadataStderrBuf)) - - // Phase-level CI log grouping (Dimension "phase"): fold the main subcommand - // (plan/apply/destroy/…) into its own collapsible group, separate from init - // and workspace setup. - mainErr := ci.Group(atmosConfig, ci.DimensionPhase, terraformPhaseLabel(info, info.SubCommand), func() error { - return executeGovernedTerraformCommand(atmosConfig, info, allArgsAndFlags, componentPath, uploadStatusFlag, execCtx.planPolicy, mainOpts...) - }) - info.ExecMetadataRawOutput = combineExecMetadataOutput(&execMetadataStdoutBuf, &execMetadataStderrBuf) - if mainErr == nil { - return nil - } - - return recoverFromInitRequired(atmosConfig, info, execCtx, allArgsAndFlags, componentPath, uploadStatusFlag, - mainErr, &execMetadataStdoutBuf, &execMetadataStderrBuf, opts...) -} - -// recoverFromInitRequired is smart init's plan/apply-time safety net: when the main command -// just failed, it classifies the captured output for a known "init is required" diagnostic -// (autoinit.Classify) and, if Atmos's own recovery policy says to (autoinit.ShouldRecover), -// forces exactly one `terraform init` re-run with the flag(s) the diagnostic asked for, then -// retries the main command exactly once more. This is the correction for a smart-init bet gone -// wrong: skipping init looked safe based on the fingerprint, but terraform/tofu itself now -// disagrees. -// -// No recovery is attempted for the init subcommand itself (there is no "init required" fallback -// for init) or when ShouldRecover reports a policy error (e.g. the user explicitly disabled -// implicit init) — that error is joined with the original failure and returned as-is. -func recoverFromInitRequired( //nolint:revive // argument-limit: every parameter is load-bearing context from runMainCommandPhase. - atmosConfig *schema.AtmosConfiguration, - info *schema.ConfigAndStacksInfo, - execCtx *componentExecContext, - allArgsAndFlags []string, - componentPath string, - uploadStatusFlag bool, - mainErr error, - stdoutBuf, stderrBuf *bytes.Buffer, - opts ...ShellCommandOption, -) error { - if info.SubCommand == subcommandInit { - return mainErr - } - - combined := stdoutBuf.String() + "\n" + stderrBuf.String() + "\n" + mainErr.Error() - rec, policyErr := classifyInitRecovery(atmosConfig, info, combined) - if policyErr != nil { - return errors.Join(mainErr, policyErr) - } - if !rec.Run { - return mainErr - } - - ui.Warning(fmt.Sprintf("Terraform requires initialization; running init and retrying '%s'", info.SubCommand)) - - initErr := ci.Group(atmosConfig, ci.DimensionPhase, terraformPhaseLabel(info, subcommandInit), func() error { - return executeTerraformInitForcedFn(atmosConfig, info, componentPath, execCtx.varFile, rec.WithReconfigure, rec.WithUpgrade, opts...) - }) - if initErr != nil { - return errors.Join(mainErr, initErr) - } - - return retryMainCommand(atmosConfig, info, allArgsAndFlags, componentPath, uploadStatusFlag, execCtx.planPolicy, opts...) -} - -// retryMainCommand re-runs the main subcommand once after a forced init recovery, resetting the -// exec-metadata capture so info.ExecMetadataRawOutput reflects only the retry's own output. -func retryMainCommand( //nolint:revive // argument-limit: every parameter is load-bearing context passed straight through to executeMainTerraformCommandFn. - atmosConfig *schema.AtmosConfiguration, - info *schema.ConfigAndStacksInfo, - allArgsAndFlags []string, - componentPath string, - uploadStatusFlag bool, - gate *planfile.Guard, - opts ...ShellCommandOption, -) error { - var execMetadataStdoutBuf, execMetadataStderrBuf bytes.Buffer - mainOpts := append(slices.Clone(opts), withExecMetadataOutputCapture(&execMetadataStdoutBuf, &execMetadataStderrBuf)) - - err := ci.Group(atmosConfig, ci.DimensionPhase, terraformPhaseLabel(info, info.SubCommand), func() error { - if gate != nil { - return executeGovernedTerraformCommand(atmosConfig, info, allArgsAndFlags, componentPath, uploadStatusFlag, gate, mainOpts...) - } - return executeMainTerraformCommandFn(atmosConfig, info, allArgsAndFlags, componentPath, uploadStatusFlag, mainOpts...) - }) - info.ExecMetadataRawOutput = combineExecMetadataOutput(&execMetadataStdoutBuf, &execMetadataStderrBuf) - return err -} - -// dispatchAfterInitFn is a seam for testing the explicit-init path. Implicit -// init dispatches the same provisioners in executeTerraformInitCommand. -var dispatchAfterInitFn = dispatchAfterInit - -// executeMainTerraformCommandFn is a seam for testing recoverFromInitRequired's retry-once -// behavior (retryMainCommand) without launching a real subprocess. -var executeMainTerraformCommandFn = executeMainTerraformCommand - -// runWorkspaceSetupPhase selects or creates the Terraform workspace under its own -// phase-level CI log group. The group is emitted only when workspace setup will -// actually run, avoiding empty groups for backends/subcommands that skip setup. -func runWorkspaceSetupPhase(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, opts ...ShellCommandOption) error { - if shouldSkipWorkspaceSetup(info) { - return nil - } - return ci.Group(atmosConfig, ci.DimensionPhase, terraformPhaseLabel(info, subcommandWorkspace), func() error { - return runWorkspaceSetup(atmosConfig, info, componentPath, opts...) - }) -} - -func shouldRunMainTerraformCommand(info *schema.ConfigAndStacksInfo) bool { - return info.SubCommand != subcommandWorkspace || info.SubCommand2 != "" -} - -// combineExecMetadataOutput concatenates the main command's scoped stdout/stderr -// capture for the exec-metadata parser (FR-006f), mirroring the stdout+"\n"+stderr -// convention cmd/terraform's terraformExecMetadataParserFunc previously applied -// when it owned buffer construction directly. -func combineExecMetadataOutput(stdoutBuf, stderrBuf *bytes.Buffer) string { - combined := stdoutBuf.String() - if errOut := stderrBuf.String(); errOut != "" { - combined += "\n" + errOut - } - return combined -} - -func addTerraformTestVarfileArg(info *schema.ConfigAndStacksInfo, testVarFile string) { - if info.SubCommand != "test" || testVarFile == "" { - return - } - info.AdditionalArgsAndFlags = append([]string{varFileFlag, testVarFile}, info.AdditionalArgsAndFlags...) -} - -// shouldSkipWorkspaceSetup returns true when workspace setup should be skipped. -func shouldSkipWorkspaceSetup(info *schema.ConfigAndStacksInfo) bool { - if info.SubCommand == subcommandInit || (info.SubCommand == subcommandWorkspace && info.SubCommand2 != "") { - return true - } - // `providers` subcommands (mirror, lock, schema) operate on configuration, not - // state, so they need no workspace — and selecting one fails on never-applied - // components. - if strings.HasPrefix(info.SubCommand, "providers") { - return true - } - if info.ComponentBackendType == "http" { - return true - } - //nolint:forbidigo // TF_WORKSPACE is a Terraform convention, not an Atmos config var. - return os.Getenv("TF_WORKSPACE") != "" -} - -// runWorkspaceSetup selects (or creates) the Terraform workspace before the main command -// runs. It is a no-op when shouldSkipWorkspaceSetup returns true. -func runWorkspaceSetup(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, opts ...ShellCommandOption) error { - if shouldSkipWorkspaceSetup(info) { - return nil - } - - // Default: merge workspace-select stderr into stdout and capture both streams. - // Workspace setup is an Atmos pre-command implementation detail; Terraform may - // print "Workspace doesn't exist" or "You're now on a new, empty workspace" - // during normal first-run setup, which is noisy in user-facing command output. - redirectStdErr := "/dev/stdout" - if info.RedirectStdErr != "" { - redirectStdErr = info.RedirectStdErr - } - - // Merge caller-provided opts (e.g., WithEnvironment) with workspace-specific opts. - var workspaceOutput bytes.Buffer - wsOpts := append([]ShellCommandOption{}, opts...) - wsOpts = append(wsOpts, WithStdoutOverride(&workspaceOutput)) - - err := ExecuteShellCommandWithRetry( - atmosConfig, - info, - "workspace-select", - func(o ...ShellCommandOption) error { - return executeStreamingOrShell(atmosConfig, info, &streamingExecRequest{ - componentPath: componentPath, - args: []string{"workspace", "select", "-or-create", info.TerraformWorkspace}, - gatePhase: subcommandInit, - subCommand: subcommandWorkspace, - workspace: info.TerraformWorkspace, - redirectStdErr: redirectStdErr, - shellOpts: o, - }) - }, - wsOpts..., - ) - if err == nil { - return nil - } - - selectStep := workspaceStepOutput{command: "terraform workspace select -or-create", output: workspaceOutput.String()} - - // Exit code 1 means the workspace doesn't exist yet; create it. - var exitCodeErr errUtils.ExitCodeError - if !errors.As(err, &exitCodeErr) || exitCodeErr.Code != 1 { - return workspaceSetupError(err, info.TerraformWorkspace, selectStep) - } - - return createWorkspaceFallback(atmosConfig, info, componentPath, selectStep, opts...) -} - -// workspaceStepOutput pairs a workspace subcommand with the output Terraform produced for it. -// Workspace setup output is hidden on success, so it is kept to explain a failure. -type workspaceStepOutput struct { - command string - output string -} - -// workspaceSetupError wraps a failed workspace setup step, attaching the output Terraform -// printed so the real cause (backend auth, permissions, lock, network) is not lost behind a -// bare exit code. The cause stays in the error chain, so the exit code is preserved. -func workspaceSetupError(cause error, workspace string, steps ...workspaceStepOutput) error { - builder := errUtils.Build(errUtils.ErrTerraformWorkspaceOp). - WithCause(cause). - WithContext("workspace", workspace). - WithHint("Check the backend credentials and permissions, then re-run with ATMOS_LOGS_LEVEL=debug for the full command trace") - - var sections []string - for _, step := range steps { - if output := strings.TrimSpace(step.output); output != "" { - sections = append(sections, fmt.Sprintf("%s:\n%s", step.command, output)) - } - } - if len(sections) > 0 { - builder = builder.WithExplanation(strings.Join(sections, "\n\n")) - } - - return builder.Err() -} - -// createWorkspaceFallback attempts to create a new workspace when select fails. -// If workspace creation also fails with exit code 1 and the workspace is already -// active (stale .terraform/environment file), it proceeds with a warning. Otherwise the -// returned error carries the output of both steps (selectStep is the failed select). -func createWorkspaceFallback(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, selectStep workspaceStepOutput, opts ...ShellCommandOption) error { - redirectStdErr := info.RedirectStdErr - if redirectStdErr == "" { - redirectStdErr = "/dev/stdout" - } - - var workspaceOutput bytes.Buffer - wsOpts := append([]ShellCommandOption{}, opts...) - wsOpts = append(wsOpts, WithStdoutOverride(&workspaceOutput)) - - newErr := ExecuteShellCommandWithRetry( - atmosConfig, - info, - "workspace-new", - func(o ...ShellCommandOption) error { - return executeStreamingOrShell(atmosConfig, info, &streamingExecRequest{ - componentPath: componentPath, - args: []string{"workspace", "new", info.TerraformWorkspace}, - gatePhase: subcommandInit, - subCommand: subcommandWorkspace, - workspace: info.TerraformWorkspace, - redirectStdErr: redirectStdErr, - shellOpts: o, - }) - }, - wsOpts..., - ) - if newErr == nil { - return nil - } - - // If `workspace new` also fails with exit code 1, the workspace may already be the - // active workspace (the .terraform/environment file names it) but its state directory - // was deleted. In that case we are already in the correct workspace and can proceed. - var newExitCodeErr errUtils.ExitCodeError - if errors.As(newErr, &newExitCodeErr) && newExitCodeErr.Code == 1 && - isTerraformCurrentWorkspace(componentPath, info.TerraformWorkspace, info.ComponentEnvList) { - log.Warn("Workspace is already active but its state directory is missing; proceeding — subsequent terraform commands may report missing state", - "workspace", info.TerraformWorkspace) - return nil - } - - newStep := workspaceStepOutput{command: "terraform workspace new", output: workspaceOutput.String()} - return workspaceSetupError(newErr, info.TerraformWorkspace, selectStep, newStep) -} - -// checkTTYRequirement returns an error when `terraform apply` is invoked without -// -auto-approve in a non-interactive environment (stdin is nil). -func checkTTYRequirement(info *schema.ConfigAndStacksInfo) error { - if os.Stdin != nil { - return nil - } - if info.SubCommand == subcommandApply && !slices.Contains(info.AdditionalArgsAndFlags, autoApproveFlag) { - return fmt.Errorf( - "%w: 'terraform apply' requires a user interaction, but no TTY is attached. "+ - "Use 'terraform apply -auto-approve' or 'terraform deploy' instead", - errUtils.ErrNoTty, - ) - } - return nil -} - -// addRegionEnvVarForImport appends AWS_REGION to the component env list when the -// subcommand is `import` and the component has a `region` variable configured. -func addRegionEnvVarForImport(info *schema.ConfigAndStacksInfo) { - if info.SubCommand != "import" { - return - } - if region, ok := info.ComponentVarsSection["region"].(string); ok { - info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("AWS_REGION=%s", region)) - } -} - -// resolveExitCode extracts the integer exit code from an error returned by -// ExecuteShellCommand. Returns 0 when err is nil, 1 for generic (non-typed) errors. -func resolveExitCode(err error) int { - if err == nil { - return 0 - } - var ec errUtils.ExitCodeError - if errors.As(err, &ec) { - return ec.Code - } - var osErr *osexec.ExitError - if errors.As(err, &osErr) { - return osErr.ExitCode() - } - return 1 -} - -// executeMainTerraformCommand runs the final terraform sub-command. -// It handles exit-code extraction, plan-status upload (for --upload-status), and -// appropriate error propagation. A no-op when info.SubCommand is "workspace" with -// no sub-subcommand (workspace listing was already handled by runWorkspaceSetup). -func executeMainTerraformCommand( //nolint:revive // argument-limit: opts variadic is not a true argument. - atmosConfig *schema.AtmosConfiguration, - info *schema.ConfigAndStacksInfo, - allArgsAndFlags []string, - componentPath string, - uploadStatusFlag bool, - opts ...ShellCommandOption, -) error { - // Bare `workspace` (no sub-subcommand) was fully handled by runWorkspaceSetup. - if info.SubCommand == subcommandWorkspace && info.SubCommand2 == "" { - return nil - } - - err := runMainTerraformCommand(atmosConfig, info, allArgsAndFlags, componentPath, opts...) - return finishMainTerraformCommand(atmosConfig, info, err, uploadStatusFlag) -} - -func runMainTerraformCommand( - atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, allArgsAndFlags []string, - componentPath string, opts ...ShellCommandOption, -) error { - // An explicit `atmos terraform init` about to run below (see the matching recordAutoInit - // call after ExecuteShellCommandWithRetry) must invalidate any existing marker first: see - // executeTerraformInitCommand's matching call for why a failed init must not leave a - // stale-but-still-matching marker behind for a later, unrelated invocation to trust. - if info.SubCommand == subcommandInit { - autoinit.InvalidateFromInfo(newAutoInitInputs(atmosConfig, info, componentPath, constructTerraformComponentVarfileName(info))) - } - - // capturedMetrics receives this invocation's subprocess-tree resource - // usage (terraform/tofu plus its own children, e.g. provider plugins) via - // WithMetricsCallback, for combining with atmos's own self-usage below. - var capturedMetrics *metricsprocess.ProcessMetrics - shellStartedAt := time.Now() - allOpts := make([]ShellCommandOption, 0, len(opts)+1) - allOpts = append(allOpts, opts...) - allOpts = append(allOpts, WithMetricsCallback(func(m *metricsprocess.ProcessMetrics) { - capturedMetrics = m - })) - - err := ExecuteShellCommandWithRetry( - atmosConfig, - info, - info.SubCommand, - func(o ...ShellCommandOption) error { - return executeStreamingOrShell(atmosConfig, info, &streamingExecRequest{ - componentPath: componentPath, - args: allArgsAndFlags, - gatePhase: info.SubCommand, - subCommand: info.SubCommand, - redirectStdErr: info.RedirectStdErr, - shellOpts: o, - }) - }, - allOpts..., - ) - - // An explicit `atmos terraform init` reaches this main-command path rather - // than executeTerraformInitCommand. Keep its lifecycle equivalent to an - // implicit init so post-init provisioners can complete and persist provider - // locks for workdir and vendored components, and so the smart-init marker - // gets recorded (recordAutoInit) exactly as it would for an implicit init. - if err == nil && info.SubCommand == subcommandInit { - dispatchAfterInitFn(atmosConfig, info, componentPath, opts...) - recordAutoInit(newAutoInitInputs(atmosConfig, info, componentPath, constructTerraformComponentVarfileName(info)), allArgsAndFlags) - } - exitCode := resolveExitCode(err) - - // FR-006e: record the terraform/tofu subprocess's own exit code before any - // CI-mode remapping or local neutralization below, so TerraformExecData.exit_code - // (via captureExecMetadataSync) reports the real subprocess outcome even when - // Atmos's own returned status is remapped/neutralized further down. - info.ExecMetadataRawExitCode = exitCode - recordMainTerraformMetrics(atmosConfig, info, capturedMetrics, shellStartedAt) - - return err -} - -func recordMainTerraformMetrics( - atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, - capturedMetrics *metricsprocess.ProcessMetrics, shellStartedAt time.Time, -) { - // Combine this invocation's subprocess-tree usage (terraform/tofu plus its - // own children) with atmos's own self-usage so both the exec-metadata - // upload and the local display reflect the whole command, not just one - // side of it. Local display is scoped to the sync-upload allowlist - // (terraform plan/apply/deploy) so it stays in lockstep with what Atmos - // Pro actually receives — commands with no subprocess (e.g. `describe - // affected`) keep reporting only atmos's own self-usage, unchanged. - if capturedMetrics != nil { - combined := metricsprocess.Combine(metricsprocess.SelfUsageSoFar(), *capturedMetrics) - combined.WallTime = time.Since(shellStartedAt) - info.ExecMetadataRawMetrics = &combined - - if proexec.IsSyncCommand("atmos terraform " + info.SubCommand) { - // Identify the component/stack in the label itself — in a multi-component - // run (e.g. --all/--affected), this line prints once per component, so a - // bare "Completed" would be ambiguous once scrolled away from its own - // plan/apply output. Plain "component (stack)" prose, not CLI flag syntax - // (e.g. not "-s stack") — this is a status line, not a command to copy/paste. - // Backticks render as inline code in the toast (ui.Info renders markdown; - // see pkg/ui/formatter.go's toastMarkdown), matching this repo's convention - // for identifiers in status messages (e.g. terraform_generate_varfile.go's - // "Generated varfile `%s`"). - label := fmt.Sprintf("Completed `%s` (`%s`)", info.ComponentFromArg, info.Stack) - metricsprocess.DisplaySummary(label, combined, atmosConfig) - } - } -} - -func finishMainTerraformCommand( - atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, err error, uploadStatusFlag bool, -) error { - exitCode := resolveExitCode(err) - // Upload status only when explicitly requested via --upload-status flag. - // Upload failures are logged but never cause the terraform command to fail — - // the exit code should reflect the plan/apply result, not telemetry. - if uploadStatusFlag && shouldUploadStatus(info) { - if uploadErr := uploadCommandStatus(atmosConfig, info, exitCode); uploadErr != nil { - log.Warn("Failed to upload command status to Atmos Pro. The terraform command result is unaffected.", "error", uploadErr) - } - } - - // Apply CI exit code mapping: remap terraform exit codes for CI runners. - // This is independent of upload — it only affects what the caller sees. - if mappedCode := mapCIExitCode(atmosConfig, exitCode); mappedCode == 0 { - return nil - } - - // FR-006e/Decision 36: -detailed-exitcode was added to this invocation solely - // because exec-metadata capture required it (info.ExecMetadataDetailedExitCodeAdded), - // not because the user explicitly passed --upload-status. The global CI-mode remap - // above didn't neutralize this exit code (atmosConfig.CI.Enabled is false, or its - // mapping doesn't cover it), so without this local, call-site-scoped neutralization, - // Atmos's own process exit code for `plan` would silently change from 0 to 2 in CI - // environments that satisfy FR-001 but haven't separately set ci.enabled. This does - // NOT touch atmosConfig.CI.Enabled or mapCIExitCode's own gate — those also govern - // annotations/SARIF/summaries/hooks CI-mode, well outside this fix's scope. - if info.ExecMetadataDetailedExitCodeAdded && exitCode == detailedExitCodeChangesDetected { - return nil - } - - return err -} - -// uploadCommandStatus uploads the command status to Atmos Pro. -func uploadCommandStatus( - atmosConfig *schema.AtmosConfiguration, - info *schema.ConfigAndStacksInfo, - exitCode int, -) error { - client, cerr := pro.NewAtmosProAPIClientFromEnv(atmosConfig) - if cerr != nil { - return cerr - } - gitRepo := &git.DefaultGitRepo{} - return uploadStatus(info, exitCode, client, gitRepo) -} - -// cleanupTerraformFiles removes ephemeral plan and varfiles that Atmos generates. -// Failures are logged at Trace level and not propagated, since cleanup errors should -// not mask the result of the main command. -func cleanupTerraformFiles(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) { - // Only a completed apply/destroy consumes the default plan. Inspection commands - // also pass through this pipeline and must preserve plans saved for later use. - if (info.SubCommand == subcommandApply || info.SubCommand == "destroy") && info.PlanFile == "" { - planFilePath := constructTerraformComponentPlanfilePath(atmosConfig, info) - if err := os.Remove(planFilePath); err != nil && !os.IsNotExist(err) { - log.Trace("Failed to remove plan file during cleanup", "error", err, "file", planFilePath) - } - } - - if info.SubCommand == subcommandApply { - varFilePath := constructTerraformComponentVarfilePath(atmosConfig, info) - if err := os.Remove(varFilePath); err != nil && !os.IsNotExist(err) { - log.Trace("Failed to remove var file during cleanup", "error", err, "file", varFilePath) - } - } - - if info.SubCommand == "test" { - testVarFilePath := constructTerraformComponentTestVarfilePath(atmosConfig, info) - if err := os.Remove(testVarFilePath); err != nil && !os.IsNotExist(err) { - log.Trace("Failed to remove Terraform test var file during cleanup", "error", err, "file", testVarFilePath) - } - } -} - -// invokeShellCommandFunc is the signature used by ExecuteShellCommandWithRetry to delegate -// the actual subprocess invocation back to its caller. The caller provides a closure that -// already binds the command, args, dir, env, etc., and only needs the variadic options -// (used by the retry wrapper to inject stdout/stderr capture writers). -type invokeShellCommandFunc func(opts ...ShellCommandOption) error - -// resetExecMetadataBufs resets the exec-metadata stdout/stderr tee buffers (if -// present) between retry attempts, so the exec-metadata parser only ever sees -// the latest attempt's output rather than every attempt's output concatenated -// together. -func resetExecMetadataBufs(stdoutW, stderrW io.Writer) { - if resettable, ok := stdoutW.(interface{ Reset() }); ok { - resettable.Reset() - } - if resettable, ok := stderrW.(interface{ Reset() }); ok { - resettable.Reset() - } -} - -// retryExecParams bundles the per-invocation shell command inputs shared by the -// helmfile/packer retry-wrapping helpers (executeHelmfileCommandWithRetry, -// executePackerCommandWithRetry), keeping their parameter lists within the argument-limit. -type retryExecParams struct { - allArgsAndFlags []string - componentPath string - envVars []string -} - -// ExecuteShellCommandWithRetry runs invoke exactly once when info.ComponentRetrySection is -// nil or has no Conditions configured (zero behavioural change for non-retry components). -// It is component-type agnostic: terraform, helmfile, packer, and ansible all shell out -// through ExecuteShellCommand and share this same wrapper. -// -// When retry is configured, stdout and stderr are tee'd into a buffer and the buffer is -// matched against the compiled retry conditions after each attempt. Errors whose captured -// output matches at least one condition trigger another attempt with the configured backoff; -// other errors fail fast. This is intentionally pattern-driven so that real failures (e.g., -// `terraform plan` exit-code-2) are never retried unless the user opts in by listing a -// matching condition. -// -// If baseOpts already requests its own stdout/stderr capture (e.g. helmfile capturing output -// for a NodeHooks.After callback), that writer keeps receiving the full output — the retry -// buffer is teed in alongside it via io.MultiWriter, not substituted for it. -// -// `phase` is included in retry log lines so users can see which subprocess invocation is -// being retried (e.g. "init", "workspace-select", "apply", "sync", "build"). -func ExecuteShellCommandWithRetry( - atmosConfig *schema.AtmosConfiguration, - info *schema.ConfigAndStacksInfo, - phase string, - invoke invokeShellCommandFunc, - baseOpts ...ShellCommandOption, -) error { - defer perf.Track(atmosConfig, "exec.ExecuteShellCommandWithRetry")() - - if handled, err := executeShellRetryPolicy(info, phase, invoke, baseOpts...); handled { - return err - } - - cfg := info.ComponentRetrySection - if cfg == nil || len(cfg.Conditions) == 0 { - return invoke(baseOpts...) - } - - patterns, err := retry.CompileConditions(cfg.Conditions) - if err != nil { - return errors.Join(errUtils.ErrInvalidConfig, err) - } - - var buf bytes.Buffer - stdoutWriter, stderrWriter := composeRetryCaptureWriters(baseOpts, &buf) - captureOpts := append([]ShellCommandOption{}, baseOpts...) - captureOpts = append(captureOpts, WithStdoutCapture(stdoutWriter), WithStderrCapture(stderrWriter)) - - execMetadataStdout, execMetadataStderr := execMetadataOutputCaptureFromOpts(baseOpts...) - - attempt := 0 - return retry.WithPredicate( - context.Background(), - cfg, - func() error { - attempt++ - buf.Reset() - resetExecMetadataBufs(execMetadataStdout, execMetadataStderr) - if attempt > 1 { - log.Warn( - "Retrying subprocess after recoverable error", - "phase", phase, - logFieldComponent, info.ComponentFromArg, - "stack", info.StackFromArg, - "attempt", attempt, - ) - } - return invoke(captureOpts...) - }, - func(invokeErr error) bool { - if invokeErr == nil { - return false - } - return retry.MatchesAny(patterns, buf.String()) - }, - ) -} - -// composeRetryCaptureWriters returns the stdout/stderr writers ExecuteShellCommandWithRetry -// should inject: buf alone when baseOpts requests no capture of its own, or an -// io.MultiWriter teeing into both buf and the caller's writer when it does — so a caller's -// own capture (e.g. helmfile's NodeHooks.After output) keeps receiving output instead of -// being silently replaced by the retry buffer. -// -// Buf is wrapped in a syncWriter (see terraform_streaming_ui.go) because the returned -// stdout/stderr writers can end up being invoked concurrently: when the caller streams through -// the TUI (executeStreamingOrShell -> pkg/terraform/ui's newInitCommand), stdout and stderr are -// set to two distinct io.Writer values even though both ultimately tee into this same buf, and -// os/exec.Cmd copies each stream in its own goroutine whenever Stdout and Stderr aren't the -// exact same Writer value — so without synchronization, concurrent Write calls into buf would -// race (bytes.Buffer is not safe for concurrent use). -func composeRetryCaptureWriters(baseOpts []ShellCommandOption, buf *bytes.Buffer) (stdout, stderr io.Writer) { - var probe shellCommandConfig - for _, opt := range baseOpts { - opt(&probe) - } - - safeBuf := &syncWriter{w: buf} - - stdout = safeBuf - if probe.stdoutCapture != nil { - stdout = io.MultiWriter(probe.stdoutCapture, safeBuf) - } - stderr = safeBuf - if probe.stderrCapture != nil { - stderr = io.MultiWriter(probe.stderrCapture, safeBuf) - } - return stdout, stderr -} diff --git a/internal/exec/terraform_generate_backend_test.go b/internal/exec/terraform_generate_backend_test.go index 69022d5b0b6..6cebbac63ce 100644 --- a/internal/exec/terraform_generate_backend_test.go +++ b/internal/exec/terraform_generate_backend_test.go @@ -1,224 +1,17 @@ package exec import ( - "os" "path/filepath" "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - errUtils "github.com/cloudposse/atmos/errors" - cfg "github.com/cloudposse/atmos/pkg/config" - provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir" "github.com/cloudposse/atmos/pkg/schema" ) -func TestValidateBackendConfig(t *testing.T) { - t.Parallel() - - tests := []struct { - name string - info *schema.ConfigAndStacksInfo - expectedErr error - }{ - { - name: "Valid backend config", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: "s3", - ComponentBackendSection: map[string]any{"bucket": "test-bucket"}, - }, - expectedErr: nil, - }, - { - name: "Missing backend type", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: "", - ComponentBackendSection: map[string]any{"bucket": "test-bucket"}, - }, - expectedErr: errUtils.ErrMissingTerraformBackendType, - }, - { - name: "Missing backend section", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: "s3", - ComponentBackendSection: nil, - }, - expectedErr: errUtils.ErrMissingTerraformBackendConfig, - }, - { - name: "Both missing", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: "", - ComponentBackendSection: nil, - }, - expectedErr: errUtils.ErrMissingTerraformBackendType, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - t.Parallel() - err := validateBackendConfig(tt.info) - if tt.expectedErr != nil { - assert.ErrorIs(t, err, tt.expectedErr) - } else { - assert.NoError(t, err) - } - }) - } -} - -func TestValidateBackendTypeRequirements(t *testing.T) { - t.Parallel() - - tests := []struct { - name string - info *schema.ConfigAndStacksInfo - expectedErr error - }{ - { - name: "S3 backend with workspace_key_prefix", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: cfg.BackendTypeS3, - ComponentBackendSection: map[string]any{ - "workspace_key_prefix": "terraform-state", - }, - }, - expectedErr: nil, - }, - { - name: "S3 backend without workspace_key_prefix", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: cfg.BackendTypeS3, - ComponentBackendSection: map[string]any{}, - }, - expectedErr: errUtils.ErrMissingTerraformWorkspaceKeyPrefix, - }, - { - name: "GCS backend with bucket", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: cfg.BackendTypeGCS, - ComponentBackendSection: map[string]any{ - "bucket": "test-bucket", - }, - }, - expectedErr: nil, - }, - { - name: "GCS backend without bucket", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: cfg.BackendTypeGCS, - ComponentBackendSection: map[string]any{}, - }, - expectedErr: errUtils.ErrGCSBucketRequired, - }, - { - name: "Azure backend - no specific requirements", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: cfg.BackendTypeAzurerm, - ComponentBackendSection: map[string]any{}, - }, - expectedErr: nil, - }, - { - name: "Local backend - no specific requirements", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: "local", - ComponentBackendSection: map[string]any{}, - }, - expectedErr: nil, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - t.Parallel() - err := validateBackendTypeRequirements(tt.info) - if tt.expectedErr != nil { - assert.ErrorIs(t, err, tt.expectedErr) - } else { - assert.NoError(t, err) - } - }) - } -} - // TestExecuteTerraformGenerateBackendCmd_Deprecated tests the deprecated command returns an error. -func TestExecuteTerraformGenerateBackendCmd_Deprecated(t *testing.T) { - t.Parallel() - - err := ExecuteTerraformGenerateBackendCmd(nil, nil) - assert.Error(t, err) - assert.ErrorIs(t, err, errUtils.ErrDeprecatedCmdNotCallable) -} // TestWriteBackendConfigFile tests the writeBackendConfigFile function across dry-run, normal write, and workdir cases. -func TestWriteBackendConfigFile(t *testing.T) { - t.Parallel() - - tempDir := t.TempDir() - componentDir := filepath.Join(tempDir, "components", "terraform", "vpc") - workDir := filepath.Join(tempDir, "workdir", "vpc") - require.NoError(t, os.MkdirAll(componentDir, 0o755)) - require.NoError(t, os.MkdirAll(workDir, 0o755)) - - atmosConfig := &schema.AtmosConfiguration{ - BasePath: tempDir, - Components: schema.Components{ - Terraform: schema.Terraform{ - BasePath: filepath.Join("components", "terraform"), - }, - }, - } - - tests := []struct { - name string - info *schema.ConfigAndStacksInfo - config map[string]any - wantPath string - wantContains string - }{ - { - name: "dry-run skips writing", - info: &schema.ConfigAndStacksInfo{FinalComponent: "vpc", DryRun: true}, - config: map[string]any{"terraform": map[string]any{"backend": map[string]any{"s3": map[string]any{"bucket": "test-bucket"}}}}, - }, - { - name: "writes to component dir", - info: &schema.ConfigAndStacksInfo{FinalComponent: "vpc", ComponentSection: map[string]any{}}, - config: map[string]any{"terraform": map[string]any{"backend": map[string]any{"s3": map[string]any{"bucket": "my-state-bucket", "key": "vpc/terraform.tfstate", "region": "us-east-1"}}}}, - wantPath: filepath.Join(componentDir, "backend.tf.json"), - wantContains: "my-state-bucket", - }, - { - name: "writes to workdir path", - info: &schema.ConfigAndStacksInfo{ - FinalComponent: "vpc", - ComponentSection: map[string]any{provWorkdir.WorkdirPathKey: workDir}, - }, - config: map[string]any{"terraform": map[string]any{"backend": map[string]any{"s3": map[string]any{"bucket": "test"}}}}, - wantPath: filepath.Join(workDir, "backend.tf.json"), - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - t.Parallel() - err := writeBackendConfigFile(atmosConfig, tt.info, tt.config) - assert.NoError(t, err) - - if tt.wantPath != "" { - assert.FileExists(t, tt.wantPath) - if tt.wantContains != "" { - content, readErr := os.ReadFile(tt.wantPath) - require.NoError(t, readErr) - assert.Contains(t, string(content), tt.wantContains) - } - } - }) - } -} // TestExecuteGenerateBackend_ProcessStacksFails tests that ExecuteGenerateBackend returns an error // when ProcessStacks fails due to missing stack config files. @@ -244,56 +37,3 @@ func TestExecuteGenerateBackend_ProcessStacksFails(t *testing.T) { err := ExecuteGenerateBackend(opts, atmosConfig) assert.Error(t, err, "should fail when stack config is not set up") } - -func TestValidateBackendTypeRequirementsTypeAssertions(t *testing.T) { - t.Parallel() - - tests := []struct { - name string - info *schema.ConfigAndStacksInfo - expectedErr error - }{ - { - name: "S3 backend with workspace_key_prefix as int (wrong type)", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: cfg.BackendTypeS3, - ComponentBackendSection: map[string]any{ - "workspace_key_prefix": 123, // Wrong type, should be string. - }, - }, - expectedErr: errUtils.ErrMissingTerraformWorkspaceKeyPrefix, - }, - { - name: "GCS backend with bucket as int (wrong type)", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: cfg.BackendTypeGCS, - ComponentBackendSection: map[string]any{ - "bucket": 123, // Wrong type, should be string. - }, - }, - expectedErr: errUtils.ErrGCSBucketRequired, - }, - { - name: "S3 backend with empty string workspace_key_prefix", - info: &schema.ConfigAndStacksInfo{ - ComponentBackendType: cfg.BackendTypeS3, - ComponentBackendSection: map[string]any{ - "workspace_key_prefix": "", - }, - }, - expectedErr: nil, // Empty string is still a string type. - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - t.Parallel() - err := validateBackendTypeRequirements(tt.info) - if tt.expectedErr != nil { - assert.ErrorIs(t, err, tt.expectedErr) - } else { - assert.NoError(t, err) - } - }) - } -} diff --git a/internal/exec/terraform_generate_varfile_test.go b/internal/exec/terraform_generate_varfile_test.go index 33e6e4c8a12..ef067d0ccaf 100644 --- a/internal/exec/terraform_generate_varfile_test.go +++ b/internal/exec/terraform_generate_varfile_test.go @@ -11,227 +11,28 @@ import ( "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" - errUtils "github.com/cloudposse/atmos/errors" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" cfg "github.com/cloudposse/atmos/pkg/config" iolib "github.com/cloudposse/atmos/pkg/io" - provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir" "github.com/cloudposse/atmos/pkg/schema" ) // TestEnsureTerraformComponentExists_ExistingComponent tests that existing components pass validation. -func TestEnsureTerraformComponentExists_ExistingComponent(t *testing.T) { - // Create a temporary directory structure. - tempDir := t.TempDir() - componentPath := filepath.Join(tempDir, "components", "terraform", "vpc") - require.NoError(t, os.MkdirAll(componentPath, 0o755)) - - // Create a minimal main.tf to make it a valid component. - mainTF := filepath.Join(componentPath, "main.tf") - require.NoError(t, os.WriteFile(mainTF, []byte("# vpc component\n"), 0o644)) - - atmosConfig := &schema.AtmosConfiguration{ - BasePath: tempDir, - Components: schema.Components{ - Terraform: schema.Terraform{ - BasePath: filepath.Join("components", "terraform"), - }, - }, - } - - info := &schema.ConfigAndStacksInfo{ - ComponentFromArg: "vpc", - FinalComponent: "vpc", - ComponentFolderPrefix: "", - ComponentSection: map[string]any{}, - } - - err := ensureTerraformComponentExists(atmosConfig, info) - assert.NoError(t, err, "existing component should not return error") -} // TestEnsureTerraformComponentExists_MissingComponentNoSource tests error for missing component without source. -func TestEnsureTerraformComponentExists_MissingComponentNoSource(t *testing.T) { - tempDir := t.TempDir() - - atmosConfig := &schema.AtmosConfiguration{ - BasePath: tempDir, - Components: schema.Components{ - Terraform: schema.Terraform{ - BasePath: filepath.Join("components", "terraform"), - }, - }, - } - - info := &schema.ConfigAndStacksInfo{ - ComponentFromArg: "nonexistent", - FinalComponent: "nonexistent", - ComponentFolderPrefix: "", - ComponentSection: map[string]any{}, - } - - err := ensureTerraformComponentExists(atmosConfig, info) - assert.Error(t, err, "missing component without source should return error") - assert.Contains(t, err.Error(), "nonexistent") -} // TestEnsureTerraformComponentExists_WorkdirPathSet tests that workdir path set by provisioner is accepted. -func TestEnsureTerraformComponentExists_WorkdirPathSet(t *testing.T) { - tempDir := t.TempDir() - - // Create workdir path. - workdirPath := filepath.Join(tempDir, "workdir", "vpc") - require.NoError(t, os.MkdirAll(workdirPath, 0o755)) - - atmosConfig := &schema.AtmosConfiguration{ - BasePath: tempDir, - Components: schema.Components{ - Terraform: schema.Terraform{ - BasePath: filepath.Join("components", "terraform"), - }, - }, - } - - info := &schema.ConfigAndStacksInfo{ - ComponentFromArg: "vpc", - FinalComponent: "vpc", - ComponentFolderPrefix: "", - ComponentSection: map[string]any{ - provWorkdir.WorkdirPathKey: workdirPath, - }, - } - - // Even though the original component path doesn't exist, the workdir path is set. - err := ensureTerraformComponentExists(atmosConfig, info) - assert.NoError(t, err, "component with workdir path set should pass") -} // TestEnsureTerraformComponentExists_WithFolderPrefix tests component resolution with a folder prefix. -func TestEnsureTerraformComponentExists_WithFolderPrefix(t *testing.T) { - tempDir := t.TempDir() - componentPath := filepath.Join(tempDir, "components", "terraform", "myprefix", "vpc") - require.NoError(t, os.MkdirAll(componentPath, 0o755)) - require.NoError(t, os.WriteFile(filepath.Join(componentPath, "main.tf"), []byte("# vpc\n"), 0o644)) - - atmosConfig := &schema.AtmosConfiguration{ - BasePath: tempDir, - Components: schema.Components{ - Terraform: schema.Terraform{ - BasePath: filepath.Join("components", "terraform"), - }, - }, - } - - info := &schema.ConfigAndStacksInfo{ - ComponentFromArg: "vpc", - FinalComponent: "vpc", - ComponentFolderPrefix: "myprefix", - ComponentSection: map[string]any{}, - } - - err := ensureTerraformComponentExists(atmosConfig, info) - assert.NoError(t, err, "component with folder prefix should be found") -} // TestEnsureTerraformComponentExists_ReturnsErrorWithBasePath tests the error message contains base path info. -func TestEnsureTerraformComponentExists_ReturnsErrorWithBasePath(t *testing.T) { - tempDir := t.TempDir() - - atmosConfig := &schema.AtmosConfiguration{ - BasePath: tempDir, - Components: schema.Components{ - Terraform: schema.Terraform{ - BasePath: filepath.Join("components", "terraform"), - }, - }, - } - - info := &schema.ConfigAndStacksInfo{ - ComponentFromArg: "missing-comp", - FinalComponent: "missing-comp", - ComponentFolderPrefix: "", - ComponentSection: map[string]any{}, - } - - err := ensureTerraformComponentExists(atmosConfig, info) - assert.Error(t, err) - assert.Contains(t, err.Error(), "missing-comp") - assert.Contains(t, err.Error(), filepath.Join("components", "terraform")) -} // TestExecuteTerraformGenerateVarfileCmd_Deprecated tests the deprecated command returns an error. -func TestExecuteTerraformGenerateVarfileCmd_Deprecated(t *testing.T) { - err := ExecuteTerraformGenerateVarfileCmd(nil, nil) - assert.Error(t, err) - assert.ErrorIs(t, err, errUtils.ErrDeprecatedCmdNotCallable) -} // TestEnsureTerraformComponentExists_JITProvisionFails tests the error wrapping when JIT provisioning fails. -func TestEnsureTerraformComponentExists_JITProvisionFails(t *testing.T) { - tempDir := t.TempDir() - - atmosConfig := &schema.AtmosConfiguration{ - BasePath: tempDir, - Components: schema.Components{ - Terraform: schema.Terraform{ - BasePath: filepath.Join("components", "terraform"), - }, - }, - } - - // Component doesn't exist and has a source with an unreachable URI — JIT provision will fail. - info := &schema.ConfigAndStacksInfo{ - ComponentFromArg: "jit-fail", - FinalComponent: "jit-fail", - ComponentFolderPrefix: "", - ComponentSection: map[string]any{ - "source": map[string]any{ - "uri": "file:///nonexistent/path/to/source", - }, - }, - } - - err := ensureTerraformComponentExists(atmosConfig, info) - assert.Error(t, err) - assert.ErrorIs(t, err, errUtils.ErrInvalidTerraformComponent) - assert.Contains(t, err.Error(), "auto-provision") -} // TestEnsureTerraformComponentExists_PostJITComponentAppears tests the path where JIT provisioning // succeeds (no source, returns nil) and the component directory appears at the standard path. -func TestEnsureTerraformComponentExists_PostJITComponentAppears(t *testing.T) { - tempDir := t.TempDir() - - // Do NOT create the component directory initially. - componentPath := filepath.Join(tempDir, "components", "terraform", "lazy-vpc") - - atmosConfig := &schema.AtmosConfiguration{ - BasePath: tempDir, - Components: schema.Components{ - Terraform: schema.Terraform{ - BasePath: filepath.Join("components", "terraform"), - }, - }, - } - - info := &schema.ConfigAndStacksInfo{ - ComponentFromArg: "lazy-vpc", - FinalComponent: "lazy-vpc", - ComponentFolderPrefix: "", - ComponentSection: map[string]any{}, - } - - // First call: component doesn't exist, no source, no workdir → error. - err := ensureTerraformComponentExists(atmosConfig, info) - assert.Error(t, err, "should fail when component doesn't exist") - - // Now create the directory to simulate JIT provisioning having put it there. - require.NoError(t, os.MkdirAll(componentPath, 0o755)) - require.NoError(t, os.WriteFile(filepath.Join(componentPath, "main.tf"), []byte("# lazy-vpc\n"), 0o644)) - - // Second call: component now exists at the standard path. - err = ensureTerraformComponentExists(atmosConfig, info) - assert.NoError(t, err, "should pass when component exists") -} // TestExecuteGenerateVarfile_ProcessStacksFails tests that ExecuteGenerateVarfile returns an error // when ProcessStacks fails due to missing stack config files. @@ -360,35 +161,6 @@ func TestExecuteGenerateBackend_Integration(t *testing.T) { assert.Contains(t, string(content), "nonprod-tfstate") } -func TestVarfileVarsToWrite(t *testing.T) { - const secret = "genvarfile-secret-aa11bb22" - iolib.RegisterSecret(secret) - - newInfo := func() *schema.ConfigAndStacksInfo { - return &schema.ConfigAndStacksInfo{ - ComponentVarsSection: map[string]any{ - "db_password": secret, - "region": "us-east-1-genvarfile", - }, - } - } - - t.Run("default omits secrets", func(t *testing.T) { - info := newInfo() - got := varfileVarsToWrite(info, false, "test.tfvars.json") - _, hasSecret := got["db_password"] - assert.False(t, hasSecret, "secret var must be omitted from the varfile by default") - assert.Equal(t, "us-east-1-genvarfile", got["region"], "non-secret var must remain") - }) - - t.Run("with-secrets includes secrets", func(t *testing.T) { - info := newInfo() - got := varfileVarsToWrite(info, true, "test.tfvars.json") - assert.Equal(t, secret, got["db_password"], "secret var must be written with --with-secrets") - assert.Equal(t, "us-east-1-genvarfile", got["region"]) - }) -} - // TestExecuteGenerateVarfile_SensitiveStateValueNeverLeaks reproduces the sensitive // YAML-function regression end-to-end. The explicit YAML tag must survive decoding, // the state lookup must be evaluated, and the sensitive consumer input must use @@ -454,8 +226,8 @@ func TestExecuteGenerateVarfile_SensitiveStateValueNeverLeaks(t *testing.T) { ComponentType: cfg.TerraformComponentType, }, true, true, true, nil, nil) require.NoError(t, err) - computeTerraformSecretVarKeys(&info) - env, err := secretVarEnv(&info) + terraformcomponent.ComputeTerraformSecretVarKeys(&info) + env, err := terraformcomponent.SecretVarEnv(&info) require.NoError(t, err) joinedEnv := strings.Join(env, "\n") assert.Contains(t, joinedEnv, "TF_VAR_sensitive_value="+resolved) diff --git a/internal/exec/terraform_lookup_helpers_test.go b/internal/exec/terraform_lookup_helpers_test.go index b4c4fd1a70b..8887bc0d47d 100644 --- a/internal/exec/terraform_lookup_helpers_test.go +++ b/internal/exec/terraform_lookup_helpers_test.go @@ -8,6 +8,7 @@ import ( "github.com/stretchr/testify/require" errUtils "github.com/cloudposse/atmos/errors" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" "github.com/cloudposse/atmos/pkg/schema" ) @@ -17,7 +18,7 @@ const malformedYqDefault = `.missing // (` func TestTerraformLookupErrorHandlersWithMalformedYqDefault(t *testing.T) { atmosConfig := &schema.AtmosConfiguration{} - lookup := &terraformStateLookup{yamlFunc: "!terraform.output vpc " + malformedYqDefault, stack: "dev", component: "vpc", output: malformedYqDefault} + lookup := &terraformcomponent.TerraformStateLookup{YamlFunc: "!terraform.output vpc " + malformedYqDefault, Stack: "dev", Component: "vpc", Output: malformedYqDefault} notProvisioned := fmt.Errorf("not yet: %w", errUtils.ErrTerraformStateNotProvisioned) t.Run("output error keeps the original recoverable error", func(t *testing.T) { @@ -41,23 +42,15 @@ func TestTerraformLookupErrorHandlersWithMalformedYqDefault(t *testing.T) { } func TestResolveTerraformOutputWithMocksMalformedExpression(t *testing.T) { - lookup := &terraformStateLookup{stack: "dev", component: "vpc", output: ".vpc_id["} + lookup := &terraformcomponent.TerraformStateLookup{Stack: "dev", Component: "vpc", Output: ".vpc_id["} - value, err := resolveTerraformOutputWithMocks(&schema.AtmosConfiguration{}, lookup, map[string]any{"vpc_id": "vpc-mock"}, nil, nil) + value, err := terraformExecutor.ResolveTerraformOutputWithMocks(&schema.AtmosConfiguration{}, lookup, map[string]any{"vpc_id": "vpc-mock"}, nil, nil) require.Error(t, err) assert.Contains(t, err.Error(), "against component mocks") assert.Nil(t, value) } -func TestTerraformMocksModeGuards(t *testing.T) { - assert.Equal(t, schema.TerraformMocksModeFallback, terraformMocksMode(nil)) - - invalid := &schema.AtmosConfiguration{} - invalid.Components.Terraform.Mocks.Mode = "bogus" - require.NoError(t, validateTerraformMocksMode(invalid, nil), "a nil stack info means mocks are off") -} - func TestTerraformLookupAuth(t *testing.T) { atmosConfig := &schema.AtmosConfiguration{} @@ -68,9 +61,9 @@ func TestTerraformLookupAuth(t *testing.T) { // AuthDisabled without an AuthManager still propagates downstream through the wrapper. stackInfo := &schema.ConfigAndStacksInfo{AuthDisabled: true} _, authManager = terraformLookupAuth(atmosConfig, stackInfo) - wrapper, ok := authManager.(*authContextWrapper) + wrapper, ok := authManager.(*terraformcomponent.AuthContextWrapper) require.True(t, ok) - assert.Same(t, stackInfo, wrapper.stackInfo) + assert.Same(t, stackInfo, wrapper.StackInfo) // Auth enabled with no manager passes nothing through. _, authManager = terraformLookupAuth(atmosConfig, &schema.ConfigAndStacksInfo{}) diff --git a/internal/exec/terraform_mocks_test.go b/internal/exec/terraform_mocks_test.go index b87b591b31b..fd2281e90c9 100644 --- a/internal/exec/terraform_mocks_test.go +++ b/internal/exec/terraform_mocks_test.go @@ -13,6 +13,7 @@ import ( errUtils "github.com/cloudposse/atmos/errors" tb "github.com/cloudposse/atmos/internal/terraform_backend" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/schema" tfoutput "github.com/cloudposse/atmos/pkg/terraform/output" @@ -173,7 +174,7 @@ type mocksFallbackCase struct { // input is the YAML function expression; stack is always dev. input string // lookupExpr is the output expression the real getter must receive. Empty means the whole - // output map is requested (terraformAllOutputsExpression), which is what fallback mode does + // output map is requested (terraformcomponent.TerraformAllOutputsExpression), which is what fallback mode does // for a component that declares mocks. lookupExpr string // lookupValue / lookupErr are what the real getter returns; skipLookup means the getter must @@ -332,7 +333,7 @@ func mocksFallbackCases(function string) []mocksFallbackCase { func (c *mocksFallbackCase) expectedLookupExpr() string { if c.lookupExpr == "" { - return terraformAllOutputsExpression + return terraformcomponent.TerraformAllOutputsExpression } return c.lookupExpr } @@ -397,7 +398,7 @@ func TestTerraformComponentMocksFallbackHintsAtAlwaysMode(t *testing.T) { t.Run("terraform.state", func(t *testing.T) { atmosConfig := newMocksFixtureConfig(t, schema.TerraformMocksModeFallback) stateMock, _ := installMockGetters(t) - expectStateLookup(stateMock, terraformAllOutputsExpression, nil, errMocksAccessDenied) + expectStateLookup(stateMock, terraformcomponent.TerraformAllOutputsExpression, nil, errMocksAccessDenied) _, err := processTagTerraformState(atmosConfig, "!terraform.state vpc vpc_id", "dev", &schema.ConfigAndStacksInfo{UseMocks: true}) @@ -408,7 +409,7 @@ func TestTerraformComponentMocksFallbackHintsAtAlwaysMode(t *testing.T) { t.Run("terraform.output", func(t *testing.T) { atmosConfig := newMocksFixtureConfig(t, schema.TerraformMocksModeFallback) _, outputMock := installMockGetters(t) - expectOutputLookup(outputMock, terraformAllOutputsExpression, nil, false, errMocksAccessDenied) + expectOutputLookup(outputMock, terraformcomponent.TerraformAllOutputsExpression, nil, false, errMocksAccessDenied) _, err := processTagTerraformOutput(atmosConfig, "!terraform.output vpc vpc_id", "dev", &schema.ConfigAndStacksInfo{UseMocks: true}) @@ -427,8 +428,8 @@ func TestTerraformOutputComponentMocksFallbackOnMissingOutput(t *testing.T) { lookupExpr string want any }{ - {name: "mock resolves a missing output map", input: "!terraform.output vpc vpc_id", useMocks: true, lookupExpr: terraformAllOutputsExpression, want: "vpc-local"}, - {name: "no mock for the output returns nil like before", input: "!terraform.output vpc missing", useMocks: true, lookupExpr: terraformAllOutputsExpression, want: nil}, + {name: "mock resolves a missing output map", input: "!terraform.output vpc vpc_id", useMocks: true, lookupExpr: terraformcomponent.TerraformAllOutputsExpression, want: "vpc-local"}, + {name: "no mock for the output returns nil like before", input: "!terraform.output vpc missing", useMocks: true, lookupExpr: terraformcomponent.TerraformAllOutputsExpression, want: nil}, {name: "no mocks declared and a YQ default uses the default", input: `!terraform.output app '.missing // "yq-fallback"'`, useMocks: true, lookupExpr: `.missing // "yq-fallback"`, want: "yq-fallback"}, {name: "mocks off keeps returning nil", input: "!terraform.output vpc vpc_id", useMocks: false, lookupExpr: "vpc_id", want: nil}, } @@ -451,7 +452,7 @@ func TestTerraformOutputComponentMocksFallbackOnMissingOutput(t *testing.T) { func TestTerraformOutputComponentMocksWrapsRecoverableError(t *testing.T) { atmosConfig := newMocksFixtureConfig(t, schema.TerraformMocksModeFallback) _, outputMock := installMockGetters(t) - expectOutputLookup(outputMock, terraformAllOutputsExpression, nil, false, fmt.Errorf("not yet: %w", errUtils.ErrTerraformStateNotProvisioned)) + expectOutputLookup(outputMock, terraformcomponent.TerraformAllOutputsExpression, nil, false, fmt.Errorf("not yet: %w", errUtils.ErrTerraformStateNotProvisioned)) got, err := processTagTerraformOutput(atmosConfig, "!terraform.output vpc missing", "dev", &schema.ConfigAndStacksInfo{UseMocks: true}) @@ -467,7 +468,7 @@ func TestTerraformStateComponentMocksDoesNotMutateInputs(t *testing.T) { atmosConfig := newMocksFixtureConfig(t, schema.TerraformMocksModeFallback) stateMock, _ := installMockGetters(t) realOutputs := map[string]any{"vpc_id": "vpc-real", "extra": map[string]any{"k": "v"}} - expectStateLookup(stateMock, terraformAllOutputsExpression, realOutputs, nil) + expectStateLookup(stateMock, terraformcomponent.TerraformAllOutputsExpression, realOutputs, nil) got, err := processTagTerraformState(atmosConfig, "!terraform.state vpc private_subnet_ids", "dev", &schema.ConfigAndStacksInfo{UseMocks: true}) @@ -479,9 +480,9 @@ func TestTerraformStateComponentMocksDoesNotMutateInputs(t *testing.T) { t.Run("merge helper isolates the result from both sources", func(t *testing.T) { mocks := map[string]any{"vpc_id": "vpc-local", "mock_only": "m"} realOutputs := map[string]any{"vpc_id": "vpc-real", "real_only": "r"} - lookup := &terraformStateLookup{stack: "dev", component: "vpc", output: "vpc_id"} + lookup := &terraformcomponent.TerraformStateLookup{Stack: "dev", Component: "vpc", Output: "vpc_id"} - got, err := resolveTerraformOutputWithMocks(&schema.AtmosConfiguration{}, lookup, mocks, realOutputs, nil) + got, err := terraformExecutor.ResolveTerraformOutputWithMocks(&schema.AtmosConfiguration{}, lookup, mocks, realOutputs, nil) require.NoError(t, err) assert.Equal(t, "vpc-real", got) @@ -491,7 +492,7 @@ func TestTerraformStateComponentMocksDoesNotMutateInputs(t *testing.T) { // Mutating the sources afterwards must not matter to a later evaluation either. mocks["vpc_id"] = "changed" realOutputs["vpc_id"] = "changed" - got, err = resolveTerraformOutputWithMocks(&schema.AtmosConfiguration{}, lookup, map[string]any{"vpc_id": "vpc-local"}, map[string]any{}, nil) + got, err = terraformExecutor.ResolveTerraformOutputWithMocks(&schema.AtmosConfiguration{}, lookup, map[string]any{"vpc_id": "vpc-local"}, map[string]any{}, nil) require.NoError(t, err) assert.Equal(t, "vpc-local", got) }) @@ -508,127 +509,22 @@ func TestTerraformAllOutputsExpression(t *testing.T) { } t.Run("backend variable returns the whole map", func(t *testing.T) { - got, err := tb.GetTerraformBackendVariable(atmosConfig, outputs, terraformAllOutputsExpression) + got, err := tb.GetTerraformBackendVariable(atmosConfig, outputs, terraformcomponent.TerraformAllOutputsExpression) require.NoError(t, err) assert.Equal(t, outputs, got) }) t.Run("output layer reports the whole map as existing", func(t *testing.T) { - got, exists, err := tfoutput.GetStaticRemoteStateOutput(atmosConfig, "vpc", "dev", outputs, terraformAllOutputsExpression) + got, exists, err := tfoutput.GetStaticRemoteStateOutput(atmosConfig, "vpc", "dev", outputs, terraformcomponent.TerraformAllOutputsExpression) require.NoError(t, err) assert.True(t, exists) assert.Equal(t, outputs, got) }) t.Run("an empty map still reports as existing", func(t *testing.T) { - got, exists, err := tfoutput.GetStaticRemoteStateOutput(atmosConfig, "vpc", "dev", map[string]any{}, terraformAllOutputsExpression) + got, exists, err := tfoutput.GetStaticRemoteStateOutput(atmosConfig, "vpc", "dev", map[string]any{}, terraformcomponent.TerraformAllOutputsExpression) require.NoError(t, err) assert.True(t, exists) assert.Empty(t, got) }) } - -func TestMergeRealOverMocks(t *testing.T) { - mocks := map[string]any{ - "vpc_id": "vpc-mock", - "network": map[string]any{"cidr": "10.0.0.0/16", "tags": map[string]any{"env": "mock", "team": "mock"}}, - "subnets": []any{"mock-a", "mock-b"}, - "only": "mock-only", - } - real := map[string]any{ - "vpc_id": "vpc-real", - "network": map[string]any{"name": "real", "tags": map[string]any{"env": "real"}}, - "subnets": []any{"real-a"}, - "nulled": nil, - } - - merged := mergeRealOverMocks(mocks, real) - - assert.Equal(t, map[string]any{ - "vpc_id": "vpc-real", - "network": map[string]any{"name": "real", "cidr": "10.0.0.0/16", "tags": map[string]any{"env": "real", "team": "mock"}}, - "subnets": []any{"real-a"}, - "only": "mock-only", - "nulled": nil, - }, merged) - - // Neither input is mutated by the merge, and the result does not alias their nested maps. - assert.Equal(t, map[string]any{"cidr": "10.0.0.0/16", "tags": map[string]any{"env": "mock", "team": "mock"}}, mocks["network"]) - assert.Equal(t, map[string]any{"name": "real", "tags": map[string]any{"env": "real"}}, real["network"]) - merged["network"].(map[string]any)["cidr"] = "changed" - assert.Equal(t, "10.0.0.0/16", mocks["network"].(map[string]any)["cidr"]) -} - -func TestResolvedFromMocks(t *testing.T) { - mocks := map[string]any{"vpc_id": "vpc-mock", "network": map[string]any{"cidr": "10.0.0.0/16"}} - real := map[string]any{"vpc_id": "vpc-real", "network": map[string]any{"name": "real"}} - tests := []struct { - output string - want bool - }{ - {output: "vpc_id", want: false}, - {output: ".network.cidr", want: true}, - {output: ".network.name", want: false}, - {output: "missing", want: false}, - {output: `.network.cidr // "x"`, want: false}, - } - for _, tt := range tests { - t.Run(tt.output, func(t *testing.T) { - assert.Equal(t, tt.want, resolvedFromMocks(mocks, real, tt.output)) - }) - } -} - -func TestMockPathExists(t *testing.T) { - mocks := map[string]any{ - "vpc_id": "vpc-1", - "nullable": nil, - "network": map[string]any{"cidr": "10.0.0.0/16"}, - } - tests := []struct { - output string - wantExists bool - wantDecidable bool - wantOutputFlag bool - }{ - {output: "vpc_id", wantExists: true, wantDecidable: true, wantOutputFlag: true}, - {output: "nullable", wantExists: true, wantDecidable: true, wantOutputFlag: true}, - {output: "missing", wantExists: false, wantDecidable: true, wantOutputFlag: false}, - {output: ".network.cidr", wantExists: true, wantDecidable: true, wantOutputFlag: true}, - {output: ".network.missing", wantExists: false, wantDecidable: true, wantOutputFlag: false}, - {output: ".vpc_id.deeper", wantExists: false, wantDecidable: true, wantOutputFlag: false}, - {output: `.missing // "x"`, wantExists: false, wantDecidable: false, wantOutputFlag: true}, - } - for _, tt := range tests { - t.Run(tt.output, func(t *testing.T) { - exists, decidable := mockPathExists(mocks, tt.output) - assert.Equal(t, tt.wantExists, exists) - assert.Equal(t, tt.wantDecidable, decidable) - assert.Equal(t, tt.wantOutputFlag, mockOutputExists(mocks, tt.output)) - }) - } -} - -// TestResolveTerraformMockOutputInvalidMode verifies that an unknown -// components.terraform.mocks.mode (only reachable through atmos.yaml, since the -// env var and flag paths validate it) fails loudly under --use-mocks instead of -// silently matching neither mode, and is ignored when mocks are off. -func TestResolveTerraformMockOutputInvalidMode(t *testing.T) { - atmosConfig := &schema.AtmosConfiguration{} - atmosConfig.Components.Terraform.Mocks.Mode = schema.TerraformMocksMode("bogus") - - t.Run("use-mocks on errors", func(t *testing.T) { - value, handled, err := resolveTerraformMockOutput(atmosConfig, &schema.ConfigAndStacksInfo{UseMocks: true}, "dev", "vpc", "vpc_id") - require.ErrorIs(t, err, errUtils.ErrInvalidMocksMode) - assert.True(t, handled) - assert.Nil(t, value) - assert.Contains(t, err.Error(), `"bogus"`) - }) - - t.Run("use-mocks off ignores the mode", func(t *testing.T) { - value, handled, err := resolveTerraformMockOutput(atmosConfig, &schema.ConfigAndStacksInfo{UseMocks: false}, "dev", "vpc", "vpc_id") - require.NoError(t, err) - assert.False(t, handled) - assert.Nil(t, value) - }) -} diff --git a/internal/exec/terraform_output_deferred_auth_test.go b/internal/exec/terraform_output_deferred_auth_test.go index 821c1eca714..9d9ec91cf8d 100644 --- a/internal/exec/terraform_output_deferred_auth_test.go +++ b/internal/exec/terraform_output_deferred_auth_test.go @@ -49,7 +49,7 @@ func TestDeferredOutputDoesNotReuseCallerAuthContext(t *testing.T) { tfoutput.SetDefaultExecutor(tfoutput.NewExecutor(&componentDescriberAdapter{}, tfoutput.WithBackendGenerator(backend))) t.Cleanup(func() { tfoutput.SetDefaultExecutor(executor) }) - getter := &defaultOutputGetter{} + getter := terraformExecutor.OutputGetter() _, _, err := getter.GetOutput(ac, "dev", "target", "id", true, callerContext, nil, TerraformLookupOptions{SecretsMaskOnly: maskOnly}) require.ErrorIs(t, err, errUtils.ErrTerraformOutputFailed) diff --git a/internal/exec/terraform_plan_diff_preparation_test.go b/internal/exec/terraform_plan_diff_preparation_test.go deleted file mode 100644 index ce40a7a8e14..00000000000 --- a/internal/exec/terraform_plan_diff_preparation_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package exec - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestFilterPlanDiffFlags(t *testing.T) { - t.Parallel() - - tests := []struct { - name string - args []string - expected []string - }{ - { - name: "no flags to filter", - args: []string{"--var=foo=bar", "--auto-approve"}, - expected: []string{"--var=foo=bar", "--auto-approve"}, - }, - { - name: "with --orig= and --new= flags", - args: []string{"--orig=orig.plan", "--new=new.plan", "--var=foo=bar"}, - expected: []string{"--var=foo=bar"}, - }, - { - name: "with --orig and --new flags with space", - args: []string{"--orig", "orig.plan", "--var=foo=bar", "--new", "new.plan"}, - expected: []string{"--var=foo=bar"}, - }, - { - name: "with --orig flag but missing value", - args: []string{"--orig", "--var=foo=bar", "--new=new.plan"}, - expected: []string{"--var=foo=bar"}, - }, - { - name: "with mixed flags", - args: []string{"--var=foo=bar", "--orig=orig.plan", "--auto-approve", "--new=new.plan"}, - expected: []string{"--var=foo=bar", "--auto-approve"}, - }, - { - name: "with -var flag using space (integration test format)", - args: []string{"--orig=orig.plan", "-var", "foo=new-value"}, - expected: []string{"-var", "foo=new-value"}, - }, - { - name: "preserves multiple -var flags", - args: []string{"--orig", "orig.plan", "-var", "foo=bar", "-var", "baz=qux", "--new", "new.plan"}, - expected: []string{"-var", "foo=bar", "-var", "baz=qux"}, - }, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - t.Parallel() - result := filterPlanDiffFlags(tc.args) - assert.Equal(t, tc.expected, result) - }) - } -} diff --git a/internal/exec/terraform_plan_diff_test.go b/internal/exec/terraform_plan_diff_test.go index 87987fdc7af..b07537ec987 100644 --- a/internal/exec/terraform_plan_diff_test.go +++ b/internal/exec/terraform_plan_diff_test.go @@ -208,8 +208,8 @@ func TestRunTerraformShowCapturesWithInjectedStdout(t *testing.T) { for _, opt := range opts { opt(&cfg) } - require.NotNil(t, cfg.stdoutOverride) - _, err := cfg.stdoutOverride.Write([]byte("terraform noise\n{\"format_version\":\"1.0\"}")) + require.NotNil(t, cfg.StdoutOverride) + _, err := cfg.StdoutOverride.Write([]byte("terraform noise\n{\"format_version\":\"1.0\"}")) require.NoError(t, err) return nil }) diff --git a/internal/exec/terraform_query_test.go b/internal/exec/terraform_query_auth_test.go similarity index 68% rename from internal/exec/terraform_query_test.go rename to internal/exec/terraform_query_auth_test.go index b035c21ea55..d06728eb375 100644 --- a/internal/exec/terraform_query_test.go +++ b/internal/exec/terraform_query_auth_test.go @@ -1,127 +1,16 @@ package exec import ( - "context" - "path/filepath" "testing" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" - errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/auth" "github.com/cloudposse/atmos/pkg/auth/types" - scheduleradapters "github.com/cloudposse/atmos/pkg/scheduler/adapters" "github.com/cloudposse/atmos/pkg/schema" ) -// TestCreateQueryAuthManager verifies createQueryAuthManager behavior across -// different scenarios using injected auth factory to cover all branches. -func TestCreateQueryAuthManager(t *testing.T) { - tests := []struct { - name string - setupMock func(ctrl *gomock.Controller, m *MockAuthManagerQueryFactory) - expectErr bool - expectSentinel error - expectNilMgr bool - expectInfoStored bool - expectExit bool - }{ - { - name: "no auth configured returns nil manager", - setupMock: func(_ *gomock.Controller, m *MockAuthManagerQueryFactory) { - m.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil) - }, - expectNilMgr: true, - expectInfoStored: false, - }, - { - name: "nonexistent identity returns wrapped error", - setupMock: func(_ *gomock.Controller, m *MockAuthManagerQueryFactory) { - m.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errUtils.ErrAuthNotConfigured) - }, - expectErr: true, - expectSentinel: errUtils.ErrAuthNotConfigured, - expectNilMgr: true, - expectInfoStored: false, - }, - { - name: "non-nil manager is stored in info", - setupMock: func(ctrl *gomock.Controller, m *MockAuthManagerQueryFactory) { - m.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(types.NewMockAuthManager(ctrl), nil) - }, - expectNilMgr: false, - expectInfoStored: true, - }, - { - name: "ErrUserAborted calls Exit with SIGINT code", - setupMock: func(_ *gomock.Controller, m *MockAuthManagerQueryFactory) { - m.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errUtils.ErrUserAborted) - }, - expectExit: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - // Inject test factory. - ctrl := gomock.NewController(t) - mockFactory := NewMockAuthManagerQueryFactory(ctrl) - tt.setupMock(ctrl, mockFactory) - original := authManagerFactory - authManagerFactory = mockFactory - defer func() { authManagerFactory = original }() - - // Mock os.Exit to capture exit calls. - var exitCode int - exitCalled := false - originalExit := errUtils.OsExit - errUtils.OsExit = func(code int) { - exitCode = code - exitCalled = true - } - defer func() { errUtils.OsExit = originalExit }() - - info := &schema.ConfigAndStacksInfo{} - atmosConfig := &schema.AtmosConfiguration{ - BasePath: t.TempDir(), - } - - authManager, err := createQueryAuthManager(info, atmosConfig) - - if tt.expectExit { - assert.True(t, exitCalled, "Exit should have been called") - assert.Equal(t, errUtils.ExitCodeSIGINT, exitCode) - return - } - - assert.False(t, exitCalled, "Exit should not have been called") - - if tt.expectErr { - assert.Error(t, err) - if tt.expectSentinel != nil { - assert.ErrorIs(t, err, tt.expectSentinel) - } - } else { - assert.NoError(t, err) - } - - if tt.expectNilMgr { - assert.Nil(t, authManager) - } else { - assert.NotNil(t, authManager) - } - - if tt.expectInfoStored { - assert.NotNil(t, info.AuthManager, "AuthManager should be stored in info") - } else { - assert.Nil(t, info.AuthManager, "AuthManager should not be stored in info") - } - }) - } -} - // TestPropagateAuth verifies that propagateAuth extracts AuthContext from // AuthManager.GetStackInfo() and populates ConfigAndStacksInfo. This is the // core propagation logic used by ExecuteDescribeStacks for each component. @@ -458,42 +347,3 @@ func TestMultipleYamlFunctionsAuthPropagation(t *testing.T) { assert.Equal(t, "state-bucket", result["bucket"]) assert.Equal(t, "output-region", result["region"]) } - -// TestExecuteTerraformQueryComponent covers executeTerraformQueryComponent -// directly — it has no test at all today (only ExecuteTerraformQuery / -// ExecuteTerraformQueryWithContext, which call it indirectly through the -// scheduler, are exercised elsewhere). A nonexistent component/stack makes -// the underlying ExecuteTerraform fail fast during stack processing, without -// needing a real terraform/tofu binary, so both CaptureOutput branches can be -// asserted directly. -func TestExecuteTerraformQueryComponent(t *testing.T) { - t.Chdir(filepath.Join("..", "..", "examples", "demo-stacks")) - - tests := []struct { - name string - captureOutput bool - }{ - {name: "without output capture", captureOutput: false}, - {name: "with output capture", captureOutput: true}, - } - - for _, tc := range tests { - t.Run(tc.name, func(t *testing.T) { - execution := scheduleradapters.TerraformExecution{ - Context: context.Background(), - Info: schema.ConfigAndStacksInfo{ - ComponentFromArg: "nonexistent", - Stack: "nonexistent", - SubCommand: "plan", - }, - CaptureOutput: tc.captureOutput, - } - - result, err := executeTerraformQueryComponent(execution) - - require.Error(t, err, "a nonexistent component/stack must fail during stack processing") - assert.Empty(t, result.Stdout, "no shell command ever runs, so nothing is captured") - assert.Empty(t, result.Stderr) - }) - } -} diff --git a/internal/exec/terraform_reference_cache_auth_test.go b/internal/exec/terraform_reference_cache_auth_test.go index decfb461335..b8d6feb2616 100644 --- a/internal/exec/terraform_reference_cache_auth_test.go +++ b/internal/exec/terraform_reference_cache_auth_test.go @@ -59,28 +59,6 @@ func TestGetTerraformStateCacheSeparatesCallerCredentials(t *testing.T) { } // Invalidating a component removes the reads of every caller scope. -func TestInvalidateTerraformStateCacheRemovesAllCallerScopes(t *testing.T) { - ac, _, path := setupDeferredCacheTarget(t) - ac.AuthManager = nil - ResetStateCache() - t.Cleanup(ResetStateCache) - scopes := []*schema.AuthContext{authContextForProfile("first"), authContextForProfile("second"), nil} - writeTerraformStateOutput(t, path, "before") - for _, scope := range scopes { - value, err := GetTerraformState(ac, "!terraform.state", "dev", "target", "id", false, scope, nil) - require.NoError(t, err) - require.Equal(t, "before", value) - } - - writeTerraformStateOutput(t, path, "after") - invalidateTerraformStateCache("dev", "target") - for _, scope := range scopes { - value, err := GetTerraformState(ac, "!terraform.state", "dev", "target", "id", false, scope, nil) - require.NoError(t, err) - require.Equal(t, "after", value) - } -} - // atmos.Component() results are partitioned by the enclosing component's credentials and // by whether authentication is disabled. func TestComponentFuncCacheSeparatesCallerCredentials(t *testing.T) { diff --git a/internal/exec/terraform_reference_cache_test.go b/internal/exec/terraform_reference_cache_test.go index 3da98e17052..57fe5787098 100644 --- a/internal/exec/terraform_reference_cache_test.go +++ b/internal/exec/terraform_reference_cache_test.go @@ -15,7 +15,6 @@ import ( "github.com/cloudposse/atmos/pkg/degradation" iolib "github.com/cloudposse/atmos/pkg/io" "github.com/cloudposse/atmos/pkg/schema" - "github.com/cloudposse/atmos/pkg/terraform/refcache" ) // Compile guards for the configuration fields that scope these regressions. @@ -162,34 +161,6 @@ func TestReferenceCacheSeparatesStaticConfiguration(t *testing.T) { } } -func TestInvalidateTerraformStateCacheAllPartitions(t *testing.T) { - ResetStateCache() - t.Cleanup(ResetStateCache) - keys := []refcache.Key{} - for _, basePath := range []string{"head", "base"} { - for _, configPath := range []string{"config-first", "config-second"} { - for _, maskOnly := range []bool{false, true} { - for _, authDisabled := range []bool{false, true} { - key := refcache.NewKey(&schema.AtmosConfiguration{BasePath: basePath, CliConfigPath: configPath}, "dev", "target") - key.Inspection = maskOnly - key.Auth = refcache.AuthScope(nil, nil, authDisabled) - keys = append(keys, key) - terraformStateCache.Store(key, terraformStateNotProvisionedCacheEntry{}) - } - } - } - } - unrelated := refcache.NewKey(&schema.AtmosConfiguration{}, "other", "target") - terraformStateCache.Store(unrelated, terraformStateNotProvisionedCacheEntry{}) - invalidateTerraformStateCache("dev", "target") - for _, key := range keys { - _, exists := terraformStateCache.Load(key) - require.False(t, exists) - } - _, exists := terraformStateCache.Load(unrelated) - require.True(t, exists, "another stack must retain its cached state") -} - func TestComponentInspectionRetriesComputedValues(t *testing.T) { for _, deferredMode := range []bool{false, true} { t.Run(fmt.Sprintf("deferred=%t", deferredMode), func(t *testing.T) { diff --git a/internal/exec/terraform_secret_resolution_test.go b/internal/exec/terraform_secret_resolution_test.go index ae69ccd5946..90ba3cdbf3b 100644 --- a/internal/exec/terraform_secret_resolution_test.go +++ b/internal/exec/terraform_secret_resolution_test.go @@ -17,8 +17,6 @@ import ( cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/function/yamlsource" iolib "github.com/cloudposse/atmos/pkg/io" - "github.com/cloudposse/atmos/pkg/process" - "github.com/cloudposse/atmos/pkg/provisioner" "github.com/cloudposse/atmos/pkg/schema" "github.com/cloudposse/atmos/pkg/secrets" "github.com/cloudposse/atmos/pkg/store" @@ -270,39 +268,6 @@ func runSecretPlanForTest() int { return 0 } -// TestSecretReferenceReachesTerraformPlan checks subprocess inputs and masked output together. -func TestSecretReferenceReachesTerraformPlan(t *testing.T) { - config, mockStore := setupNativeSecretReference(t) - mockStore.EXPECT().Get("dev", "producer", "CREDENTIAL").Return(nativeReferenceSecret, nil).MinTimes(1) - exe, err := os.Executable() - require.NoError(t, err) - // Load the consumer through the same execution stack processor used by plan. - info, err := ProcessStacks(config, schema.ConfigAndStacksInfo{ - ComponentFromArg: "consumer", ComponentType: "terraform", Stack: "dev", SubCommand: "plan", - }, true, true, true, nil, nil) - require.NoError(t, err) - info.Command = exe - info.SkipInit = true - info.ComponentBackendType = "http" // Skip workspace selection; only plan is under test. - info.ComponentEnvSection["_ATMOS_TEST_SECRET_PLAN"] = "1" - execCtx, err := prepareComponentExecution(context.Background(), provisioner.OutputWriters{}, config, &info, false) - require.NoError(t, err) - t.Cleanup(func() { - if info.RCCleanup != nil { - assert.NoError(t, info.RCCleanup()) - } - }) - var stdout, stderr bytes.Buffer - err = executeCommandPipeline( - config, &info, execCtx, - WithProcessStreams(process.Streams{Stdin: strings.NewReader(""), Stdout: &stdout, Stderr: &stderr}), - ) - require.NoError(t, err, "controlled Terraform stderr: %s", stderr.String()) - assert.Contains(t, stdout.String(), "plan credential: "+iolib.MaskReplacement) - assert.Contains(t, stderr.String(), "diagnostic credential: "+iolib.MaskReplacement) - assert.NotContains(t, stdout.String()+stderr.String(), nativeReferenceSecret) -} - // TestNonTerraformSecretReferences checks native references in non-Terraform execution. func TestNonTerraformSecretReferences(t *testing.T) { for _, componentType := range []string{"helm", "helmfile", "kubernetes", "container"} { diff --git a/internal/exec/terraform_verify_plan_test.go b/internal/exec/terraform_verify_plan_test.go index a79e5569ad0..740a13b74b8 100644 --- a/internal/exec/terraform_verify_plan_test.go +++ b/internal/exec/terraform_verify_plan_test.go @@ -45,39 +45,3 @@ func TestVerifyPlanfile_StoredPlanFileExists_FailsOnStackProcessing(t *testing.T // Should NOT be ErrPlanVerificationFailed since it fails on config init, not verification. assert.NotErrorIs(t, err, errUtils.ErrPlanVerificationFailed) } - -func TestFinalizeVerification(t *testing.T) { - t.Parallel() - - freshPlan := filepath.Join(t.TempDir(), "fresh.tfplan") - - t.Run("drift + fail returns verification error", func(t *testing.T) { - t.Parallel() - info := &schema.ConfigAndStacksInfo{} - err := finalizeVerification(info, freshPlan, " ~ resource changed", true, schema.PlanfileVerifyFail) - require.Error(t, err) - assert.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) - assert.Contains(t, err.Error(), "resource changed") - // On failure the fresh plan is NOT selected for apply. - assert.Empty(t, info.PlanFile) - assert.False(t, info.UseTerraformPlan) - }) - - t.Run("drift + warn proceeds with the fresh plan", func(t *testing.T) { - t.Parallel() - info := &schema.ConfigAndStacksInfo{} - err := finalizeVerification(info, freshPlan, " ~ resource changed", true, schema.PlanfileVerifyWarn) - require.NoError(t, err) - assert.Equal(t, freshPlan, info.PlanFile) - assert.True(t, info.UseTerraformPlan) - }) - - t.Run("no drift selects the fresh plan", func(t *testing.T) { - t.Parallel() - info := &schema.ConfigAndStacksInfo{} - err := finalizeVerification(info, freshPlan, "", false, schema.PlanfileVerifyFail) - require.NoError(t, err) - assert.Equal(t, freshPlan, info.PlanFile) - assert.True(t, info.UseTerraformPlan) - }) -} diff --git a/internal/exec/testmain_test.go b/internal/exec/testmain_test.go index e27dc1a675e..7e40e2fe9bc 100644 --- a/internal/exec/testmain_test.go +++ b/internal/exec/testmain_test.go @@ -3,7 +3,6 @@ package exec import ( "fmt" "os" - "path/filepath" "slices" "strconv" "strings" @@ -11,11 +10,8 @@ import ( "time" "github.com/cloudposse/atmos/pkg/auth/credentials/credtest" - "github.com/cloudposse/atmos/pkg/ci" - githubprovider "github.com/cloudposse/atmos/pkg/ci/providers/github" "github.com/cloudposse/atmos/pkg/data" iolib "github.com/cloudposse/atmos/pkg/io" - "github.com/cloudposse/atmos/pkg/schema" "github.com/cloudposse/atmos/pkg/ui" ) @@ -78,9 +74,6 @@ func TestMain(m *testing.M) { if os.Getenv(testEnvFakeTerraform) == "1" { os.Exit(runFakeTerraformForTest()) } - if os.Getenv(testEnvRunLogGroupPipeline) == "1" { - os.Exit(runLogGroupPipelineForTest()) - } // Write a single byte to the counter file on every invocation. // This lets tests count how many times the subprocess was spawned by reading @@ -203,7 +196,7 @@ func runFakeTerraformForTest() int { args := os.Args[1:] fmt.Printf("fake terraform %s\n", strings.Join(args, " ")) - isWorkspaceSelect := len(args) >= 3 && args[0] == subcommandWorkspace && args[1] == "select" + isWorkspaceSelect := len(args) >= 3 && args[0] == "workspace" && args[1] == "select" if isWorkspaceSelect && os.Getenv(testEnvFakeTerraformSelectFail) == "1" { fmt.Fprintf(os.Stderr, "Workspace %q doesn't exist.\n", args[2]) return 1 @@ -222,53 +215,6 @@ func runFakeTerraformForTest() int { return 0 } -func runLogGroupPipelineForTest() int { - ci.Register(githubprovider.NewProvider()) - - componentPath := filepath.Join(os.TempDir(), fmt.Sprintf("atmos-log-group-pipeline-%d", os.Getpid())) - if err := os.MkdirAll(componentPath, 0o755); err != nil { - fmt.Fprintf(os.Stderr, "creating temp component path: %v\n", err) - return 1 - } - defer func() { _ = os.RemoveAll(componentPath) }() - - exePath, err := os.Executable() - if err != nil { - fmt.Fprintf(os.Stderr, "resolving test executable: %v\n", err) - return 1 - } - - atmosConfig := schema.AtmosConfiguration{} - atmosConfig.CI.Enabled = true - atmosConfig.CI.Groups.Mode = ci.GroupModeAuto - - componentEnv := []string{testEnvFakeTerraform + "=1"} - if os.Getenv(testEnvFakeTerraformSelectFail) == "1" { - componentEnv = append(componentEnv, testEnvFakeTerraformSelectFail+"=1") - } - - info := schema.ConfigAndStacksInfo{ - SubCommand: "plan", - SkipInit: os.Getenv(testEnvPipelineSkipInit) == "1", - ComponentBackendType: os.Getenv(testEnvPipelineBackendType), - TerraformWorkspace: "dev", - Command: exePath, - ComponentEnvList: componentEnv, - } - execCtx := &componentExecContext{ - componentPath: componentPath, - varFile: "vars.tfvars", - planFile: "plan.tfplan", - workingDir: componentPath, - } - - if err := executeCommandPipeline(&atmosConfig, &info, execCtx); err != nil { - fmt.Fprintf(os.Stderr, "pipeline failed: %v\n", err) - return 1 - } - return 0 -} - // runPlanDiffTerraformForTest implements show/plan using JSON plan fixtures, without a provider or network. func runPlanDiffTerraformForTest() int { args := os.Args[1:] diff --git a/internal/exec/yaml_func_aws.go b/internal/exec/yaml_func_aws.go index da75c0dda23..b4457a87544 100644 --- a/internal/exec/yaml_func_aws.go +++ b/internal/exec/yaml_func_aws.go @@ -10,6 +10,7 @@ import ( awscfn "github.com/cloudposse/atmos/pkg/aws/cloudformation" awsIdentity "github.com/cloudposse/atmos/pkg/aws/identity" awsOrg "github.com/cloudposse/atmos/pkg/aws/organization" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" cfg "github.com/cloudposse/atmos/pkg/config" fnparser "github.com/cloudposse/atmos/pkg/function/parser" log "github.com/cloudposse/atmos/pkg/logger" @@ -317,8 +318,8 @@ func resolveCloudFormationOutputAuth(ac *schema.AtmosConfiguration, component, s // Deferred resolution needs the caller's auth configuration, not just its // already-resolved credentials, to bind inherited defaults to the target. if parent == nil || authdeferred.IsDeferred(ac.AuthManager) { - parent = &authContextWrapper{stackInfo: info} + parent = &terraformcomponent.AuthContextWrapper{StackInfo: info} } } - return resolveAuthManagerForNestedComponent(ac, component, stack, parent) + return terraformExecutor.ResolveAuthManagerForNestedComponent(ac, component, stack, parent) } diff --git a/internal/exec/yaml_func_terraform_output.go b/internal/exec/yaml_func_terraform_output.go index 42569853429..eabeee9ab0b 100644 --- a/internal/exec/yaml_func_terraform_output.go +++ b/internal/exec/yaml_func_terraform_output.go @@ -3,6 +3,7 @@ package exec import ( "fmt" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" fnparser "github.com/cloudposse/atmos/pkg/function/parser" log "github.com/cloudposse/atmos/pkg/logger" "github.com/cloudposse/atmos/pkg/perf" @@ -103,11 +104,11 @@ func processTagTerraformOutputWithContext( } defer cleanup() - if value, mocked, mockErr := resolveTerraformMockOutput(atmosConfig, stackInfo, stack, component, output); mocked { + if value, mocked, mockErr := terraformExecutor.ResolveTerraformMockOutput(atmosConfig, stackInfo, stack, component, output); mocked { return value, mockErr } - return lookupTerraformOutput(atmosConfig, stackInfo, &terraformStateLookup{yamlFunc: input, stack: stack, component: component, output: output}) + return lookupTerraformOutput(atmosConfig, stackInfo, &terraformcomponent.TerraformStateLookup{YamlFunc: input, Stack: stack, Component: component, Output: output}) } // lookupTerraformOutput reads the real Terraform output. When `--use-mocks` runs in fallback mode @@ -117,9 +118,9 @@ func processTagTerraformOutputWithContext( func lookupTerraformOutput( atmosConfig *schema.AtmosConfiguration, stackInfo *schema.ConfigAndStacksInfo, - lookup *terraformStateLookup, + lookup *terraformcomponent.TerraformStateLookup, ) (any, error) { - mocks, err := declaredFallbackMocks(atmosConfig, stackInfo, lookup) + mocks, err := terraformExecutor.DeclaredFallbackMocks(atmosConfig, stackInfo, lookup) if err != nil { return nil, err } @@ -129,7 +130,7 @@ func lookupTerraformOutput( authContext, authManager := terraformLookupAuth(atmosConfig, stackInfo) - value, exists, err := outputGetter.GetOutput(atmosConfig, lookup.stack, lookup.component, lookup.output, false, authContext, authManager, terraform.LookupOptionsFromInfo(stackInfo)...) + value, exists, err := outputGetter.GetOutput(atmosConfig, lookup.Stack, lookup.Component, lookup.Output, false, authContext, authManager, terraform.LookupOptionsFromInfo(stackInfo)...) if err != nil { return handleTerraformOutputError(atmosConfig, lookup, err) } @@ -144,8 +145,8 @@ func lookupTerraformOutput( } // wrapTerraformOutputError adds the component, stack, and output context to a failed output lookup. -func wrapTerraformOutputError(lookup *terraformStateLookup, err error) error { - return fmt.Errorf("failed to get terraform output for component %s in stack %s, output %s: %w", lookup.component, lookup.stack, lookup.output, err) +func wrapTerraformOutputError(lookup *terraformcomponent.TerraformStateLookup, err error) error { + return fmt.Errorf("failed to get terraform output for component %s in stack %s, output %s: %w", lookup.Component, lookup.Stack, lookup.Output, err) } // lookupTerraformOutputWithMocks fetches the whole real output map and evaluates the expression @@ -156,25 +157,25 @@ func wrapTerraformOutputError(lookup *terraformStateLookup, err error) error { func lookupTerraformOutputWithMocks( atmosConfig *schema.AtmosConfiguration, stackInfo *schema.ConfigAndStacksInfo, - lookup *terraformStateLookup, + lookup *terraformcomponent.TerraformStateLookup, mocks map[string]any, ) (any, error) { authContext, authManager := terraformLookupAuth(atmosConfig, stackInfo) - whole, exists, err := outputGetter.GetOutput(atmosConfig, lookup.stack, lookup.component, terraformAllOutputsExpression, false, authContext, authManager, terraform.LookupOptionsFromInfo(stackInfo)...) + whole, exists, err := outputGetter.GetOutput(atmosConfig, lookup.Stack, lookup.Component, terraformcomponent.TerraformAllOutputsExpression, false, authContext, authManager, terraform.LookupOptionsFromInfo(stackInfo)...) if err != nil { wrapped := wrapTerraformOutputError(lookup, err) if !isRecoverableTerraformError(err) { - return nil, withFallbackModeHint(wrapped) + return nil, terraformcomponent.WithFallbackModeHint(wrapped) } - return resolveTerraformOutputWithMocks(atmosConfig, lookup, mocks, nil, wrapped) + return terraformExecutor.ResolveTerraformOutputWithMocks(atmosConfig, lookup, mocks, nil, wrapped) } var realOutputs map[string]any if exists { realOutputs, _ = whole.(map[string]any) } - return resolveTerraformOutputWithMocks(atmosConfig, lookup, mocks, realOutputs, nil) + return terraformExecutor.ResolveTerraformOutputWithMocks(atmosConfig, lookup, mocks, realOutputs, nil) } // handleTerraformOutputError resolves a failed output lookup. Only recoverable terraform errors @@ -182,23 +183,23 @@ func lookupTerraformOutputWithMocks( // (API failures, auth errors, infrastructure issues) fail hard. func handleTerraformOutputError( atmosConfig *schema.AtmosConfiguration, - lookup *terraformStateLookup, + lookup *terraformcomponent.TerraformStateLookup, err error, ) (any, error) { wrapped := wrapTerraformOutputError(lookup, err) if !isRecoverableTerraformError(err) { return nil, wrapped } - if !hasYqDefault(lookup.output) { + if !hasYqDefault(lookup.Output) { return nil, wrapped } log.Debug( "Evaluating YQ default for recoverable error", - log.FieldFunction, lookup.yamlFunc, + log.FieldFunction, lookup.YamlFunc, "error", err.Error(), ) // Evaluate YQ against an empty map to get the default value. - defaultValue, yqErr := evaluateYqDefault(atmosConfig, lookup.output) + defaultValue, yqErr := evaluateYqDefault(atmosConfig, lookup.Output) if yqErr != nil { // If YQ evaluation fails, return the original error. return nil, wrapped @@ -210,26 +211,26 @@ func handleTerraformOutputError( // and otherwise nil (backward compatible). func handleMissingTerraformOutput( atmosConfig *schema.AtmosConfiguration, - lookup *terraformStateLookup, + lookup *terraformcomponent.TerraformStateLookup, ) (any, error) { - if !hasYqDefault(lookup.output) { + if !hasYqDefault(lookup.Output) { // No default available, return nil (backward compatible). return nil, nil } log.Debug( "Evaluating YQ default for non-existent output", - log.FieldFunction, lookup.yamlFunc, - "component", lookup.component, - "stack", lookup.stack, - "output", lookup.output, + log.FieldFunction, lookup.YamlFunc, + "component", lookup.Component, + "stack", lookup.Stack, + "output", lookup.Output, ) // Evaluate YQ against an empty map to get the default value. - defaultValue, yqErr := evaluateYqDefault(atmosConfig, lookup.output) + defaultValue, yqErr := evaluateYqDefault(atmosConfig, lookup.Output) if yqErr != nil { // If YQ evaluation fails, return nil (backward compatible). log.Debug( "YQ default evaluation failed, returning nil", - log.FieldFunction, lookup.yamlFunc, + log.FieldFunction, lookup.YamlFunc, "error", yqErr.Error(), ) return nil, nil diff --git a/internal/exec/yaml_func_terraform_output_authcontext_test.go b/internal/exec/yaml_func_terraform_output_authcontext_test.go index 4674faae1d7..b1250a02287 100644 --- a/internal/exec/yaml_func_terraform_output_authcontext_test.go +++ b/internal/exec/yaml_func_terraform_output_authcontext_test.go @@ -7,6 +7,7 @@ import ( "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" "github.com/cloudposse/atmos/pkg/schema" ) @@ -61,7 +62,7 @@ func TestAuthDisabledPropagatesWrapperToOutputGetter(t *testing.T) { return } - wrapper, ok := gotAuthManager.(*authContextWrapper) + wrapper, ok := gotAuthManager.(*terraformcomponent.AuthContextWrapper) require.True(t, ok, "auth-disabled with no AuthManager must pass an authContextWrapper downstream") require.NotNil(t, wrapper.GetStackInfo()) assert.True(t, wrapper.GetStackInfo().AuthDisabled) diff --git a/internal/exec/yaml_func_terraform_state.go b/internal/exec/yaml_func_terraform_state.go index d78a3ad97a3..26c5a4a2dbc 100644 --- a/internal/exec/yaml_func_terraform_state.go +++ b/internal/exec/yaml_func_terraform_state.go @@ -8,6 +8,7 @@ import ( errUtils "github.com/cloudposse/atmos/errors" tb "github.com/cloudposse/atmos/internal/terraform_backend" authdeferred "github.com/cloudposse/atmos/pkg/auth/deferred" + terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" fnparser "github.com/cloudposse/atmos/pkg/function/parser" log "github.com/cloudposse/atmos/pkg/logger" "github.com/cloudposse/atmos/pkg/perf" @@ -108,11 +109,11 @@ func processTagTerraformStateWithContext( defer resolutionCtx.Pop(atmosConfig) } - if value, mocked, mockErr := resolveTerraformMockOutput(atmosConfig, stackInfo, stack, component, output); mocked { + if value, mocked, mockErr := terraformExecutor.ResolveTerraformMockOutput(atmosConfig, stackInfo, stack, component, output); mocked { return value, mockErr } - return lookupTerraformState(atmosConfig, stackInfo, &terraformStateLookup{yamlFunc: input, stack: stack, component: component, output: output}) + return lookupTerraformState(atmosConfig, stackInfo, &terraformcomponent.TerraformStateLookup{YamlFunc: input, Stack: stack, Component: component, Output: output}) } // terraformLookupAuth extracts the auth context and auth manager a Terraform state/output lookup @@ -126,7 +127,7 @@ func terraformLookupAuth(atmosConfig *schema.AtmosConfiguration, stackInfo *sche authContext := stackInfo.AuthContext authManager := stackInfo.AuthManager if authdeferred.IsDeferred(atmosConfig.AuthManager) || (authManager == nil && stackInfo.AuthDisabled) { - authManager = &authContextWrapper{stackInfo: stackInfo} + authManager = &terraformcomponent.AuthContextWrapper{StackInfo: stackInfo} } return authContext, authManager } @@ -138,9 +139,9 @@ func terraformLookupAuth(atmosConfig *schema.AtmosConfiguration, stackInfo *sche func lookupTerraformState( atmosConfig *schema.AtmosConfiguration, stackInfo *schema.ConfigAndStacksInfo, - lookup *terraformStateLookup, + lookup *terraformcomponent.TerraformStateLookup, ) (any, error) { - mocks, err := declaredFallbackMocks(atmosConfig, stackInfo, lookup) + mocks, err := terraformExecutor.DeclaredFallbackMocks(atmosConfig, stackInfo, lookup) if err != nil { return nil, err } @@ -150,7 +151,7 @@ func lookupTerraformState( authContext, authManager := terraformLookupAuth(atmosConfig, stackInfo) - value, err := stateGetter.GetState(atmosConfig, lookup.yamlFunc, lookup.stack, lookup.component, lookup.output, false, authContext, authManager, terraform.LookupOptionsFromInfo(stackInfo)...) + value, err := stateGetter.GetState(atmosConfig, lookup.YamlFunc, lookup.Stack, lookup.Component, lookup.Output, false, authContext, authManager, terraform.LookupOptionsFromInfo(stackInfo)...) if err != nil { return handleTerraformStateError(atmosConfig, lookup, err) } @@ -165,44 +166,44 @@ func lookupTerraformState( func lookupTerraformStateWithMocks( atmosConfig *schema.AtmosConfiguration, stackInfo *schema.ConfigAndStacksInfo, - lookup *terraformStateLookup, + lookup *terraformcomponent.TerraformStateLookup, mocks map[string]any, ) (any, error) { authContext, authManager := terraformLookupAuth(atmosConfig, stackInfo) - whole, err := stateGetter.GetState(atmosConfig, lookup.yamlFunc, lookup.stack, lookup.component, terraformAllOutputsExpression, false, authContext, authManager, terraform.LookupOptionsFromInfo(stackInfo)...) + whole, err := stateGetter.GetState(atmosConfig, lookup.YamlFunc, lookup.Stack, lookup.Component, terraformcomponent.TerraformAllOutputsExpression, false, authContext, authManager, terraform.LookupOptionsFromInfo(stackInfo)...) if err != nil && !isRecoverableTerraformError(err) { - return nil, withFallbackModeHint(err) + return nil, terraformcomponent.WithFallbackModeHint(err) } var realOutputs map[string]any if err == nil { realOutputs, _ = whole.(map[string]any) } - return resolveTerraformOutputWithMocks(atmosConfig, lookup, mocks, realOutputs, err) + return terraformExecutor.ResolveTerraformOutputWithMocks(atmosConfig, lookup, mocks, realOutputs, err) } // handleTerraformStateError resolves a failed state lookup: a recoverable error may be rescued by // a YQ default; anything else fails unchanged. func handleTerraformStateError( atmosConfig *schema.AtmosConfiguration, - lookup *terraformStateLookup, + lookup *terraformcomponent.TerraformStateLookup, err error, ) (any, error) { if !isRecoverableTerraformError(err) { // Non-recoverable error: auth, network, or backend failures are never masked. return nil, err } - if !hasYqDefault(lookup.output) { + if !hasYqDefault(lookup.Output) { return nil, err } log.Debug( "Evaluating YQ default for recoverable error", - "function", lookup.yamlFunc, + "function", lookup.YamlFunc, "error", err.Error(), ) // Evaluate YQ against an empty map to get the default value. - defaultValue, yqErr := evaluateYqDefault(atmosConfig, lookup.output) + defaultValue, yqErr := evaluateYqDefault(atmosConfig, lookup.Output) if yqErr != nil { // If YQ evaluation fails, return the original error. return nil, fmt.Errorf("%w: failed to evaluate YQ default: %w", err, yqErr) diff --git a/pkg/component/execution_options.go b/pkg/component/execution_options.go new file mode 100644 index 00000000000..ac9ab55fbeb --- /dev/null +++ b/pkg/component/execution_options.go @@ -0,0 +1,235 @@ +package component + +import ( + "context" + "io" + + "github.com/spf13/cobra" + + metricsprocess "github.com/cloudposse/atmos/pkg/metrics/process" + "github.com/cloudposse/atmos/pkg/perf" + "github.com/cloudposse/atmos/pkg/process" +) + +// ExecutionOption is a functional option for ExecuteShellCommand. +type ExecutionOption func(*ExecutionOptions) + +// ExecutionOptions holds optional configuration for shell command execution. +type ExecutionOptions struct { + StdoutCapture io.Writer + StderrCapture io.Writer + StdoutOverride io.Writer + Streams *process.Streams + Ctx context.Context + // ProcessEnv replaces os.Environ() as the process environment. + // When set, ExecuteShellCommand uses this instead of re-reading os.Environ(). + // This is used when auth has already sanitized the environment (e.g., removed IRSA vars). + ProcessEnv []string + // InvokingCmd is the Cobra command the user actually invoked (e.g. the + // terraform plan subcommand). Threaded through so ExecuteTerraform can + // report the real Flags actually passed (FR-003b) to the exec-metadata + // sync capture, instead of a pass-through-args collection that cannot + // contain atmos-recognized flags (research.md Decision 14). + InvokingCmd *cobra.Command + // ExecMetadataParser, when set, is called once by captureExecMetadataSync + // (sync-allowlisted commands only) to obtain command-specific structured + // data (FR-006) for the execution record. cmd/terraform supplies this — + // it can safely call pkg/ci/plugins/terraform's output parser, unlike + // internal/exec, which cannot import it without reintroducing a confirmed + // import cycle (research.md Decision 18). The output parameter is the + // scoped exec-metadata output text executeCommandPipeline captured for + // just the main plan/apply/deploy subprocess (FR-006f). + ExecMetadataParser func(subCommand string, exitCode int, output string) any + + // ExecMetadataStdoutCapture/ExecMetadataStderrCapture, when set, additionally + // tee this specific ExecuteShellCommand call's stdout/stderr into these + // writers, alongside (not instead of) StdoutCapture/StderrCapture. Used + // internally by executeCommandPipeline (via WithMetadataOutputCapture) to + // scope the exec-metadata parser's input to only the final plan/apply/deploy + // subprocess invocation, while StdoutCapture/StderrCapture's buffers keep + // accumulating the whole pipeline's output for other consumers (e.g. CI + // job-summary hooks) that legitimately need it (FR-006f, research.md + // Decision 32). + ExecMetadataStdoutCapture io.Writer + ExecMetadataStderrCapture io.Writer + + // MetricsCallback, when set, is invoked once after the subprocess exits + // with the subprocess tree's collected resource-usage metrics (nil-safe: + // only called when a non-nil result was collected). Lets a caller (e.g. + // executeMainTerraformCommand) capture this specific shell command's + // subprocess-tree metrics for combining with atmos's own self-usage, for + // both the local per-command display and the exec-metadata sync upload. + MetricsCallback func(*metricsprocess.ProcessMetrics) +} + +// WithInvokingCommand provides the Cobra command the user actually invoked, +// so ExecuteTerraform's exec-metadata sync capture can derive Flags from the +// command's own record of explicitly-set flags (proexec.FlagsFromCommand) +// rather than info.AdditionalArgsAndFlags. +func WithInvokingCommand(cmd *cobra.Command) ExecutionOption { + defer perf.Track(nil, "component.WithInvokingCommand")() + + return func(c *ExecutionOptions) { + c.InvokingCmd = cmd + } +} + +// InvokingCommandFromOptions extracts the invoking *cobra.Command (if any) set +// via WithInvokingCommand among opts. +func InvokingCommandFromOptions(opts ...ExecutionOption) *cobra.Command { + defer perf.Track(nil, "component.InvokingCommandFromOptions")() + + var cfg ExecutionOptions + for _, opt := range opts { + opt(&cfg) + } + return cfg.InvokingCmd +} + +// WithExecMetadataParser provides a closure that, given the invoking +// subcommand name and the command's own exit code, returns command-specific +// structured data (FR-006) for the execution record — or nil if there is +// none to report. The exitCode parameter is the terraform/tofu subprocess's +// own exit code (research.md Decision 27), supplied by +// captureExecMetadataSync at call time since it isn't known when this +// closure is created. Cmd/terraform +// supplies this closure so ExecuteTerraform's exec-metadata sync capture can +// obtain parsed terraform plan/apply/deploy output without internal/exec +// itself importing the CI plugin's parser (research.md Decision 18). +func WithExecMetadataParser(fn func(subCommand string, exitCode int, output string) any) ExecutionOption { + defer perf.Track(nil, "component.WithExecMetadataParser")() + + return func(c *ExecutionOptions) { + c.ExecMetadataParser = fn + } +} + +// MetadataParserFromOptions extracts the parser closure (if any) set via +// WithExecMetadataParser among opts. +func MetadataParserFromOptions(opts ...ExecutionOption) func(subCommand string, exitCode int, output string) any { + defer perf.Track(nil, "component.MetadataParserFromOptions")() + + var cfg ExecutionOptions + for _, opt := range opts { + opt(&cfg) + } + return cfg.ExecMetadataParser +} + +// WithMetricsCallback provides a closure that ExecuteShellCommand invokes +// once, after the subprocess exits, with the subprocess tree's collected +// resource-usage metrics. Used by executeMainTerraformCommand to capture the +// main plan/apply/deploy subprocess's own metrics for combining with atmos's +// self-usage — both for local display and the exec-metadata sync upload. +func WithMetricsCallback(fn func(*metricsprocess.ProcessMetrics)) ExecutionOption { + defer perf.Track(nil, "component.WithMetricsCallback")() + + return func(c *ExecutionOptions) { + c.MetricsCallback = fn + } +} + +// WithMetadataOutputCapture tees this specific ExecuteShellCommand call's +// stdout/stderr into stdoutW/stderrW, in addition to whatever +// WithStdoutCapture/WithStderrCapture already capture. Used internally by +// executeCommandPipeline to scope the exec-metadata parser's input to only the +// final plan/apply/deploy subprocess invocation (FR-006f), without disturbing +// the broader stdout/stderr buffers other consumers (e.g. cmd/terraform's +// capturedPlanOutput, used by CI job-summary hooks) rely on for the whole +// init+workspace-select+main pipeline. +func WithMetadataOutputCapture(stdoutW, stderrW io.Writer) ExecutionOption { + defer perf.Track(nil, "component.WithMetadataOutputCapture")() + + return func(c *ExecutionOptions) { + c.ExecMetadataStdoutCapture = stdoutW + c.ExecMetadataStderrCapture = stderrW + } +} + +// MetadataOutputCaptureFromOptions extracts the exec-metadata stdout/stderr +// tee writers (if any) set via WithMetadataOutputCapture among opts, so +// callers that retry the underlying invocation (executeShellCommandWithRetry) +// can reset them between attempts. +func MetadataOutputCaptureFromOptions(opts ...ExecutionOption) (io.Writer, io.Writer) { + defer perf.Track(nil, "component.MetadataOutputCaptureFromOptions")() + + var cfg ExecutionOptions + for _, opt := range opts { + opt(&cfg) + } + return cfg.ExecMetadataStdoutCapture, cfg.ExecMetadataStderrCapture +} + +// WithStdoutCapture returns an ExecutionOption that tees stdout to the provided writer. +// The captured output includes secret masking (post-MaskWriter). +func WithStdoutCapture(w io.Writer) ExecutionOption { + defer perf.Track(nil, "component.WithStdoutCapture")() + + return func(c *ExecutionOptions) { + c.StdoutCapture = w + } +} + +// WithStderrCapture returns an ExecutionOption that tees stderr to the provided writer. +// The captured output includes secret masking (post-MaskWriter). +func WithStderrCapture(w io.Writer) ExecutionOption { + defer perf.Track(nil, "component.WithStderrCapture")() + + return func(c *ExecutionOptions) { + c.StderrCapture = w + } +} + +// WithStdoutOverride returns an ExecutionOption that replaces the default stdout +// (os.Stdout) with a different writer. Used to redirect noisy commands (e.g., +// workspace select) to stderr so they don't pollute data-producing commands like output. +func WithStdoutOverride(w io.Writer) ExecutionOption { + defer perf.Track(nil, "component.WithStdoutOverride")() + + return func(c *ExecutionOptions) { + c.StdoutOverride = w + } +} + +// WithProcessStreams provides the subprocess standard streams. +func WithProcessStreams(streams process.Streams) ExecutionOption { + defer perf.Track(nil, "component.WithProcessStreams")() + + return func(c *ExecutionOptions) { + c.Streams = &streams + } +} + +// WithProcessContext provides the context used for subprocess execution. +func WithProcessContext(ctx context.Context) ExecutionOption { + defer perf.Track(nil, "component.WithProcessContext")() + + return func(c *ExecutionOptions) { + c.Ctx = ctx + } +} + +// ContextFromOptions returns the last supplied context or a background fallback. +func ContextFromOptions(opts ...ExecutionOption) context.Context { + defer perf.Track(nil, "component.ContextFromOptions")() + + var cfg ExecutionOptions + for _, opt := range opts { + opt(&cfg) + } + if cfg.Ctx == nil { + return context.Background() + } + return cfg.Ctx +} + +// WithEnvironment provides a pre-sanitized process environment for subprocess execution. +// When provided, ExecuteShellCommand uses this instead of re-reading os.Environ(). +// Pass nil to fall back to the default os.Environ() behavior. +func WithEnvironment(env []string) ExecutionOption { + defer perf.Track(nil, "component.WithEnvironment")() + + return func(c *ExecutionOptions) { + c.ProcessEnv = env + } +} diff --git a/pkg/component/execution_options_test.go b/pkg/component/execution_options_test.go new file mode 100644 index 00000000000..211acca30a3 --- /dev/null +++ b/pkg/component/execution_options_test.go @@ -0,0 +1,67 @@ +package component + +import ( + "bytes" + "context" + "testing" + + "github.com/spf13/cobra" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + metricsprocess "github.com/cloudposse/atmos/pkg/metrics/process" + "github.com/cloudposse/atmos/pkg/process" +) + +func TestExecutionOptionsPreserveStreamsAndReporting(t *testing.T) { + var stdout, stderr, override, metadataOut, metadataErr bytes.Buffer + command := &cobra.Command{Use: "plan"} + var metrics *metricsprocess.ProcessMetrics + parser := func(command string, code int, output string) any { return []any{command, code, output} } + options := []ExecutionOption{ + WithInvokingCommand(command), WithExecMetadataParser(parser), + WithMetricsCallback(func(value *metricsprocess.ProcessMetrics) { metrics = value }), + WithStdoutCapture(&stdout), WithStderrCapture(&stderr), WithStdoutOverride(&override), + WithMetadataOutputCapture(&metadataOut, &metadataErr), + WithProcessStreams(process.Streams{Stdin: &bytes.Buffer{}, Stdout: &stdout, Stderr: &stderr}), + WithProcessContext(t.Context()), WithEnvironment([]string{"TOKEN=component"}), + } + var resolved ExecutionOptions + for _, option := range options { + option(&resolved) + } + require.Same(t, &stdout, resolved.StdoutCapture) + require.Same(t, &stderr, resolved.StderrCapture) + require.Same(t, &override, resolved.StdoutOverride) + require.Same(t, &stdout, resolved.Streams.Stdout) + require.Same(t, &stderr, resolved.Streams.Stderr) + require.NotNil(t, resolved.Streams.Stdin) + assert.Equal(t, []string{"TOKEN=component"}, resolved.ProcessEnv) + require.Same(t, command, InvokingCommandFromOptions(options...)) + assert.Equal(t, []any{"plan", 2, "changes"}, MetadataParserFromOptions(options...)("plan", 2, "changes")) + out, errOut := MetadataOutputCaptureFromOptions(options...) + require.Same(t, &metadataOut, out) + require.Same(t, &metadataErr, errOut) + require.Same(t, t.Context(), ContextFromOptions(options...)) + value := &metricsprocess.ProcessMetrics{} + resolved.MetricsCallback(value) + require.Same(t, value, metrics) +} + +func TestExecutionOptionPrecedenceAndEmptyEnvironment(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + options := []ExecutionOption{WithProcessContext(context.Background()), WithProcessContext(ctx)} + cancel() + require.ErrorIs(t, ContextFromOptions(options...).Err(), context.Canceled) + assert.Equal(t, context.Background(), ContextFromOptions()) + assert.Nil(t, InvokingCommandFromOptions()) + assert.Nil(t, MetadataParserFromOptions()) + out, errOut := MetadataOutputCaptureFromOptions() + assert.Nil(t, out) + assert.Nil(t, errOut) + var resolved ExecutionOptions + WithEnvironment([]string{})(&resolved) + require.NotNil(t, resolved.ProcessEnv, "an explicitly empty environment must not become ambient inheritance") + WithEnvironment(nil)(&resolved) + assert.Nil(t, resolved.ProcessEnv) +} diff --git a/pkg/component/host_contracts.go b/pkg/component/host_contracts.go new file mode 100644 index 00000000000..52073c5c300 --- /dev/null +++ b/pkg/component/host_contracts.go @@ -0,0 +1,126 @@ +package component + +import ( + cobra "github.com/spf13/cobra" + + auth "github.com/cloudposse/atmos/pkg/auth" + degradation "github.com/cloudposse/atmos/pkg/degradation" + m "github.com/cloudposse/atmos/pkg/merge" + schema "github.com/cloudposse/atmos/pkg/schema" +) + +type ExecuteDescribeComponentParams struct { + // ResolveSecrets retrieves actual secrets for internal value-producing callers. + // The default preserves credential-free, masked inspection. + ResolveSecrets bool + AtmosConfig *schema.AtmosConfiguration // Optional: Use provided config instead of initializing new one. + Component string + Stack string + ComponentType string // Optional: if set, only try this component type. + ProcessTemplates bool + ProcessYamlFunctions bool + UseMocks bool + Skip []string + AuthManager auth.AuthManager + AuthDisabled bool + ErrorOptions DescribeStacksErrorOptions +} + +type ( + OnErrorMode string + DescribeStacksErrorOptions struct { + // StaticOnly inspects merged configuration without auth, expression evaluation, workspace + // synthesis, or evaluated-map cache reuse. Stack name templates allow only Go's pure built-ins. + StaticOnly bool + // EvaluationPaths optionally narrows list columns to fields within sections. + EvaluationPaths [][]string + OnError OnErrorMode + OnWarning func(degradation.Warning) + // OnProgress, when non-nil, is called once per stack file immediately before it's + // processed — stackFile is the file name, index is 0-based, total is the number of + // stack files describe-stacks will visit this call (both counted before filtering by + // includeEmptyStacks, so total is a stable upper bound even though some files are + // skipped). Bundled onto this struct (rather than a new parameter) purely for plumbing + // convenience — it shares the same threading path as OnError/OnWarning. Optional; a nil + // OnProgress is a no-op, matching every existing caller's behavior exactly. + OnProgress func(stackFile string, index, total int) + // StrictAuth, when true, keeps a component-specific auth-manager construction failure + // fatal even under OnErrorWarn (see describeStacksProcessor.resolveComponentAuthManager) -- + // only the recoverable per-value YAML-function errors (e.g. !terraform.state hitting a + // not-yet-provisioned backend) degrade. The zero value (false) preserves the original + // list/describe --error-mode=warn behavior, where a component whose own declared identity + // can't be resolved gracefully falls back to the parent AuthManager instead of aborting -- + // appropriate for merely listing/describing stacks, but not for a Terraform preflight + // about to run real commands with whatever credentials it resolves. + StrictAuth bool + // ExtraComponentTypes names additional component kinds, such as custom kinds declared + // by custom commands, to process after the built-in kinds. Each kind must already be + // registered with the component registry for its stack section to be available. The + // zero value leaves the described component kinds exactly as before. + ExtraComponentTypes []string + } +) + +type DescribeAffectedCmdArgs struct { + CLIConfig *schema.AtmosConfiguration + Base string // Unified base commit (ref or SHA). Takes precedence over Ref/SHA. + CloneTargetRef bool + Format string + IncludeDependents bool + IncludeSettings bool + IncludeSpaceliftAdminStacks bool + OutputFile string + GithubOutputFile string // Output file for $GITHUB_OUTPUT format (key=value). + Ref string + RepoPath string + SHA string + SSHKeyPath string + SSHKeyPassword string + Verbose bool + Upload bool + Stack string + Query string + ProcessTemplates bool + ProcessYamlFunctions bool + Skip []string + ExcludeLocked bool + Tags []string // Keep only components whose `metadata.tags` contain any of these (from --tags). + LabelsRaw string // Raw --labels value (comma-separated key=value pairs). + Labels map[string]string // Parsed --labels: keep only components whose `metadata.labels` contain all of these. + TagsEnvVar string // Environment variable that supplied Tags (for example ATMOS_TAGS); empty when Tags came from the command line or are unset. + LabelsEnvVar string // Environment variable that supplied Labels; empty when Labels came from the command line or are unset. + Flatten bool // Lift every dependent into the top-level list as `affected: dependent` (from --flatten); requires IncludeDependents. + FlattenEnvVar string // Environment variable that supplied Flatten; empty when it came from the command line or is unset. + AuthManager auth.AuthManager // Optional: Auth manager for credential management (from --identity flag). + AuthDisabled bool // True when --identity=false (or alias) explicitly disables authentication; routes stack resolution to ExecuteDescribeStacksWithAuthDisabled. + HeadSHAOverride string // PR head SHA from CI event payload, used for upload correlation with Atmos Pro. + CIEventType string // CI event type (e.g., "pull_request", "push") for upload validation. + TargetBranch string // PR target branch (e.g., "main") used to auto-fetch when refs are missing locally. + ErrorMode string // How to handle recoverable errors: "strict" (default), "warn", or "silent". + Cmd *cobra.Command // The invoking Cobra command, used to derive Flags for the exec-metadata sync capture (proexec.FlagsFromCommand). +} + +const ( + OnErrorStrict OnErrorMode = "strict" + OnErrorWarn OnErrorMode = "warn" +) + +// ComponentDeferredContexts holds the per-section *merge.DeferredMergeContext produced while +// merging a single component's configuration, keyed by section name (vars, settings, env, auth, +// providers, required_providers, hooks, test, generate — see the cfg.*SectionName constants used +// as keys). Each context tracks the deferred YAML functions (!template, !terraform.output, +// !labels, etc.) collected for that section during the raw structural merge; a later, +// per-invocation stage resolves them with a real YAMLFunctionProcessor and deep-merges the result +// against any concrete override at the same path. +type ComponentDeferredContexts map[string]*m.DeferredMergeContext + +// StackComponentDeferredContexts holds, for a single stack, every component's +// ComponentDeferredContexts keyed by [componentType][component] (e.g. +// ["terraform"]["vpc"]). It is the shape ProcessStackConfig produces per stack. +type StackComponentDeferredContexts map[string]map[string]ComponentDeferredContexts + +// AllStacksDeferredContexts holds every stack's deferred contexts keyed by +// [stack][componentType][component]. This is the shape threaded from +// ProcessYAMLConfigFiles through FindStacksMap to the per-invocation Stage 3 +// resolver (see resolveDeferredYamlFunctions). +type AllStacksDeferredContexts map[string]StackComponentDeferredContexts diff --git a/pkg/component/provider.go b/pkg/component/provider.go index 92f84712855..a94d1360e1f 100644 --- a/pkg/component/provider.go +++ b/pkg/component/provider.go @@ -49,7 +49,11 @@ type ComponentProvider interface { type ExecutionContext struct { // Context carries caller cancellation and deadlines into component execution. // Use GoContext so legacy callers that omit it retain background behavior. - Context context.Context + Context context.Context + // Options carries process streams, environment, and reporting through provider dispatch. + Options []ExecutionOption + // WorkingDir is required by providers that generate artifacts for a prepared component. + WorkingDir string AtmosConfig *schema.AtmosConfiguration ComponentType string Component string diff --git a/internal/exec/terraform_affected.go b/pkg/component/terraform/affected.go similarity index 56% rename from internal/exec/terraform_affected.go rename to pkg/component/terraform/affected.go index 52bf2ca42c7..51084566b06 100644 --- a/internal/exec/terraform_affected.go +++ b/pkg/component/terraform/affected.go @@ -1,10 +1,12 @@ -package exec +package terraform import ( "context" "fmt" errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/auth" + "github.com/cloudposse/atmos/pkg/component" cfg "github.com/cloudposse/atmos/pkg/config" log "github.com/cloudposse/atmos/pkg/logger" "github.com/cloudposse/atmos/pkg/perf" @@ -47,113 +49,22 @@ func FilterTerraformAffected(affectedList []schema.Affected) []schema.Affected { return filtered } -// GetAffectedComponents retrieves the list of affected components based on the -// provided arguments, dispatching to the right git-target resolution mode -// (repo path / clone-and-diff / local ref checkout). Exported so other -// affected-target dispatchers (e.g. `atmos terraform lint --affected`) reuse -// this switch instead of re-declaring it against the same three -// ExecuteDescribeAffectedWith* functions. -func GetAffectedComponents(args *DescribeAffectedCmdArgs) ([]schema.Affected, error) { - defer perf.Track(nil, "exec.GetAffectedComponents")() - - return GetAffectedComponentsWithOptions(args, DescribeStacksErrorOptions{}) -} - -// GetAffectedComponentsWithOptions is GetAffectedComponents plus opt-in graceful degradation -// for recoverable per-value YAML function errors (see DescribeStacksErrorOptions), dispatching -// to the *WithOptions variant of whichever git-target resolution mode applies. -func GetAffectedComponentsWithOptions(args *DescribeAffectedCmdArgs, errOptions DescribeStacksErrorOptions) ([]schema.Affected, error) { - defer perf.Track(nil, "exec.GetAffectedComponentsWithOptions")() - - if args == nil { - return nil, errUtils.ErrNilParam - } - return dispatchAffectedComponentsWithOptions(args, errOptions) -} - -// dispatchAffectedComponentsWithOptions is GetAffectedComponentsWithOptions's git-target -// dispatch switch, split out so the exported entry point stays a short nil-check + delegate. -func dispatchAffectedComponentsWithOptions(args *DescribeAffectedCmdArgs, errOptions DescribeStacksErrorOptions) ([]schema.Affected, error) { - switch { - case args.RepoPath != "": - affectedList, _, _, _, err := ExecuteDescribeAffectedWithTargetRepoPathWithOptions( - args.CLIConfig, - args.RepoPath, - args.IncludeSpaceliftAdminStacks, - args.IncludeSettings, - args.Stack, - args.ProcessTemplates, - args.ProcessYamlFunctions, - args.Skip, - args.affectedFilter(), - args.AuthManager, - args.AuthDisabled, - errOptions, - ) - return affectedList, err - case args.CloneTargetRef: - affectedList, _, _, _, err := ExecuteDescribeAffectedWithTargetRefCloneWithOptions( - args.CLIConfig, - args.Ref, - args.SHA, - args.SSHKeyPath, - args.SSHKeyPassword, - args.IncludeSpaceliftAdminStacks, - args.IncludeSettings, - args.Stack, - args.ProcessTemplates, - args.ProcessYamlFunctions, - args.Skip, - args.affectedFilter(), - args.AuthManager, - args.AuthDisabled, - errOptions, - ) - return affectedList, err - default: - affectedList, _, _, _, err := ExecuteDescribeAffectedWithTargetRefCheckoutWithOptions( - args.CLIConfig, - args.Ref, - args.SHA, - args.TargetBranch, - args.IncludeSpaceliftAdminStacks, - args.IncludeSettings, - args.Stack, - args.ProcessTemplates, - args.ProcessYamlFunctions, - args.Skip, - args.affectedFilter(), - args.AuthManager, - args.AuthDisabled, - errOptions, - ) - return affectedList, err - } -} - // ExecuteTerraformAffected executes `atmos terraform --affected`. -func ExecuteTerraformAffected(args *DescribeAffectedCmdArgs, info *schema.ConfigAndStacksInfo) error { +func (e *Executor) ExecuteTerraformAffected(args *component.DescribeAffectedCmdArgs, info *schema.ConfigAndStacksInfo) error { defer perf.Track(nil, "exec.ExecuteTerraformAffected")() - return ExecuteTerraformAffectedWithContext(context.Background(), args, info) + return e.ExecuteTerraformAffectedWithContext(context.Background(), args, info) } // ExecuteTerraformAffectedWithContext executes affected Terraform components through // the shared graph-backed scheduler path. -func ExecuteTerraformAffectedWithContext(ctx context.Context, args *DescribeAffectedCmdArgs, info *schema.ConfigAndStacksInfo) error { +func (e *Executor) ExecuteTerraformAffectedWithContext(ctx context.Context, args *component.DescribeAffectedCmdArgs, info *schema.ConfigAndStacksInfo) error { defer perf.Track(nil, "exec.ExecuteTerraformAffectedWithContext")() - if args == nil { - return errUtils.ErrNilParam - } - if info == nil { + if args == nil || info == nil { return errUtils.ErrNilParam } - authDisabled := args.AuthDisabled || info.AuthDisabled || info.Identity == cfg.IdentityFlagDisabledValue - if authDisabled { - info.Identity = cfg.IdentityFlagDisabledValue - info.AuthDisabled = true - } + authDisabled := applyAffectedAuthDisable(args, info) atmosConfig, err := cfg.InitCliConfig(*info, true) if err != nil { @@ -165,14 +76,14 @@ func ExecuteTerraformAffectedWithContext(ctx context.Context, args *DescribeAffe return err } if authManager != nil { - injectTerraformStoreAuthResolver(&atmosConfig, info, authManager) + e.injectTerraformStoreAuthResolver(&atmosConfig, info, authManager) } args.CLIConfig = &atmosConfig args.AuthManager = authManager args.AuthDisabled = authDisabled - affectedList, err := GetAffectedComponents(args) + affectedList, err := e.services.GetAffectedComponents(args) if err != nil { return err } @@ -193,21 +104,7 @@ func ExecuteTerraformAffectedWithContext(ctx context.Context, args *DescribeAffe } log.Debug("Affected", "components", affectedYaml) - stacks, err := ExecuteDescribeStacksWithAuthDisabledAndMocks( - &atmosConfig, - "", // all stacks; the affected selector already constrains direct matches - nil, // all components; graph filtering applies the selected affected set - []string{cfg.TerraformComponentType}, - nil, - false, - info.ProcessTemplates, - info.ProcessFunctions, - false, - info.Skip, - authManager, - authDisabled, - info.UseMocks, - ) + stacks, err := e.describeAffectedTerraformStacks(&atmosConfig, info, authManager, authDisabled) if err != nil { return err } @@ -216,7 +113,7 @@ func ExecuteTerraformAffectedWithContext(ctx context.Context, args *DescribeAffe AtmosConfig: &atmosConfig, Info: info, Stacks: stacks, - Executor: executeTerraformQueryComponent, + Executor: e.executeTerraformQueryComponent, Selection: affectedTerraformSelection(affectedList, args, info), }) } @@ -230,7 +127,7 @@ func ExecuteTerraformAffectedWithContext(ctx context.Context, args *DescribeAffe // user-supplied --include-dependents=N bound. When the bool is set without a // flag-side depth (e.g. dependents forced programmatically), zero is kept and // correctly means unlimited. -func affectedTerraformSelection(affectedList []schema.Affected, args *DescribeAffectedCmdArgs, info *schema.ConfigAndStacksInfo) *scheduleradapters.TerraformSelection { +func affectedTerraformSelection(affectedList []schema.Affected, args *component.DescribeAffectedCmdArgs, info *schema.ConfigAndStacksInfo) *scheduleradapters.TerraformSelection { selection := &scheduleradapters.TerraformSelection{ NodeIDs: extractAffectedNodeIDs(affectedList), IncludeDependents: args.IncludeDependents, @@ -240,3 +137,33 @@ func affectedTerraformSelection(affectedList []schema.Affected, args *DescribeAf } return selection } + +// applyAffectedAuthDisable preserves an explicit authentication opt-out for all selected nodes. +func applyAffectedAuthDisable(args *component.DescribeAffectedCmdArgs, info *schema.ConfigAndStacksInfo) bool { + authDisabled := args.AuthDisabled || info.AuthDisabled || info.Identity == cfg.IdentityFlagDisabledValue + if authDisabled { + info.Identity = cfg.IdentityFlagDisabledValue + info.AuthDisabled = true + } + + return authDisabled +} + +// describeAffectedTerraformStacks evaluates the full graph before applying the affected selection. +func (e *Executor) describeAffectedTerraformStacks(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, authManager auth.AuthManager, authDisabled bool) (map[string]any, error) { + return e.services.ExecuteDescribeStacksWithAuthDisabledAndMocks( + atmosConfig, + "", // all stacks; the affected selector already constrains direct matches + nil, // all components; graph filtering applies the selected affected set + []string{cfg.TerraformComponentType}, + nil, + false, + info.ProcessTemplates, + info.ProcessFunctions, + false, + info.Skip, + authManager, + authDisabled, + info.UseMocks, + ) +} diff --git a/internal/exec/terraform_affected_filter_test.go b/pkg/component/terraform/affected_filter_test.go similarity index 99% rename from internal/exec/terraform_affected_filter_test.go rename to pkg/component/terraform/affected_filter_test.go index 9dca9afe32f..5ee98295943 100644 --- a/internal/exec/terraform_affected_filter_test.go +++ b/pkg/component/terraform/affected_filter_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "testing" diff --git a/pkg/component/terraform/affected_test.go b/pkg/component/terraform/affected_test.go new file mode 100644 index 00000000000..2d47b609415 --- /dev/null +++ b/pkg/component/terraform/affected_test.go @@ -0,0 +1,335 @@ +package terraform + +import ( + context "context" + errors "errors" + filepath "path/filepath" + testing "testing" + + errUtils "github.com/cloudposse/atmos/errors" + auth "github.com/cloudposse/atmos/pkg/auth" + types "github.com/cloudposse/atmos/pkg/auth/types" + component "github.com/cloudposse/atmos/pkg/component" + cfg "github.com/cloudposse/atmos/pkg/config" + scheduleradapters "github.com/cloudposse/atmos/pkg/scheduler/adapters" + schema "github.com/cloudposse/atmos/pkg/schema" + assert "github.com/stretchr/testify/assert" + require "github.com/stretchr/testify/require" + gomock "go.uber.org/mock/gomock" +) + +func TestExtractAffectedNodeIDs(t *testing.T) { + t.Run("empty list", func(t *testing.T) { + ids := extractAffectedNodeIDs(nil) + assert.Empty(t, ids) + }) + + t.Run("single affected", func(t *testing.T) { + affected := []schema.Affected{ + {Component: "vpc", Stack: "dev"}, + } + ids := extractAffectedNodeIDs(affected) + assert.Equal(t, []string{"vpc-dev"}, ids) + }) + + t.Run("multiple affected", func(t *testing.T) { + affected := []schema.Affected{ + {Component: "vpc", Stack: "dev"}, + {Component: "rds", Stack: "prod"}, + {Component: "app", Stack: "staging"}, + } + ids := extractAffectedNodeIDs(affected) + assert.Equal(t, 3, len(ids)) + assert.Equal(t, "vpc-dev", ids[0]) + assert.Equal(t, "rds-prod", ids[1]) + assert.Equal(t, "app-staging", ids[2]) + }) +} + +// TestAffectedTerraformSelection verifies the selection carries the parsed +// --include-dependents depth from info: a zero DependentDepth means unlimited +// in dependency.Filter encoding, so omitting it silently erased a user-supplied +// depth bound on the --affected path (the closure spec keeps the most +// permissive depth when merging selection and info). +func TestAffectedTerraformSelection(t *testing.T) { + affected := []schema.Affected{{Component: "vpc", Stack: "dev"}} + + tests := []struct { + name string + argsIncludeDependents bool + infoIncludeDependents int + wantInclude bool + wantDepth int + }{ + { + name: "flag off", + argsIncludeDependents: false, + infoIncludeDependents: 0, + wantInclude: false, + wantDepth: 0, + }, + { + name: "bare flag is unlimited", + argsIncludeDependents: true, + infoIncludeDependents: -1, + wantInclude: true, + wantDepth: 0, + }, + { + name: "numeric depth is carried through, not erased to unlimited", + argsIncludeDependents: true, + infoIncludeDependents: 2, + wantInclude: true, + wantDepth: 2, + }, + { + name: "programmatically forced dependents without flag depth stay unlimited", + argsIncludeDependents: true, + infoIncludeDependents: 0, + wantInclude: true, + wantDepth: 0, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + args := &DescribeAffectedCmdArgs{IncludeDependents: tc.argsIncludeDependents} + info := &schema.ConfigAndStacksInfo{IncludeDependents: tc.infoIncludeDependents} + + selection := affectedTerraformSelection(affected, args, info) + + assert.Equal(t, []string{"vpc-dev"}, selection.NodeIDs) + assert.Equal(t, tc.wantInclude, selection.IncludeDependents) + assert.Equal(t, tc.wantDepth, selection.DependentDepth) + }) + } + + t.Run("nil info leaves depth unlimited", func(t *testing.T) { + args := &DescribeAffectedCmdArgs{IncludeDependents: true} + selection := affectedTerraformSelection(affected, args, nil) + assert.True(t, selection.IncludeDependents) + assert.Equal(t, 0, selection.DependentDepth) + }) +} + +func TestAffectedExecutorRejectsMissingRequest(t *testing.T) { + executor := NewExecutor(Services{}) + require.ErrorIs(t, executor.ExecuteTerraformAffected(nil, &schema.ConfigAndStacksInfo{}), errUtils.ErrNilParam) + require.ErrorIs(t, executor.ExecuteTerraformAffected(&component.DescribeAffectedCmdArgs{}, nil), errUtils.ErrNilParam) +} + +func TestAffectedExecutorPreservesServiceBoundaries(t *testing.T) { + t.Chdir(filepath.Join("..", "..", "..", "tests", "fixtures", "scenarios", "terraform-apply-affected")) + t.Setenv("ATMOS_BASE_PATH", "") + t.Setenv("ATMOS_CLI_CONFIG_PATH", "") + for _, outcome := range []string{"lookup error", "empty after filtering", "describe error", "scheduled"} { + t.Run(outcome, func(t *testing.T) { + executor := testExecutor(t) + sentinel := errors.New("host service unavailable") + args := &component.DescribeAffectedCmdArgs{Stack: "prod", AuthDisabled: true} + info := &schema.ConfigAndStacksInfo{ComponentType: "terraform", SubCommand: "plan", Stack: "prod", ProcessTemplates: true, ProcessFunctions: true, UseMocks: true, Skip: []string{"terraform.state"}} + lookedUp, described, executed := false, false, false + executor.services.GetAffectedComponents = func(actual *component.DescribeAffectedCmdArgs) ([]schema.Affected, error) { + lookedUp = true + require.Same(t, args, actual) + require.NotNil(t, actual.CLIConfig) + require.True(t, actual.AuthDisabled) + require.Nil(t, actual.AuthManager) + if outcome == "lookup error" { + return nil, sentinel + } + result := []schema.Affected{{Component: "chart", Stack: "prod", ComponentType: "helmfile"}, {Component: "removed", Stack: "prod", ComponentType: "terraform", Deleted: true}} + if outcome != "empty after filtering" { + result = append(result, schema.Affected{Component: "vpc", Stack: "prod", ComponentType: "terraform"}) + } + return result, nil + } + executor.services.ExecuteDescribeStacksWithAuthDisabledAndMocks = func(ac *schema.AtmosConfiguration, stack string, components, types, sections []string, ignoreMissing, templates, functions, empty bool, skip []string, manager auth.AuthManager, disabled, mocks bool) (map[string]any, error) { + described = true + require.Same(t, args.CLIConfig, ac) + require.Empty(t, stack) + require.Nil(t, components) + require.Equal(t, []string{"terraform"}, types) + require.Nil(t, sections) + require.False(t, ignoreMissing) + require.True(t, templates) + require.True(t, functions) + require.False(t, empty) + require.Equal(t, info.Skip, skip) + require.Nil(t, manager) + require.True(t, disabled) + require.True(t, mocks) + if outcome == "describe error" { + return nil, sentinel + } + return map[string]any{"prod": map[string]any{"components": map[string]any{"terraform": map[string]any{"vpc": map[string]any{"vars": map[string]any{}}, "unrelated": map[string]any{"vars": map[string]any{}}}}}}, nil + } + executor.services.ExecuteTerraform = func(actual schema.ConfigAndStacksInfo, opts ...component.ExecutionOption) error { + executed = true + require.Equal(t, "vpc", actual.ComponentFromArg) + require.Equal(t, "prod", actual.Stack) + require.Equal(t, "plan", actual.SubCommand) + require.True(t, actual.AuthDisabled) + require.Equal(t, cfg.IdentityFlagDisabledValue, actual.Identity) + require.NoError(t, component.ContextFromOptions(opts...).Err()) + return nil + } + err := executor.ExecuteTerraformAffectedWithContext(context.Background(), args, info) + if outcome == "lookup error" || outcome == "describe error" { + require.ErrorIs(t, err, sentinel) + } else { + require.NoError(t, err) + } + require.True(t, lookedUp) + require.Equal(t, outcome == "describe error" || outcome == "scheduled", described) + require.Equal(t, outcome == "scheduled", executed) + require.True(t, info.AuthDisabled) + require.Equal(t, cfg.IdentityFlagDisabledValue, info.Identity) + }) + } +} + +// TestCreateQueryAuthManager verifies createQueryAuthManager behavior across +// different scenarios using injected auth factory to cover all branches. +func TestCreateQueryAuthManager(t *testing.T) { + tests := []struct { + name string + setupMock func(ctrl *gomock.Controller, m *MockAuthManagerQueryFactory) + expectErr bool + expectSentinel error + expectNilMgr bool + expectInfoStored bool + expectExit bool + }{ + { + name: "no auth configured returns nil manager", + setupMock: func(_ *gomock.Controller, m *MockAuthManagerQueryFactory) { + m.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil) + }, + expectNilMgr: true, + expectInfoStored: false, + }, + { + name: "nonexistent identity returns wrapped error", + setupMock: func(_ *gomock.Controller, m *MockAuthManagerQueryFactory) { + m.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errUtils.ErrAuthNotConfigured) + }, + expectErr: true, + expectSentinel: errUtils.ErrAuthNotConfigured, + expectNilMgr: true, + expectInfoStored: false, + }, + { + name: "non-nil manager is stored in info", + setupMock: func(ctrl *gomock.Controller, m *MockAuthManagerQueryFactory) { + m.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(types.NewMockAuthManager(ctrl), nil) + }, + expectNilMgr: false, + expectInfoStored: true, + }, + { + name: "ErrUserAborted calls Exit with SIGINT code", + setupMock: func(_ *gomock.Controller, m *MockAuthManagerQueryFactory) { + m.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, errUtils.ErrUserAborted) + }, + expectExit: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + // Inject test factory. + ctrl := gomock.NewController(t) + mockFactory := NewMockAuthManagerQueryFactory(ctrl) + tt.setupMock(ctrl, mockFactory) + original := authManagerFactory + authManagerFactory = mockFactory + defer func() { authManagerFactory = original }() + + // Mock os.Exit to capture exit calls. + var exitCode int + exitCalled := false + originalExit := errUtils.OsExit + errUtils.OsExit = func(code int) { + exitCode = code + exitCalled = true + } + defer func() { errUtils.OsExit = originalExit }() + + info := &schema.ConfigAndStacksInfo{} + atmosConfig := &schema.AtmosConfiguration{ + BasePath: t.TempDir(), + } + + authManager, err := createQueryAuthManager(info, atmosConfig) + + if tt.expectExit { + assert.True(t, exitCalled, "Exit should have been called") + assert.Equal(t, errUtils.ExitCodeSIGINT, exitCode) + return + } + + assert.False(t, exitCalled, "Exit should not have been called") + + if tt.expectErr { + assert.Error(t, err) + if tt.expectSentinel != nil { + assert.ErrorIs(t, err, tt.expectSentinel) + } + } else { + assert.NoError(t, err) + } + + if tt.expectNilMgr { + assert.Nil(t, authManager) + } else { + assert.NotNil(t, authManager) + } + + if tt.expectInfoStored { + assert.NotNil(t, info.AuthManager, "AuthManager should be stored in info") + } else { + assert.Nil(t, info.AuthManager, "AuthManager should not be stored in info") + } + }) + } +} + +// TestExecuteTerraformQueryComponent covers testExecutor(t).executeTerraformQueryComponent +// directly — it has no test at all today (only testExecutor(t).ExecuteTerraformQuery / +// testExecutor(t).ExecuteTerraformQueryWithContext, which call it indirectly through the +// scheduler, are exercised elsewhere). A nonexistent component/stack makes +// the underlying ExecuteTerraform fail fast during stack processing, without +// needing a real terraform/tofu binary, so both CaptureOutput branches can be +// asserted directly. +func TestExecuteTerraformQueryComponent(t *testing.T) { + t.Chdir(filepath.Join("..", "..", "..", "examples", "demo-stacks")) + + tests := []struct { + name string + captureOutput bool + }{ + {name: "without output capture", captureOutput: false}, + {name: "with output capture", captureOutput: true}, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + execution := scheduleradapters.TerraformExecution{ + Context: context.Background(), + Info: schema.ConfigAndStacksInfo{ + ComponentFromArg: "nonexistent", + Stack: "nonexistent", + SubCommand: "plan", + }, + CaptureOutput: tc.captureOutput, + } + + result, err := testExecutor(t).executeTerraformQueryComponent(execution) + + require.Error(t, err, "a nonexistent component/stack must fail during stack processing") + assert.Empty(t, result.Stdout, "no shell command ever runs, so nothing is captured") + assert.Empty(t, result.Stderr) + }) + } +} diff --git a/internal/exec/terraform_all.go b/pkg/component/terraform/all.go similarity index 87% rename from internal/exec/terraform_all.go rename to pkg/component/terraform/all.go index d60d4ab7bb1..5781607e535 100644 --- a/internal/exec/terraform_all.go +++ b/pkg/component/terraform/all.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" @@ -7,7 +7,9 @@ import ( errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/auth" + "github.com/cloudposse/atmos/pkg/component" cfg "github.com/cloudposse/atmos/pkg/config" + degradation "github.com/cloudposse/atmos/pkg/degradation" "github.com/cloudposse/atmos/pkg/dependency" listdeps "github.com/cloudposse/atmos/pkg/list/dependencies" log "github.com/cloudposse/atmos/pkg/logger" @@ -24,14 +26,14 @@ import ( const errWrapFmt = "%w: %w" // ExecuteTerraformAll executes terraform commands for all components in dependency order. -func ExecuteTerraformAll(info *schema.ConfigAndStacksInfo) error { +func (e *Executor) ExecuteTerraformAll(info *schema.ConfigAndStacksInfo) error { defer perf.Track(nil, "exec.ExecuteTerraformAll")() - return ExecuteTerraformAllWithContext(context.Background(), info) + return e.ExecuteTerraformAllWithContext(context.Background(), info) } // ExecuteTerraformAllWithContext executes all selected Terraform components through // the graph-backed scheduler using the provided cancellation context. -func ExecuteTerraformAllWithContext(ctx context.Context, info *schema.ConfigAndStacksInfo) error { +func (e *Executor) ExecuteTerraformAllWithContext(ctx context.Context, info *schema.ConfigAndStacksInfo) error { defer perf.Track(nil, "exec.ExecuteTerraformAllWithContext")() // Validate inputs for --all flag usage. // When no stack is given, --all processes every stack — matching the documented @@ -63,11 +65,11 @@ func ExecuteTerraformAllWithContext(ctx context.Context, info *schema.ConfigAndS return err } if authManager != nil { - injectTerraformStoreAuthResolver(&atmosConfig, info, authManager) + e.injectTerraformStoreAuthResolver(&atmosConfig, info, authManager) } preflight.Update("Resolving Terraform component instances, secrets, and state references") - stacks, err = describeTerraformStacksForExecution(&atmosConfig, info, authManager, nil) + stacks, err = e.describeTerraformStacksForExecution(&atmosConfig, info, authManager, nil) if err != nil { preflight.Error("Failed to resolve Terraform component instances") return terraformPreflightDescribeError(err) @@ -84,7 +86,7 @@ func ExecuteTerraformAllWithContext(ctx context.Context, info *schema.ConfigAndS AtmosConfig: &atmosConfig, Info: info, Stacks: stacks, - Executor: executeTerraformQueryComponent, + Executor: e.executeTerraformQueryComponent, }) } @@ -108,9 +110,9 @@ func terraformClosureRequested(info *schema.ConfigAndStacksInfo) bool { // the stacks the reachable closure touches. The scheduler adapter then // re-applies the selection filters as the seed and expands the closure on the // resulting graph, so the evaluation scope and execution set always agree. -func describeTerraformStacksForExecution(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, authManager auth.AuthManager, components []string) (map[string]any, error) { +func (e *Executor) describeTerraformStacksForExecution(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, authManager auth.AuthManager, components []string) (map[string]any, error) { describe := func(stackName string, closureComponents []string, processTemplates, processFunctions bool) (map[string]any, error) { - return ExecuteDescribeStacksWithMocks( + return e.services.ExecuteDescribeStacksWithMocks( atmosConfig, stackName, closureComponents, // engine-supplied closure members (nil = all); never the caller's own selection. @@ -130,7 +132,7 @@ func describeTerraformStacksForExecution(atmosConfig *schema.AtmosConfiguration, } if !terraformClosureRequested(info) { - return describeTerraformStacksNarrowed(atmosConfig, info, authManager, components) + return e.describeTerraformStacksNarrowed(atmosConfig, info, authManager, components) } // Closure requested. Scoped evaluation only pays off when the selection is @@ -138,7 +140,7 @@ func describeTerraformStacksForExecution(atmosConfig *schema.AtmosConfiguration, // actually on, and eager evaluation was not forced as the escape hatch. bounded := info.Stack != "" || len(components) > 0 || len(info.Tags) > 0 || len(info.Labels) > 0 needsEvaluation := info.ProcessTemplates || info.ProcessFunctions - if !bounded || !needsEvaluation || GetEagerEvaluationSetting(atmosConfig) { + if !bounded || !needsEvaluation || e.services.GetEagerEvaluationSetting(atmosConfig) { return describe("", nil, info.ProcessTemplates, info.ProcessFunctions) } @@ -164,8 +166,8 @@ func describeTerraformStacksForExecution(atmosConfig *schema.AtmosConfiguration, // describeTerraformStacksNarrowed is the historical (no-closure) describe: // narrowed by -s/--components and the tags/labels early-skip, bit for bit. -func describeTerraformStacksNarrowed(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, authManager auth.AuthManager, components []string) (map[string]any, error) { - return ExecuteDescribeStacksWithMocks( +func (e *Executor) describeTerraformStacksNarrowed(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, authManager auth.AuthManager, components []string) (map[string]any, error) { + return e.services.ExecuteDescribeStacksWithMocks( atmosConfig, info.Stack, components, @@ -199,11 +201,11 @@ func describeTerraformStacksNarrowed(atmosConfig *schema.AtmosConfiguration, inf // falling back to a parent AuthManager and going on to attempt a real, and much slower and // more confusing, network call with the wrong credentials — the list/describe --error-mode=warn // behavior that plain YAML-function degradation shares this mechanism with. -func terraformPreflightErrorOptions() DescribeStacksErrorOptions { - return DescribeStacksErrorOptions{ - OnError: OnErrorWarn, +func terraformPreflightErrorOptions() component.DescribeStacksErrorOptions { + return component.DescribeStacksErrorOptions{ + OnError: component.OnErrorWarn, StrictAuth: true, - OnWarning: func(w DegradationWarning) { + OnWarning: func(w degradation.Warning) { log.Debug( "Deferring unresolved value until its dependency is applied", cfg.ComponentStr, w.Component, @@ -235,7 +237,7 @@ func terraformPreflightDescribeError(cause error) error { } // buildTerraformDependencyGraph builds the complete dependency graph from stacks. -func buildTerraformDependencyGraph( +func (e *Executor) buildTerraformDependencyGraph( atmosConfig *schema.AtmosConfiguration, stacks map[string]any, _ *schema.ConfigAndStacksInfo, @@ -244,12 +246,12 @@ func buildTerraformDependencyGraph( nodeMap := make(map[string]string) // Maps component-stack to node ID // First pass: add all nodes. - if err := addNodesToGraph(stacks, builder, nodeMap); err != nil { + if err := e.addNodesToGraph(stacks, builder, nodeMap); err != nil { return nil, fmt.Errorf("%w: adding nodes: %w", errUtils.ErrBuildDepGraph, err) } // Second pass: build dependencies using settings.depends_on. - if err := buildGraphDependencies(atmosConfig, stacks, builder, nodeMap); err != nil { + if err := e.buildGraphDependencies(atmosConfig, stacks, builder, nodeMap); err != nil { return nil, fmt.Errorf("%w: building dependencies: %w", errUtils.ErrBuildDepGraph, err) } @@ -264,13 +266,13 @@ func buildTerraformDependencyGraph( } // addNodesToGraph adds all component nodes to the dependency graph. -func addNodesToGraph( +func (e *Executor) addNodesToGraph( stacks map[string]any, builder *dependency.GraphBuilder, nodeMap map[string]string, ) error { return walkTerraformComponents(stacks, func(stackName, componentName string, componentSection map[string]any) error { - if shouldSkipComponentForGraph(componentSection, componentName) { + if e.shouldSkipComponentForGraph(componentSection, componentName) { return nil } @@ -289,7 +291,7 @@ func addNodesToGraph( } // buildGraphDependencies builds dependencies between nodes in the graph. -func buildGraphDependencies( +func (e *Executor) buildGraphDependencies( atmosConfig *schema.AtmosConfiguration, stacks map[string]any, builder *dependency.GraphBuilder, @@ -310,10 +312,10 @@ func buildGraphDependencies( return err } leftDelim, _ := tags.TemplateDelims(atmosConfig.Templates.Settings.Delimiters) - parser := NewDependencyParserWithDelimiter(builder, nodeMap, targetStates, leftDelim) + parser := e.services.NewDependencyParserWithDelimiter(builder, nodeMap, targetStates, leftDelim) return walkTerraformComponents(stacks, func(stackName, componentName string, componentSection map[string]any) error { - if shouldSkipComponentForGraph(componentSection, componentName) { + if e.shouldSkipComponentForGraph(componentSection, componentName) { return nil } return parser.ParseComponentDependencies(stackName, componentName, componentSection) @@ -321,7 +323,7 @@ func buildGraphDependencies( } // shouldSkipComponentForGraph determines if a component should be skipped when building the graph. -func shouldSkipComponentForGraph(componentSection map[string]any, componentName string) bool { +func (e *Executor) shouldSkipComponentForGraph(componentSection map[string]any, componentName string) bool { metadataSection, ok := componentSection[cfg.MetadataSectionName].(map[string]any) if !ok { return false @@ -333,7 +335,7 @@ func shouldSkipComponentForGraph(componentSection map[string]any, componentName } // Skip disabled components. - return !isComponentEnabled(metadataSection, componentName) + return !e.services.IsComponentEnabled(metadataSection, componentName) } // applyFiltersToGraph applies query and component filters to the graph. diff --git a/internal/exec/terraform_all_closure_scope_test.go b/pkg/component/terraform/all_closure_scope_test.go similarity index 87% rename from internal/exec/terraform_all_closure_scope_test.go rename to pkg/component/terraform/all_closure_scope_test.go index 948d387a4ac..a3c225ab697 100644 --- a/internal/exec/terraform_all_closure_scope_test.go +++ b/pkg/component/terraform/all_closure_scope_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "os" @@ -12,8 +12,8 @@ import ( ) // TestDescribeTerraformStacksForExecution_ClosureRequested exercises the -// closure-requested branches of describeTerraformStacksForExecution that -// ExecuteTerraformAll's default (--all, no closure flags) tests never reach: +// closure-requested branches of testExecutor(t).describeTerraformStacksForExecution that +// testExecutor(t).ExecuteTerraformAll's default (--all, no closure flags) tests never reach: // the unbounded-selection fallback and the bounded scoped-closure path backed // by pkg/list/dependencies.ResolveScopedClosure. Both run against the real // terraform-apply-affected fixture (vpc -> eks/cluster -> eks/istio/base -> @@ -25,7 +25,7 @@ func TestDescribeTerraformStacksForExecution_ClosureRequested(t *testing.T) { os.Unsetenv("ATMOS_BASE_PATH") os.Unsetenv("ATMOS_CLI_CONFIG_PATH") - t.Chdir(filepath.Join("..", "..", "tests", "fixtures", "scenarios", "terraform-apply-affected")) + t.Chdir(filepath.Join("..", "..", "..", "tests", "fixtures", "scenarios", "terraform-apply-affected")) t.Run("unbounded selection with closure requested falls back to a full describe", func(t *testing.T) { info := &schema.ConfigAndStacksInfo{ @@ -36,7 +36,7 @@ func TestDescribeTerraformStacksForExecution_ClosureRequested(t *testing.T) { atmosConfig, err := cfg.InitCliConfig(*info, true) require.NoError(t, err) - stacks, err := describeTerraformStacksForExecution(&atmosConfig, info, nil, nil) + stacks, err := testExecutor(t).describeTerraformStacksForExecution(&atmosConfig, info, nil, nil) require.NoError(t, err) // Unbounded: every stack the fixture defines comes back, matching the @@ -62,7 +62,7 @@ func TestDescribeTerraformStacksForExecution_ClosureRequested(t *testing.T) { atmosConfig, err := cfg.InitCliConfig(*info, true) require.NoError(t, err) - stacks, err := describeTerraformStacksForExecution(&atmosConfig, info, nil, components) + stacks, err := testExecutor(t).describeTerraformStacksForExecution(&atmosConfig, info, nil, components) require.NoError(t, err) // Bounded + evaluation requested + eager off routes through @@ -83,7 +83,7 @@ func TestDescribeTerraformStacksForExecution_ClosureRequested(t *testing.T) { } // TestDescribeTerraformStacksForExecution_ClosureErrorPropagates proves -// describeTerraformStacksForExecution surfaces a genuine evaluation error from +// testExecutor(t).describeTerraformStacksForExecution surfaces a genuine evaluation error from // listdeps.ResolveScopedClosure instead of swallowing it: seeding by the // `broken-tag` tag (list-components-closure-tags fixture) pulls in a // component whose template unconditionally fails to render once evaluated. @@ -93,7 +93,7 @@ func TestDescribeTerraformStacksForExecution_ClosureErrorPropagates(t *testing.T os.Unsetenv("ATMOS_BASE_PATH") os.Unsetenv("ATMOS_CLI_CONFIG_PATH") - t.Chdir(filepath.Join("..", "..", "tests", "fixtures", "scenarios", "list-components-closure-tags")) + t.Chdir(filepath.Join("..", "..", "..", "tests", "fixtures", "scenarios", "list-components-closure-tags")) info := &schema.ConfigAndStacksInfo{ ComponentType: "terraform", @@ -105,7 +105,7 @@ func TestDescribeTerraformStacksForExecution_ClosureErrorPropagates(t *testing.T atmosConfig, err := cfg.InitCliConfig(*info, true) require.NoError(t, err) - _, err = describeTerraformStacksForExecution(&atmosConfig, info, nil, nil) + _, err = testExecutor(t).describeTerraformStacksForExecution(&atmosConfig, info, nil, nil) require.Error(t, err) require.Contains(t, err.Error(), "broken component must only be evaluated when explicitly seeded") } @@ -168,7 +168,7 @@ components: atmosConfig, err := cfg.InitCliConfig(*info, true) require.NoError(t, err) - stacks, err := describeTerraformStacksForExecution(&atmosConfig, info, nil, nil) + stacks, err := testExecutor(t).describeTerraformStacksForExecution(&atmosConfig, info, nil, nil) require.NoError(t, err) require.ElementsMatch(t, []string{"app"}, terraformComponentNames(t, stacks, "dev")) } diff --git a/internal/exec/terraform_all_dependency_bootstrap_test.go b/pkg/component/terraform/all_dependency_bootstrap_test.go similarity index 97% rename from internal/exec/terraform_all_dependency_bootstrap_test.go rename to pkg/component/terraform/all_dependency_bootstrap_test.go index 4c15e587996..e6f1a8565fb 100644 --- a/internal/exec/terraform_all_dependency_bootstrap_test.go +++ b/pkg/component/terraform/all_dependency_bootstrap_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "os" @@ -93,7 +93,7 @@ stacks: os.Unsetenv("ATMOS_CLI_CONFIG_PATH") t.Chdir(root) - err := ExecuteTerraformAll(&schema.ConfigAndStacksInfo{ + err := testExecutor(t).ExecuteTerraformAll(&schema.ConfigAndStacksInfo{ Stack: "dev", ComponentType: "terraform", SubCommand: "apply", diff --git a/internal/exec/terraform_all_output_cache_test.go b/pkg/component/terraform/all_output_cache_test.go similarity index 96% rename from internal/exec/terraform_all_output_cache_test.go rename to pkg/component/terraform/all_output_cache_test.go index 8afd2993f3e..af68cea0ab9 100644 --- a/internal/exec/terraform_all_output_cache_test.go +++ b/pkg/component/terraform/all_output_cache_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "encoding/json" @@ -106,7 +106,7 @@ output "cluster_id" { value = terraform_data.cluster.id } if existing { // Seed a deployment with individual applies, as in issue #2355. for _, component := range []string{"producer", "consumer"} { - require.NoError(t, ExecuteTerraform(schema.ConfigAndStacksInfo{ + require.NoError(t, testExecutor(t).services.ExecuteTerraform(schema.ConfigAndStacksInfo{ Stack: "test", ComponentType: "terraform", ComponentFromArg: component, @@ -120,7 +120,7 @@ output "cluster_id" { value = terraform_data.cluster.id } applyAll := func() { t.Helper() - require.NoError(t, ExecuteTerraformAll(&schema.ConfigAndStacksInfo{ + require.NoError(t, testExecutor(t).ExecuteTerraformAll(&schema.ConfigAndStacksInfo{ Stack: "test", ComponentType: "terraform", SubCommand: "apply", diff --git a/internal/exec/terraform_all_simple_test.go b/pkg/component/terraform/all_simple_test.go similarity index 93% rename from internal/exec/terraform_all_simple_test.go rename to pkg/component/terraform/all_simple_test.go index 16d923f556b..90dc62d14e1 100644 --- a/internal/exec/terraform_all_simple_test.go +++ b/pkg/component/terraform/all_simple_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "testing" @@ -34,7 +34,7 @@ func TestExecuteTerraformAll_ValidationSimple(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() - err := ExecuteTerraformAll(tt.info) + err := testExecutor(t).ExecuteTerraformAll(tt.info) if tt.expectError { assert.Error(t, err) diff --git a/internal/exec/terraform_all_test.go b/pkg/component/terraform/all_test.go similarity index 93% rename from internal/exec/terraform_all_test.go rename to pkg/component/terraform/all_test.go index 8ab9a3b6a0f..af5003ea16e 100644 --- a/internal/exec/terraform_all_test.go +++ b/pkg/component/terraform/all_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "encoding/json" @@ -66,7 +66,7 @@ func TestBuildTerraformDependencyGraph(t *testing.T) { atmosConfig := &schema.AtmosConfiguration{} info := &schema.ConfigAndStacksInfo{} - graph, err := buildTerraformDependencyGraph(atmosConfig, stacks, info) + graph, err := testExecutor(t).buildTerraformDependencyGraph(atmosConfig, stacks, info) assert.NoError(t, err) assert.NotNil(t, graph) @@ -127,7 +127,7 @@ func TestBuildTerraformDependencyGraph_WithAbstractComponents(t *testing.T) { atmosConfig := &schema.AtmosConfiguration{} info := &schema.ConfigAndStacksInfo{} - graph, err := buildTerraformDependencyGraph(atmosConfig, stacks, info) + graph, err := testExecutor(t).buildTerraformDependencyGraph(atmosConfig, stacks, info) assert.NoError(t, err) assert.NotNil(t, graph) @@ -171,7 +171,7 @@ func TestBuildTerraformDependencyGraph_WithDisabledComponents(t *testing.T) { atmosConfig := &schema.AtmosConfiguration{} info := &schema.ConfigAndStacksInfo{} - graph, err := buildTerraformDependencyGraph(atmosConfig, stacks, info) + graph, err := testExecutor(t).buildTerraformDependencyGraph(atmosConfig, stacks, info) assert.NoError(t, err) assert.NotNil(t, graph) @@ -240,7 +240,7 @@ func TestBuildTerraformDependencyGraphModernDependencies(t *testing.T) { }, } - graph, err := buildTerraformDependencyGraph(&schema.AtmosConfiguration{}, stacks, &schema.ConfigAndStacksInfo{}) + graph, err := testExecutor(t).buildTerraformDependencyGraph(&schema.AtmosConfiguration{}, stacks, &schema.ConfigAndStacksInfo{}) if test.wantErr != nil { require.ErrorIs(t, err, test.wantErr) return @@ -294,7 +294,7 @@ components: atmosConfig.Templates.Settings.Delimiters, ) require.NoError(t, err) - rendered, err := ProcessTmplWithDatasources( + rendered, err := testExecutor(t).services.ProcessTmplWithDatasources( atmosConfig, &schema.ConfigAndStacksInfo{}, settings, @@ -318,7 +318,7 @@ components: }, } - graph, err := buildTerraformDependencyGraph(atmosConfig, stacks, &schema.ConfigAndStacksInfo{}) + graph, err := testExecutor(t).buildTerraformDependencyGraph(atmosConfig, stacks, &schema.ConfigAndStacksInfo{}) require.NoError(t, err) node, exists := graph.GetNode("app-dev") require.True(t, exists) @@ -348,7 +348,7 @@ func TestBuildTerraformDependencyGraphModernCrossStackDependency(t *testing.T) { }, } - graph, err := buildTerraformDependencyGraph(&schema.AtmosConfiguration{}, stacks, &schema.ConfigAndStacksInfo{}) + graph, err := testExecutor(t).buildTerraformDependencyGraph(&schema.AtmosConfiguration{}, stacks, &schema.ConfigAndStacksInfo{}) require.NoError(t, err) app, exists := graph.GetNode("app-dev") require.True(t, exists) @@ -375,7 +375,7 @@ func TestBuildTerraformDependencyGraphModernEmptyPreventsLegacyFallback(t *testi }, } - graph, err := buildTerraformDependencyGraph(&schema.AtmosConfiguration{}, stacks, &schema.ConfigAndStacksInfo{}) + graph, err := testExecutor(t).buildTerraformDependencyGraph(&schema.AtmosConfiguration{}, stacks, &schema.ConfigAndStacksInfo{}) require.NoError(t, err) app, exists := graph.GetNode("app-dev") require.True(t, exists) @@ -474,7 +474,7 @@ func TestExecuteTerraformAll_Validation(t *testing.T) { t.Run(tt.name, func(t *testing.T) { // Mock to avoid actually executing terraform. tt.info.SubCommand = "plan" - err := ExecuteTerraformAll(tt.info) + err := testExecutor(t).ExecuteTerraformAll(tt.info) if tt.expectError { assert.Error(t, err) @@ -488,7 +488,7 @@ func TestExecuteTerraformAll_Validation(t *testing.T) { } } -// TestExecuteTerraformAll_DryRunHappyPath exercises the full ExecuteTerraformAll +// TestExecuteTerraformAll_DryRunHappyPath exercises the full testExecutor(t).ExecuteTerraformAll // pipeline against a real fixture in dry-run mode. The existing validation tests // short-circuit on the very first guard (component-with-all), so the auth-manager // bootstrap, ExecuteDescribeStacks, dependency graph build, applyFiltersToGraph, @@ -508,7 +508,7 @@ func TestExecuteTerraformAll_DryRunHappyPath(t *testing.T) { os.Unsetenv("ATMOS_BASE_PATH") os.Unsetenv("ATMOS_CLI_CONFIG_PATH") - t.Chdir(filepath.Join("..", "..", "tests", "fixtures", "scenarios", "terraform-apply-affected")) + t.Chdir(filepath.Join("..", "..", "..", "tests", "fixtures", "scenarios", "terraform-apply-affected")) t.Run("no stack filter walks every stack", func(t *testing.T) { info := &schema.ConfigAndStacksInfo{ @@ -518,7 +518,7 @@ func TestExecuteTerraformAll_DryRunHappyPath(t *testing.T) { } // Validation has been dropped for the no-stack case; the call should // proceed end-to-end and short-circuit at the dry-run boundary. - assert.NoError(t, ExecuteTerraformAll(info)) + assert.NoError(t, testExecutor(t).ExecuteTerraformAll(info)) }) t.Run("stack filter scopes execution to the requested stack", func(t *testing.T) { @@ -528,7 +528,7 @@ func TestExecuteTerraformAll_DryRunHappyPath(t *testing.T) { SubCommand: "plan", DryRun: true, } - assert.NoError(t, ExecuteTerraformAll(info)) + assert.NoError(t, testExecutor(t).ExecuteTerraformAll(info)) }) t.Run("destroy reverses order and still completes", func(t *testing.T) { @@ -538,7 +538,7 @@ func TestExecuteTerraformAll_DryRunHappyPath(t *testing.T) { SubCommand: "destroy", DryRun: true, } - assert.NoError(t, ExecuteTerraformAll(info)) + assert.NoError(t, testExecutor(t).ExecuteTerraformAll(info)) }) } @@ -552,7 +552,7 @@ func TestExecuteTerraformAll_ReportedSameStackOrder(t *testing.T) { os.Unsetenv("ATMOS_BASE_PATH") os.Unsetenv("ATMOS_CLI_CONFIG_PATH") - t.Chdir(filepath.Join("..", "..", "tests", "fixtures", "scenarios", "terraform-apply-all-dependencies")) + t.Chdir(filepath.Join("..", "..", "..", "tests", "fixtures", "scenarios", "terraform-apply-all-dependencies")) for _, stack := range []string{"reported-order", "reported-order-alias", "reported-order-legacy"} { t.Run(stack, func(t *testing.T) { @@ -567,7 +567,7 @@ func TestExecuteTerraformAll_ReportedSameStackOrder(t *testing.T) { TerraformPlanSummaryFile: summaryPath, } - require.NoError(t, ExecuteTerraformAll(info)) + require.NoError(t, testExecutor(t).ExecuteTerraformAll(info)) assertReportedSameStackOrder(t, summaryPath, stack) }) } @@ -632,7 +632,7 @@ func assertReportedSameStackOrder(t *testing.T, summaryPath, stack string) { for dependent, prerequisites := range dependencies { for _, prerequisite := range prerequisites { - require.Less(t, positions[prerequisite], positions[dependent], "%s must execute before %s", prerequisite, dependent) + require.Less(t, positions[prerequisite], positions[dependent], "%s must testExecutor(t).execute before %s", prerequisite, dependent) // A dry-run step can start and finish within one Windows clock tick, so // equality is valid here. The result positions above establish the // required execution order; this check ensures the timing data does not @@ -677,7 +677,7 @@ components: t.Setenv("ATMOS_BASE_PATH", "") t.Setenv("ATMOS_CLI_CONFIG_PATH", "") - err := ExecuteTerraformAll(&schema.ConfigAndStacksInfo{ + err := testExecutor(t).ExecuteTerraformAll(&schema.ConfigAndStacksInfo{ ComponentType: config.TerraformComponentType, SubCommand: "plan", DryRun: true, @@ -717,7 +717,7 @@ func TestExecuteTerraformAll_AuthManagerResolverWired(t *testing.T) { os.Unsetenv("ATMOS_BASE_PATH") os.Unsetenv("ATMOS_CLI_CONFIG_PATH") - t.Chdir(filepath.Join("..", "..", "tests", "fixtures", "scenarios", "terraform-apply-affected")) + t.Chdir(filepath.Join("..", "..", "..", "tests", "fixtures", "scenarios", "terraform-apply-affected")) ctrl := gomock.NewController(t) mgr := types.NewMockAuthManager(ctrl) @@ -740,14 +740,14 @@ func TestExecuteTerraformAll_AuthManagerResolverWired(t *testing.T) { SubCommand: "plan", DryRun: true, } - require.NoError(t, ExecuteTerraformAll(info)) + require.NoError(t, testExecutor(t).ExecuteTerraformAll(info)) // The factory above stored the AuthManager on info via createQueryAuthManager; // this confirms the non-nil path actually ran rather than silently skipping. assert.NotNil(t, info.AuthManager, "non-nil factory should populate info.AuthManager") } // TestExecuteTerraformAll_AuthManagerCreationError verifies that a failure in -// createQueryAuthManager short-circuits ExecuteTerraformAll with the wrapped +// createQueryAuthManager short-circuits testExecutor(t).ExecuteTerraformAll with the wrapped // error — i.e. we don't silently proceed to ExecuteDescribeStacks with a nil // auth manager when the user has clearly configured auth that we couldn't // resolve. This pins the error-propagation branch at terraform_all.go:41-43. @@ -757,7 +757,7 @@ func TestExecuteTerraformAll_AuthManagerCreationError(t *testing.T) { os.Unsetenv("ATMOS_BASE_PATH") os.Unsetenv("ATMOS_CLI_CONFIG_PATH") - t.Chdir(filepath.Join("..", "..", "tests", "fixtures", "scenarios", "terraform-apply-affected")) + t.Chdir(filepath.Join("..", "..", "..", "tests", "fixtures", "scenarios", "terraform-apply-affected")) sentinel := errors.New("simulated auth manager init failure") ctrl := gomock.NewController(t) @@ -773,13 +773,13 @@ func TestExecuteTerraformAll_AuthManagerCreationError(t *testing.T) { SubCommand: "plan", DryRun: true, } - err := ExecuteTerraformAll(info) + err := testExecutor(t).ExecuteTerraformAll(info) require.Error(t, err) // createQueryAuthManager wraps its own error in `create query auth manager: %w`, // which then bubbles up unchanged. Use errors.Is to stay robust against // future re-wrapping in either layer. assert.ErrorIs(t, err, sentinel, - "auth manager init failure must propagate from ExecuteTerraformAll") + "auth manager init failure must propagate from testExecutor(t).ExecuteTerraformAll") } // TestApplyFiltersToGraph_NoCrossStackPullIn pins the new IncludeDependencies:false @@ -859,9 +859,9 @@ func TestShouldIncludeComponentForAll(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { // This function is internal to terraform_all.go, we'll test it indirectly - // through buildTerraformDependencyGraph tests. + // through testExecutor(t).buildTerraformDependencyGraph tests. _ = tt // Avoid unused variable warning. - // Test covered by buildTerraformDependencyGraph tests. + // Test covered by testExecutor(t).buildTerraformDependencyGraph tests. }) } } @@ -928,7 +928,7 @@ func TestCollectFilteredNodeIDsRemoved(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // Function not exported - test indirectly through buildTerraformDependencyGraph. + // Function not exported - test indirectly through testExecutor(t).buildTerraformDependencyGraph. }) } } @@ -1203,7 +1203,7 @@ func TestShouldSkipComponentForGraph(t *testing.T) { section := map[string]any{ "vars": map[string]any{"key": "val"}, } - assert.False(t, shouldSkipComponentForGraph(section, "vpc")) + assert.False(t, testExecutor(t).shouldSkipComponentForGraph(section, "vpc")) }) t.Run("abstract component", func(t *testing.T) { @@ -1212,7 +1212,7 @@ func TestShouldSkipComponentForGraph(t *testing.T) { "type": "abstract", }, } - assert.True(t, shouldSkipComponentForGraph(section, "base")) + assert.True(t, testExecutor(t).shouldSkipComponentForGraph(section, "base")) }) t.Run("disabled component", func(t *testing.T) { @@ -1221,7 +1221,7 @@ func TestShouldSkipComponentForGraph(t *testing.T) { "enabled": false, }, } - assert.True(t, shouldSkipComponentForGraph(section, "vpc")) + assert.True(t, testExecutor(t).shouldSkipComponentForGraph(section, "vpc")) }) t.Run("enabled component", func(t *testing.T) { @@ -1230,7 +1230,7 @@ func TestShouldSkipComponentForGraph(t *testing.T) { "enabled": true, }, } - assert.False(t, shouldSkipComponentForGraph(section, "vpc")) + assert.False(t, testExecutor(t).shouldSkipComponentForGraph(section, "vpc")) }) } diff --git a/internal/exec/terraform_auth_context.go b/pkg/component/terraform/auth_context.go similarity index 98% rename from internal/exec/terraform_auth_context.go rename to pkg/component/terraform/auth_context.go index d1a35600e1f..cca55ce29d2 100644 --- a/internal/exec/terraform_auth_context.go +++ b/pkg/component/terraform/auth_context.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "sync" diff --git a/pkg/component/terraform/auth_context_test.go b/pkg/component/terraform/auth_context_test.go new file mode 100644 index 00000000000..1ca57338be0 --- /dev/null +++ b/pkg/component/terraform/auth_context_test.go @@ -0,0 +1,273 @@ +package terraform + +import ( + "context" + "encoding/json" + "errors" + "os" + "strings" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/auth" + authtypes "github.com/cloudposse/atmos/pkg/auth/types" + cfg "github.com/cloudposse/atmos/pkg/config" + atmosio "github.com/cloudposse/atmos/pkg/io" + "github.com/cloudposse/atmos/pkg/schema" + tfcache "github.com/cloudposse/atmos/pkg/terraform/cache" + "github.com/cloudposse/atmos/pkg/terraform/rc" +) + +func TestSetLastAuthContext_RoundTrips(t *testing.T) { + t.Cleanup(ClearLastAuthContext) + + authCtx := &schema.AuthContext{ + AWS: &schema.AWSAuthContext{ + Profile: "test-profile", + CredentialsFile: "/tmp/creds", + Region: "us-east-1", + }, + } + authMgr := "mock-manager" + + SetLastAuthContext(authCtx, authMgr) + + gotCtx, gotMgr := GetLastAuthContext() + require.NotNil(t, gotCtx) + assert.Equal(t, "test-profile", gotCtx.AWS.Profile) + assert.Equal(t, "us-east-1", gotCtx.AWS.Region) + assert.Equal(t, "mock-manager", gotMgr) +} + +func TestGetLastAuthContext_ReturnsNilWhenUnset(t *testing.T) { + t.Cleanup(ClearLastAuthContext) + ClearLastAuthContext() + + gotCtx, gotMgr := GetLastAuthContext() + assert.Nil(t, gotCtx) + assert.Nil(t, gotMgr) +} + +func TestClearLastAuthContext_ResetsState(t *testing.T) { + authCtx := &schema.AuthContext{ + AWS: &schema.AWSAuthContext{Profile: "before-clear"}, + } + SetLastAuthContext(authCtx, "mgr") + + ClearLastAuthContext() + + gotCtx, gotMgr := GetLastAuthContext() + assert.Nil(t, gotCtx) + assert.Nil(t, gotMgr) +} + +func TestSetLastAuthContext_OverwritesPrevious(t *testing.T) { + t.Cleanup(ClearLastAuthContext) + + first := &schema.AuthContext{ + AWS: &schema.AWSAuthContext{Profile: "first"}, + } + second := &schema.AuthContext{ + AWS: &schema.AWSAuthContext{Profile: "second"}, + } + + SetLastAuthContext(first, "mgr1") + SetLastAuthContext(second, "mgr2") + + gotCtx, gotMgr := GetLastAuthContext() + require.NotNil(t, gotCtx) + assert.Equal(t, "second", gotCtx.AWS.Profile) + assert.Equal(t, "mgr2", gotMgr) +} + +// TestResolveStoreDefaultIdentityFallback verifies explicit identities win and missing defaults preserve ambient credentials. +func TestResolveStoreDefaultIdentityFallback(t *testing.T) { + for _, tc := range []struct { + name, identity, defaultIdentity, want string + lookupErr error + lookup bool + }{ + {name: "disabled does not authenticate", identity: cfg.IdentityFlagDisabledValue}, + {name: "explicit bypasses defaults", identity: "deployment", want: "deployment"}, + {name: "empty selects stack default", defaultIdentity: "stack-default", want: "stack-default", lookup: true}, + {name: "selection uses stack default", identity: cfg.IdentityFlagSelectValue, defaultIdentity: "stack-default", want: "stack-default", lookup: true}, + {name: "missing default keeps ambient credentials", lookupErr: errUtils.ErrInvalidAuthConfig, lookup: true}, + } { + t.Run(tc.name, func(t *testing.T) { + manager := authtypes.NewMockAuthManager(gomock.NewController(t)) + if tc.lookup { + manager.EXPECT().GetDefaultIdentity(false).Return(tc.defaultIdentity, tc.lookupErr) + } + assert.Equal(t, tc.want, resolveStoreDefaultIdentity(tc.identity, manager)) + }) + } + assert.Empty(t, resolveStoreDefaultIdentity("", nil)) +} + +// TestSetupTerraformAuthResolvesConfigurationFailures verifies auth failures retain recovery context and error sentinels. +func TestSetupTerraformAuthResolvesConfigurationFailures(t *testing.T) { + for _, tc := range []struct { + name string + creatorFails bool + wantSentinel error + }{ + {name: "merged config", wantSentinel: errUtils.ErrInvalidAuthConfig}, + {name: "manager creation", creatorFails: true, wantSentinel: errUtils.ErrFailedToInitializeAuthManager}, + } { + t.Run(tc.name, func(t *testing.T) { + cause := errors.New("identity configuration unavailable") + config := &schema.AtmosConfiguration{} + info := &schema.ConfigAndStacksInfo{Identity: "deploy", Stack: "dev", ComponentFromArg: "vpc", ComponentPrompted: true, StackPrompted: true} + merged := &schema.AuthConfig{} + resolved := false + executor := &Executor{services: Services{ + DefaultMergedAuthConfigGetter: func(gotConfig *schema.AtmosConfiguration, gotInfo *schema.ConfigAndStacksInfo) (*schema.AuthConfig, error) { + require.Same(t, config, gotConfig) + require.Same(t, info, gotInfo) + if !tc.creatorFails { + return nil, cause + } + return merged, nil + }, + DefaultAuthManagerCreator: func(identity string, gotMerged *schema.AuthConfig, selectValue string, gotConfig *schema.AtmosConfiguration, reexec auth.ReExecContext) (auth.AuthManager, error) { + require.True(t, tc.creatorFails, "invalid merged configuration must stop before auth creation") + assert.Equal(t, "deploy", identity) + assert.Same(t, merged, gotMerged) + assert.Same(t, config, gotConfig) + assert.Equal(t, cfg.IdentityFlagSelectValue, selectValue) + assert.Equal(t, auth.ReExecContext{Component: "vpc", Stack: "dev", ComponentPrompted: true, StackPrompted: true}, reexec) + return nil, cause + }, + ResolveIdentityConfigError: func(gotConfig *schema.AtmosConfiguration, gotInfo *schema.ConfigAndStacksInfo, err, sentinel error) error { + resolved = true + assert.Same(t, config, gotConfig) + assert.Same(t, info, gotInfo) + assert.ErrorIs(t, err, cause) + assert.ErrorIs(t, sentinel, tc.wantSentinel) + return errors.Join(sentinel, err) + }, + }} + manager, err := executor.setupTerraformAuth(config, info) + require.ErrorIs(t, err, cause) + require.ErrorIs(t, err, tc.wantSentinel) + assert.Nil(t, manager) + assert.Nil(t, info.AuthManager) + assert.True(t, resolved, "auth errors must retain the identity-selection recovery path") + }) + } +} + +// TestSetupTerraformAuthThreadsTargetStack verifies nested store authentication inherits the selected stack and identity. +func TestSetupTerraformAuthThreadsTargetStack(t *testing.T) { + manager := authtypes.NewMockAuthManager(gomock.NewController(t)) + managerInfo := &schema.ConfigAndStacksInfo{} + manager.EXPECT().GetStackInfo().Return(managerInfo) + info := &schema.ConfigAndStacksInfo{Stack: "dev"} + config := &schema.AtmosConfiguration{} + executor := &Executor{services: Services{ + DefaultMergedAuthConfigGetter: func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo) (*schema.AuthConfig, error) { + return &schema.AuthConfig{}, nil + }, + DefaultAuthManagerCreator: func(string, *schema.AuthConfig, string, *schema.AtmosConfiguration, auth.ReExecContext) (auth.AuthManager, error) { + return manager, nil + }, + StoreAutoDetectedIdentity: func(got auth.AuthManager, target *schema.ConfigAndStacksInfo) { + require.Same(t, manager, got) + require.Same(t, info, target) + target.Identity = "detected-deploy" + }, + }} + got, err := executor.setupTerraformAuth(config, info) + require.NoError(t, err) + assert.Same(t, manager, got) + assert.Same(t, manager, info.AuthManager) + assert.Equal(t, "dev", managerInfo.Stack, "nested store auth needs the target stack to resolve emulator endpoints") + require.NotNil(t, config.SecretsAuth) + assert.Equal(t, "detected-deploy", config.SecretsAuth.DefaultIdentity) + assert.NotNil(t, config.SecretsAuth.Resolver) +} + +// TestAppendTerraformSecretEnv verifies secret encoding failures propagate and test secrets take precedence over component values. +func TestAppendTerraformSecretEnv(t *testing.T) { + secret := "environment-fixture-sensitive-value" + atmosio.RegisterSecret(secret) + for _, tc := range []struct { + name string + info schema.ConfigAndStacksInfo + want []string + invalid bool + }{ + {name: "component encoding error", info: schema.ConfigAndStacksInfo{ + TerraformSecretVarKeys: map[string]bool{"token": true}, ComponentVarsSection: map[string]any{"token": make(chan int)}, + }, invalid: true}, + {name: "test encoding error", info: schema.ConfigAndStacksInfo{ + SubCommand: "test", ComponentSection: map[string]any{"test": map[string]any{"vars": map[string]any{"credentials": map[string]any{"token": secret, "invalid": make(chan int)}}}}, + }, invalid: true}, + {name: "test secrets override component variables", info: schema.ConfigAndStacksInfo{ + SubCommand: "test", ComponentEnvList: []string{"TF_VAR_token=component-value"}, + ComponentSection: map[string]any{"test": map[string]any{"vars": map[string]any{"token": secret, "public": "unclassified"}}}, + }, want: []string{"TF_VAR_token=component-value", "TF_VAR_token=" + secret}}, + } { + t.Run(tc.name, func(t *testing.T) { + err := appendTerraformSecretEnv(&tc.info) + if tc.invalid { + var encodingErr *json.UnsupportedTypeError + require.ErrorAs(t, err, &encodingErr) + assert.Empty(t, tc.info.ComponentEnvList, "invalid secrets must not yield partial environment output") + return + } + require.NoError(t, err) + assert.Equal(t, tc.want, tc.info.ComponentEnvList) + }) + } +} + +// TestAssembleComponentEnvVarsRejectsInvalidRC verifies malformed CLI configuration stops environment preparation. +func TestAssembleComponentEnvVarsRejectsInvalidRC(t *testing.T) { + clearUserCLIConfigEnv(t) + config := &schema.AtmosConfiguration{BasePath: t.TempDir()} + config.Components.Terraform.RC = &schema.TerraformRCConfig{ + Enabled: true, + Config: map[string]any{"provider_installation": "invalid-method-list"}, + } + info := &schema.ConfigAndStacksInfo{} + err := assembleComponentEnvVars(config, info, nil) + require.ErrorIs(t, err, errUtils.ErrInvalidConfig) + assert.Nil(t, info.RCCleanup) + for _, env := range info.ComponentEnvList { + assert.NotContains(t, env, rc.EnvKeyTFCLIConfigFile+"=", "invalid configuration must not be exported to Terraform") + } +} + +// TestConfigureTerraformRCIncludesRegistryCache verifies the generated CLI configuration routes providers and modules through the registry proxy. +func TestConfigureTerraformRCIncludesRegistryCache(t *testing.T) { + clearUserCLIConfigEnv(t) + config := &schema.AtmosConfiguration{} + config.Components.Terraform.Cache = &schema.TerraformCacheConfig{Enabled: true, Location: t.TempDir()} + setup, err := tfcache.Start(t.Context(), config) + require.NoError(t, err) + require.NotNil(t, setup) + t.Cleanup(func() { require.NoError(t, setup.Close(context.Background())) }) + info := &schema.ConfigAndStacksInfo{TerraformCache: setup} + require.NoError(t, configureTerraformRC(config, info)) + require.NotNil(t, info.RCCleanup) + t.Cleanup(func() { require.NoError(t, info.RCCleanup()) }) + var configPath string + for _, env := range info.ComponentEnvList { + if path, ok := strings.CutPrefix(env, rc.EnvKeyTFCLIConfigFile+"="); ok { + configPath = path + } + } + require.NotEmpty(t, configPath) + contents, err := os.ReadFile(configPath) + require.NoError(t, err) + assert.Contains(t, string(contents), "network_mirror") + assert.Contains(t, string(contents), "https://127.0.0.1:") + assert.Contains(t, string(contents), `host "registry.terraform.io"`) + assert.Contains(t, string(contents), `host "registry.opentofu.org"`) + assert.Contains(t, info.ComponentEnvList, rc.EnvKeyTofuCLIConfigFile+"="+configPath) +} diff --git a/internal/exec/terraform_cli_args_utils.go b/pkg/component/terraform/cli_args_utils.go similarity index 99% rename from internal/exec/terraform_cli_args_utils.go rename to pkg/component/terraform/cli_args_utils.go index c6f9e5ea821..832dbdcfa71 100644 --- a/internal/exec/terraform_cli_args_utils.go +++ b/pkg/component/terraform/cli_args_utils.go @@ -1,14 +1,13 @@ -package exec +package terraform import ( "fmt" "os" "strings" - "github.com/cloudposse/atmos/pkg/perf" - errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/filetype" + "github.com/cloudposse/atmos/pkg/perf" u "github.com/cloudposse/atmos/pkg/utils" ) diff --git a/internal/exec/terraform_cli_args_utils_test.go b/pkg/component/terraform/cli_args_utils_test.go similarity index 99% rename from internal/exec/terraform_cli_args_utils_test.go rename to pkg/component/terraform/cli_args_utils_test.go index 7bf3ae15d9d..3742de10a40 100644 --- a/internal/exec/terraform_cli_args_utils_test.go +++ b/pkg/component/terraform/cli_args_utils_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "fmt" diff --git a/internal/exec/terraform_configure_rc_test.go b/pkg/component/terraform/configure_rc_test.go similarity index 99% rename from internal/exec/terraform_configure_rc_test.go rename to pkg/component/terraform/configure_rc_test.go index 05c88352c7a..12a22cf8af7 100644 --- a/internal/exec/terraform_configure_rc_test.go +++ b/pkg/component/terraform/configure_rc_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "os" diff --git a/internal/exec/terraform_declared_var_type.go b/pkg/component/terraform/declared_var_type.go similarity index 87% rename from internal/exec/terraform_declared_var_type.go rename to pkg/component/terraform/declared_var_type.go index b05ffc456eb..a433e04e920 100644 --- a/internal/exec/terraform_declared_var_type.go +++ b/pkg/component/terraform/declared_var_type.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "github.com/hashicorp/terraform-config-inspect/tfconfig" @@ -16,11 +16,11 @@ import ( func TerraformDeclaredVarType(componentSection map[string]any, varName string) (string, bool) { defer perf.Track(nil, "exec.TerraformDeclaredVarType")() - componentInfo, ok := componentSection[componentInfoKey].(map[string]any) + componentInfo, ok := componentSection["component_info"].(map[string]any) if !ok { return "", false } - module, ok := componentInfo[terraformConfigKey].(*tfconfig.Module) + module, ok := componentInfo["terraform_config"].(*tfconfig.Module) if !ok || module == nil { return "", false } diff --git a/internal/exec/terraform_declared_var_type_test.go b/pkg/component/terraform/declared_var_type_test.go similarity index 89% rename from internal/exec/terraform_declared_var_type_test.go rename to pkg/component/terraform/declared_var_type_test.go index 0a825e8d654..577de669a5f 100644 --- a/internal/exec/terraform_declared_var_type_test.go +++ b/pkg/component/terraform/declared_var_type_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "testing" @@ -11,8 +11,8 @@ func TestTerraformDeclaredVarType(t *testing.T) { t.Parallel() componentSection := map[string]any{ - componentInfoKey: map[string]any{ - terraformConfigKey: &tfconfig.Module{Variables: map[string]*tfconfig.Variable{ + "component_info": map[string]any{ + "terraform_config": &tfconfig.Module{Variables: map[string]*tfconfig.Variable{ "replicas": {Name: "replicas", Type: "number"}, "enabled": {Name: "enabled", Type: "bool"}, "anyvar": {Name: "anyvar", Type: ""}, @@ -63,7 +63,7 @@ func TestTerraformDeclaredVarType(t *testing.T) { t.Run("component_info present but no terraform_config", func(t *testing.T) { t.Parallel() _, ok := TerraformDeclaredVarType(map[string]any{ - componentInfoKey: map[string]any{}, + "component_info": map[string]any{}, }, "replicas") assert.False(t, ok) }) @@ -71,8 +71,8 @@ func TestTerraformDeclaredVarType(t *testing.T) { t.Run("nil module", func(t *testing.T) { t.Parallel() _, ok := TerraformDeclaredVarType(map[string]any{ - componentInfoKey: map[string]any{ - terraformConfigKey: (*tfconfig.Module)(nil), + "component_info": map[string]any{ + "terraform_config": (*tfconfig.Module)(nil), }, }, "replicas") assert.False(t, ok) diff --git a/internal/exec/terraform_detection.go b/pkg/component/terraform/detection.go similarity index 97% rename from internal/exec/terraform_detection.go rename to pkg/component/terraform/detection.go index 54dee49a9ef..e5fca7e62a3 100644 --- a/internal/exec/terraform_detection.go +++ b/pkg/component/terraform/detection.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" @@ -158,7 +158,7 @@ func diagnosticPositionKey(index int, diag tfconfig.Diagnostic) string { return fmt.Sprintf("%s:%d", diag.Pos.Filename, diag.Pos.Line) } -// allDiagnosticsAreModuleSourceInterpolation reports whether every error-severity diagnostic in +// AllDiagnosticsAreModuleSourceInterpolation reports whether every error-severity diagnostic in // diags belongs to a known module-source-interpolation event. // // Terraform-config-inspect emits a *companion* diagnostic at the same source position as the @@ -175,7 +175,9 @@ func diagnosticPositionKey(index int, diag tfconfig.Diagnostic) string { // genuine, unrelated diagnostic occurring alongside the known-safe one -- and sorting before it -- // would never surface in the matched text, yet the caller would still skip validation for the // whole diagnostics set, silently discarding the real error. -func allDiagnosticsAreModuleSourceInterpolation(diags tfconfig.Diagnostics) bool { +func AllDiagnosticsAreModuleSourceInterpolation(diags tfconfig.Diagnostics) bool { + defer perf.Track(nil, "terraform.AllDiagnosticsAreModuleSourceInterpolation")() + groupIsKnown := make(map[string]bool) var order []string diff --git a/internal/exec/terraform_detection_test.go b/pkg/component/terraform/detection_test.go similarity index 99% rename from internal/exec/terraform_detection_test.go rename to pkg/component/terraform/detection_test.go index 33ea86f5ff2..545bafd3fca 100644 --- a/internal/exec/terraform_detection_test.go +++ b/pkg/component/terraform/detection_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "errors" @@ -418,7 +418,7 @@ func TestAllDiagnosticsAreModuleSourceInterpolation(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - result := allDiagnosticsAreModuleSourceInterpolation(tt.diags) + result := AllDiagnosticsAreModuleSourceInterpolation(tt.diags) assert.Equal(t, tt.expected, result, "result should match expected for: %s", tt.name) }) } diff --git a/internal/exec/terraform_exec_metadata_buffer_scoping_test.go b/pkg/component/terraform/exec_metadata_buffer_scoping_test.go similarity index 96% rename from internal/exec/terraform_exec_metadata_buffer_scoping_test.go rename to pkg/component/terraform/exec_metadata_buffer_scoping_test.go index c0578c9a6ef..f3fbd6f153b 100644 --- a/internal/exec/terraform_exec_metadata_buffer_scoping_test.go +++ b/pkg/component/terraform/exec_metadata_buffer_scoping_test.go @@ -1,4 +1,4 @@ -package exec +package terraform // terraform_exec_metadata_buffer_scoping_test.go is the reproduce-first // regression test for FR-006f (research.md Decision 32): the exec-metadata @@ -69,7 +69,7 @@ func TestExecuteCommandPipeline_ExecMetadataOutput_NotPoisonedByWorkspaceSelect( // hooks) — this must still see both phases combined, unaffected by the // exec-metadata scoping fix. var wholePipelineStdout bytes.Buffer - err = executeCommandPipeline(&atmosConfig, &info, execCtx, WithStdoutCapture(&wholePipelineStdout)) + err = testExecutor(t).executeCommandPipeline(&atmosConfig, &info, execCtx, WithStdoutCapture(&wholePipelineStdout)) require.NoError(t, err) assert.Contains(t, info.ExecMetadataRawOutput, planOutput, diff --git a/internal/exec/terraform_exec_metadata_flags_test.go b/pkg/component/terraform/exec_metadata_flags_test.go similarity index 99% rename from internal/exec/terraform_exec_metadata_flags_test.go rename to pkg/component/terraform/exec_metadata_flags_test.go index 469029cd8c3..787d1f97387 100644 --- a/internal/exec/terraform_exec_metadata_flags_test.go +++ b/pkg/component/terraform/exec_metadata_flags_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" diff --git a/internal/exec/terraform_exec_metadata_test.go b/pkg/component/terraform/exec_metadata_test.go similarity index 99% rename from internal/exec/terraform_exec_metadata_test.go rename to pkg/component/terraform/exec_metadata_test.go index 02987a92d55..d2888bd04bf 100644 --- a/internal/exec/terraform_exec_metadata_test.go +++ b/pkg/component/terraform/exec_metadata_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "testing" diff --git a/pkg/component/terraform/execute.go b/pkg/component/terraform/execute.go new file mode 100644 index 00000000000..5ac79ac83ef --- /dev/null +++ b/pkg/component/terraform/execute.go @@ -0,0 +1,442 @@ +package terraform + +import ( + "context" + "fmt" + "maps" + "os" + + "github.com/spf13/cobra" + "github.com/spf13/viper" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/auth/broker" + "github.com/cloudposse/atmos/pkg/component" + cfg "github.com/cloudposse/atmos/pkg/config" + "github.com/cloudposse/atmos/pkg/dependencies" + log "github.com/cloudposse/atmos/pkg/logger" + metricsprocess "github.com/cloudposse/atmos/pkg/metrics/process" + "github.com/cloudposse/atmos/pkg/perf" + "github.com/cloudposse/atmos/pkg/proexec" + // Import backend provisioner to register S3 provisioner. + _ "github.com/cloudposse/atmos/pkg/provisioner/backend" + _ "github.com/cloudposse/atmos/pkg/provisioner/lock" // Register after-init provider locking. + _ "github.com/cloudposse/atmos/pkg/provisioner/source" // Register component source provisioning. + "github.com/cloudposse/atmos/pkg/schema" + tfcache "github.com/cloudposse/atmos/pkg/terraform/cache" + tfoutput "github.com/cloudposse/atmos/pkg/terraform/output" + tfplugin "github.com/cloudposse/atmos/pkg/terraform/plugin" +) + +const ( + terraformPluginCacheDirEnv = tfplugin.CacheDirEnv + terraformPluginCacheMayBreakLockFileEnv = tfplugin.CacheMayBreakLockFileEnv + + // BeforeTerraformInitEvent is the hook event name for provisioners that run before terraform init. + // This matches the hook event registered by backend provisioners in pkg/provisioner/backend/backend.go. + // See pkg/hooks/event.go (hooks.BeforeTerraformInit) for the canonical definition. + beforeTerraformInitEvent = "before.terraform.init" + + // AfterTerraformInitEvent is the hook event name for provisioners that run after a + // successful terraform init (e.g. the multi-platform providers-lock hook in + // pkg/provisioner/lock). It is dispatched with a TerraformExecContext so the provisioner + // can run a terraform subcommand against the live env, RC, and working directory. + afterTerraformInitEvent = "after.terraform.init" + + subcommandApply = "apply" + subcommandDeploy = "deploy" + subcommandInit = "init" + subcommandWorkspace = "workspace" + subcommandProvidersLock = "providers-lock" + + autoApproveFlag = "-auto-approve" + outFlag = "-out" + varFileFlag = "-var-file" + skipTerraformLockFileFlag = "--skip-lock-file" + forceFlag = "--force" + everythingFlag = "--everything" + detailedExitCodeFlag = "-detailed-exitcode" + lockTimeoutFlag = "-lock-timeout" + lockFlag = "-lock" + parallelismFlag = "-parallelism" + refreshFlag = "-refresh" + compactWarningsFlag = "-compact-warnings" + // Terraform's -detailed-exitcode documents this as "succeeded, there is a + // diff", distinct from 0 (no changes) and 1 (error). + detailedExitCodeChangesDetected = 2 + logFieldComponent = "component" + dirPermissions = 0o755 +) + +// resolveAndInstallToolchainDeps resolves and installs toolchain dependencies for a terraform component. +// Returns the ToolchainEnvironment for resolving executable paths downstream. +func resolveAndInstallToolchainDeps(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (*dependencies.ToolchainEnvironment, error) { + defer perf.Track(atmosConfig, "exec.resolveAndInstallToolchainDeps")() + + tenv, err := dependencies.ForComponent(atmosConfig, "terraform", info.StackSection, info.ComponentSection) + if err != nil { + return nil, err + } + + return tenv, nil +} + +// startManagedTerraformCache starts the registry cache for this execution and returns +// its cleanup. It returns a no-op cleanup when caching is disabled or when the caller +// owns the cache lifecycle (info.TerraformCacheExternal, e.g. a graph-backed bulk run +// sharing one proxy across components). On a trust failure the proxy is closed before +// returning so it does not leak. +func startManagedTerraformCache(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (*tfcache.Setup, func(), error) { + defer perf.Track(atmosConfig, "exec.startManagedTerraformCache")() + + if info.TerraformCacheExternal { + return nil, func() {}, nil + } + setup, cleanup, err := tfcache.StartForExecution(context.Background(), atmosConfig) + if err != nil { + return nil, func() {}, err + } + if setup == nil { + return nil, cleanup, nil + } + info.TerraformCache = setup + return setup, cleanup, nil +} + +// execute runs the Terraform component lifecycle behind the registered provider. +// Optional ShellCommandOption values are forwarded to the final ExecuteShellCommand call. +// +//nolint:revive,cyclop,funlen,gocritic,gocognit // Existing pipeline complexity; reporting adds only a deferred snapshot. +func (e *Executor) execute(info schema.ConfigAndStacksInfo, opts ...component.ExecutionOption) (resultErr error) { + defer perf.Track(nil, "exec.ExecuteTerraform")() + defer e.services.AttachComponentReporting(&resultErr, &info, "terraform", info.SubCommand) + + // Captured before any pipeline step can rewrite info.SubCommand (e.g. + // handleDeploySubcommand rewrites "deploy" to "apply" in place so + // downstream terraform invocation logic can treat them uniformly). The + // exec-metadata record must report the command the user actually typed, + // not its internal apply-equivalent rewrite. + originalSubCommand := info.SubCommand + + log.Debug( + "ExecuteTerraform entry", + "SubCommand", info.SubCommand, + "ComponentFromArg", info.ComponentFromArg, + "FinalComponent", info.FinalComponent, + "Stack", info.Stack, + "StackFromArg", info.StackFromArg, + ) + + info.CliArgs = []string{"terraform", info.SubCommand, info.SubCommand2} + planPolicy, err := loadTerraformPlanPolicy(&info) + if err != nil { + return err + } + + atmosConfig, err := cfg.InitCliConfig(info, true) + if err != nil { + return err + } + + if info.NeedHelp { + return nil + } + + // Resolve the terraform executable (e.g. "terraform", "tofu", or a custom path). + resolveTerraformCommand(&atmosConfig, &info) + + // Short-circuit for `terraform version` – no stack processing required. + if info.SubCommand == "version" { + return e.handleVersionSubcommand(&atmosConfig, &info) + } + + // Test fixtures may start the emulator that authentication needs. Provision + // their workdir and run their hooks first, retaining per-invocation source state. + fixtureState, err := e.prepareTerraformTestFixtures(&atmosConfig, &info, opts...) + if err != nil { + return err + } + + // Set up authentication (merge global + component auth, create AuthManager, inject bridge). + authManager, err := e.setupTerraformAuth(&atmosConfig, &info) + if err != nil { + return err + } + + // Process and validate stack configuration. + shouldProcess, shouldCheckStack := shouldProcessStacks(&info) + if shouldProcess { + info, err = e.services.ProcessStacks(&atmosConfig, info, shouldCheckStack, info.ProcessTemplates, info.ProcessFunctions, info.Skip, authManager) + if err != nil { + return err + } + } + // Stack processing rebuilds ComponentSection; keep the provisioning markers + // so implicit init cannot re-copy the source over fixture-generated files. + maps.Copy(info.ComponentSection, fixtureState) + if shouldCheckStack && len(info.Stack) < 1 { + return errUtils.ErrMissingStack + } + if !info.ComponentIsEnabled { + log.Info("Component is not enabled and skipped", logFieldComponent, info.ComponentFromArg) + return nil + } + planPolicy, err = bindTerraformPolicy(planPolicy, &info, atmosConfig.Templates.Settings.Delimiters...) + if err != nil { + return err + } + + // Ensure ambient credential brokers (e.g., Atmos Pro github/sts) have provisioned before the + // subprocess environment is built, so terraform's own `git::` module fetches can read private + // repos via the inherited GIT_CONFIG_* rewrites. Process-once and gated (CI + configured). + broker.EnsureCredentials(context.Background(), &atmosConfig) + + // Start the Terraform registry cache (no-op when disabled or caller-owned). The + // ephemeral proxy must outlive the whole pipeline, so its Close is deferred here. + // Env assembly merges its CLI-config contribution into the generated RC. + cacheSetup, closeCache, err := startManagedTerraformCache(&atmosConfig, &info) + if err != nil { + return err + } + if cacheSetup != nil { + defer closeCache() + } + + // Resolve paths, install toolchain, write varfiles, validate, run hooks, and build env. + execCtx, err := e.prepareComponentExecution(component.ContextFromOptions(opts...), executionOutputWriters(opts...), &atmosConfig, &info, shouldProcess) + if err != nil { + return err + } + execCtx.planPolicy = planPolicy + + // Remove the temporary Terraform CLI config (TF_CLI_CONFIG_FILE) after the whole + // pipeline (init, workspace, plan/apply) completes. Registered here, not inside the + // pipeline, so the file survives every subprocess and is cleaned up on early errors. + if info.RCCleanup != nil { + defer func() { + if cleanupErr := info.RCCleanup(); cleanupErr != nil { + log.Debug("Failed to remove temporary Terraform CLI config", "error", cleanupErr) + } + }() + } + + // Persist auth context so PostRunE hooks (e.g. store hooks that read + // terraform outputs) can reuse the credentials established during this + // execution. Without this, hooks create a fresh info with no auth. + SetLastAuthContext(info.AuthContext, info.AuthManager) + + // Test hooks already ran before authentication to bootstrap their fixtures. + if fixtureState == nil { + if err := runPreparedTerraformHooks(&atmosConfig, &info, opts...); err != nil { + return err + } + } + + // Run the full command pipeline: init, arg build, workspace, execute, cleanup. + // Forward caller-provided options (e.g. CI stdout/stderr capture) alongside the environment option. + opts = append(opts, component.WithEnvironment(info.SanitizedEnv)) + err = e.executeCommandPipeline(&atmosConfig, &info, execCtx, opts...) + if err == nil { + // A successful Terraform command can create, change, or remove state. Drop + // any preflight snapshot so a dependent graph node reads the current outputs. + invalidateTerraformStateCache(info.Stack, info.ComponentFromArg) + tfoutput.InvalidateComponentOutputs(info.Stack, info.ComponentFromArg) + } + + captureExecMetadataSync(&atmosConfig, originalSubCommand, &info, execMetadataSyncParams{ + Cmd: component.InvokingCommandFromOptions(opts...), + Parser: component.MetadataParserFromOptions(opts...), + Err: err, + }) + + return err +} + +// captureExecMetadataSync reports an execution record to Atmos Pro for the +// synchronous allowlist (terraform plan/apply/deploy), blocking briefly per +// proexec.CaptureSync's own configurable timeout. No-op for every other +// terraform subcommand. SubCommand must be the subcommand the user actually +// invoked (captured before handleDeploySubcommand's in-place "deploy" -> +// "apply" rewrite), so a `deploy` invocation is reported as `deploy`, not +// misattributed to `apply`. +// +// Multi-component invocations (info.NodeHooks != nil, wired by +// cmd/terraform/utils.go's wirePerComponentHook for --affected/--all/query +// runs) are skipped here: this function fires once per graph node, but +// FR-006a requires exactly one execution record for the whole invocation. +// Cmd/terraform/utils.go's terraformNodeHooks accumulates each node's +// identity/outcome instead and fires a single aggregate CaptureSync call +// after the graph run completes (research.md Decisions 11/17). +// +// Structured plugin.TerraformOutputData enrichment described for User Story 3 +// is obtained via parser, a closure supplied by cmd/terraform through +// WithExecMetadataParser (research.md Decision 18) — internal/exec never +// imports pkg/ci/plugins/terraform directly, since pkg/ci/internal/plugin is +// only importable from within the pkg/ci tree and, independently, +// pkg/ci/plugins/terraform itself imports internal/exec (a confirmed import +// cycle). Parser is nil for callers that don't wire one (e.g. tests), in +// which case data is reported as nil, same as before this data was wired. +// ExecMetadataSyncParams bundles the invoking Cobra command, the optional +// structured-output parser, and the command's own error — the three values +// captureExecMetadataSync needs beyond atmosConfig/subCommand/info, grouped +// to stay under the linter's argument-count limit. +type execMetadataSyncParams struct { + Cmd *cobra.Command + Parser func(subCommand string, exitCode int, output string) any + Err error +} + +func captureExecMetadataSync(atmosConfig *schema.AtmosConfiguration, subCommand string, info *schema.ConfigAndStacksInfo, params execMetadataSyncParams) { + commandPath := "atmos terraform " + subCommand + if !proexec.IsSyncCommand(commandPath) { + return + } + + if info.NodeHooks != nil { + log.Debug("Skipping per-node exec-metadata sync capture: part of a multi-component run.", "component", info.ComponentFromArg) + return + } + + exitCode := errUtils.GetExitCode(params.Err) + if params.Err != nil && exitCode == 0 { + exitCode = 1 + } + + // FR-006e: TerraformExecData.exit_code must report the terraform/tofu subprocess's + // real, pre-CI-remap exit code (info.ExecMetadataRawExitCode, set by + // executeMainTerraformCommand), not the post-remap/neutralized exitCode above — + // that value is reserved for the base envelope's own exit_code (FR-003), which + // is unaffected by this. Falls back to exitCode when the raw field was never + // populated (e.g. the pipeline failed before the main command ran at all, or a + // test invokes captureExecMetadataSync directly without going through + // executeMainTerraformCommand). + rawExitCode := info.ExecMetadataRawExitCode + if rawExitCode == 0 && exitCode != 0 { + rawExitCode = exitCode + } + + var args []string + if info.ComponentFromArg != "" { + args = []string{info.ComponentFromArg} + } + + // Flags MUST be sourced from the invoking Cobra command's own record of + // explicitly-set flags, not info.AdditionalArgsAndFlags — that field is a + // pass-through-args collection that never contains atmos-recognized flags + // like -s/--stack and has --upload-status stripped out of it before this + // call runs, so it structurally cannot represent "the flags actually + // passed" (research.md Decision 14). + flags := proexec.FlagsFromCommand(params.Cmd) + + var data any + if params.Parser != nil { + data = params.Parser(subCommand, rawExitCode, info.ExecMetadataRawOutput) + } + + // info.ExecMetadataRawMetrics is typed `any` (not *process.ProcessMetrics) + // to avoid an import cycle — pkg/metrics/process imports pkg/schema for + // its own settings gate, so pkg/schema cannot import it back. A nil or + // mistyped value here is silently treated as "no override", falling back + // to proexec.buildRecord's own process.SelfUsageSoFar() default. + metrics, _ := info.ExecMetadataRawMetrics.(*metricsprocess.ProcessMetrics) + + in := &proexec.ExecRecordInput{Command: "terraform " + subCommand, Args: args, Flags: flags, ExitCode: exitCode, Data: data, Metrics: metrics} + if syncErr := proexec.CaptureSync(atmosConfig, in); syncErr != nil { + log.Debug("Exec-metadata sync capture returned an error.", "error", syncErr) + } +} + +// configurePluginCache returns environment variables for Terraform plugin caching. +// It checks if the user has already set TF_PLUGIN_CACHE_DIR (via OS env or global env), +// and if not, configures automatic caching based on atmosConfig.Components.Terraform.PluginCache. +func configurePluginCache(atmosConfig *schema.AtmosConfiguration) []string { + override, overrideSet := pluginCacheOverride(atmosConfig) + cache := tfplugin.Resolve(atmosConfig, override, overrideSet) + if !cache.Automatic { + if cache.Directory != "" { + log.Debug("TF_PLUGIN_CACHE_DIR already set, skipping automatic plugin cache configuration") + } + return nil + } + return []string{ + fmt.Sprintf("%s=%s", terraformPluginCacheDirEnv, cache.Directory), + fmt.Sprintf("%s=true", terraformPluginCacheMayBreakLockFileEnv), + } +} + +// pluginCacheOverride resolves explicit user configuration with the historical +// command-path precedence: process environment first, then atmos.yaml global env. +func pluginCacheOverride(atmosConfig *schema.AtmosConfiguration) (string, bool) { + if value, ok := os.LookupEnv(terraformPluginCacheDirEnv); ok { + return value, true + } + if atmosConfig != nil { + value, ok := atmosConfig.Env[terraformPluginCacheDirEnv] + return value, ok + } + return "", false +} + +// disableTerraformPluginCacheForExecution removes Terraform/OpenTofu plugin-cache +// configuration from this execution. This is intentionally scoped to the current +// subprocess environment and config copy so concurrent graph runs can keep full +// scheduler parallelism without racing on a shared provider cache. +func disableTerraformPluginCacheForExecution(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) { + if atmosConfig == nil || info == nil { + return + } + // Isolated stack lifecycle workers inherit the same cache policy as the + // in-process Terraform scheduler, including during planfile verification. + workerConfig := viper.New() + _ = workerConfig.BindEnv(tfplugin.DisableCacheEnv, tfplugin.DisableCacheEnv) + if workerConfig.GetString(tfplugin.DisableCacheEnv) == "true" { + info.DisablePluginCache = true + } + if !info.DisablePluginCache { + return + } + + atmosConfig.Components.Terraform.PluginCache = false + atmosConfig.Components.Terraform.PluginCacheDir = "" + + delete(atmosConfig.Env, terraformPluginCacheDirEnv) + delete(atmosConfig.Env, terraformPluginCacheMayBreakLockFileEnv) + delete(info.ComponentEnvSection, terraformPluginCacheDirEnv) + delete(info.ComponentEnvSection, terraformPluginCacheMayBreakLockFileEnv) + + baseEnv := info.SanitizedEnv + if baseEnv == nil { + baseEnv = os.Environ() + } + info.SanitizedEnv = removeEnvKeys(baseEnv, terraformPluginCacheDirEnv, terraformPluginCacheMayBreakLockFileEnv) + info.ComponentEnvList = removeEnvKeys(info.ComponentEnvList, terraformPluginCacheDirEnv, terraformPluginCacheMayBreakLockFileEnv) +} + +func removeEnvKeys(env []string, keys ...string) []string { + if len(env) == 0 || len(keys) == 0 { + return env + } + skip := make(map[string]struct{}, len(keys)) + for _, key := range keys { + skip[key] = struct{}{} + } + + filtered := env[:0] + for _, entry := range env { + key := envKey(entry) + if _, ok := skip[key]; ok { + continue + } + filtered = append(filtered, entry) + } + return filtered +} + +func envKey(entry string) string { + for i := 0; i < len(entry); i++ { + if entry[i] == '=' { + return entry[:i] + } + } + return entry +} diff --git a/pkg/component/terraform/execute_command.go b/pkg/component/terraform/execute_command.go new file mode 100644 index 00000000000..40ec335ec94 --- /dev/null +++ b/pkg/component/terraform/execute_command.go @@ -0,0 +1,465 @@ +package terraform + +import ( + bytes "bytes" + context "context" + errors "errors" + fmt "fmt" + io "io" + os "os" + osexec "os/exec" + slices "slices" + time "time" + + errUtils "github.com/cloudposse/atmos/errors" + component "github.com/cloudposse/atmos/pkg/component" + git "github.com/cloudposse/atmos/pkg/git" + log "github.com/cloudposse/atmos/pkg/logger" + metricsprocess "github.com/cloudposse/atmos/pkg/metrics/process" + perf "github.com/cloudposse/atmos/pkg/perf" + recovery "github.com/cloudposse/atmos/pkg/policy/recovery" + pro "github.com/cloudposse/atmos/pkg/pro" + proexec "github.com/cloudposse/atmos/pkg/proexec" + retry "github.com/cloudposse/atmos/pkg/retry" + schema "github.com/cloudposse/atmos/pkg/schema" + autoinit "github.com/cloudposse/atmos/pkg/terraform/autoinit" +) + +// checkTTYRequirement returns an error when `terraform apply` is invoked without +// -auto-approve in a non-interactive environment (stdin is nil). +func checkTTYRequirement(info *schema.ConfigAndStacksInfo) error { + if os.Stdin != nil { + return nil + } + if info.SubCommand == subcommandApply && !slices.Contains(info.AdditionalArgsAndFlags, autoApproveFlag) { + return fmt.Errorf( + "%w: 'terraform apply' requires a user interaction, but no TTY is attached. "+ + "Use 'terraform apply -auto-approve' or 'terraform deploy' instead", + errUtils.ErrNoTty, + ) + } + return nil +} + +// addRegionEnvVarForImport appends AWS_REGION to the component env list when the +// subcommand is `import` and the component has a `region` variable configured. +func addRegionEnvVarForImport(info *schema.ConfigAndStacksInfo) { + if info.SubCommand != "import" { + return + } + if region, ok := info.ComponentVarsSection["region"].(string); ok { + info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("AWS_REGION=%s", region)) + } +} + +// resolveExitCode extracts the integer exit code from an error returned by +// ExecuteShellCommand. Returns 0 when err is nil, 1 for generic (non-typed) errors. +func resolveExitCode(err error) int { + if err == nil { + return 0 + } + var ec errUtils.ExitCodeError + if errors.As(err, &ec) { + return ec.Code + } + var osErr *osexec.ExitError + if errors.As(err, &osErr) { + return osErr.ExitCode() + } + return 1 +} + +// executeMainTerraformCommand runs the final terraform sub-command. +// It handles exit-code extraction, plan-status upload (for --upload-status), and +// appropriate error propagation. A no-op when info.SubCommand is "workspace" with +// no sub-subcommand (workspace listing was already handled by runWorkspaceSetup). +func (e *Executor) executeMainTerraformCommand( //nolint:revive // argument-limit: opts variadic is not a true argument. + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + allArgsAndFlags []string, + componentPath string, + uploadStatusFlag bool, + opts ...component.ExecutionOption, +) error { + // Bare `workspace` (no sub-subcommand) was fully handled by runWorkspaceSetup. + if info.SubCommand == subcommandWorkspace && info.SubCommand2 == "" { + return nil + } + + err := e.runMainTerraformCommand(atmosConfig, info, allArgsAndFlags, componentPath, opts...) + return e.finishMainTerraformCommand(atmosConfig, info, err, uploadStatusFlag) +} + +func (e *Executor) runMainTerraformCommand( + atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, allArgsAndFlags []string, + componentPath string, opts ...component.ExecutionOption, +) error { + // An explicit `atmos terraform init` about to run below (see the matching recordAutoInit + // call after ExecuteShellCommandWithRetry) must invalidate any existing marker first: see + // executeTerraformInitCommand's matching call for why a failed init must not leave a + // stale-but-still-matching marker behind for a later, unrelated invocation to trust. + if info.SubCommand == subcommandInit { + autoinit.InvalidateFromInfo(newAutoInitInputs(atmosConfig, info, componentPath, ConstructTerraformComponentVarfileName(info))) + } + + // capturedMetrics receives this invocation's subprocess-tree resource + // usage (terraform/tofu plus its own children, e.g. provider plugins) via + // WithMetricsCallback, for combining with atmos's own self-usage below. + var capturedMetrics *metricsprocess.ProcessMetrics + shellStartedAt := time.Now() + allOpts := make([]component.ExecutionOption, 0, len(opts)+1) + allOpts = append(allOpts, opts...) + allOpts = append(allOpts, component.WithMetricsCallback(func(m *metricsprocess.ProcessMetrics) { + capturedMetrics = m + })) + + err := ExecuteShellCommandWithRetry( + atmosConfig, + info, + info.SubCommand, + func(o ...component.ExecutionOption) error { + return e.executeStreamingOrShell(atmosConfig, info, &streamingExecRequest{ + componentPath: componentPath, + args: allArgsAndFlags, + gatePhase: info.SubCommand, + subCommand: info.SubCommand, + redirectStdErr: info.RedirectStdErr, + shellOpts: o, + }) + }, + allOpts..., + ) + + // An explicit `atmos terraform init` reaches this main-command path rather + // than executeTerraformInitCommand. Keep its lifecycle equivalent to an + // implicit init so post-init provisioners can complete and persist provider + // locks for workdir and vendored components, and so the smart-init marker + // gets recorded (recordAutoInit) exactly as it would for an implicit init. + if err == nil && info.SubCommand == subcommandInit { + e.dispatchAfterInitFn(atmosConfig, info, componentPath, opts...) + recordAutoInit(newAutoInitInputs(atmosConfig, info, componentPath, ConstructTerraformComponentVarfileName(info)), allArgsAndFlags) + } + exitCode := resolveExitCode(err) + + // FR-006e: record the terraform/tofu subprocess's own exit code before any + // CI-mode remapping or local neutralization below, so TerraformExecData.exit_code + // (via captureExecMetadataSync) reports the real subprocess outcome even when + // Atmos's own returned status is remapped/neutralized further down. + info.ExecMetadataRawExitCode = exitCode + recordMainTerraformMetrics(atmosConfig, info, capturedMetrics, shellStartedAt) + + return err +} + +func recordMainTerraformMetrics( + atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, + capturedMetrics *metricsprocess.ProcessMetrics, shellStartedAt time.Time, +) { + // Combine this invocation's subprocess-tree usage (terraform/tofu plus its + // own children) with atmos's own self-usage so both the exec-metadata + // upload and the local display reflect the whole command, not just one + // side of it. Local display is scoped to the sync-upload allowlist + // (terraform plan/apply/deploy) so it stays in lockstep with what Atmos + // Pro actually receives — commands with no subprocess (e.g. `describe + // affected`) keep reporting only atmos's own self-usage, unchanged. + if capturedMetrics != nil { + combined := metricsprocess.Combine(metricsprocess.SelfUsageSoFar(), *capturedMetrics) + combined.WallTime = time.Since(shellStartedAt) + info.ExecMetadataRawMetrics = &combined + + if proexec.IsSyncCommand("atmos terraform " + info.SubCommand) { + // Identify the component/stack in the label itself — in a multi-component + // run (e.g. --all/--affected), this line prints once per component, so a + // bare "Completed" would be ambiguous once scrolled away from its own + // plan/apply output. Plain "component (stack)" prose, not CLI flag syntax + // (e.g. not "-s stack") — this is a status line, not a command to copy/paste. + // Backticks render as inline code in the toast (ui.Info renders markdown; + // see pkg/ui/formatter.go's toastMarkdown), matching this repo's convention + // for identifiers in status messages (e.g. terraform_generate_varfile.go's + // "Generated varfile `%s`"). + label := fmt.Sprintf("Completed `%s` (`%s`)", info.ComponentFromArg, info.Stack) + metricsprocess.DisplaySummary(label, combined, atmosConfig) + } + } +} + +func (e *Executor) finishMainTerraformCommand( + atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, err error, uploadStatusFlag bool, +) error { + exitCode := resolveExitCode(err) + // Upload status only when explicitly requested via --upload-status flag. + // Upload failures are logged but never cause the terraform command to fail — + // the exit code should reflect the plan/apply result, not telemetry. + if uploadStatusFlag && e.services.ShouldUploadStatus(info) { + if uploadErr := e.uploadCommandStatus(atmosConfig, info, exitCode); uploadErr != nil { + log.Warn("Failed to upload command status to Atmos Pro. The terraform command result is unaffected.", "error", uploadErr) + } + } + + // Apply CI exit code mapping: remap terraform exit codes for CI runners. + // This is independent of upload — it only affects what the caller sees. + if mappedCode := e.services.MapCIExitCode(atmosConfig, exitCode); mappedCode == 0 { + return nil + } + + // FR-006e/Decision 36: -detailed-exitcode was added to this invocation solely + // because exec-metadata capture required it (info.ExecMetadataDetailedExitCodeAdded), + // not because the user explicitly passed --upload-status. The global CI-mode remap + // above didn't neutralize this exit code (atmosConfig.CI.Enabled is false, or its + // mapping doesn't cover it), so without this local, call-site-scoped neutralization, + // Atmos's own process exit code for `plan` would silently change from 0 to 2 in CI + // environments that satisfy FR-001 but haven't separately set ci.enabled. This does + // NOT touch atmosConfig.CI.Enabled or mapCIExitCode's own gate — those also govern + // annotations/SARIF/summaries/hooks CI-mode, well outside this fix's scope. + if info.ExecMetadataDetailedExitCodeAdded && exitCode == detailedExitCodeChangesDetected { + return nil + } + + return err +} + +// uploadCommandStatus uploads the command status to Atmos Pro. +func (e *Executor) uploadCommandStatus( + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + exitCode int, +) error { + client, cerr := pro.NewAtmosProAPIClientFromEnv(atmosConfig) + if cerr != nil { + return cerr + } + gitRepo := &git.DefaultGitRepo{} + return e.services.UploadStatus(info, exitCode, client, gitRepo) +} + +// cleanupTerraformFiles removes ephemeral plan and varfiles that Atmos generates. +// Failures are logged at Trace level and not propagated, since cleanup errors should +// not mask the result of the main command. +func cleanupTerraformFiles(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) { + // Only a completed apply/destroy consumes the default plan. Inspection commands + // also pass through this pipeline and must preserve plans saved for later use. + if (info.SubCommand == subcommandApply || info.SubCommand == "destroy") && info.PlanFile == "" { + planFilePath := ConstructTerraformComponentPlanfilePath(atmosConfig, info) + removeTerraformFile(planFilePath, "Failed to remove plan file during cleanup") + } + + if info.SubCommand == subcommandApply { + varFilePath := ConstructTerraformComponentVarfilePath(atmosConfig, info) + removeTerraformFile(varFilePath, "Failed to remove var file during cleanup") + } + + if info.SubCommand == "test" { + testVarFilePath := ConstructTerraformComponentTestVarfilePath(atmosConfig, info) + removeTerraformFile(testVarFilePath, "Failed to remove Terraform test var file during cleanup") + } +} + +func removeTerraformFile(path, message string) { + if err := os.Remove(path); err != nil && !os.IsNotExist(err) { + log.Trace(message, "error", err, "file", path) + } +} + +// invokeShellCommandFunc is the signature used by ExecuteShellCommandWithRetry to delegate +// the actual subprocess invocation back to its caller. The caller provides a closure that +// already binds the command, args, dir, env, etc., and only needs the variadic options +// (used by the retry wrapper to inject stdout/stderr capture writers). +type invokeShellCommandFunc func(opts ...component.ExecutionOption) error + +// resetExecMetadataBufs resets the exec-metadata stdout/stderr tee buffers (if +// present) between retry attempts, so the exec-metadata parser only ever sees +// the latest attempt's output rather than every attempt's output concatenated +// together. +func resetExecMetadataBufs(stdoutW, stderrW io.Writer) { + if resettable, ok := stdoutW.(interface{ Reset() }); ok { + resettable.Reset() + } + if resettable, ok := stderrW.(interface{ Reset() }); ok { + resettable.Reset() + } +} + +// RetryExecParams bundles the per-invocation shell command inputs shared by the +// helmfile/packer retry-wrapping helpers (executeHelmfileCommandWithRetry, +// executePackerCommandWithRetry), keeping their parameter lists within the argument-limit. +type RetryExecParams struct { + AllArgsAndFlags []string + ComponentPath string + EnvVars []string +} + +// ExecuteShellCommandWithRetry runs invoke exactly once when info.ComponentRetrySection is +// nil or has no Conditions configured (zero behavioural change for non-retry components). +// It is component-type agnostic: terraform, helmfile, packer, and ansible all shell out +// through ExecuteShellCommand and share this same wrapper. +// +// When retry is configured, stdout and stderr are tee'd into a buffer and the buffer is +// matched against the compiled retry conditions after each attempt. Errors whose captured +// output matches at least one condition trigger another attempt with the configured backoff; +// other errors fail fast. This is intentionally pattern-driven so that real failures (e.g., +// `terraform plan` exit-code-2) are never retried unless the user opts in by listing a +// matching condition. +// +// If baseOpts already requests its own stdout/stderr capture (e.g. helmfile capturing output +// for a NodeHooks.After callback), that writer keeps receiving the full output — the retry +// buffer is teed in alongside it via io.MultiWriter, not substituted for it. +// +// `phase` is included in retry log lines so users can see which subprocess invocation is +// being retried (e.g. "init", "workspace-select", "apply", "sync", "build"). +func ExecuteShellCommandWithRetry( + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + phase string, + invoke invokeShellCommandFunc, + baseOpts ...component.ExecutionOption, +) error { + defer perf.Track(atmosConfig, "exec.ExecuteShellCommandWithRetry")() + + if handled, err := executeShellRetryPolicy(info, phase, invoke, baseOpts...); handled { + return err + } + + cfg := info.ComponentRetrySection + if cfg == nil || len(cfg.Conditions) == 0 { + return invoke(baseOpts...) + } + + patterns, err := retry.CompileConditions(cfg.Conditions) + if err != nil { + return errors.Join(errUtils.ErrInvalidConfig, err) + } + + var buf bytes.Buffer + stdoutWriter, stderrWriter := composeRetryCaptureWriters(baseOpts, &buf) + captureOpts := append([]component.ExecutionOption{}, baseOpts...) + captureOpts = append(captureOpts, component.WithStdoutCapture(stdoutWriter), component.WithStderrCapture(stderrWriter)) + + execMetadataStdout, execMetadataStderr := component.MetadataOutputCaptureFromOptions(baseOpts...) + + attempt := 0 + return retry.WithPredicate( + component.ContextFromOptions(baseOpts...), + cfg, + func() error { + attempt++ + buf.Reset() + resetExecMetadataBufs(execMetadataStdout, execMetadataStderr) + if attempt > 1 { + log.Warn( + "Retrying subprocess after recoverable error", + "phase", phase, + logFieldComponent, info.ComponentFromArg, + "stack", info.StackFromArg, + "attempt", attempt, + ) + } + return invoke(captureOpts...) + }, + func(invokeErr error) bool { + if invokeErr == nil { + return false + } + return retry.MatchesAny(patterns, buf.String()) + }, + ) +} + +// composeRetryCaptureWriters returns the stdout/stderr writers ExecuteShellCommandWithRetry +// should inject: buf alone when baseOpts requests no capture of its own, or an +// io.MultiWriter teeing into both buf and the caller's writer when it does — so a caller's +// own capture (e.g. helmfile's NodeHooks.After output) keeps receiving output instead of +// being silently replaced by the retry buffer. +// +// Buf is wrapped in a syncWriter (see terraform_streaming_ui.go) because the returned +// stdout/stderr writers can end up being invoked concurrently: when the caller streams through +// the TUI (executeStreamingOrShell -> pkg/terraform/ui's newInitCommand), stdout and stderr are +// set to two distinct io.Writer values even though both ultimately tee into this same buf, and +// os/exec.Cmd copies each stream in its own goroutine whenever Stdout and Stderr aren't the +// exact same Writer value — so without synchronization, concurrent Write calls into buf would +// race (bytes.Buffer is not safe for concurrent use). +func composeRetryCaptureWriters(baseOpts []component.ExecutionOption, buf *bytes.Buffer) (stdout, stderr io.Writer) { + var probe component.ExecutionOptions + for _, opt := range baseOpts { + opt(&probe) + } + + safeBuf := &syncWriter{w: buf} + + stdout = safeBuf + if probe.StdoutCapture != nil { + stdout = io.MultiWriter(probe.StdoutCapture, safeBuf) + } + stderr = safeBuf + if probe.StderrCapture != nil { + stderr = io.MultiWriter(probe.StderrCapture, safeBuf) + } + return stdout, stderr +} + +func executeShellRetryPolicy(info *schema.ConfigAndStacksInfo, phase string, invoke invokeShellCommandFunc, options ...component.ExecutionOption) (bool, error) { + componentType, eventCommand := shellErrorEvent(info, phase) + runner, err := recovery.New(info, componentType, eventCommand) + if err != nil || runner == nil { + return err != nil, err + } + capture := errorPolicyCapture(options) + metadataOut, metadataErr := component.MetadataOutputCaptureFromOptions(options...) + var detailedResult error + err = runner.Run(capture.ctx, phase, info.ComponentRetrySection, func() recovery.Result { + capture.stdout.Reset() + capture.stderr.Reset() + resetExecMetadataBufs(metadataOut, metadataErr) + err := invoke(capture.options...) + code := resolveExitCode(err) + // A detailed plan result is not an error event. Preserve its exit status. + if componentType == "terraform" && phase == "plan" && code == 2 { + detailedResult = err + return recovery.Result{ExitCode: code} + } + return recovery.Result{Err: err, ExitCode: code, Stdout: capture.stdout.String(), Stderr: capture.stderr.String()} + }) + if err == nil && detailedResult != nil { + return true, detailedResult + } + return true, err +} + +type policyCapture struct { + ctx context.Context + options []component.ExecutionOption + stdout, stderr *bytes.Buffer +} + +func errorPolicyCapture(options []component.ExecutionOption) policyCapture { + var config component.ExecutionOptions + for _, option := range options { + option(&config) + } + ctx := config.Ctx + if ctx == nil { + ctx = context.Background() + } + stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} + stdoutWriter, stderrWriter := io.Writer(&syncWriter{w: stdout}), io.Writer(&syncWriter{w: stderr}) + if config.StdoutCapture != nil { + stdoutWriter = io.MultiWriter(config.StdoutCapture, stdoutWriter) + } + if config.StderrCapture != nil { + stderrWriter = io.MultiWriter(config.StderrCapture, stderrWriter) + } + capture := append([]component.ExecutionOption{}, options...) + capture = append(capture, component.WithStdoutCapture(stdoutWriter), component.WithStderrCapture(stderrWriter)) + return policyCapture{ctx: ctx, options: capture, stdout: stdout, stderr: stderr} +} + +func shellErrorEvent(info *schema.ConfigAndStacksInfo, phase string) (string, string) { + componentType := info.ComponentType + if componentType == "" { + componentType = "terraform" + } + eventCommand := phase + if phase == info.SubCommand && info.SubCommand2 != "" { + eventCommand += "." + info.SubCommand2 + } + return componentType, eventCommand +} diff --git a/pkg/component/terraform/execute_environment.go b/pkg/component/terraform/execute_environment.go new file mode 100644 index 00000000000..67f1d5e03e0 --- /dev/null +++ b/pkg/component/terraform/execute_environment.go @@ -0,0 +1,414 @@ +package terraform + +import ( + errors "errors" + fmt "fmt" + os "os" + filepath "path/filepath" + slices "slices" + strings "strings" + + errUtils "github.com/cloudposse/atmos/errors" + auth "github.com/cloudposse/atmos/pkg/auth" + component "github.com/cloudposse/atmos/pkg/component" + cfg "github.com/cloudposse/atmos/pkg/config" + dependencies "github.com/cloudposse/atmos/pkg/dependencies" + log "github.com/cloudposse/atmos/pkg/logger" + perf "github.com/cloudposse/atmos/pkg/perf" + schema "github.com/cloudposse/atmos/pkg/schema" + store "github.com/cloudposse/atmos/pkg/store" + authbridge "github.com/cloudposse/atmos/pkg/store/authbridge" + tfcache "github.com/cloudposse/atmos/pkg/terraform/cache" + rc "github.com/cloudposse/atmos/pkg/terraform/rc" +) + +// warnOnConflictingEnvVars inspects the current process environment for variables +// that are known to interfere with Atmos's management of Terraform, and emits a +// warning when any are detected. +func warnOnConflictingEnvVars() { + warnOnExactVars := []string{"TF_CLI_ARGS", "TF_WORKSPACE"} + warnOnPrefixVars := []string{"TF_VAR_", "TF_CLI_ARGS_"} + + var problematicVars []string + for _, envVar := range os.Environ() { + parts := strings.SplitN(envVar, "=", 2) + if len(parts) != 2 { + continue + } + if slices.Contains(warnOnExactVars, parts[0]) { + problematicVars = append(problematicVars, parts[0]) + continue + } + for _, prefix := range warnOnPrefixVars { + if strings.HasPrefix(parts[0], prefix) { + problematicVars = append(problematicVars, parts[0]) + break + } + } + } + + if len(problematicVars) > 0 { + log.Warn("Detected environment variables that may interfere with Atmos's control of Terraform", + "variables", problematicVars) + } +} + +// assembleComponentEnvVars builds the complete list of environment variables for +// the terraform subprocess. It combines the component env section, standard Atmos +// variables (ATMOS_CLI_CONFIG_PATH, ATMOS_BASE_PATH, TF_IN_AUTOMATION), the +// TF_APPEND_USER_AGENT value, the plugin-cache env, and any toolchain PATH overrides. +func assembleComponentEnvVars(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, tenv *dependencies.ToolchainEnvironment) error { + // Convert ComponentEnvSection (set by auth hooks and stack config) to a list. + for k, v := range info.ComponentEnvSection { + info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("%s=%v", k, v)) + } + + info.ComponentEnvList = append(info.ComponentEnvList, + fmt.Sprintf("ATMOS_CLI_CONFIG_PATH=%s", atmosConfig.CliConfigPath)) + + basePath, err := filepath.Abs(atmosConfig.BasePath) + if err != nil { + return err + } + info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("ATMOS_BASE_PATH=%s", basePath)) + + // Suppress verbose Terraform instructions in automated environments. + // https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_in_automation + info.ComponentEnvList = append(info.ComponentEnvList, "TF_IN_AUTOMATION=true") + + // Precedence: OS env > atmos.yaml > default (empty/omitted). + appendUserAgent := atmosConfig.Components.Terraform.AppendUserAgent + if envUA, exists := os.LookupEnv("TF_APPEND_USER_AGENT"); exists && envUA != "" { + appendUserAgent = envUA + } + if appendUserAgent != "" { + info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("TF_APPEND_USER_AGENT=%s", appendUserAgent)) + } + + // Plugin cache directory. + info.ComponentEnvList = append(info.ComponentEnvList, configurePluginCache(atmosConfig)...) + + // Terraform CLI configuration (.terraformrc) rendered by Atmos and exposed via + // TF_CLI_CONFIG_FILE / TOFU_CLI_CONFIG_FILE. Also the injection point for the + // registry cache's network_mirror/host directives. The cleanup closer is stashed + // on info and run (deferred) after the whole terraform pipeline so the temp file + // survives every subprocess (init, workspace, plan/apply). + if err := configureTerraformRC(atmosConfig, info); err != nil { + return err + } + + // Toolchain PATH must come last so it takes precedence over all other PATH entries. + if tenv != nil { + info.ComponentEnvList = append(info.ComponentEnvList, tenv.EnvVars()...) + } + + if len(info.ComponentEnvList) > 0 { + log.Debug("Using ENV vars:") + for _, v := range info.ComponentEnvList { + log.Debug(v) + } + } + + return appendTerraformSecretEnv(info) +} + +// configureTerraformRC builds the effective Terraform CLI-config map from the +// user's components.terraform.rc plus any registry-cache contribution (network +// mirror + module host overrides), renders it to a temp file, and appends +// TF_CLI_CONFIG_FILE / TOFU_CLI_CONFIG_FILE. The cleanup closer is stashed on info +// and deferred at the ExecuteTerraform level so the file outlives every subprocess. +func configureTerraformRC(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { + rcMap := map[string]any{} + + if rcCfg := atmosConfig.Components.Terraform.RC; rcCfg != nil && rcCfg.Enabled { + for k, v := range rcCfg.Config { + // Deep-copy nested values: mergeCacheContribution mutates the "host" map in + // place, and a shallow copy would leak cache loopback overrides back into + // atmosConfig.Components.Terraform.RC.Config for later components/commands in + // the same process. + rcMap[k] = cloneRCValue(v) + } + } + + if setup, ok := info.TerraformCache.(*tfcache.Setup); ok && setup != nil { + mergeCacheContribution(rcMap, setup.Contribute()) + + // Make the terraform/tofu subprocess trust the proxy's self-signed cert. + trustEnv, err := setup.TrustEnv() + if err != nil { + return err + } + info.ComponentEnvList = append(info.ComponentEnvList, trustEnv...) + } + + if len(rcMap) == 0 { + return nil + } + + rcEnv, rcCleanup, err := rc.Generate(rcMap, atmosConfig, info) + if err != nil { + return err + } + info.ComponentEnvList = append(info.ComponentEnvList, rcEnv...) + info.RCCleanup = rcCleanup + return nil +} + +// mergeCacheContribution merges the registry cache's CLI-config directives into +// rcMap. The cache owns provider_installation (it must be the single network-mirror +// egress); host overrides are merged per-host so user-declared hosts are preserved. +func mergeCacheContribution(rcMap, contribution map[string]any) { + const hostKey = "host" + for key, value := range contribution { + if key != hostKey { + rcMap[key] = value + continue + } + add, _ := value.(map[string]any) + existing, _ := rcMap[hostKey].(map[string]any) + if existing == nil { + rcMap[hostKey] = add + continue + } + for host, services := range add { + existing[host] = services + } + rcMap[hostKey] = existing + } +} + +// cloneRCValue deep-copies a CLI-config value so callers can mutate the result without +// affecting the source. Maps and slices are cloned recursively; scalars are returned +// as-is. This protects atmosConfig.Components.Terraform.RC.Config from in-place mutation +// when the registry cache contribution is merged. +func cloneRCValue(v any) any { + switch val := v.(type) { + case map[string]any: + out := make(map[string]any, len(val)) + for k, item := range val { + out[k] = cloneRCValue(item) + } + return out + case []any: + out := make([]any, len(val)) + for i, item := range val { + out[i] = cloneRCValue(item) + } + return out + default: + return val + } +} + +// appendTerraformSecretEnv adds secrets after environment logging to keep them out of logs. +func appendTerraformSecretEnv(info *schema.ConfigAndStacksInfo) error { + // Inject secret-bearing variables as TF_VAR_ so they reach Terraform via the + // (transient) process environment instead of the on-disk varfile. Appended AFTER the + // debug dump above so secret values are never written to logs. + secretEnv, err := SecretVarEnv(info) + if err != nil { + return err + } + if len(secretEnv) > 0 { + info.ComponentEnvList = append(info.ComponentEnvList, secretEnv...) + log.Debug("Injecting secret variables as TF_VAR_* environment variables", "count", len(secretEnv)) + } + + testSecretEnv, err := terraformTestSecretVarEnv(info) + if err != nil { + return err + } + if len(testSecretEnv) > 0 { + info.ComponentEnvList = append(info.ComponentEnvList, testSecretEnv...) + log.Debug("Injecting secret Terraform test variables as TF_VAR_* environment variables", "count", len(testSecretEnv)) + } + + return nil +} + +// resolveTerraformCommand sets info.Command from atmosConfig if not already set. +// Falls back to the cfg.TerraformComponentType default when neither is configured. +func resolveTerraformCommand(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) { + if info.ComponentCommand != "" { + info.Command = info.ComponentCommand + return + } + if info.Command != "" { + return + } + if atmosConfig.Components.Terraform.Command != "" { + info.Command = atmosConfig.Components.Terraform.Command + } else { + info.Command = cfg.TerraformComponentType + } +} + +// handleVersionSubcommand executes the `terraform version` command and returns the result. +// It resolves the toolchain binary and delegates directly to the shell, bypassing +// full stack processing. +func (e *Executor) handleVersionSubcommand(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { + tenv, err := dependencies.ForComponent(atmosConfig, "terraform", nil, nil) + if err != nil { + return err + } + return ExecuteShellCommandWithRetry(atmosConfig, info, "version", func(opts ...component.ExecutionOption) error { + return e.services.ExecuteShellCommand( + *atmosConfig, + tenv.Resolve(info.Command), + []string{info.SubCommand}, + "", + tenv.EnvVars(), + false, + info.RedirectStdErr, + opts..., + ) + }) +} + +func (e *Executor) setupTerraformAuth(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (auth.AuthManager, error) { + // Log the identity-selection decision point for easy debugging. + log.Debug("Resolving auth config for component command", + "stack", info.Stack, "component", info.ComponentFromArg, "subcommand", info.SubCommand) + + // Get merged auth config (global + component-specific if stack/component are set). + // getMergedAuthConfig logs on debug when falling back to global config after an error. + mergedAuthConfig, err := e.services.DefaultMergedAuthConfigGetter(atmosConfig, info) + if err != nil { + // Propagate ErrInvalidComponent directly — prevents an auth prompt for a nonexistent component. + if errors.Is(err, errUtils.ErrInvalidComponent) { + return nil, err + } + // Wrap unexpected errors (e.g. MergeComponentAuthFromConfig failures) with the sentinel + // to match the behaviour of createAndAuthenticateAuthManagerWithDeps. When the error names + // a missing identity, offer the same profile-selection prompt `atmos auth login` has. + return nil, e.services.ResolveIdentityConfigError(atmosConfig, info, err, errUtils.ErrInvalidAuthConfig) + } + + // Create and authenticate the AuthManager using the same injectable creator as + // createAndAuthenticateAuthManagerWithDeps to keep injection points unified. Carry + // forward prompted component/stack so a later identity-not-found fallback inside + // Authenticate can re-inject them into a profile-fallback re-exec. + authManager, err := e.services.DefaultAuthManagerCreator( + info.Identity, mergedAuthConfig, cfg.IdentityFlagSelectValue, atmosConfig, auth.ReExecContext{ + Component: info.ComponentFromArg, + ComponentPrompted: info.ComponentPrompted, + Stack: info.Stack, + StackPrompted: info.StackPrompted, + }, + ) + if err != nil { + if errors.Is(err, errUtils.ErrUserAborted) { + errUtils.Exit(errUtils.ExitCodeSIGINT) + } + // Wrap auth creation failures with the sentinel to match createAndAuthenticateAuthManagerWithDeps. + // When the error names a missing identity, offer the same profile-selection prompt + // `atmos auth login` has. + return nil, e.services.ResolveIdentityConfigError(atmosConfig, info, err, errUtils.ErrFailedToInitializeAuthManager) + } + + // Store manager for nested YAML functions (e.g. !terraform.state). + info.AuthManager = authManager + + // The manager is created with its own empty stackInfo; thread the target stack + // through so emulator identities can resolve the running emulator's endpoint + // when stores (`!store`, `!secret`, hooks) build their in-process auth context. + // authManager may be nil when no identity/auth is configured. + if authManager != nil { + if si := authManager.GetStackInfo(); si != nil && si.Stack == "" { + si.Stack = info.Stack + } + } + + e.injectTerraformStoreAuthResolver(atmosConfig, info, authManager) + + return authManager, nil +} + +func (e *Executor) injectTerraformStoreAuthResolver(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, authManager auth.AuthManager) { + if authManager == nil { + return + } + + // Persist the auto-detected identity so downstream hooks, nested operations, + // and stores without their own identity use the same authenticated principal. + e.services.StoreAutoDetectedIdentity(authManager, info) + + resolver := authbridge.NewResolver(authManager, info) + defaultIdentity := resolveStoreDefaultIdentity(info.Identity, authManager) + atmosConfig.Stores.SetAuthContextResolverWithDefaultIdentity(resolver, defaultIdentity) + + // Also expose the resolver to cloud-KMS SOPS providers so `!secret` resolution during terraform + // authenticates KMS decrypt as the component's effective identity (issue #2637). + atmosConfig.SecretsAuth = &store.SecretsAuthContext{ + Resolver: resolver, + DefaultIdentity: defaultIdentity, + } +} + +func storeDefaultIdentity(identity string) string { + switch identity { + case "", cfg.IdentityFlagSelectValue, cfg.IdentityFlagDisabledValue: + return "" + default: + return identity + } +} + +// resolveStoreDefaultIdentity returns the identity that stores without their own +// `identity:` should authenticate as. An explicit `--identity`/`ATMOS_IDENTITY` +// wins; otherwise we fall back to the stack's `default: true` identity so +// in-process store/secret reads use the same principal (and emulator endpoint) as +// the Terraform subprocess. When there is no default identity (or auth is +// disabled), this returns "" — stores then use the AWS/GCP/Azure default +// credential chain exactly as before (back-compat for ambient real-cloud creds). +func resolveStoreDefaultIdentity(identity string, authManager auth.AuthManager) string { + if identity == cfg.IdentityFlagDisabledValue { + return "" + } + if explicit := storeDefaultIdentity(identity); explicit != "" { + return explicit + } + if authManager == nil { + return "" + } + defaultIdentity, err := authManager.GetDefaultIdentity(false) + if err != nil { + return "" + } + return defaultIdentity +} + +// HookStoreDefaultIdentity resolves the identity that identity-less stores invoked by terraform +// lifecycle hooks (e.g. the after-apply `store-outputs` hook) should inherit. It mirrors the main +// terraform path (injectTerraformStoreAuthResolver): it auto-detects the active identity from the +// auth manager's chain when info carries no explicit identity, then normalizes empty/select/disabled +// to "" (meaning "no inheritance — keep ambient/default SDK credentials"). It returns "" when no auth +// manager is present, preserving the prior behavior for runs without Atmos auth. +// +// The hook executes in a freshly loaded config (prepareHookContext), so it must re-derive this from +// the persisted auth manager rather than relying on the apply-phase config. +func (e *Executor) HookStoreDefaultIdentity(authManager auth.AuthManager, info *schema.ConfigAndStacksInfo) string { + defer perf.Track(nil, "exec.HookStoreDefaultIdentity")() + + if authManager == nil || info == nil { + return "" + } + e.services.StoreAutoDetectedIdentity(authManager, info) + return storeDefaultIdentity(info.Identity) +} + +// SetupTerraformAuthForCLI exposes terraform auth setup to command-layer callers +// that need the same merged-auth and explicit-identity behavior as ExecuteTerraform. +func (e *Executor) SetupTerraformAuthForCLI(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (any, error) { + defer perf.Track(atmosConfig, "exec.SetupTerraformAuthForCLI")() + + return e.setupTerraformAuth(atmosConfig, info) +} + +// SetupComponentAuthForCLI exposes the shared component auth setup to non-Terraform +// command layers that still need authenticated YAML functions such as !terraform.state. +func (e *Executor) SetupComponentAuthForCLI(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (auth.AuthManager, error) { + defer perf.Track(atmosConfig, "exec.SetupComponentAuthForCLI")() + + return e.setupTerraformAuth(atmosConfig, info) +} diff --git a/internal/exec/terraform_execute_helpers_args.go b/pkg/component/terraform/execute_helpers_args.go similarity index 97% rename from internal/exec/terraform_execute_helpers_args.go rename to pkg/component/terraform/execute_helpers_args.go index dd76e9713a3..16f468f5399 100644 --- a/internal/exec/terraform_execute_helpers_args.go +++ b/pkg/component/terraform/execute_helpers_args.go @@ -1,4 +1,4 @@ -package exec +package terraform // terraform_execute_helpers_args.go contains the per-subcommand argument builders // extracted from buildTerraformCommandArgs. Each function is small, pure, and @@ -12,6 +12,7 @@ import ( "time" errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/component" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/proexec" "github.com/cloudposse/atmos/pkg/schema" @@ -277,15 +278,17 @@ func appendApplyPlanFileArg(info *schema.ConfigAndStacksInfo, allArgsAndFlags [] // in terraform_execute_helpers.go handles the provisioner call via prepareInitExecution. // These two paths must never both execute in the same command invocation or provisioners // will run twice. +// +//nolint:revive // Keep the existing argument-building contract during lifecycle extraction. func buildInitSubcommandArgs( atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, allArgsAndFlags []string, varFile string, componentPath *string, - opts ...ShellCommandOption, + opts ...component.ExecutionOption, ) ([]string, error) { - newPath, provErr := prepareInitExecution(shellCommandContext(opts...), shellCommandOutputWriters(opts...), atmosConfig, info, *componentPath) + newPath, provErr := prepareInitExecution(component.ContextFromOptions(opts...), executionOutputWriters(opts...), atmosConfig, info, *componentPath) if provErr != nil { return nil, provErr } @@ -330,7 +333,7 @@ type subcommandArgsParams struct { planFile string uploadStatusFlag bool componentPath *string - opts []ShellCommandOption + opts []component.ExecutionOption } // dispatchSubcommandArgs routes to the per-subcommand argument builder based on @@ -363,12 +366,14 @@ func dispatchSubcommandArgs(allArgsAndFlags []string, p *subcommandArgsParams) ( // command based on the subcommand. For the "init" subcommand it also runs provisioners // and may update *componentPath via the workdir provisioner. // Returns the argument list, an uploadStatus flag, and any error from provisioners. +// +//nolint:revive // Keep the existing argument-building contract during lifecycle extraction. func buildTerraformCommandArgs( atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, varFile, planFile string, componentPath *string, - opts ...ShellCommandOption, + opts ...component.ExecutionOption, ) (allArgsAndFlags []string, uploadStatusFlag bool, err error) { allArgsAndFlags = strings.Fields(info.SubCommand) diff --git a/internal/exec/terraform_execute_helpers_args_test.go b/pkg/component/terraform/execute_helpers_args_test.go similarity index 99% rename from internal/exec/terraform_execute_helpers_args_test.go rename to pkg/component/terraform/execute_helpers_args_test.go index 272f6e604fa..558b633a092 100644 --- a/internal/exec/terraform_execute_helpers_args_test.go +++ b/pkg/component/terraform/execute_helpers_args_test.go @@ -1,4 +1,4 @@ -package exec +package terraform // terraform_execute_helpers_args_test.go tests argument-building helpers extracted from // ExecuteTerraform — specifically the subcommand arg constructors and env-var assembly: @@ -381,7 +381,7 @@ func TestBuildPlanSubcommandArgs_UploadStatusFlag(t *testing.T) { // Upload status flag should be removed from additional args. assert.NotContains(t, info.AdditionalArgsAndFlags, "--upload-status") // --upload-status is the reason the flag was added, not exec-metadata capture — - // the local exit-2 neutralization in executeMainTerraformCommand must not apply + // the local exit-2 neutralization in testExecutor(t).executeMainTerraformCommand must not apply // (research.md Decision 36's own pre-existing --upload-status behavior). assert.False(t, info.ExecMetadataDetailedExitCodeAdded) } @@ -404,7 +404,7 @@ func TestBuildPlanSubcommandArgs_ExecMetadataActive_AddsDetailedExitCode(t *test require.NoError(t, err) assert.Contains(t, args, detailedExitCodeFlag) assert.True(t, info.ExecMetadataDetailedExitCodeAdded, - "executeMainTerraformCommand needs this signal to know the flag was added for exec-metadata, not --upload-status") + "testExecutor(t).executeMainTerraformCommand needs this signal to know the flag was added for exec-metadata, not --upload-status") } // TestBuildPlanSubcommandArgs_ExecMetadataInactive_NoDetailedExitCode verifies diff --git a/internal/exec/terraform_execute_helpers_coverage_test.go b/pkg/component/terraform/execute_helpers_coverage_test.go similarity index 96% rename from internal/exec/terraform_execute_helpers_coverage_test.go rename to pkg/component/terraform/execute_helpers_coverage_test.go index 54bd53c4aa9..eeef8455dba 100644 --- a/internal/exec/terraform_execute_helpers_coverage_test.go +++ b/pkg/component/terraform/execute_helpers_coverage_test.go @@ -1,4 +1,4 @@ -package exec +package terraform // terraform_execute_helpers_coverage_test.go contains unit tests for file-I/O, // validation, init, arg-building, and cleanup helpers extracted from ExecuteTerraform. @@ -7,7 +7,7 @@ package exec // // Organisation: // - printAndWriteVarFiles -// - validateTerraformComponent +// - testExecutor(t).validateTerraformComponent // - prepareInitExecution // - buildInitSubcommandArgs (via prepareInitExecution) // - warnOnConflictingEnvVars (with env-var triggers) @@ -168,7 +168,7 @@ func TestPrintAndWriteVarFiles_WriteActualFile(t *testing.T) { } // The function builds the varfile path itself from atmosConfig+info. - expectedVarfilePath := constructTerraformComponentVarfilePath(&atmosConfig, &info) + expectedVarfilePath := ConstructTerraformComponentVarfilePath(&atmosConfig, &info) require.NoError(t, os.MkdirAll(filepath.Dir(expectedVarfilePath), 0o755)) err := printAndWriteVarFiles(&atmosConfig, &info) @@ -197,7 +197,7 @@ func TestPrintAndWriteVarFiles_WritesTerraformTestVarfile(t *testing.T) { }, }, } - expectedVarfilePath := constructTerraformComponentTestVarfilePath(&atmosConfig, &info) + expectedVarfilePath := ConstructTerraformComponentTestVarfilePath(&atmosConfig, &info) require.NoError(t, os.MkdirAll(filepath.Dir(expectedVarfilePath), 0o755)) err := printAndWriteVarFiles(&atmosConfig, &info) @@ -257,7 +257,7 @@ func TestPrintAndWriteVarFiles_IgnoresTerraformTestVarsForNonTestCommand(t *test }, }, } - expectedVarfilePath := constructTerraformComponentTestVarfilePath(&atmosConfig, &info) + expectedVarfilePath := ConstructTerraformComponentTestVarfilePath(&atmosConfig, &info) require.NoError(t, os.MkdirAll(filepath.Dir(expectedVarfilePath), 0o755)) err := printAndWriteVarFiles(&atmosConfig, &info) @@ -328,7 +328,7 @@ func TestTerraformTestVarsDefaults(t *testing.T) { } // ────────────────────────────────────────────────────────────────────────────── -// validateTerraformComponent +// testExecutor(t).validateTerraformComponent // ────────────────────────────────────────────────────────────────────────────── // TestValidateTerraformComponent_EmptySection_Valid verifies that a component @@ -339,7 +339,7 @@ func TestValidateTerraformComponent_EmptySection_Valid(t *testing.T) { ComponentFromArg: "my-component", ComponentSection: map[string]any{}, } - err := validateTerraformComponent(&atmosConfig, &info) + err := testExecutor(t).validateTerraformComponent(&atmosConfig, &info) assert.NoError(t, err) } @@ -351,7 +351,7 @@ func TestValidateTerraformComponent_NilSection_Valid(t *testing.T) { ComponentFromArg: "my-component", ComponentSection: nil, } - err := validateTerraformComponent(&atmosConfig, &info) + err := testExecutor(t).validateTerraformComponent(&atmosConfig, &info) assert.NoError(t, err) } @@ -500,7 +500,7 @@ func TestCleanupTerraformFiles_ApplyRemovesVarfileForReal(t *testing.T) { } // Construct the expected path so we can create the file before cleanup. - varfilePath := constructTerraformComponentVarfilePath(&atmosConfig, &info) + varfilePath := ConstructTerraformComponentVarfilePath(&atmosConfig, &info) require.NoError(t, os.MkdirAll(filepath.Dir(varfilePath), 0o755)) require.NoError(t, os.WriteFile(varfilePath, []byte(`{"key":"value"}`), 0o644)) require.FileExists(t, varfilePath) @@ -524,7 +524,7 @@ func TestCleanupTerraformFiles_NonPlanShow_RemovesPlanfile(t *testing.T) { PlanFile: "", // empty PlanFile triggers planfile removal } - planfilePath := constructTerraformComponentPlanfilePath(&atmosConfig, &info) + planfilePath := ConstructTerraformComponentPlanfilePath(&atmosConfig, &info) require.NoError(t, os.MkdirAll(filepath.Dir(planfilePath), 0o755)) require.NoError(t, os.WriteFile(planfilePath, []byte("plan-data"), 0o644)) require.FileExists(t, planfilePath) @@ -547,7 +547,7 @@ func TestCleanupTerraformFiles_ShowSubcommand_KeepsPlanfile(t *testing.T) { Component: "mycomp", } - planfilePath := constructTerraformComponentPlanfilePath(&atmosConfig, &info) + planfilePath := ConstructTerraformComponentPlanfilePath(&atmosConfig, &info) require.NoError(t, os.MkdirAll(filepath.Dir(planfilePath), 0o755)) require.NoError(t, os.WriteFile(planfilePath, []byte("plan-data"), 0o644)) require.FileExists(t, planfilePath) @@ -570,7 +570,7 @@ func TestCleanupTerraformFiles_PlanSubcommand_KeepsPlanfile(t *testing.T) { Component: "mycomp", } - planfilePath := constructTerraformComponentPlanfilePath(&atmosConfig, &info) + planfilePath := ConstructTerraformComponentPlanfilePath(&atmosConfig, &info) require.NoError(t, os.MkdirAll(filepath.Dir(planfilePath), 0o755)) require.NoError(t, os.WriteFile(planfilePath, []byte("plan-data"), 0o644)) require.FileExists(t, planfilePath) @@ -595,7 +595,7 @@ func TestCleanupTerraformFiles_ApplyWithCustomPlanFile_SkipsPlanfileRemoval(t *t PlanFile: "custom.planfile", // non-empty → planfile removal is skipped } - planfilePath := constructTerraformComponentPlanfilePath(&atmosConfig, &info) + planfilePath := ConstructTerraformComponentPlanfilePath(&atmosConfig, &info) require.NoError(t, os.MkdirAll(filepath.Dir(planfilePath), 0o755)) require.NoError(t, os.WriteFile(planfilePath, []byte("plan-data"), 0o644)) diff --git a/internal/exec/terraform_execute_helpers_pipeline_test.go b/pkg/component/terraform/execute_helpers_pipeline_test.go similarity index 85% rename from internal/exec/terraform_execute_helpers_pipeline_test.go rename to pkg/component/terraform/execute_helpers_pipeline_test.go index 5f7758bc68e..0fef4e2eeaa 100644 --- a/internal/exec/terraform_execute_helpers_pipeline_test.go +++ b/pkg/component/terraform/execute_helpers_pipeline_test.go @@ -1,21 +1,25 @@ -package exec +package terraform // terraform_execute_helpers_pipeline_test.go contains unit tests for command-pipeline // and argument-building helpers extracted from ExecuteTerraform: // - buildTerraformCommandArgs (unknown subcommand path) // - buildWorkspaceSubcommandArgs (delete and select paths) -// - prepareComponentExecution (early-return error guards) -// - executeCommandPipeline (TTY error short-circuit + double-execution regression guard) -// - runWorkspaceSetup (recovery path when workspace already active) +// - testExecutor(t).prepareComponentExecution (early-return error guards) +// - testExecutor(t).executeCommandPipeline (TTY error short-circuit + double-execution regression guard) +// - testExecutor(t).runWorkspaceSetup (recovery path when workspace already active) import ( "bytes" + "context" "errors" "os" osexec "os/exec" "path/filepath" "strings" "testing" + "time" + + "github.com/cloudposse/atmos/pkg/component" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -75,7 +79,7 @@ func TestBuildWorkspaceSubcommandArgs_Select(t *testing.T) { // TestBuildWorkspaceSubcommandArgs_Bare in _args_test.go. Duplicate removed. // ────────────────────────────────────────────────────────────────────────────── -// prepareComponentExecution (error paths — exercises early-return guards) +// testExecutor(t).prepareComponentExecution (error paths — exercises early-return guards) // ────────────────────────────────────────────────────────────────────────────── // TestPrepareComponentExecution_NoComponentPath_ReturnsError verifies that an @@ -85,23 +89,23 @@ func TestPrepareComponentExecution_NoComponentPath_ReturnsError(t *testing.T) { // BasePath is empty → GetComponentPath returns an error. info := schema.ConfigAndStacksInfo{} - _, err := prepareComponentExecution(t.Context(), provisioner.OutputWriters{}, &atmosConfig, &info, false) + _, err := testExecutor(t).prepareComponentExecution(t.Context(), provisioner.OutputWriters{}, &atmosConfig, &info, false) // An empty BasePath causes checkTerraformConfig to return an error. require.Error(t, err) } // ────────────────────────────────────────────────────────────────────────────── -// executeCommandPipeline (TTY error short-circuit via nil stdin) +// testExecutor(t).executeCommandPipeline (TTY error short-circuit via nil stdin) // ────────────────────────────────────────────────────────────────────────────── // TestExecuteCommandPipeline_TTYError verifies that an apply without -auto-approve // in a nil-stdin environment returns ErrNoTty before calling any shell command. // // This test also guards against the double-execution regression where ExecuteTerraform -// called executeCommandPipeline twice per invocation (every terraform apply ran twice). +// called testExecutor(t).executeCommandPipeline twice per invocation (every terraform apply ran twice). // If the function were called twice, the second invocation would try to reach the TTY // check a second time; any duplication of side-effects (logs, output) would be visible. -// Asserting ErrNoTty from a single executeCommandPipeline call confirms the pipeline +// Asserting ErrNoTty from a single testExecutor(t).executeCommandPipeline call confirms the pipeline // has a consistent single-invocation exit path. // // Must not run in parallel — sets os.Stdin = nil (global state). @@ -124,7 +128,7 @@ func TestExecuteCommandPipeline_TTYError(t *testing.T) { workingDir: "/nonexistent", } - err := executeCommandPipeline(&atmosConfig, &info, execCtx) + err := testExecutor(t).executeCommandPipeline(&atmosConfig, &info, execCtx) require.Error(t, err) assert.ErrorIs(t, err, errUtils.ErrNoTty) } @@ -181,7 +185,7 @@ func runLogGroupPipelineSubprocess(t *testing.T, env map[string]string) string { } // ────────────────────────────────────────────────────────────────────────────── -// runWorkspaceSetup (workspace recovery path) +// testExecutor(t).runWorkspaceSetup (workspace recovery path) // ────────────────────────────────────────────────────────────────────────────── // TestExecuteShellCommand_PropagatesEnvToSubprocess is a prerequisite for @@ -195,7 +199,7 @@ func TestExecuteShellCommand_PropagatesEnvToSubprocess(t *testing.T) { require.NoError(t, err, "os.Executable() must succeed") atmosConfig := schema.AtmosConfiguration{} - execErr := ExecuteShellCommand( + execErr := testExecutor(t).services.ExecuteShellCommand( atmosConfig, exePath, []string{"-test.run=^$"}, // no test matches → exits 0 normally WITHOUT the env var "", // dir: current @@ -223,7 +227,7 @@ func TestRunWorkspaceSetup_UsesSelectOrCreate(t *testing.T) { ComponentEnvList: []string{"_ATMOS_TEST_ARGS_FILE=" + argsFile}, } - require.NoError(t, runWorkspaceSetup(&atmosConfig, &info, t.TempDir())) + require.NoError(t, testExecutor(t).runWorkspaceSetup(&atmosConfig, &info, t.TempDir())) argsBytes, err := os.ReadFile(argsFile) require.NoError(t, err) @@ -247,7 +251,7 @@ func TestRunWorkspaceSetup_SuppressesTerraformWorkspaceOutput(t *testing.T) { }, } - require.NoError(t, runWorkspaceSetup( + require.NoError(t, testExecutor(t).runWorkspaceSetup( &atmosConfig, &info, t.TempDir(), @@ -263,7 +267,7 @@ func TestRunWorkspaceSetup_SuppressesTerraformWorkspaceOutput(t *testing.T) { // TestRunWorkspaceSetup_RecoveryPath verifies that when both "workspace select" and // "workspace new" fail with exit code 1 but the .terraform/environment file already -// names the target workspace, runWorkspaceSetup logs a warning and returns nil. +// names the target workspace, testExecutor(t).runWorkspaceSetup logs a warning and returns nil. // This protects against regressions of the workspace-recovery logic added in this PR. // // Cross-platform approach: the test binary (os.Executable) is used as the "terraform" @@ -280,7 +284,7 @@ func TestRunWorkspaceSetup_RecoveryPath(t *testing.T) { tmpDir := t.TempDir() workspace := "dev" - // Write the environment file so isTerraformCurrentWorkspace returns true. + // Write the environment file so testExecutor(t).isTerraformCurrentWorkspace returns true. terraformDir := filepath.Join(tmpDir, ".terraform") require.NoError(t, os.MkdirAll(terraformDir, 0o755)) require.NoError(t, os.WriteFile( @@ -300,15 +304,15 @@ func TestRunWorkspaceSetup_RecoveryPath(t *testing.T) { } // Recovery path: both select and new fail with exit 1, environment file names the - // workspace → runWorkspaceSetup must return nil (proceed with warning). + // workspace → testExecutor(t).runWorkspaceSetup must return nil (proceed with warning). // // Capture log output so we can verify the expected recovery warning is emitted. var logBuf bytes.Buffer log.Default().SetOutput(&logBuf) defer log.Default().SetOutput(os.Stderr) - wsErr := runWorkspaceSetup(&atmosConfig, &info, tmpDir) - assert.NoError(t, wsErr, "runWorkspaceSetup must succeed when environment file confirms active workspace") + wsErr := testExecutor(t).runWorkspaceSetup(&atmosConfig, &info, tmpDir) + assert.NoError(t, wsErr, "testExecutor(t).runWorkspaceSetup must succeed when environment file confirms active workspace") // Assert the recovery warning was emitted. logOutput := logBuf.String() @@ -320,7 +324,7 @@ func TestRunWorkspaceSetup_RecoveryPath(t *testing.T) { // TestRunWorkspaceSetup_NoRecoveryOnMismatchedEnv verifies the negative recovery case: // when both "workspace select" and "workspace new" fail with exit code 1, and the // .terraform/environment file names a DIFFERENT workspace than requested, the recovery -// guard must NOT trigger — runWorkspaceSetup must return a non-nil error. +// guard must NOT trigger — testExecutor(t).runWorkspaceSetup must return a non-nil error. // // This prevents regressions where recovery triggers too eagerly (e.g., in "staging" but // requesting "dev" → should fail, not silently continue with the wrong workspace). @@ -334,7 +338,7 @@ func TestRunWorkspaceSetup_NoRecoveryOnMismatchedEnv(t *testing.T) { tmpDir := t.TempDir() // Write "staging" to the environment file but request workspace "dev". - // isTerraformCurrentWorkspace("dev") must return false → no recovery. + // testExecutor(t).isTerraformCurrentWorkspace("dev") must return false → no recovery. terraformDir := filepath.Join(tmpDir, ".terraform") require.NoError(t, os.MkdirAll(terraformDir, 0o755)) require.NoError(t, os.WriteFile( @@ -357,9 +361,9 @@ func TestRunWorkspaceSetup_NoRecoveryOnMismatchedEnv(t *testing.T) { } // Both workspace select and new return exit 1, but environment file says "staging" - // (not "dev") → isTerraformCurrentWorkspace returns false → must return error. - wsErr := runWorkspaceSetup(&atmosConfig, &info, tmpDir) - require.Error(t, wsErr, "runWorkspaceSetup must fail when environment file names a different workspace") + // (not "dev") → testExecutor(t).isTerraformCurrentWorkspace returns false → must return error. + wsErr := testExecutor(t).runWorkspaceSetup(&atmosConfig, &info, tmpDir) + require.Error(t, wsErr, "testExecutor(t).runWorkspaceSetup must fail when environment file names a different workspace") var exitErr errUtils.ExitCodeError require.True(t, errors.As(wsErr, &exitErr), "error must be an ExitCodeError, got: %T (%v)", wsErr, wsErr) @@ -409,7 +413,7 @@ func TestRunWorkspaceSetup_FailureSurfacesTerraformOutput(t *testing.T) { ComponentEnvList: tt.env, } - wsErr := runWorkspaceSetup(&atmosConfig, &info, t.TempDir()) + wsErr := testExecutor(t).runWorkspaceSetup(&atmosConfig, &info, t.TempDir()) require.Error(t, wsErr) assert.ErrorIs(t, wsErr, errUtils.ErrTerraformWorkspaceOp) @@ -436,8 +440,8 @@ func TestRunWorkspaceSetup_FailureSurfacesTerraformOutput(t *testing.T) { // applyOptsForTest captures the stdout/stderr writers a caller passed via // WithStdoutCapture / WithStderrCapture so a fake invoke can write to them and // drive the predicate check inside executeShellCommandWithRetry. -func applyOptsForTest(opts []ShellCommandOption) shellCommandConfig { - var cfg shellCommandConfig +func applyOptsForTest(opts []ShellCommandOption) component.ExecutionOptions { + var cfg component.ExecutionOptions for _, o := range opts { o(&cfg) } @@ -456,8 +460,8 @@ func TestExecuteShellCommandWithRetry_NilConfig_RunsOnce(t *testing.T) { cfg := applyOptsForTest(opts) // Without retry config the helper must not inject capture writers — they // would silently swallow the user's terraform output. - assert.Nil(t, cfg.stdoutCapture, "stdoutCapture must be nil when retry config is absent") - assert.Nil(t, cfg.stderrCapture, "stderrCapture must be nil when retry config is absent") + assert.Nil(t, cfg.StdoutCapture, "stdoutCapture must be nil when retry config is absent") + assert.Nil(t, cfg.StderrCapture, "stderrCapture must be nil when retry config is absent") return errors.New("boom") } @@ -501,8 +505,8 @@ func TestExecuteShellCommandWithRetry_MatchingError_Retries(t *testing.T) { fakeInvoke := func(opts ...ShellCommandOption) error { called++ cfg := applyOptsForTest(opts) - require.NotNil(t, cfg.stderrCapture, "retry mode must inject stderr capture") - _, _ = cfg.stderrCapture.Write([]byte("Error: 502 Bad Gateway returned")) + require.NotNil(t, cfg.StderrCapture, "retry mode must inject stderr capture") + _, _ = cfg.StderrCapture.Write([]byte("Error: 502 Bad Gateway returned")) return errors.New("install failed") } @@ -527,7 +531,7 @@ func TestExecuteShellCommandWithRetry_NonMatchingError_FailsFast(t *testing.T) { fakeInvoke := func(opts ...ShellCommandOption) error { called++ cfg := applyOptsForTest(opts) - _, _ = cfg.stderrCapture.Write([]byte("Error: invalid resource argument")) + _, _ = cfg.StderrCapture.Write([]byte("Error: invalid resource argument")) return errors.New("plan failed") } @@ -552,7 +556,7 @@ func TestExecuteShellCommandWithRetry_RecoveryAfterTransient(t *testing.T) { called++ cfg := applyOptsForTest(opts) if called < 3 { - _, _ = cfg.stderrCapture.Write([]byte("502 Bad Gateway")) + _, _ = cfg.StderrCapture.Write([]byte("502 Bad Gateway")) return errors.New("transient") } // Buffer reset between attempts means the success path writes nothing @@ -605,12 +609,12 @@ func TestExecuteShellCommandWithRetry_BufferResetBetweenAttempts(t *testing.T) { called++ cfg := applyOptsForTest(opts) if called == 1 { - _, _ = cfg.stderrCapture.Write([]byte("502 Bad Gateway")) + _, _ = cfg.StderrCapture.Write([]byte("502 Bad Gateway")) return errors.New("transient") } // Second attempt emits a NON-matching error; if the buffer carried over // from attempt 1 the predicate would still match and we'd keep retrying. - _, _ = cfg.stderrCapture.Write([]byte("permission denied")) + _, _ = cfg.StderrCapture.Write([]byte("permission denied")) return errors.New("real failure") } @@ -620,7 +624,7 @@ func TestExecuteShellCommandWithRetry_BufferResetBetweenAttempts(t *testing.T) { } // TestExecuteShellCommandWithRetry_ExecMetadataBufsResetBetweenAttempts verifies -// that the exec-metadata stdout/stderr tee buffers (withExecMetadataOutputCapture, +// that the exec-metadata stdout/stderr tee buffers (component.WithMetadataOutputCapture, // carried through baseOpts) are reset at the start of every retry attempt, so the // exec-metadata parser only ever sees the latest attempt's output rather than // every attempt's output concatenated together. @@ -634,19 +638,19 @@ func TestExecuteShellCommandWithRetry_ExecMetadataBufsResetBetweenAttempts(t *te } var execStdoutBuf, execStderrBuf bytes.Buffer - baseOpts := []ShellCommandOption{withExecMetadataOutputCapture(&execStdoutBuf, &execStderrBuf)} + baseOpts := []ShellCommandOption{component.WithMetadataOutputCapture(&execStdoutBuf, &execStderrBuf)} called := 0 fakeInvoke := func(opts ...ShellCommandOption) error { called++ cfg := applyOptsForTest(opts) if called == 1 { - _, _ = cfg.stderrCapture.Write([]byte("502 Bad Gateway")) - _, _ = cfg.execMetadataStdoutCapture.Write([]byte("attempt-1 output")) + _, _ = cfg.StderrCapture.Write([]byte("502 Bad Gateway")) + _, _ = cfg.ExecMetadataStdoutCapture.Write([]byte("attempt-1 output")) return errors.New("transient") } - _, _ = cfg.stderrCapture.Write([]byte("permission denied")) - _, _ = cfg.execMetadataStdoutCapture.Write([]byte("attempt-2 output")) + _, _ = cfg.StderrCapture.Write([]byte("permission denied")) + _, _ = cfg.ExecMetadataStdoutCapture.Write([]byte("attempt-2 output")) return errors.New("real failure") } @@ -683,12 +687,12 @@ func TestExecuteShellCommandWithRetry_ComposesWithCallerCapture(t *testing.T) { fakeInvoke := func(opts ...ShellCommandOption) error { called++ cfg := applyOptsForTest(opts) - _, _ = cfg.stdoutCapture.Write([]byte("some progress\n")) + _, _ = cfg.StdoutCapture.Write([]byte("some progress\n")) if called == 1 { - _, _ = cfg.stderrCapture.Write([]byte("502 Bad Gateway")) + _, _ = cfg.StderrCapture.Write([]byte("502 Bad Gateway")) return errors.New("transient") } - _, _ = cfg.stderrCapture.Write([]byte("ok")) + _, _ = cfg.StderrCapture.Write([]byte("ok")) return nil } @@ -701,7 +705,38 @@ func TestExecuteShellCommandWithRetry_ComposesWithCallerCapture(t *testing.T) { "caller's stderr capture must still receive output when retry is also configured") } -// Compile-time guarantee that we are exercising the same `shellCommandConfig` +// Compile-time guarantee that we are exercising the same `component.ExecutionOptions` // the helper writes into. If the field is renamed this test file will fail to // compile before the regression can ship. -var _ = shellCommandConfig{stdoutCapture: nil, stderrCapture: nil} +var _ = component.ExecutionOptions{StdoutCapture: nil, StderrCapture: nil} + +// TestExecuteShellCommandWithRetry_CancellationStopsBackoff verifies that cancelling +// the caller's context after a recoverable failure prevents another attempt. +func TestExecuteShellCommandWithRetry_CancellationStopsBackoff(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + delay := 100 * time.Millisecond + info := schema.ConfigAndStacksInfo{ + ComponentRetrySection: &schema.RetryConfig{ + MaxAttempts: intPtr(2), + InitialDelay: &delay, + Conditions: []string{"/Bad Gateway/"}, + }, + } + called := 0 + invoke := func(opts ...ShellCommandOption) error { + called++ + require.Same(t, ctx, component.ContextFromOptions(opts...)) + capture := applyOptsForTest(opts) + _, err := capture.StderrCapture.Write([]byte("502 Bad Gateway")) + require.NoError(t, err) + cancel() + return errors.New("transient gateway failure") + } + + err := ExecuteShellCommandWithRetry(&schema.AtmosConfiguration{}, &info, "plan", invoke, + component.WithProcessContext(ctx)) + + require.ErrorIs(t, err, context.Canceled) + assert.Equal(t, 1, called, "cancellation during retry backoff must prevent another command") +} diff --git a/internal/exec/terraform_execute_helpers_recovery_test.go b/pkg/component/terraform/execute_helpers_recovery_test.go similarity index 58% rename from internal/exec/terraform_execute_helpers_recovery_test.go rename to pkg/component/terraform/execute_helpers_recovery_test.go index 5cae6815817..07d98a3e084 100644 --- a/internal/exec/terraform_execute_helpers_recovery_test.go +++ b/pkg/component/terraform/execute_helpers_recovery_test.go @@ -1,34 +1,40 @@ -package exec +package terraform // terraform_execute_helpers_recovery_test.go covers smart init's plan/apply-time recovery // path added by this patch: -// - executeTerraformInitForced (terraform_execute_helpers.go): the forced-reinit helper -// recoverFromInitRequired calls after a plan/apply-time "init is required" diagnostic. -// - recoverFromInitRequired / retryMainCommand (terraform_execute_helpers_exec.go): the +// - testExecutor(t).executeTerraformInitForced (terraform_execute_helpers.go): the forced-reinit helper +// testExecutor(t).recoverFromInitRequired calls after a plan/apply-time "init is required" diagnostic. +// - testExecutor(t).recoverFromInitRequired / testExecutor(t).retryMainCommand (terraform_execute_helpers_exec.go): the // classify -> force-init -> retry-once orchestration itself. // // These use the same seams already established elsewhere in this package: -// executeTerraformInitForcedFn / executeMainTerraformCommandFn function-variable injection +// testExecutor(t).executeTerraformInitForcedFn / testExecutor(t).executeMainTerraformCommandFn function-variable injection // (see TestExecuteMainTerraformCommand_ExplicitInitDispatchesAfterInit), and the test binary // itself as a portable fake terraform/tofu subprocess (os.Executable() + testmain_test.go env -// vars) for executeTerraformInitForced's own subprocess-launching behavior. +// vars) for testExecutor(t).executeTerraformInitForced's own subprocess-launching behavior. import ( "bytes" "errors" + "io" "os" "path/filepath" "testing" + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/component" + "github.com/cloudposse/atmos/pkg/policy/recovery/recoverytest" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" + _ "github.com/cloudposse/atmos/pkg/provisioner/lock" "github.com/cloudposse/atmos/pkg/schema" autoinit "github.com/cloudposse/atmos/pkg/terraform/autoinit" ) // ────────────────────────────────────────────────────────────────────────────── -// executeTerraformInitForced +// testExecutor(t).executeTerraformInitForced // ────────────────────────────────────────────────────────────────────────────── // TestExecuteTerraformInitForced_Success verifies that a successful forced reinit @@ -48,7 +54,7 @@ func TestExecuteTerraformInitForced_Success(t *testing.T) { ComponentEnvList: []string{"_ATMOS_TEST_ARGS_FILE=" + argsFile}, } - initErr := executeTerraformInitForced(&atmosConfig, info, componentPath, "vars.tfvars", true, true) + initErr := testExecutor(t).executeTerraformInitForced(&atmosConfig, info, componentPath, "vars.tfvars", true, true) require.NoError(t, initErr) argsBytes, err := os.ReadFile(argsFile) @@ -78,7 +84,7 @@ func TestExecuteTerraformInitForced_PassVarsAddsVarFileFlag(t *testing.T) { ComponentEnvList: []string{"_ATMOS_TEST_ARGS_FILE=" + argsFile}, } - require.NoError(t, executeTerraformInitForced(&atmosConfig, info, componentPath, "vars.tfvars", false, false)) + require.NoError(t, testExecutor(t).executeTerraformInitForced(&atmosConfig, info, componentPath, "vars.tfvars", false, false)) argsBytes, err := os.ReadFile(argsFile) require.NoError(t, err) @@ -100,7 +106,7 @@ func TestExecuteTerraformInitForced_SubprocessFailurePropagates(t *testing.T) { ComponentEnvList: []string{"_ATMOS_TEST_EXIT_ONE=1"}, } - initErr := executeTerraformInitForced(&atmosConfig, info, componentPath, "vars.tfvars", false, false) + initErr := testExecutor(t).executeTerraformInitForced(&atmosConfig, info, componentPath, "vars.tfvars", false, false) require.Error(t, initErr) markerPath := autoinit.MarkerPath(autoinit.DataDir(componentPath, nil)) @@ -128,7 +134,7 @@ func TestExecuteTerraformInitForced_SubprocessFailureInvalidatesStaleMarker(t *t ComponentEnvList: []string{"_ATMOS_TEST_EXIT_ONE=1"}, } - initErr := executeTerraformInitForced(&atmosConfig, info, componentPath, "vars.tfvars", false, false) + initErr := testExecutor(t).executeTerraformInitForced(&atmosConfig, info, componentPath, "vars.tfvars", false, false) require.Error(t, initErr) _, statErr := os.Stat(markerPath) @@ -136,26 +142,26 @@ func TestExecuteTerraformInitForced_SubprocessFailureInvalidatesStaleMarker(t *t } // ────────────────────────────────────────────────────────────────────────────── -// recoverFromInitRequired +// testExecutor(t).recoverFromInitRequired // ────────────────────────────────────────────────────────────────────────────── -// withForcedInitSeam swaps executeTerraformInitForcedFn for the duration of the test and -// restores the original on cleanup, mirroring the dispatchAfterInitFn seam pattern already +// withForcedInitSeam swaps testExecutor(t).executeTerraformInitForcedFn for the duration of the test and +// restores the original on cleanup, mirroring the testExecutor(t).dispatchAfterInitFn seam pattern already // used in terraform_execute_helpers_workspace_test.go. func withForcedInitSeam(t *testing.T, fake func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, string, string, bool, bool, ...ShellCommandOption) error) { t.Helper() - original := executeTerraformInitForcedFn - t.Cleanup(func() { executeTerraformInitForcedFn = original }) - executeTerraformInitForcedFn = fake + original := testExecutor(t).executeTerraformInitForcedFn + t.Cleanup(func() { testExecutor(t).executeTerraformInitForcedFn = original }) + testExecutor(t).executeTerraformInitForcedFn = fake } -// withMainCommandSeam swaps executeMainTerraformCommandFn (retryMainCommand's own seam) for +// withMainCommandSeam swaps testExecutor(t).executeMainTerraformCommandFn (testExecutor(t).retryMainCommand's own seam) for // the duration of the test. func withMainCommandSeam(t *testing.T, fake func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, []string, string, bool, ...ShellCommandOption) error) { t.Helper() - original := executeMainTerraformCommandFn - t.Cleanup(func() { executeMainTerraformCommandFn = original }) - executeMainTerraformCommandFn = fake + original := testExecutor(t).executeMainTerraformCommandFn + t.Cleanup(func() { testExecutor(t).executeMainTerraformCommandFn = original }) + testExecutor(t).executeMainTerraformCommandFn = fake } // TestRecoverFromInitRequired_InitSubcommandNeverRecovers verifies that a failed `terraform @@ -173,7 +179,7 @@ func TestRecoverFromInitRequired_InitSubcommandNeverRecovers(t *testing.T) { mainErr := errors.New("init failed") var stdoutBuf, stderrBuf bytes.Buffer - err := recoverFromInitRequired(&atmosConfig, info, execCtx, nil, "/tmp/component", false, mainErr, &stdoutBuf, &stderrBuf) + err := testExecutor(t).recoverFromInitRequired(&atmosConfig, info, execCtx, nil, "/tmp/component", false, mainErr, &stdoutBuf, &stderrBuf) require.Error(t, err) assert.Same(t, mainErr, err) } @@ -193,7 +199,7 @@ func TestRecoverFromInitRequired_NoDiagnosisReturnsErrUnchanged(t *testing.T) { stdoutBuf := bytes.NewBufferString("Error: some unrelated validation failure") var stderrBuf bytes.Buffer - err := recoverFromInitRequired(&atmosConfig, info, execCtx, nil, "/tmp/component", false, mainErr, stdoutBuf, &stderrBuf) + err := testExecutor(t).recoverFromInitRequired(&atmosConfig, info, execCtx, nil, "/tmp/component", false, mainErr, stdoutBuf, &stderrBuf) require.Error(t, err) assert.Same(t, mainErr, err) } @@ -214,7 +220,7 @@ func TestRecoverFromInitRequired_OptedOutJoinsPolicyError(t *testing.T) { stdoutBuf := bytes.NewBufferString("Error: Backend configuration changed") var stderrBuf bytes.Buffer - err := recoverFromInitRequired(&atmosConfig, info, execCtx, nil, "/tmp/component", false, mainErr, stdoutBuf, &stderrBuf) + err := testExecutor(t).recoverFromInitRequired(&atmosConfig, info, execCtx, nil, "/tmp/component", false, mainErr, stdoutBuf, &stderrBuf) require.Error(t, err) assert.True(t, errors.Is(err, mainErr)) } @@ -239,7 +245,7 @@ func TestRecoverFromInitRequired_ForcedInitFailureJoinsError(t *testing.T) { stdoutBuf := bytes.NewBufferString("Error: Backend configuration changed") var stderrBuf bytes.Buffer - err := recoverFromInitRequired(&atmosConfig, info, execCtx, nil, "/tmp/component", false, mainErr, stdoutBuf, &stderrBuf) + err := testExecutor(t).recoverFromInitRequired(&atmosConfig, info, execCtx, nil, "/tmp/component", false, mainErr, stdoutBuf, &stderrBuf) require.Error(t, err) assert.True(t, errors.Is(err, mainErr)) assert.True(t, errors.Is(err, forcedInitErr)) @@ -247,7 +253,7 @@ func TestRecoverFromInitRequired_ForcedInitFailureJoinsError(t *testing.T) { // TestRecoverFromInitRequired_SuccessfulRecoveryRetriesMainCommand verifies the full happy // path: a diagnosed failure forces exactly one reinit (with the flags the diagnostic asked -// for) and then retries the main command exactly once via retryMainCommand. +// for) and then retries the main command exactly once via testExecutor(t).retryMainCommand. func TestRecoverFromInitRequired_SuccessfulRecoveryRetriesMainCommand(t *testing.T) { var forcedInitCalls int withForcedInitSeam(t, func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, componentPath, varFile string, reconfigure, upgrade bool, _ ...ShellCommandOption) error { @@ -275,23 +281,23 @@ func TestRecoverFromInitRequired_SuccessfulRecoveryRetriesMainCommand(t *testing stdoutBuf := bytes.NewBufferString("Error: Backend configuration changed") var stderrBuf bytes.Buffer - err := recoverFromInitRequired(&atmosConfig, info, execCtx, []string{"plan", "-out=plan.tfplan"}, "/tmp/component", false, mainErr, stdoutBuf, &stderrBuf) + err := testExecutor(t).recoverFromInitRequired(&atmosConfig, info, execCtx, []string{"plan", "-out=plan.tfplan"}, "/tmp/component", false, mainErr, stdoutBuf, &stderrBuf) require.NoError(t, err) assert.Equal(t, 1, forcedInitCalls) assert.Equal(t, 1, retryCalls) } // ────────────────────────────────────────────────────────────────────────────── -// retryMainCommand +// testExecutor(t).retryMainCommand // ────────────────────────────────────────────────────────────────────────────── -// TestRetryMainCommand_ScopesExecMetadataToRetryOnly verifies that retryMainCommand resets +// TestRetryMainCommand_ScopesExecMetadataToRetryOnly verifies that testExecutor(t).retryMainCommand resets // info.ExecMetadataRawOutput to reflect only the retry attempt's own captured output, not // anything accumulated by the original (failed) attempt. func TestRetryMainCommand_ScopesExecMetadataToRetryOnly(t *testing.T) { withMainCommandSeam(t, func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _ []string, _ string, _ bool, opts ...ShellCommandOption) error { - stdoutW, _ := execMetadataOutputCaptureFromOpts(opts...) - require.NotNil(t, stdoutW, "retryMainCommand must inject its own exec-metadata capture writer") + stdoutW, _ := component.MetadataOutputCaptureFromOptions(opts...) + require.NotNil(t, stdoutW, "testExecutor(t).retryMainCommand must inject its own exec-metadata capture writer") _, err := stdoutW.Write([]byte("retry attempt output")) require.NoError(t, err) return nil @@ -303,7 +309,7 @@ func TestRetryMainCommand_ScopesExecMetadataToRetryOnly(t *testing.T) { ExecMetadataRawOutput: "stale output from the original failed attempt", } - err := retryMainCommand(&atmosConfig, info, []string{"plan"}, "/tmp/component", false, nil) + err := testExecutor(t).retryMainCommand(&atmosConfig, info, []string{"plan"}, "/tmp/component", false, nil) require.NoError(t, err) assert.Equal(t, "retry attempt output", info.ExecMetadataRawOutput, "ExecMetadataRawOutput must reflect only the retry, not the original attempt's stale output") @@ -320,7 +326,146 @@ func TestRetryMainCommand_PropagatesRetryError(t *testing.T) { atmosConfig := schema.AtmosConfiguration{} info := &schema.ConfigAndStacksInfo{SubCommand: "plan"} - err := retryMainCommand(&atmosConfig, info, []string{"plan"}, "/tmp/component", false, nil) + err := testExecutor(t).retryMainCommand(&atmosConfig, info, []string{"plan"}, "/tmp/component", false, nil) require.Error(t, err) assert.Same(t, retryErr, err) } + +// TestImplicitInitRecoversAndLocksProviders proves the implicit init path retries with +// the diagnosed flag, records only successful init, and keeps lock failures best-effort. +func TestImplicitInitRecoversAndLocksProviders(t *testing.T) { + for _, failRecovery := range []bool{false, true} { + name := "successful recovery then failed lock" + if failRecovery { + name = "failed recovery leaves no marker" + } + t.Run(name, func(t *testing.T) { + dir := t.TempDir() + executor := testExecutor(t) + initErr := errors.New("init needs upgrade") + recoveryErr := errors.New("provider download failed") + lockErr := errors.New("lock unavailable") + config := &schema.AtmosConfiguration{} + config.Components.Terraform.Init.Mode = schema.TerraformInitModeAlways + config.Components.Terraform.PluginCache = true + config.Components.Terraform.Platforms = []string{"linux_amd64", "windows_amd64"} + info := &schema.ConfigAndStacksInfo{SubCommand: "plan", Command: "test-terraform", ComponentEnvList: []string{"TF_CLI_CONFIG_FILE=test.rc"}, UIFlagExplicitlySet: true} + var calls [][]string + executor.services.ExecuteShellCommand = func(_ schema.AtmosConfiguration, command string, args []string, gotDir string, env []string, _ bool, _ string, opts ...component.ExecutionOption) error { + calls = append(calls, append([]string(nil), args...)) + require.Equal(t, "test-terraform", command) + require.Equal(t, dir, gotDir) + require.Equal(t, info.ComponentEnvList, env) + require.Same(t, t.Context(), component.ContextFromOptions(opts...)) + switch len(calls) { + case 1: + out, _ := streamingCaptureWriters(opts) + require.NotNil(t, out) + _, err := out.Write([]byte("must use terraform init -upgrade")) + require.NoError(t, err) + return initErr + case 2: + require.Contains(t, args, "-upgrade") + if failRecovery { + return recoveryErr + } + return nil + default: + require.Equal(t, []string{"providers", "lock", "-platform=linux_amd64", "-platform=windows_amd64"}, args) + return lockErr + } + } + err := executor.executeTerraformInitCommand(config, info, dir, "", component.WithProcessContext(t.Context())) + marker, markerErr := autoinit.ReadMarker(autoinit.MarkerPath(autoinit.DataDir(dir, nil))) + if failRecovery { + require.ErrorIs(t, err, recoveryErr) + require.Len(t, calls, 2) + require.NoError(t, markerErr) + require.Nil(t, marker) + return + } + require.NoError(t, err, "provider lock failure must not fail successful init") + require.Len(t, calls, 3) + require.NoError(t, markerErr) + require.NotNil(t, marker) + require.NotEmpty(t, marker.Fingerprint) + require.Contains(t, marker.InitArgs, "-upgrade") + }) + } +} + +func TestShellRetryPolicyActualCommand(t *testing.T) { + for _, tc := range []struct{ kind, phase, parent, nested, event string }{ + {"terraform", "init", "plan", "", "terraform.init.error"}, + {"", "init", "plan", "", "terraform.init.error"}, + {"terraform", "output", "output", "", "terraform.output.error"}, + {"terraform", "workspace-select", "apply", "", "terraform.workspace.select.error"}, + {"terraform", "workspace", "workspace", "select", "terraform.workspace.select.error"}, + {"terraform", "validate", "validate", "", "terraform.*.error"}, + {"helmfile", "sync", "sync", "", "helmfile.apply.error"}, + {"packer", "build", "build", "", "packer.build.error"}, + {"ansible", "playbook", "playbook", "", "ansible.playbook.error"}, + } { + t.Run(tc.kind+"/"+tc.phase, func(t *testing.T) { + recoverytest.Install(t, tc.event, "input.stderr.contains('transient') && input.stdout.contains('attempt')") + info := &schema.ConfigAndStacksInfo{ComponentType: tc.kind, SubCommand: tc.parent, SubCommand2: tc.nested} + var callerCapture, callerStderr bytes.Buffer + attempts := 0 + err := ExecuteShellCommandWithRetry(&schema.AtmosConfiguration{}, info, tc.phase, func(options ...component.ExecutionOption) error { + attempts++ + var config component.ExecutionOptions + for _, option := range options { + option(&config) + } + _, _ = io.WriteString(config.StdoutCapture, "attempt\n") + if attempts < 3 { + _, _ = io.WriteString(config.StderrCapture, "transient\n") + return errors.New("transient") + } + return nil + }, component.WithStdoutCapture(&callerCapture), component.WithStderrCapture(&callerStderr)) + require.NoError(t, err) + assert.Equal(t, 3, attempts) + assert.Equal(t, "attempt\nattempt\nattempt\n", callerCapture.String()) + assert.Equal(t, "transient\ntransient\n", callerStderr.String()) + }) + } +} + +func TestShellRetryPolicyPreservesPlanDetailedExitCode(t *testing.T) { + recoverytest.Install(t, "terraform.plan.error", "true") + info := &schema.ConfigAndStacksInfo{ComponentType: "terraform", SubCommand: "plan"} + attempts := 0 + detailed := errUtils.ExitCodeError{Code: 2} + err := ExecuteShellCommandWithRetry(&schema.AtmosConfiguration{}, info, "plan", func(...component.ExecutionOption) error { attempts++; return detailed }) + assert.Equal(t, 1, attempts) + assert.Equal(t, 2, resolveExitCode(err)) +} + +func TestShellRetryPolicyDoesNotReusePreviousOutput(t *testing.T) { + recoverytest.Install(t, "terraform.init.error", "input.output.contains('transient')") + info := &schema.ConfigAndStacksInfo{ComponentType: "terraform", SubCommand: "plan"} + attempts := 0 + err := ExecuteShellCommandWithRetry(&schema.AtmosConfiguration{}, info, "init", func(options ...component.ExecutionOption) error { + attempts++ + var config component.ExecutionOptions + for _, option := range options { + option(&config) + } + if attempts == 1 { + _, _ = io.WriteString(config.StderrCapture, "transient") + } + return errors.New("failed") + }) + require.Error(t, err) + assert.Equal(t, 2, attempts) +} + +func TestShellRetryPolicyUnselectedEvent(t *testing.T) { + recoverytest.Install(t, "terraform.init.error", "true") + calls := 0 + failure := errors.New("output failed") + err := ExecuteShellCommandWithRetry(&schema.AtmosConfiguration{}, &schema.ConfigAndStacksInfo{}, "output", func(...component.ExecutionOption) error { calls++; return failure }) + require.ErrorIs(t, err, failure) + require.Equal(t, 1, calls) +} diff --git a/internal/exec/terraform_execute_helpers_test.go b/pkg/component/terraform/execute_helpers_test.go similarity index 95% rename from internal/exec/terraform_execute_helpers_test.go rename to pkg/component/terraform/execute_helpers_test.go index 04aa74a71e9..15c3a304134 100644 --- a/internal/exec/terraform_execute_helpers_test.go +++ b/pkg/component/terraform/execute_helpers_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" @@ -17,7 +17,6 @@ import ( errUtils "github.com/cloudposse/atmos/errors" cfg "github.com/cloudposse/atmos/pkg/config" - "github.com/cloudposse/atmos/pkg/degradation" atmosio "github.com/cloudposse/atmos/pkg/io" "github.com/cloudposse/atmos/pkg/provisioner" provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir" @@ -505,7 +504,7 @@ func TestResolveAndProvisionComponentPathPropagatesOutputSuppression(t *testing. atmosConfig := schema.AtmosConfiguration{BasePath: t.TempDir()} info := schema.ConfigAndStacksInfo{FinalComponent: "component"} - _, err := resolveAndProvisionComponentPath(provWorkdir.WithOutputSuppressed(t.Context()), provisioner.OutputWriters{}, &atmosConfig, &info) + _, err := testExecutor(t).resolveAndProvisionComponentPath(provWorkdir.WithOutputSuppressed(t.Context()), provisioner.OutputWriters{}, &atmosConfig, &info) require.NoError(t, err) require.True(t, observedSuppression.Load()) } @@ -678,7 +677,7 @@ func TestAddRegionEnvVarForImport_SkipsForNonImportSubcommands(t *testing.T) { } // ────────────────────────────────────────────────────────────────────────────── -// Secret variables never hit disk (computeTerraformSecretVarKeys / diskSafeVars / secretVarEnv) +// Secret variables never hit disk (ComputeTerraformSecretVarKeys / diskSafeVars / SecretVarEnv) // ────────────────────────────────────────────────────────────────────────────── func TestTerraformSecretVars_NeverHitDisk(t *testing.T) { @@ -694,7 +693,7 @@ func TestTerraformSecretVars_NeverHitDisk(t *testing.T) { }, } - computeTerraformSecretVarKeys(info) + ComputeTerraformSecretVarKeys(info) // Secret-bearing keys (direct, composed substring, nested) are flagged; the plain // non-secret one is not. @@ -716,7 +715,7 @@ func TestTerraformSecretVars_NeverHitDisk(t *testing.T) { assert.Contains(t, string(onDisk), "us-east-1-tfdiskguard", "non-secret vars must still be written") // The secret-bearing vars are instead injected as TF_VAR_* env entries. - env, err := secretVarEnv(info) + env, err := SecretVarEnv(info) require.NoError(t, err) joined := strings.Join(env, "\n") assert.Contains(t, joined, "TF_VAR_plain_password=") @@ -724,18 +723,6 @@ func TestTerraformSecretVars_NeverHitDisk(t *testing.T) { assert.Contains(t, joined, secret, "secret value is carried in the TF_VAR_ env entry") } -func TestTerraformSecretVars_NoSecrets(t *testing.T) { - info := &schema.ConfigAndStacksInfo{ - ComponentVarsSection: map[string]any{"region": "us-west-2-nosecret-xyz", "count": 1}, - } - computeTerraformSecretVarKeys(info) - assert.Nil(t, info.TerraformSecretVarKeys, "no secret keys expected when no secrets present") - - env, err := secretVarEnv(info) - require.NoError(t, err) - assert.Empty(t, env, "no TF_VAR_ entries expected when no secrets present") -} - func TestTerraformSensitiveDeclaredVars_NeverHitDisk(t *testing.T) { atmosio.Reset() t.Cleanup(atmosio.Reset) @@ -757,15 +744,15 @@ func TestTerraformSensitiveDeclaredVars_NeverHitDisk(t *testing.T) { }, } - computeTerraformSecretVarKeys(info) + ComputeTerraformSecretVarKeys(info) require.True(t, info.TerraformSecretVarKeys["sensitive_value"]) assert.False(t, info.TerraformSecretVarKeys["region"]) assert.NotContains(t, diskSafeVars(info), "sensitive_value") assert.Equal(t, "us-east-1-sensitive-declaration", diskSafeVars(info)["region"]) - assert.NotContains(t, varfileVarsToWrite(info, true, "test.tfvars.json"), "sensitive_value") - assert.Equal(t, "us-east-1-sensitive-declaration", varfileVarsToWrite(info, true, "test.tfvars.json")["region"]) + assert.NotContains(t, VarfileVarsToWrite(info, true, "test.tfvars.json"), "sensitive_value") + assert.Equal(t, "us-east-1-sensitive-declaration", VarfileVarsToWrite(info, true, "test.tfvars.json")["region"]) - env, err := secretVarEnv(info) + env, err := SecretVarEnv(info) require.NoError(t, err) assert.Contains(t, strings.Join(env, "\n"), "TF_VAR_sensitive_value=ordinary-resolved-value") } @@ -787,21 +774,11 @@ func TestTerraformSensitiveDeclaredVars_MaskingDisabledPreservesJSONCompatibilit }, } - computeTerraformSecretVarKeys(info) + ComputeTerraformSecretVarKeys(info) assert.Nil(t, info.TerraformSecretVarKeys) assert.Equal(t, info.ComponentVarsSection, diskSafeVars(info)) } -func TestRejectComputedTerraformVars(t *testing.T) { - assert.NoError(t, rejectComputedTerraformVars(map[string]any{"region": "us-east-1"})) - err := rejectComputedTerraformVars(map[string]any{"value": degradation.AtmosComputedValue{}}) - assert.ErrorIs(t, err, errUtils.ErrUnresolvedComputedTerraformVar) - err = rejectComputedTerraformVars(map[string]any{ - "nested": []any{map[string]any{"value": degradation.AtmosComputedValue{}}}, - }) - assert.ErrorIs(t, err, errUtils.ErrUnresolvedComputedTerraformVar) -} - func TestHandleDeploySubcommand(t *testing.T) { tests := []struct { name string diff --git a/internal/exec/terraform_execute_helpers_workspace_test.go b/pkg/component/terraform/execute_helpers_workspace_test.go similarity index 75% rename from internal/exec/terraform_execute_helpers_workspace_test.go rename to pkg/component/terraform/execute_helpers_workspace_test.go index d525a3561cd..54b9845228a 100644 --- a/internal/exec/terraform_execute_helpers_workspace_test.go +++ b/pkg/component/terraform/execute_helpers_workspace_test.go @@ -1,17 +1,20 @@ -package exec +package terraform // terraform_execute_helpers_workspace_test.go contains unit tests for workspace-related // and TTY-gating helpers extracted from ExecuteTerraform: -// - runWorkspaceSetup (all early-return paths + wsOpts branch) +// - testExecutor(t).runWorkspaceSetup (all early-return paths + wsOpts branch) // - checkTTYRequirement (nil-stdin paths) -// - executeMainTerraformCommand (bare-workspace short-circuit + error propagation) +// - testExecutor(t).executeMainTerraformCommand (bare-workspace short-circuit + error propagation) import ( "bytes" "errors" "os" + "path/filepath" "testing" + "github.com/cloudposse/atmos/pkg/component" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -23,7 +26,7 @@ import ( ) // ────────────────────────────────────────────────────────────────────────────── -// runWorkspaceSetup (early-return paths — no shell required) +// testExecutor(t).runWorkspaceSetup (early-return paths — no shell required) // ────────────────────────────────────────────────────────────────────────────── // TestRunWorkspaceSetup_InitSubcommand_ReturnsNil verifies init bypasses workspace @@ -31,7 +34,7 @@ import ( func TestRunWorkspaceSetup_InitSubcommand_ReturnsNil(t *testing.T) { atmosConfig := schema.AtmosConfiguration{} info := schema.ConfigAndStacksInfo{SubCommand: "init"} - err := runWorkspaceSetup(&atmosConfig, &info, "/tmp/component") + err := testExecutor(t).runWorkspaceSetup(&atmosConfig, &info, "/tmp/component") assert.NoError(t, err) } @@ -43,7 +46,7 @@ func TestRunWorkspaceSetup_WorkspaceWithSubCommand2_ReturnsNil(t *testing.T) { SubCommand: "workspace", SubCommand2: "new", } - err := runWorkspaceSetup(&atmosConfig, &info, "/tmp/component") + err := testExecutor(t).runWorkspaceSetup(&atmosConfig, &info, "/tmp/component") assert.NoError(t, err) } @@ -55,7 +58,7 @@ func TestRunWorkspaceSetup_HTTPBackend_ReturnsNil(t *testing.T) { SubCommand: "apply", ComponentBackendType: "http", } - err := runWorkspaceSetup(&atmosConfig, &info, "/tmp/component") + err := testExecutor(t).runWorkspaceSetup(&atmosConfig, &info, "/tmp/component") assert.NoError(t, err) } @@ -65,7 +68,7 @@ func TestRunWorkspaceSetup_TFWorkspaceEnvSet_ReturnsNil(t *testing.T) { t.Setenv("TF_WORKSPACE", "my-workspace") atmosConfig := schema.AtmosConfiguration{} info := schema.ConfigAndStacksInfo{SubCommand: "apply"} - err := runWorkspaceSetup(&atmosConfig, &info, "/tmp/component") + err := testExecutor(t).runWorkspaceSetup(&atmosConfig, &info, "/tmp/component") assert.NoError(t, err) } @@ -79,7 +82,7 @@ func TestRunWorkspaceSetup_WorkspaceNoSubCommand2_TFWorkspace_ReturnsNil(t *test SubCommand: "workspace", SubCommand2: "", } - err := runWorkspaceSetup(&atmosConfig, &info, "/tmp/component") + err := testExecutor(t).runWorkspaceSetup(&atmosConfig, &info, "/tmp/component") assert.NoError(t, err) } @@ -93,7 +96,7 @@ func TestRunWorkspaceSetup_OutputSubcommand_HTTPBackend_EarlyReturn(t *testing.T SubCommand: "output", ComponentBackendType: "http", } - err := runWorkspaceSetup(&atmosConfig, &info, "/tmp/component") + err := testExecutor(t).runWorkspaceSetup(&atmosConfig, &info, "/tmp/component") assert.NoError(t, err) } @@ -111,7 +114,7 @@ func TestRunWorkspaceSetup_OutputSubcommand_DryRun_RedirectsToStderr(t *testing. SubCommand: "output", DryRun: true, } - err := runWorkspaceSetup(&atmosConfig, &info, t.TempDir()) + err := testExecutor(t).runWorkspaceSetup(&atmosConfig, &info, t.TempDir()) assert.NoError(t, err) } @@ -164,19 +167,19 @@ func TestCheckTTYRequirement_NilStdin_NonApply_NoError(t *testing.T) { } // ────────────────────────────────────────────────────────────────────────────── -// executeMainTerraformCommand (bare-workspace short-circuit) +// testExecutor(t).executeMainTerraformCommand (bare-workspace short-circuit) // ────────────────────────────────────────────────────────────────────────────── // TestExecuteMainTerraformCommand_BareWorkspace_ReturnsNil verifies that a bare // "workspace" subcommand (no SubCommand2) is treated as a no-op since the -// workspace listing was already handled by runWorkspaceSetup. +// workspace listing was already handled by testExecutor(t).runWorkspaceSetup. func TestExecuteMainTerraformCommand_BareWorkspace_ReturnsNil(t *testing.T) { atmosConfig := schema.AtmosConfiguration{} info := schema.ConfigAndStacksInfo{ SubCommand: "workspace", SubCommand2: "", } - err := executeMainTerraformCommand(&atmosConfig, &info, []string{"workspace"}, "/tmp/component", false) + err := testExecutor(t).executeMainTerraformCommand(&atmosConfig, &info, []string{"workspace"}, "/tmp/component", false) assert.NoError(t, err) } @@ -204,13 +207,13 @@ func TestExecuteMainTerraformCommand_Error_Propagates(t *testing.T) { DryRun: false, } - execErr := executeMainTerraformCommand( + execErr := testExecutor(t).executeMainTerraformCommand( &atmosConfig, &info, []string{"-test.run=^$"}, // no test matches → exits 0 normally, but env overrides "", // component path: current dir false, // uploadStatusFlag ) - require.Error(t, execErr, "executeMainTerraformCommand must propagate non-zero exit from subprocess") + require.Error(t, execErr, "testExecutor(t).executeMainTerraformCommand must propagate non-zero exit from subprocess") // Verify the error is wrapped as ExitCodeError (the contract of ExecuteShellCommand). var exitCodeErr errUtils.ExitCodeError @@ -223,12 +226,12 @@ func TestExecuteMainTerraformCommand_Error_Propagates(t *testing.T) { } func TestExecuteMainTerraformCommand_ExplicitInitDispatchesAfterInit(t *testing.T) { - originalDispatch := dispatchAfterInitFn - t.Cleanup(func() { dispatchAfterInitFn = originalDispatch }) + originalDispatch := testExecutor(t).dispatchAfterInitFn + t.Cleanup(func() { testExecutor(t).dispatchAfterInitFn = originalDispatch }) var dispatched bool var dispatchOpts []ShellCommandOption - dispatchAfterInitFn = func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, opts ...ShellCommandOption) { + testExecutor(t).dispatchAfterInitFn = func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, opts ...ShellCommandOption) { dispatched = true dispatchOpts = append([]ShellCommandOption(nil), opts...) assert.Equal(t, "/tmp/component", componentPath) @@ -239,7 +242,7 @@ func TestExecuteMainTerraformCommand_ExplicitInitDispatchesAfterInit(t *testing. info := schema.ConfigAndStacksInfo{SubCommand: subcommandInit, DryRun: true} ctx := provWorkdir.WithOutputSuppressed(t.Context()) var stdout, stderr bytes.Buffer - require.NoError(t, executeMainTerraformCommand( + require.NoError(t, testExecutor(t).executeMainTerraformCommand( &atmosConfig, &info, []string{subcommandInit}, @@ -249,17 +252,17 @@ func TestExecuteMainTerraformCommand_ExplicitInitDispatchesAfterInit(t *testing. WithProcessStreams(process.Streams{Stdout: &stdout, Stderr: &stderr}), )) require.True(t, dispatched, "successful explicit init must dispatch after.terraform.init provisioners") - assert.True(t, provWorkdir.OutputSuppressed(shellCommandContext(dispatchOpts...))) - writers := shellCommandOutputWriters(dispatchOpts...) + assert.True(t, provWorkdir.OutputSuppressed(component.ContextFromOptions(dispatchOpts...))) + writers := executionOutputWriters(dispatchOpts...) assert.Same(t, &stdout, writers.Stdout) assert.Same(t, &stderr, writers.Stderr) } func TestExecuteMainTerraformCommand_FailedExplicitInitSkipsAfterInit(t *testing.T) { - originalDispatch := dispatchAfterInitFn - t.Cleanup(func() { dispatchAfterInitFn = originalDispatch }) + originalDispatch := testExecutor(t).dispatchAfterInitFn + t.Cleanup(func() { testExecutor(t).dispatchAfterInitFn = originalDispatch }) - dispatchAfterInitFn = func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, string, ...ShellCommandOption) { + testExecutor(t).dispatchAfterInitFn = func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, string, ...ShellCommandOption) { t.Fatal("failed init must not dispatch after.terraform.init provisioners") } @@ -271,7 +274,7 @@ func TestExecuteMainTerraformCommand_FailedExplicitInitSkipsAfterInit(t *testing Command: exePath, ComponentEnvList: []string{"_ATMOS_TEST_EXIT_ONE=1"}, } - require.Error(t, executeMainTerraformCommand(&atmosConfig, &info, []string{"-test.run=^$"}, "", false)) + require.Error(t, testExecutor(t).executeMainTerraformCommand(&atmosConfig, &info, []string{"-test.run=^$"}, "", false)) } // TestExecuteMainTerraformCommand_ExplicitInitWritesMarker verifies that a successful explicit @@ -288,7 +291,7 @@ func TestExecuteMainTerraformCommand_ExplicitInitWritesMarker(t *testing.T) { Command: exePath, } - require.NoError(t, executeMainTerraformCommand(&atmosConfig, &info, []string{"-test.run=^$"}, componentPath, false)) + require.NoError(t, testExecutor(t).executeMainTerraformCommand(&atmosConfig, &info, []string{"-test.run=^$"}, componentPath, false)) dataDir := autoinit.DataDir(componentPath, nil) marker, markerErr := autoinit.ReadMarker(autoinit.MarkerPath(dataDir)) @@ -318,7 +321,7 @@ func TestExecuteMainTerraformCommand_LocalNeutralization_ExecMetadataOnly(t *tes ExecMetadataDetailedExitCodeAdded: true, } - execErr := executeMainTerraformCommand(&atmosConfig, &info, []string{"-test.run=^$"}, "", false) + execErr := testExecutor(t).executeMainTerraformCommand(&atmosConfig, &info, []string{"-test.run=^$"}, "", false) assert.NoError(t, execErr, "exit code 2 from an exec-metadata-only -detailed-exitcode addition must be neutralized for Atmos's own returned status") assert.Equal(t, 2, info.ExecMetadataRawExitCode, "the real subprocess exit code must still be recorded for TerraformExecData.exit_code") @@ -343,7 +346,7 @@ func TestExecuteMainTerraformCommand_NoNeutralization_UploadStatusOnly(t *testin // ExecMetadataDetailedExitCodeAdded intentionally left false. } - execErr := executeMainTerraformCommand(&atmosConfig, &info, []string{"-test.run=^$"}, "", true) + execErr := testExecutor(t).executeMainTerraformCommand(&atmosConfig, &info, []string{"-test.run=^$"}, "", true) require.Error(t, execErr, "exit code 2 must still propagate when -detailed-exitcode wasn't added for exec-metadata reasons") var exitCodeErr errUtils.ExitCodeError @@ -370,7 +373,7 @@ func TestExecuteMainTerraformCommand_GlobalCIRemap_StillTakesPrecedence(t *testi ExecMetadataDetailedExitCodeAdded: true, } - execErr := executeMainTerraformCommand(&atmosConfig, &info, []string{"-test.run=^$"}, "", false) + execErr := testExecutor(t).executeMainTerraformCommand(&atmosConfig, &info, []string{"-test.run=^$"}, "", false) assert.NoError(t, execErr) assert.Equal(t, 2, info.ExecMetadataRawExitCode) @@ -388,7 +391,7 @@ func TestCaptureExecMetadataSync_RawExitCodeSurvivesGlobalRemap(t *testing.T) { info := &schema.ConfigAndStacksInfo{ ComponentFromArg: "cdn", Stack: "plat-use2-dev", - ExecMetadataRawExitCode: 2, // set by executeMainTerraformCommand before any remap. + ExecMetadataRawExitCode: 2, // set by testExecutor(t).executeMainTerraformCommand before any remap. } var gotExitCode int @@ -398,10 +401,60 @@ func TestCaptureExecMetadataSync_RawExitCodeSurvivesGlobalRemap(t *testing.T) { } atmosConfig := &schema.AtmosConfiguration{} - // params.Err is nil, mirroring executeMainTerraformCommand's neutralized return + // params.Err is nil, mirroring testExecutor(t).executeMainTerraformCommand's neutralized return // for Atmos's own status — the base envelope's exit_code (FR-003) derives from // this and correctly reports 0/success. captureExecMetadataSync(atmosConfig, "plan", info, execMetadataSyncParams{Parser: parser, Err: nil}) assert.Equal(t, 2, gotExitCode, "TerraformExecData.exit_code must report the real pre-remap exit code, not the neutralized 0") } + +// TestWorkspaceSetupPreservesFailureBoundaries distinguishes a missing workspace from +// other command failures and only accepts an already-active workspace after exit code 1. +func TestWorkspaceSetupPreservesFailureBoundaries(t *testing.T) { + t.Setenv("TF_WORKSPACE", "") + t.Setenv("TF_DATA_DIR", "") + selectErr := errors.New("select transport failed") + createErr := errors.New("create transport failed") + cases := []struct { + name string + selectErr error + createErr error + active string + wantErr error + wantCalls int + }{ + {"select non-exit error", selectErr, nil, "", selectErr, 1}, + {"select exit 2", errUtils.ExitCodeError{Code: 2}, nil, "", errUtils.ExitCodeError{Code: 2}, 1}, + {"create succeeds", errUtils.ExitCodeError{Code: 1}, nil, "", nil, 2}, + {"create non-exit error", errUtils.ExitCodeError{Code: 1}, createErr, "dev", createErr, 2}, + {"already active", errUtils.ExitCodeError{Code: 1}, errUtils.ExitCodeError{Code: 1}, "dev", nil, 2}, + {"different active workspace", errUtils.ExitCodeError{Code: 1}, errUtils.ExitCodeError{Code: 1}, "prod", errUtils.ExitCodeError{Code: 1}, 2}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + if tc.active != "" { + require.NoError(t, os.MkdirAll(filepath.Join(dir, ".terraform"), 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(dir, ".terraform", "environment"), []byte(tc.active), 0o600)) + } + executor := testExecutor(t) + calls := 0 + executor.services.ExecuteShellCommand = func(_ schema.AtmosConfiguration, _ string, args []string, gotDir string, _ []string, _ bool, redirect string, _ ...component.ExecutionOption) error { + calls++ + require.Equal(t, dir, gotDir) + require.Equal(t, "workspace-errors.log", redirect) + if calls == 1 { + require.Equal(t, []string{"workspace", "select", "-or-create", "dev"}, args) + return tc.selectErr + } + require.Equal(t, []string{"workspace", "new", "dev"}, args) + return tc.createErr + } + info := &schema.ConfigAndStacksInfo{SubCommand: "plan", TerraformWorkspace: "dev", RedirectStdErr: "workspace-errors.log", UIFlagExplicitlySet: true} + err := executor.runWorkspaceSetup(&schema.AtmosConfiguration{}, info, dir) + require.ErrorIs(t, err, tc.wantErr) + require.Equal(t, tc.wantCalls, calls) + }) + } +} diff --git a/internal/exec/terraform_execute_helpers_workspace_varfile_test.go b/pkg/component/terraform/execute_helpers_workspace_varfile_test.go similarity index 96% rename from internal/exec/terraform_execute_helpers_workspace_varfile_test.go rename to pkg/component/terraform/execute_helpers_workspace_varfile_test.go index b9458f12699..7d0c136824b 100644 --- a/internal/exec/terraform_execute_helpers_workspace_varfile_test.go +++ b/pkg/component/terraform/execute_helpers_workspace_varfile_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "os" @@ -45,7 +45,7 @@ func TestPrintAndWriteVarFiles_WorkspaceSubcommand_PassVars(t *testing.T) { require.NoError(t, printAndWriteVarFiles(&atmosConfig, &info)) - varFilePath := filepath.Join(workdir, constructTerraformComponentVarfileName(&info)) + varFilePath := filepath.Join(workdir, ConstructTerraformComponentVarfileName(&info)) _, err := os.Stat(varFilePath) if !tt.expectWrite { assert.ErrorIs(t, err, os.ErrNotExist, "varfile must not be written") diff --git a/pkg/component/terraform/execute_init.go b/pkg/component/terraform/execute_init.go new file mode 100644 index 00000000000..b267c54a63b --- /dev/null +++ b/pkg/component/terraform/execute_init.go @@ -0,0 +1,594 @@ +package terraform + +import ( + bytes "bytes" + context "context" + errors "errors" + fmt "fmt" + os "os" + filepath "path/filepath" + slices "slices" + strings "strings" + time "time" + + errUtils "github.com/cloudposse/atmos/errors" + component "github.com/cloudposse/atmos/pkg/component" + log "github.com/cloudposse/atmos/pkg/logger" + perf "github.com/cloudposse/atmos/pkg/perf" + provisioner "github.com/cloudposse/atmos/pkg/provisioner" + provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir" + schema "github.com/cloudposse/atmos/pkg/schema" + autoinit "github.com/cloudposse/atmos/pkg/terraform/autoinit" + tfoutput "github.com/cloudposse/atmos/pkg/terraform/output" +) + +// shouldRunTerraformInit returns true when a `terraform init` should be executed as a +// pre-step before the main command. Init is skipped when: the subcommand is init +// itself (init runs as the main command), deploy with DeployRunInit=false is configured, +// the caller passed the --skip-init flag, or components.terraform.init.mode is "never". +// Note this only gates whether the smart-init decision point (executeTerraformInitCommand) +// runs at all -- the decision of whether that pre-step actually shells out to `terraform +// init`, and with which flags, is made by autoinit.Decide once it does run. +// +// The init.mode: never check below excludes the workspace subcommand: autoinit.RequestFromInfo +// forces Decide's decision (Force: true) whenever info.SubCommand == "workspace", independent of +// init.mode, because workspace select/new needs a freshly reconfigured backend regardless of +// whether the ordinary pre-command init would otherwise run -- this predates init.mode entirely +// (see buildInitArgs's history). Gating that decision point out here for mode: never would +// silently drop that forced reconfigure and break workspace select/new for anyone who set +// init.mode: never intending only to suppress the ordinary unconditional pre-command init. +func shouldRunTerraformInit(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) bool { + if info.SubCommand == subcommandInit { + return false + } + if info.SubCommand == subcommandDeploy && !atmosConfig.Components.Terraform.DeployRunInit { + return false + } + if info.SkipInit { + log.Debug("Skipping over 'terraform init' due to '--skip-init' flag being passed") + return false + } + if info.SubCommand != subcommandWorkspace && atmosConfig.Components.Terraform.EffectiveInitMode() == schema.TerraformInitModeNever { + log.Debug("Skipping over 'terraform init' due to components.terraform.init.mode: never") + return false + } + return true +} + +// buildInitArgs constructs the argument list for `terraform init` from an already-computed +// autoinit.Decision: `init`, then `-reconfigure` when decision.Reconfigure, then `-upgrade` +// when decision.Upgrade, then `-var-file ` when Init.PassVars is enabled. +// +// This replaces a former workdir-specific carve-out that ignored the legacy +// InitRunReconfigure setting unless the workdir was actually re-provisioned this invocation +// (WorkdirReprovisionedKey) -- adding -reconfigure unconditionally used to make OpenTofu treat +// init as a fresh backend initialization and prompt "Do you want to migrate all workspaces?" +// whenever it saw existing workspace state directories (terraform.tfstate.d/) but no evidence +// the backend itself had moved; decision.Reconfigure now encodes exactly that rule for every +// component (not just workdir ones): auto reconfigure compares the current and last-recorded +// backend fingerprints (see autoinit.Decide), so -reconfigure is added only when the backend +// configuration actually changed, or a caller explicitly forced it (workspace subcommand, a +// re-provisioned workdir, or `init.reconfigure: always`). +// +//nolint:gocritic // autoinit.Decision (88 bytes) is passed by value throughout this call chain and its tests; not worth a pointer for a non-hot-path helper. +func buildInitArgs(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, varFile string, decision autoinit.Decision) []string { + args := []string{subcommandInit} + if decision.Reconfigure { + args = append(args, "-reconfigure") + } + if decision.Upgrade { + args = append(args, "-upgrade") + } + if atmosConfig.Components.Terraform.Init.PassVars { + args = append(args, varFileFlag, varFile) + } + log.Debug("autoinit: init args", logFieldComponent, info.ComponentFromArg, "args", args) + return args +} + +// newAutoInitInputs builds this invocation's fingerprint inputs -- the thin internal/exec-local +// wrapper around autoinit.InputsFromInfo shared by buildInitSubcommandArgs, recordAutoInit, and +// executeTerraformInitForced. +func newAutoInitInputs(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, varFile string) *autoinit.Inputs { + return autoinit.InputsFromInfo(atmosConfig, info, componentPath, varFile) +} + +// decideAutoInit resolves atmos.yaml's init policy plus force into an autoinit.Request and +// returns autoinit.Decide's outcome -- the thin internal/exec-local wrapper around +// autoinit.RequestFromInfo + autoinit.Decide used by buildInitSubcommandArgs for an explicit +// `atmos terraform init`. +func decideAutoInit(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, in *autoinit.Inputs, force bool) autoinit.Decision { + return autoinit.Decide(autoinit.RequestFromInfo(atmosConfig, info, in, force)) +} + +// recordAutoInit best-effort records the init marker after a successful init -- the thin +// internal/exec-local wrapper around autoinit.RecordFromInfo. +func recordAutoInit(in *autoinit.Inputs, initArgs []string) { + autoinit.RecordFromInfo(in, initArgs) +} + +// classifyInitRecovery applies Atmos's init-recovery policy to a failed main command's captured +// output: classify the diagnostic (autoinit.Classify), then decide whether/how to recover +// (autoinit.ShouldRecover) given the component's resolved init policy and opt-out signals. Used +// by recoverFromInitRequired, smart init's plan/apply-time safety net. +func classifyInitRecovery(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, output string) (autoinit.Recovery, error) { + tf := &atmosConfig.Components.Terraform + return autoinit.ShouldRecover( + autoinit.Classify(output), + tf.EffectiveInitMode(), + tf.EffectiveInitReconfigure(), + tf.EffectiveInitUpgrade(), + autoinit.OptedOut(atmosConfig, info), + ) +} + +// executeTerraformInitForced runs `terraform init` unconditionally with exactly the +// -reconfigure/-upgrade flags smart-init's plan/apply-time recovery (recoverFromInitRequired) +// determined were needed, dispatches the after.terraform.init provisioners, and records the +// resulting marker -- exactly as an implicit init would. +// +//nolint:revive // argument-limit: every parameter is load-bearing context from recoverFromInitRequired's forced init/retry. +func (e *Executor) executeTerraformInitForced(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, varFile string, reconfigure, upgrade bool, opts ...component.ExecutionOption) error { + defer perf.Track(atmosConfig, "exec.executeTerraformInitForced")() + + args := []string{subcommandInit} + if reconfigure { + args = append(args, "-reconfigure") + } + if upgrade { + args = append(args, "-upgrade") + } + if atmosConfig.Components.Terraform.Init.PassVars { + args = append(args, varFileFlag, varFile) + } + + in := newAutoInitInputs(atmosConfig, info, componentPath, varFile) + // Invalidate before attempting: see executeTerraformInitCommand's matching call for why a + // forced init that fails must not leave a stale-but-still-matching marker behind. + autoinit.InvalidateFromInfo(in) + if _, err := e.runTerraformInitSubprocess(atmosConfig, info, componentPath, args, opts...); err != nil { + return err + } + + e.dispatchAfterInit(atmosConfig, info, componentPath, opts...) + recordAutoInit(in, args) + return nil +} + +// prepareInitExecution performs the pre-init housekeeping: +// 1. Deletes the .terraform/environment file so Terraform doesn't prompt for workspace selection +// (skipped for workdir-enabled components — see note below). +// 2. Executes all provisioners registered for the before.terraform.init hook event. +// 3. Returns the effective component path (which may be overridden by a workdir provisioner). +// +// NOTE on cleanTerraformWorkspace and workdir components: +// cleanTerraformWorkspace was designed to prevent workspace-selection prompts when different +// backends are used for the same component across runs. For workdir-enabled components the +// backend configuration is always consistent (generated fresh from the same stack config), +// so deleting .terraform/environment is not only unnecessary — it is actively harmful: +// when -reconfigure or init_run_reconfigure is also used, OpenTofu sees workspace state +// directories (terraform.tfstate.d/) but no .terraform/environment file and interprets the +// situation as a backend migration, producing the "Do you want to migrate all workspaces?" +// prompt on every apply. Skipping the cleanup for workdir components avoids this. +func prepareInitExecution(ctx context.Context, writers provisioner.OutputWriters, atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string) (string, error) { + _, isWorkdir := info.ComponentSection[provWorkdir.WorkdirPathKey].(string) + if !isWorkdir { + cleanTerraformWorkspace(*atmosConfig, componentPath) + } + + if ctx == nil { + ctx = context.Background() + } + provisionCtx, cancel := context.WithTimeout(ctx, 5*time.Minute) + defer cancel() + + if err := executeBeforeInitProvisioners( + provisionCtx, + provisioner.HookEvent(beforeTerraformInitEvent), + atmosConfig, + info.ComponentSection, + info.AuthContext, + writers, + ); err != nil { + return componentPath, fmt.Errorf("provisioner execution failed: %w", err) + } + + if workdirPath, ok := info.ComponentSection[provWorkdir.WorkdirPathKey].(string); ok && workdirPath != "" { + log.Debug("Using workdir path for terraform command", "workdirPath", workdirPath) + return workdirPath, nil + } + + return componentPath, nil +} + +// executeTerraformInitPhase runs `terraform init` as a pre-step before the main command. +// It prepares the init execution environment, builds the init args, and delegates to +// ExecuteShellCommand. Returns the (possibly updated) component path. +// +// MUTUAL EXCLUSION CONTRACT: this function is called ONLY when shouldRunTerraformInit() +// returns true (i.e. SubCommand ≠ "init"). For the "init" subcommand itself, +// buildInitSubcommandArgs in terraform_execute_helpers_args.go handles the provisioner +// invocation via prepareInitExecution. These two code paths must never both execute +// in the same command invocation or provisioners will run twice. +func (e *Executor) executeTerraformInitPhase(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, varFile string, opts ...component.ExecutionOption) (string, error) { + newPath, err := prepareInitExecution(component.ContextFromOptions(opts...), executionOutputWriters(opts...), atmosConfig, info, componentPath) + if err != nil { + return componentPath, err + } + + if err = e.executeTerraformInitCommand(atmosConfig, info, newPath, varFile, opts...); err != nil { + return newPath, err + } + + return newPath, nil +} + +// runTerraformInitSubprocess runs `terraform init` (or a forced re-init retry with recovery +// flags) with args, teeing its stdout+stderr into a buffer so callers can classify the output +// for smart-init recovery diagnostics. Shared by executeTerraformInitCommand's own init run and +// executeCommandPipeline's plan/apply-time forced re-init retry -- the one low-level piece both +// need that pkg/terraform/autoinit itself cannot own, since it talks to ExecuteShellCommandWithRetry +// and executeStreamingOrShell, both internal/exec concerns. +func (e *Executor) runTerraformInitSubprocess(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, args []string, opts ...component.ExecutionOption) (string, error) { + var buf bytes.Buffer + stdoutW, stderrW := composeRetryCaptureWriters(opts, &buf) + captureOpts := append(slices.Clone(opts), component.WithStdoutCapture(stdoutW), component.WithStderrCapture(stderrW)) + + err := ExecuteShellCommandWithRetry( + atmosConfig, + info, + "init", + func(o ...component.ExecutionOption) error { + return e.executeStreamingOrShell(atmosConfig, info, &streamingExecRequest{ + componentPath: componentPath, + args: args, + gatePhase: subcommandInit, + subCommand: subcommandInit, + redirectStdErr: info.RedirectStdErr, + shellOpts: o, + }) + }, + captureOpts..., + ) + return buf.String(), err +} + +// executeTerraformInitCommand is the "smart init" decision point: it fingerprints this +// invocation's init inputs, compares them against the marker recorded by the last successful +// init (autoinit.Decide), and either skips the subprocess entirely or runs it with exactly the +// flags the decision calls for -- recovering once, via autoinit.RecoverInit, if terraform/tofu's +// own output says init needs -upgrade or -reconfigure after all. Unlike executeTerraformInitPhase +// it does NOT run prepareInitExecution — it skips cleanTerraformWorkspace, the +// before.terraform.init provisioners, and workdir-path resolution. Callers that have already +// performed that preparation (e.g. ExecuteTerraformShell, which fires the before.terraform.init +// provisioners itself) use this directly to avoid running the provisioners twice. +func (e *Executor) executeTerraformInitCommand(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, varFile string, opts ...component.ExecutionOption) error { + defer perf.Track(atmosConfig, "exec.executeTerraformInitCommand")() + + in := autoinit.InputsFromInfo(atmosConfig, info, componentPath, varFile) + decision := autoinit.Decide(autoinit.RequestFromInfo(atmosConfig, info, in, false)) + + if !decision.RunInit { + info.InitSkipped = true + autoinit.AnnounceSkipped(info, decision.Reason) + return nil + } + + tf := &atmosConfig.Components.Terraform + args := autoinit.InitArgs(decision, tf.Init.PassVars, varFile) + // Invalidate before attempting: if this init fails, a stale-but-still-matching marker must + // not survive it -- see autoinit.InvalidateFromInfo's doc comment. RecordFromInfo below + // re-records a fresh marker once (and only once) this attempt or its recovery retry succeeds. + autoinit.InvalidateFromInfo(in) + output, err := e.runTerraformInitSubprocess(atmosConfig, info, componentPath, args, opts...) + usedArgs := args + + if err != nil { + var rec autoinit.Recovery + rec, err = autoinit.RecoverInit(autoinit.RecoverInitParams{ + Output: output + "\n" + err.Error(), + Err: err, + Mode: tf.EffectiveInitMode(), + Reconfigure: tf.EffectiveInitReconfigure(), + Upgrade: tf.EffectiveInitUpgrade(), + Rerun: func(r autoinit.Recovery) error { + _, rerunErr := e.runTerraformInitSubprocess(atmosConfig, info, componentPath, autoinit.ApplyRecovery(args, r), opts...) + return rerunErr + }, + Warn: func(msg string, kv ...any) { log.Warn(msg, kv...) }, + }) + if err != nil { + return err + } + usedArgs = autoinit.ApplyRecovery(args, rec) + } + + e.dispatchAfterInit(atmosConfig, info, componentPath, opts...) + autoinit.RecordFromInfo(in, usedArgs) + + return nil +} + +// dispatchAfterInit fires the after.terraform.init provisioners (e.g. the multi-platform +// providers-lock hook) once init has succeeded. It hands them a TerraformExecContext whose +// runner reuses the same binary, env (incl. TF_CLI_CONFIG_FILE pointing at the live proxy), +// and working directory as init, so a `providers lock` runs against the already-warm cache. +// The runner goes through executeStreamingOrShell (not a raw ExecuteShellCommand) so that, +// under --ui, `providers lock`'s own provider-fetch/checksum output (e.g. when a registry +// cache/mirror forces a cross-platform lock) is captured by the same init spinner instead of +// leaking raw terraform/opentofu output between the "Init … completed" and +// "Selected … workspace" lines. Lock completion is best-effort: a failure is logged, not +// propagated, so it never fails the user's plan/apply. +func (e *Executor) dispatchAfterInit(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, opts ...component.ExecutionOption) { + execCtx := &provisioner.TerraformExecContext{ + WorkingDir: componentPath, + Run: func(args []string) error { + return e.executeStreamingOrShell(atmosConfig, info, &streamingExecRequest{ + componentPath: componentPath, + args: args, + gatePhase: subcommandInit, + subCommand: subcommandProvidersLock, + redirectStdErr: info.RedirectStdErr, + shellOpts: opts, + }) + }, + } + + ctx, cancel := context.WithTimeout(component.ContextFromOptions(opts...), 5*time.Minute) + defer cancel() + + if err := provisioner.ExecuteProvisioners( + ctx, + provisioner.HookEvent(afterTerraformInitEvent), + atmosConfig, + info.ComponentSection, + info.AuthContext, + executionOutputWriters(opts...), + execCtx, + ); err != nil { + log.Warn("Failed to complete multi-platform provider lock", "error", err) + } +} + +// handleDeploySubcommand converts `deploy` into `apply` and ensures -auto-approve is +// added when appropriate. When ApplyAutoApprove is set in atmos.yaml, it is also +// applied to plain `apply` subcommands. +func handleDeploySubcommand(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) { + if info.SubCommand == subcommandDeploy { + info.SubCommand = subcommandApply + if !info.UseTerraformPlan && !slices.Contains(info.AdditionalArgsAndFlags, autoApproveFlag) { + info.AdditionalArgsAndFlags = append(info.AdditionalArgsAndFlags, autoApproveFlag) + } + } + + if info.SubCommand == subcommandApply && atmosConfig.Components.Terraform.ApplyAutoApprove && !info.UseTerraformPlan { + if !slices.Contains(info.AdditionalArgsAndFlags, autoApproveFlag) { + info.AdditionalArgsAndFlags = append(info.AdditionalArgsAndFlags, autoApproveFlag) + } + } +} + +// logTerraformContext emits a debug log line with the full execution context +// (executable, command, component, stack, flags, working directory, inheritance chain). +func logTerraformContext(info *schema.ConfigAndStacksInfo, workingDir string) { + command := info.SubCommand + if info.SubCommand2 != "" { + command = fmt.Sprintf("%s %s", info.SubCommand, info.SubCommand2) + } + + var inheritance string + if len(info.ComponentInheritanceChain) > 0 { + inheritance = info.ComponentFromArg + " -> " + strings.Join(info.ComponentInheritanceChain, " -> ") + } + + log.Debug( + "Terraform context", + "executable", info.Command, + "command", command, + logFieldComponent, info.ComponentFromArg, + "stack", info.StackFromArg, + "arguments and flags", info.AdditionalArgsAndFlags, + "terraform component", info.BaseComponentPath, + "inheritance", inheritance, + "working directory", workingDir, + ) +} + +// shouldSkipWorkspaceSetup returns true when workspace setup should be skipped. +func shouldSkipWorkspaceSetup(info *schema.ConfigAndStacksInfo) bool { + if info.SubCommand == subcommandInit || (info.SubCommand == subcommandWorkspace && info.SubCommand2 != "") { + return true + } + // `providers` subcommands (mirror, lock, schema) operate on configuration, not + // state, so they need no workspace — and selecting one fails on never-applied + // components. + if strings.HasPrefix(info.SubCommand, "providers") { + return true + } + if info.ComponentBackendType == "http" { + return true + } + //nolint:forbidigo // TF_WORKSPACE is a Terraform convention, not an Atmos config var. + return os.Getenv("TF_WORKSPACE") != "" +} + +// runWorkspaceSetup selects (or creates) the Terraform workspace before the main command +// runs. It is a no-op when shouldSkipWorkspaceSetup returns true. +func (e *Executor) runWorkspaceSetup(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, opts ...component.ExecutionOption) error { + if shouldSkipWorkspaceSetup(info) { + return nil + } + + // Default: merge workspace-select stderr into stdout and capture both streams. + // Workspace setup is an Atmos pre-command implementation detail; Terraform may + // print "Workspace doesn't exist" or "You're now on a new, empty workspace" + // during normal first-run setup, which is noisy in user-facing command output. + redirectStdErr := "/dev/stdout" + if info.RedirectStdErr != "" { + redirectStdErr = info.RedirectStdErr + } + + // Merge caller-provided opts (e.g., WithEnvironment) with workspace-specific opts. + var workspaceOutput bytes.Buffer + wsOpts := append([]component.ExecutionOption{}, opts...) + wsOpts = append(wsOpts, component.WithStdoutOverride(&workspaceOutput)) + + err := ExecuteShellCommandWithRetry( + atmosConfig, + info, + "workspace-select", + func(o ...component.ExecutionOption) error { + return e.executeStreamingOrShell(atmosConfig, info, &streamingExecRequest{ + componentPath: componentPath, + args: []string{"workspace", "select", "-or-create", info.TerraformWorkspace}, + gatePhase: subcommandInit, + subCommand: subcommandWorkspace, + workspace: info.TerraformWorkspace, + redirectStdErr: redirectStdErr, + shellOpts: o, + }) + }, + wsOpts..., + ) + if err == nil { + return nil + } + + selectStep := workspaceStepOutput{command: "terraform workspace select -or-create", output: workspaceOutput.String()} + + // Exit code 1 means the workspace doesn't exist yet; create it. + var exitCodeErr errUtils.ExitCodeError + if !errors.As(err, &exitCodeErr) || exitCodeErr.Code != 1 { + return workspaceSetupError(err, info.TerraformWorkspace, selectStep) + } + + return e.createWorkspaceFallback(atmosConfig, info, componentPath, selectStep, opts...) +} + +// workspaceStepOutput pairs a workspace subcommand with the output Terraform produced for it. +// Workspace setup output is hidden on success, so it is kept to explain a failure. +type workspaceStepOutput struct { + command string + output string +} + +// workspaceSetupError wraps a failed workspace setup step, attaching the output Terraform +// printed so the real cause (backend auth, permissions, lock, network) is not lost behind a +// bare exit code. The cause stays in the error chain, so the exit code is preserved. +func workspaceSetupError(cause error, workspace string, steps ...workspaceStepOutput) error { + builder := errUtils.Build(errUtils.ErrTerraformWorkspaceOp). + WithCause(cause). + WithContext("workspace", workspace). + WithHint("Check the backend credentials and permissions, then re-run with ATMOS_LOGS_LEVEL=debug for the full command trace") + + var sections []string + for _, step := range steps { + if output := strings.TrimSpace(step.output); output != "" { + sections = append(sections, fmt.Sprintf("%s:\n%s", step.command, output)) + } + } + if len(sections) > 0 { + builder = builder.WithExplanation(strings.Join(sections, "\n\n")) + } + + return builder.Err() +} + +// createWorkspaceFallback attempts to create a new workspace when select fails. +// If workspace creation also fails with exit code 1 and the workspace is already +// active (stale .terraform/environment file), it proceeds with a warning. Otherwise the +// returned error carries the output of both steps (selectStep is the failed select). +func (e *Executor) createWorkspaceFallback(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, selectStep workspaceStepOutput, opts ...component.ExecutionOption) error { + redirectStdErr := info.RedirectStdErr + if redirectStdErr == "" { + redirectStdErr = "/dev/stdout" + } + + var workspaceOutput bytes.Buffer + wsOpts := append([]component.ExecutionOption{}, opts...) + wsOpts = append(wsOpts, component.WithStdoutOverride(&workspaceOutput)) + + newErr := ExecuteShellCommandWithRetry( + atmosConfig, + info, + "workspace-new", + func(o ...component.ExecutionOption) error { + return e.executeStreamingOrShell(atmosConfig, info, &streamingExecRequest{ + componentPath: componentPath, + args: []string{"workspace", "new", info.TerraformWorkspace}, + gatePhase: subcommandInit, + subCommand: subcommandWorkspace, + workspace: info.TerraformWorkspace, + redirectStdErr: redirectStdErr, + shellOpts: o, + }) + }, + wsOpts..., + ) + if newErr == nil { + return nil + } + + // If `workspace new` also fails with exit code 1, the workspace may already be the + // active workspace (the .terraform/environment file names it) but its state directory + // was deleted. In that case we are already in the correct workspace and can proceed. + var newExitCodeErr errUtils.ExitCodeError + if errors.As(newErr, &newExitCodeErr) && newExitCodeErr.Code == 1 && + e.isTerraformCurrentWorkspace(componentPath, info.TerraformWorkspace, info.ComponentEnvList) { + log.Warn("Workspace is already active but its state directory is missing; proceeding — subsequent terraform commands may report missing state", + "workspace", info.TerraformWorkspace) + return nil + } + + newStep := workspaceStepOutput{command: "terraform workspace new", output: workspaceOutput.String()} + return workspaceSetupError(newErr, info.TerraformWorkspace, selectStep, newStep) +} + +// CleanTerraformWorkspace deletes the `.terraform/environment` file from the component directory. +// The `.terraform/environment` file contains the name of the currently selected workspace, +// helping Terraform identify the active workspace context for managing your infrastructure. +// We delete the file to prevent the Terraform prompt asking to select the default or the +// previously used workspace. This happens when different backends are used for the same component. +// +//nolint:nestif // Preserve workspace cleanup and Windows retry behavior during extraction. +func CleanTerraformWorkspace(componentPath string) { + defer perf.Track(nil, "terraform.CleanTerraformWorkspace")() + + // Get `TF_DATA_DIR` ENV variable, default to `.terraform` if not set + //nolint:forbidigo // TF_DATA_DIR is a Terraform convention, not an Atmos configuration variable. + tfDataDir := os.Getenv("TF_DATA_DIR") + if tfDataDir == "" { + tfDataDir = ".terraform" + } + + // Convert relative path to absolute + if !filepath.IsAbs(tfDataDir) { + tfDataDir = filepath.Join(componentPath, tfDataDir) + } + + // Ensure the path is cleaned properly + tfDataDir = filepath.Clean(tfDataDir) + + // Construct the full file path + filePath := filepath.Join(tfDataDir, "environment") + + // Check if the file exists before attempting deletion + if _, err := os.Stat(filePath); err == nil { + log.Debug("Terraform environment file found. Proceeding with deletion.", "file", filePath) + + // Use retry logic on Windows to handle file locking + deleteErr := tfoutput.RetryOnWindows(func() error { + return os.Remove(filePath) + }) + + if deleteErr != nil { + log.Debug("Failed to delete Terraform environment file.", "file", filePath, "error", deleteErr) + } else { + log.Debug("Successfully deleted Terraform environment file.", "file", filePath) + } + } else if os.IsNotExist(err) { + log.Debug("Terraform environment file not found. No action needed.", "file", filePath) + } else { + log.Debug("Error checking Terraform environment file.", "file", filePath, "error", err) + } +} diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go new file mode 100644 index 00000000000..fb1b8f9d4b4 --- /dev/null +++ b/pkg/component/terraform/execute_pipeline.go @@ -0,0 +1,351 @@ +package terraform + +import ( + "bytes" + "context" + "errors" + "fmt" + "path/filepath" + "slices" + "strings" + + auth "github.com/cloudposse/atmos/pkg/auth" + "github.com/cloudposse/atmos/pkg/ci" + "github.com/cloudposse/atmos/pkg/component" + "github.com/cloudposse/atmos/pkg/dependencies" + log "github.com/cloudposse/atmos/pkg/logger" + "github.com/cloudposse/atmos/pkg/policy/planfile" + "github.com/cloudposse/atmos/pkg/provisioner" + "github.com/cloudposse/atmos/pkg/schema" + "github.com/cloudposse/atmos/pkg/ui" +) + +// componentExecContext holds the per-execution state assembled by prepareComponentExecution. +// It is consumed by executeCommandPipeline without further modification. +type componentExecContext struct { + componentPath string + varFile string + testVarFile string + planFile string + workingDir string + tenv *dependencies.ToolchainEnvironment + planPolicy *planfile.Guard +} + +// prepareComponentExecution consolidates all per-component setup into one call: +// path resolution, access checks, toolchain installation, variable file generation, +// OPA/JSON-schema validation, auth pre-hook, config file generation, and env assembly. +// Extracting this reduces ExecuteTerraform's cyclomatic complexity by ~10 decision points. +func (e *Executor) prepareComponentExecution( + ctx context.Context, + writers provisioner.OutputWriters, + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + shouldProcess bool, +) (*componentExecContext, error) { + if err := checkTerraformConfig(*atmosConfig); err != nil { + return nil, err + } + + componentPath, err := e.resolveAndProvisionComponentPath(ctx, writers, atmosConfig, info) + if err != nil { + return nil, err + } + + if err = checkComponentRestrictions(info); err != nil { + return nil, err + } + + var tenv *dependencies.ToolchainEnvironment + if shouldProcess { + tenv, err = resolveAndInstallToolchainDeps(atmosConfig, info) + if err != nil { + return nil, err + } + info.Command = tenv.Resolve(info.Command) + } + + varFile := ConstructTerraformComponentVarfileName(info) + testVarFile := "" + if hasTerraformTestVars(info) { + testVarFile = ConstructTerraformComponentTestVarfileName(info) + } + planFile := ConstructTerraformComponentPlanfileName(info) + workingDir := ConstructTerraformComponentWorkingDir(atmosConfig, info) + + if err = e.runPreExecutionSteps(atmosConfig, info, workingDir, tenv); err != nil { + return nil, err + } + + return &componentExecContext{ + componentPath: componentPath, + varFile: varFile, + testVarFile: testVarFile, + planFile: planFile, + workingDir: workingDir, + tenv: tenv, + }, nil +} + +// runPreExecutionSteps performs validation, auth pre-hook, config file generation, +// env var warnings, and env var assembly. Extracted from prepareComponentExecution +// to keep cyclomatic complexity within bounds. +func (e *Executor) runPreExecutionSteps( + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + workingDir string, + tenv *dependencies.ToolchainEnvironment, +) error { + // `(computed)` is an inspection-only degradation marker. Never permit it to + // reach Terraform via a generated tfvars file or TF_VAR_ environment value. + if err := rejectComputedTerraformVars(info.ComponentVarsSection); err != nil { + return err + } + + // Partition variables into disk-safe vs. secret-bearing BEFORE writing the varfile or + // assembling env vars (and before the auth pre-hook registers credentials with the + // masker), so secrets are kept off disk and injected as TF_VAR_* env vars instead. + ComputeTerraformSecretVarKeys(info) + + if err := printAndWriteVarFiles(atmosConfig, info); err != nil { + return err + } + + if err := e.validateTerraformComponent(atmosConfig, info); err != nil { + return err + } + + if err := auth.TerraformPreHook(atmosConfig, info); err != nil { + // The error is returned and rendered once by the command boundary, so it is only logged + // at debug level here to avoid printing the same failure twice. + log.Debug("Error executing 'atmos auth terraform pre-hook'", + logFieldComponent, info.ComponentFromArg, "error", err) + // Pre-hook failures terminate execution — this matches the original terraform.go behavior. + // Authentication setup failures must not silently produce unauthenticated terraform commands. + return err + } + disableTerraformPluginCacheForExecution(atmosConfig, info) + + // Generate backend and provider-override files. When AutoGenerateFiles=true, + // GenerateFilesForComponent was already called inside resolveAndProvisionComponentPath + // for generate: section files. This call handles the distinct backend/provider-override + // responsibility. + if err := e.GenerateConfigFiles(atmosConfig, info, workingDir); err != nil { + return err + } + + warnOnConflictingEnvVars() + + return assembleComponentEnvVars(atmosConfig, info, tenv) +} + +// executeCommandPipeline runs the full terraform command pipeline after the component +// has been prepared: optional init pre-step, argument construction, workspace setup, +// TTY guard, and final command execution + cleanup. +// Extracting this reduces ExecuteTerraform's cyclomatic complexity by ~7 decision points. +// This builds the CI log-group label for a terraform/tofu phase, +// e.g. "terraform init" or "tofu plan". The command is the resolved executable +// (often an absolute toolchain path), so it is reduced to its base name. +func terraformPhaseLabel(info *schema.ConfigAndStacksInfo, phase string) string { + tool := filepath.Base(strings.TrimSpace(info.Command)) + if tool == "" || tool == "." { + tool = "terraform" + } + return strings.TrimSpace(tool + " " + phase) +} + +func (e *Executor) executeCommandPipeline( + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + execCtx *componentExecContext, + opts ...component.ExecutionOption, +) error { + componentPath := execCtx.componentPath + + if shouldRunTerraformInit(atmosConfig, info) { + // Phase-level CI log grouping (Dimension "phase"): fold the `init` phase + // into its own collapsible group. A no-op unless ci.groups.mode is "auto" + // and this is the outermost Atmos invocation (a terraform run nested inside + // a workflow step stays flat inside the step's group). + err := ci.Group(atmosConfig, ci.DimensionPhase, terraformPhaseLabel(info, subcommandInit), func() error { + var phaseErr error + componentPath, phaseErr = e.executeTerraformInitPhase(atmosConfig, info, componentPath, execCtx.varFile, opts...) + return phaseErr + }) + if err != nil { + return err + } + } + + handleDeploySubcommand(atmosConfig, info) + logTerraformContext(info, execCtx.workingDir) + addTerraformTestVarfileArg(info, execCtx.testVarFile) + + allArgsAndFlags, uploadStatusFlag, err := buildTerraformCommandArgs(atmosConfig, info, execCtx.varFile, execCtx.planFile, &componentPath, opts...) + if err != nil { + return err + } + + if err = e.runWorkspaceSetupPhase(atmosConfig, info, componentPath, opts...); err != nil { + return err + } + + if err = checkTTYRequirement(info); err != nil { + return err + } + + addRegionEnvVarForImport(info) + + if shouldRunMainTerraformCommand(info) { + if err = e.runMainCommandPhase(atmosConfig, info, execCtx, allArgsAndFlags, componentPath, uploadStatusFlag, opts...); err != nil { + return err + } + } + + cleanupTerraformFiles(atmosConfig, info) + return nil +} + +// runMainCommandPhase runs the main plan/apply/destroy/… subcommand under its own CI phase +// group, capturing scoped exec-metadata output (FR-006f), and — on failure — gives smart init +// exactly one chance to recover via recoverFromInitRequired before propagating the error. +func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every parameter is load-bearing context passed straight through to recoverFromInitRequired. + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + execCtx *componentExecContext, + allArgsAndFlags []string, + componentPath string, + uploadStatusFlag bool, + opts ...component.ExecutionOption, +) error { + // FR-006f: capture this invocation's own stdout/stderr into a buffer pair + // scoped to ONLY the main command, in addition to (not instead of) whatever + // WithStdoutCapture/WithStderrCapture already accumulate across the whole + // init+workspace-select+main pipeline for other consumers (e.g. + // cmd/terraform's capturedPlanOutput, used by CI job-summary hooks). Without + // this, incidental init/workspace-select output (e.g. a stray "No changes." + // lookalike) can poison the exec-metadata parser's extraction even though the + // real plan/apply's own output is correct (research.md Decision 32). + var execMetadataStdoutBuf, execMetadataStderrBuf bytes.Buffer + mainOpts := append(slices.Clone(opts), component.WithMetadataOutputCapture(&execMetadataStdoutBuf, &execMetadataStderrBuf)) + + // Phase-level CI log grouping (Dimension "phase"): fold the main subcommand + // (plan/apply/destroy/…) into its own collapsible group, separate from init + // and workspace setup. + mainErr := ci.Group(atmosConfig, ci.DimensionPhase, terraformPhaseLabel(info, info.SubCommand), func() error { + return e.executeGovernedTerraformCommand(atmosConfig, info, allArgsAndFlags, componentPath, uploadStatusFlag, execCtx.planPolicy, mainOpts...) + }) + info.ExecMetadataRawOutput = combineExecMetadataOutput(&execMetadataStdoutBuf, &execMetadataStderrBuf) + if mainErr == nil { + return nil + } + + return e.recoverFromInitRequired(atmosConfig, info, execCtx, allArgsAndFlags, componentPath, uploadStatusFlag, + mainErr, &execMetadataStdoutBuf, &execMetadataStderrBuf, opts...) +} + +// recoverFromInitRequired is smart init's plan/apply-time safety net: when the main command +// just failed, it classifies the captured output for a known "init is required" diagnostic +// (autoinit.Classify) and, if Atmos's own recovery policy says to (autoinit.ShouldRecover), +// forces exactly one `terraform init` re-run with the flag(s) the diagnostic asked for, then +// retries the main command exactly once more. This is the correction for a smart-init bet gone +// wrong: skipping init looked safe based on the fingerprint, but terraform/tofu itself now +// disagrees. +// +// No recovery is attempted for the init subcommand itself (there is no "init required" fallback +// for init) or when ShouldRecover reports a policy error (e.g. the user explicitly disabled +// implicit init) — that error is joined with the original failure and returned as-is. +func (e *Executor) recoverFromInitRequired( //nolint:revive // argument-limit: every parameter is load-bearing context from runMainCommandPhase. + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + execCtx *componentExecContext, + allArgsAndFlags []string, + componentPath string, + uploadStatusFlag bool, + mainErr error, + stdoutBuf, stderrBuf *bytes.Buffer, + opts ...component.ExecutionOption, +) error { + if info.SubCommand == subcommandInit { + return mainErr + } + + combined := stdoutBuf.String() + "\n" + stderrBuf.String() + "\n" + mainErr.Error() + rec, policyErr := classifyInitRecovery(atmosConfig, info, combined) + if policyErr != nil { + return errors.Join(mainErr, policyErr) + } + if !rec.Run { + return mainErr + } + + ui.Warning(fmt.Sprintf("Terraform requires initialization; running init and retrying '%s'", info.SubCommand)) + + initErr := ci.Group(atmosConfig, ci.DimensionPhase, terraformPhaseLabel(info, subcommandInit), func() error { + return e.executeTerraformInitForcedFn(atmosConfig, info, componentPath, execCtx.varFile, rec.WithReconfigure, rec.WithUpgrade, opts...) + }) + if initErr != nil { + return errors.Join(mainErr, initErr) + } + + return e.retryMainCommand(atmosConfig, info, allArgsAndFlags, componentPath, uploadStatusFlag, execCtx.planPolicy, opts...) +} + +// retryMainCommand re-runs the main subcommand once after a forced init recovery, resetting the +// exec-metadata capture so info.ExecMetadataRawOutput reflects only the retry's own output. +func (e *Executor) retryMainCommand( //nolint:revive // argument-limit: every parameter is load-bearing context passed straight through to executeMainTerraformCommandFn. + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + allArgsAndFlags []string, + componentPath string, + uploadStatusFlag bool, + gate *planfile.Guard, + opts ...component.ExecutionOption, +) error { + var execMetadataStdoutBuf, execMetadataStderrBuf bytes.Buffer + mainOpts := append(slices.Clone(opts), component.WithMetadataOutputCapture(&execMetadataStdoutBuf, &execMetadataStderrBuf)) + + err := ci.Group(atmosConfig, ci.DimensionPhase, terraformPhaseLabel(info, info.SubCommand), func() error { + if gate != nil { + return e.executeGovernedTerraformCommand(atmosConfig, info, allArgsAndFlags, componentPath, uploadStatusFlag, gate, mainOpts...) + } + return e.executeMainTerraformCommandFn(atmosConfig, info, allArgsAndFlags, componentPath, uploadStatusFlag, mainOpts...) + }) + info.ExecMetadataRawOutput = combineExecMetadataOutput(&execMetadataStdoutBuf, &execMetadataStderrBuf) + return err +} + +// runWorkspaceSetupPhase selects or creates the Terraform workspace under its own +// phase-level CI log group. The group is emitted only when workspace setup will +// actually run, avoiding empty groups for backends/subcommands that skip setup. +func (e *Executor) runWorkspaceSetupPhase(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, opts ...component.ExecutionOption) error { + if shouldSkipWorkspaceSetup(info) { + return nil + } + return ci.Group(atmosConfig, ci.DimensionPhase, terraformPhaseLabel(info, subcommandWorkspace), func() error { + return e.runWorkspaceSetup(atmosConfig, info, componentPath, opts...) + }) +} + +func shouldRunMainTerraformCommand(info *schema.ConfigAndStacksInfo) bool { + return info.SubCommand != subcommandWorkspace || info.SubCommand2 != "" +} + +// combineExecMetadataOutput concatenates the main command's scoped stdout/stderr +// capture for the exec-metadata parser (FR-006f), mirroring the stdout+"\n"+stderr +// convention cmd/terraform's terraformExecMetadataParserFunc previously applied +// when it owned buffer construction directly. +func combineExecMetadataOutput(stdoutBuf, stderrBuf *bytes.Buffer) string { + combined := stdoutBuf.String() + if errOut := stderrBuf.String(); errOut != "" { + combined += "\n" + errOut + } + return combined +} + +func addTerraformTestVarfileArg(info *schema.ConfigAndStacksInfo, testVarFile string) { + if info.SubCommand != "test" || testVarFile == "" { + return + } + info.AdditionalArgsAndFlags = append([]string{varFileFlag, testVarFile}, info.AdditionalArgsAndFlags...) +} diff --git a/pkg/component/terraform/execute_prepare.go b/pkg/component/terraform/execute_prepare.go new file mode 100644 index 00000000000..a399fdb1831 --- /dev/null +++ b/pkg/component/terraform/execute_prepare.go @@ -0,0 +1,572 @@ +package terraform + +import ( + context "context" + errors "errors" + fmt "fmt" + os "os" + filepath "path/filepath" + time "time" + + tfconfig "github.com/hashicorp/terraform-config-inspect/tfconfig" + + errUtils "github.com/cloudposse/atmos/errors" + component "github.com/cloudposse/atmos/pkg/component" + cfg "github.com/cloudposse/atmos/pkg/config" + degradation "github.com/cloudposse/atmos/pkg/degradation" + atmosio "github.com/cloudposse/atmos/pkg/io" + log "github.com/cloudposse/atmos/pkg/logger" + perf "github.com/cloudposse/atmos/pkg/perf" + provisioner "github.com/cloudposse/atmos/pkg/provisioner" + provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir" + schema "github.com/cloudposse/atmos/pkg/schema" + tfgenerate "github.com/cloudposse/atmos/pkg/terraform/generate" + tfvars "github.com/cloudposse/atmos/pkg/terraform/tfvars" + u "github.com/cloudposse/atmos/pkg/utils" +) + +// resolveAndProvisionComponentPath resolves the filesystem path for a terraform component, +// optionally auto-generates files, performs JIT source provisioning, and validates +// that the resulting directory actually exists. +// The provision-and-resolve component function is a seam for testing JIT provisioning context propagation. +var provisionAndResolveTerraformComponentPath = component.ProvisionAndResolveComponentPath + +// The before-init provisioner function is a seam for testing context propagation. +var executeBeforeInitProvisioners = provisioner.ExecuteProvisioners + +func (e *Executor) resolveAndProvisionComponentPath(ctx context.Context, writers provisioner.OutputWriters, atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (string, error) { + componentPath, err := u.GetComponentPath(atmosConfig, "terraform", info.ComponentFolderPrefix, info.FinalComponent) + if err != nil { + return "", fmt.Errorf("failed to resolve component path: %w", err) + } + + // Provision source before generating files: when provision.workdir.enabled + // is true the resolved path is the workdir, and generated files must land + // there rather than in the base component directory. + if ctx == nil { + ctx = context.Background() + } + ctx, cancel := context.WithTimeout(ctx, 5*time.Minute) + defer cancel() + componentPath, componentPathExists, err := provisionAndResolveTerraformComponentPath( + ctx, writers, atmosConfig, info, cfg.TerraformComponentType, componentPath, + ) + if err != nil { + return "", err + } + + if err = e.autoGenerateComponentFiles(atmosConfig, info, componentPath); err != nil { + return "", err + } + + if !componentPathExists { + basePath, _ := u.GetComponentBasePath(atmosConfig, cfg.TerraformComponentType) + return "", fmt.Errorf( + "%w: '%s' points to the Terraform component '%s', but it does not exist in '%s'", + errUtils.ErrInvalidTerraformComponent, + info.ComponentFromArg, + info.FinalComponent, + basePath, + ) + } + + return componentPath, nil +} + +// autoGenerateComponentFiles creates the component directory and generates source files +// when AutoGenerateFiles is enabled and a generate section is present. +func (e *Executor) autoGenerateComponentFiles(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string) error { + if !atmosConfig.Components.Terraform.AutoGenerateFiles || info.DryRun { + return nil + } + generateSection := tfgenerate.GetGenerateSectionFromComponent(info.ComponentSection) + if generateSection == nil { + return nil + } + if mkdirErr := os.MkdirAll(componentPath, dirPermissions); mkdirErr != nil { + return errors.Join(errUtils.ErrCreateDirectory, fmt.Errorf("auto-generation: %w", mkdirErr)) + } + return e.services.GenerateFilesForComponent(atmosConfig, info, componentPath) +} + +// checkComponentRestrictions returns an error when the requested subcommand is not +// permitted for the component due to its metadata (abstract, locked) or the configured +// backend type (HTTP backend does not support workspaces). +func checkComponentRestrictions(info *schema.ConfigAndStacksInfo) error { + // Abstract components cannot be provisioned. + if info.ComponentIsAbstract { + switch info.SubCommand { + case "plan", subcommandApply, subcommandDeploy, subcommandWorkspace: + return fmt.Errorf( + "%w: the component '%s' cannot be provisioned because it's marked as abstract (metadata.type: abstract)", + errUtils.ErrAbstractComponentCantBeProvisioned, + filepath.Join(info.ComponentFolderPrefix, info.Component), + ) + } + } + + // Locked components may not be mutated. + if info.ComponentIsLocked { + switch info.SubCommand { + case subcommandApply, "deploy", "destroy", "import", "state", "taint", "untaint": + return fmt.Errorf( + "%w: component '%s' cannot be modified (metadata.locked: true)", + errUtils.ErrLockedComponentCantBeProvisioned, + filepath.Join(info.ComponentFolderPrefix, info.Component), + ) + } + } + + // HTTP backend does not support workspace commands. + if info.SubCommand == subcommandWorkspace && info.ComponentBackendType == "http" { + return errUtils.ErrHTTPBackendWorkspaces + } + + return nil +} + +// printAndWriteVarFiles logs component variables and, when not using a pre-existing +// plan file, writes them to the varfile on disk (path derived from atmosConfig+info). +// Workspace subcommands do not use varfiles and are skipped, unless init.pass_vars is enabled: the init +// that runs before a workspace command then receives -var-file, so the varfile must exist even for a +// component that was never planned. +func printAndWriteVarFiles(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { + if info.SubCommand == subcommandWorkspace && !atmosConfig.Components.Terraform.Init.PassVars { + return nil + } + + if err := logAndWriteComponentVars(atmosConfig, info); err != nil { + return err + } + if err := writeTerraformTestVars(atmosConfig, info); err != nil { + return err + } + + return logCliVarsOverrides(atmosConfig, info) +} + +// rejectComputedTerraformVars delegates to the Terraform component. +func rejectComputedTerraformVars(vars map[string]any) error { + return RejectComputedTerraformVars(vars) +} + +// diskSafeVars delegates to the Terraform component. +func diskSafeVars(info *schema.ConfigAndStacksInfo) map[string]any { + return DiskSafeVars(info) +} + +// terraformTestVars delegates to the Terraform component. +func terraformTestVars(info *schema.ConfigAndStacksInfo) map[string]any { + return TerraformTestVars(info) +} + +// hasTerraformTestVars delegates to the Terraform component. +func hasTerraformTestVars(info *schema.ConfigAndStacksInfo) bool { + return HasTerraformTestVars(info) +} + +// diskSafeTerraformTestVars delegates to the Terraform component. +func diskSafeTerraformTestVars(info *schema.ConfigAndStacksInfo) map[string]any { + return DiskSafeTerraformTestVars(info) +} + +// terraformTestSecretVarEnv delegates to the Terraform component. +func terraformTestSecretVarEnv(info *schema.ConfigAndStacksInfo) ([]string, error) { + return TerraformTestSecretVarEnv(info) +} + +// logAndWriteComponentVars logs component variables and writes the varfile to disk +// when not using a pre-existing plan. Secret-bearing variables are excluded from the +// varfile (they are injected as TF_VAR_ env vars in assembleComponentEnvVars). +func logAndWriteComponentVars(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { + log.Debug("Variables for the component in the stack", logFieldComponent, info.ComponentFromArg, "stack", info.Stack) + if atmosConfig.Logs.Level == u.LogLevelTrace || atmosConfig.Logs.Level == u.LogLevelDebug { + if err := u.PrintAsYAMLToFileDescriptor(atmosConfig, info.ComponentVarsSection); err != nil { + return err + } + } + + if !info.UseTerraformPlan { + varFilePath := ConstructTerraformComponentVarfilePath(atmosConfig, info) + log.Debug("Writing the variables", "file", varFilePath) + if !info.DryRun { + if err := u.WriteToFileAsJSON(varFilePath, diskSafeVars(info), filePermissions); err != nil { + return err + } + } + } + return nil +} + +func writeTerraformTestVars(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { + if !hasTerraformTestVars(info) { + return nil + } + varFilePath := ConstructTerraformComponentTestVarfilePath(atmosConfig, info) + log.Debug("Writing Terraform test variables", "file", varFilePath) + if info.DryRun { + return nil + } + return u.WriteToFileAsJSON(varFilePath, diskSafeTerraformTestVars(info), filePermissions) +} + +// logCliVarsOverrides logs CLI variable overrides when present at debug/trace level. +func logCliVarsOverrides(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { + cliVars, ok := info.ComponentSection[cfg.TerraformCliVarsSectionName].(map[string]any) + if !ok || len(cliVars) == 0 { + return nil + } + log.Debug("CLI variables (will override the variables defined in the stack manifests):") + if atmosConfig.Logs.Level == u.LogLevelTrace || atmosConfig.Logs.Level == u.LogLevelDebug { + if err := u.PrintAsYAMLToFileDescriptor(atmosConfig, cliVars); err != nil { + return err + } + } + return nil +} + +// validateTerraformComponent runs OPA/JSON-schema validation policies against the +// component's stack configuration section and returns an error if validation fails. +func (e *Executor) validateTerraformComponent(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { + valid, err := e.services.ValidateComponent( + atmosConfig, + info.ComponentFromArg, + info.ComponentSection, + "", "", nil, 0, + ) + if err != nil { + return err + } + if !valid { + return fmt.Errorf("%w: the component '%s' did not pass the validation policies", + errUtils.ErrInvalidComponent, info.ComponentFromArg) + } + return nil +} + +// GenerateConfigFiles writes the backend configuration, generated files, and +// provider overrides for the component into the working directory. +// +// NOTE: GenerateFilesForComponent is also called by autoGenerateComponentFiles +// (inside resolveAndProvisionComponentPath) when AutoGenerateFiles=true. That call +// handles the generate: section from stack config, while this call handles the +// standard backend/provider override files. The two calls serve different purposes +// and both are needed. This is pre-existing behavior from the original terraform.go. +func (e *Executor) GenerateConfigFiles(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { + defer perf.Track(nil, "terraform.Executor.GenerateConfigFiles")() + + return e.Operations.GenerateConfigFiles(config, info, workingDir) +} + +// ComputeTerraformSecretVarKeys records the set of top-level variable keys whose value +// contains a resolved secret, so they can be kept out of the on-disk varfile and injected +// as TF_VAR_ environment variables instead. It must run after secret resolution +// (ProcessStacks) and before auth credentials are registered with the masker, so that the +// varfile-write and env-assembly steps partition the variables identically. +func ComputeTerraformSecretVarKeys(info *schema.ConfigAndStacksInfo) { + defer perf.Track(nil, "terraform.ComputeTerraformSecretVarKeys")() + + _, secret := tfvars.Partition(info.ComponentVarsSection, atmosio.ContainsSecret) + keys := make(map[string]bool, len(secret)) + for k := range secret { + keys[k] = true + } + + // Terraform's `sensitive = true` is an explicit declaration that an input must + // not be persisted in a generated tfvars file. When masking is disabled, retain + // the established compatibility behavior that keeps typed sensitive values in + // JSON rather than coercing them through TF_VAR_ environment variables. + if atmosio.MaskingEnabled() { + for k := range terraformSensitiveVarKeys(info) { + if _, supplied := info.ComponentVarsSection[k]; supplied { + keys[k] = true + } + } + } + + if len(keys) == 0 { + info.TerraformSecretVarKeys = nil + return + } + info.TerraformSecretVarKeys = keys +} + +// terraformSensitiveVarKeys returns the root-module variable names declared +// `sensitive = true`. Component metadata is populated during stack processing +// with terraform-config-inspect, so this adds no extra filesystem parsing on +// the execution hot path. +func terraformSensitiveVarKeys(info *schema.ConfigAndStacksInfo) map[string]bool { + if info == nil { + return nil + } + componentInfo, ok := info.ComponentSection["component_info"].(map[string]any) + if !ok { + return nil + } + module, ok := componentInfo["terraform_config"].(*tfconfig.Module) + if !ok || module == nil { + return nil + } + + keys := make(map[string]bool) + for name, variable := range module.Variables { + if variable != nil && variable.Sensitive { + keys[name] = true + } + } + return keys +} + +// RejectComputedTerraformVars ensures display-only degraded values cannot cross +// into Terraform through either tfvars JSON or TF_VAR_ environment variables. +func RejectComputedTerraformVars(vars map[string]any) error { + defer perf.Track(nil, "terraform.RejectComputedTerraformVars")() + + for key, value := range vars { + if containsComputedTerraformValue(value) { + return fmt.Errorf("%w: %q", errUtils.ErrUnresolvedComputedTerraformVar, key) + } + } + return nil +} + +// containsComputedTerraformValue recursively detects display-only placeholders in Terraform input values. +func containsComputedTerraformValue(value any) bool { + switch v := value.(type) { + case degradation.AtmosComputedValue: + return true + case *degradation.AtmosComputedValue: + return v != nil + case map[string]any: + for _, child := range v { + if containsComputedTerraformValue(child) { + return true + } + } + case []any: + for _, child := range v { + if containsComputedTerraformValue(child) { + return true + } + } + } + return false +} + +// DiskSafeVars returns ComponentVarsSection with secret-bearing top-level keys removed, +// so resolved secrets are never written to the on-disk varfile. Returns the original map +// unchanged when no secret-bearing variables were detected. +func DiskSafeVars(info *schema.ConfigAndStacksInfo) map[string]any { + defer perf.Track(nil, "terraform.DiskSafeVars")() + + testSecretVars := terraformTestSecretVars(info) + if len(info.TerraformSecretVarKeys) == 0 && len(testSecretVars) == 0 { + return info.ComponentVarsSection + } + safe := make(map[string]any, len(info.ComponentVarsSection)) + for k, v := range info.ComponentVarsSection { + if info.TerraformSecretVarKeys[k] { + continue + } + if _, collidesWithSecretTestVar := testSecretVars[k]; collidesWithSecretTestVar { + continue + } + safe[k] = v + } + return safe +} + +// varsWithoutKeys removes excluded top-level variables, returning the original map when no exclusions exist. +func varsWithoutKeys(vars map[string]any, excluded map[string]bool) map[string]any { + if len(excluded) == 0 { + return vars + } + // `excluded` can include declared inputs that this stack does not supply, so + // use the source size as the allocation hint rather than subtracting it. + safe := make(map[string]any, len(vars)) + for k, v := range vars { + if !excluded[k] { + safe[k] = v + } + } + return safe +} + +// SecretVarEnv returns the TF_VAR_ environment entries for the component's +// secret-bearing variables, for injection into the subprocess environment. +func SecretVarEnv(info *schema.ConfigAndStacksInfo) ([]string, error) { + defer perf.Track(nil, "terraform.SecretVarEnv")() + + if len(info.TerraformSecretVarKeys) == 0 { + return nil, nil + } + secret := make(map[string]any, len(info.TerraformSecretVarKeys)) + for k := range info.TerraformSecretVarKeys { + if v, ok := info.ComponentVarsSection[k]; ok { + secret[k] = v + } + } + return tfvars.SecretEnv(secret) +} + +// TerraformTestVars returns test.vars for the test subcommand, or nil for other commands and absent values. +func TerraformTestVars(info *schema.ConfigAndStacksInfo) map[string]any { + defer perf.Track(nil, "terraform.TerraformTestVars")() + + if info == nil || info.SubCommand != "test" { + return nil + } + testSection, ok := info.ComponentSection[cfg.TestSectionName].(map[string]any) + if !ok { + return nil + } + vars, ok := testSection[cfg.VarsSectionName].(map[string]any) + if !ok || len(vars) == 0 { + return nil + } + return vars +} + +// HasTerraformTestVars reports whether the current test command supplies additional test variables. +func HasTerraformTestVars(info *schema.ConfigAndStacksInfo) bool { + defer perf.Track(nil, "terraform.HasTerraformTestVars")() + + return len(TerraformTestVars(info)) > 0 +} + +// DiskSafeTerraformTestVars excludes secret-bearing values from the variables written to the test varfile. +func DiskSafeTerraformTestVars(info *schema.ConfigAndStacksInfo) map[string]any { + defer perf.Track(nil, "terraform.DiskSafeTerraformTestVars")() + + vars := TerraformTestVars(info) + if len(vars) == 0 { + return nil + } + safe, _ := tfvars.Partition(vars, atmosio.ContainsSecret) + return safe +} + +// terraformTestSecretVars selects test variables that must be passed through the subprocess environment. +func terraformTestSecretVars(info *schema.ConfigAndStacksInfo) map[string]any { + vars := TerraformTestVars(info) + if len(vars) == 0 { + return nil + } + _, secret := tfvars.Partition(vars, atmosio.ContainsSecret) + return secret +} + +// TerraformTestSecretVarEnv encodes secret-bearing test variables as TF_VAR_ environment entries. +func TerraformTestSecretVarEnv(info *schema.ConfigAndStacksInfo) ([]string, error) { + defer perf.Track(nil, "terraform.TerraformTestSecretVarEnv")() + + secret := terraformTestSecretVars(info) + if len(secret) == 0 { + return nil, nil + } + return tfvars.SecretEnv(secret) +} + +// ConstructTerraformComponentWorkingDir constructs the working dir for a terraform component in a stack. +func ConstructTerraformComponentWorkingDir(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) string { + defer perf.Track(atmosConfig, "terraform.ConstructTerraformComponentWorkingDir")() + + // Check if a provisioner (source or workdir) set a workdir path. + // This takes precedence over the standard component path. + // Use filepath.FromSlash to normalize path separators for cross-platform compatibility. + if workdirPath, ok := info.ComponentSection[provWorkdir.WorkdirPathKey].(string); ok && workdirPath != "" { + return filepath.FromSlash(workdirPath) + } + + // If we have a resolved absolute path, use GetComponentPath. + // Otherwise, just construct the path directly (for tests and simple cases). + if atmosConfig.TerraformDirAbsolutePath != "" { + path, err := u.GetComponentPath(atmosConfig, "terraform", info.ComponentFolderPrefix, info.FinalComponent) + if err == nil { + return path + } + // Log error but still try to return something. + log.Debug("Failed to resolve component path, falling back to construction", "error", err) + } + + // Direct path construction without converting to absolute. + // This maintains compatibility with tests that use simple paths. + return filepath.Join( + atmosConfig.BasePath, + atmosConfig.Components.Terraform.BasePath, + info.ComponentFolderPrefix, + info.FinalComponent, + ) +} + +// ConstructTerraformComponentPlanfileName constructs the planfile name for a terraform component in a stack. +func ConstructTerraformComponentPlanfileName(info *schema.ConfigAndStacksInfo) string { + defer perf.Track(nil, "terraform.ConstructTerraformComponentPlanfileName")() + + var planFile string + if len(info.ComponentFolderPrefixReplaced) == 0 { + planFile = fmt.Sprintf("%s-%s.planfile", info.ContextPrefix, info.Component) + } else { + planFile = fmt.Sprintf("%s-%s-%s.planfile", info.ContextPrefix, info.ComponentFolderPrefixReplaced, info.Component) + } + + return planFile +} + +// ConstructTerraformComponentVarfileName constructs the varfile name for a terraform component in a stack. +func ConstructTerraformComponentVarfileName(info *schema.ConfigAndStacksInfo) string { + defer perf.Track(nil, "terraform.ConstructTerraformComponentVarfileName")() + + var varFile string + if len(info.ComponentFolderPrefixReplaced) == 0 { + varFile = fmt.Sprintf("%s-%s.terraform.tfvars.json", info.ContextPrefix, info.Component) + } else { + varFile = fmt.Sprintf("%s-%s-%s.terraform.tfvars.json", info.ContextPrefix, info.ComponentFolderPrefixReplaced, info.Component) + } + + return varFile +} + +// ConstructTerraformComponentTestVarfileName constructs the test varfile name for a terraform component in a stack. +func ConstructTerraformComponentTestVarfileName(info *schema.ConfigAndStacksInfo) string { + defer perf.Track(nil, "terraform.ConstructTerraformComponentTestVarfileName")() + + var varFile string + if len(info.ComponentFolderPrefixReplaced) == 0 { + varFile = fmt.Sprintf("%s-%s.terraform.test.tfvars.json", info.ContextPrefix, info.Component) + } else { + varFile = fmt.Sprintf("%s-%s-%s.terraform.test.tfvars.json", info.ContextPrefix, info.ComponentFolderPrefixReplaced, info.Component) + } + + return varFile +} + +// ConstructTerraformComponentVarfilePath constructs the varfile path for a terraform component in a stack. +func ConstructTerraformComponentVarfilePath(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) string { + defer perf.Track(atmosConfig, "terraform.ConstructTerraformComponentVarfilePath")() + + return filepath.Join( + ConstructTerraformComponentWorkingDir(atmosConfig, info), + ConstructTerraformComponentVarfileName(info), + ) +} + +// ConstructTerraformComponentTestVarfilePath constructs the test varfile path for a terraform component in a stack. +func ConstructTerraformComponentTestVarfilePath(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) string { + defer perf.Track(atmosConfig, "terraform.ConstructTerraformComponentTestVarfilePath")() + + return filepath.Join( + ConstructTerraformComponentWorkingDir(atmosConfig, info), + ConstructTerraformComponentTestVarfileName(info), + ) +} + +// ConstructTerraformComponentPlanfilePath constructs the planfile path for a terraform component in a stack. +func ConstructTerraformComponentPlanfilePath(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) string { + defer perf.Track(atmosConfig, "terraform.ConstructTerraformComponentPlanfilePath")() + + return filepath.Join( + ConstructTerraformComponentWorkingDir(atmosConfig, info), + ConstructTerraformComponentPlanfileName(info), + ) +} diff --git a/internal/exec/terraform_execute_single_invocation_test.go b/pkg/component/terraform/execute_single_invocation_test.go similarity index 97% rename from internal/exec/terraform_execute_single_invocation_test.go rename to pkg/component/terraform/execute_single_invocation_test.go index 6803c529918..64b237cd287 100644 --- a/internal/exec/terraform_execute_single_invocation_test.go +++ b/pkg/component/terraform/execute_single_invocation_test.go @@ -1,4 +1,4 @@ -package exec +package terraform // terraform_execute_single_invocation_test.go guards against the double-execution // regression where executeCommandPipeline was called twice per ExecuteTerraform @@ -77,7 +77,7 @@ func TestExecuteCommandPipeline_SingleInvocation(t *testing.T) { } // We expect an error because the subprocess exits 1. - pipelineErr := executeCommandPipeline(&atmosConfig, &info, execCtx) + pipelineErr := testExecutor(t).executeCommandPipeline(&atmosConfig, &info, execCtx) require.Error(t, pipelineErr, "subprocess exits 1 so pipeline must return an error") // Verify the exit code is preserved through the pipeline as ExitCodeError. diff --git a/internal/exec/terraform_exitcode_test.go b/pkg/component/terraform/exitcode_test.go similarity index 99% rename from internal/exec/terraform_exitcode_test.go rename to pkg/component/terraform/exitcode_test.go index 456a261141c..ae569b8983e 100644 --- a/internal/exec/terraform_exitcode_test.go +++ b/pkg/component/terraform/exitcode_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "errors" diff --git a/internal/exec/terraform_generate_backend.go b/pkg/component/terraform/generate_backend.go similarity index 86% rename from internal/exec/terraform_generate_backend.go rename to pkg/component/terraform/generate_backend.go index 18878ba4554..e25eb8c6d34 100644 --- a/internal/exec/terraform_generate_backend.go +++ b/pkg/component/terraform/generate_backend.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "path/filepath" @@ -38,10 +38,11 @@ func validateBackendTypeRequirements(info *schema.ConfigAndStacksInfo) error { } // ExecuteGenerateBackend generates backend config for a terraform component. -func ExecuteGenerateBackend(opts *GenerateBackendOptions, atmosConfig *schema.AtmosConfiguration) error { +func (s *Operations) ExecuteGenerateBackend(opts *GenerateBackendOptions, atmosConfig *schema.AtmosConfiguration) error { defer perf.Track(atmosConfig, "exec.ExecuteGenerateBackend")() - log.Debug("ExecuteGenerateBackend called", + log.Debug( + "ExecuteGenerateBackend called", "component", opts.Component, "stack", opts.Stack, "processTemplates", opts.ProcessTemplates, @@ -58,13 +59,13 @@ func ExecuteGenerateBackend(opts *GenerateBackendOptions, atmosConfig *schema.At } // Process stacks to get component configuration. - info, err := ProcessStacks(atmosConfig, info, true, opts.ProcessTemplates, opts.ProcessFunctions, opts.Skip, nil) + info, err := s.host.ProcessStacks(atmosConfig, info, true, opts.ProcessTemplates, opts.ProcessFunctions, opts.Skip, nil) if err != nil { return err } // Ensure component exists, provisioning via JIT if needed. - if err := ensureTerraformComponentExists(atmosConfig, &info); err != nil { + if err := EnsureTerraformComponentExists(atmosConfig, &info); err != nil { return err } @@ -88,10 +89,10 @@ func ExecuteGenerateBackend(opts *GenerateBackendOptions, atmosConfig *schema.At // writeBackendConfigFile writes the backend config to a file. func writeBackendConfigFile(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, config map[string]any) error { - // Use constructTerraformComponentWorkingDir to properly handle JIT vendored components + // Use ConstructTerraformComponentWorkingDir to properly handle JIT vendored components // that may have a workdir path set by the source provisioner. backendFilePath := filepath.Join( - constructTerraformComponentWorkingDir(atmosConfig, info), + ConstructTerraformComponentWorkingDir(atmosConfig, info), "backend.tf.json", ) diff --git a/pkg/component/terraform/generate_backend_test.go b/pkg/component/terraform/generate_backend_test.go new file mode 100644 index 00000000000..05192338016 --- /dev/null +++ b/pkg/component/terraform/generate_backend_test.go @@ -0,0 +1,424 @@ +package terraform + +import ( + os "os" + filepath "path/filepath" + testing "testing" + + errUtils "github.com/cloudposse/atmos/errors" + cfg "github.com/cloudposse/atmos/pkg/config" + provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir" + schema "github.com/cloudposse/atmos/pkg/schema" + assert "github.com/stretchr/testify/assert" + require "github.com/stretchr/testify/require" +) + +func TestValidateBackendConfig(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + info *schema.ConfigAndStacksInfo + expectedErr error + }{ + { + name: "Valid backend config", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: "s3", + ComponentBackendSection: map[string]any{"bucket": "test-bucket"}, + }, + expectedErr: nil, + }, + { + name: "Missing backend type", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: "", + ComponentBackendSection: map[string]any{"bucket": "test-bucket"}, + }, + expectedErr: errUtils.ErrMissingTerraformBackendType, + }, + { + name: "Missing backend section", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: "s3", + ComponentBackendSection: nil, + }, + expectedErr: errUtils.ErrMissingTerraformBackendConfig, + }, + { + name: "Both missing", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: "", + ComponentBackendSection: nil, + }, + expectedErr: errUtils.ErrMissingTerraformBackendType, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + err := validateBackendConfig(tt.info) + if tt.expectedErr != nil { + assert.ErrorIs(t, err, tt.expectedErr) + } else { + assert.NoError(t, err) + } + }) + } +} + +func TestValidateBackendTypeRequirements(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + info *schema.ConfigAndStacksInfo + expectedErr error + }{ + { + name: "S3 backend with workspace_key_prefix", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: cfg.BackendTypeS3, + ComponentBackendSection: map[string]any{ + "workspace_key_prefix": "terraform-state", + }, + }, + expectedErr: nil, + }, + { + name: "S3 backend without workspace_key_prefix", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: cfg.BackendTypeS3, + ComponentBackendSection: map[string]any{}, + }, + expectedErr: errUtils.ErrMissingTerraformWorkspaceKeyPrefix, + }, + { + name: "GCS backend with bucket", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: cfg.BackendTypeGCS, + ComponentBackendSection: map[string]any{ + "bucket": "test-bucket", + }, + }, + expectedErr: nil, + }, + { + name: "GCS backend without bucket", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: cfg.BackendTypeGCS, + ComponentBackendSection: map[string]any{}, + }, + expectedErr: errUtils.ErrGCSBucketRequired, + }, + { + name: "Azure backend - no specific requirements", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: cfg.BackendTypeAzurerm, + ComponentBackendSection: map[string]any{}, + }, + expectedErr: nil, + }, + { + name: "Local backend - no specific requirements", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: "local", + ComponentBackendSection: map[string]any{}, + }, + expectedErr: nil, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + err := validateBackendTypeRequirements(tt.info) + if tt.expectedErr != nil { + assert.ErrorIs(t, err, tt.expectedErr) + } else { + assert.NoError(t, err) + } + }) + } +} + +func TestExecuteTerraformGenerateBackendCmd_Deprecated(t *testing.T) { + t.Parallel() + + err := ExecuteTerraformGenerateBackendCmd(nil, nil) + assert.Error(t, err) + assert.ErrorIs(t, err, errUtils.ErrDeprecatedCmdNotCallable) +} + +func TestWriteBackendConfigFile(t *testing.T) { + t.Parallel() + + tempDir := t.TempDir() + componentDir := filepath.Join(tempDir, "components", "terraform", "vpc") + workDir := filepath.Join(tempDir, "workdir", "vpc") + require.NoError(t, os.MkdirAll(componentDir, 0o755)) + require.NoError(t, os.MkdirAll(workDir, 0o755)) + + atmosConfig := &schema.AtmosConfiguration{ + BasePath: tempDir, + Components: schema.Components{ + Terraform: schema.Terraform{ + BasePath: filepath.Join("components", "terraform"), + }, + }, + } + + tests := []struct { + name string + info *schema.ConfigAndStacksInfo + config map[string]any + wantPath string + wantContains string + }{ + { + name: "dry-run skips writing", + info: &schema.ConfigAndStacksInfo{FinalComponent: "vpc", DryRun: true}, + config: map[string]any{"terraform": map[string]any{"backend": map[string]any{"s3": map[string]any{"bucket": "test-bucket"}}}}, + }, + { + name: "writes to component dir", + info: &schema.ConfigAndStacksInfo{FinalComponent: "vpc", ComponentSection: map[string]any{}}, + config: map[string]any{"terraform": map[string]any{"backend": map[string]any{"s3": map[string]any{"bucket": "my-state-bucket", "key": "vpc/terraform.tfstate", "region": "us-east-1"}}}}, + wantPath: filepath.Join(componentDir, "backend.tf.json"), + wantContains: "my-state-bucket", + }, + { + name: "writes to workdir path", + info: &schema.ConfigAndStacksInfo{ + FinalComponent: "vpc", + ComponentSection: map[string]any{provWorkdir.WorkdirPathKey: workDir}, + }, + config: map[string]any{"terraform": map[string]any{"backend": map[string]any{"s3": map[string]any{"bucket": "test"}}}}, + wantPath: filepath.Join(workDir, "backend.tf.json"), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + err := writeBackendConfigFile(atmosConfig, tt.info, tt.config) + assert.NoError(t, err) + + if tt.wantPath != "" { + assert.FileExists(t, tt.wantPath) + if tt.wantContains != "" { + content, readErr := os.ReadFile(tt.wantPath) + require.NoError(t, readErr) + assert.Contains(t, string(content), tt.wantContains) + } + } + }) + } +} + +func TestValidateBackendTypeRequirementsTypeAssertions(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + info *schema.ConfigAndStacksInfo + expectedErr error + }{ + { + name: "S3 backend with workspace_key_prefix as int (wrong type)", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: cfg.BackendTypeS3, + ComponentBackendSection: map[string]any{ + "workspace_key_prefix": 123, // Wrong type, should be string. + }, + }, + expectedErr: errUtils.ErrMissingTerraformWorkspaceKeyPrefix, + }, + { + name: "GCS backend with bucket as int (wrong type)", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: cfg.BackendTypeGCS, + ComponentBackendSection: map[string]any{ + "bucket": 123, // Wrong type, should be string. + }, + }, + expectedErr: errUtils.ErrGCSBucketRequired, + }, + { + name: "S3 backend with empty string workspace_key_prefix", + info: &schema.ConfigAndStacksInfo{ + ComponentBackendType: cfg.BackendTypeS3, + ComponentBackendSection: map[string]any{ + "workspace_key_prefix": "", + }, + }, + expectedErr: nil, // Empty string is still a string type. + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + err := validateBackendTypeRequirements(tt.info) + if tt.expectedErr != nil { + assert.ErrorIs(t, err, tt.expectedErr) + } else { + assert.NoError(t, err) + } + }) + } +} + +func TestConstructTerraformComponentWorkingDir_WithWorkdirPath(t *testing.T) { + t.Parallel() + + // Test workdir path takes precedence (returned verbatim from config). + workdirPath := filepath.Join("workdir", "terraform", "dev-vpc") + atmosConfig := schema.AtmosConfiguration{ + BasePath: "base", + Components: schema.Components{ + Terraform: schema.Terraform{ + BasePath: filepath.Join("components", "terraform"), + }, + }, + } + info := schema.ConfigAndStacksInfo{ + ComponentFolderPrefix: "", + FinalComponent: "vpc", + ComponentSection: map[string]any{ + provWorkdir.WorkdirPathKey: workdirPath, + }, + } + got := ConstructTerraformComponentWorkingDir(&atmosConfig, &info) + assert.Equal(t, workdirPath, got) + + // Test empty workdir path falls back to standard path. + info2 := schema.ConfigAndStacksInfo{ + ComponentFolderPrefix: "", + FinalComponent: "vpc", + ComponentSection: map[string]any{ + provWorkdir.WorkdirPathKey: "", + }, + } + got2 := ConstructTerraformComponentWorkingDir(&atmosConfig, &info2) + assert.Equal(t, filepath.Join("base", "components", "terraform", "vpc"), got2) + + // Test no workdir path uses standard path. + info3 := schema.ConfigAndStacksInfo{ + ComponentFolderPrefix: "", + FinalComponent: "vpc", + ComponentSection: map[string]any{}, + } + got3 := ConstructTerraformComponentWorkingDir(&atmosConfig, &info3) + assert.Equal(t, filepath.Join("base", "components", "terraform", "vpc"), got3) +} + +func TestConstructTerraformComponentVarfilePath_WithWorkdirPath(t *testing.T) { + t.Parallel() + + // Test varfile path uses workdir path when set (JIT vendored component scenario). + workdirPath := filepath.Join("workdir", "terraform", "dev-vpc") + atmosConfig := schema.AtmosConfiguration{ + BasePath: "base", + Components: schema.Components{ + Terraform: schema.Terraform{ + BasePath: filepath.Join("components", "terraform"), + }, + }, + } + info := schema.ConfigAndStacksInfo{ + ContextPrefix: "tenant1-ue2-dev", + ComponentFolderPrefix: "", + Component: "vpc", + FinalComponent: "vpc", + ComponentSection: map[string]any{ + provWorkdir.WorkdirPathKey: workdirPath, + }, + } + got := ConstructTerraformComponentVarfilePath(&atmosConfig, &info) + assert.Equal(t, filepath.Join(workdirPath, "tenant1-ue2-dev-vpc.terraform.tfvars.json"), got) + + // Test varfile path uses standard path when no workdir. + info2 := schema.ConfigAndStacksInfo{ + ContextPrefix: "tenant1-ue2-dev", + ComponentFolderPrefix: "", + Component: "vpc", + FinalComponent: "vpc", + ComponentSection: map[string]any{}, + } + got2 := ConstructTerraformComponentVarfilePath(&atmosConfig, &info2) + assert.Equal(t, filepath.Join("base", "components", "terraform", "vpc", "tenant1-ue2-dev-vpc.terraform.tfvars.json"), got2) +} + +func TestConstructTerraformComponentWorkingDir_JITVendoredComponent(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + workdirPath string + expectedPath string + componentName string + hasSource bool + sourceConfig map[string]any + }{ + { + name: "JIT vendored component with workdir path", + workdirPath: filepath.Join("tmp", "atmos-vendor", "abc123", "modules", "vpc"), + expectedPath: filepath.Join("tmp", "atmos-vendor", "abc123", "modules", "vpc"), + componentName: "vpc", + hasSource: true, + sourceConfig: map[string]any{ + "source": map[string]any{ + "uri": "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=v1.0.0", + }, + }, + }, + { + name: "JIT vendored component with string source", + workdirPath: filepath.Join("tmp", "vendor", "my-component"), + expectedPath: filepath.Join("tmp", "vendor", "my-component"), + componentName: "my-component", + hasSource: true, + sourceConfig: map[string]any{ + "source": "git::https://github.com/org/repo.git?ref=main", + }, + }, + { + name: "Regular component without source (no workdir)", + workdirPath: "", + expectedPath: filepath.Join("base", "components", "terraform", "vpc"), + componentName: "vpc", + hasSource: false, + sourceConfig: map[string]any{}, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + atmosConfig := schema.AtmosConfiguration{ + BasePath: "base", + Components: schema.Components{ + Terraform: schema.Terraform{ + BasePath: filepath.Join("components", "terraform"), + }, + }, + } + + componentSection := tt.sourceConfig + if tt.workdirPath != "" { + componentSection[provWorkdir.WorkdirPathKey] = tt.workdirPath + } + + info := schema.ConfigAndStacksInfo{ + ComponentFolderPrefix: "", + FinalComponent: tt.componentName, + ComponentSection: componentSection, + } + + got := ConstructTerraformComponentWorkingDir(&atmosConfig, &info) + assert.Equal(t, tt.expectedPath, got) + }) + } +} diff --git a/internal/exec/terraform_generate_backends.go b/pkg/component/terraform/generate_backends.go similarity index 87% rename from internal/exec/terraform_generate_backends.go rename to pkg/component/terraform/generate_backends.go index bebef51a8bd..48c50c418bf 100644 --- a/internal/exec/terraform_generate_backends.go +++ b/pkg/component/terraform/generate_backends.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "errors" @@ -20,39 +20,43 @@ import ( ) // backendConfig holds extracted backend configuration from a component section. -type backendConfig struct { +type BackendConfig struct { BackendSection map[string]any BackendType string Err error } -// extractBackendConfig extracts and validates backend configuration from a component section. -func extractBackendConfig(componentSection map[string]any) backendConfig { +// ExtractBackendConfig extracts and validates backend configuration from a component section. +func ExtractBackendConfig(componentSection map[string]any) BackendConfig { + defer perf.Track(nil, "terraform.ExtractBackendConfig")() + backendSection, ok := componentSection[cfg.BackendSectionName].(map[string]any) if !ok { - return backendConfig{Err: errUtils.ErrBackendSectionMissing} + return BackendConfig{Err: errUtils.ErrBackendSectionMissing} } backendType, ok := componentSection[cfg.BackendTypeSectionName].(string) if !ok { - return backendConfig{Err: errUtils.ErrBackendTypeMissing} + return BackendConfig{Err: errUtils.ErrBackendTypeMissing} } // Check if backend section is empty for backends that require configuration. // The "local" backend can work without any configuration, but remote backends // like s3, gcs, azurerm, and cloud require at least some configuration. if len(backendSection) == 0 && backendType != cfg.BackendTypeLocal { - return backendConfig{Err: errUtils.ErrBackendConfigEmpty} + return BackendConfig{Err: errUtils.ErrBackendConfigEmpty} } - return backendConfig{ + return BackendConfig{ BackendSection: backendSection, BackendType: backendType, } } -// checkBackendTypeAfterProcessing validates that backend_type is not empty after template processing. -func checkBackendTypeAfterProcessing(backendType string) error { +// CheckBackendTypeAfterProcessing validates that backend_type is not empty after template processing. +func CheckBackendTypeAfterProcessing(backendType string) error { + defer perf.Track(nil, "terraform.CheckBackendTypeAfterProcessing")() + if backendType == "" { return errUtils.ErrBackendTypeEmptyAfterRender } @@ -60,7 +64,9 @@ func checkBackendTypeAfterProcessing(backendType string) error { } // ExecuteTerraformGenerateBackends generates backend configs for all terraform components. -func ExecuteTerraformGenerateBackends( +// +//nolint:cyclop,funlen,gocognit,revive // Preserve the existing generation and evaluation order during package extraction. +func (s *Operations) ExecuteTerraformGenerateBackends( atmosConfig *schema.AtmosConfiguration, fileTemplate string, format string, @@ -69,7 +75,7 @@ func ExecuteTerraformGenerateBackends( ) error { defer perf.Track(atmosConfig, "exec.ExecuteTerraformGenerateBackends")() - stacksMap, rawStackConfigs, deferredContexts, err := FindStacksMap(atmosConfig, false) + stacksMap, rawStackConfigs, deferredContexts, err := s.host.FindStacksMap(atmosConfig, false) if err != nil { return err } @@ -119,7 +125,7 @@ func ExecuteTerraformGenerateBackends( } // Extract backend configuration. - backend := extractBackendConfig(componentSection) + backend := ExtractBackendConfig(componentSection) if backend.Err != nil { log.Warn("Skipping backend generation: "+backend.Err.Error()+". "+ "Set 'components.terraform.auto_generate_backend_file: false' in atmos.yaml to disable.", @@ -188,7 +194,7 @@ func ExecuteTerraformGenerateBackends( // Snapshot the complete merged component section (auth, required_providers, // generate, etc. included), not just the sections this generator directly // consumes — see cloneComponentSectionWithOverrides. - ComponentSection: cloneComponentSectionWithOverrides(componentSection, map[string]any{ + ComponentSection: s.host.CloneComponentSectionWithOverrides(componentSection, map[string]any{ cfg.VarsSectionName: varsSection, cfg.MetadataSectionName: metadataSection, cfg.SettingsSectionName: settingsSection, @@ -223,12 +229,12 @@ func ExecuteTerraformGenerateBackends( // Stack name var stackName string if atmosConfig.Stacks.NameTemplate != "" { - stackName, err = ProcessTmpl(atmosConfig, "terraform-generate-backends-template", atmosConfig.Stacks.NameTemplate, configAndStacksInfo.ComponentSection, atmosConfig.Templates.Settings.IgnoreMissingTemplateValues) + stackName, err = s.host.ProcessTmpl(atmosConfig, "terraform-generate-backends-template", atmosConfig.Stacks.NameTemplate, configAndStacksInfo.ComponentSection, atmosConfig.Templates.Settings.IgnoreMissingTemplateValues) if err != nil { return err } } else { - stackName, err = cfg.GetContextPrefix(stackFileName, context, GetStackNamePattern(atmosConfig), stackFileName) + stackName, err = cfg.GetContextPrefix(stackFileName, context, atmosConfig.Stacks.NamePattern, stackFileName) if err != nil { return err } @@ -236,7 +242,7 @@ func ExecuteTerraformGenerateBackends( // A computed name is not final yet. Keep derived metadata from being // interpreted as another Starlark program during value evaluation. - if containsStarlark(stackName) { + if s.host.ContainsStarlark(stackName) { stackName = stackFileName } @@ -271,7 +277,7 @@ func ExecuteTerraformGenerateBackends( componentTemplateContext[k] = v } - componentSectionProcessed, err := ProcessTmplWithDatasources( + componentSectionProcessed, err := s.host.ProcessTmplWithDatasources( atmosConfig, &configAndStacksInfo, settingsSectionStruct, @@ -297,9 +303,9 @@ func ExecuteTerraformGenerateBackends( } configAndStacksInfo.ComponentSection = componentSectionConverted - configAndStacksInfo.StackLocalsSection = stackLocalsForComponent(rawStackConfigs, stackFileName, cfg.TerraformComponentType) - yamlSkip, finishValues := prepareConfigurationValues(atmosConfig, &configAndStacksInfo, nil, nil) - componentSectionFinal, err := ProcessCustomYamlTags(atmosConfig, componentSectionConverted, stackName, yamlSkip, &configAndStacksInfo) + configAndStacksInfo.StackLocalsSection = s.host.StackLocalsForComponent(rawStackConfigs, stackFileName, cfg.TerraformComponentType) + yamlSkip, finishValues := s.host.PrepareConfigurationValues(atmosConfig, &configAndStacksInfo, nil, nil) + componentSectionFinal, err := s.host.ProcessCustomYamlTags(atmosConfig, componentSectionConverted, stackName, yamlSkip, &configAndStacksInfo) if err != nil { return err } @@ -313,14 +319,14 @@ func ExecuteTerraformGenerateBackends( // unresolved function string (or a placeholder) would silently lose the function's // contribution here, the same #2888 data-loss bug the main describe/plan path fixes // via this same call (see internal/exec/utils.go). - if err := resolveDeferredYamlFunctions(atmosConfig, &configAndStacksInfo, &settingsSectionStruct, componentTemplateContext, nil, nil); err != nil { + if err := s.host.ResolveDeferredYamlFunctions(atmosConfig, &configAndStacksInfo, &settingsSectionStruct, componentTemplateContext, nil, nil); err != nil { return err } if err := finishValues(); err != nil { return err } configAndStacksInfo.ComponentSection = literalsource.RestoreMap(configAndStacksInfo.ComponentSection) - if err := refreshTerraformGeneratorContext(atmosConfig, &configAndStacksInfo, stackFileName); err != nil { + if err := s.refreshTerraformGeneratorContext(atmosConfig, &configAndStacksInfo, stackFileName); err != nil { return err } context = configAndStacksInfo.Context @@ -336,7 +342,7 @@ func ExecuteTerraformGenerateBackends( } // Skip if backend_type is empty after template processing. - if err := checkBackendTypeAfterProcessing(backendTypeSection); err != nil { + if err := CheckBackendTypeAfterProcessing(backendTypeSection); err != nil { log.Warn("Skipping backend generation: "+err.Error()+". "+ "Set 'components.terraform.auto_generate_backend_file: false' in atmos.yaml to disable.", "component", componentName, "stack", stackName) diff --git a/internal/exec/terraform_generate_context.go b/pkg/component/terraform/generate_context.go similarity index 74% rename from internal/exec/terraform_generate_context.go rename to pkg/component/terraform/generate_context.go index b969a40b85c..eb394ca73c2 100644 --- a/internal/exec/terraform_generate_context.go +++ b/pkg/component/terraform/generate_context.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( cfg "github.com/cloudposse/atmos/pkg/config" @@ -7,7 +7,7 @@ import ( // refreshTerraformGeneratorContext derives output identity after configuration // values have resolved, preserving the component identity supplied by the generator. -func refreshTerraformGeneratorContext(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, stackFileName string) error { +func (s *Operations) refreshTerraformGeneratorContext(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, stackFileName string) error { vars, _ := info.ComponentSection[cfg.VarsSectionName].(map[string]any) context := cfg.GetContextFromVars(vars) context.Component = info.Context.Component @@ -16,10 +16,10 @@ func refreshTerraformGeneratorContext(config *schema.AtmosConfiguration, info *s var stackName string var err error if config.Stacks.NameTemplate != "" { - stackName, err = ProcessTmpl(config, "terraform-generate-stack-name", config.Stacks.NameTemplate, + stackName, err = s.host.ProcessTmpl(config, "terraform-generate-stack-name", config.Stacks.NameTemplate, info.ComponentSection, config.Templates.Settings.IgnoreMissingTemplateValues) } else { - stackName, err = cfg.GetContextPrefix(stackFileName, context, GetStackNamePattern(config), stackFileName) + stackName, err = cfg.GetContextPrefix(stackFileName, context, config.Stacks.NamePattern, stackFileName) } if err != nil { return err diff --git a/internal/exec/terraform_generate_planfile.go b/pkg/component/terraform/generate_planfile.go similarity index 80% rename from internal/exec/terraform_generate_planfile.go rename to pkg/component/terraform/generate_planfile.go index 5328151790f..9191ca7b27a 100644 --- a/internal/exec/terraform_generate_planfile.go +++ b/pkg/component/terraform/generate_planfile.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "errors" @@ -29,7 +29,7 @@ var ( type PlanfileOptions = tf.PlanfileOptions // ExecuteGeneratePlanfile generates a planfile for a terraform component. -func ExecuteGeneratePlanfile(opts *PlanfileOptions, atmosConfig *schema.AtmosConfiguration) error { +func (s *Operations) ExecuteGeneratePlanfile(opts *PlanfileOptions, atmosConfig *schema.AtmosConfiguration) error { defer perf.Track(atmosConfig, "exec.ExecuteGeneratePlanfile")() log.Debug( @@ -51,11 +51,11 @@ func ExecuteGeneratePlanfile(opts *PlanfileOptions, atmosConfig *schema.AtmosCon CliArgs: []string{"terraform", "generate", "planfile"}, } - return ExecuteTerraformGeneratePlanfile(opts, info) + return s.ExecuteTerraformGeneratePlanfile(opts, info) } // ExecuteTerraformGeneratePlanfileOld executes `terraform generate planfile` command. -func ExecuteTerraformGeneratePlanfileOld(cmd *cobra.Command, args []string) error { +func (s *Operations) ExecuteTerraformGeneratePlanfileOld(cmd *cobra.Command, args []string) error { defer perf.Track(nil, "exec.ExecuteTerraformGeneratePlanfileCmd")() if len(args) == 0 { @@ -94,7 +94,7 @@ func ExecuteTerraformGeneratePlanfileOld(cmd *cobra.Command, args []string) erro return err } - info, err := ProcessCommandLineArgs("terraform", cmd, args, nil) + info, err := s.host.ProcessCommandLineArgs("terraform", cmd, args, nil) if err != nil { return err } @@ -113,21 +113,23 @@ func ExecuteTerraformGeneratePlanfileOld(cmd *cobra.Command, args []string) erro Skip: skip, } - return ExecuteTerraformGeneratePlanfile(&options, &info) + return s.ExecuteTerraformGeneratePlanfile(&options, &info) } // ExecuteTerraformGeneratePlanfile executes `terraform generate planfile`. -func ExecuteTerraformGeneratePlanfile( +// +//nolint:revive,funlen // Preserve the existing generation and evaluation order during package extraction. +func (s *Operations) ExecuteTerraformGeneratePlanfile( options *PlanfileOptions, info *schema.ConfigAndStacksInfo, ) error { defer perf.Track(nil, "exec.ExecuteTerraformGeneratePlanfile")() - if err := validatePlanfileFormat(&options.Format); err != nil { + if err := ValidatePlanfileFormat(&options.Format); err != nil { return err } - if err := validateComponent(options.Component); err != nil { + if err := ValidateComponent(options.Component); err != nil { return err } @@ -145,7 +147,7 @@ func ExecuteTerraformGeneratePlanfile( return err } - *info, err = ProcessStacks(&atmosConfig, *info, true, options.ProcessTemplates, options.ProcessYamlFunctions, options.Skip, nil) + *info, err = s.host.ProcessStacks(&atmosConfig, *info, true, options.ProcessTemplates, options.ProcessYamlFunctions, options.Skip, nil) if err != nil { return err } @@ -166,13 +168,13 @@ func ExecuteTerraformGeneratePlanfile( }(tmpDir) // Generate planfile in the temp directory. - planFile, err := generateNewPlanFile(&atmosConfig, info, componentPath, tmpDir) + planFile, err := s.GenerateNewPlanFile(&atmosConfig, info, componentPath, tmpDir) if err != nil { return err } // Get the JSON representation of the new plan. - planJSON, err := getTerraformPlanJSON(&atmosConfig, info, componentPath, planFile) + planJSON, err := s.getTerraformPlanJSON(&atmosConfig, info, componentPath, planFile) if err != nil { return errors.Join(ErrGettingJsonForPlanfile, err) } @@ -194,8 +196,10 @@ func ExecuteTerraformGeneratePlanfile( return nil } -// validatePlanfileFormat checks if the format is valid and sets default if empty. -func validatePlanfileFormat(format *string) error { +// ValidatePlanfileFormat checks if the format is valid and sets default if empty. +func ValidatePlanfileFormat(format *string) error { + defer perf.Track(nil, "terraform.ValidatePlanfileFormat")() + if *format == "" { *format = "json" } @@ -206,8 +210,10 @@ func validatePlanfileFormat(format *string) error { return nil } -// validateComponent checks if the provided component is not empty. -func validateComponent(component string) error { +// ValidateComponent checks if the provided component is not empty. +func ValidateComponent(component string) error { + defer perf.Track(nil, "terraform.ValidateComponent")() + if component == "" { return ErrNoComponent } @@ -224,7 +230,7 @@ func resolvePlanfilePath(componentPath, format string, customFile string, info * planFilePath = filepath.Join(componentPath, customFile) } } else { - planFilePath = fmt.Sprintf("%s.%s", constructTerraformComponentPlanfilePath(atmosConfig, info), format) + planFilePath = fmt.Sprintf("%s.%s", ConstructTerraformComponentPlanfilePath(atmosConfig, info), format) } err := u.EnsureDir(planFilePath) diff --git a/internal/exec/terraform_generate_required_providers.go b/pkg/component/terraform/generate_required_providers.go similarity index 90% rename from internal/exec/terraform_generate_required_providers.go rename to pkg/component/terraform/generate_required_providers.go index f759642cc43..1e5b968339f 100644 --- a/internal/exec/terraform_generate_required_providers.go +++ b/pkg/component/terraform/generate_required_providers.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" @@ -52,7 +52,7 @@ func parseRequiredProvidersFlags(cmd *cobra.Command) (requiredProvidersFlags, er // ExecuteTerraformGenerateRequiredProvidersCmd executes `terraform generate required-providers` command. // This generates a terraform_override.tf.json file with required_version and required_providers // blocks from stack configuration (DEV-3124). -func ExecuteTerraformGenerateRequiredProvidersCmd(cmd *cobra.Command, args []string) error { +func (s *Operations) ExecuteTerraformGenerateRequiredProvidersCmd(cmd *cobra.Command, args []string) error { defer perf.Track(nil, "exec.ExecuteTerraformGenerateRequiredProvidersCmd")() if len(args) != 1 { @@ -65,7 +65,7 @@ func ExecuteTerraformGenerateRequiredProvidersCmd(cmd *cobra.Command, args []str } component := args[0] - info, err := ProcessCommandLineArgs("terraform", cmd, args, nil) + info, err := s.host.ProcessCommandLineArgs("terraform", cmd, args, nil) if err != nil { return err } @@ -80,7 +80,7 @@ func ExecuteTerraformGenerateRequiredProvidersCmd(cmd *cobra.Command, args []str return err } - info, err = ProcessStacks(&atmosConfig, info, true, f.processTemplates, f.processYamlFunctions, f.skip, nil) + info, err = s.host.ProcessStacks(&atmosConfig, info, true, f.processTemplates, f.processYamlFunctions, f.skip, nil) if err != nil { return err } @@ -99,7 +99,7 @@ func generateRequiredProvidersFile(atmosConfig *schema.AtmosConfiguration, info return nil } - workingDir := constructTerraformComponentWorkingDir(atmosConfig, info) + workingDir := ConstructTerraformComponentWorkingDir(atmosConfig, info) genCtx := generator.NewGeneratorContext(atmosConfig, info, workingDir) configureCustomFilePath(genCtx, fileFromArg) diff --git a/internal/exec/terraform_generate_required_providers_test.go b/pkg/component/terraform/generate_required_providers_test.go similarity index 98% rename from internal/exec/terraform_generate_required_providers_test.go rename to pkg/component/terraform/generate_required_providers_test.go index af6d87ea319..096134b6b4e 100644 --- a/internal/exec/terraform_generate_required_providers_test.go +++ b/pkg/component/terraform/generate_required_providers_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "errors" @@ -252,7 +252,7 @@ func TestExecuteTerraformGenerateRequiredProvidersCmd_ArgValidation(t *testing.T for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() - err := ExecuteTerraformGenerateRequiredProvidersCmd(cmd, tt.args) + err := NewOperations(nil).ExecuteTerraformGenerateRequiredProvidersCmd(cmd, tt.args) require.Error(t, err) assert.True(t, errors.Is(err, errUtils.ErrInvalidComponentArgument), "%d args must surface ErrInvalidComponentArgument, got: %v", len(tt.args), err) diff --git a/internal/exec/terraform_generate_varfile.go b/pkg/component/terraform/generate_varfile.go similarity index 84% rename from internal/exec/terraform_generate_varfile.go rename to pkg/component/terraform/generate_varfile.go index c63a7df0b2e..8377f321730 100644 --- a/internal/exec/terraform_generate_varfile.go +++ b/pkg/component/terraform/generate_varfile.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" @@ -21,10 +21,12 @@ import ( u "github.com/cloudposse/atmos/pkg/utils" ) -// ensureTerraformComponentExists checks if a terraform component exists and +// EnsureTerraformComponentExists checks if a terraform component exists and // provisions it via JIT when source.uri is configured. Errors are wrapped // with ErrInvalidTerraformComponent. -func ensureTerraformComponentExists(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { +func EnsureTerraformComponentExists(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { + defer perf.Track(atmosConfig, "terraform.EnsureTerraformComponentExists")() + componentPath, err := u.GetComponentPath(atmosConfig, cfg.TerraformComponentType, info.ComponentFolderPrefix, info.FinalComponent) if err != nil { return errors.Join(errUtils.ErrInvalidTerraformComponent, fmt.Errorf("failed to resolve component path: %w", err)) @@ -58,7 +60,9 @@ func ensureTerraformComponentExists(atmosConfig *schema.AtmosConfiguration, info } // ExecuteGenerateVarfile generates a varfile for a terraform component. -func ExecuteGenerateVarfile(opts *VarfileOptions, atmosConfig *schema.AtmosConfiguration) error { +// +//nolint:revive,funlen // Preserve the existing generation and evaluation order during package extraction. +func (s *Operations) ExecuteGenerateVarfile(opts *VarfileOptions, atmosConfig *schema.AtmosConfiguration) error { defer perf.Track(atmosConfig, "exec.ExecuteGenerateVarfile")() log.Debug( @@ -80,13 +84,13 @@ func ExecuteGenerateVarfile(opts *VarfileOptions, atmosConfig *schema.AtmosConfi } // Process stacks to get component configuration. - info, err := ProcessStacks(atmosConfig, info, true, opts.ProcessTemplates, opts.ProcessFunctions, opts.Skip, nil) + info, err := s.host.ProcessStacks(atmosConfig, info, true, opts.ProcessTemplates, opts.ProcessFunctions, opts.Skip, nil) if err != nil { return err } // Ensure component exists, provisioning via JIT if needed. - if err := ensureTerraformComponentExists(atmosConfig, &info); err != nil { + if err := EnsureTerraformComponentExists(atmosConfig, &info); err != nil { return err } @@ -95,7 +99,7 @@ func ExecuteGenerateVarfile(opts *VarfileOptions, atmosConfig *schema.AtmosConfi if len(opts.File) > 0 { varFilePath = opts.File } else { - varFilePath = constructTerraformComponentVarfilePath(atmosConfig, &info) + varFilePath = ConstructTerraformComponentVarfilePath(atmosConfig, &info) } // Print the component variables @@ -105,20 +109,19 @@ func ExecuteGenerateVarfile(opts *VarfileOptions, atmosConfig *schema.AtmosConfi "stack", info.Stack, "variables", info.ComponentVarsSection, ) - // Display the varfile path relative to the current working directory. displayPath := relativeToCwd(varFilePath) // Varfile generation is an execution handoff, not an inspection surface: a // degraded `(computed)` value must never be serialized for Terraform. - if err := rejectComputedTerraformVars(info.ComponentVarsSection); err != nil { + if err := RejectComputedTerraformVars(info.ComponentVarsSection); err != nil { return err } // Write the variables to a file. log.Debug("Writing the variables to file", "file", displayPath) - varsToWrite := varfileVarsToWrite(&info, opts.WithSecrets, displayPath) + varsToWrite := VarfileVarsToWrite(&info, opts.WithSecrets, displayPath) if !info.DryRun { err = u.WriteToFileAsJSON(varFilePath, varsToWrite, filePermissions) @@ -150,12 +153,14 @@ func relativeToCwd(p string) string { return rel } -// varfileVarsToWrite returns the variables to write to a generated varfile. By default, +// VarfileVarsToWrite returns the variables to write to a generated varfile. By default, // secret-bearing variables are omitted so plaintext secrets never hit disk; with // withSecrets=true they are included (e.g. to export the file). An appropriate warning is // emitted in each case. Requires that secrets have already been resolved into info. -func varfileVarsToWrite(info *schema.ConfigAndStacksInfo, withSecrets bool, varFilePath string) map[string]any { - computeTerraformSecretVarKeys(info) +func VarfileVarsToWrite(info *schema.ConfigAndStacksInfo, withSecrets bool, varFilePath string) map[string]any { + defer perf.Track(nil, "terraform.VarfileVarsToWrite")() + + ComputeTerraformSecretVarKeys(info) if withSecrets { if len(info.TerraformSecretVarKeys) > 0 { @@ -175,7 +180,7 @@ func varfileVarsToWrite(info *schema.ConfigAndStacksInfo, withSecrets bool, varF log.Warn("Omitting secrets from the generated varfile; pass --with-secrets to include them", "file", varFilePath, "count", len(info.TerraformSecretVarKeys)) } - return diskSafeVars(info) + return DiskSafeVars(info) } // ExecuteTerraformGenerateVarfileCmd executes `terraform generate varfile` command. diff --git a/pkg/component/terraform/generate_varfile_test.go b/pkg/component/terraform/generate_varfile_test.go new file mode 100644 index 00000000000..3031e725182 --- /dev/null +++ b/pkg/component/terraform/generate_varfile_test.go @@ -0,0 +1,426 @@ +package terraform + +import ( + bytes "bytes" + context "context" + fmt "fmt" + os "os" + filepath "path/filepath" + strings "strings" + testing "testing" + + errUtils "github.com/cloudposse/atmos/errors" + cfg "github.com/cloudposse/atmos/pkg/config" + degradation "github.com/cloudposse/atmos/pkg/degradation" + iolib "github.com/cloudposse/atmos/pkg/io" + process "github.com/cloudposse/atmos/pkg/process" + provisioner "github.com/cloudposse/atmos/pkg/provisioner" + provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir" + schema "github.com/cloudposse/atmos/pkg/schema" + store "github.com/cloudposse/atmos/pkg/store" + tfoutput "github.com/cloudposse/atmos/pkg/terraform/output" + assert "github.com/stretchr/testify/assert" + require "github.com/stretchr/testify/require" + gomock "go.uber.org/mock/gomock" +) + +func TestEnsureTerraformComponentExists_ExistingComponent(t *testing.T) { + // Create a temporary directory structure. + tempDir := t.TempDir() + componentPath := filepath.Join(tempDir, "components", "terraform", "vpc") + require.NoError(t, os.MkdirAll(componentPath, 0o755)) + + // Create a minimal main.tf to make it a valid component. + mainTF := filepath.Join(componentPath, "main.tf") + require.NoError(t, os.WriteFile(mainTF, []byte("# vpc component\n"), 0o644)) + + atmosConfig := &schema.AtmosConfiguration{ + BasePath: tempDir, + Components: schema.Components{ + Terraform: schema.Terraform{ + BasePath: filepath.Join("components", "terraform"), + }, + }, + } + + info := &schema.ConfigAndStacksInfo{ + ComponentFromArg: "vpc", + FinalComponent: "vpc", + ComponentFolderPrefix: "", + ComponentSection: map[string]any{}, + } + + err := EnsureTerraformComponentExists(atmosConfig, info) + assert.NoError(t, err, "existing component should not return error") +} + +func TestEnsureTerraformComponentExists_MissingComponentNoSource(t *testing.T) { + tempDir := t.TempDir() + + atmosConfig := &schema.AtmosConfiguration{ + BasePath: tempDir, + Components: schema.Components{ + Terraform: schema.Terraform{ + BasePath: filepath.Join("components", "terraform"), + }, + }, + } + + info := &schema.ConfigAndStacksInfo{ + ComponentFromArg: "nonexistent", + FinalComponent: "nonexistent", + ComponentFolderPrefix: "", + ComponentSection: map[string]any{}, + } + + err := EnsureTerraformComponentExists(atmosConfig, info) + assert.Error(t, err, "missing component without source should return error") + assert.Contains(t, err.Error(), "nonexistent") +} + +func TestEnsureTerraformComponentExists_WorkdirPathSet(t *testing.T) { + tempDir := t.TempDir() + + // Create workdir path. + workdirPath := filepath.Join(tempDir, "workdir", "vpc") + require.NoError(t, os.MkdirAll(workdirPath, 0o755)) + + atmosConfig := &schema.AtmosConfiguration{ + BasePath: tempDir, + Components: schema.Components{ + Terraform: schema.Terraform{ + BasePath: filepath.Join("components", "terraform"), + }, + }, + } + + info := &schema.ConfigAndStacksInfo{ + ComponentFromArg: "vpc", + FinalComponent: "vpc", + ComponentFolderPrefix: "", + ComponentSection: map[string]any{ + provWorkdir.WorkdirPathKey: workdirPath, + }, + } + + // Even though the original component path doesn't exist, the workdir path is set. + err := EnsureTerraformComponentExists(atmosConfig, info) + assert.NoError(t, err, "component with workdir path set should pass") +} + +func TestEnsureTerraformComponentExists_WithFolderPrefix(t *testing.T) { + tempDir := t.TempDir() + componentPath := filepath.Join(tempDir, "components", "terraform", "myprefix", "vpc") + require.NoError(t, os.MkdirAll(componentPath, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(componentPath, "main.tf"), []byte("# vpc\n"), 0o644)) + + atmosConfig := &schema.AtmosConfiguration{ + BasePath: tempDir, + Components: schema.Components{ + Terraform: schema.Terraform{ + BasePath: filepath.Join("components", "terraform"), + }, + }, + } + + info := &schema.ConfigAndStacksInfo{ + ComponentFromArg: "vpc", + FinalComponent: "vpc", + ComponentFolderPrefix: "myprefix", + ComponentSection: map[string]any{}, + } + + err := EnsureTerraformComponentExists(atmosConfig, info) + assert.NoError(t, err, "component with folder prefix should be found") +} + +func TestEnsureTerraformComponentExists_ReturnsErrorWithBasePath(t *testing.T) { + tempDir := t.TempDir() + + atmosConfig := &schema.AtmosConfiguration{ + BasePath: tempDir, + Components: schema.Components{ + Terraform: schema.Terraform{ + BasePath: filepath.Join("components", "terraform"), + }, + }, + } + + info := &schema.ConfigAndStacksInfo{ + ComponentFromArg: "missing-comp", + FinalComponent: "missing-comp", + ComponentFolderPrefix: "", + ComponentSection: map[string]any{}, + } + + err := EnsureTerraformComponentExists(atmosConfig, info) + assert.Error(t, err) + assert.Contains(t, err.Error(), "missing-comp") + assert.Contains(t, err.Error(), filepath.Join("components", "terraform")) +} + +func TestExecuteTerraformGenerateVarfileCmd_Deprecated(t *testing.T) { + err := ExecuteTerraformGenerateVarfileCmd(nil, nil) + assert.Error(t, err) + assert.ErrorIs(t, err, errUtils.ErrDeprecatedCmdNotCallable) +} + +func TestEnsureTerraformComponentExists_JITProvisionFails(t *testing.T) { + tempDir := t.TempDir() + + atmosConfig := &schema.AtmosConfiguration{ + BasePath: tempDir, + Components: schema.Components{ + Terraform: schema.Terraform{ + BasePath: filepath.Join("components", "terraform"), + }, + }, + } + + // Component doesn't exist and has a source with an unreachable URI — JIT provision will fail. + info := &schema.ConfigAndStacksInfo{ + ComponentFromArg: "jit-fail", + FinalComponent: "jit-fail", + ComponentFolderPrefix: "", + ComponentSection: map[string]any{ + "source": map[string]any{ + "uri": "file:///nonexistent/path/to/source", + }, + }, + } + + err := EnsureTerraformComponentExists(atmosConfig, info) + assert.Error(t, err) + assert.ErrorIs(t, err, errUtils.ErrInvalidTerraformComponent) + assert.Contains(t, err.Error(), "auto-provision") +} + +func TestEnsureTerraformComponentExists_PostJITComponentAppears(t *testing.T) { + tempDir := t.TempDir() + + // Do NOT create the component directory initially. + componentPath := filepath.Join(tempDir, "components", "terraform", "lazy-vpc") + + atmosConfig := &schema.AtmosConfiguration{ + BasePath: tempDir, + Components: schema.Components{ + Terraform: schema.Terraform{ + BasePath: filepath.Join("components", "terraform"), + }, + }, + } + + info := &schema.ConfigAndStacksInfo{ + ComponentFromArg: "lazy-vpc", + FinalComponent: "lazy-vpc", + ComponentFolderPrefix: "", + ComponentSection: map[string]any{}, + } + + // First call: component doesn't exist, no source, no workdir → error. + err := EnsureTerraformComponentExists(atmosConfig, info) + assert.Error(t, err, "should fail when component doesn't exist") + + // Now create the directory to simulate JIT provisioning having put it there. + require.NoError(t, os.MkdirAll(componentPath, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(componentPath, "main.tf"), []byte("# lazy-vpc\n"), 0o644)) + + // Second call: component now exists at the standard path. + err = EnsureTerraformComponentExists(atmosConfig, info) + assert.NoError(t, err, "should pass when component exists") +} + +func TestVarfileVarsToWrite(t *testing.T) { + const secret = "genvarfile-secret-aa11bb22" + iolib.RegisterSecret(secret) + + newInfo := func() *schema.ConfigAndStacksInfo { + return &schema.ConfigAndStacksInfo{ + ComponentVarsSection: map[string]any{ + "db_password": secret, + "region": "us-east-1-genvarfile", + }, + } + } + + t.Run("default omits secrets", func(t *testing.T) { + info := newInfo() + got := VarfileVarsToWrite(info, false, "test.tfvars.json") + _, hasSecret := got["db_password"] + assert.False(t, hasSecret, "secret var must be omitted from the varfile by default") + assert.Equal(t, "us-east-1-genvarfile", got["region"], "non-secret var must remain") + }) + + t.Run("with-secrets includes secrets", func(t *testing.T) { + info := newInfo() + got := VarfileVarsToWrite(info, true, "test.tfvars.json") + assert.Equal(t, secret, got["db_password"], "secret var must be written with --with-secrets") + assert.Equal(t, "us-east-1-genvarfile", got["region"]) + }) +} + +func TestTerraformSecretVars_NoSecrets(t *testing.T) { + info := &schema.ConfigAndStacksInfo{ + ComponentVarsSection: map[string]any{"region": "us-west-2-nosecret-xyz", "count": 1}, + } + ComputeTerraformSecretVarKeys(info) + assert.Nil(t, info.TerraformSecretVarKeys, "no secret keys expected when no secrets present") + + env, err := SecretVarEnv(info) + require.NoError(t, err) + assert.Empty(t, env, "no TF_VAR_ entries expected when no secrets present") +} + +func TestRejectComputedTerraformVars(t *testing.T) { + assert.NoError(t, RejectComputedTerraformVars(map[string]any{"region": "us-east-1"})) + err := RejectComputedTerraformVars(map[string]any{"value": degradation.AtmosComputedValue{}}) + assert.ErrorIs(t, err, errUtils.ErrUnresolvedComputedTerraformVar) + err = RejectComputedTerraformVars(map[string]any{ + "nested": []any{map[string]any{"value": degradation.AtmosComputedValue{}}}, + }) + assert.ErrorIs(t, err, errUtils.ErrUnresolvedComputedTerraformVar) +} + +const nativeReferenceSecret = "synthetic-reference-secret-63f7a92" + +// setupNativeSecretReference loads real stack files with a mock secret store. +// Mocking the component describer would miss the inspection-mode regression. +func setupNativeSecretReference(t *testing.T) (*schema.AtmosConfiguration, *store.MockStore) { + t.Helper() + dir := t.TempDir() + files := map[string]string{ + "atmos.yaml": `base_path: "." +components: + terraform: + base_path: components/terraform +stacks: + base_path: stacks + included_paths: ["**/*"] +templates: + settings: + enabled: true + evaluations: 1 +settings: + terminal: + mask: + enabled: true +`, + "components/terraform/producer/main.tf": "", + "components/terraform/consumer/main.tf": "variable \"credential\" { type = string }\n", + "stacks/dev.yaml": `components: + terraform: + producer: + secrets: + vars: + CREDENTIAL: + store: test-secrets + required: true + vars: + credential: !secret CREDENTIAL + env: + AWS_SECRET_ACCESS_KEY: !secret CREDENTIAL + backend_type: s3 + backend: + s3: + bucket: test-state + region: us-east-1 + access_key: !secret CREDENTIAL + secret_key: !secret CREDENTIAL + remote_state_backend_type: static + remote_state_backend: + static: + credential: !secret CREDENTIAL + consumer: + vars: + credential: !terraform.output producer credential +`, + } + for name, content := range files { + path := filepath.Join(dir, name) + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700)) + require.NoError(t, os.WriteFile(path, []byte(content), 0o600)) + } + t.Chdir(dir) + t.Setenv("ATMOS_CLI_CONFIG_PATH", dir) + t.Setenv("ATMOS_BASE_PATH", dir) + t.Setenv("ATMOS_MASK", "true") + atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true) + require.NoError(t, err) + mockStore := store.NewMockStore(gomock.NewController(t)) + atmosConfig.StoresConfig = store.StoresConfig{"test-secrets": {Type: "test", Secret: true}} + atmosConfig.Stores = store.StoreRegistry{"test-secrets": mockStore} + iolib.Reset() + require.NoError(t, iolib.Initialize()) + iolib.ApplyMaskingConfig(&iolib.Config{AtmosConfig: atmosConfig}) + reset := func() { + ResetStateCache() + tfoutput.ResetOutputsCache() + } + reset() + t.Cleanup(func() { reset(); iolib.Reset() }) + return &atmosConfig, mockStore +} + +// runSecretPlanForTest is a controlled Terraform subprocess: it rejects masked +// inputs and echoes the received synthetic secret to exercise terminal redaction. +func runSecretPlanForTest() int { + if len(os.Args) < 2 || os.Args[1] != "plan" { + fmt.Fprintln(os.Stderr, "expected a plan invocation") + return 1 + } + if os.Getenv("TF_VAR_credential") != nativeReferenceSecret { + fmt.Fprintln(os.Stderr, "plan received an incorrect credential") + return 1 + } + foundVarfile := false + for i, arg := range os.Args[1:] { + if arg != "-var-file" || i+2 >= len(os.Args) { + continue + } + content, err := os.ReadFile(os.Args[i+2]) + if err != nil || strings.Contains(string(content), nativeReferenceSecret) || strings.Contains(string(content), "credential") { + fmt.Fprintln(os.Stderr, "secret must be excluded from the generated varfile") + return 1 + } + foundVarfile = true + } + if !foundVarfile { + return 1 + } + fmt.Fprintln(os.Stdout, "plan credential:", os.Getenv("TF_VAR_credential")) + fmt.Fprintln(os.Stderr, "diagnostic credential:", os.Getenv("TF_VAR_credential")) + return 0 +} + +// TestSecretReferenceReachesTerraformPlan checks subprocess inputs and masked output together. +func TestSecretReferenceReachesTerraformPlan(t *testing.T) { + config, mockStore := setupNativeSecretReference(t) + mockStore.EXPECT().Get("dev", "producer", "CREDENTIAL").Return(nativeReferenceSecret, nil).MinTimes(1) + exe, err := os.Executable() + require.NoError(t, err) + // Load the consumer through the same execution stack processor used by plan. + info, err := testExecutor(t).services.ProcessStacks(config, schema.ConfigAndStacksInfo{ + ComponentFromArg: "consumer", ComponentType: "terraform", Stack: "dev", SubCommand: "plan", + }, true, true, true, nil, nil) + require.NoError(t, err) + info.Command = exe + info.SkipInit = true + info.ComponentBackendType = "http" // Skip workspace selection; only plan is under test. + info.ComponentEnvSection["_ATMOS_TEST_SECRET_PLAN"] = "1" + execCtx, err := testExecutor(t).prepareComponentExecution(context.Background(), provisioner.OutputWriters{}, config, &info, false) + require.NoError(t, err) + t.Cleanup(func() { + if info.RCCleanup != nil { + assert.NoError(t, info.RCCleanup()) + } + }) + var stdout, stderr bytes.Buffer + err = testExecutor(t).executeCommandPipeline( + config, &info, execCtx, + WithProcessStreams(process.Streams{Stdin: strings.NewReader(""), Stdout: &stdout, Stderr: &stderr}), + ) + require.NoError(t, err, "controlled Terraform stderr: %s", stderr.String()) + assert.Contains(t, stdout.String(), "plan credential: "+iolib.MaskReplacement) + assert.Contains(t, stderr.String(), "diagnostic credential: "+iolib.MaskReplacement) + assert.NotContains(t, stdout.String()+stderr.String(), nativeReferenceSecret) +} diff --git a/internal/exec/terraform_generate_varfiles.go b/pkg/component/terraform/generate_varfiles.go similarity index 88% rename from internal/exec/terraform_generate_varfiles.go rename to pkg/component/terraform/generate_varfiles.go index 7f4fd837f98..1e8eb6a8628 100644 --- a/internal/exec/terraform_generate_varfiles.go +++ b/pkg/component/terraform/generate_varfiles.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "errors" @@ -22,7 +22,9 @@ import ( ) // ExecuteTerraformGenerateVarfiles generates varfiles for all terraform components in all stacks. -func ExecuteTerraformGenerateVarfiles( +// +//nolint:cyclop,funlen,gocognit,revive // Preserve the existing generation and evaluation order during package extraction. +func (s *Operations) ExecuteTerraformGenerateVarfiles( atmosConfig *schema.AtmosConfiguration, fileTemplate string, format string, @@ -31,7 +33,7 @@ func ExecuteTerraformGenerateVarfiles( ) error { defer perf.Track(atmosConfig, "exec.ExecuteTerraformGenerateVarfiles")() - stacksMap, rawStackConfigs, deferredContexts, err := FindStacksMap(atmosConfig, false) + stacksMap, rawStackConfigs, deferredContexts, err := s.host.FindStacksMap(atmosConfig, false) if err != nil { return err } @@ -142,7 +144,7 @@ func ExecuteTerraformGenerateVarfiles( // Snapshot the complete merged component section (hooks, generate, // required_providers, etc. included), not just the sections this generator // directly consumes — see cloneComponentSectionWithOverrides. - ComponentSection: cloneComponentSectionWithOverrides(componentSection, map[string]any{ + ComponentSection: s.host.CloneComponentSectionWithOverrides(componentSection, map[string]any{ cfg.VarsSectionName: varsSection, cfg.MetadataSectionName: metadataSection, cfg.SettingsSectionName: settingsSection, @@ -176,12 +178,12 @@ func ExecuteTerraformGenerateVarfiles( // Stack name var stackName string if atmosConfig.Stacks.NameTemplate != "" { - stackName, err = ProcessTmpl(atmosConfig, "terraform-generate-varfiles-template", atmosConfig.Stacks.NameTemplate, configAndStacksInfo.ComponentSection, atmosConfig.Templates.Settings.IgnoreMissingTemplateValues) + stackName, err = s.host.ProcessTmpl(atmosConfig, "terraform-generate-varfiles-template", atmosConfig.Stacks.NameTemplate, configAndStacksInfo.ComponentSection, atmosConfig.Templates.Settings.IgnoreMissingTemplateValues) if err != nil { return err } } else { - stackName, err = cfg.GetContextPrefix(stackFileName, context, GetStackNamePattern(atmosConfig), stackFileName) + stackName, err = cfg.GetContextPrefix(stackFileName, context, atmosConfig.Stacks.NamePattern, stackFileName) if err != nil { return err } @@ -189,7 +191,7 @@ func ExecuteTerraformGenerateVarfiles( // A computed name is not final yet. Keep derived metadata from being // interpreted as another Starlark program during value evaluation. - if containsStarlark(stackName) { + if s.host.ContainsStarlark(stackName) { stackName = stackFileName } @@ -204,11 +206,11 @@ func ExecuteTerraformGenerateVarfiles( // Terraform workspace. Batch generation starts from a physical manifest before // evaluating computed vars. Keep its provisional workspace physical as well; // BuildTerraformWorkspace runs again after finishValues with the resolved context. - workspace, err := BuildTerraformWorkspace(atmosConfig, configAndStacksInfo) + workspace, err := s.host.BuildTerraformWorkspace(atmosConfig, configAndStacksInfo) if err != nil && !errors.Is(err, errUtils.ErrStarlarkStackIdentity) { return err } - if errors.Is(err, errUtils.ErrStarlarkStackIdentity) || containsStarlark(workspace) { + if errors.Is(err, errUtils.ErrStarlarkStackIdentity) || s.host.ContainsStarlark(workspace) { workspace = strings.ReplaceAll(stackFileName, "/", "-") } componentSection["workspace"] = workspace @@ -242,7 +244,7 @@ func ExecuteTerraformGenerateVarfiles( componentTemplateContext[k] = v } - componentSectionProcessed, err := ProcessTmplWithDatasources( + componentSectionProcessed, err := s.host.ProcessTmplWithDatasources( atmosConfig, &configAndStacksInfo, settingsSectionStruct, @@ -268,9 +270,9 @@ func ExecuteTerraformGenerateVarfiles( } configAndStacksInfo.ComponentSection = componentSectionConverted - configAndStacksInfo.StackLocalsSection = stackLocalsForComponent(rawStackConfigs, stackFileName, cfg.TerraformComponentType) - yamlSkip, finishValues := prepareConfigurationValues(atmosConfig, &configAndStacksInfo, nil, nil) - componentSectionFinal, err := ProcessCustomYamlTags(atmosConfig, componentSectionConverted, stackName, yamlSkip, &configAndStacksInfo) + configAndStacksInfo.StackLocalsSection = s.host.StackLocalsForComponent(rawStackConfigs, stackFileName, cfg.TerraformComponentType) + yamlSkip, finishValues := s.host.PrepareConfigurationValues(atmosConfig, &configAndStacksInfo, nil, nil) + componentSectionFinal, err := s.host.ProcessCustomYamlTags(atmosConfig, componentSectionConverted, stackName, yamlSkip, &configAndStacksInfo) if err != nil { return err } @@ -284,19 +286,19 @@ func ExecuteTerraformGenerateVarfiles( // unresolved function string (or a placeholder) would silently lose the function's // contribution here, the same #2888 data-loss bug the main describe/plan path fixes // via this same call (see internal/exec/utils.go). - if err := resolveDeferredYamlFunctions(atmosConfig, &configAndStacksInfo, &settingsSectionStruct, componentTemplateContext, nil, nil); err != nil { + if err := s.host.ResolveDeferredYamlFunctions(atmosConfig, &configAndStacksInfo, &settingsSectionStruct, componentTemplateContext, nil, nil); err != nil { return err } if err := finishValues(); err != nil { return err } configAndStacksInfo.ComponentSection = literalsource.RestoreMap(configAndStacksInfo.ComponentSection) - if err := refreshTerraformGeneratorContext(atmosConfig, &configAndStacksInfo, stackFileName); err != nil { + if err := s.refreshTerraformGeneratorContext(atmosConfig, &configAndStacksInfo, stackFileName); err != nil { return err } context = configAndStacksInfo.Context stackName = configAndStacksInfo.Stack - workspace, err = BuildTerraformWorkspace(atmosConfig, configAndStacksInfo) + workspace, err = s.host.BuildTerraformWorkspace(atmosConfig, configAndStacksInfo) if err != nil { return err } diff --git a/internal/exec/terraform_lookup_options.go b/pkg/component/terraform/lookup_options.go similarity index 93% rename from internal/exec/terraform_lookup_options.go rename to pkg/component/terraform/lookup_options.go index 374f2a598e8..c04fa7bd901 100644 --- a/internal/exec/terraform_lookup_options.go +++ b/pkg/component/terraform/lookup_options.go @@ -1,4 +1,4 @@ -package exec +package terraform import "github.com/cloudposse/atmos/pkg/terraform" diff --git a/internal/exec/terraform_managed_cache_test.go b/pkg/component/terraform/managed_cache_test.go similarity index 99% rename from internal/exec/terraform_managed_cache_test.go rename to pkg/component/terraform/managed_cache_test.go index 43ca3bf06cf..97e50964b9e 100644 --- a/internal/exec/terraform_managed_cache_test.go +++ b/pkg/component/terraform/managed_cache_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "testing" diff --git a/internal/exec/terraform_mirror_targets_test.go b/pkg/component/terraform/mirror_targets_test.go similarity index 94% rename from internal/exec/terraform_mirror_targets_test.go rename to pkg/component/terraform/mirror_targets_test.go index d4d978efdd1..a1572ae869a 100644 --- a/internal/exec/terraform_mirror_targets_test.go +++ b/pkg/component/terraform/mirror_targets_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "testing" @@ -71,7 +71,7 @@ func TestMirrorTargetIncluded(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() - got, err := mirrorTargetIncluded(atmosConfig, "vpc", tt.section, tt.query) + got, err := testExecutor(t).mirrorTargetIncluded(atmosConfig, "vpc", tt.section, tt.query) if tt.expectErr { require.Error(t, err) return diff --git a/pkg/component/terraform/mock_component_provider_test.go b/pkg/component/terraform/mock_component_provider_test.go new file mode 100644 index 00000000000..0e7cc72fc32 --- /dev/null +++ b/pkg/component/terraform/mock_component_provider_test.go @@ -0,0 +1,156 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: ../provider.go +// +// Generated by this command: +// +// mockgen -source=../provider.go -destination=mock_component_provider_test.go -package=terraform +// + +// Package terraform is a generated GoMock package. +package terraform + +import ( + context "context" + reflect "reflect" + + component "github.com/cloudposse/atmos/pkg/component" + schema "github.com/cloudposse/atmos/pkg/schema" + gomock "go.uber.org/mock/gomock" +) + +// MockComponentProvider is a mock of ComponentProvider interface. +type MockComponentProvider struct { + ctrl *gomock.Controller + recorder *MockComponentProviderMockRecorder + isgomock struct{} +} + +// MockComponentProviderMockRecorder is the mock recorder for MockComponentProvider. +type MockComponentProviderMockRecorder struct { + mock *MockComponentProvider +} + +// NewMockComponentProvider creates a new mock instance. +func NewMockComponentProvider(ctrl *gomock.Controller) *MockComponentProvider { + mock := &MockComponentProvider{ctrl: ctrl} + mock.recorder = &MockComponentProviderMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockComponentProvider) EXPECT() *MockComponentProviderMockRecorder { + return m.recorder +} + +// Execute mocks base method. +func (m *MockComponentProvider) Execute(ctx *component.ExecutionContext) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Execute", ctx) + ret0, _ := ret[0].(error) + return ret0 +} + +// Execute indicates an expected call of Execute. +func (mr *MockComponentProviderMockRecorder) Execute(ctx any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Execute", reflect.TypeOf((*MockComponentProvider)(nil).Execute), ctx) +} + +// GenerateArtifacts mocks base method. +func (m *MockComponentProvider) GenerateArtifacts(ctx *component.ExecutionContext) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GenerateArtifacts", ctx) + ret0, _ := ret[0].(error) + return ret0 +} + +// GenerateArtifacts indicates an expected call of GenerateArtifacts. +func (mr *MockComponentProviderMockRecorder) GenerateArtifacts(ctx any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GenerateArtifacts", reflect.TypeOf((*MockComponentProvider)(nil).GenerateArtifacts), ctx) +} + +// GetAvailableCommands mocks base method. +func (m *MockComponentProvider) GetAvailableCommands() []string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetAvailableCommands") + ret0, _ := ret[0].([]string) + return ret0 +} + +// GetAvailableCommands indicates an expected call of GetAvailableCommands. +func (mr *MockComponentProviderMockRecorder) GetAvailableCommands() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetAvailableCommands", reflect.TypeOf((*MockComponentProvider)(nil).GetAvailableCommands)) +} + +// GetBasePath mocks base method. +func (m *MockComponentProvider) GetBasePath(atmosConfig *schema.AtmosConfiguration) string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBasePath", atmosConfig) + ret0, _ := ret[0].(string) + return ret0 +} + +// GetBasePath indicates an expected call of GetBasePath. +func (mr *MockComponentProviderMockRecorder) GetBasePath(atmosConfig any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBasePath", reflect.TypeOf((*MockComponentProvider)(nil).GetBasePath), atmosConfig) +} + +// GetGroup mocks base method. +func (m *MockComponentProvider) GetGroup() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetGroup") + ret0, _ := ret[0].(string) + return ret0 +} + +// GetGroup indicates an expected call of GetGroup. +func (mr *MockComponentProviderMockRecorder) GetGroup() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetGroup", reflect.TypeOf((*MockComponentProvider)(nil).GetGroup)) +} + +// GetType mocks base method. +func (m *MockComponentProvider) GetType() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetType") + ret0, _ := ret[0].(string) + return ret0 +} + +// GetType indicates an expected call of GetType. +func (mr *MockComponentProviderMockRecorder) GetType() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetType", reflect.TypeOf((*MockComponentProvider)(nil).GetType)) +} + +// ListComponents mocks base method. +func (m *MockComponentProvider) ListComponents(ctx context.Context, stack string, stackConfig map[string]any) ([]string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ListComponents", ctx, stack, stackConfig) + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ListComponents indicates an expected call of ListComponents. +func (mr *MockComponentProviderMockRecorder) ListComponents(ctx, stack, stackConfig any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ListComponents", reflect.TypeOf((*MockComponentProvider)(nil).ListComponents), ctx, stack, stackConfig) +} + +// ValidateComponent mocks base method. +func (m *MockComponentProvider) ValidateComponent(config map[string]any) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ValidateComponent", config) + ret0, _ := ret[0].(error) + return ret0 +} + +// ValidateComponent indicates an expected call of ValidateComponent. +func (mr *MockComponentProviderMockRecorder) ValidateComponent(config any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ValidateComponent", reflect.TypeOf((*MockComponentProvider)(nil).ValidateComponent), config) +} diff --git a/internal/exec/mock_terraform_query.go b/pkg/component/terraform/mock_query_test.go similarity index 91% rename from internal/exec/mock_terraform_query.go rename to pkg/component/terraform/mock_query_test.go index 716b7df306d..ac6cd4e677c 100644 --- a/internal/exec/mock_terraform_query.go +++ b/pkg/component/terraform/mock_query_test.go @@ -1,13 +1,13 @@ // Code generated by MockGen. DO NOT EDIT. -// Source: terraform_query.go +// Source: query.go // // Generated by this command: // -// mockgen -source=terraform_query.go -destination=mock_terraform_query.go -package=exec +// mockgen -source=query.go -destination=mock_query_test.go -package=terraform // -// Package exec is a generated GoMock package. -package exec +// Package terraform is a generated GoMock package. +package terraform import ( reflect "reflect" diff --git a/pkg/component/terraform/mock_runtime_test.go b/pkg/component/terraform/mock_runtime_test.go new file mode 100644 index 00000000000..da677a973c7 --- /dev/null +++ b/pkg/component/terraform/mock_runtime_test.go @@ -0,0 +1,351 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: provider.go +// +// Generated by this command: +// +// mockgen -source=provider.go -destination=mock_runtime_test.go -package=terraform +// + +// Package terraform is a generated GoMock package. +package terraform + +import ( + reflect "reflect" + + auth "github.com/cloudposse/atmos/pkg/auth" + component "github.com/cloudposse/atmos/pkg/component" + degradation "github.com/cloudposse/atmos/pkg/degradation" + schema "github.com/cloudposse/atmos/pkg/schema" + cobra "github.com/spf13/cobra" + gomock "go.uber.org/mock/gomock" +) + +// MockRuntime is a mock of Runtime interface. +type MockRuntime struct { + ctrl *gomock.Controller + recorder *MockRuntimeMockRecorder + isgomock struct{} +} + +// MockRuntimeMockRecorder is the mock recorder for MockRuntime. +type MockRuntimeMockRecorder struct { + mock *MockRuntime +} + +// NewMockRuntime creates a new mock instance. +func NewMockRuntime(ctrl *gomock.Controller) *MockRuntime { + mock := &MockRuntime{ctrl: ctrl} + mock.recorder = &MockRuntimeMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockRuntime) EXPECT() *MockRuntimeMockRecorder { + return m.recorder +} + +// Execute mocks base method. +func (m *MockRuntime) Execute(arg0 *component.ExecutionContext) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Execute", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// Execute indicates an expected call of Execute. +func (mr *MockRuntimeMockRecorder) Execute(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Execute", reflect.TypeOf((*MockRuntime)(nil).Execute), arg0) +} + +// GenerateArtifacts mocks base method. +func (m *MockRuntime) GenerateArtifacts(arg0 *component.ExecutionContext) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GenerateArtifacts", arg0) + ret0, _ := ret[0].(error) + return ret0 +} + +// GenerateArtifacts indicates an expected call of GenerateArtifacts. +func (mr *MockRuntimeMockRecorder) GenerateArtifacts(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GenerateArtifacts", reflect.TypeOf((*MockRuntime)(nil).GenerateArtifacts), arg0) +} + +// MockDependencyParser is a mock of DependencyParser interface. +type MockDependencyParser struct { + ctrl *gomock.Controller + recorder *MockDependencyParserMockRecorder + isgomock struct{} +} + +// MockDependencyParserMockRecorder is the mock recorder for MockDependencyParser. +type MockDependencyParserMockRecorder struct { + mock *MockDependencyParser +} + +// NewMockDependencyParser creates a new mock instance. +func NewMockDependencyParser(ctrl *gomock.Controller) *MockDependencyParser { + mock := &MockDependencyParser{ctrl: ctrl} + mock.recorder = &MockDependencyParserMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockDependencyParser) EXPECT() *MockDependencyParserMockRecorder { + return m.recorder +} + +// ParseComponentDependencies mocks base method. +func (m *MockDependencyParser) ParseComponentDependencies(stackName, componentName string, componentSection map[string]any) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ParseComponentDependencies", stackName, componentName, componentSection) + ret0, _ := ret[0].(error) + return ret0 +} + +// ParseComponentDependencies indicates an expected call of ParseComponentDependencies. +func (mr *MockDependencyParserMockRecorder) ParseComponentDependencies(stackName, componentName, componentSection any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ParseComponentDependencies", reflect.TypeOf((*MockDependencyParser)(nil).ParseComponentDependencies), stackName, componentName, componentSection) +} + +// MockOperationServices is a mock of OperationServices interface. +type MockOperationServices struct { + ctrl *gomock.Controller + recorder *MockOperationServicesMockRecorder + isgomock struct{} +} + +// MockOperationServicesMockRecorder is the mock recorder for MockOperationServices. +type MockOperationServicesMockRecorder struct { + mock *MockOperationServices +} + +// NewMockOperationServices creates a new mock instance. +func NewMockOperationServices(ctrl *gomock.Controller) *MockOperationServices { + mock := &MockOperationServices{ctrl: ctrl} + mock.recorder = &MockOperationServicesMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockOperationServices) EXPECT() *MockOperationServicesMockRecorder { + return m.recorder +} + +// BuildTerraformWorkspace mocks base method. +func (m *MockOperationServices) BuildTerraformWorkspace(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo schema.ConfigAndStacksInfo) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "BuildTerraformWorkspace", atmosConfig, configAndStacksInfo) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// BuildTerraformWorkspace indicates an expected call of BuildTerraformWorkspace. +func (mr *MockOperationServicesMockRecorder) BuildTerraformWorkspace(atmosConfig, configAndStacksInfo any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "BuildTerraformWorkspace", reflect.TypeOf((*MockOperationServices)(nil).BuildTerraformWorkspace), atmosConfig, configAndStacksInfo) +} + +// CloneComponentSectionWithOverrides mocks base method. +func (m *MockOperationServices) CloneComponentSectionWithOverrides(raw, overrides map[string]any) map[string]any { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "CloneComponentSectionWithOverrides", raw, overrides) + ret0, _ := ret[0].(map[string]any) + return ret0 +} + +// CloneComponentSectionWithOverrides indicates an expected call of CloneComponentSectionWithOverrides. +func (mr *MockOperationServicesMockRecorder) CloneComponentSectionWithOverrides(raw, overrides any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "CloneComponentSectionWithOverrides", reflect.TypeOf((*MockOperationServices)(nil).CloneComponentSectionWithOverrides), raw, overrides) +} + +// ContainsStarlark mocks base method. +func (m *MockOperationServices) ContainsStarlark(value any) bool { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ContainsStarlark", value) + ret0, _ := ret[0].(bool) + return ret0 +} + +// ContainsStarlark indicates an expected call of ContainsStarlark. +func (mr *MockOperationServicesMockRecorder) ContainsStarlark(value any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ContainsStarlark", reflect.TypeOf((*MockOperationServices)(nil).ContainsStarlark), value) +} + +// ExecuteTerraform mocks base method. +func (m *MockOperationServices) ExecuteTerraform(info schema.ConfigAndStacksInfo, opts ...component.ExecutionOption) error { + m.ctrl.T.Helper() + varargs := []any{info} + for _, a := range opts { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "ExecuteTerraform", varargs...) + ret0, _ := ret[0].(error) + return ret0 +} + +// ExecuteTerraform indicates an expected call of ExecuteTerraform. +func (mr *MockOperationServicesMockRecorder) ExecuteTerraform(info any, opts ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{info}, opts...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ExecuteTerraform", reflect.TypeOf((*MockOperationServices)(nil).ExecuteTerraform), varargs...) +} + +// FindStacksMap mocks base method. +func (m *MockOperationServices) FindStacksMap(atmosConfig *schema.AtmosConfiguration, ignoreMissingFiles bool) (map[string]any, map[string]map[string]any, component.AllStacksDeferredContexts, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "FindStacksMap", atmosConfig, ignoreMissingFiles) + ret0, _ := ret[0].(map[string]any) + ret1, _ := ret[1].(map[string]map[string]any) + ret2, _ := ret[2].(component.AllStacksDeferredContexts) + ret3, _ := ret[3].(error) + return ret0, ret1, ret2, ret3 +} + +// FindStacksMap indicates an expected call of FindStacksMap. +func (mr *MockOperationServicesMockRecorder) FindStacksMap(atmosConfig, ignoreMissingFiles any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "FindStacksMap", reflect.TypeOf((*MockOperationServices)(nil).FindStacksMap), atmosConfig, ignoreMissingFiles) +} + +// GenerateFilesForComponent mocks base method. +func (m *MockOperationServices) GenerateFilesForComponent(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GenerateFilesForComponent", config, info, workingDir) + ret0, _ := ret[0].(error) + return ret0 +} + +// GenerateFilesForComponent indicates an expected call of GenerateFilesForComponent. +func (mr *MockOperationServicesMockRecorder) GenerateFilesForComponent(config, info, workingDir any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GenerateFilesForComponent", reflect.TypeOf((*MockOperationServices)(nil).GenerateFilesForComponent), config, info, workingDir) +} + +// PrepareConfigurationValues mocks base method. +func (m *MockOperationServices) PrepareConfigurationValues(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, skip, sections []string) ([]string, func() error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PrepareConfigurationValues", config, info, skip, sections) + ret0, _ := ret[0].([]string) + ret1, _ := ret[1].(func() error) + return ret0, ret1 +} + +// PrepareConfigurationValues indicates an expected call of PrepareConfigurationValues. +func (mr *MockOperationServicesMockRecorder) PrepareConfigurationValues(config, info, skip, sections any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PrepareConfigurationValues", reflect.TypeOf((*MockOperationServices)(nil).PrepareConfigurationValues), config, info, skip, sections) +} + +// ProcessCommandLineArgs mocks base method. +func (m *MockOperationServices) ProcessCommandLineArgs(componentType string, cmd *cobra.Command, args, additionalArgsAndFlags []string) (schema.ConfigAndStacksInfo, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ProcessCommandLineArgs", componentType, cmd, args, additionalArgsAndFlags) + ret0, _ := ret[0].(schema.ConfigAndStacksInfo) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ProcessCommandLineArgs indicates an expected call of ProcessCommandLineArgs. +func (mr *MockOperationServicesMockRecorder) ProcessCommandLineArgs(componentType, cmd, args, additionalArgsAndFlags any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ProcessCommandLineArgs", reflect.TypeOf((*MockOperationServices)(nil).ProcessCommandLineArgs), componentType, cmd, args, additionalArgsAndFlags) +} + +// ProcessCustomYamlTags mocks base method. +func (m *MockOperationServices) ProcessCustomYamlTags(atmosConfig *schema.AtmosConfiguration, input schema.AtmosSectionMapType, currentStack string, skip []string, stackInfo *schema.ConfigAndStacksInfo) (schema.AtmosSectionMapType, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ProcessCustomYamlTags", atmosConfig, input, currentStack, skip, stackInfo) + ret0, _ := ret[0].(schema.AtmosSectionMapType) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ProcessCustomYamlTags indicates an expected call of ProcessCustomYamlTags. +func (mr *MockOperationServicesMockRecorder) ProcessCustomYamlTags(atmosConfig, input, currentStack, skip, stackInfo any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ProcessCustomYamlTags", reflect.TypeOf((*MockOperationServices)(nil).ProcessCustomYamlTags), atmosConfig, input, currentStack, skip, stackInfo) +} + +// ProcessStacks mocks base method. +func (m *MockOperationServices) ProcessStacks(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo schema.ConfigAndStacksInfo, checkStack, processTemplates, processYamlFunctions bool, skip []string, authManager auth.AuthManager) (schema.ConfigAndStacksInfo, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ProcessStacks", atmosConfig, configAndStacksInfo, checkStack, processTemplates, processYamlFunctions, skip, authManager) + ret0, _ := ret[0].(schema.ConfigAndStacksInfo) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ProcessStacks indicates an expected call of ProcessStacks. +func (mr *MockOperationServicesMockRecorder) ProcessStacks(atmosConfig, configAndStacksInfo, checkStack, processTemplates, processYamlFunctions, skip, authManager any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ProcessStacks", reflect.TypeOf((*MockOperationServices)(nil).ProcessStacks), atmosConfig, configAndStacksInfo, checkStack, processTemplates, processYamlFunctions, skip, authManager) +} + +// ProcessTmpl mocks base method. +func (m *MockOperationServices) ProcessTmpl(atmosConfig *schema.AtmosConfiguration, tmplName, tmplValue string, tmplData any, ignoreMissingTemplateValues bool) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ProcessTmpl", atmosConfig, tmplName, tmplValue, tmplData, ignoreMissingTemplateValues) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ProcessTmpl indicates an expected call of ProcessTmpl. +func (mr *MockOperationServicesMockRecorder) ProcessTmpl(atmosConfig, tmplName, tmplValue, tmplData, ignoreMissingTemplateValues any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ProcessTmpl", reflect.TypeOf((*MockOperationServices)(nil).ProcessTmpl), atmosConfig, tmplName, tmplValue, tmplData, ignoreMissingTemplateValues) +} + +// ProcessTmplWithDatasources mocks base method. +func (m *MockOperationServices) ProcessTmplWithDatasources(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo *schema.ConfigAndStacksInfo, settingsSection schema.Settings, tmplName, tmplValue string, tmplData any, ignoreMissingTemplateValues bool) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ProcessTmplWithDatasources", atmosConfig, configAndStacksInfo, settingsSection, tmplName, tmplValue, tmplData, ignoreMissingTemplateValues) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ProcessTmplWithDatasources indicates an expected call of ProcessTmplWithDatasources. +func (mr *MockOperationServicesMockRecorder) ProcessTmplWithDatasources(atmosConfig, configAndStacksInfo, settingsSection, tmplName, tmplValue, tmplData, ignoreMissingTemplateValues any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ProcessTmplWithDatasources", reflect.TypeOf((*MockOperationServices)(nil).ProcessTmplWithDatasources), atmosConfig, configAndStacksInfo, settingsSection, tmplName, tmplValue, tmplData, ignoreMissingTemplateValues) +} + +// ResolveDeferredYamlFunctions mocks base method. +func (m *MockOperationServices) ResolveDeferredYamlFunctions(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo *schema.ConfigAndStacksInfo, settingsSectionStruct *schema.Settings, componentTemplateContext map[string]any, skip, evalSections []string, warnings ...func(degradation.Warning)) error { + m.ctrl.T.Helper() + varargs := []any{atmosConfig, configAndStacksInfo, settingsSectionStruct, componentTemplateContext, skip, evalSections} + for _, a := range warnings { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "ResolveDeferredYamlFunctions", varargs...) + ret0, _ := ret[0].(error) + return ret0 +} + +// ResolveDeferredYamlFunctions indicates an expected call of ResolveDeferredYamlFunctions. +func (mr *MockOperationServicesMockRecorder) ResolveDeferredYamlFunctions(atmosConfig, configAndStacksInfo, settingsSectionStruct, componentTemplateContext, skip, evalSections any, warnings ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{atmosConfig, configAndStacksInfo, settingsSectionStruct, componentTemplateContext, skip, evalSections}, warnings...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ResolveDeferredYamlFunctions", reflect.TypeOf((*MockOperationServices)(nil).ResolveDeferredYamlFunctions), varargs...) +} + +// StackLocalsForComponent mocks base method. +func (m *MockOperationServices) StackLocalsForComponent(rawStackConfigs map[string]map[string]any, stackFile, componentType string) map[string]any { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "StackLocalsForComponent", rawStackConfigs, stackFile, componentType) + ret0, _ := ret[0].(map[string]any) + return ret0 +} + +// StackLocalsForComponent indicates an expected call of StackLocalsForComponent. +func (mr *MockOperationServicesMockRecorder) StackLocalsForComponent(rawStackConfigs, stackFile, componentType any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "StackLocalsForComponent", reflect.TypeOf((*MockOperationServices)(nil).StackLocalsForComponent), rawStackConfigs, stackFile, componentType) +} diff --git a/internal/exec/terraform_mocks.go b/pkg/component/terraform/mocks.go similarity index 84% rename from internal/exec/terraform_mocks.go rename to pkg/component/terraform/mocks.go index 1914b7ee70d..7f7ec21af54 100644 --- a/internal/exec/terraform_mocks.go +++ b/pkg/component/terraform/mocks.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "fmt" @@ -6,8 +6,10 @@ import ( errUtils "github.com/cloudposse/atmos/errors" tb "github.com/cloudposse/atmos/internal/terraform_backend" + componentpkg "github.com/cloudposse/atmos/pkg/component" cfg "github.com/cloudposse/atmos/pkg/config" log "github.com/cloudposse/atmos/pkg/logger" + "github.com/cloudposse/atmos/pkg/perf" "github.com/cloudposse/atmos/pkg/schema" ) @@ -37,12 +39,12 @@ func terraformMocksEnabledInMode( // describeComponentMocks returns the literal `mocks` map of the referenced component, or nil when // the component declares none. It deliberately describes the target with templates and YAML // functions disabled (and auth disabled) so mocks cannot trigger the real dependency they replace. -func describeComponentMocks( +func (e *Executor) describeComponentMocks( atmosConfig *schema.AtmosConfiguration, stack string, component string, ) (map[string]any, error) { - componentSection, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{ + componentSection, err := e.services.ExecuteDescribeComponent(&componentpkg.ExecuteDescribeComponentParams{ AtmosConfig: atmosConfig, Component: component, Stack: stack, @@ -62,7 +64,7 @@ func describeComponentMocks( return mocks, nil } -// resolveTerraformMockOutput resolves a Terraform lookup from the referenced +// ResolveTerraformMockOutput resolves a Terraform lookup from the referenced // component's literal `mocks` map in `always` mode. It deliberately describes the // target with templates and YAML functions disabled so mocks cannot trigger the // real dependency they replace. @@ -72,13 +74,15 @@ func describeComponentMocks( // fall back to remote state while the user explicitly requested hermetic mocks. // In `fallback` mode this reports inactive: the Terraform lookups merge the mocks under the // real outputs instead (see resolveTerraformOutputWithMocks). -func resolveTerraformMockOutput( +func (e *Executor) ResolveTerraformMockOutput( atmosConfig *schema.AtmosConfiguration, stackInfo *schema.ConfigAndStacksInfo, stack string, component string, output string, ) (any, bool, error) { + defer perf.Track(nil, "terraform.Executor.ResolveTerraformMockOutput")() + if err := validateTerraformMocksMode(atmosConfig, stackInfo); err != nil { return nil, true, err } @@ -86,11 +90,11 @@ func resolveTerraformMockOutput( return nil, false, nil } - mocks, err := describeComponentMocks(atmosConfig, stack, component) + mocks, err := e.describeComponentMocks(atmosConfig, stack, component) if err != nil { return nil, true, err } - mocks, err = defaultUndeclaredMocks(mocks, stack, component, output) + mocks, err = e.defaultUndeclaredMocks(mocks, stack, component, output) if err != nil { return nil, true, err } @@ -99,7 +103,7 @@ func resolveTerraformMockOutput( if err != nil { return nil, true, fmt.Errorf("failed to resolve mocked Terraform output %q for component %q in stack %q: %w", output, component, stack, err) } - if value == nil && !hasYqDefault(output) && !mockOutputExists(mocks, output) { + if value == nil && !e.services.HasYqDefault(output) && !mockOutputExists(mocks, output) { return nil, true, fmt.Errorf("%w: %q for component %q in stack %q", errUtils.ErrTerraformMockOutputNotDeclared, output, component, stack) } @@ -130,37 +134,39 @@ func validateTerraformMocksMode(atmosConfig *schema.AtmosConfiguration, stackInf // component with no `mocks` map, a `//` default in the caller's expression is honored the same // way whether or not the component declares mocks, mirroring how a `//` default rescues a // component with no real state at all. Without a default, an undeclared `mocks` map is a hard error. -func defaultUndeclaredMocks(mocks map[string]any, stack, component, output string) (map[string]any, error) { +func (e *Executor) defaultUndeclaredMocks(mocks map[string]any, stack, component, output string) (map[string]any, error) { if mocks != nil { return mocks, nil } - if !hasYqDefault(output) { + if !e.services.HasYqDefault(output) { return nil, fmt.Errorf("%w: component %q in stack %q", errUtils.ErrTerraformComponentMocksNotDeclared, component, stack) } return map[string]any{}, nil } -// terraformAllOutputsExpression is the YQ identity expression used to fetch the whole real +// TerraformAllOutputsExpression is the YQ identity expression used to fetch the whole real // output map from a Terraform state/output getter, so the entire map comes back through the // same cached, authenticated, and masked lookup path as any other expression. -const terraformAllOutputsExpression = "." +const TerraformAllOutputsExpression = "." -// declaredFallbackMocks returns the referenced component's mocks when --use-mocks runs in +// DeclaredFallbackMocks returns the referenced component's mocks when --use-mocks runs in // `fallback` mode and the component declares a `mocks` map. It returns nil (and no error) when // mocks are off, the mode is not fallback, or the component declares none; callers then use the // plain real lookup unchanged. -func declaredFallbackMocks( +func (e *Executor) DeclaredFallbackMocks( atmosConfig *schema.AtmosConfiguration, stackInfo *schema.ConfigAndStacksInfo, - lookup *terraformStateLookup, + lookup *TerraformStateLookup, ) (map[string]any, error) { + defer perf.Track(nil, "terraform.Executor.DeclaredFallbackMocks")() + if !terraformMocksEnabledInMode(atmosConfig, stackInfo, schema.TerraformMocksModeFallback) { return nil, nil } - return describeComponentMocks(atmosConfig, lookup.stack, lookup.component) + return e.describeComponentMocks(atmosConfig, lookup.Stack, lookup.Component) } -// resolveTerraformOutputWithMocks evaluates the caller's expression against the component mocks +// ResolveTerraformOutputWithMocks evaluates the caller's expression against the component mocks // deep-merged under the real outputs in `fallback` mode. Precedence is therefore: real value, // then component mock, then YQ `//` default, then the original lookup error (or nil for an output // missing from provisioned state). @@ -172,29 +178,31 @@ func declaredFallbackMocks( // The realOutputs argument is the whole real output map (nil when the state is not provisioned or the // output map is absent) and realErr is the recoverable lookup error that produced it, if any. // Neither the mocks nor the real map is mutated: the merge builds new maps. -func resolveTerraformOutputWithMocks( +func (e *Executor) ResolveTerraformOutputWithMocks( atmosConfig *schema.AtmosConfiguration, - lookup *terraformStateLookup, + lookup *TerraformStateLookup, mocks map[string]any, realOutputs map[string]any, realErr error, ) (any, error) { + defer perf.Track(nil, "terraform.Executor.ResolveTerraformOutputWithMocks")() + merged := mergeRealOverMocks(mocks, realOutputs) - value, err := tb.GetTerraformBackendVariable(atmosConfig, merged, lookup.output) + value, err := tb.GetTerraformBackendVariable(atmosConfig, merged, lookup.Output) if err != nil { - return nil, fmt.Errorf("failed to evaluate Terraform output %q for component %q in stack %q against component mocks: %w", lookup.output, lookup.component, lookup.stack, err) + return nil, fmt.Errorf("failed to evaluate Terraform output %q for component %q in stack %q against component mocks: %w", lookup.Output, lookup.Component, lookup.Stack, err) } if value == nil { - return nil, missingTerraformOutputWithMocks(merged, lookup.output, realErr) + return nil, e.missingTerraformOutputWithMocks(merged, lookup.Output, realErr) } - if resolvedFromMocks(mocks, realOutputs, lookup.output) { + if resolvedFromMocks(mocks, realOutputs, lookup.Output) { log.Debug( "Terraform YAML function resolved from component mocks (fallback)", - "component", lookup.component, - "stack", lookup.stack, - "output", lookup.output, + "component", lookup.Component, + "stack", lookup.Stack, + "output", lookup.Output, ) } return value, nil @@ -232,10 +240,12 @@ func resolvedFromMocks(mocks, realOutputs map[string]any, output string) bool { return inMocks } -// withFallbackModeHint adds a hint to a non-recoverable lookup error raised in `fallback` mode: +// WithFallbackModeHint adds a hint to a non-recoverable lookup error raised in `fallback` mode: // fallback reads real state first, so a machine without credentials, network access, or the // Terraform binary now fails where mocks-only resolution would not. -func withFallbackModeHint(err error) error { +func WithFallbackModeHint(err error) error { + defer perf.Track(nil, "terraform.WithFallbackModeHint")() + return errUtils.Build(err). WithHintf("`%s` runs in `%s` mode, which reads real state first and uses mocks only when state or an output is missing. To resolve from mocks only, without Terraform, credentials, or backend access, pass `%s=%s` or set `components.terraform.mocks.mode: %s`.", cfg.UseMocksFlag, schema.TerraformMocksModeFallback, cfg.UseMocksFlag, schema.TerraformMocksModeAlways, schema.TerraformMocksModeAlways). @@ -246,11 +256,11 @@ func withFallbackModeHint(err error) error { // merged outputs. An explicit null declared in the mocks or the real outputs is a hit. Otherwise // the original recoverable lookup error is preserved (unless a YQ default already ran), and an // output that is simply missing from provisioned state resolves to nil (backward compatible). -func missingTerraformOutputWithMocks(merged map[string]any, output string, realErr error) error { +func (e *Executor) missingTerraformOutputWithMocks(merged map[string]any, output string, realErr error) error { if exists, decidable := mockPathExists(merged, output); decidable && exists { return nil } - if realErr != nil && !hasYqDefault(output) { + if realErr != nil && !e.services.HasYqDefault(output) { return realErr } return nil diff --git a/pkg/component/terraform/mocks_test.go b/pkg/component/terraform/mocks_test.go new file mode 100644 index 00000000000..8b4abb735db --- /dev/null +++ b/pkg/component/terraform/mocks_test.go @@ -0,0 +1,241 @@ +package terraform + +import ( + fmt "fmt" + os "os" + filepath "path/filepath" + testing "testing" + + errUtils "github.com/cloudposse/atmos/errors" + authdeferred "github.com/cloudposse/atmos/pkg/auth/deferred" + cfg "github.com/cloudposse/atmos/pkg/config" + schema "github.com/cloudposse/atmos/pkg/schema" + refcache "github.com/cloudposse/atmos/pkg/terraform/refcache" + testhelpers "github.com/cloudposse/atmos/tests/testhelpers" + assert "github.com/stretchr/testify/assert" + require "github.com/stretchr/testify/require" + gomock "go.uber.org/mock/gomock" +) + +func TestMergeRealOverMocks(t *testing.T) { + mocks := map[string]any{ + "vpc_id": "vpc-mock", + "network": map[string]any{"cidr": "10.0.0.0/16", "tags": map[string]any{"env": "mock", "team": "mock"}}, + "subnets": []any{"mock-a", "mock-b"}, + "only": "mock-only", + } + real := map[string]any{ + "vpc_id": "vpc-real", + "network": map[string]any{"name": "real", "tags": map[string]any{"env": "real"}}, + "subnets": []any{"real-a"}, + "nulled": nil, + } + + merged := mergeRealOverMocks(mocks, real) + + assert.Equal(t, map[string]any{ + "vpc_id": "vpc-real", + "network": map[string]any{"name": "real", "cidr": "10.0.0.0/16", "tags": map[string]any{"env": "real", "team": "mock"}}, + "subnets": []any{"real-a"}, + "only": "mock-only", + "nulled": nil, + }, merged) + + // Neither input is mutated by the merge, and the result does not alias their nested maps. + assert.Equal(t, map[string]any{"cidr": "10.0.0.0/16", "tags": map[string]any{"env": "mock", "team": "mock"}}, mocks["network"]) + assert.Equal(t, map[string]any{"name": "real", "tags": map[string]any{"env": "real"}}, real["network"]) + merged["network"].(map[string]any)["cidr"] = "changed" + assert.Equal(t, "10.0.0.0/16", mocks["network"].(map[string]any)["cidr"]) +} + +func TestResolvedFromMocks(t *testing.T) { + mocks := map[string]any{"vpc_id": "vpc-mock", "network": map[string]any{"cidr": "10.0.0.0/16"}} + real := map[string]any{"vpc_id": "vpc-real", "network": map[string]any{"name": "real"}} + tests := []struct { + output string + want bool + }{ + {output: "vpc_id", want: false}, + {output: ".network.cidr", want: true}, + {output: ".network.name", want: false}, + {output: "missing", want: false}, + {output: `.network.cidr // "x"`, want: false}, + } + for _, tt := range tests { + t.Run(tt.output, func(t *testing.T) { + assert.Equal(t, tt.want, resolvedFromMocks(mocks, real, tt.output)) + }) + } +} + +func TestMockPathExists(t *testing.T) { + mocks := map[string]any{ + "vpc_id": "vpc-1", + "nullable": nil, + "network": map[string]any{"cidr": "10.0.0.0/16"}, + } + tests := []struct { + output string + wantExists bool + wantDecidable bool + wantOutputFlag bool + }{ + {output: "vpc_id", wantExists: true, wantDecidable: true, wantOutputFlag: true}, + {output: "nullable", wantExists: true, wantDecidable: true, wantOutputFlag: true}, + {output: "missing", wantExists: false, wantDecidable: true, wantOutputFlag: false}, + {output: ".network.cidr", wantExists: true, wantDecidable: true, wantOutputFlag: true}, + {output: ".network.missing", wantExists: false, wantDecidable: true, wantOutputFlag: false}, + {output: ".vpc_id.deeper", wantExists: false, wantDecidable: true, wantOutputFlag: false}, + {output: `.missing // "x"`, wantExists: false, wantDecidable: false, wantOutputFlag: true}, + } + for _, tt := range tests { + t.Run(tt.output, func(t *testing.T) { + exists, decidable := mockPathExists(mocks, tt.output) + assert.Equal(t, tt.wantExists, exists) + assert.Equal(t, tt.wantDecidable, decidable) + assert.Equal(t, tt.wantOutputFlag, mockOutputExists(mocks, tt.output)) + }) + } +} + +// TestResolveTerraformMockOutputInvalidMode verifies that an unknown +// components.terraform.mocks.mode (only reachable through atmos.yaml, since the +// env var and flag paths validate it) fails loudly under --use-mocks instead of +// silently matching neither mode, and is ignored when mocks are off. +func TestResolveTerraformMockOutputInvalidMode(t *testing.T) { + atmosConfig := &schema.AtmosConfiguration{} + atmosConfig.Components.Terraform.Mocks.Mode = schema.TerraformMocksMode("bogus") + + t.Run("use-mocks on errors", func(t *testing.T) { + value, handled, err := testExecutor(t).ResolveTerraformMockOutput(atmosConfig, &schema.ConfigAndStacksInfo{UseMocks: true}, "dev", "vpc", "vpc_id") + require.ErrorIs(t, err, errUtils.ErrInvalidMocksMode) + assert.True(t, handled) + assert.Nil(t, value) + assert.Contains(t, err.Error(), `"bogus"`) + }) + + t.Run("use-mocks off ignores the mode", func(t *testing.T) { + value, handled, err := testExecutor(t).ResolveTerraformMockOutput(atmosConfig, &schema.ConfigAndStacksInfo{UseMocks: false}, "dev", "vpc", "vpc_id") + require.NoError(t, err) + assert.False(t, handled) + assert.Nil(t, value) + }) +} + +func TestTerraformMocksModeGuards(t *testing.T) { + assert.Equal(t, schema.TerraformMocksModeFallback, terraformMocksMode(nil)) + + invalid := &schema.AtmosConfiguration{} + invalid.Components.Terraform.Mocks.Mode = "bogus" + require.NoError(t, validateTerraformMocksMode(invalid, nil), "a nil stack info means mocks are off") +} + +func authContextForProfile(profile string) *schema.AuthContext { + return &schema.AuthContext{AWS: &schema.AWSAuthContext{Profile: profile}} +} + +func writeTerraformStateOutput(t *testing.T, path, value string) { + t.Helper() + require.NoError(t, os.WriteFile(path, []byte(fmt.Sprintf(`{"version":4,"outputs":{"id":{"value":%q,"type":"string"}}}`, value)), 0o600)) +} + +func setupDeferredCacheTarget(t *testing.T) (*schema.AtmosConfiguration, *authdeferred.MockAuthFactory, string) { + t.Helper() + dir := t.TempDir() + for name, content := range map[string]string{ + "atmos.yaml": `base_path: . +components: + terraform: + base_path: components/terraform +stacks: + base_path: stacks + included_paths: ["**/*"] +`, + "components/terraform/target/main.tf": "", + "stacks/dev.yaml": `components: + terraform: + target: + backend_type: local + backend: + local: + path: cached.tfstate + vars: {} +`, + } { + path := filepath.Join(dir, name) + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700)) + require.NoError(t, os.WriteFile(path, []byte(content), 0o600)) + } + t.Chdir(dir) + t.Setenv("ATMOS_CLI_CONFIG_PATH", dir) + t.Setenv("ATMOS_BASE_PATH", dir) + ac, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true) + require.NoError(t, err) + factory := authdeferred.NewMockAuthFactory(gomock.NewController(t)) + ac.AuthManager = authdeferred.NewManager(authdeferred.AuthOptions{Factory: factory}) + return &ac, factory, filepath.Join(dir, "components", "terraform", "target", "cached.tfstate") +} + +func TestInvalidateTerraformStateCacheRemovesAllCallerScopes(t *testing.T) { + ac, _, path := setupDeferredCacheTarget(t) + ac.AuthManager = nil + ResetStateCache() + t.Cleanup(ResetStateCache) + scopes := []*schema.AuthContext{authContextForProfile("first"), authContextForProfile("second"), nil} + writeTerraformStateOutput(t, path, "before") + for _, scope := range scopes { + value, err := testExecutor(t).GetTerraformState(ac, "!terraform.state", "dev", "target", "id", false, scope, nil) + require.NoError(t, err) + require.Equal(t, "before", value) + } + + writeTerraformStateOutput(t, path, "after") + invalidateTerraformStateCache("dev", "target") + for _, scope := range scopes { + value, err := testExecutor(t).GetTerraformState(ac, "!terraform.state", "dev", "target", "id", false, scope, nil) + require.NoError(t, err) + require.Equal(t, "after", value) + } +} + +func TestInvalidateTerraformStateCacheAllPartitions(t *testing.T) { + ResetStateCache() + t.Cleanup(ResetStateCache) + keys := []refcache.Key{} + for _, basePath := range []string{"head", "base"} { + for _, configPath := range []string{"config-first", "config-second"} { + for _, maskOnly := range []bool{false, true} { + for _, authDisabled := range []bool{false, true} { + key := refcache.NewKey(&schema.AtmosConfiguration{BasePath: basePath, CliConfigPath: configPath}, "dev", "target") + key.Inspection = maskOnly + key.Auth = refcache.AuthScope(nil, nil, authDisabled) + keys = append(keys, key) + terraformStateCache.Store(key, terraformStateNotProvisionedCacheEntry{}) + } + } + } + } + unrelated := refcache.NewKey(&schema.AtmosConfiguration{}, "other", "target") + terraformStateCache.Store(unrelated, terraformStateNotProvisionedCacheEntry{}) + invalidateTerraformStateCache("dev", "target") + for _, key := range keys { + _, exists := terraformStateCache.Load(key) + require.False(t, exists) + } + _, exists := terraformStateCache.Load(unrelated) + require.True(t, exists, "another stack must retain its cached state") +} + +func setupTerraformYamlFunctionSandbox(t *testing.T, workDir string) { + t.Helper() + + sandbox, err := testhelpers.SetupSandbox(t, workDir) + if err != nil { + t.Fatalf("Failed to setup sandbox: %v", err) + } + t.Cleanup(sandbox.Cleanup) + + for k, v := range sandbox.GetEnvironmentVariables() { + t.Setenv(k, v) + } +} diff --git a/internal/exec/terraform_nested_auth_cache_test.go b/pkg/component/terraform/nested_auth_cache_test.go similarity index 92% rename from internal/exec/terraform_nested_auth_cache_test.go rename to pkg/component/terraform/nested_auth_cache_test.go index d7526f27f3e..d6d5f6e41cd 100644 --- a/internal/exec/terraform_nested_auth_cache_test.go +++ b/pkg/component/terraform/nested_auth_cache_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "testing" @@ -11,7 +11,7 @@ import ( ) // fakeChainAuthManager is a minimal auth.AuthManager whose GetChain returns a fixed chain. -// Only GetChain is exercised by buildComponentAuthCacheKey; the other methods inherit the embedded +// Only GetChain is exercised by BuildComponentAuthCacheKey; the other methods inherit the embedded // nil interface and would panic if called, catching any unexpected use. type fakeChainAuthManager struct { auth.AuthManager @@ -71,9 +71,9 @@ func TestBuildComponentAuthCacheKey(t *testing.T) { t.Run(tc.name, func(t *testing.T) { t.Parallel() - keyA, okA := buildComponentAuthCacheKey(tc.parentA, tc.sectionA) + keyA, okA := BuildComponentAuthCacheKey(tc.parentA, tc.sectionA) require.True(t, okA, "a string-keyed auth section must be cacheable") - keyB, okB := buildComponentAuthCacheKey(tc.parentB, tc.sectionB) + keyB, okB := BuildComponentAuthCacheKey(tc.parentB, tc.sectionB) require.True(t, okB, "a string-keyed auth section must be cacheable") if tc.wantEqual { @@ -90,12 +90,12 @@ func TestBuildComponentAuthCacheKey(t *testing.T) { func TestBuildComponentAuthCacheKey_NonSerializable(t *testing.T) { t.Parallel() - _, ok := buildComponentAuthCacheKey(nil, map[string]any{"identities": make(chan int)}) + _, ok := BuildComponentAuthCacheKey(nil, map[string]any{"identities": make(chan int)}) assert.False(t, ok, "a section that cannot be JSON-serialized must be reported non-cacheable") } // TestBuildComponentAuthCacheKey_AuthContextWrapperNeverCaches verifies that a parent AuthManager -// propagated via authContextWrapper (used by atmos.Component() and !terraform.state to carry an +// propagated via AuthContextWrapper (used by atmos.Component() and !terraform.state to carry an // enclosing component's AuthContext into a nested lookup) is never cached. Its GetChain method always // returns an empty slice (it deliberately cannot prove chain identity — see its doc comment), so // without this guard two DIFFERENT real identities wrapped this way would collapse onto the same cache @@ -105,14 +105,14 @@ func TestBuildComponentAuthCacheKey_AuthContextWrapperNeverCaches(t *testing.T) t.Parallel() section := authSectionWithDefault() - wrapperA := newAuthContextWrapper(&schema.AuthContext{AWS: &schema.AWSAuthContext{Profile: "identity-a"}}) - wrapperB := newAuthContextWrapper(&schema.AuthContext{AWS: &schema.AWSAuthContext{Profile: "identity-b"}}) + wrapperA := NewAuthContextWrapper(&schema.AuthContext{AWS: &schema.AWSAuthContext{Profile: "identity-a"}}) + wrapperB := NewAuthContextWrapper(&schema.AuthContext{AWS: &schema.AWSAuthContext{Profile: "identity-b"}}) - _, cacheableA := buildComponentAuthCacheKey(wrapperA, section) - _, cacheableB := buildComponentAuthCacheKey(wrapperB, section) + _, cacheableA := BuildComponentAuthCacheKey(wrapperA, section) + _, cacheableB := BuildComponentAuthCacheKey(wrapperB, section) - assert.False(t, cacheableA, "an authContextWrapper-rooted lookup must never be cached") - assert.False(t, cacheableB, "an authContextWrapper-rooted lookup must never be cached") + assert.False(t, cacheableA, "an AuthContextWrapper-rooted lookup must never be cached") + assert.False(t, cacheableB, "an AuthContextWrapper-rooted lookup must never be cached") } // sentinelAuthManager is a distinguishable AuthManager used to assert pointer identity of cached results. diff --git a/internal/exec/terraform_nested_auth_helper.go b/pkg/component/terraform/nested_auth_helper.go similarity index 79% rename from internal/exec/terraform_nested_auth_helper.go rename to pkg/component/terraform/nested_auth_helper.go index 08ce897a1da..0ccaa5f3304 100644 --- a/internal/exec/terraform_nested_auth_helper.go +++ b/pkg/component/terraform/nested_auth_helper.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "encoding/json" @@ -9,6 +9,7 @@ import ( errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/auth" authdeferred "github.com/cloudposse/atmos/pkg/auth/deferred" + componentpkg "github.com/cloudposse/atmos/pkg/component" cfg "github.com/cloudposse/atmos/pkg/config" log "github.com/cloudposse/atmos/pkg/logger" "github.com/cloudposse/atmos/pkg/perf" @@ -16,8 +17,8 @@ import ( ) const ( - logKeyComponent = "component" - logKeyStack = "stack" + LogKeyComponent = "component" + LogKeyStack = "stack" ) // nestedAuthManagerCache memoizes per-component AuthManagers resolved for nested terraform.state / @@ -41,23 +42,25 @@ func ResetNestedAuthManagerCache() { }) } -// buildComponentAuthCacheKey keys a per-component AuthManager by the parent auth chain plus a +// BuildComponentAuthCacheKey keys a per-component AuthManager by the parent auth chain plus a // deterministic JSON fingerprint of the component's auth section. Because identities are defined // globally and only referenced by components, "same auth section" is a safe, provable proxy for // "same identity" — it never merges components whose sections differ. The parent chain disambiguates // an inherited identity from an auto-detected one. Returns cacheable=false when the section cannot be // serialized (e.g. a channel/func value), so callers resolve without caching. Shared by // describeStacksProcessor and the nested terraform.state path so the two cannot drift. -func buildComponentAuthCacheKey(parent auth.AuthManager, authSection map[string]any) (string, bool) { - // authContextWrapper only propagates an enclosing component's AuthContext for template/YAML-function +func BuildComponentAuthCacheKey(parent auth.AuthManager, authSection map[string]any) (string, bool) { + defer perf.Track(nil, "terraform.BuildComponentAuthCacheKey")() + + // AuthContextWrapper only propagates an enclosing component's AuthContext for template/YAML-function // evaluation (see terraform_output_utils.go) and cannot prove chain identity: its GetChain always // returns an empty slice by design, since a non-empty chain would incorrectly make nested targets // with their own default-identity auth section inherit the caller's identity instead of their own // (see createComponentAuthManager). Without this guard, two different real identities propagated via - // authContextWrapper would collapse onto the same cache key for any shared-shape auth section, + // AuthContextWrapper would collapse onto the same cache key for any shared-shape auth section, // silently reusing one identity's AuthManager (and credentials) for a different identity's nested // lookup. Always resolve fresh for these parents. - if _, isWrapper := parent.(*authContextWrapper); isWrapper { + if _, isWrapper := parent.(*AuthContextWrapper); isWrapper { return "", false } @@ -74,22 +77,24 @@ func buildComponentAuthCacheKey(parent auth.AuthManager, authSection map[string] // resolveCachedComponentAuthManager returns a memoized AuthManager for authSection when one was // already resolved this process, otherwise it calls resolve and caches a successful, non-nil result. -// resolve is injected so tests can substitute a counting fake; production passes createComponentAuthManager. +// The resolver is injected so tests can substitute a counting fake; production passes createComponentAuthManager. +// +//nolint:revive // Preserve the cache resolver signature and its injected auth constructor. func resolveCachedComponentAuthManager( atmosConfig *schema.AtmosConfiguration, componentConfig map[string]any, component, stack string, parentAuthManager auth.AuthManager, authSection map[string]any, - resolve componentAuthManagerResolver, + resolve ComponentAuthManagerResolver, ) (auth.AuthManager, error) { - cacheKey, cacheable := buildComponentAuthCacheKey(parentAuthManager, authSection) + cacheKey, cacheable := BuildComponentAuthCacheKey(parentAuthManager, authSection) if cacheable { if cached, ok := nestedAuthManagerCache.Load(cacheKey); ok { log.Debug( "Reusing cached component-specific AuthManager", - logKeyComponent, component, - logKeyStack, stack, + LogKeyComponent, component, + LogKeyStack, stack, ) return cached.(auth.AuthManager), nil } @@ -105,8 +110,10 @@ func resolveCachedComponentAuthManager( return resolved, nil } -// hasDefaultIdentity checks if the auth section contains at least one identity with default: true. -func hasDefaultIdentity(authSection map[string]any) bool { +// HasDefaultIdentity checks if the auth section contains at least one identity with default: true. +func HasDefaultIdentity(authSection map[string]any) bool { + defer perf.Track(nil, "terraform.HasDefaultIdentity")() + identities, ok := authSection["identities"].(map[string]any) if !ok || identities == nil { return false @@ -126,7 +133,7 @@ func hasDefaultIdentity(authSection map[string]any) bool { return false } -// resolveAuthManagerForNestedComponent determines which AuthManager to use for a nested component evaluation. +// ResolveAuthManagerForNestedComponent determines which AuthManager to use for a nested component evaluation. // // Logic: // 1. Gets component config WITHOUT processing templates/functions (to avoid circular dependency) @@ -150,22 +157,24 @@ func hasDefaultIdentity(authSection map[string]any) bool { // Returns: // - AuthManager to use for this component (may be new or inherited) // - error if component config retrieval or auth creation fails. -func resolveAuthManagerForNestedComponent( +func (e *Executor) ResolveAuthManagerForNestedComponent( atmosConfig *schema.AtmosConfiguration, component string, stack string, parentAuthManager auth.AuthManager, ) (auth.AuthManager, error) { + defer perf.Track(nil, "terraform.Executor.ResolveAuthManagerForNestedComponent")() + if authdeferred.IsDeferred(atmosConfig.AuthManager) { - return deferredTargetAuth(atmosConfig, component, stack, parentAuthManager) + return e.services.DeferredTargetAuth(atmosConfig, component, stack, parentAuthManager) } // Get component configuration WITHOUT processing templates/functions. - componentConfig, err := getComponentConfigForAuthResolution(component, stack) + componentConfig, err := e.getComponentConfigForAuthResolution(component, stack) if err != nil { log.Debug( "Could not get component config for auth resolution, using parent AuthManager", - logKeyComponent, component, - logKeyStack, stack, + LogKeyComponent, component, + LogKeyStack, stack, "error", err, ) return parentAuthManager, err @@ -177,8 +186,8 @@ func resolveAuthManagerForNestedComponent( // Component has no auth config, inherit parent's AuthManager. log.Debug( "Component has no auth config, inheriting parent AuthManager", - logKeyComponent, component, - logKeyStack, stack, + LogKeyComponent, component, + LogKeyStack, stack, ) return parentAuthManager, nil } @@ -186,12 +195,12 @@ func resolveAuthManagerForNestedComponent( // Check if component's auth config has a default identity. // Only create component-specific AuthManager if there's a default identity. // This prevents interactive selector from showing for component auth overrides. - hasDefault := hasDefaultIdentity(authSection) + hasDefault := HasDefaultIdentity(authSection) if !hasDefault { log.Debug( "Component auth config has no default identity, inheriting parent AuthManager", - logKeyComponent, component, - logKeyStack, stack, + LogKeyComponent, component, + LogKeyStack, stack, ) return parentAuthManager, nil } @@ -199,19 +208,19 @@ func resolveAuthManagerForNestedComponent( // Component has auth config with default identity, create (or reuse) a component-specific AuthManager. log.Debug( "Component has auth config with default identity, creating component-specific AuthManager", - logKeyComponent, component, - logKeyStack, stack, + LogKeyComponent, component, + LogKeyStack, stack, ) return resolveCachedComponentAuthManager( - atmosConfig, componentConfig, component, stack, parentAuthManager, authSection, createComponentAuthManager, + atmosConfig, componentConfig, component, stack, parentAuthManager, authSection, CreateComponentAuthManager, ) } // getComponentConfigForAuthResolution retrieves component configuration without processing // templates or YAML functions to avoid circular dependencies. -func getComponentConfigForAuthResolution(component, stack string) (map[string]any, error) { - componentConfig, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{ +func (e *Executor) getComponentConfigForAuthResolution(component, stack string) (map[string]any, error) { + componentConfig, err := e.services.ExecuteDescribeComponent(&componentpkg.ExecuteDescribeComponentParams{ Component: component, Stack: stack, ProcessTemplates: false, // Critical: avoid triggering template processing @@ -225,14 +234,16 @@ func getComponentConfigForAuthResolution(component, stack string) (map[string]an return componentConfig, nil } -// createComponentAuthManager merges component auth with global auth and creates a new AuthManager. -func createComponentAuthManager( +// CreateComponentAuthManager merges component auth with global auth and creates a new AuthManager. +func CreateComponentAuthManager( atmosConfig *schema.AtmosConfiguration, componentConfig map[string]any, component string, stack string, parentAuthManager auth.AuthManager, ) (auth.AuthManager, error) { + defer perf.Track(nil, "terraform.CreateComponentAuthManager")() + // Merge component auth with global auth. mergedAuthConfig, err := auth.MergeComponentAuthFromConfig( &atmosConfig.Auth, @@ -243,31 +254,14 @@ func createComponentAuthManager( if err != nil { log.Debug( "Could not merge component auth config, using parent AuthManager", - logKeyComponent, component, - logKeyStack, stack, + LogKeyComponent, component, + LogKeyStack, stack, "error", err, ) return parentAuthManager, fmt.Errorf("%w: failed to merge component auth config: %w", errUtils.ErrAuthManager, err) } - // Determine identity to use for component authentication. - // If parent AuthManager exists and is authenticated, inherit its identity. - // This ensures that when user explicitly specifies --identity flag, it propagates to nested components. - var identityName string - if parentAuthManager != nil { - chain := parentAuthManager.GetChain() - if len(chain) > 0 { - // Last element in chain is the authenticated identity. - identityName = chain[len(chain)-1] - log.Debug( - "Inheriting identity from parent AuthManager for component", - logKeyComponent, component, - logKeyStack, stack, - "inheritedIdentity", identityName, - "chain", chain, - ) - } - } + identityName := inheritedComponentIdentity(parentAuthManager, component, stack) // Create and authenticate new AuthManager with merged config. // Use inherited identity from parent, or empty string to auto-detect from component's defaults. @@ -287,8 +281,8 @@ func createComponentAuthManager( if err != nil { log.Debug( "Auth does not exist for the component, using parent AuthManager", - logKeyComponent, component, - logKeyStack, stack, + LogKeyComponent, component, + LogKeyStack, stack, "error", err, ) return parentAuthManager, fmt.Errorf("%w: failed to create component-specific AuthManager: %w", errUtils.ErrAuthConsole, err) @@ -299,11 +293,32 @@ func createComponentAuthManager( chain := componentAuthManager.GetChain() log.Debug( "Created component-specific AuthManager", - logKeyComponent, component, - logKeyStack, stack, + LogKeyComponent, component, + LogKeyStack, stack, "identityChain", chain, ) } return componentAuthManager, nil } + +// inheritedComponentIdentity propagates an explicitly authenticated parent identity. +func inheritedComponentIdentity(parentAuthManager auth.AuthManager, component, stack string) string { + var identityName string + if parentAuthManager != nil { + chain := parentAuthManager.GetChain() + if len(chain) > 0 { + // Last element in chain is the authenticated identity. + identityName = chain[len(chain)-1] + log.Debug( + "Inheriting identity from parent AuthManager for component", + LogKeyComponent, component, + LogKeyStack, stack, + "inheritedIdentity", identityName, + "chain", chain, + ) + } + } + + return identityName +} diff --git a/internal/exec/terraform_nested_auth_helper_test.go b/pkg/component/terraform/nested_auth_helper_test.go similarity index 80% rename from internal/exec/terraform_nested_auth_helper_test.go rename to pkg/component/terraform/nested_auth_helper_test.go index 2cf68e3ce43..b4c8d739216 100644 --- a/internal/exec/terraform_nested_auth_helper_test.go +++ b/pkg/component/terraform/nested_auth_helper_test.go @@ -1,12 +1,15 @@ -package exec +package terraform import ( - "testing" + atomic "sync/atomic" + testing "testing" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - "github.com/cloudposse/atmos/pkg/schema" + auth "github.com/cloudposse/atmos/pkg/auth" + authtypes "github.com/cloudposse/atmos/pkg/auth/types" + schema "github.com/cloudposse/atmos/pkg/schema" + assert "github.com/stretchr/testify/assert" + require "github.com/stretchr/testify/require" + gomock "go.uber.org/mock/gomock" ) // TestHasDefaultIdentity tests the hasDefaultIdentity function with various auth configurations. @@ -110,7 +113,7 @@ func TestHasDefaultIdentity(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() - result := hasDefaultIdentity(tt.authSection) + result := HasDefaultIdentity(tt.authSection) assert.Equal(t, tt.expected, result) }) } @@ -292,7 +295,7 @@ func TestGetComponentConfigForAuthResolution_ErrorHandling(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() - _, err := getComponentConfigForAuthResolution(tt.component, tt.stack) + _, err := testExecutor(t).getComponentConfigForAuthResolution(tt.component, tt.stack) // We expect an error because these components/stacks don't exist // The function should return an error with ErrDescribeComponent wrapped require.Error(t, err) @@ -411,7 +414,7 @@ func TestResolveAuthManagerForNestedComponent_WithoutAuthSection(t *testing.T) { return } - hasDefault := hasDefaultIdentity(tt.authSection) + hasDefault := HasDefaultIdentity(tt.authSection) assert.Equal(t, !tt.shouldReturnEarly, hasDefault) }) } @@ -471,8 +474,78 @@ func TestHasDefaultIdentity_EdgeCases(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { t.Parallel() - result := hasDefaultIdentity(tt.authSection) + result := HasDefaultIdentity(tt.authSection) assert.Equal(t, tt.expected, result) }) } } + +type componentAuthResolverSpy struct { + calls atomic.Int64 + lastCompID string + lastStack string + returnMgr auth.AuthManager + returnErr error +} + +func (s *componentAuthResolverSpy) resolver() ComponentAuthManagerResolver { + return func( + _ *schema.AtmosConfiguration, + _ map[string]any, + component string, + stack string, + _ auth.AuthManager, + ) (auth.AuthManager, error) { + s.calls.Add(1) + s.lastCompID = component + s.lastStack = stack + return s.returnMgr, s.returnErr + } +} + +// authSectionWithDefault returns an auth section map that will make +// hasDefaultIdentity return true. The identity name is deliberately arbitrary; +// the spy never looks at it because resolveComponentAuthManager only delegates. +func authSectionWithDefault() map[string]any { + return map[string]any{ + "identities": map[string]any{ + "ci-default": map[string]any{ + "default": true, + // hasDefaultIdentity does not read kind/chain; omitted. + }, + }, + } +} + +// componentSectionWithAuth wraps an auth section into a component section map +// with the key that matches cfg.AuthSectionName ("auth"). +func componentSectionWithAuth(authSection map[string]any) map[string]any { + section := map[string]any{ + "vars": map[string]any{"stage": "test"}, + } + if authSection != nil { + section["auth"] = authSection + } + return section +} + +func TestInheritedComponentIdentityUsesAuthenticatedEndOfChain(t *testing.T) { + t.Parallel() + require.Empty(t, inheritedComponentIdentity(nil, "app", "dev")) + for _, tc := range []struct { + name string + chain []string + want string + }{ + {name: "empty chain"}, + {name: "single identity", chain: []string{"developer"}, want: "developer"}, + {name: "assumed role", chain: []string{"sso", "account", "deployment"}, want: "deployment"}, + } { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + manager := authtypes.NewMockAuthManager(gomock.NewController(t)) + manager.EXPECT().GetChain().Return(tc.chain) + require.Equal(t, tc.want, inheritedComponentIdentity(manager, "app", "dev")) + }) + } +} diff --git a/internal/exec/terraform_output_authcontext_wrapper_test.go b/pkg/component/terraform/output_authcontext_wrapper_test.go similarity index 88% rename from internal/exec/terraform_output_authcontext_wrapper_test.go rename to pkg/component/terraform/output_authcontext_wrapper_test.go index d75898b4581..0cf451bf269 100644 --- a/internal/exec/terraform_output_authcontext_wrapper_test.go +++ b/pkg/component/terraform/output_authcontext_wrapper_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" @@ -11,13 +11,13 @@ import ( "github.com/cloudposse/atmos/pkg/schema" ) -// TestNewAuthContextWrapper verifies that authContextWrapper is properly created. +// TestNewAuthContextWrapper verifies that AuthContextWrapper is properly created. func TestNewAuthContextWrapper(t *testing.T) { t.Parallel() t.Run("nil authContext returns nil wrapper", func(t *testing.T) { t.Parallel() - wrapper := newAuthContextWrapper(nil) + wrapper := NewAuthContextWrapper(nil) assert.Nil(t, wrapper, "Should return nil for nil authContext") }) @@ -30,11 +30,11 @@ func TestNewAuthContextWrapper(t *testing.T) { }, } - wrapper := newAuthContextWrapper(authContext) + wrapper := NewAuthContextWrapper(authContext) require.NotNil(t, wrapper, "Should create wrapper for valid authContext") - require.NotNil(t, wrapper.stackInfo, "Wrapper should have stackInfo") - assert.Equal(t, authContext, wrapper.stackInfo.AuthContext, "StackInfo should contain the provided authContext") + require.NotNil(t, wrapper.StackInfo, "Wrapper should have stackInfo") + assert.Equal(t, authContext, wrapper.StackInfo.AuthContext, "StackInfo should contain the provided authContext") }) } @@ -51,7 +51,7 @@ func TestAuthContextWrapperGetStackInfo(t *testing.T) { }, } - wrapper := newAuthContextWrapper(authContext) + wrapper := NewAuthContextWrapper(authContext) stackInfo := wrapper.GetStackInfo() @@ -73,7 +73,7 @@ func TestAuthContextWrapperResolvePrincipalSetting(t *testing.T) { }, } - wrapper := newAuthContextWrapper(authContext) + wrapper := NewAuthContextWrapper(authContext) // ResolvePrincipalSetting should always return nil, false for the wrapper. // It only propagates existing auth context, not full identity configuration. @@ -92,7 +92,7 @@ func TestAuthContextWrapperResolvePrincipalSetting(t *testing.T) { func TestAuthContextWrapperCredentialStoreType(t *testing.T) { t.Parallel() - wrapper := newAuthContextWrapper(&schema.AuthContext{}) + wrapper := NewAuthContextWrapper(&schema.AuthContext{}) require.NotNil(t, wrapper) assert.PanicsWithValue(t, "authContextWrapper.CredentialStoreType should not be called", func() { @@ -112,7 +112,7 @@ func TestAuthContextWrapperResolveProviderConfig(t *testing.T) { }, } - wrapper := newAuthContextWrapper(authContext) + wrapper := NewAuthContextWrapper(authContext) // ResolveProviderConfig should always return nil, false for the wrapper. // It only propagates existing auth context, not provider configuration. @@ -129,7 +129,7 @@ func TestAuthContextWrapperResolveProviderConfig(t *testing.T) { func TestAuthContextWrapper_AuthenticateIsolated(t *testing.T) { t.Parallel() - wrapper := &authContextWrapper{stackInfo: &schema.ConfigAndStacksInfo{}} + wrapper := &AuthContextWrapper{StackInfo: &schema.ConfigAndStacksInfo{}} var got *schema.AuthContext var err error diff --git a/internal/exec/terraform_output_getter.go b/pkg/component/terraform/output_getter.go similarity index 88% rename from internal/exec/terraform_output_getter.go rename to pkg/component/terraform/output_getter.go index fec7e55a8b6..4ba0b9a01a0 100644 --- a/internal/exec/terraform_output_getter.go +++ b/pkg/component/terraform/output_getter.go @@ -1,6 +1,6 @@ -package exec +package terraform -//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=mock_$GOFILE -package=$GOPACKAGE +//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=../../../internal/exec/mock_terraform_output_getter_test.go -package=exec import ( "github.com/cloudposse/atmos/pkg/auth" @@ -30,7 +30,7 @@ type TerraformOutputGetter interface { } // defaultOutputGetter implements TerraformOutputGetter using the new pkg/terraform/output package. -type defaultOutputGetter struct{} +type defaultOutputGetter struct{ executor *Executor } //nolint:revive // argument-limit: matches interface signature func (d *defaultOutputGetter) GetOutput( @@ -52,15 +52,15 @@ func (d *defaultOutputGetter) GetOutput( var resolvedAuthManager any if authdeferred.IsDeferred(atmosConfig.AuthManager) { parent, _ := authManager.(auth.AuthManager) - manager, err := deferredTargetAuth(atmosConfig, component, stack, parent) + manager, err := d.executor.services.DeferredTargetAuth(atmosConfig, component, stack, parent) if err != nil { return nil, false, err } resolvedAuthManager = manager - resolvedAuthContext = resolvedTargetAuthContext(atmosConfig, manager, authContext, false) + resolvedAuthContext = d.executor.services.ResolvedTargetAuthContext(atmosConfig, manager, authContext, false) } else { resolvedAuthContext, resolvedAuthManager = resolveNestedOutputAuth( - atmosConfig, component, stack, authContext, authManager, resolveAuthManagerForNestedComponent, + atmosConfig, component, stack, authContext, authManager, d.executor.ResolveAuthManagerForNestedComponent, ) } if terraform.ResolveLookupOptions(options).SecretsMaskOnly || len(terraform.ResolveLookupOptions(options).Skip) > 0 { @@ -88,7 +88,7 @@ func resolveNestedOutputAuth( stack string, authContext *schema.AuthContext, authManager any, - resolve componentFuncAuthResolver, + resolve ComponentAuthResolver, ) (*schema.AuthContext, any) { defer perf.Track(atmosConfig, "exec.resolveNestedOutputAuth")() @@ -111,8 +111,8 @@ func resolveNestedOutputAuth( if err != nil { log.Debug( "Auth does not exist for nested component, using the enclosing component's auth", - logKeyComponent, component, - logKeyStack, stack, + LogKeyComponent, component, + LogKeyStack, stack, "error", err, ) return authContext, authManager @@ -128,9 +128,6 @@ func resolveNestedOutputAuth( return resolvedAuthContext, resolvedAuthMgr } -// Global variable that can be overridden in tests. -var outputGetter TerraformOutputGetter = &defaultOutputGetter{} - // GetTerraformOutput is a backward-compatible wrapper that delegates to pkg/terraform/output. // This function is kept for backward compatibility with existing tests and code. // New code should use tfoutput.GetOutput directly. @@ -153,6 +150,8 @@ func GetTerraformOutput( // GetAllTerraformOutputs is a backward-compatible wrapper that delegates to pkg/terraform/output. // This function is kept for backward compatibility with existing tests and code. // New code should use tfoutput.GetComponentOutputs directly. +// +//nolint:revive // Preserve the output lookup compatibility signature. func GetAllTerraformOutputs( atmosConfig *schema.AtmosConfiguration, component string, @@ -180,3 +179,9 @@ func GetStaticRemoteStateOutput( return tfoutput.GetStaticRemoteStateOutput(atmosConfig, component, stack, remoteStateSection, output) } + +// OutputGetter returns a reference getter bound to this executor. +func (e *Executor) OutputGetter() TerraformOutputGetter { + defer perf.Track(nil, "terraform.Executor.OutputGetter")() + return &defaultOutputGetter{executor: e} +} diff --git a/internal/exec/terraform_output_getter_test.go b/pkg/component/terraform/output_getter_test.go similarity index 99% rename from internal/exec/terraform_output_getter_test.go rename to pkg/component/terraform/output_getter_test.go index cf4e6557455..440a3fc7cba 100644 --- a/internal/exec/terraform_output_getter_test.go +++ b/pkg/component/terraform/output_getter_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "testing" diff --git a/internal/exec/terraform_output_utils.go b/pkg/component/terraform/output_utils.go similarity index 69% rename from internal/exec/terraform_output_utils.go rename to pkg/component/terraform/output_utils.go index b4df5937657..c4867901adc 100644 --- a/internal/exec/terraform_output_utils.go +++ b/pkg/component/terraform/output_utils.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" @@ -11,75 +11,79 @@ import ( "github.com/cloudposse/atmos/pkg/schema" ) -// authContextWrapper is a minimal AuthManager implementation that only provides -// GetStackInfo() for passing AuthContext to ExecuteDescribeComponent. -// Other methods panic if called since this wrapper is only for propagating existing auth context. -type authContextWrapper struct { - stackInfo *schema.ConfigAndStacksInfo +// AuthContextWrapper passes existing authentication context to nested component lookups. +// It exposes stack information but has no identity, provider, or credential configuration. +// Authentication operations are unsupported; most panic if called. +type AuthContextWrapper struct { + StackInfo *schema.ConfigAndStacksInfo } -func (a *authContextWrapper) GetStackInfo() *schema.ConfigAndStacksInfo { +// GetStackInfo returns the stack information carrying the inherited authentication context. +func (a *AuthContextWrapper) GetStackInfo() *schema.ConfigAndStacksInfo { defer perf.Track(nil, "exec.authContextWrapper.GetStackInfo")() - return a.stackInfo + return a.StackInfo } // Stub methods to satisfy AuthManager interface (not used by ExecuteDescribeComponent). -func (a *authContextWrapper) GetCachedCredentials(ctx context.Context, identityName string) (*auth_types.WhoamiInfo, error) { +func (a *AuthContextWrapper) GetCachedCredentials(ctx context.Context, identityName string) (*auth_types.WhoamiInfo, error) { defer perf.Track(nil, "exec.authContextWrapper.GetCachedCredentials")() panic("authContextWrapper.GetCachedCredentials should not be called") } -func (a *authContextWrapper) Authenticate(ctx context.Context, identityName string) (*auth_types.WhoamiInfo, error) { +func (a *AuthContextWrapper) Authenticate(ctx context.Context, identityName string) (*auth_types.WhoamiInfo, error) { defer perf.Track(nil, "exec.authContextWrapper.Authenticate")() panic("authContextWrapper.Authenticate should not be called") } -func (a *authContextWrapper) AuthenticateProvider(ctx context.Context, providerName string) (*auth_types.WhoamiInfo, error) { +// AuthenticateProvider returns ErrNotImplemented because this wrapper cannot authenticate providers. +func (a *AuthContextWrapper) AuthenticateProvider(ctx context.Context, providerName string) (*auth_types.WhoamiInfo, error) { defer perf.Track(nil, "exec.authContextWrapper.AuthenticateProvider")() return nil, fmt.Errorf("%w: authContextWrapper.AuthenticateProvider for template context", errUtils.ErrNotImplemented) } -func (a *authContextWrapper) AuthenticateIsolated(ctx context.Context, identityName string) (*schema.AuthContext, error) { +// AuthenticateIsolated returns ErrNotImplemented because this wrapper cannot authenticate identities. +func (a *AuthContextWrapper) AuthenticateIsolated(ctx context.Context, identityName string) (*schema.AuthContext, error) { defer perf.Track(nil, "exec.authContextWrapper.AuthenticateIsolated")() return nil, fmt.Errorf("%w: authContextWrapper.AuthenticateIsolated for template context", errUtils.ErrNotImplemented) } -func (a *authContextWrapper) Whoami(ctx context.Context, identityName string) (*auth_types.WhoamiInfo, error) { +func (a *AuthContextWrapper) Whoami(ctx context.Context, identityName string) (*auth_types.WhoamiInfo, error) { defer perf.Track(nil, "exec.authContextWrapper.Whoami")() panic("authContextWrapper.Whoami should not be called") } -func (a *authContextWrapper) Validate() error { +func (a *AuthContextWrapper) Validate() error { defer perf.Track(nil, "exec.authContextWrapper.Validate")() panic("authContextWrapper.Validate should not be called") } -func (a *authContextWrapper) GetDefaultIdentity(forceSelect bool) (string, error) { +func (a *AuthContextWrapper) GetDefaultIdentity(forceSelect bool) (string, error) { defer perf.Track(nil, "exec.authContextWrapper.GetDefaultIdentity")() panic("authContextWrapper.GetDefaultIdentity should not be called") } -func (a *authContextWrapper) ListProviders() []string { +func (a *AuthContextWrapper) ListProviders() []string { defer perf.Track(nil, "exec.authContextWrapper.ListProviders")() panic("authContextWrapper.ListProviders should not be called") } -func (a *authContextWrapper) Logout(ctx context.Context, identityName string, deleteKeychain bool) error { +func (a *AuthContextWrapper) Logout(ctx context.Context, identityName string, deleteKeychain bool) error { defer perf.Track(nil, "exec.authContextWrapper.Logout")() panic("authContextWrapper.Logout should not be called") } -func (a *authContextWrapper) GetChain() []string { +// GetChain returns an empty chain so nested components can select their own default identity. +func (a *AuthContextWrapper) GetChain() []string { defer perf.Track(nil, "exec.authContextWrapper.GetChain")() // Return empty slice instead of panicking. @@ -89,97 +93,98 @@ func (a *authContextWrapper) GetChain() []string { return []string{} } -func (a *authContextWrapper) ListIdentities() []string { +func (a *AuthContextWrapper) ListIdentities() []string { defer perf.Track(nil, "exec.authContextWrapper.ListIdentities")() panic("authContextWrapper.ListIdentities should not be called") } -func (a *authContextWrapper) GetProviderForIdentity(identityName string) string { +func (a *AuthContextWrapper) GetProviderForIdentity(identityName string) string { defer perf.Track(nil, "exec.authContextWrapper.GetProviderForIdentity")() panic("authContextWrapper.GetProviderForIdentity should not be called") } -func (a *authContextWrapper) GetFilesDisplayPath(providerName string) string { +func (a *AuthContextWrapper) GetFilesDisplayPath(providerName string) string { defer perf.Track(nil, "exec.authContextWrapper.GetFilesDisplayPath")() panic("authContextWrapper.GetFilesDisplayPath should not be called") } -func (a *authContextWrapper) GetProviderKindForIdentity(identityName string) (string, error) { +func (a *AuthContextWrapper) GetProviderKindForIdentity(identityName string) (string, error) { defer perf.Track(nil, "exec.authContextWrapper.GetProviderKindForIdentity")() panic("authContextWrapper.GetProviderKindForIdentity should not be called") } -func (a *authContextWrapper) GetIdentities() map[string]schema.Identity { +func (a *AuthContextWrapper) GetIdentities() map[string]schema.Identity { defer perf.Track(nil, "exec.authContextWrapper.GetIdentities")() panic("authContextWrapper.GetIdentities should not be called") } -func (a *authContextWrapper) GetProviders() map[string]schema.Provider { +func (a *AuthContextWrapper) GetProviders() map[string]schema.Provider { defer perf.Track(nil, "exec.authContextWrapper.GetProviders")() panic("authContextWrapper.GetProviders should not be called") } -func (a *authContextWrapper) LogoutProvider(ctx context.Context, providerName string, deleteKeychain bool) error { +func (a *AuthContextWrapper) LogoutProvider(ctx context.Context, providerName string, deleteKeychain bool) error { defer perf.Track(nil, "exec.authContextWrapper.LogoutProvider")() panic("authContextWrapper.LogoutProvider should not be called") } -func (a *authContextWrapper) LogoutAll(ctx context.Context, deleteKeychain bool) error { +func (a *AuthContextWrapper) LogoutAll(ctx context.Context, deleteKeychain bool) error { defer perf.Track(nil, "exec.authContextWrapper.LogoutAll")() panic("authContextWrapper.LogoutAll should not be called") } -func (a *authContextWrapper) GetEnvironmentVariables(identityName string) (map[string]string, error) { +func (a *AuthContextWrapper) GetEnvironmentVariables(identityName string) (map[string]string, error) { defer perf.Track(nil, "exec.authContextWrapper.GetEnvironmentVariables")() panic("authContextWrapper.GetEnvironmentVariables should not be called") } -func (a *authContextWrapper) PrepareShellEnvironment(ctx context.Context, identityName string, currentEnv []string) ([]string, error) { +func (a *AuthContextWrapper) PrepareShellEnvironment(ctx context.Context, identityName string, currentEnv []string) ([]string, error) { defer perf.Track(nil, "exec.authContextWrapper.PrepareShellEnvironment")() panic("authContextWrapper.PrepareShellEnvironment should not be called") } -func (a *authContextWrapper) EnsureIdentityEnvironment(ctx context.Context, identityName string) (map[string]string, error) { +func (a *AuthContextWrapper) EnsureIdentityEnvironment(ctx context.Context, identityName string) (map[string]string, error) { defer perf.Track(nil, "exec.authContextWrapper.EnsureIdentityEnvironment")() panic("authContextWrapper.EnsureIdentityEnvironment should not be called") } -func (a *authContextWrapper) ExecuteIntegration(ctx context.Context, integrationName string, opts ...auth_types.IntegrationOption) error { +func (a *AuthContextWrapper) ExecuteIntegration(ctx context.Context, integrationName string, opts ...auth_types.IntegrationOption) error { defer perf.Track(nil, "exec.authContextWrapper.ExecuteIntegration")() panic("authContextWrapper.ExecuteIntegration should not be called") } -func (a *authContextWrapper) ExecuteIdentityIntegrations(ctx context.Context, identityName string) error { +func (a *AuthContextWrapper) ExecuteIdentityIntegrations(ctx context.Context, identityName string) error { defer perf.Track(nil, "exec.authContextWrapper.ExecuteIdentityIntegrations")() panic("authContextWrapper.ExecuteIdentityIntegrations should not be called") } -func (a *authContextWrapper) GetIntegration(integrationName string) (*schema.Integration, error) { +func (a *AuthContextWrapper) GetIntegration(integrationName string) (*schema.Integration, error) { defer perf.Track(nil, "exec.authContextWrapper.GetIntegration")() panic("authContextWrapper.GetIntegration should not be called") } -func (a *authContextWrapper) RevokeEphemeralIntegrations(_ context.Context, _ string, _ *bool) error { +func (a *AuthContextWrapper) RevokeEphemeralIntegrations(_ context.Context, _ string, _ *bool) error { defer perf.Track(nil, "exec.authContextWrapper.RevokeEphemeralIntegrations")() panic("authContextWrapper.RevokeEphemeralIntegrations should not be called") } -func (a *authContextWrapper) ResolvePrincipalSetting(identityName, key string) (interface{}, bool) { +// ResolvePrincipalSetting reports no setting because the wrapper has no identity configuration. +func (a *AuthContextWrapper) ResolvePrincipalSetting(identityName, key string) (interface{}, bool) { defer perf.Track(nil, "exec.authContextWrapper.ResolvePrincipalSetting")() // Return false - this wrapper doesn't have access to identity/provider configuration. @@ -187,7 +192,8 @@ func (a *authContextWrapper) ResolvePrincipalSetting(identityName, key string) ( return nil, false } -func (a *authContextWrapper) ResolveProviderConfig(identityName string) (*schema.Provider, bool) { +// ResolveProviderConfig reports no provider because the wrapper carries context only. +func (a *AuthContextWrapper) ResolveProviderConfig(identityName string) (*schema.Provider, bool) { defer perf.Track(nil, "exec.authContextWrapper.ResolveProviderConfig")() // Return false - this wrapper doesn't have access to provider configuration. @@ -195,7 +201,8 @@ func (a *authContextWrapper) ResolveProviderConfig(identityName string) (*schema return nil, false } -func (a *authContextWrapper) GetRealm() realm.RealmInfo { +// GetRealm returns an empty realm because the wrapper does not resolve authentication realms. +func (a *AuthContextWrapper) GetRealm() realm.RealmInfo { defer perf.Track(nil, "exec.authContextWrapper.GetRealm")() // Return empty realm - this wrapper doesn't track realm information. @@ -203,25 +210,27 @@ func (a *authContextWrapper) GetRealm() realm.RealmInfo { return realm.RealmInfo{} } -func (a *authContextWrapper) CredentialStoreType() string { +func (a *AuthContextWrapper) CredentialStoreType() string { defer perf.Track(nil, "exec.authContextWrapper.CredentialStoreType")() panic("authContextWrapper.CredentialStoreType should not be called") } -func (a *authContextWrapper) MaybeOfferAnyProfileFallback(ctx context.Context) error { +func (a *AuthContextWrapper) MaybeOfferAnyProfileFallback(ctx context.Context) error { defer perf.Track(nil, "exec.authContextWrapper.MaybeOfferAnyProfileFallback")() panic("authContextWrapper.MaybeOfferAnyProfileFallback should not be called") } -// newAuthContextWrapper creates an AuthManager wrapper that returns the given AuthContext. -func newAuthContextWrapper(authContext *schema.AuthContext) *authContextWrapper { +// NewAuthContextWrapper creates an AuthManager wrapper that returns the given AuthContext. +func NewAuthContextWrapper(authContext *schema.AuthContext) *AuthContextWrapper { + defer perf.Track(nil, "terraform.NewAuthContextWrapper")() + if authContext == nil { return nil } - return &authContextWrapper{ - stackInfo: &schema.ConfigAndStacksInfo{ + return &AuthContextWrapper{ + StackInfo: &schema.ConfigAndStacksInfo{ AuthContext: authContext, }, } @@ -238,7 +247,7 @@ func propagateAuthDisabledManager(authManager any, stackInfo *schema.ConfigAndSt defer perf.Track(nil, "exec.propagateAuthDisabledManager")() if authManager == nil && stackInfo != nil && stackInfo.AuthDisabled { - return &authContextWrapper{stackInfo: stackInfo} + return &AuthContextWrapper{StackInfo: stackInfo} } return authManager } diff --git a/internal/exec/terraform_output_utils_test.go b/pkg/component/terraform/output_utils_test.go similarity index 87% rename from internal/exec/terraform_output_utils_test.go rename to pkg/component/terraform/output_utils_test.go index 3efa5dd84db..4b0c2393f38 100644 --- a/internal/exec/terraform_output_utils_test.go +++ b/pkg/component/terraform/output_utils_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" @@ -14,7 +14,7 @@ import ( func TestNewAuthContextWrapper_NilContext(t *testing.T) { t.Parallel() - result := newAuthContextWrapper(nil) + result := NewAuthContextWrapper(nil) assert.Nil(t, result) } @@ -30,10 +30,10 @@ func TestNewAuthContextWrapper_WithContext(t *testing.T) { }, } - wrapper := newAuthContextWrapper(authContext) + wrapper := NewAuthContextWrapper(authContext) require.NotNil(t, wrapper) - require.NotNil(t, wrapper.stackInfo) - assert.Equal(t, authContext, wrapper.stackInfo.AuthContext) + require.NotNil(t, wrapper.StackInfo) + assert.Equal(t, authContext, wrapper.StackInfo.AuthContext) } // TestPropagateAuthDisabledManager covers the shared decision logic used by !terraform.output, @@ -47,12 +47,12 @@ func TestPropagateAuthDisabledManager(t *testing.T) { for _, tc := range []struct { name string stackInfo *schema.ConfigAndStacksInfo - existing *authContextWrapper + existing *AuthContextWrapper wantWrapper bool }{ {name: "nil authManager and AuthDisabled wraps stackInfo", stackInfo: &schema.ConfigAndStacksInfo{AuthDisabled: true}, wantWrapper: true}, {name: "nil authManager with AuthDisabled false stays nil", stackInfo: &schema.ConfigAndStacksInfo{}}, - {name: "non-nil authManager passes through even when AuthDisabled", stackInfo: &schema.ConfigAndStacksInfo{AuthDisabled: true}, existing: &authContextWrapper{stackInfo: &schema.ConfigAndStacksInfo{}}}, + {name: "non-nil authManager passes through even when AuthDisabled", stackInfo: &schema.ConfigAndStacksInfo{AuthDisabled: true}, existing: &AuthContextWrapper{StackInfo: &schema.ConfigAndStacksInfo{}}}, {name: "nil stackInfo is a no-op"}, } { t.Run(tc.name, func(t *testing.T) { @@ -66,9 +66,9 @@ func TestPropagateAuthDisabledManager(t *testing.T) { } switch { case tc.wantWrapper: - wrapper, ok := got.(*authContextWrapper) + wrapper, ok := got.(*AuthContextWrapper) require.True(t, ok, "nil authManager with AuthDisabled must be wrapped") - assert.Same(t, tc.stackInfo, wrapper.stackInfo) + assert.Same(t, tc.stackInfo, wrapper.StackInfo) assert.True(t, wrapper.GetStackInfo().AuthDisabled) case tc.existing != nil: assert.Same(t, tc.existing, got, "an already-populated AuthManager must never be overwritten") @@ -88,7 +88,7 @@ func TestAuthContextWrapper_GetStackInfo(t *testing.T) { }, } - wrapper := newAuthContextWrapper(authContext) + wrapper := NewAuthContextWrapper(authContext) require.NotNil(t, wrapper) stackInfo := wrapper.GetStackInfo() @@ -101,7 +101,7 @@ func TestAuthContextWrapper_GetStackInfo_EmptyAuthContext(t *testing.T) { authContext := &schema.AuthContext{} - wrapper := newAuthContextWrapper(authContext) + wrapper := NewAuthContextWrapper(authContext) require.NotNil(t, wrapper) stackInfo := wrapper.GetStackInfo() @@ -112,8 +112,8 @@ func TestAuthContextWrapper_GetStackInfo_EmptyAuthContext(t *testing.T) { func TestAuthContextWrapper_AuthenticateProvider(t *testing.T) { t.Parallel() - wrapper := &authContextWrapper{ - stackInfo: &schema.ConfigAndStacksInfo{}, + wrapper := &AuthContextWrapper{ + StackInfo: &schema.ConfigAndStacksInfo{}, } ctx := context.Background() @@ -125,9 +125,9 @@ func TestAuthContextWrapper_AuthenticateProvider(t *testing.T) { } // TestAuthContextWrapper_GetChain_NoLongerPanics is a regression test for Issue #1921. -// When !terraform.output processes a component with auth configured, it creates an authContextWrapper +// When !terraform.output processes a component with auth configured, it creates an AuthContextWrapper // to propagate auth context. If the nested component has its own auth config with a default identity, -// resolveAuthManagerForNestedComponent calls GetChain() on the parentAuthManager to inherit the identity. +// testExecutor(t).ResolveAuthManagerForNestedComponent calls GetChain() on the parentAuthManager to inherit the identity. // Previously, GetChain() panicked, causing the reported bug. Now it returns an empty slice. func TestAuthContextWrapper_GetChain_NoLongerPanics(t *testing.T) { t.Parallel() @@ -139,7 +139,7 @@ func TestAuthContextWrapper_GetChain_NoLongerPanics(t *testing.T) { }, } - wrapper := newAuthContextWrapper(authContext) + wrapper := NewAuthContextWrapper(authContext) require.NotNil(t, wrapper) // This should NOT panic (was the bug in #1921). @@ -153,8 +153,8 @@ func TestAuthContextWrapper_GetChain_NoLongerPanics(t *testing.T) { func TestAuthContextWrapper_PanicMethods(t *testing.T) { t.Parallel() - wrapper := &authContextWrapper{ - stackInfo: &schema.ConfigAndStacksInfo{}, + wrapper := &AuthContextWrapper{ + StackInfo: &schema.ConfigAndStacksInfo{}, } ctx := context.Background() @@ -194,7 +194,7 @@ func TestAuthContextWrapper_PanicMethods(t *testing.T) { }) // Test that GetChain returns empty slice (no panic). - // Fixed in #1921: GetChain is called by resolveAuthManagerForNestedComponent, + // Fixed in #1921: GetChain is called by testExecutor(t).ResolveAuthManagerForNestedComponent, // so it must not panic. An empty slice means no inherited identity. chain := wrapper.GetChain() assert.Empty(t, chain, "GetChain should return empty slice") diff --git a/internal/exec/terraform_plan_diff.go b/pkg/component/terraform/plan_diff.go similarity index 70% rename from internal/exec/terraform_plan_diff.go rename to pkg/component/terraform/plan_diff.go index 28fe6b10a78..e8d21e9011e 100644 --- a/internal/exec/terraform_plan_diff.go +++ b/pkg/component/terraform/plan_diff.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "bytes" @@ -27,13 +27,16 @@ var ( ErrNoJSONOutput = errors.New("no JSON output found in terraform show output") ) -type terraformPlanDiffExecutor interface { +type TerraformPlanDiffExecutor interface { ExecuteTerraform(schema.ConfigAndStacksInfo, ...ShellCommandOption) error } -type terraformPlanDiffExecutorFunc func(schema.ConfigAndStacksInfo, ...ShellCommandOption) error +type TerraformPlanDiffExecutorFunc func(schema.ConfigAndStacksInfo, ...ShellCommandOption) error + +//nolint:gocritic // Preserve the value-based execution contract. +func (f TerraformPlanDiffExecutorFunc) ExecuteTerraform(info schema.ConfigAndStacksInfo, opts ...ShellCommandOption) error { + defer perf.Track(nil, "terraform.TerraformPlanDiffExecutorFunc.ExecuteTerraform")() -func (f terraformPlanDiffExecutorFunc) ExecuteTerraform(info schema.ConfigAndStacksInfo, opts ...ShellCommandOption) error { return f(info, opts...) } @@ -46,7 +49,9 @@ type PlanFileOptions struct { } // TerraformPlanDiff represents the plan-diff command implementation. -func TerraformPlanDiff(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { +// +//nolint:revive // Preserve the existing generation and evaluation order during package extraction. +func (s *Operations) TerraformPlanDiff(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { defer perf.Track(atmosConfig, "exec.TerraformPlanDiff")() if _, err := plandiff.Policy(atmosConfig); err != nil { return err @@ -56,13 +61,13 @@ func TerraformPlanDiff(atmosConfig *schema.AtmosConfiguration, info *schema.Conf // This is required to get the correct FinalComponent and ComponentFolderPrefix. // Without this, plan-diff would look in the wrong directory when the component instance // name differs from the actual component (e.g., "foobar-atmos-pro" with metadata.component: "foobar"). - processedInfo, err := ProcessStacks(atmosConfig, *info, true, true, true, nil, nil) + processedInfo, err := s.host.ProcessStacks(atmosConfig, *info, true, true, true, nil, nil) if err != nil { return fmt.Errorf("error processing stacks for plan-diff: %w", err) } // Extract flags and setup paths - origPlanFile, newPlanFile, err := parsePlanDiffFlags(processedInfo.AdditionalArgsAndFlags) + origPlanFile, newPlanFile, err := ParsePlanDiffFlags(processedInfo.AdditionalArgsAndFlags) if err != nil { return err } @@ -91,7 +96,7 @@ func TerraformPlanDiff(atmosConfig *schema.AtmosConfiguration, info *schema.Conf } // Ensure original plan file exists and is absolute - origPlanFile, err = validateOriginalPlanFile(origPlanFile, componentPath) + origPlanFile, err = ValidateOriginalPlanFile(origPlanFile, componentPath) if err != nil { return err } @@ -103,16 +108,18 @@ func TerraformPlanDiff(atmosConfig *schema.AtmosConfiguration, info *schema.Conf NewPlanFile: newPlanFile, TmpDir: tmpDir, } - newPlanFile, err = prepareNewPlanFile(atmosConfig, &processedInfo, opts) + newPlanFile, err = s.prepareNewPlanFile(atmosConfig, &processedInfo, opts) if err != nil { return err } // Compare the plans and generate diff - return comparePlansAndGenerateDiff(atmosConfig, &processedInfo, componentPath, origPlanFile, newPlanFile) + return s.ComparePlansAndGenerateDiff(atmosConfig, &processedInfo, componentPath, origPlanFile, newPlanFile) } -// parsePlanDiffFlags extracts the orig and new plan file paths from command arguments. -func parsePlanDiffFlags(args []string) (string, string, error) { +// ParsePlanDiffFlags extracts the orig and new plan file paths from command arguments. +func ParsePlanDiffFlags(args []string) (string, string, error) { + defer perf.Track(nil, "terraform.ParsePlanDiffFlags")() + origPlanFile := "" newPlanFile := "" @@ -140,8 +147,10 @@ func parsePlanDiffFlags(args []string) (string, string, error) { return origPlanFile, newPlanFile, nil } -// validateOriginalPlanFile ensures original plan file exists and returns absolute path. -func validateOriginalPlanFile(origPlanFile, componentPath string) (string, error) { +// ValidateOriginalPlanFile ensures original plan file exists and returns absolute path. +func ValidateOriginalPlanFile(origPlanFile, componentPath string) (string, error) { + defer perf.Track(nil, "terraform.ValidateOriginalPlanFile")() + // Make sure the original plan file exists if !filepath.IsAbs(origPlanFile) { // If the path is relative, make it absolute based on the component directory @@ -155,20 +164,22 @@ func validateOriginalPlanFile(origPlanFile, componentPath string) (string, error return origPlanFile, nil } -// comparePlansAndGenerateDiff compares two plan files and generates a diff. -func comparePlansAndGenerateDiff(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, origPlanFile, newPlanFile string) error { +// ComparePlansAndGenerateDiff compares two plan files and generates a diff. +func (s *Operations) ComparePlansAndGenerateDiff(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, origPlanFile, newPlanFile string) error { + defer perf.Track(atmosConfig, "terraform.Operations.ComparePlansAndGenerateDiff")() + policy, err := plandiff.Policy(atmosConfig) if err != nil { return err } // Get the JSON representation of the original plan - origPlanJSON, err := getTerraformPlanJSON(atmosConfig, info, componentPath, origPlanFile) + origPlanJSON, err := s.getTerraformPlanJSON(atmosConfig, info, componentPath, origPlanFile) if err != nil { return errors.Wrap(err, "error getting JSON for original plan") } // Get the JSON representation of the new plan - newPlanJSON, err := getTerraformPlanJSON(atmosConfig, info, componentPath, newPlanFile) + newPlanJSON, err := s.getTerraformPlanJSON(atmosConfig, info, componentPath, newPlanFile) if err != nil { return errors.Wrap(err, "error getting JSON for new plan") } @@ -211,14 +222,14 @@ func comparePlansAndGenerateDiff(atmosConfig *schema.AtmosConfiguration, info *s } // getTerraformPlanJSON gets the JSON representation of a terraform plan. -func getTerraformPlanJSON(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, planFile string) (string, error) { +func (s *Operations) getTerraformPlanJSON(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, planFile string) (string, error) { // Run terraform init before show - if err := runTerraformInit(atmosConfig, componentPath, info); err != nil { + if err := s.runTerraformInit(atmosConfig, componentPath, info); err != nil { return "", err } // Copy the plan file to the component directory if needed - planFileInComponentDir, cleanup, err := copyPlanFileIfNeeded(planFile, componentPath) + planFileInComponentDir, cleanup, err := CopyPlanFileIfNeeded(planFile, componentPath) if err != nil { return "", err } @@ -226,16 +237,18 @@ func getTerraformPlanJSON(atmosConfig *schema.AtmosConfiguration, info *schema.C defer cleanup() } // Run terraform show and capture output - output, err := runTerraformShow(info, planFileInComponentDir) + output, err := s.runTerraformShow(info, planFileInComponentDir) if err != nil { return "", err } // Extract JSON from output - return extractJSONFromOutput(output) + return ExtractJSONFromOutput(output) } -// extractJSONFromOutput extracts the JSON part from terraform show output. -func extractJSONFromOutput(output string) (string, error) { +// ExtractJSONFromOutput extracts the JSON part from terraform show output. +func ExtractJSONFromOutput(output string) (string, error) { + defer perf.Track(nil, "terraform.ExtractJSONFromOutput")() + // Find the beginning of the JSON output (first '{' character) jsonStartIdx := strings.Index(output, "{") if jsonStartIdx == -1 { @@ -248,8 +261,10 @@ func extractJSONFromOutput(output string) (string, error) { return jsonOutput, nil } -// copyPlanFileIfNeeded copies the plan file to the component directory if it's not already there. -func copyPlanFileIfNeeded(planFile, componentPath string) (string, func(), error) { +// CopyPlanFileIfNeeded copies the plan file to the component directory if it's not already there. +func CopyPlanFileIfNeeded(planFile, componentPath string) (string, func(), error) { + defer perf.Track(nil, "terraform.CopyPlanFileIfNeeded")() + rel, err := filepath.Rel(componentPath, planFile) if err == nil && !filepath.IsAbs(rel) && rel != ".." && !strings.HasPrefix(rel, ".."+string(filepath.Separator)) { return planFile, nil, nil @@ -281,11 +296,14 @@ func copyPlanFileIfNeeded(planFile, componentPath string) (string, func(), error } // runTerraformShow runs the terraform show command and captures its output. -func runTerraformShow(info *schema.ConfigAndStacksInfo, planFile string) (string, error) { - return runTerraformShowWithExecutor(info, planFile, terraformPlanDiffExecutorFunc(ExecuteTerraform)) +func (s *Operations) runTerraformShow(info *schema.ConfigAndStacksInfo, planFile string) (string, error) { + return s.RunTerraformShowWithExecutor(info, planFile, TerraformPlanDiffExecutorFunc(s.host.ExecuteTerraform)) } -func runTerraformShowWithExecutor(info *schema.ConfigAndStacksInfo, planFile string, executor terraformPlanDiffExecutor) (string, error) { +// RunTerraformShowWithExecutor captures plan JSON through the supplied executor without changing the caller's command metadata. +func (s *Operations) RunTerraformShowWithExecutor(info *schema.ConfigAndStacksInfo, planFile string, executor TerraformPlanDiffExecutor) (string, error) { + defer perf.Track(nil, "terraform.Operations.RunTerraformShowWithExecutor")() + var outputBuf bytes.Buffer // Set up the show command @@ -294,7 +312,7 @@ func runTerraformShowWithExecutor(info *schema.ConfigAndStacksInfo, planFile str showInfo.AdditionalArgsAndFlags = []string{"-json", planFile} // Run the command - if execErr := executor.ExecuteTerraform(showInfo, WithStdoutOverride(&outputBuf)); execErr != nil { + if execErr := executor.ExecuteTerraform(showInfo, component.WithStdoutOverride(&outputBuf)); execErr != nil { return "", fmt.Errorf("error running terraform show: %w", execErr) } @@ -303,13 +321,15 @@ func runTerraformShowWithExecutor(info *schema.ConfigAndStacksInfo, planFile str // runTerraformInit runs a basic terraform init in the specified directory using // terraformRun method (ExecuteTerraform). -func runTerraformInit(atmosConfig *schema.AtmosConfiguration, dir string, info *schema.ConfigAndStacksInfo) error { +func (s *Operations) runTerraformInit(atmosConfig *schema.AtmosConfiguration, dir string, info *schema.ConfigAndStacksInfo) error { + defer perf.Track(atmosConfig, "terraform.Operations.runTerraformInit")() + if info.SkipInit { return nil } // Clean terraform workspace to prevent workspace selection prompt - cleanTerraformWorkspace(*atmosConfig, dir) + CleanTerraformWorkspace(dir) // Create a copy of the info struct with init subcommand initInfo := *info @@ -322,7 +342,7 @@ func runTerraformInit(atmosConfig *schema.AtmosConfiguration, dir string, info * initInfo.AdditionalArgsAndFlags = []string{} // Run terraform init using ExecuteTerraform. - err := ExecuteTerraform(initInfo) + err := s.host.ExecuteTerraform(initInfo) if err != nil { return fmt.Errorf("error running terraform init: %w", err) } diff --git a/internal/exec/terraform_plan_diff_preparation.go b/pkg/component/terraform/plan_diff_preparation.go similarity index 70% rename from internal/exec/terraform_plan_diff_preparation.go rename to pkg/component/terraform/plan_diff_preparation.go index 83247908b21..9823e606a33 100644 --- a/internal/exec/terraform_plan_diff_preparation.go +++ b/pkg/component/terraform/plan_diff_preparation.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "fmt" @@ -9,15 +9,16 @@ import ( "github.com/pkg/errors" errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/perf" "github.com/cloudposse/atmos/pkg/schema" ) // prepareNewPlanFile handles the new plan file (generates one if not provided). -func prepareNewPlanFile(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, opts PlanFileOptions) (string, error) { +func (s *Operations) prepareNewPlanFile(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, opts PlanFileOptions) (string, error) { // If no new plan file is specified, generate one if opts.NewPlanFile == "" { var err error - opts.NewPlanFile, err = generateNewPlanFile(atmosConfig, info, opts.ComponentPath, opts.TmpDir) + opts.NewPlanFile, err = s.GenerateNewPlanFile(atmosConfig, info, opts.ComponentPath, opts.TmpDir) if err != nil { return "", errors.Wrap(err, "error generating new plan file") } @@ -34,8 +35,10 @@ func prepareNewPlanFile(atmosConfig *schema.AtmosConfiguration, info *schema.Con return opts.NewPlanFile, nil } -// filterPlanDiffFlags filters out --orig and --new flags from arguments. -func filterPlanDiffFlags(args []string) []string { +// FilterPlanDiffFlags filters out --orig and --new flags from arguments. +func FilterPlanDiffFlags(args []string) []string { + defer perf.Track(nil, "terraform.FilterPlanDiffFlags")() + var filteredArgs []string for i := 0; i < len(args); i++ { arg := args[i] @@ -58,13 +61,15 @@ func filterPlanDiffFlags(args []string) []string { return filteredArgs } -// generateNewPlanFile generates a new plan file by running terraform plan. -func generateNewPlanFile(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, tmpDir string) (string, error) { +// GenerateNewPlanFile generates a new plan file by running terraform plan. +func (s *Operations) GenerateNewPlanFile(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, tmpDir string) (string, error) { + defer perf.Track(atmosConfig, "terraform.Operations.GenerateNewPlanFile")() + // Create a temporary file for the new plan newPlanFile := filepath.Join(tmpDir, "new.plan") // Run terraform init before plan - if err := runTerraformInit(atmosConfig, componentPath, info); err != nil { + if err := s.runTerraformInit(atmosConfig, componentPath, info); err != nil { return "", err } @@ -77,7 +82,7 @@ func generateNewPlanFile(atmosConfig *schema.AtmosConfiguration, info *schema.Co planInfo.ProcessFunctions = true // Filter out --orig and --new flags from AdditionalArgsAndFlags - planArgs := filterPlanDiffFlags(info.AdditionalArgsAndFlags) + planArgs := FilterPlanDiffFlags(info.AdditionalArgsAndFlags) // Add -out flag to specify the output plan file planArgs = append(planArgs, "-out="+newPlanFile) @@ -86,7 +91,7 @@ func generateNewPlanFile(atmosConfig *schema.AtmosConfiguration, info *schema.Co planInfo.AdditionalArgsAndFlags = planArgs // Execute the plan command using the standard Atmos terraform execution - err := ExecuteTerraform(planInfo) + err := s.host.ExecuteTerraform(planInfo) if err != nil { // If the error is ErrPlanHasDiff, we want to propagate that error if errors.Is(err, errUtils.ErrPlanHasDiff) { diff --git a/internal/exec/terraform_plan_preservation_test.go b/pkg/component/terraform/plan_diff_preparation_test.go similarity index 51% rename from internal/exec/terraform_plan_preservation_test.go rename to pkg/component/terraform/plan_diff_preparation_test.go index 99f8b6d5123..bfe5107fbe1 100644 --- a/internal/exec/terraform_plan_preservation_test.go +++ b/pkg/component/terraform/plan_diff_preparation_test.go @@ -1,21 +1,76 @@ -package exec +package terraform import ( "os" "path/filepath" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/cloudposse/atmos/pkg/schema" ) +func TestFilterPlanDiffFlags(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + args []string + expected []string + }{ + { + name: "no flags to filter", + args: []string{"--var=foo=bar", "--auto-approve"}, + expected: []string{"--var=foo=bar", "--auto-approve"}, + }, + { + name: "with --orig= and --new= flags", + args: []string{"--orig=orig.plan", "--new=new.plan", "--var=foo=bar"}, + expected: []string{"--var=foo=bar"}, + }, + { + name: "with --orig and --new flags with space", + args: []string{"--orig", "orig.plan", "--var=foo=bar", "--new", "new.plan"}, + expected: []string{"--var=foo=bar"}, + }, + { + name: "with --orig flag but missing value", + args: []string{"--orig", "--var=foo=bar", "--new=new.plan"}, + expected: []string{"--var=foo=bar"}, + }, + { + name: "with mixed flags", + args: []string{"--var=foo=bar", "--orig=orig.plan", "--auto-approve", "--new=new.plan"}, + expected: []string{"--var=foo=bar", "--auto-approve"}, + }, + { + name: "with -var flag using space (integration test format)", + args: []string{"--orig=orig.plan", "-var", "foo=new-value"}, + expected: []string{"-var", "foo=new-value"}, + }, + { + name: "preserves multiple -var flags", + args: []string{"--orig", "orig.plan", "-var", "foo=bar", "-var", "baz=qux", "--new", "new.plan"}, + expected: []string{"-var", "foo=bar", "-var", "baz=qux"}, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + result := FilterPlanDiffFlags(tc.args) + assert.Equal(t, tc.expected, result) + }) + } +} + func TestCleanupTerraformFiles_InspectionPreservesSavedPlan(t *testing.T) { for _, command := range []string{"plan-diff", "generate", "output", "state", "validate", "fmt", "graph", "providers", "version"} { t.Run(command, func(t *testing.T) { config := &schema.AtmosConfiguration{BasePath: t.TempDir()} info := &schema.ConfigAndStacksInfo{SubCommand: command, Component: "demo", ContextPrefix: "dev"} - path := constructTerraformComponentPlanfilePath(config, info) + path := ConstructTerraformComponentPlanfilePath(config, info) require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) original := []byte("saved binary plan for later apply") require.NoError(t, os.WriteFile(path, original, 0o600)) @@ -37,7 +92,7 @@ func TestCopyPlanFileIfNeeded_PreservesExistingSameName(t *testing.T) { existing := filepath.Join(component, "saved.planfile") require.NoError(t, os.WriteFile(source, []byte("external plan"), 0o600)) require.NoError(t, os.WriteFile(existing, []byte("existing plan"), 0o600)) - copied, cleanup, err := copyPlanFileIfNeeded(source, component) + copied, cleanup, err := CopyPlanFileIfNeeded(source, component) require.NoError(t, err) require.NotNil(t, cleanup) defer cleanup() diff --git a/internal/exec/terraform_plugin_cache_test.go b/pkg/component/terraform/plugin_cache_test.go similarity index 99% rename from internal/exec/terraform_plugin_cache_test.go rename to pkg/component/terraform/plugin_cache_test.go index 4855b85644c..4a081a3b2de 100644 --- a/internal/exec/terraform_plugin_cache_test.go +++ b/pkg/component/terraform/plugin_cache_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "os" diff --git a/internal/exec/terraform_policy.go b/pkg/component/terraform/policy.go similarity index 86% rename from internal/exec/terraform_policy.go rename to pkg/component/terraform/policy.go index 80160fb3bd5..ad8c55b2d18 100644 --- a/internal/exec/terraform_policy.go +++ b/pkg/component/terraform/policy.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" @@ -8,6 +8,7 @@ import ( errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/ci" + "github.com/cloudposse/atmos/pkg/component" cfg "github.com/cloudposse/atmos/pkg/config" envpkg "github.com/cloudposse/atmos/pkg/env" log "github.com/cloudposse/atmos/pkg/logger" @@ -114,17 +115,17 @@ func validateTerraformPolicyOperation(info *schema.ConfigAndStacksInfo, gate *pl } func terraformPolicyInvocation( - atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, args []string, dir string, opts []ShellCommandOption, + atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, args []string, dir string, opts []component.ExecutionOption, ) (context.Context, planfile.Invocation) { - var config shellCommandConfig + var config component.ExecutionOptions for _, option := range opts { option(&config) } - ctx := config.ctx + ctx := config.Ctx if ctx == nil { ctx = context.Background() } - env := config.processEnv + env := config.ProcessEnv if env == nil { env = os.Environ() } @@ -138,12 +139,12 @@ func terraformPolicyInvocation( // executeGovernedTerraformCommand keeps the policy gate outside CI exit-code // mapping and best-effort uploads, while using the existing executor for the main operation. -func executeGovernedTerraformCommand( //nolint:revive // argument-limit: mirrors the existing main command executor plus its gate. +func (e *Executor) executeGovernedTerraformCommand( //nolint:revive // argument-limit: mirrors the existing main command executor plus its gate. atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, args []string, componentPath string, - uploadStatusFlag bool, gate *planfile.Guard, opts ...ShellCommandOption, + uploadStatusFlag bool, gate *planfile.Guard, opts ...component.ExecutionOption, ) error { if gate == nil { - return executeMainTerraformCommand(atmosConfig, info, args, componentPath, uploadStatusFlag, opts...) + return e.executeMainTerraformCommand(atmosConfig, info, args, componentPath, uploadStatusFlag, opts...) } if err := validateTerraformPolicyOperation(info, gate); err != nil { return err @@ -151,17 +152,17 @@ func executeGovernedTerraformCommand( //nolint:revive // argument-limit: mirrors if info.DryRun { policy.Explain(policy.TerraformPlan, "", "unavailable in dry-run: no saved plan evaluated") log.Warn("Dry-run: saved-plan policies were NOT evaluated", "rules", len(gate.Rules)) - return executeMainTerraformCommand(atmosConfig, info, args, componentPath, uploadStatusFlag, opts...) + return e.executeMainTerraformCommand(atmosConfig, info, args, componentPath, uploadStatusFlag, opts...) } ctx, invocation := terraformPolicyInvocation(atmosConfig, info, args, componentPath, opts) result, gateErr := gate.Execute(ctx, &invocation, func(privateArgs []string) process.Result { - err := runMainTerraformCommand(atmosConfig, info, privateArgs, componentPath, opts...) + err := e.runMainTerraformCommand(atmosConfig, info, privateArgs, componentPath, opts...) return process.Result{Err: err, ExitCode: resolveExitCode(err), Started: true, Canceled: ctx.Err() != nil} }) if gateErr != nil { return gateErr } - return finishMainTerraformCommand(atmosConfig, info, result.Err, uploadStatusFlag) + return e.finishMainTerraformCommand(atmosConfig, info, result.Err, uploadStatusFlag) } // Keep the loader's command scope exact, even if a caller passes whitespace. diff --git a/internal/exec/terraform_policy_test.go b/pkg/component/terraform/policy_test.go similarity index 74% rename from internal/exec/terraform_policy_test.go rename to pkg/component/terraform/policy_test.go index ec1b98fe3b0..629553ad6d3 100644 --- a/internal/exec/terraform_policy_test.go +++ b/pkg/component/terraform/policy_test.go @@ -1,21 +1,22 @@ -package exec +package terraform import ( - "bytes" - "context" - "os" + bytes "bytes" + context "context" + json "encoding/json" + os "os" osexec "os/exec" - "path/filepath" - "testing" - "time" - - "github.com/stretchr/testify/require" + filepath "path/filepath" + strings "strings" + testing "testing" + time "time" errUtils "github.com/cloudposse/atmos/errors" - "github.com/cloudposse/atmos/pkg/policy" - "github.com/cloudposse/atmos/pkg/policy/planfile" - "github.com/cloudposse/atmos/pkg/process" - "github.com/cloudposse/atmos/pkg/schema" + policy "github.com/cloudposse/atmos/pkg/policy" + planfile "github.com/cloudposse/atmos/pkg/policy/planfile" + process "github.com/cloudposse/atmos/pkg/process" + schema "github.com/cloudposse/atmos/pkg/schema" + require "github.com/stretchr/testify/require" ) const testTerraformPolicyManifest = `apiVersion: atmos/v1 @@ -40,7 +41,7 @@ func TestTerraformPolicyLoading(t *testing.T) { _, err = loadTerraformPlanPolicy(&info) require.ErrorIs(t, err, errUtils.ErrPolicyInvalid) // Loading happens before Atmos config resolution, even without a usable stack. - require.ErrorIs(t, ExecuteTerraform(info), errUtils.ErrPolicyInvalid) + require.ErrorIs(t, testExecutor(t).services.ExecuteTerraform(info), errUtils.ErrPolicyInvalid) require.NoError(t, os.WriteFile(filepath.Join(directory, "policy.yaml"), []byte(testTerraformPolicyManifest), 0o600)) gate, err = loadTerraformPlanPolicy(&info) require.NoError(t, err) @@ -129,12 +130,12 @@ func TestTerraformPolicyDryRunAndNoGate(t *testing.T) { config := schema.AtmosConfiguration{} info := schema.ConfigAndStacksInfo{SubCommand: "plan", DryRun: true} - require.NoError(t, executeGovernedTerraformCommand(&config, &info, []string{"plan", "-out=p"}, t.TempDir(), false, nil)) + require.NoError(t, testExecutor(t).executeGovernedTerraformCommand(&config, &info, []string{"plan", "-out=p"}, t.TempDir(), false, nil)) // An incomplete Guard would error if dry-run accidentally called extraction/evaluation. - require.NoError(t, executeGovernedTerraformCommand(&config, &info, []string{"plan", "-out=p"}, t.TempDir(), false, &planfile.Guard{})) + require.NoError(t, testExecutor(t).executeGovernedTerraformCommand(&config, &info, []string{"plan", "-out=p"}, t.TempDir(), false, &planfile.Guard{})) require.Contains(t, explanations, policy.Explanation{Kind: policy.TerraformPlan, Status: "unavailable in dry-run: no saved plan evaluated"}) info.SubCommand = "apply" - require.Error(t, executeGovernedTerraformCommand(&config, &info, []string{"apply"}, t.TempDir(), false, &planfile.Guard{})) + require.Error(t, testExecutor(t).executeGovernedTerraformCommand(&config, &info, []string{"apply"}, t.TempDir(), false, &planfile.Guard{})) } func TestTerraformPolicyRetryRequiresGate(t *testing.T) { @@ -144,7 +145,7 @@ func TestTerraformPolicyRetryRequiresGate(t *testing.T) { info := schema.ConfigAndStacksInfo{SubCommand: "plan"} // An unconfigured guard must fail before launching a subprocess, including // the smart-init recovery retry. CI success mappings must not swallow it. - err := retryMainCommand(&config, &info, []string{"plan", "-out=approved.plan"}, t.TempDir(), false, &planfile.Guard{}) + err := testExecutor(t).retryMainCommand(&config, &info, []string{"plan", "-out=approved.plan"}, t.TempDir(), false, &planfile.Guard{}) require.ErrorIs(t, err, errUtils.ErrPolicyInvalid) } @@ -178,28 +179,45 @@ func TestTerraformPolicyRealPlanAndApply(t *testing.T) { var captured bytes.Buffer opts := []ShellCommandOption{WithStdoutOverride(&captured), WithStdoutCapture(&captured), WithProcessContext(ctx)} args := []string{"plan", "-input=false", "-no-color", "-detailed-exitcode", "-out=approved.plan"} - require.NoError(t, executeGovernedTerraformCommand(&config, &info, args, directory, false, gate, opts...)) + require.NoError(t, testExecutor(t).executeGovernedTerraformCommand(&config, &info, args, directory, false, gate, opts...)) require.FileExists(t, filepath.Join(directory, "approved.plan")) require.NotContains(t, captured.String(), "policy-test-secret") require.NotContains(t, captured.String(), `"format_version"`) info.SubCommand = "apply" info.UseTerraformPlan = true - require.NoError(t, executeGovernedTerraformCommand(&config, &info, []string{"apply", "-no-color", "approved.plan"}, directory, false, gate, opts...)) + require.NoError(t, testExecutor(t).executeGovernedTerraformCommand(&config, &info, []string{"apply", "-no-color", "approved.plan"}, directory, false, gate, opts...)) require.FileExists(t, filepath.Join(directory, "terraform.tfstate")) // A fresh no-change plan must still be evaluated and pass. info.SubCommand = "plan" - require.NoError(t, executeGovernedTerraformCommand(&config, &info, args, directory, false, gate, opts...)) + require.NoError(t, testExecutor(t).executeGovernedTerraformCommand(&config, &info, args, directory, false, gate, opts...)) // A destroy plan is denied even though Terraform itself exits successfully (2). args = append(args, "-destroy") - require.ErrorIs(t, executeGovernedTerraformCommand(&config, &info, args, directory, false, gate, opts...), errUtils.ErrPolicyDenied) + require.ErrorIs(t, testExecutor(t).executeGovernedTerraformCommand(&config, &info, args, directory, false, gate, opts...), errUtils.ErrPolicyDenied) // Changed governing policy is re-evaluated before consuming a previously accepted plan. gate.Rules[0].Expression = "true" info.SubCommand = "apply" - require.ErrorIs(t, executeGovernedTerraformCommand(&config, &info, []string{"apply", "approved.plan"}, directory, false, gate, opts...), errUtils.ErrPolicyDenied) + require.ErrorIs(t, testExecutor(t).executeGovernedTerraformCommand(&config, &info, []string{"apply", "approved.plan"}, directory, false, gate, opts...), errUtils.ErrPolicyDenied) // Smart-init retries must retain the same governing gate and reject a saved // artifact under the changed policy rather than taking the ungoverned path. - require.ErrorIs(t, retryMainCommand(&config, &info, []string{"apply", "approved.plan"}, directory, false, gate, opts...), errUtils.ErrPolicyDenied) + require.ErrorIs(t, testExecutor(t).retryMainCommand(&config, &info, []string{"apply", "approved.plan"}, directory, false, gate, opts...), errUtils.ErrPolicyDenied) files, err := filepath.Glob(filepath.Join(directory, ".atmos-*")) require.NoError(t, err) require.Empty(t, files) } + +func TestStarlarkTerraformPolicyRegistration(t *testing.T) { + directory := t.TempDir() + t.Setenv("ATMOS_POLICY_DIR", directory) + document := strings.Replace(testTerraformPolicyManifest, + `rule: "has(input.resource_changes) && input.resource_changes.exists(r, 'delete' in r.change.actions)"`, + "engine: starlark\n rule: return True", 1) + require.NoError(t, os.WriteFile(filepath.Join(directory, "guard.yaml"), []byte(document), 0o600)) + gate, err := loadTerraformPlanPolicy(&schema.ConfigAndStacksInfo{SubCommand: "plan"}) + require.NoError(t, err) + require.NotNil(t, gate) + decision, err := gate.Engine.Evaluate(t.Context(), &policy.Request{ + Kind: policy.TerraformPlan, Input: json.RawMessage(`{"format_version":"1.2","planned_values":{}}`), + }, gate.Rules) + require.NoError(t, err) + require.ErrorIs(t, policy.EnforceDecision(decision), errUtils.ErrPolicyDenied) +} diff --git a/pkg/component/terraform/provider.go b/pkg/component/terraform/provider.go new file mode 100644 index 00000000000..6db0136b1fa --- /dev/null +++ b/pkg/component/terraform/provider.go @@ -0,0 +1,394 @@ +// Package terraform implements the Terraform component provider and runtime. +// Shared host services are injected; this package does not import internal/exec. +package terraform + +//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=mock_runtime_test.go -package=$GOPACKAGE + +import ( + context "context" + fmt "fmt" + filepath "path/filepath" + slices "slices" + sort "sort" + + cobra "github.com/spf13/cobra" + + errUtils "github.com/cloudposse/atmos/errors" + auth "github.com/cloudposse/atmos/pkg/auth" + component "github.com/cloudposse/atmos/pkg/component" + deferred "github.com/cloudposse/atmos/pkg/deferred" + degradation "github.com/cloudposse/atmos/pkg/degradation" + dependency "github.com/cloudposse/atmos/pkg/dependency" + generator "github.com/cloudposse/atmos/pkg/generator" + providers "github.com/cloudposse/atmos/pkg/generator/providers" + required_providers "github.com/cloudposse/atmos/pkg/generator/required_providers" + git "github.com/cloudposse/atmos/pkg/git" + log "github.com/cloudposse/atmos/pkg/logger" + perf "github.com/cloudposse/atmos/pkg/perf" + pro "github.com/cloudposse/atmos/pkg/pro" + provisioner "github.com/cloudposse/atmos/pkg/provisioner" + schema "github.com/cloudposse/atmos/pkg/schema" + tf "github.com/cloudposse/atmos/pkg/terraform" + tfbackend "github.com/cloudposse/atmos/pkg/terraform/backend" + tfoutput "github.com/cloudposse/atmos/pkg/terraform/output" + u "github.com/cloudposse/atmos/pkg/utils" +) + +// Runtime supplies Terraform execution and artifact generation. +// Implementations must preserve execution options and error identity. +type Runtime interface { + Execute(*component.ExecutionContext) error + GenerateArtifacts(*component.ExecutionContext) error +} + +// Provider is the built-in Terraform component implementation. Registration is +// performed by the host that supplies Runtime; importing this package has no +// registration side effects and cannot overwrite an installed provider. +type Provider struct{ runtime Runtime } + +var _ component.ComponentProvider = (*Provider)(nil) + +// New constructs a provider with the supplied Terraform runtime. +func New(runtime Runtime) *Provider { + defer perf.Track(nil, "terraform.New")() + + return &Provider{runtime: runtime} +} + +// GetType returns the stable component family, including OpenTofu executions. +func (*Provider) GetType() string { + defer perf.Track(nil, "terraform.Provider.GetType")() + + return "terraform" +} + +// GetGroup returns the infrastructure tooling group. +func (*Provider) GetGroup() string { + defer perf.Track(nil, "terraform.Provider.GetGroup")() + + return "Infrastructure as Code" +} + +// GetBasePath returns the configured source directory or its conventional default. +func (*Provider) GetBasePath(config *schema.AtmosConfiguration) string { + defer perf.Track(nil, "terraform.Provider.GetBasePath")() + + if config != nil && config.Components.Terraform.BasePath != "" { + return config.Components.Terraform.BasePath + } + return "components/terraform" +} + +// ListComponents lists configured Terraform instances without evaluating them. +func (*Provider) ListComponents(ctx context.Context, _ string, stack map[string]any) ([]string, error) { + defer perf.Track(nil, "terraform.Provider.ListComponents")() + + if err := ctx.Err(); err != nil { + return nil, err + } + components, _ := stack["components"].(map[string]any) + terraform, _ := components["terraform"].(map[string]any) + names := make([]string, 0, len(terraform)) + for name := range terraform { + names = append(names, name) + } + sort.Strings(names) + return names, nil +} + +// ValidateComponent validates the resolved backend type's shape. Policy and +// module validation remain in the lifecycle, where stack and auth are available. +func (*Provider) ValidateComponent(config map[string]any) error { + defer perf.Track(nil, "terraform.Provider.ValidateComponent")() + + if value, exists := config["backend_type"]; exists && value != nil { + if _, ok := value.(string); !ok { + return fmt.Errorf("%w: backend_type must be a string", errUtils.ErrComponentConfigInvalid) + } + } + return nil +} + +// Execute runs a Terraform operation using the component runtime. +func (p *Provider) Execute(ctx *component.ExecutionContext) error { + defer perf.Track(nil, "terraform.Provider.Execute")() + + if err := p.checkContext(ctx); err != nil { + return err + } + return p.runtime.Execute(ctx) +} + +// GenerateArtifacts writes files for an already resolved component. It does not +// initialize Terraform, select a workspace, or execute a plan/apply operation. +func (p *Provider) GenerateArtifacts(ctx *component.ExecutionContext) error { + defer perf.Track(nil, "terraform.Provider.GenerateArtifacts")() + + if err := p.checkContext(ctx); err != nil { + return err + } + if ctx.AtmosConfig == nil || ctx.WorkingDir == "" { + return fmt.Errorf("%w: Terraform artifact generation requires configuration and a working directory", errUtils.ErrComponentConfigInvalid) + } + return p.runtime.GenerateArtifacts(ctx) +} + +// checkContext rejects missing requests, cancelled callers, and providers without a host runtime. +func (p *Provider) checkContext(ctx *component.ExecutionContext) error { + if ctx == nil { + return fmt.Errorf("%w: missing Terraform execution context", errUtils.ErrComponentConfigInvalid) + } + if err := ctx.GoContext().Err(); err != nil { + return err + } + if p.runtime == nil { + return fmt.Errorf("%w: Terraform runtime is not configured", errUtils.ErrComponentExecutionFailed) + } + return nil +} + +// GetAvailableCommands advertises tool commands. Execution deliberately remains +// open to passthrough commands supported by the selected Terraform/OpenTofu binary. +func (*Provider) GetAvailableCommands() []string { + defer perf.Track(nil, "terraform.Provider.GetAvailableCommands")() + + return []string{"apply", "console", "deploy", "destroy", "fmt", "force-unlock", "get", "graph", "import", "init", "login", "logout", "metadata", "output", "plan", "providers", "refresh", "show", "state", "taint", "test", "untaint", "validate", "version", "workspace"} +} + +var _ Runtime = (*Executor)(nil) + +// Execute preserves caller options while making the provider request's context authoritative. +func (e *Executor) Execute(ctx *component.ExecutionContext) error { + defer perf.Track(nil, "terraform.Executor.Execute")() + opts := append(slices.Clone(ctx.Options), component.WithProcessContext(ctx.GoContext())) + return e.execute(ctx.ConfigAndStacksInfo, opts...) +} + +// GenerateArtifacts writes configuration in the prepared component working directory. +func (e *Executor) GenerateArtifacts(ctx *component.ExecutionContext) error { + defer perf.Track(nil, "terraform.Executor.GenerateArtifacts")() + info := ctx.ConfigAndStacksInfo + return e.GenerateConfigFiles(ctx.AtmosConfig, &info, ctx.WorkingDir) +} + +// Services supplies the shared Atmos facilities used by one Terraform executor. +type Services struct { + OperationServices + ExecuteDescribeComponent func(params *component.ExecuteDescribeComponentParams) (map[string]any, error) + ExecuteDescribeStacksWithAuthDisabled func(atmosConfig *schema.AtmosConfiguration, filterByStack string, components []string, componentTypes []string, sections []string, ignoreMissingFiles bool, processTemplates bool, processYamlFunctions bool, includeEmptyStacks bool, skip []string, authManager auth.AuthManager, authDisabled bool) (map[string]any, error) + ExecuteDescribeStacksWithAuthDisabledAndMocks func(atmosConfig *schema.AtmosConfiguration, filterByStack string, components []string, componentTypes []string, sections []string, ignoreMissingFiles bool, processTemplates bool, processYamlFunctions bool, includeEmptyStacks bool, skip []string, authManager auth.AuthManager, authDisabled bool, useMocks bool) (map[string]any, error) + ExecuteDescribeStacksWithMocks func(atmosConfig *schema.AtmosConfiguration, filterByStack string, components []string, componentTypes []string, sections []string, ignoreMissingFiles bool, processTemplates bool, processYamlFunctions bool, includeEmptyStacks bool, skip []string, authManager auth.AuthManager, useMocks bool, tagsFilter []string, labelsFilter map[string]string, errOptions component.DescribeStacksErrorOptions) (map[string]any, error) + ExecuteShellCommand func(atmosConfig schema.AtmosConfiguration, command string, args []string, dir string, env []string, dryRun bool, redirectStdError string, opts ...component.ExecutionOption) error + ExecuteTerraform func(info schema.ConfigAndStacksInfo, opts ...component.ExecutionOption) error + GenerateFilesForComponent func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error + GetAffectedComponents func(args *component.DescribeAffectedCmdArgs) ([]schema.Affected, error) + GetEagerEvaluationSetting func(atmosConfig *schema.AtmosConfiguration) bool + NewDependencyParserWithDelimiter func(builder *dependency.GraphBuilder, nodeMap map[string]string, targetStates map[string]string, leftDelim string) DependencyParser + ProcessStacks func(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo schema.ConfigAndStacksInfo, checkStack bool, processTemplates bool, processYamlFunctions bool, skip []string, authManager auth.AuthManager) (schema.ConfigAndStacksInfo, error) + ValidateComponent func(atmosConfig *schema.AtmosConfiguration, componentName string, componentSection map[string]any, schemaPath string, schemaType string, modulePaths []string, timeoutSeconds int) (bool, error) + AttachComponentReporting func(result *error, info *schema.ConfigAndStacksInfo, componentType string, command string) + CreateAndAuthenticateAuthManager func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (auth.AuthManager, error) + DefaultAuthManagerCreator func(string, *schema.AuthConfig, string, *schema.AtmosConfiguration, auth.ReExecContext) (auth.AuthManager, error) + DefaultMergedAuthConfigGetter func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (*schema.AuthConfig, error) + DeferredTargetAuth func(ac *schema.AtmosConfiguration, component string, stack string, parents ...auth.AuthManager) (auth.AuthManager, error) + DeferredTargetAuthAndCache func(ac *schema.AtmosConfiguration, component string, stack string, parents ...auth.AuthManager) (auth.AuthManager, *deferred.ValueCache, error) + EnvVarFromList func(env []string, key string) string + ExecTerraformShellCommand func(atmosConfig *schema.AtmosConfiguration, component string, stack string, componentEnvList []string, varFile string, workingDir string, workspaceName string, componentPath string) error + GetMergedAuthConfig func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (*schema.AuthConfig, error) + HasYqDefault func(yqExpr string) bool + IsComponentEnabled func(metadataSection map[string]any, componentName string) bool + MapCIExitCode func(atmosConfig *schema.AtmosConfiguration, exitCode int) int + ResolveIdentityConfigError func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, err error, wrapSentinel error) error + ResolvedTargetAuthContext func(ac *schema.AtmosConfiguration, manager auth.AuthManager, fallback *schema.AuthContext, disabled bool) *schema.AuthContext + ShouldUploadStatus func(info *schema.ConfigAndStacksInfo) bool + StoreAutoDetectedIdentity func(authManager auth.AuthManager, info *schema.ConfigAndStacksInfo) + UploadStatus func(info *schema.ConfigAndStacksInfo, exitCode int, client pro.AtmosProAPIClientInterface, gitRepo git.GitRepoInterface) error +} + +// Executor owns Terraform's execution pipeline and reference lookups. +type Executor struct { + *Operations + services Services + executeTerraformInitForcedFn func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, varFile string, reconfigure, upgrade bool, opts ...component.ExecutionOption) error + dispatchAfterInitFn func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, opts ...component.ExecutionOption) + executeMainTerraformCommandFn func( + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + allArgsAndFlags []string, + componentPath string, + uploadStatusFlag bool, + opts ...component.ExecutionOption, + ) error + shellInitFn func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, varFile string, opts ...component.ExecutionOption) error + shellWorkspaceFn func(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, opts ...component.ExecutionOption) error + shellExecFn func(*schema.AtmosConfiguration, string, string, []string, string, string, string, string) error +} + +// NewExecutor binds shared host services to a Terraform executor. +// +//nolint:gocritic // Copy service bindings so later changes to the caller's struct cannot change this executor. +func NewExecutor(services Services) *Executor { + defer perf.Track(nil, "terraform.NewExecutor")() + + e := &Executor{Operations: NewOperations(services.OperationServices), services: services} + e.executeTerraformInitForcedFn = e.executeTerraformInitForced + e.dispatchAfterInitFn = e.dispatchAfterInit + e.executeMainTerraformCommandFn = e.executeMainTerraformCommand + e.shellInitFn = e.executeTerraformInitCommand + e.shellWorkspaceFn = e.runWorkspaceSetup + e.shellExecFn = services.ExecTerraformShellCommand + return e +} + +// DependencyParser is the shared graph parser used by Terraform dependency discovery. +type DependencyParser interface { + ParseComponentDependencies(stackName, componentName string, componentSection map[string]any) error +} + +// ComponentAuthManagerResolver constructs component-scoped authentication. +type ComponentAuthManagerResolver = func(*schema.AtmosConfiguration, map[string]any, string, string, auth.AuthManager) (auth.AuthManager, error) + +// ComponentAuthResolver resolves authentication for a nested component lookup. +type ComponentAuthResolver = func(*schema.AtmosConfiguration, string, string, auth.AuthManager) (auth.AuthManager, error) + +// executionOutputWriters forwards configured process streams to provisioning and generation. +// Without streams, provisioners retain their default output handling. +func executionOutputWriters(opts ...component.ExecutionOption) provisioner.OutputWriters { + var options component.ExecutionOptions + for _, opt := range opts { + opt(&options) + } + if options.Streams == nil { + return provisioner.OutputWriters{} + } + return provisioner.OutputWriters{Stdout: options.Streams.Stdout, Stderr: options.Streams.Stderr} +} + +// OperationServices supplies shared Atmos configuration and execution services. +// Implementations are injected per Operations instance; no process-global callbacks are installed. +type OperationServices interface { + GenerateFilesForComponent(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error + ProcessStacks(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo schema.ConfigAndStacksInfo, checkStack bool, processTemplates bool, processYamlFunctions bool, skip []string, authManager auth.AuthManager) (schema.ConfigAndStacksInfo, error) + FindStacksMap(atmosConfig *schema.AtmosConfiguration, ignoreMissingFiles bool) (map[string]any, map[string]map[string]any, component.AllStacksDeferredContexts, error) + ProcessTmpl(atmosConfig *schema.AtmosConfiguration, tmplName string, tmplValue string, tmplData any, ignoreMissingTemplateValues bool) (string, error) + ProcessTmplWithDatasources(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo *schema.ConfigAndStacksInfo, settingsSection schema.Settings, tmplName string, tmplValue string, tmplData any, ignoreMissingTemplateValues bool) (string, error) + ProcessCustomYamlTags(atmosConfig *schema.AtmosConfiguration, input schema.AtmosSectionMapType, currentStack string, skip []string, stackInfo *schema.ConfigAndStacksInfo) (schema.AtmosSectionMapType, error) + ProcessCommandLineArgs(componentType string, cmd *cobra.Command, args []string, additionalArgsAndFlags []string) (schema.ConfigAndStacksInfo, error) + ResolveDeferredYamlFunctions(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo *schema.ConfigAndStacksInfo, settingsSectionStruct *schema.Settings, componentTemplateContext map[string]any, skip []string, evalSections []string, warnings ...func(degradation.Warning)) error + PrepareConfigurationValues(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, skip []string, sections []string) ([]string, func() error) + StackLocalsForComponent(rawStackConfigs map[string]map[string]any, stackFile string, componentType string) map[string]any + CloneComponentSectionWithOverrides(raw map[string]any, overrides map[string]any) map[string]any + ContainsStarlark(value any) bool + BuildTerraformWorkspace(atmosConfig *schema.AtmosConfiguration, configAndStacksInfo schema.ConfigAndStacksInfo) (string, error) + ExecuteTerraform(info schema.ConfigAndStacksInfo, opts ...component.ExecutionOption) error +} + +// Operations owns Terraform generation, plan comparison, and verification. +type Operations struct{ host OperationServices } + +// NewOperations binds shared services to a Terraform operations instance. +func NewOperations(host OperationServices) *Operations { + defer perf.Track(nil, "terraform.NewOperations")() + return &Operations{host: host} +} + +type ( + GenerateBackendOptions = tf.GenerateBackendOptions + VarfileOptions = tf.VarfileOptions + ShellCommandOption = component.ExecutionOption +) + +const ( + filePermissions = 0o644 + planFileMode = 0o600 +) + +// generateComponentBackendConfig builds backend settings for the selected Terraform workspace. +func generateComponentBackendConfig(backendType string, backendConfig map[string]any, workspace string, _ *schema.AuthContext) (map[string]any, error) { + return tfbackend.Config(backendType, backendConfig, workspace) +} + +// GenerateConfigFiles generates backend, custom, provider, and version configuration. +func (s *Operations) GenerateConfigFiles(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { + defer perf.Track(atmosConfig, "terraform.Operations.GenerateConfigFiles")() + + if err := GenerateBackendConfig(atmosConfig, info, workingDir); err != nil { + return err + } + if err := s.host.GenerateFilesForComponent(atmosConfig, info, workingDir); err != nil { + return err + } + if err := GenerateProviderOverrides(atmosConfig, info, workingDir); err != nil { + return err + } + // Generate required_providers (terraform_override.tf.json) for version pinning (DEV-3124). + return GenerateRequiredProviders(atmosConfig, info, workingDir) +} + +// GenerateBackendConfig delegates backend-file generation using the resolved configuration and working directory. +func GenerateBackendConfig(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { + defer perf.Track(atmosConfig, "terraform.GenerateBackendConfig")() + + return tfbackend.GenerateFile(atmosConfig, info, workingDir) +} + +// GenerateProviderOverrides writes `providers_override.tf.json` from the component's `providers` +// section, and removes a previously generated file when that section is no longer configured. +func GenerateProviderOverrides(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { + defer perf.Track(atmosConfig, "terraform.GenerateProviderOverrides")() + + // Let registered provider-config contributors (e.g. an emulator binding) deep-merge + // provider fragments UNDER the explicit `providers:` section before generation. + genCtx := generator.NewGeneratorContext(atmosConfig, info, workingDir) + if merged, err := generator.ApplyProviderContributors(context.Background(), genCtx); err != nil { + return err + } else if merged != nil { + info.ComponentProvidersSection = merged + } + + // Generate `providers_override.tf.json` file if the `providers` section is configured. + if len(info.ComponentProvidersSection) > 0 { + providerOverrideFileName := filepath.Join(workingDir, providers.DefaultFilenameConst) + + log.Debug("Writing the provider overrides to file.", "file", providerOverrideFileName) + + if !info.DryRun { + providerOverrides := map[string]any{"provider": tfoutput.ProcessProviderAliases(info.ComponentProvidersSection)} + err := u.WriteToFileAsJSON(providerOverrideFileName, providerOverrides, planFileMode) + return err + } + } else if !info.DryRun { + // Remove a stale file, otherwise an earlier stack's providers apply to this one. + return generator.RemoveGenerated(workingDir, providers.DefaultFilenameConst) + } + return nil +} + +// GenerateRequiredProviders generates the terraform_override.tf.json file with required_version +// and required_providers blocks from stack configuration (DEV-3124). +func GenerateRequiredProviders(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { + defer perf.Track(atmosConfig, "exec.GenerateRequiredProviders")() + + // Skip if no required_version or required_providers configured, removing a stale file + // so an earlier stack's version pins don't apply to this one. + if info.RequiredVersion == "" && len(info.RequiredProviders) == 0 { + if info.DryRun { + return nil + } + return generator.RemoveGenerated(workingDir, required_providers.DefaultFilenameConst) + } + + requiredProvidersFileName := filepath.Join(workingDir, required_providers.DefaultFilenameConst) + + log.Debug("Writing the required_providers to file.", "file", requiredProvidersFileName) + + if info.DryRun { + return nil + } + + // Create generator context. + genCtx := generator.NewGeneratorContext(atmosConfig, info, workingDir) + + // Generate and write using the generator package. + return generator.Generate(context.Background(), required_providers.Name, genCtx, generator.NewFileWriter()) +} diff --git a/pkg/component/terraform/provider_test.go b/pkg/component/terraform/provider_test.go new file mode 100644 index 00000000000..dd8cfc1f77f --- /dev/null +++ b/pkg/component/terraform/provider_test.go @@ -0,0 +1,506 @@ +package terraform + +//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=../provider.go -destination=mock_component_provider_test.go -package=terraform + +import ( + bytes "bytes" + context "context" + errors "errors" + os "os" + filepath "path/filepath" + testing "testing" + + errUtils "github.com/cloudposse/atmos/errors" + component "github.com/cloudposse/atmos/pkg/component" + log "github.com/cloudposse/atmos/pkg/logger" + provisioner "github.com/cloudposse/atmos/pkg/provisioner" + scheduleradapters "github.com/cloudposse/atmos/pkg/scheduler/adapters" + schema "github.com/cloudposse/atmos/pkg/schema" + assert "github.com/stretchr/testify/assert" + require "github.com/stretchr/testify/require" + gomock "go.uber.org/mock/gomock" +) + +func TestProviderInventory(t *testing.T) { + p := New(nil) + assert.Equal(t, "terraform", p.GetType()) + assert.Equal(t, "Infrastructure as Code", p.GetGroup()) + assert.Equal(t, "components/terraform", p.GetBasePath(nil)) + config := &schema.AtmosConfiguration{} + assert.Equal(t, "components/terraform", p.GetBasePath(config)) + config.Components.Terraform.BasePath = "infra/modules" + assert.Equal(t, "infra/modules", p.GetBasePath(config)) + commands := p.GetAvailableCommands() + assert.Contains(t, commands, "plan") + assert.Contains(t, commands, "workspace") + commands[0] = "modified" + assert.Equal(t, "apply", p.GetAvailableCommands()[0]) + for _, stack := range []map[string]any{nil, {}, {"components": "invalid"}, {"components": map[string]any{"helm": map[string]any{"release": nil}}}} { + names, err := p.ListComponents(t.Context(), "dev", stack) + require.NoError(t, err) + assert.Empty(t, names) + } + names, err := p.ListComponents(t.Context(), "dev", map[string]any{"components": map[string]any{"terraform": map[string]any{"network": nil, "app": nil}}}) + require.NoError(t, err) + assert.Equal(t, []string{"app", "network"}, names) + ctx, cancel := context.WithCancel(t.Context()) + cancel() + _, err = p.ListComponents(ctx, "dev", nil) + require.ErrorIs(t, err, context.Canceled) +} + +func TestValidateBackendType(t *testing.T) { + p := New(nil) + for _, config := range []map[string]any{nil, {}, {"backend_type": nil}, {"backend_type": "s3"}} { + require.NoError(t, p.ValidateComponent(config)) + } + require.ErrorIs(t, p.ValidateComponent(map[string]any{"backend_type": 42}), errUtils.ErrComponentConfigInvalid) +} + +func TestProviderExecution(t *testing.T) { + runtime := NewMockRuntime(gomock.NewController(t)) + p := New(runtime) + request := &component.ExecutionContext{Context: t.Context(), Component: "app", Stack: "dev", SubCommand: "plan"} + // Detailed-exit-code and cancellation sentinels must retain their identity. + for _, result := range []error{nil, errUtils.ErrPlanHasDiff, context.Canceled} { + runtime.EXPECT().Execute(gomock.Eq(request)).Return(result) + require.ErrorIs(t, p.Execute(request), result) + } + request.AtmosConfig, request.WorkingDir = &schema.AtmosConfiguration{}, t.TempDir() + runtime.EXPECT().GenerateArtifacts(gomock.Eq(request)).Return(nil) + require.NoError(t, p.GenerateArtifacts(request)) + runtime.EXPECT().GenerateArtifacts(gomock.Eq(request)).Return(errUtils.ErrCreateDirectory) + require.ErrorIs(t, p.GenerateArtifacts(request), errUtils.ErrCreateDirectory) +} + +func TestProviderRejectsIncompleteRequests(t *testing.T) { + runtime := NewMockRuntime(gomock.NewController(t)) // No calls are allowed on rejected requests. + p := New(runtime) + for _, operation := range []func(*component.ExecutionContext) error{p.Execute, p.GenerateArtifacts} { + require.ErrorIs(t, operation(nil), errUtils.ErrComponentConfigInvalid) + ctx, cancel := context.WithCancel(t.Context()) + cancel() + require.ErrorIs(t, operation(&component.ExecutionContext{Context: ctx}), context.Canceled) + } + require.ErrorIs(t, New(nil).Execute(&component.ExecutionContext{}), errUtils.ErrComponentExecutionFailed) + require.ErrorIs(t, p.GenerateArtifacts(&component.ExecutionContext{}), errUtils.ErrComponentConfigInvalid) + require.ErrorIs(t, p.GenerateArtifacts(&component.ExecutionContext{AtmosConfig: &schema.AtmosConfiguration{}}), errUtils.ErrComponentConfigInvalid) +} + +func TestOperationsKeepInjectedHostsIndependent(t *testing.T) { + t.Parallel() + + controller := gomock.NewController(t) + first, second := NewMockOperationServices(controller), NewMockOperationServices(controller) + firstOperations, secondOperations := NewOperations(first), NewOperations(second) + original := schema.ConfigAndStacksInfo{ + ComponentFromArg: "app", Stack: "dev", Command: "tofu", SubCommand: "plan-diff", + AdditionalArgsAndFlags: []string{"--orig", "old.plan", "-var", "revision=next"}, + } + writeResult := func(result string) func(schema.ConfigAndStacksInfo, ...component.ExecutionOption) error { + return func(info schema.ConfigAndStacksInfo, options ...component.ExecutionOption) error { + assert.Equal(t, original.ComponentFromArg, info.ComponentFromArg) + assert.Equal(t, original.Stack, info.Stack) + assert.Equal(t, original.Command, info.Command) + assert.Equal(t, "show", info.SubCommand) + assert.Equal(t, []string{"-json", "current.plan"}, info.AdditionalArgsAndFlags) + var execution component.ExecutionOptions + for _, option := range options { + option(&execution) + } + require.NotNil(t, execution.StdoutOverride) + _, err := execution.StdoutOverride.Write([]byte(result)) + return err + } + } + gomock.InOrder( + first.EXPECT().ExecuteTerraform(gomock.Any(), gomock.Any()).DoAndReturn(writeResult("first")), + second.EXPECT().ExecuteTerraform(gomock.Any(), gomock.Any()).DoAndReturn(writeResult("second")), + first.EXPECT().ExecuteTerraform(gomock.Any(), gomock.Any()).DoAndReturn(writeResult("first-again")), + ) + for _, test := range []struct { + operations *Operations + want string + }{{firstOperations, "first"}, {secondOperations, "second"}, {firstOperations, "first-again"}} { + info := original + output, err := test.operations.runTerraformShow(&info, "current.plan") + require.NoError(t, err) + assert.Equal(t, test.want, output) + assert.Equal(t, original, info, "show must not rewrite the caller's command or arguments") + } +} + +func TestOperationsGeneratePlanUsesInitThenPlan(t *testing.T) { + t.Parallel() + + host := NewMockOperationServices(gomock.NewController(t)) + operations := NewOperations(host) + config := &schema.AtmosConfiguration{} + workingDir, temporaryDir := t.TempDir(), t.TempDir() + info := schema.ConfigAndStacksInfo{ + ComponentFromArg: "app", Stack: "dev", SubCommand: "plan-diff", Command: "tofu", + AdditionalArgsAndFlags: []string{"--orig", "old.plan", "--new=next.plan", "-var", "revision=next", "-input=false"}, + ComponentEnvList: []string{"TF_VAR_region=test"}, + } + originalArgs := append([]string{}, info.AdditionalArgsAndFlags...) + planPath := filepath.Join(temporaryDir, "new.plan") + gomock.InOrder( + host.EXPECT().ExecuteTerraform(gomock.Any()).DoAndReturn(func(actual schema.ConfigAndStacksInfo, _ ...component.ExecutionOption) error { + assert.Equal(t, "init", actual.SubCommand) + assert.Empty(t, actual.AdditionalArgsAndFlags) + assert.Equal(t, info.ComponentEnvList, actual.ComponentEnvList) + return nil + }), + host.EXPECT().ExecuteTerraform(gomock.Any()).DoAndReturn(func(actual schema.ConfigAndStacksInfo, _ ...component.ExecutionOption) error { + assert.Equal(t, "plan", actual.SubCommand) + assert.Equal(t, []string{"-var", "revision=next", "-input=false", "-out=" + planPath}, actual.AdditionalArgsAndFlags) + assert.Equal(t, info.ComponentFromArg, actual.ComponentFromArg) + assert.Equal(t, info.Stack, actual.Stack) + assert.Equal(t, info.Command, actual.Command) + assert.Equal(t, info.ComponentEnvList, actual.ComponentEnvList) + assert.True(t, actual.ProcessTemplates) + assert.True(t, actual.ProcessFunctions) + return nil + }), + ) + result, err := operations.GenerateNewPlanFile(config, &info, workingDir, temporaryDir) + require.NoError(t, err) + assert.Equal(t, planPath, result) + assert.Equal(t, "plan-diff", info.SubCommand) + assert.Equal(t, originalArgs, info.AdditionalArgsAndFlags) + assert.False(t, info.ProcessTemplates) + assert.False(t, info.ProcessFunctions) +} + +func TestOperationsKeepExecutionErrors(t *testing.T) { + t.Parallel() + + for _, test := range []struct { + name string + command string + err error + skipInit bool + }{ + {name: "init prevents plan", command: "init", err: errUtils.ErrComponentExecutionFailed}, + {name: "plan error", command: "plan", err: errUtils.ErrComponentExecutionFailed, skipInit: true}, + {name: "detailed plan exit", command: "plan", err: errUtils.ErrPlanHasDiff, skipInit: true}, + {name: "show error", command: "show", err: errUtils.ErrComponentExecutionFailed}, + } { + t.Run(test.name, func(t *testing.T) { + t.Parallel() + host := NewMockOperationServices(gomock.NewController(t)) + operations := NewOperations(host) + info := schema.ConfigAndStacksInfo{SubCommand: "plan-diff", SkipInit: test.skipInit} + var err error + if test.command == "show" { + host.EXPECT().ExecuteTerraform(gomock.Any(), gomock.Any()).Return(test.err) + _, err = operations.runTerraformShow(&info, "current.plan") + } else { + host.EXPECT().ExecuteTerraform(gomock.Any()).DoAndReturn(func(actual schema.ConfigAndStacksInfo, _ ...component.ExecutionOption) error { + assert.Equal(t, test.command, actual.SubCommand) + return test.err + }) + _, err = operations.GenerateNewPlanFile(&schema.AtmosConfiguration{}, &info, t.TempDir(), t.TempDir()) + } + require.ErrorIs(t, err, test.err) + }) + } +} + +func TestOperationsGenerateArtifactsInOrder(t *testing.T) { + t.Parallel() + + host := NewMockOperationServices(gomock.NewController(t)) + operations := NewOperations(host) + config, info := operationArtifactConfig() + workingDir := t.TempDir() + host.EXPECT().GenerateFilesForComponent(config, info, workingDir).DoAndReturn(func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, directory string) error { + require.FileExists(t, filepath.Join(directory, "backend.tf.json"), "backend must precede custom generation") + require.NoFileExists(t, filepath.Join(directory, "providers_override.tf.json")) + require.NoFileExists(t, filepath.Join(directory, "terraform_override.tf.json")) + return os.WriteFile(filepath.Join(directory, "custom.tf"), []byte("# generated by host\n"), 0o600) + }) + require.NoError(t, operations.GenerateConfigFiles(config, info, workingDir)) + backend, err := os.ReadFile(filepath.Join(workingDir, "backend.tf.json")) + require.NoError(t, err) + assert.JSONEq(t, `{"terraform":{"backend":{"local":{"path":"component.tfstate"}}}}`, string(backend)) + providers, err := os.ReadFile(filepath.Join(workingDir, "providers_override.tf.json")) + require.NoError(t, err) + assert.JSONEq(t, `{"provider":{"aws":{"region":"us-east-1"}}}`, string(providers)) + versions, err := os.ReadFile(filepath.Join(workingDir, "terraform_override.tf.json")) + require.NoError(t, err) + assert.JSONEq(t, `{"terraform":{"required_version":">= 1.0.0"}}`, string(versions)) + require.FileExists(t, filepath.Join(workingDir, "custom.tf")) + require.NoDirExists(t, filepath.Join(workingDir, ".terraform"), "generation must not initialize the tool") +} + +func TestOperationsArtifactFailureStopsLaterStages(t *testing.T) { + t.Parallel() + + for _, stage := range []string{"backend", "custom", "providers", "required providers"} { + t.Run(stage, func(t *testing.T) { + t.Parallel() + host := NewMockOperationServices(gomock.NewController(t)) + operations := NewOperations(host) + config, info := operationArtifactConfig() + workingDir := t.TempDir() + blockedFile := map[string]string{ + "backend": "backend.tf.json", "providers": "providers_override.tf.json", "required providers": "terraform_override.tf.json", + }[stage] + if blockedFile != "" { + require.NoError(t, os.Mkdir(filepath.Join(workingDir, blockedFile), 0o700)) + } + if stage != "backend" { + host.EXPECT().GenerateFilesForComponent(config, info, workingDir).DoAndReturn(func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, directory string) error { + require.FileExists(t, filepath.Join(directory, "backend.tf.json")) + if stage == "custom" { + return errUtils.ErrComponentArtifactGeneration + } + return nil + }) + } + err := operations.GenerateConfigFiles(config, info, workingDir) + require.Error(t, err) + if stage == "custom" { + require.ErrorIs(t, err, errUtils.ErrComponentArtifactGeneration) + } else { + var pathError *os.PathError + require.True(t, errors.As(err, &pathError), "filesystem failure must retain its cause: %v", err) + } + if stage == "backend" || stage == "custom" { + require.NoFileExists(t, filepath.Join(workingDir, "providers_override.tf.json")) + } + if stage != "required providers" { + require.NoFileExists(t, filepath.Join(workingDir, "terraform_override.tf.json")) + } else { + require.FileExists(t, filepath.Join(workingDir, "providers_override.tf.json"), "provider output must precede required-provider generation") + } + }) + } +} + +func operationArtifactConfig() (*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo) { + config := &schema.AtmosConfiguration{} + config.Components.Terraform.AutoGenerateBackendFile = true + return config, &schema.ConfigAndStacksInfo{ + ComponentBackendType: "local", ComponentBackendSection: map[string]any{"path": "component.tfstate"}, + ComponentProvidersSection: map[string]any{"aws": map[string]any{"region": "us-east-1"}}, + RequiredVersion: ">= 1.0.0", + } +} + +func TestAutoGenerationPreservesDirectoryAndFailureContract(t *testing.T) { + for _, mode := range []string{"disabled", "dry run", "no generation", "generated", "generator failure", "directory failure"} { + t.Run(mode, func(t *testing.T) { + root := t.TempDir() + path := filepath.Join(root, "generated") + config := &schema.AtmosConfiguration{} + config.Components.Terraform.AutoGenerateFiles = mode != "disabled" + info := &schema.ConfigAndStacksInfo{DryRun: mode == "dry run", ComponentSection: map[string]any{"generate": map[string]any{"main.tf": "content"}}} + if mode == "no generation" { + info.ComponentSection = nil + } + if mode == "directory failure" { + require.NoError(t, os.WriteFile(path, []byte("obstacle"), 0o600)) + } + sentinel := errors.New("generation failed") + executor := NewExecutor(Services{GenerateFilesForComponent: func(actual *schema.AtmosConfiguration, actualInfo *schema.ConfigAndStacksInfo, actualPath string) error { + require.Same(t, config, actual) + require.Same(t, info, actualInfo) + require.Equal(t, path, actualPath) + require.DirExists(t, path) + if mode == "generator failure" { + return sentinel + } + return os.WriteFile(filepath.Join(actualPath, "main.tf"), []byte("generated"), 0o600) + }}) + err := executor.autoGenerateComponentFiles(config, info, path) + switch mode { + case "generator failure": + require.ErrorIs(t, err, sentinel) + case "directory failure": + require.ErrorIs(t, err, errUtils.ErrCreateDirectory) + var pathErr *os.PathError + require.ErrorAs(t, err, &pathErr) + default: + require.NoError(t, err) + } + if mode == "generated" { + content, readErr := os.ReadFile(filepath.Join(path, "main.tf")) + require.NoError(t, readErr) + require.Equal(t, "generated", string(content)) + } else { + require.NoFileExists(t, filepath.Join(path, "main.tf")) + } + }) + } +} + +func TestResolveProvisionedComponentPreservesFailures(t *testing.T) { + for _, mode := range []string{"provision failure", "generation failure", "missing component", "nil context"} { + t.Run(mode, func(t *testing.T) { + config := &schema.AtmosConfiguration{BasePath: t.TempDir()} + config.Components.Terraform.BasePath = "components" + config.Components.Terraform.AutoGenerateFiles = mode == "generation failure" + info := &schema.ConfigAndStacksInfo{ComponentFromArg: "alias", FinalComponent: "module", ComponentSection: map[string]any{"generate": map[string]any{"main.tf": "content"}}} + sentinel := errors.New("provision or generate failed") + original := provisionAndResolveTerraformComponentPath + t.Cleanup(func() { provisionAndResolveTerraformComponentPath = original }) + var expectedPath string + provisionAndResolveTerraformComponentPath = func(ctx context.Context, writers provisioner.OutputWriters, ac *schema.AtmosConfiguration, actual *schema.ConfigAndStacksInfo, kind, path string) (string, bool, error) { + require.NotNil(t, ctx) + _, hasDeadline := ctx.Deadline() + require.True(t, hasDeadline) + require.Same(t, config, ac) + require.Same(t, info, actual) + require.Equal(t, "terraform", kind) + expectedPath = path + if mode == "provision failure" { + return "", false, sentinel + } + return path, mode != "missing component", nil + } + executor := NewExecutor(Services{GenerateFilesForComponent: func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, string) error { return sentinel }}) + path, err := executor.resolveAndProvisionComponentPath(nil, provisioner.OutputWriters{}, config, info) //nolint:staticcheck // Verify the nil-context fallback creates a bounded provisioning context. + switch mode { + case "nil context": + require.NoError(t, err) + require.Equal(t, expectedPath, path) + case "missing component": + require.ErrorIs(t, err, errUtils.ErrInvalidTerraformComponent) + require.Contains(t, err.Error(), "alias") + require.Contains(t, err.Error(), "module") + require.Empty(t, path) + default: + require.ErrorIs(t, err, sentinel) + require.Empty(t, path) + } + }) + } +} + +func TestValidationServicePreservesResultAndError(t *testing.T) { + for _, outcome := range []string{"accepted", "rejected", "error"} { + t.Run(outcome, func(t *testing.T) { + ac := &schema.AtmosConfiguration{} + info := &schema.ConfigAndStacksInfo{ComponentFromArg: "app", ComponentSection: map[string]any{"vars": map[string]any{"stage": "dev"}}} + sentinel := errors.New("schema unavailable") + executor := NewExecutor(Services{ValidateComponent: func(config *schema.AtmosConfiguration, name string, section map[string]any, path, kind string, modules []string, timeout int) (bool, error) { + require.Same(t, ac, config) + require.Equal(t, info.ComponentFromArg, name) + require.Equal(t, info.ComponentSection, section) + require.Empty(t, path) + require.Empty(t, kind) + require.Nil(t, modules) + require.Zero(t, timeout) + if outcome == "error" { + return false, sentinel + } + return outcome == "accepted", nil + }}) + err := executor.validateTerraformComponent(ac, info) + switch outcome { + case "accepted": + require.NoError(t, err) + case "rejected": + require.ErrorIs(t, err, errUtils.ErrInvalidComponent) + require.Contains(t, err.Error(), "app") + case "error": + require.ErrorIs(t, err, sentinel) + } + }) + } +} + +func TestConflictingEnvironmentWarningNamesVariablesWithoutValues(t *testing.T) { + t.Setenv("TF_WORKSPACE", "private-workspace-value") + t.Setenv("TF_VAR_db_password", "secret-password-value") + t.Setenv("TF_CLI_ARGS_plan", "private-cli-value") + var output bytes.Buffer + logger := log.Default() + originalLevel := logger.GetLevel() + logger.SetLevel(log.WarnLevel) + logger.SetOutput(&output) + t.Cleanup(func() { logger.SetLevel(originalLevel); logger.SetOutput(os.Stderr) }) + warnOnConflictingEnvVars() + require.Contains(t, output.String(), "TF_WORKSPACE") + require.Contains(t, output.String(), "TF_VAR_db_password") + require.Contains(t, output.String(), "TF_CLI_ARGS_plan") + require.NotContains(t, output.String(), "private-workspace-value") + require.NotContains(t, output.String(), "secret-password-value") + require.NotContains(t, output.String(), "private-cli-value") +} + +func TestFinalizeVerification(t *testing.T) { + t.Parallel() + + freshPlan := filepath.Join(t.TempDir(), "fresh.tfplan") + + t.Run("drift + fail returns verification error", func(t *testing.T) { + t.Parallel() + info := &schema.ConfigAndStacksInfo{} + err := finalizeVerification(info, freshPlan, " ~ resource changed", true, schema.PlanfileVerifyFail) + require.Error(t, err) + assert.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + assert.Contains(t, err.Error(), "resource changed") + // On failure the fresh plan is NOT selected for apply. + assert.Empty(t, info.PlanFile) + assert.False(t, info.UseTerraformPlan) + }) + + t.Run("drift + warn proceeds with the fresh plan", func(t *testing.T) { + t.Parallel() + info := &schema.ConfigAndStacksInfo{} + err := finalizeVerification(info, freshPlan, " ~ resource changed", true, schema.PlanfileVerifyWarn) + require.NoError(t, err) + assert.Equal(t, freshPlan, info.PlanFile) + assert.True(t, info.UseTerraformPlan) + }) + + t.Run("no drift selects the fresh plan", func(t *testing.T) { + t.Parallel() + info := &schema.ConfigAndStacksInfo{} + err := finalizeVerification(info, freshPlan, "", false, schema.PlanfileVerifyFail) + require.NoError(t, err) + assert.Equal(t, freshPlan, info.PlanFile) + assert.True(t, info.UseTerraformPlan) + }) +} + +func TestScheduledTerraformUsesRegistryAndCapturesOutput(t *testing.T) { + provider := replaceTerraformProvider(t) + var stdout, stderr bytes.Buffer + provider.EXPECT().Execute(gomock.Any()).DoAndReturn(func(request *component.ExecutionContext) error { + require.Same(t, t.Context(), request.Context) + assert.Equal(t, "app", request.ConfigAndStacksInfo.ComponentFromArg) + var options component.ExecutionOptions + for _, option := range request.Options { + option(&options) + } + require.Same(t, &stdout, options.Streams.Stdout) + require.Same(t, &stderr, options.Streams.Stderr) + _, err := options.StdoutCapture.Write([]byte("plan output")) + require.NoError(t, err) + _, err = options.StderrCapture.Write([]byte("diagnostic")) + require.NoError(t, err) + return errUtils.ErrPlanHasDiff + }) + result, err := testExecutor(t).executeTerraformQueryComponent(scheduleradapters.TerraformExecution{ + Context: t.Context(), Info: schema.ConfigAndStacksInfo{ComponentFromArg: "app", SubCommand: "plan"}, + CaptureOutput: true, Stdout: &stdout, Stderr: &stderr, + }) + require.ErrorIs(t, err, errUtils.ErrPlanHasDiff) + assert.Equal(t, "plan output", result.Stdout) + assert.Equal(t, "diagnostic", result.Stderr) +} + +func replaceTerraformProvider(t *testing.T) *MockComponentProvider { + t.Helper() + original, ok := component.GetProvider("terraform") + require.True(t, ok, "the host must register the built-in Terraform provider") + t.Cleanup(func() { require.NoError(t, component.Register(original)) }) + provider := NewMockComponentProvider(gomock.NewController(t)) + provider.EXPECT().GetType().Return("terraform") + require.NoError(t, component.Register(provider)) + return provider +} diff --git a/internal/exec/terraform_providers_aliases_test.go b/pkg/component/terraform/providers_aliases_test.go similarity index 95% rename from internal/exec/terraform_providers_aliases_test.go rename to pkg/component/terraform/providers_aliases_test.go index 11a5d33b47a..7b2c7da98c3 100644 --- a/internal/exec/terraform_providers_aliases_test.go +++ b/pkg/component/terraform/providers_aliases_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "encoding/json" @@ -41,7 +41,7 @@ func TestGenerateProviderOverridesForAliases(t *testing.T) { t.Setenv("ATMOS_CLI_CONFIG_PATH", "") t.Setenv("ATMOS_BASE_PATH", "") - fixture := "../../tests/fixtures/scenarios/atmos-providers-aliases" + fixture := "../../../tests/fixtures/scenarios/atmos-providers-aliases" t.Chdir(fixture) tests := []struct { @@ -70,7 +70,7 @@ func TestGenerateProviderOverridesForAliases(t *testing.T) { atmosConfig, err := cfg.InitCliConfig(info, true) require.NoError(t, err) - result, err := ProcessStacks(&atmosConfig, info, true, false, false, nil, nil) + result, err := testExecutor(t).services.ProcessStacks(&atmosConfig, info, true, false, false, nil, nil) require.NoError(t, err, "ProcessStacks must load the fixture stack") require.NotEmpty(t, result.ComponentProvidersSection, "ComponentProvidersSection should be populated from the stack") diff --git a/internal/exec/terraform_query.go b/pkg/component/terraform/query.go similarity index 79% rename from internal/exec/terraform_query.go rename to pkg/component/terraform/query.go index 1310c686ed3..2c5129cb5ae 100644 --- a/internal/exec/terraform_query.go +++ b/pkg/component/terraform/query.go @@ -1,6 +1,6 @@ -package exec +package terraform -//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=mock_$GOFILE -package=$GOPACKAGE +//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=mock_query_test.go -package=$GOPACKAGE import ( "bytes" @@ -11,6 +11,7 @@ import ( errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/auth" + "github.com/cloudposse/atmos/pkg/component" cfg "github.com/cloudposse/atmos/pkg/config" log "github.com/cloudposse/atmos/pkg/logger" "github.com/cloudposse/atmos/pkg/perf" @@ -41,13 +42,13 @@ func (defaultAuthManagerQueryFactory) Create(identity string, authConfig *schema var authManagerFactory AuthManagerQueryFactory = defaultAuthManagerQueryFactory{} // ExecuteTerraformQuery executes `atmos terraform --query `. -func ExecuteTerraformQuery(info *schema.ConfigAndStacksInfo) error { +func (e *Executor) ExecuteTerraformQuery(info *schema.ConfigAndStacksInfo) error { defer perf.Track(nil, "exec.ExecuteTerraformQuery")() - return ExecuteTerraformQueryWithContext(context.Background(), info) + return e.ExecuteTerraformQueryWithContext(context.Background(), info) } // ExecuteTerraformQueryWithContext executes graph-backed multi-component Terraform work. -func ExecuteTerraformQueryWithContext(ctx context.Context, info *schema.ConfigAndStacksInfo) error { +func (e *Executor) ExecuteTerraformQueryWithContext(ctx context.Context, info *schema.ConfigAndStacksInfo) error { defer perf.Track(nil, "exec.ExecuteTerraformQueryWithContext")() atmosConfig, err := cfg.InitCliConfig(*info, true) if err != nil { @@ -66,10 +67,10 @@ func ExecuteTerraformQueryWithContext(ctx context.Context, info *schema.ConfigAn // Inject auth resolver into identity-aware stores so they can lazily resolve // credentials on first access. This bridges the store system with the auth system. if authManager != nil { - injectTerraformStoreAuthResolver(&atmosConfig, info, authManager) + e.injectTerraformStoreAuthResolver(&atmosConfig, info, authManager) } - stacks, err := describeTerraformStacksForExecution(&atmosConfig, info, authManager, info.Components) + stacks, err := e.describeTerraformStacksForExecution(&atmosConfig, info, authManager, info.Components) if err != nil { return err } @@ -78,7 +79,7 @@ func ExecuteTerraformQueryWithContext(ctx context.Context, info *schema.ConfigAn AtmosConfig: &atmosConfig, Info: info, Stacks: stacks, - Executor: executeTerraformQueryComponent, + Executor: e.executeTerraformQueryComponent, }) } @@ -111,11 +112,13 @@ func createQueryAuthManager(info *schema.ConfigAndStacksInfo, atmosConfig *schem // executeTerraformQueryComponent runs one scheduled Terraform component and captures optional output. // Per-node lifecycle hooks (user + CI, before and after) are handled one layer up by // TerraformDispatcher.Dispatch via info.NodeHooks — this function stays hook-unaware. -func executeTerraformQueryComponent(execution scheduleradapters.TerraformExecution) (scheduleradapters.TerraformExecutionResult, error) { +// +//nolint:gocritic // Match the shared scheduler callback signature. +func (e *Executor) executeTerraformQueryComponent(execution scheduleradapters.TerraformExecution) (scheduleradapters.TerraformExecutionResult, error) { info := execution.Info - opts := []ShellCommandOption{WithProcessContext(execution.Context)} + opts := []component.ExecutionOption{component.WithProcessContext(execution.Context)} if execution.Stdout != nil || execution.Stderr != nil { - opts = append(opts, WithProcessStreams(process.Streams{ + opts = append(opts, component.WithProcessStreams(process.Streams{ Stdin: os.Stdin, Stdout: execution.Stdout, Stderr: execution.Stderr, @@ -124,10 +127,10 @@ func executeTerraformQueryComponent(execution scheduleradapters.TerraformExecuti var stdoutBuf, stderrBuf bytes.Buffer if execution.CaptureOutput { - opts = append(opts, WithStdoutCapture(&stdoutBuf), WithStderrCapture(&stderrBuf)) + opts = append(opts, component.WithStdoutCapture(&stdoutBuf), component.WithStderrCapture(&stderrBuf)) } - execErr := ExecuteTerraform(info, opts...) + execErr := e.services.ExecuteTerraform(info, opts...) return scheduleradapters.TerraformExecutionResult{ Stdout: stdoutBuf.String(), Stderr: stderrBuf.String(), diff --git a/internal/exec/terraform_rc_merge_test.go b/pkg/component/terraform/rc_merge_test.go similarity index 99% rename from internal/exec/terraform_rc_merge_test.go rename to pkg/component/terraform/rc_merge_test.go index 7c3955a0d45..8d498dd8a20 100644 --- a/internal/exec/terraform_rc_merge_test.go +++ b/pkg/component/terraform/rc_merge_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "testing" diff --git a/internal/exec/terraform_remote_state_backend_utils.go b/pkg/component/terraform/remote_state_backend_utils.go similarity index 97% rename from internal/exec/terraform_remote_state_backend_utils.go rename to pkg/component/terraform/remote_state_backend_utils.go index 1dc45d02d06..0bee1675ca5 100644 --- a/internal/exec/terraform_remote_state_backend_utils.go +++ b/pkg/component/terraform/remote_state_backend_utils.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( cfg "github.com/cloudposse/atmos/pkg/config" diff --git a/internal/exec/terraform_remote_state_backend_utils_test.go b/pkg/component/terraform/remote_state_backend_utils_test.go similarity index 99% rename from internal/exec/terraform_remote_state_backend_utils_test.go rename to pkg/component/terraform/remote_state_backend_utils_test.go index 1f85a3a3629..050dc117d50 100644 --- a/internal/exec/terraform_remote_state_backend_utils_test.go +++ b/pkg/component/terraform/remote_state_backend_utils_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "testing" diff --git a/pkg/component/terraform/runtime_host_test.go b/pkg/component/terraform/runtime_host_test.go new file mode 100644 index 00000000000..65852263e21 --- /dev/null +++ b/pkg/component/terraform/runtime_host_test.go @@ -0,0 +1,10 @@ +package terraform_test + +import ( + host "github.com/cloudposse/atmos/internal/exec" + "github.com/cloudposse/atmos/pkg/component/terraform" +) + +func init() { + terraform.NewTestExecutor = host.NewTerraformExecutor +} diff --git a/internal/exec/terraform_shell.go b/pkg/component/terraform/shell.go similarity index 82% rename from internal/exec/terraform_shell.go rename to pkg/component/terraform/shell.go index e54db6249d8..bd9942b3588 100644 --- a/internal/exec/terraform_shell.go +++ b/pkg/component/terraform/shell.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" @@ -12,16 +12,13 @@ import ( log "github.com/cloudposse/atmos/pkg/logger" "github.com/cloudposse/atmos/pkg/perf" "github.com/cloudposse/atmos/pkg/provisioner" - _ "github.com/cloudposse/atmos/pkg/provisioner/source" provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir" "github.com/cloudposse/atmos/pkg/schema" + tf "github.com/cloudposse/atmos/pkg/terraform" "github.com/cloudposse/atmos/pkg/ui" u "github.com/cloudposse/atmos/pkg/utils" ) -// filePermissions is the standard file permission mode for generated files. -const filePermissions = 0o644 - // shellConfig holds the configuration needed for shell execution. type shellConfig struct { componentPath string @@ -41,7 +38,7 @@ func printShellDryRunInfo(info *schema.ConfigAndStacksInfo, cfg *shellConfig) { } // shellInfoFromOptions builds a ConfigAndStacksInfo from ShellOptions. -func shellInfoFromOptions(opts *ShellOptions) schema.ConfigAndStacksInfo { +func shellInfoFromOptions(opts *tf.ShellOptions) schema.ConfigAndStacksInfo { return schema.ConfigAndStacksInfo{ ComponentFromArg: opts.Component, Stack: opts.Stack, @@ -67,7 +64,7 @@ func applyShellSecretEnv(info *schema.ConfigAndStacksInfo, withSecrets bool) err return nil } - secretEnv, err := secretVarEnv(info) + secretEnv, err := SecretVarEnv(info) if err != nil { return err } @@ -89,7 +86,9 @@ func resolveWorkdirPath(componentSection map[string]any, componentPath string) s } // ExecuteTerraformShell starts an interactive shell configured for a terraform component. -func ExecuteTerraformShell(opts *ShellOptions, atmosConfig *schema.AtmosConfiguration) error { +// +//nolint:revive,funlen // Preserve the shell preparation order and resource cleanup during extraction. +func (e *Executor) ExecuteTerraformShell(opts *tf.ShellOptions, atmosConfig *schema.AtmosConfiguration) error { defer perf.Track(atmosConfig, "exec.ExecuteTerraformShell")() log.Debug( @@ -103,7 +102,7 @@ func ExecuteTerraformShell(opts *ShellOptions, atmosConfig *schema.AtmosConfigur // Create and authenticate AuthManager by merging global + component auth config. // This enables YAML functions like !terraform.state to use authenticated credentials. - authManager, err := createAndAuthenticateAuthManager(atmosConfig, &info) + authManager, err := e.services.CreateAndAuthenticateAuthManager(atmosConfig, &info) if err != nil { // Special case: If user aborted (Ctrl+C), exit immediately without showing error. if errors.Is(err, errUtils.ErrUserAborted) { @@ -116,10 +115,10 @@ func ExecuteTerraformShell(opts *ShellOptions, atmosConfig *schema.AtmosConfigur if authManager != nil { info.AuthManager = authManager - injectTerraformStoreAuthResolver(atmosConfig, &info, authManager) + e.injectTerraformStoreAuthResolver(atmosConfig, &info, authManager) } - info, err = ProcessStacks(atmosConfig, info, true, opts.ProcessTemplates, opts.ProcessFunctions, opts.Skip, authManager) + info, err = e.services.ProcessStacks(atmosConfig, info, true, opts.ProcessTemplates, opts.ProcessFunctions, opts.Skip, authManager) if err != nil { return err } @@ -158,8 +157,8 @@ func ExecuteTerraformShell(opts *ShellOptions, atmosConfig *schema.AtmosConfigur cfg := &shellConfig{ componentPath: componentPath, - workingDir: constructTerraformComponentWorkingDir(atmosConfig, &info), - varFile: constructTerraformComponentVarfileName(&info), + workingDir: ConstructTerraformComponentWorkingDir(atmosConfig, &info), + varFile: ConstructTerraformComponentVarfileName(&info), } if info.DryRun { @@ -167,17 +166,17 @@ func ExecuteTerraformShell(opts *ShellOptions, atmosConfig *schema.AtmosConfigur return nil } - return runShellSession(atmosConfig, &info, cfg, opts.WithSecrets) + return e.runShellSession(atmosConfig, &info, cfg, opts.WithSecrets) } // runShellSession performs the per-component setup (prepareShellExecution) and then runs // `terraform init`/`workspace` and launches the interactive shell (executeShellLifecycle). // It is split out of ExecuteTerraformShell so the post-ProcessStacks lifecycle can be unit-tested // without resolving real stacks or launching a shell. -func runShellSession(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, cfg *shellConfig, withSecrets bool) error { +func (e *Executor) runShellSession(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, cfg *shellConfig, withSecrets bool) error { defer perf.Track(atmosConfig, "exec.runShellSession")() - if err := prepareShellExecution(atmosConfig, info, cfg, withSecrets); err != nil { + if err := e.prepareShellExecution(atmosConfig, info, cfg, withSecrets); err != nil { return err } @@ -192,23 +191,15 @@ func runShellSession(atmosConfig *schema.AtmosConfiguration, info *schema.Config }() } - return executeShellLifecycle(atmosConfig, info, cfg) + return e.executeShellLifecycle(atmosConfig, info, cfg) } -// Seams for testing the shell lifecycle without launching real subprocesses or an interactive -// shell. They default to the real implementations (mirrors the execTerraformFn pattern). -var ( - shellInitFn = executeTerraformInitCommand - shellWorkspaceFn = runWorkspaceSetup - shellExecFn = execTerraformShellCommand -) - // executeShellLifecycle runs `terraform init` and selects/creates the workspace before launching // the interactive shell, so the user lands in an initialized component and the correct workspace // (not `default`). This matches the documented behavior and the pre-v1.202.0 flow where shell ran // through the shared ExecuteTerraform pipeline. The before.terraform.init provisioners already ran // in ExecuteTerraformShell, so init uses the provisioner-free path to avoid running them twice. -func executeShellLifecycle(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, cfg *shellConfig) error { +func (e *Executor) executeShellLifecycle(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, cfg *shellConfig) error { defer perf.Track(atmosConfig, "exec.executeShellLifecycle")() if shouldRunTerraformInit(atmosConfig, info) { @@ -217,18 +208,18 @@ func executeShellLifecycle(atmosConfig *schema.AtmosConfiguration, info *schema. if _, isWorkdir := info.ComponentSection[provWorkdir.WorkdirPathKey].(string); !isWorkdir { cleanTerraformWorkspace(*atmosConfig, cfg.componentPath) } - if err := shellInitFn(atmosConfig, info, cfg.componentPath, cfg.varFile); err != nil { + if err := e.shellInitFn(atmosConfig, info, cfg.componentPath, cfg.varFile); err != nil { return err } } // Select (or create) the workspace. No-op for the http backend or when TF_WORKSPACE is set // (see shouldSkipWorkspaceSetup); resolves to `default` when workspaces_enabled: false. - if err := shellWorkspaceFn(atmosConfig, info, cfg.componentPath); err != nil { + if err := e.shellWorkspaceFn(atmosConfig, info, cfg.componentPath); err != nil { return err } - return shellExecFn(atmosConfig, info.ComponentFromArg, info.Stack, + return e.shellExecFn(atmosConfig, info.ComponentFromArg, info.Stack, info.ComponentEnvList, cfg.varFile, cfg.workingDir, info.TerraformWorkspace, cfg.componentPath) } @@ -237,7 +228,7 @@ func executeShellLifecycle(atmosConfig *schema.AtmosConfiguration, info *schema. // binary (including the toolchain), writing the disk-safe varfile, generating backend and // provider-override config files, and assembling the component environment. It registers // info.RCCleanup so the temporary Terraform CLI config survives init, workspace, and the shell. -func prepareShellExecution(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, cfg *shellConfig, withSecrets bool) error { +func (e *Executor) prepareShellExecution(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, cfg *shellConfig, withSecrets bool) error { defer perf.Track(atmosConfig, "exec.prepareShellExecution")() // Shell setup also writes a Terraform varfile and can assemble TF_VAR_* values. @@ -258,9 +249,9 @@ func prepareShellExecution(atmosConfig *schema.AtmosConfiguration, info *schema. // Keep resolved secrets out of the on-disk varfile. With --with-secrets, export them // into the interactive shell as TF_VAR_* env vars; otherwise they are not available // (terraform commands in the shell that need them will prompt or fail). - computeTerraformSecretVarKeys(info) + ComputeTerraformSecretVarKeys(info) - varFilePath := constructTerraformComponentVarfilePath(atmosConfig, info) + varFilePath := ConstructTerraformComponentVarfilePath(atmosConfig, info) if err := u.WriteToFileAsJSON(varFilePath, diskSafeVars(info), filePermissions); err != nil { return err } @@ -272,7 +263,7 @@ func prepareShellExecution(atmosConfig *schema.AtmosConfiguration, info *schema. } // Generate backend + provider-override files so `terraform init` configures the backend. - if err := generateConfigFiles(atmosConfig, info, cfg.workingDir); err != nil { + if err := e.GenerateConfigFiles(atmosConfig, info, cfg.workingDir); err != nil { return err } diff --git a/internal/exec/terraform_shell_test.go b/pkg/component/terraform/shell_test.go similarity index 86% rename from internal/exec/terraform_shell_test.go rename to pkg/component/terraform/shell_test.go index 707d2c3d338..7ac5a5b33f3 100644 --- a/internal/exec/terraform_shell_test.go +++ b/pkg/component/terraform/shell_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "bytes" @@ -170,42 +170,10 @@ func TestResolveWorkdirPath_NilSection(t *testing.T) { assert.Equal(t, "/components/terraform/vpc", result) } -func TestShellOptionsForUI(t *testing.T) { - tests := []struct { - name string - component string - stack string - }{ - { - name: "typical values", - component: "vpc", - stack: "dev-us-west-2", - }, - { - name: "empty values", - component: "", - stack: "", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - opts := shellOptionsForUI(tt.component, tt.stack) - assert.Equal(t, tt.component, opts.Component) - assert.Equal(t, tt.stack, opts.Stack) - assert.True(t, opts.ProcessTemplates, "UI path must enable template processing") - assert.True(t, opts.ProcessFunctions, "UI path must enable function processing") - assert.False(t, opts.DryRun, "UI path does not support dry-run") - assert.Empty(t, opts.Identity, "UI path does not support identity selection") - assert.Empty(t, opts.Skip, "UI path does not support skip") - }) - } -} - func TestShellInfoFromOptions_AllFieldsMapped(t *testing.T) { // Ensure all relevant fields from ShellOptions are properly mapped to ConfigAndStacksInfo. // Note: ProcessingOptions (ProcessTemplates, ProcessFunctions, Skip) are NOT mapped here; - // they are passed directly to ProcessStacks in ExecuteTerraformShell. + // they are passed directly to ProcessStacks in testExecutor(t).ExecuteTerraformShell. opts := &ShellOptions{ Component: "my-component", Stack: "my-stack", @@ -245,15 +213,6 @@ func TestResolveWorkdirPath_EmptyMap(t *testing.T) { assert.Equal(t, "/original/path", result) } -func TestShellOptionsForUI_DefaultProcessingOptions(t *testing.T) { - // Verify that UI shell options have correct default processing options. - opts := shellOptionsForUI("vpc", "dev") - - assert.True(t, opts.ProcessTemplates) - assert.True(t, opts.ProcessFunctions) - assert.Empty(t, opts.Skip) -} - func TestPrintShellDryRunInfo(t *testing.T) { tests := []struct { name string @@ -352,7 +311,7 @@ func TestApplyShellSecretEnv(t *testing.T) { "region": "us-east-1-shellsecret", }, } - computeTerraformSecretVarKeys(info) + ComputeTerraformSecretVarKeys(info) require.True(t, info.TerraformSecretVarKeys["token"], "secret var should be flagged") require.False(t, info.TerraformSecretVarKeys["region"], "non-secret var should not be flagged") return info @@ -442,20 +401,20 @@ func swapShellLifecycleSeams(t *testing.T) *[]string { t.Helper() calls := &[]string{} - origInit, origWs, origExec := shellInitFn, shellWorkspaceFn, shellExecFn + origInit, origWs, origExec := testExecutor(t).shellInitFn, testExecutor(t).shellWorkspaceFn, testExecutor(t).shellExecFn t.Cleanup(func() { - shellInitFn, shellWorkspaceFn, shellExecFn = origInit, origWs, origExec + testExecutor(t).shellInitFn, testExecutor(t).shellWorkspaceFn, testExecutor(t).shellExecFn = origInit, origWs, origExec }) - shellInitFn = func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _, _ string, _ ...ShellCommandOption) error { + testExecutor(t).shellInitFn = func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _, _ string, _ ...ShellCommandOption) error { *calls = append(*calls, "init") return nil } - shellWorkspaceFn = func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _ string, _ ...ShellCommandOption) error { + testExecutor(t).shellWorkspaceFn = func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _ string, _ ...ShellCommandOption) error { *calls = append(*calls, "workspace") return nil } - shellExecFn = func(_ *schema.AtmosConfiguration, _, _ string, _ []string, _, _, _, _ string) error { + testExecutor(t).shellExecFn = func(_ *schema.AtmosConfiguration, _, _ string, _ []string, _, _, _, _ string) error { *calls = append(*calls, "shell") return nil } @@ -472,7 +431,7 @@ func TestExecuteShellLifecycle_RunsInitThenWorkspaceThenShell(t *testing.T) { info := shellInfoFromOptions(&ShellOptions{Component: "vpc", Stack: "dev"}) cfg := &shellConfig{componentPath: t.TempDir(), workingDir: t.TempDir(), varFile: "dev-vpc.tfvars.json"} - require.NoError(t, executeShellLifecycle(atmosConfig, &info, cfg)) + require.NoError(t, testExecutor(t).executeShellLifecycle(atmosConfig, &info, cfg)) assert.Equal(t, []string{"init", "workspace", "shell"}, *calls) } @@ -485,14 +444,14 @@ func TestExecuteShellLifecycle_SkipInit_KeepsWorkspace(t *testing.T) { info := shellInfoFromOptions(&ShellOptions{Component: "vpc", Stack: "dev", SkipInit: true}) cfg := &shellConfig{componentPath: t.TempDir(), workingDir: t.TempDir(), varFile: "dev-vpc.tfvars.json"} - require.NoError(t, executeShellLifecycle(atmosConfig, &info, cfg)) + require.NoError(t, testExecutor(t).executeShellLifecycle(atmosConfig, &info, cfg)) assert.Equal(t, []string{"workspace", "shell"}, *calls, "--skip-init must skip init but still select the workspace and launch the shell") } // newShellPrepInfo builds a minimal ConfigAndStacksInfo whose component working dir resolves to -// tmpDir (via the workdir provisioner key), so prepareShellExecution writes its varfile there and -// generateConfigFiles no-ops (no backend/providers configured). +// tmpDir (via the workdir provisioner key), so testExecutor(t).prepareShellExecution writes its varfile there and +// testExecutor(t).GenerateConfigFiles no-ops (no backend/providers configured). func newShellPrepInfo(tmpDir string) *schema.ConfigAndStacksInfo { return &schema.ConfigAndStacksInfo{ ComponentFromArg: "vpc", @@ -515,7 +474,7 @@ func TestPrepareShellExecution_HappyPath(t *testing.T) { info := newShellPrepInfo(tmpDir) cfg := &shellConfig{componentPath: tmpDir, workingDir: tmpDir, varFile: "dev-vpc.terraform.tfvars.json"} - err := prepareShellExecution(atmosConfig, info, cfg, false) + err := testExecutor(t).prepareShellExecution(atmosConfig, info, cfg, false) require.NoError(t, err) // RC cleanup is registered so the temporary Terraform CLI config survives init/workspace/shell; @@ -528,7 +487,7 @@ func TestPrepareShellExecution_HappyPath(t *testing.T) { assert.NotEmpty(t, info.Command, "terraform/tofu command must be resolved") // The disk-safe varfile was written into the resolved working dir. - varfilePath := constructTerraformComponentVarfilePath(atmosConfig, info) + varfilePath := ConstructTerraformComponentVarfilePath(atmosConfig, info) assert.FileExists(t, varfilePath, "disk-safe varfile must be written") // Standard Atmos env vars were assembled for the subprocess/shell. @@ -548,7 +507,7 @@ func TestPrepareShellExecution_WithSecrets(t *testing.T) { info.ComponentVarsSection = map[string]any{"token": secret, "region": "us-east-1-prepareshell"} cfg := &shellConfig{componentPath: tmpDir, workingDir: tmpDir, varFile: "dev-vpc.terraform.tfvars.json"} - require.NoError(t, prepareShellExecution(atmosConfig, info, cfg, true)) + require.NoError(t, testExecutor(t).prepareShellExecution(atmosConfig, info, cfg, true)) if info.RCCleanup != nil { t.Cleanup(func() { _ = info.RCCleanup() }) } @@ -563,7 +522,7 @@ func TestPrepareShellExecution_WithSecrets(t *testing.T) { assert.True(t, exported, "with --with-secrets the secret must be exported as TF_VAR_token") // The secret must NOT be written to the on-disk varfile. - varfilePath := constructTerraformComponentVarfilePath(atmosConfig, info) + varfilePath := ConstructTerraformComponentVarfilePath(atmosConfig, info) data, err := os.ReadFile(varfilePath) require.NoError(t, err) assert.NotContains(t, string(data), secret, "secret must never be written to the on-disk varfile") @@ -584,12 +543,12 @@ func TestPrepareShellExecution_VarfileWriteError(t *testing.T) { info := newShellPrepInfo(badDir) cfg := &shellConfig{componentPath: badDir, workingDir: badDir, varFile: "dev-vpc.terraform.tfvars.json"} - err := prepareShellExecution(atmosConfig, info, cfg, false) + err := testExecutor(t).prepareShellExecution(atmosConfig, info, cfg, false) require.Error(t, err, "varfile write into a non-directory path must fail") } // TestExecuteShellLifecycle_WorkdirComponent_SkipsWorkspaceClean covers the workdir branch: when -// the component is provisioned into a workdir, executeShellLifecycle skips the .terraform/environment +// the component is provisioned into a workdir, testExecutor(t).executeShellLifecycle skips the .terraform/environment // cleanup but still runs init -> workspace -> shell in order. func TestExecuteShellLifecycle_WorkdirComponent_SkipsWorkspaceClean(t *testing.T) { calls := swapShellLifecycleSeams(t) @@ -600,7 +559,7 @@ func TestExecuteShellLifecycle_WorkdirComponent_SkipsWorkspaceClean(t *testing.T info.ComponentSection = map[string]any{provWorkdir.WorkdirPathKey: tmpDir} cfg := &shellConfig{componentPath: tmpDir, workingDir: tmpDir, varFile: "dev-vpc.tfvars.json"} - require.NoError(t, executeShellLifecycle(atmosConfig, &info, cfg)) + require.NoError(t, testExecutor(t).executeShellLifecycle(atmosConfig, &info, cfg)) assert.Equal(t, []string{"init", "workspace", "shell"}, *calls, "workdir components still run init, workspace, and shell in order") } @@ -621,12 +580,12 @@ func TestExecuteTerraformInitCommand_DryRun(t *testing.T) { ComponentSection: map[string]any{}, } - err := executeTerraformInitCommand(atmosConfig, info, tmpDir, "dev-vpc.terraform.tfvars.json") + err := testExecutor(t).executeTerraformInitCommand(atmosConfig, info, tmpDir, "dev-vpc.terraform.tfvars.json") require.NoError(t, err, "dry-run init must not launch a subprocess and must not error") } // TestRunShellSession_PreparesThenRunsLifecycle covers the post-ProcessStacks glue of the shell -// entry point: prepareShellExecution runs, the RC cleanup is registered, and the lifecycle +// entry point: testExecutor(t).prepareShellExecution runs, the RC cleanup is registered, and the lifecycle // (init -> workspace -> shell) runs in order. The lifecycle subprocess/shell steps are stubbed. func TestRunShellSession_PreparesThenRunsLifecycle(t *testing.T) { calls := swapShellLifecycleSeams(t) @@ -636,16 +595,16 @@ func TestRunShellSession_PreparesThenRunsLifecycle(t *testing.T) { info := newShellPrepInfo(tmpDir) cfg := &shellConfig{componentPath: tmpDir, workingDir: tmpDir, varFile: "dev-vpc.terraform.tfvars.json"} - require.NoError(t, runShellSession(atmosConfig, info, cfg, false)) + require.NoError(t, testExecutor(t).runShellSession(atmosConfig, info, cfg, false)) - // prepareShellExecution ran (binary resolved, env assembled). - assert.NotEmpty(t, info.Command, "prepareShellExecution must resolve the terraform binary") + // testExecutor(t).prepareShellExecution ran (binary resolved, env assembled). + assert.NotEmpty(t, info.Command, "testExecutor(t).prepareShellExecution must resolve the terraform binary") assert.Contains(t, info.ComponentEnvList, "TF_IN_AUTOMATION=true") // The lifecycle ran in order. assert.Equal(t, []string{"init", "workspace", "shell"}, *calls) } -// TestRunShellSession_PrepareErrorSkipsLifecycle verifies that when prepareShellExecution fails, +// TestRunShellSession_PrepareErrorSkipsLifecycle verifies that when testExecutor(t).prepareShellExecution fails, // the lifecycle (init/workspace/shell) never runs. func TestRunShellSession_PrepareErrorSkipsLifecycle(t *testing.T) { calls := swapShellLifecycleSeams(t) @@ -660,7 +619,7 @@ func TestRunShellSession_PrepareErrorSkipsLifecycle(t *testing.T) { info := newShellPrepInfo(badDir) cfg := &shellConfig{componentPath: badDir, workingDir: badDir, varFile: "dev-vpc.terraform.tfvars.json"} - err := runShellSession(atmosConfig, info, cfg, false) + err := testExecutor(t).runShellSession(atmosConfig, info, cfg, false) require.Error(t, err, "prepare failure must propagate") assert.Empty(t, *calls, "the init/workspace/shell lifecycle must not run when prepare fails") } @@ -670,7 +629,7 @@ func TestRunShellSession_PrepareErrorSkipsLifecycle(t *testing.T) { func TestExecuteShellLifecycle_WorkspaceErrorStopsBeforeShell(t *testing.T) { calls := swapShellLifecycleSeams(t) wantErr := errors.New("workspace failed") - shellWorkspaceFn = func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _ string, _ ...ShellCommandOption) error { + testExecutor(t).shellWorkspaceFn = func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _ string, _ ...ShellCommandOption) error { *calls = append(*calls, "workspace") return wantErr } @@ -679,13 +638,13 @@ func TestExecuteShellLifecycle_WorkspaceErrorStopsBeforeShell(t *testing.T) { info := shellInfoFromOptions(&ShellOptions{Component: "vpc", Stack: "dev"}) cfg := &shellConfig{componentPath: t.TempDir(), workingDir: t.TempDir(), varFile: "dev-vpc.tfvars.json"} - err := executeShellLifecycle(atmosConfig, &info, cfg) + err := testExecutor(t).executeShellLifecycle(atmosConfig, &info, cfg) require.ErrorIs(t, err, wantErr) assert.Equal(t, []string{"init", "workspace"}, *calls, "shell must not launch after workspace setup fails") } // TestExecuteTerraformInitPhase_DryRun covers the standard init pre-step (prepareInitExecution + -// executeTerraformInitCommand) in dry-run mode: it returns the resolved path without launching a +// testExecutor(t).executeTerraformInitCommand) in dry-run mode: it returns the resolved path without launching a // real subprocess. func TestExecuteTerraformInitPhase_DryRun(t *testing.T) { tmpDir := t.TempDir() @@ -700,13 +659,13 @@ func TestExecuteTerraformInitPhase_DryRun(t *testing.T) { ComponentSection: map[string]any{provWorkdir.WorkdirPathKey: tmpDir}, } - newPath, err := executeTerraformInitPhase(atmosConfig, info, tmpDir, "dev-vpc.terraform.tfvars.json") + newPath, err := testExecutor(t).executeTerraformInitPhase(atmosConfig, info, tmpDir, "dev-vpc.terraform.tfvars.json") require.NoError(t, err) assert.Equal(t, tmpDir, newPath, "workdir components return the workdir path") } // TestExecuteTerraformInitPhase_SubprocessError covers the init error path: when the terraform -// binary cannot be executed, both executeTerraformInitPhase and executeTerraformInitCommand +// binary cannot be executed, both testExecutor(t).executeTerraformInitPhase and testExecutor(t).executeTerraformInitCommand // propagate the failure (no dry-run, so the subprocess actually runs and fails fast). func TestExecuteTerraformInitPhase_SubprocessError(t *testing.T) { tmpDir := t.TempDir() @@ -722,7 +681,7 @@ func TestExecuteTerraformInitPhase_SubprocessError(t *testing.T) { ComponentSection: map[string]any{provWorkdir.WorkdirPathKey: tmpDir}, } - _, err := executeTerraformInitPhase(atmosConfig, info, tmpDir, "dev-vpc.terraform.tfvars.json") + _, err := testExecutor(t).executeTerraformInitPhase(atmosConfig, info, tmpDir, "dev-vpc.terraform.tfvars.json") require.Error(t, err, "init must fail when the terraform binary cannot be executed") } @@ -731,7 +690,7 @@ func TestExecuteTerraformInitPhase_SubprocessError(t *testing.T) { func TestExecuteShellLifecycle_InitErrorStopsBeforeShell(t *testing.T) { calls := swapShellLifecycleSeams(t) wantErr := errTestInitFailed - shellInitFn = func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _, _ string, _ ...ShellCommandOption) error { + testExecutor(t).shellInitFn = func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _, _ string, _ ...ShellCommandOption) error { *calls = append(*calls, "init") return wantErr } @@ -740,7 +699,7 @@ func TestExecuteShellLifecycle_InitErrorStopsBeforeShell(t *testing.T) { info := shellInfoFromOptions(&ShellOptions{Component: "vpc", Stack: "dev"}) cfg := &shellConfig{componentPath: t.TempDir(), workingDir: t.TempDir(), varFile: "dev-vpc.tfvars.json"} - err := executeShellLifecycle(atmosConfig, &info, cfg) + err := testExecutor(t).executeShellLifecycle(atmosConfig, &info, cfg) require.ErrorIs(t, err, wantErr) assert.Equal(t, []string{"init"}, *calls, "workspace and shell must not run after init fails") } @@ -774,7 +733,7 @@ func TestPrepareShellExecution_InjectsAuthCredentials(t *testing.T) { } cfg := &shellConfig{componentPath: tmpDir, workingDir: tmpDir, varFile: "dev-vpc.terraform.tfvars.json"} - require.NoError(t, prepareShellExecution(atmosConfig, info, cfg, false)) + require.NoError(t, testExecutor(t).prepareShellExecution(atmosConfig, info, cfg, false)) if info.RCCleanup != nil { t.Cleanup(func() { _ = info.RCCleanup() }) } @@ -798,7 +757,7 @@ func TestPrepareShellExecution_AuthErrorStopsSetup(t *testing.T) { info.ComponentAuthSection = map[string]any{"providers": "not-a-map"} cfg := &shellConfig{componentPath: tmpDir, workingDir: tmpDir, varFile: "dev-vpc.terraform.tfvars.json"} - err := prepareShellExecution(atmosConfig, info, cfg, false) + err := testExecutor(t).prepareShellExecution(atmosConfig, info, cfg, false) require.ErrorIs(t, err, errUtils.ErrInvalidAuthConfig) assert.Empty(t, info.ComponentEnvList, "env must not be assembled after an auth failure") } diff --git a/internal/exec/terraform_state_getter.go b/pkg/component/terraform/state_getter.go similarity index 70% rename from internal/exec/terraform_state_getter.go rename to pkg/component/terraform/state_getter.go index 9548fc1076a..7b9d5a2bc2e 100644 --- a/internal/exec/terraform_state_getter.go +++ b/pkg/component/terraform/state_getter.go @@ -1,6 +1,6 @@ -package exec +package terraform -//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=mock_$GOFILE -package=$GOPACKAGE +//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=../../../internal/exec/mock_terraform_state_getter_test.go -package=exec import ( "github.com/cloudposse/atmos/pkg/perf" @@ -26,7 +26,7 @@ type TerraformStateGetter interface { } // defaultStateGetter implements TerraformStateGetter using the real GetTerraformState function. -type defaultStateGetter struct{} +type defaultStateGetter struct{ executor *Executor } //nolint:revive // argument-limit: matches interface signature func (d *defaultStateGetter) GetState( @@ -42,8 +42,11 @@ func (d *defaultStateGetter) GetState( ) (any, error) { defer perf.Track(atmosConfig, "exec.defaultStateGetter.GetState")() - return GetTerraformState(atmosConfig, yamlFunc, stack, component, output, skipCache, authContext, authManager, options...) + return d.executor.GetTerraformState(atmosConfig, yamlFunc, stack, component, output, skipCache, authContext, authManager, options...) } -// Global variable that can be overridden in tests. -var stateGetter TerraformStateGetter = &defaultStateGetter{} +// StateGetter returns a reference getter bound to this executor. +func (e *Executor) StateGetter() TerraformStateGetter { + defer perf.Track(nil, "terraform.Executor.StateGetter")() + return &defaultStateGetter{executor: e} +} diff --git a/internal/exec/terraform_state_output_coverage_test.go b/pkg/component/terraform/state_output_coverage_test.go similarity index 91% rename from internal/exec/terraform_state_output_coverage_test.go rename to pkg/component/terraform/state_output_coverage_test.go index 8eb06af2eda..52e93572263 100644 --- a/internal/exec/terraform_state_output_coverage_test.go +++ b/pkg/component/terraform/state_output_coverage_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "testing" @@ -20,7 +20,7 @@ func TestGetTerraformState_ErrorPaths(t *testing.T) { atmosConfig := &schema.AtmosConfiguration{} // Try to get state for non-existent component - result, err := GetTerraformState( + result, err := testExecutor(t).GetTerraformState( atmosConfig, "!terraform.state", "non-existent-stack", @@ -61,7 +61,7 @@ func TestGetTerraformState_ErrorPaths(t *testing.T) { atmosConfig := &schema.AtmosConfiguration{} // Try to get state for non-existent component with authManager - result, err := GetTerraformState( + result, err := testExecutor(t).GetTerraformState( atmosConfig, "!terraform.state", "non-existent-stack", @@ -80,7 +80,7 @@ func TestGetTerraformState_ErrorPaths(t *testing.T) { atmosConfig := &schema.AtmosConfiguration{} // Pass invalid authManager type (string instead of AuthManager) - result, err := GetTerraformState( + result, err := testExecutor(t).GetTerraformState( atmosConfig, "!terraform.state", "test-stack", @@ -194,7 +194,7 @@ func TestGetTerraformState_CacheHit(t *testing.T) { defer terraformStateCache.Delete(stackSlug) // Cleanup // Get cached output (skipCache = false) - result, err := GetTerraformState( + result, err := testExecutor(t).GetTerraformState( atmosConfig, "!terraform.state", "test-stack", @@ -213,13 +213,13 @@ func TestGetTerraformState_CachesNotProvisionedState(t *testing.T) { ResetStateCache() t.Cleanup(ResetStateCache) - setupTerraformYamlFunctionSandbox(t, "../../tests/fixtures/scenarios/terraform-state-jit-workdir") - t.Chdir("../../tests/fixtures/scenarios/terraform-state-jit-workdir") + setupTerraformYamlFunctionSandbox(t, "../../../tests/fixtures/scenarios/terraform-state-jit-workdir") + t.Chdir("../../../tests/fixtures/scenarios/terraform-state-jit-workdir") atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true) require.NoError(t, err) - result, err := GetTerraformState( + result, err := testExecutor(t).GetTerraformState( &atmosConfig, "!terraform.state", "test", @@ -238,7 +238,7 @@ func TestGetTerraformState_CachesNotProvisionedState(t *testing.T) { _, isNotProvisioned := cached.(terraformStateNotProvisionedCacheEntry) require.True(t, isNotProvisioned) - result, err = GetTerraformState( + result, err = testExecutor(t).GetTerraformState( &atmosConfig, "!terraform.state", "test", @@ -261,15 +261,15 @@ func TestGetTerraformState_InvalidateClearsNotProvisionedSentinel(t *testing.T) ResetStateCache() t.Cleanup(ResetStateCache) - setupTerraformYamlFunctionSandbox(t, "../../tests/fixtures/scenarios/terraform-state-jit-workdir") - t.Chdir("../../tests/fixtures/scenarios/terraform-state-jit-workdir") + setupTerraformYamlFunctionSandbox(t, "../../../tests/fixtures/scenarios/terraform-state-jit-workdir") + t.Chdir("../../../tests/fixtures/scenarios/terraform-state-jit-workdir") atmosConfig, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, true) require.NoError(t, err) // Seed the cache with the not-provisioned sentinel, mirroring // TestGetTerraformState_CachesNotProvisionedState. - result, err := GetTerraformState( + result, err := testExecutor(t).GetTerraformState( &atmosConfig, "!terraform.state", "test", @@ -301,7 +301,7 @@ func TestGetTerraformState_InvalidateClearsNotProvisionedSentinel(t *testing.T) // A subsequent read finds no cache entry, re-consults the backend (still not // provisioned in this fixture), and re-populates the sentinel — proving the // answer came from the backend rather than a frozen cache entry. - result, err = GetTerraformState( + result, err = testExecutor(t).GetTerraformState( &atmosConfig, "!terraform.state", "test", @@ -333,7 +333,7 @@ func TestGetTerraformState_SkipCache(t *testing.T) { defer terraformStateCache.Delete(stackSlug) // Cleanup // Try to get output with skipCache=true (should not use cache) - result, err := GetTerraformState( + result, err := testExecutor(t).GetTerraformState( atmosConfig, "!terraform.state", "test-stack", diff --git a/internal/exec/terraform_state_utils.go b/pkg/component/terraform/state_utils.go similarity index 87% rename from internal/exec/terraform_state_utils.go rename to pkg/component/terraform/state_utils.go index df05df37cdc..a0121c7c1e1 100644 --- a/internal/exec/terraform_state_utils.go +++ b/pkg/component/terraform/state_utils.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "fmt" @@ -9,6 +9,7 @@ import ( tb "github.com/cloudposse/atmos/internal/terraform_backend" "github.com/cloudposse/atmos/pkg/auth" authdeferred "github.com/cloudposse/atmos/pkg/auth/deferred" + componentpkg "github.com/cloudposse/atmos/pkg/component" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/deferred" log "github.com/cloudposse/atmos/pkg/logger" @@ -28,11 +29,11 @@ type terraformStateStaticCacheEntry struct { outputs map[string]any } -type terraformStateLookup struct { - yamlFunc string - stack string - component string - output string +type TerraformStateLookup struct { + YamlFunc string + Stack string + Component string + Output string kinds refcache.Kinds } @@ -73,7 +74,9 @@ func ResetStateCache() { // - authManager: Optional auth manager for nested operations that need authentication // // Returns the output value or nil if the component is not provisioned. -func GetTerraformState( +// +//nolint:gocognit,revive,cyclop,funlen // Preserve the existing auth-scoped cache and backend lookup branches during extraction. +func (e *Executor) GetTerraformState( atmosConfig *schema.AtmosConfiguration, yamlFunc string, stack string, @@ -92,7 +95,7 @@ func GetTerraformState( cacheKey.Auth = refcache.AuthScope(authContext, authManager, authdeferred.AuthDisabled(atmosConfig.AuthManager)) cacheKey.Inspection = maskOnly cacheKey.Skip = strings.Join(terraform.ResolveLookupOptions(options).Skip, "\x00") - lookup := &terraformStateLookup{yamlFunc: yamlFunc, stack: stack, component: component, output: output, kinds: kinds} + lookup := &TerraformStateLookup{YamlFunc: yamlFunc, Stack: stack, Component: component, Output: output, kinds: kinds} // Keep inspection placeholders and resolved execution values out of each other's lookups. var cachedBackend any @@ -148,13 +151,13 @@ func GetTerraformState( var valueCache *deferred.ValueCache if authdeferred.IsDeferred(atmosConfig.AuthManager) { var err error - resolvedAuthMgr, valueCache, err = deferredTargetAuthAndCache(atmosConfig, component, stack, parentAuthMgr) + resolvedAuthMgr, valueCache, err = e.services.DeferredTargetAuthAndCache(atmosConfig, component, stack, parentAuthMgr) if err != nil { return nil, err } } else if !authDisabled { var err error - resolvedAuthMgr, err = resolveAuthManagerForNestedComponent(atmosConfig, component, stack, parentAuthMgr) + resolvedAuthMgr, err = e.ResolveAuthManagerForNestedComponent(atmosConfig, component, stack, parentAuthMgr) if err != nil { log.Debug( "Auth does not exist for nested component, using parent AuthManager", @@ -178,9 +181,9 @@ func GetTerraformState( // Derive the effective AuthContext for backend reads. // If we resolved a component-specific AuthManager, use its AuthContext instead of the // passed-in one (which may be nil when the parent didn't propagate auth). - resolvedAuthContext := resolvedTargetAuthContext(atmosConfig, resolvedAuthMgr, authContext, authDisabled) + resolvedAuthContext := e.services.ResolvedTargetAuthContext(atmosConfig, resolvedAuthMgr, authContext, authDisabled) - componentSections, err := ExecuteDescribeComponent(&ExecuteDescribeComponentParams{ + componentSections, err := e.services.ExecuteDescribeComponent(&componentpkg.ExecuteDescribeComponentParams{ ResolveSecrets: !maskOnly, AtmosConfig: atmosConfig, Component: component, @@ -238,14 +241,14 @@ func GetTerraformState( // Get the output. result, err := tb.GetTerraformBackendVariable(atmosConfig, backend, output) if err != nil { - er := fmt.Errorf("%w %s for component `%s` in stack `%s`\nin YAML function: `%s`\n%v", errUtils.ErrEvaluateTerraformBackendVariable, output, component, stack, yamlFunc, err) + er := fmt.Errorf("%w %s for component `%s` in stack `%s`\nin YAML function: `%s`\n%v", errUtils.ErrEvaluateTerraformBackendVariable, output, component, stack, yamlFunc, err) //nolint:errorlint // Preserve the existing public error chain; backend details remain message-only. return nil, er } return result, nil } -func cachedTerraformStateOutput(ac *schema.AtmosConfiguration, lookup *terraformStateLookup, cache *deferred.ValueCache) (any, bool, error) { +func cachedTerraformStateOutput(ac *schema.AtmosConfiguration, lookup *TerraformStateLookup, cache *deferred.ValueCache) (any, bool, error) { if cached, ok := cache.Load(lookup.kinds.StaticState); ok { result, err := staticTerraformStateOutput(ac, lookup, cached.(map[string]any)) return result, true, err @@ -254,22 +257,22 @@ func cachedTerraformStateOutput(ac *schema.AtmosConfiguration, lookup *terraform if !ok { return nil, false, nil } - result, err := tb.GetTerraformBackendVariable(ac, cached.(map[string]any), lookup.output) + result, err := tb.GetTerraformBackendVariable(ac, cached.(map[string]any), lookup.Output) if err != nil { - return nil, true, fmt.Errorf("%w %s for component `%s` in stack `%s`\nin YAML function: `%s`\n%w", errUtils.ErrEvaluateTerraformBackendVariable, lookup.output, lookup.component, lookup.stack, lookup.yamlFunc, err) + return nil, true, fmt.Errorf("%w %s for component `%s` in stack `%s`\nin YAML function: `%s`\n%w", errUtils.ErrEvaluateTerraformBackendVariable, lookup.Output, lookup.Component, lookup.Stack, lookup.YamlFunc, err) } return result, true, nil } // Static outputs distinguish absent keys from legitimate null values on both cold // and warm reads, preserving Terraform/YQ fallback behavior. -func staticTerraformStateOutput(ac *schema.AtmosConfiguration, lookup *terraformStateLookup, outputs map[string]any) (any, error) { - result, exists, err := tfoutput.GetStaticRemoteStateOutput(ac, lookup.component, lookup.stack, outputs, lookup.output) +func staticTerraformStateOutput(ac *schema.AtmosConfiguration, lookup *TerraformStateLookup, outputs map[string]any) (any, error) { + result, exists, err := tfoutput.GetStaticRemoteStateOutput(ac, lookup.Component, lookup.Stack, outputs, lookup.Output) if err != nil { - return nil, fmt.Errorf("%w for component `%s` in stack `%s`\nin YAML function: `%s`\n%w", errUtils.ErrReadTerraformState, lookup.component, lookup.stack, lookup.yamlFunc, err) + return nil, fmt.Errorf("%w for component `%s` in stack `%s`\nin YAML function: `%s`\n%w", errUtils.ErrReadTerraformState, lookup.Component, lookup.Stack, lookup.YamlFunc, err) } if !exists { - return nil, fmt.Errorf("%w: output `%s` does not exist for component `%s` in stack `%s`\nin YAML function: `%s`", errUtils.ErrReadTerraformState, lookup.output, lookup.component, lookup.stack, lookup.yamlFunc) + return nil, fmt.Errorf("%w: output `%s` does not exist for component `%s` in stack `%s`\nin YAML function: `%s`", errUtils.ErrReadTerraformState, lookup.Output, lookup.Component, lookup.Stack, lookup.YamlFunc) } return result, nil } diff --git a/internal/exec/terraform_store_identity_isolation_test.go b/pkg/component/terraform/store_identity_isolation_test.go similarity index 94% rename from internal/exec/terraform_store_identity_isolation_test.go rename to pkg/component/terraform/store_identity_isolation_test.go index f65dd7d14ea..1d73571c995 100644 --- a/internal/exec/terraform_store_identity_isolation_test.go +++ b/pkg/component/terraform/store_identity_isolation_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" @@ -194,19 +194,19 @@ func (h *isolationHarness) assertComponentIdentityIntact() { assert.Equal(h.t, isolationDeployer, h.info.AuthContext.AWS.Profile) // terraform_state_utils derives the backend-read context through resolvedTargetAuthContext. - backendCtx := resolvedTargetAuthContext(h.atmosConfig, h.manager, h.info.AuthContext, false) + backendCtx := testExecutor(h.t).services.ResolvedTargetAuthContext(h.atmosConfig, h.manager, h.info.AuthContext, false) require.NotNil(h.t, backendCtx) require.NotNil(h.t, backendCtx.AWS) assert.Equal(h.t, isolationDeployer, backendCtx.AWS.Profile) // Identity auto-detection used by hooks and nested operations. detected := &schema.ConfigAndStacksInfo{Stack: isolationStack} - storeAutoDetectedIdentity(h.manager, detected) + testExecutor(h.t).services.StoreAutoDetectedIdentity(h.manager, detected) assert.Equal(h.t, isolationDeployer, detected.Identity) - assert.Equal(h.t, isolationDeployer, HookStoreDefaultIdentity(h.manager, &schema.ConfigAndStacksInfo{Stack: isolationStack})) + assert.Equal(h.t, isolationDeployer, testExecutor(h.t).HookStoreDefaultIdentity(h.manager, &schema.ConfigAndStacksInfo{Stack: isolationStack})) // Nested target inheritance: createComponentAuthManager inherits the last element of the parent chain. - nested, err := createComponentAuthManager(h.atmosConfig, map[string]any{}, "nested", isolationStack, h.manager) + nested, err := CreateComponentAuthManager(h.atmosConfig, map[string]any{}, "nested", isolationStack, h.manager) require.NoError(h.t, err) require.NotNil(h.t, nested) nestedChain := nested.GetChain() @@ -303,7 +303,7 @@ func TestStoreIdentityIsolation(t *testing.T) { t.Run(tt.name, func(t *testing.T) { h := newIsolationHarness(t, tt.identityFlag, tt.pins) - injectTerraformStoreAuthResolver(h.atmosConfig, h.info, h.manager) + testExecutor(t).injectTerraformStoreAuthResolver(h.atmosConfig, h.info, h.manager) assert.Equal(t, tt.wantInfoID, h.info.Identity) require.NotNil(t, h.info.RequestedIdentity) @@ -333,7 +333,7 @@ func TestStoreIdentityIsolation_AuthDisabled(t *testing.T) { t.Run("nil manager binds no resolver", func(t *testing.T) { h := newIsolationHarness(t, cfg.IdentityFlagDisabledValue, map[string]string{ssm: "automation", "secrets/inherit": ""}) - injectTerraformStoreAuthResolver(h.atmosConfig, h.info, nil) + testExecutor(t).injectTerraformStoreAuthResolver(h.atmosConfig, h.info, nil) for name, s := range h.stores { assert.Nil(t, s.resolver, "store %q must not receive a resolver", name) @@ -342,14 +342,14 @@ func TestStoreIdentityIsolation_AuthDisabled(t *testing.T) { } assert.Nil(t, h.atmosConfig.SecretsAuth) assert.Empty(t, resolveStoreDefaultIdentity(h.info.Identity, nil)) - assert.Empty(t, HookStoreDefaultIdentity(nil, h.info)) + assert.Empty(t, testExecutor(t).HookStoreDefaultIdentity(nil, h.info)) assert.Equal(t, cfg.IdentityFlagDisabledValue, h.info.Identity, "identity stays disabled") }) t.Run("disabled identity with a manager keeps identity-less stores on the SDK chain", func(t *testing.T) { h := newIsolationHarness(t, cfg.IdentityFlagDisabledValue, map[string]string{ssm: "automation", "secrets/inherit": ""}) - injectTerraformStoreAuthResolver(h.atmosConfig, h.info, h.manager) + testExecutor(t).injectTerraformStoreAuthResolver(h.atmosConfig, h.info, h.manager) assert.Empty(t, resolveStoreDefaultIdentity(cfg.IdentityFlagDisabledValue, h.manager)) for name, s := range h.stores { @@ -386,7 +386,7 @@ func (r *legacyResolver) ResolveAWSAuthContext(ctx context.Context, identityName func TestStoreIdentityIsolation_HarnessDetectsSharedManagerLeak(t *testing.T) { const ssm = "secrets/ssm" h := newIsolationHarness(t, "", map[string]string{ssm: "automation"}) - injectTerraformStoreAuthResolver(h.atmosConfig, h.info, h.manager) + testExecutor(t).injectTerraformStoreAuthResolver(h.atmosConfig, h.info, h.manager) // Swap in the legacy behavior while keeping the identity binding the real injection produced. s := h.stores[ssm] @@ -400,10 +400,10 @@ func TestStoreIdentityIsolation_HarnessDetectsSharedManagerLeak(t *testing.T) { assert.Equal(t, "automation", h.manager.GetStackInfo().AuthContext.AWS.Profile, "legacy resolver overwrites the shared auth context") detected := &schema.ConfigAndStacksInfo{Stack: isolationStack} - storeAutoDetectedIdentity(h.manager, detected) + testExecutor(h.t).services.StoreAutoDetectedIdentity(h.manager, detected) assert.Equal(t, "automation", detected.Identity) - nested, err := createComponentAuthManager(h.atmosConfig, map[string]any{}, "nested", isolationStack, h.manager) + nested, err := CreateComponentAuthManager(h.atmosConfig, map[string]any{}, "nested", isolationStack, h.manager) require.NoError(t, err) require.NotNil(t, nested) nestedChain := nested.GetChain() diff --git a/internal/exec/terraform_streaming_ui.go b/pkg/component/terraform/streaming_ui.go similarity index 90% rename from internal/exec/terraform_streaming_ui.go rename to pkg/component/terraform/streaming_ui.go index 0449015082b..50cd5d05096 100644 --- a/internal/exec/terraform_streaming_ui.go +++ b/pkg/component/terraform/streaming_ui.go @@ -1,4 +1,4 @@ -package exec +package terraform // terraform_streaming_ui.go threads the streaming TUI (pkg/terraform/ui) into the // terraform execution pipeline (terraform_execute_helpers.go, @@ -12,6 +12,7 @@ import ( "sync" errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/component" log "github.com/cloudposse/atmos/pkg/logger" "github.com/cloudposse/atmos/pkg/perf" "github.com/cloudposse/atmos/pkg/schema" @@ -49,7 +50,7 @@ type streamingExecRequest struct { subCommand string workspace string redirectStdErr string - shellOpts []ShellCommandOption + shellOpts []component.ExecutionOption } // executeStreamingOrShell runs a terraform phase through the streaming TUI when enabled @@ -69,9 +70,9 @@ type streamingExecRequest struct { // workspace select/new: those gate on the "init" phase (workspace setup is part of the // init lifecycle and has no dedicated ShouldUseStreamingUI case) while still labelling // the TUI dispatch "workspace". -func executeStreamingOrShell(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, req *streamingExecRequest) error { +func (e *Executor) executeStreamingOrShell(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, req *streamingExecRequest) error { runShell := func() error { - return ExecuteShellCommand( + return e.services.ExecuteShellCommand( *atmosConfig, info.Command, req.args, @@ -99,8 +100,8 @@ func executeStreamingOrShell(atmosConfig *schema.AtmosConfiguration, info *schem } execOpts := buildStreamingExecuteOptions(atmosConfig, info, req) - ctx := shellCommandContext(req.shellOpts...) - err := dispatchStreamingExecutor(ctx, req.subCommand, info.DryRun, execOpts) + ctx := component.ContextFromOptions(req.shellOpts...) + err := e.dispatchStreamingExecutor(ctx, req.subCommand, info.DryRun, execOpts) if errors.Is(err, errUtils.ErrStreamingNotSupported) { log.Debug("Streaming UI not supported, falling back to regular execution") return runShell() @@ -137,13 +138,13 @@ func buildStreamingExecuteOptions(atmosConfig *schema.AtmosConfiguration, info * // (withExecMetadataOutputCapture) via io.MultiWriter when both are set, so neither consumer's // capture goes dark just because a phase happened to run through the TUI instead of the plain // shell path. Returns (nil, nil) when shellOpts requests no capture at all. -func streamingCaptureWriters(shellOpts []ShellCommandOption) (stdout, stderr io.Writer) { - var cfg shellCommandConfig +func streamingCaptureWriters(shellOpts []component.ExecutionOption) (stdout, stderr io.Writer) { + var cfg component.ExecutionOptions for _, opt := range shellOpts { opt(&cfg) } - return combineCaptureWriters(cfg.stdoutCapture, cfg.execMetadataStdoutCapture), - combineCaptureWriters(cfg.stderrCapture, cfg.execMetadataStderrCapture) + return combineCaptureWriters(cfg.StdoutCapture, cfg.ExecMetadataStdoutCapture), + combineCaptureWriters(cfg.StderrCapture, cfg.ExecMetadataStderrCapture) } // combineCaptureWriters returns a over io.MultiWriter(a, b) when both are set, whichever of a/b @@ -196,6 +197,6 @@ func selectStreamingExecutor(subCommand string, dryRun bool) streamingExecutorFu // The caller's cancellation (e.g. a shell-option deadline) flows through ctx, so a // cancelled/timed-out caller can stop a running streaming Terraform process instead // of leaving it orphaned. -func dispatchStreamingExecutor(ctx context.Context, subCommand string, dryRun bool, execOpts *tfui.ExecuteOptions) error { +func (e *Executor) dispatchStreamingExecutor(ctx context.Context, subCommand string, dryRun bool, execOpts *tfui.ExecuteOptions) error { return selectStreamingExecutor(subCommand, dryRun)(ctx, execOpts) } diff --git a/internal/exec/terraform_streaming_ui_test.go b/pkg/component/terraform/streaming_ui_test.go similarity index 90% rename from internal/exec/terraform_streaming_ui_test.go rename to pkg/component/terraform/streaming_ui_test.go index 7164d9374b2..d6eb1741bf2 100644 --- a/internal/exec/terraform_streaming_ui_test.go +++ b/pkg/component/terraform/streaming_ui_test.go @@ -1,11 +1,11 @@ -package exec +package terraform // terraform_streaming_ui_test.go covers the routing/dispatch logic in // terraform_streaming_ui.go: -// - executeStreamingOrShell: retry-gated shell fallback, the "streaming not +// - testExecutor(t).executeStreamingOrShell: retry-gated shell fallback, the "streaming not // requested" silent fallback, and the "streaming requested but unsupported" // warn-then-fallback path. -// - selectStreamingExecutor/dispatchStreamingExecutor: the subCommand -> +// - selectStreamingExecutor/testExecutor(t).dispatchStreamingExecutor: the subCommand -> // tfui.Execute* routing table. // // The streaming path is exercised in dry-run mode using the existing force-TTY @@ -13,11 +13,11 @@ package exec // builder tests separately verify the execution and formatting settings handed off // to the streaming executors. // -// The switch cases inside dispatchStreamingExecutor ARE covered two ways: +// The switch cases inside testExecutor(t).dispatchStreamingExecutor ARE covered two ways: // - TestSelectStreamingExecutor_RoutesBySubcommand asserts the *identity* of // the resolved tfui.Execute* variant directly (via selectStreamingExecutor), // which is the only way to tell apart "routed to ExecuteInit and it -// refused" from "fell through to the plain Execute and it refused" - both +// refused" from "fell through to the plain testExecutor(t).Execute and it refused" - both // return the identical error below. // - TestDispatchStreamingExecutor_RoutesSafely pins CI=true so // telemetry.IsCI() deterministically forces every tfui.Execute* variant's @@ -40,6 +40,8 @@ import ( "strings" "testing" + "github.com/cloudposse/atmos/pkg/component" + "github.com/spf13/viper" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -129,14 +131,14 @@ func TestExecuteStreamingOrShell_ForcedTTYDryRun(t *testing.T) { req := &streamingExecRequest{ args: []string{"plan"}, gatePhase: "plan", subCommand: "plan", componentPath: componentPath, } - require.NoError(t, executeStreamingOrShell(&schema.AtmosConfiguration{}, info, req)) + require.NoError(t, testExecutor(t).executeStreamingOrShell(&schema.AtmosConfiguration{}, info, req)) entries, err := os.ReadDir(componentPath) require.NoError(t, err) assert.Empty(t, entries, "streaming dry run must not create plan files") } // ────────────────────────────────────────────────────────────────────────────── -// executeStreamingOrShell +// testExecutor(t).executeStreamingOrShell // ────────────────────────────────────────────────────────────────────────────── // TestExecuteStreamingOrShell_RetryActiveAlwaysUsesShellPath verifies that a @@ -167,7 +169,7 @@ func TestExecuteStreamingOrShell_RetryActiveAlwaysUsesShellPath(t *testing.T) { gatePhase: "apply", } - execErr := executeStreamingOrShell(&atmosConfig, info, req) + execErr := testExecutor(t).executeStreamingOrShell(&atmosConfig, info, req) require.NoError(t, execErr, "retry-gated path must run the shell command successfully") } @@ -217,14 +219,14 @@ func TestExecuteStreamingOrShell_FallsBackToShell(t *testing.T) { subCommand: tt.subCommand, } - execErr := executeStreamingOrShell(&atmosConfig, info, req) + execErr := testExecutor(t).executeStreamingOrShell(&atmosConfig, info, req) require.NoError(t, execErr, "must fall back to the shell command and run it successfully") }) } } // ────────────────────────────────────────────────────────────────────────────── -// dispatchStreamingExecutor +// testExecutor(t).dispatchStreamingExecutor // ────────────────────────────────────────────────────────────────────────────── // TestDispatchStreamingExecutor_RoutesSafely drives every subCommand branch of the @@ -240,7 +242,7 @@ func TestDispatchStreamingExecutor_RoutesSafely(t *testing.T) { dryRun bool }{ { - name: "dry run skips the switch entirely and calls the plain Execute path", + name: "dry run skips the switch entirely and calls the plain testExecutor(t).Execute path", subCommand: subcommandApply, dryRun: true, }, @@ -275,7 +277,7 @@ func TestDispatchStreamingExecutor_RoutesSafely(t *testing.T) { dryRun: false, }, { - name: "unrecognized subcommand falls through to the plain Execute path", + name: "unrecognized subcommand falls through to the plain testExecutor(t).Execute path", subCommand: "unknown-subcommand", dryRun: false, }, @@ -294,7 +296,7 @@ func TestDispatchStreamingExecutor_RoutesSafely(t *testing.T) { WorkingDir: t.TempDir(), } - err := dispatchStreamingExecutor(context.Background(), tt.subCommand, tt.dryRun, execOpts) + err := testExecutor(t).dispatchStreamingExecutor(context.Background(), tt.subCommand, tt.dryRun, execOpts) require.Error(t, err, "must refuse to run outside a supported interactive environment") assert.True(t, errors.Is(err, errUtils.ErrStreamingNotSupported), "expected ErrStreamingNotSupported, got: %v", err) @@ -358,7 +360,7 @@ func TestCombineCaptureWriters(t *testing.T) { } // TestStreamingCaptureWriters verifies streamingCaptureWriters correctly assembles the -// shellCommandConfig from shellOpts and combines the ordinary stdout/stderr capture with +// component.ExecutionOptions from shellOpts and combines the ordinary stdout/stderr capture with // the scoped exec-metadata capture, so neither consumer's capture goes dark when a phase // runs through the streaming TUI instead of the plain shell path. func TestStreamingCaptureWriters(t *testing.T) { @@ -392,7 +394,7 @@ func TestStreamingCaptureWriters(t *testing.T) { opts := []ShellCommandOption{ WithStdoutCapture(&stdoutBuf), WithStderrCapture(&stderrBuf), - withExecMetadataOutputCapture(&execStdoutBuf, &execStderrBuf), + component.WithMetadataOutputCapture(&execStdoutBuf, &execStderrBuf), } stdout, stderr := streamingCaptureWriters(opts) @@ -418,14 +420,14 @@ func TestSelectStreamingExecutor_RoutesBySubcommand(t *testing.T) { dryRun bool want streamingExecutorFunc }{ - {name: "dry run always uses the plain Execute path", subCommand: subcommandApply, dryRun: true, want: tfui.Execute}, + {name: "dry run always uses the plain testExecutor(t).Execute path", subCommand: subcommandApply, dryRun: true, want: tfui.Execute}, {name: "apply routes to ExecuteApply", subCommand: subcommandApply, dryRun: false, want: tfui.ExecuteApply}, {name: "destroy routes to ExecuteDestroy", subCommand: "destroy", dryRun: false, want: tfui.ExecuteDestroy}, {name: "plan routes to ExecutePlan", subCommand: "plan", dryRun: false, want: tfui.ExecutePlan}, {name: "init routes to ExecuteInit", subCommand: subcommandInit, dryRun: false, want: tfui.ExecuteInit}, - {name: "workspace routes to ExecuteInit, not the plain Execute fallback", subCommand: subcommandWorkspace, dryRun: false, want: tfui.ExecuteInit}, - {name: "providers-lock routes to ExecuteInit, not the plain Execute fallback", subCommand: subcommandProvidersLock, dryRun: false, want: tfui.ExecuteInit}, - {name: "unrecognized subcommand falls through to the plain Execute path", subCommand: "unknown-subcommand", dryRun: false, want: tfui.Execute}, + {name: "workspace routes to ExecuteInit, not the plain testExecutor(t).Execute fallback", subCommand: subcommandWorkspace, dryRun: false, want: tfui.ExecuteInit}, + {name: "providers-lock routes to ExecuteInit, not the plain testExecutor(t).Execute fallback", subCommand: subcommandProvidersLock, dryRun: false, want: tfui.ExecuteInit}, + {name: "unrecognized subcommand falls through to the plain testExecutor(t).Execute path", subCommand: "unknown-subcommand", dryRun: false, want: tfui.Execute}, } for _, tt := range tests { diff --git a/internal/exec/terraform_test_fixtures.go b/pkg/component/terraform/test_fixtures.go similarity index 61% rename from internal/exec/terraform_test_fixtures.go rename to pkg/component/terraform/test_fixtures.go index 9bf41d18495..713837288b3 100644 --- a/internal/exec/terraform_test_fixtures.go +++ b/pkg/component/terraform/test_fixtures.go @@ -1,8 +1,9 @@ -package exec +package terraform import ( "maps" + "github.com/cloudposse/atmos/pkg/component" "github.com/cloudposse/atmos/pkg/schema" ) @@ -10,7 +11,7 @@ import ( // hooks: they can start the emulator that the component's identity requires. // Source/workdir provisioning still precedes the hook so its files reach Terraform. // Only runtime keys introduced by provisioning survive authenticated stack processing. -func prepareTerraformTestFixtures(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, opts ...ShellCommandOption) (map[string]any, error) { +func (e *Executor) prepareTerraformTestFixtures(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, opts ...component.ExecutionOption) (map[string]any, error) { if info.SubCommand != "test" { return nil, nil } @@ -18,7 +19,7 @@ func prepareTerraformTestFixtures(atmosConfig *schema.AtmosConfiguration, info * if !ok { return nil, nil } - resolved, err := ProcessStacks(atmosConfig, *info, true, false, false, info.Skip, nil) + resolved, err := e.services.ProcessStacks(atmosConfig, *info, true, false, false, info.Skip, nil) if err != nil { return nil, err } @@ -33,14 +34,14 @@ func prepareTerraformTestFixtures(atmosConfig *schema.AtmosConfiguration, info * if err := checkComponentRestrictions(info); err != nil { return nil, err } - return provisionAndRunTerraformTestFixtures(atmosConfig, info, opts...) + return e.provisionAndRunTerraformTestFixtures(atmosConfig, info, opts...) } // provisionAndRunTerraformTestFixtures retains the runtime keys introduced by // provisioning, so authenticated stack resolution cannot discard them. -func provisionAndRunTerraformTestFixtures(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, opts ...ShellCommandOption) (map[string]any, error) { +func (e *Executor) provisionAndRunTerraformTestFixtures(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, opts ...component.ExecutionOption) (map[string]any, error) { original := maps.Clone(info.ComponentSection) - if _, err := resolveAndProvisionComponentPath(shellCommandContext(opts...), shellCommandOutputWriters(opts...), atmosConfig, info); err != nil { + if _, err := e.resolveAndProvisionComponentPath(component.ContextFromOptions(opts...), executionOutputWriters(opts...), atmosConfig, info); err != nil { return nil, err } state := make(map[string]any) @@ -53,13 +54,13 @@ func provisionAndRunTerraformTestFixtures(atmosConfig *schema.AtmosConfiguration } // runPreparedTerraformHooks runs hooks with the execution directory already resolved. -func runPreparedTerraformHooks(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, opts ...ShellCommandOption) error { +func runPreparedTerraformHooks(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, opts ...component.ExecutionOption) error { preparedHooks, ok := info.NodeHooks.(schema.ComponentNodeHooksWithPreparation) if !ok { return nil } - writers := shellCommandOutputWriters(opts...) - return preparedHooks.BeforePrepared(shellCommandContext(opts...), atmosConfig, info, schema.ComponentNodeHookWriters{ + writers := executionOutputWriters(opts...) + return preparedHooks.BeforePrepared(component.ContextFromOptions(opts...), atmosConfig, info, schema.ComponentNodeHookWriters{ Stdout: writers.Stdout, Stderr: writers.Stderr, }) } diff --git a/pkg/component/terraform/testmain_test.go b/pkg/component/terraform/testmain_test.go new file mode 100644 index 00000000000..34034a3c032 --- /dev/null +++ b/pkg/component/terraform/testmain_test.go @@ -0,0 +1,356 @@ +package terraform + +import ( + fmt "fmt" + os "os" + filepath "path/filepath" + slices "slices" + strconv "strconv" + strings "strings" + sync "sync" + testing "testing" + time "time" + + "github.com/cloudposse/atmos/pkg/auth/credentials/credtest" + ci "github.com/cloudposse/atmos/pkg/ci" + githubprovider "github.com/cloudposse/atmos/pkg/ci/providers/github" + component "github.com/cloudposse/atmos/pkg/component" + data "github.com/cloudposse/atmos/pkg/data" + iolib "github.com/cloudposse/atmos/pkg/io" + schema "github.com/cloudposse/atmos/pkg/schema" + tf "github.com/cloudposse/atmos/pkg/terraform" + ui "github.com/cloudposse/atmos/pkg/ui" +) + +const ( + testEnvFakeTerraform = "_ATMOS_TEST_FAKE_TERRAFORM" + testEnvFakeTerraformSelectFail = "_ATMOS_TEST_FAKE_TERRAFORM_SELECT_FAIL" + testEnvFakeTerraformSelectOutput = "_ATMOS_TEST_FAKE_TERRAFORM_SELECT_OUTPUT" + testEnvFakeTerraformPlanOutput = "_ATMOS_TEST_FAKE_TERRAFORM_PLAN_OUTPUT" + testEnvRunLogGroupPipeline = "_ATMOS_TEST_RUN_LOG_GROUP_PIPELINE" + testEnvPipelineBackendType = "_ATMOS_TEST_PIPELINE_BACKEND_TYPE" + testEnvPipelineSkipInit = "_ATMOS_TEST_PIPELINE_SKIP_INIT" +) + +// TestMain is the entry point for the Terraform component test binary. +// It intercepts several env vars before any test runs, enabling tests to use +// the test binary itself as a portable subprocess — no Unix-only binaries required. +// +// Supported env vars (processed in declaration order): +// +// _ATMOS_TEST_COUNTER_FILE= — if set, append one byte ("x") to +// on every invocation (for single-invocation +// regression guard in terraform_execute_single_invocation_test.go). +// _ATMOS_TEST_ARGS_FILE= — if set, write subprocess arguments and exit +// successfully (for command argument assertions). +// _ATMOS_TEST_SLEEP_MS= — if set, sleep N milliseconds and exit 0 (for step +// timeout tests). +// _ATMOS_TEST_STDOUT= — if set, write text to stdout. +// _ATMOS_TEST_STDERR= — if set, write text to stderr. +// _ATMOS_TEST_EXIT_ONE=1 — if set, exit 1 (writing _ATMOS_TEST_STDOUT/ +// _ATMOS_TEST_STDERR first when also set — used to +// drive retry.conditions matching in end-to-end +// retry-wiring tests). Without those, exits 1 +// immediately after the optional counter-file write +// (for workspace recovery tests). +// _ATMOS_TEST_EXIT_CODE= — if set to a valid integer, exit N immediately +// (for exit-code-neutralization tests needing a +// code other than 0/1, e.g. -detailed-exitcode's 2). +// _ATMOS_TEST_REQUIRE_ARG= — if set, exit 0 only when is present in argv; +// otherwise write _ATMOS_TEST_STDOUT (if set) and exit 1 +// (for smart-init recovery tests simulating terraform/tofu's +// own "init is required" diagnostics until a retry adds the +// missing flag). +func TestMain(m *testing.M) { + // Initialize the I/O writer and ui formatter so data.Write*/ui.Write* calls + // (used throughout internal/exec and its pkg/ci dependency, e.g. CI log + // groups) don't panic or silently no-op — including in the + // runLogGroupPipelineForTest subprocess re-exec path below, which is its + // own process invocation with no other test's init to inherit. + if ioCtx, err := iolib.NewContext(); err == nil { + data.InitWriter(ioCtx) + ui.InitFormatter(ioCtx) + } + + if os.Getenv("_ATMOS_TEST_PLAN_DIFF_TERRAFORM") == "1" { + os.Exit(runPlanDiffTerraformForTest()) + } + if os.Getenv("_ATMOS_TEST_SECRET_PLAN") == "1" { + os.Exit(runSecretPlanForTest()) + } + + if os.Getenv(testEnvFakeTerraform) == "1" { + os.Exit(runFakeTerraformForTest()) + } + if os.Getenv(testEnvRunLogGroupPipeline) == "1" { + os.Exit(runLogGroupPipelineForTest()) + } + + // Write a single byte to the counter file on every invocation. + // This lets tests count how many times the subprocess was spawned by reading + // the file length: len(file) == number of invocations. + if counterFile := os.Getenv("_ATMOS_TEST_COUNTER_FILE"); counterFile != "" { + fd, err := os.OpenFile(counterFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) + if err != nil { + fmt.Fprintf(os.Stderr, "open counter file: %v\n", err) + os.Exit(1) + } + if _, err := fd.WriteString("x"); err != nil { + fmt.Fprintf(os.Stderr, "write counter file: %v\n", err) + os.Exit(1) + } + if err := fd.Close(); err != nil { + fmt.Fprintf(os.Stderr, "close counter file: %v\n", err) + os.Exit(1) + } + } + + if argsFile := os.Getenv("_ATMOS_TEST_ARGS_FILE"); argsFile != "" { + _ = os.WriteFile(argsFile, []byte(strings.Join(os.Args[1:], "\n")), 0o600) + os.Exit(0) + } + + // _ATMOS_TEST_SLEEP_MS= makes the test binary sleep N milliseconds and exit 0, so tests can + // exercise step timeouts with the test binary itself as a long-running command. + if sleepMS := os.Getenv("_ATMOS_TEST_SLEEP_MS"); sleepMS != "" { + if ms, err := strconv.Atoi(sleepMS); err == nil { + time.Sleep(time.Duration(ms) * time.Millisecond) + } + os.Exit(0) + } + + // Subprocess helper: when the test binary is invoked as the "terraform" command, + // this env var causes it to exit 1, simulating a failed command without requiring + // the POSIX "false" command. Combined with _ATMOS_TEST_STDOUT/_ATMOS_TEST_STDERR, + // it writes the given text before exiting 1 -- used to drive retry.conditions + // pattern-matching in end-to-end retry-wiring tests (e.g. packer/helmfile). + if os.Getenv("_ATMOS_TEST_EXIT_ONE") == "1" { + if stdout := os.Getenv("_ATMOS_TEST_STDOUT"); stdout != "" { + _, _ = os.Stdout.WriteString(stdout) + } + if stderr := os.Getenv("_ATMOS_TEST_STDERR"); stderr != "" { + _, _ = os.Stderr.WriteString(stderr) + } + os.Exit(1) + } + + // Subprocess helper: when _ATMOS_TEST_REQUIRE_ARG= is set, the subprocess inspects its + // own argv: if is present, it exits 0; otherwise it writes _ATMOS_TEST_STDOUT (if set) + // to stdout and exits 1. Used to simulate terraform/tofu's own "init is required" diagnostics + // for smart-init recovery tests: the fake subprocess "fails" (asking for -upgrade / + // -reconfigure) until the caller's retry actually adds the missing flag, at which point it + // succeeds -- without needing a real terraform/tofu binary. + if requireArg := os.Getenv("_ATMOS_TEST_REQUIRE_ARG"); requireArg != "" { + if slices.Contains(os.Args[1:], requireArg) { + os.Exit(0) + } + if stdout := os.Getenv("_ATMOS_TEST_STDOUT"); stdout != "" { + _, _ = os.Stdout.WriteString(stdout) + } + os.Exit(1) + } + + wroteOutput := false + if stdout := os.Getenv("_ATMOS_TEST_STDOUT"); stdout != "" { + _, _ = os.Stdout.WriteString(stdout) + wroteOutput = true + } + if stderr := os.Getenv("_ATMOS_TEST_STDERR"); stderr != "" { + _, _ = os.Stderr.WriteString(stderr) + wroteOutput = true + } + if wroteOutput { + os.Exit(0) + } + + // Subprocess helper: when the test binary is invoked as the "terraform" command, + // this env var causes it to exit N immediately, for exit-code-neutralization + // tests needing a code other than 0/1 (e.g. -detailed-exitcode's 2). + if code := os.Getenv("_ATMOS_TEST_EXIT_CODE"); code != "" { + if n, convErr := strconv.Atoi(code); convErr == nil { + os.Exit(n) + } + } + + // Isolate the Terraform provider plugin cache for this package's tests. + // Terraform's plugin cache is NOT safe for concurrent use, and `go test ./...` + // runs package test binaries in parallel. Sharing the global XDG plugin cache + // (TF_PLUGIN_CACHE_DIR -> ~/.cache/atmos/terraform/plugins) with other packages' + // concurrent terraform invocations races on Windows, surfacing as + // "plugin cache dir cannot be opened" / "Required plugins are not installed". + // Redirecting XDG_CACHE_HOME to a per-binary temp dir removes the contention. + // The real-terraform tests in this package run serially, so they safely share + // this private cache (isolated and faster: a single provider download). + // TestMain only has *testing.M, so t.TempDir/t.Setenv are unavailable here; + // os.MkdirTemp/os.Setenv with explicit cleanup below is the only option. + cacheDir, err := os.MkdirTemp("", "atmos-exec-xdg-cache-") //nolint:lintroller // no *testing.T in TestMain; cleaned up below. + if err == nil { + _ = os.Setenv("XDG_CACHE_HOME", cacheDir) //nolint:lintroller // no *testing.T in TestMain; process-level isolation for the whole binary. + } + + // Keep the moved test package isolated from the real OS keychain too. + code := credtest.IsolateKeyringMain(m) + + if cacheDir != "" { + _ = os.RemoveAll(cacheDir) // os.Exit skips defers; clean up explicitly. + } + os.Exit(code) +} + +// runFakeTerraformForTest simulates a terraform/tofu subprocess for pipeline +// tests. The testEnvFakeTerraformSelectOutput/testEnvFakeTerraformPlanOutput +// env vars let a test control what each phase prints to stdout (e.g. a "No +// changes." lookalike string on workspace select, to reproduce FR-006f's +// original bug: incidental phase output poisoning the exec-metadata parser's +// buffer). +func runFakeTerraformForTest() int { + args := os.Args[1:] + fmt.Printf("fake terraform %s\n", strings.Join(args, " ")) + + isWorkspaceSelect := len(args) >= 3 && args[0] == subcommandWorkspace && args[1] == "select" + if isWorkspaceSelect && os.Getenv(testEnvFakeTerraformSelectFail) == "1" { + fmt.Fprintf(os.Stderr, "Workspace %q doesn't exist.\n", args[2]) + return 1 + } + if isWorkspaceSelect { + if out := os.Getenv(testEnvFakeTerraformSelectOutput); out != "" { + fmt.Println(out) + } + return 0 + } + if len(args) >= 1 && args[0] == "plan" { + if out := os.Getenv(testEnvFakeTerraformPlanOutput); out != "" { + fmt.Println(out) + } + } + return 0 +} + +func runLogGroupPipelineForTest() int { + ci.Register(githubprovider.NewProvider()) + + componentPath := filepath.Join(os.TempDir(), fmt.Sprintf("atmos-log-group-pipeline-%d", os.Getpid())) + if err := os.MkdirAll(componentPath, 0o755); err != nil { + fmt.Fprintf(os.Stderr, "creating temp component path: %v\n", err) + return 1 + } + defer func() { _ = os.RemoveAll(componentPath) }() + + exePath, err := os.Executable() + if err != nil { + fmt.Fprintf(os.Stderr, "resolving test executable: %v\n", err) + return 1 + } + + atmosConfig := schema.AtmosConfiguration{} + atmosConfig.CI.Enabled = true + atmosConfig.CI.Groups.Mode = ci.GroupModeAuto + + componentEnv := []string{testEnvFakeTerraform + "=1"} + if os.Getenv(testEnvFakeTerraformSelectFail) == "1" { + componentEnv = append(componentEnv, testEnvFakeTerraformSelectFail+"=1") + } + + info := schema.ConfigAndStacksInfo{ + SubCommand: "plan", + SkipInit: os.Getenv(testEnvPipelineSkipInit) == "1", + ComponentBackendType: os.Getenv(testEnvPipelineBackendType), + TerraformWorkspace: "dev", + Command: exePath, + ComponentEnvList: componentEnv, + } + execCtx := &componentExecContext{ + componentPath: componentPath, + varFile: "vars.tfvars", + planFile: "plan.tfplan", + workingDir: componentPath, + } + + if err := NewTestExecutor().executeCommandPipeline(&atmosConfig, &info, execCtx); err != nil { + fmt.Fprintf(os.Stderr, "pipeline failed: %v\n", err) + return 1 + } + return 0 +} + +// runPlanDiffTerraformForTest implements show/plan using JSON plan fixtures, without a provider or network. +func runPlanDiffTerraformForTest() int { + args := os.Args[1:] + if len(args) == 0 { + return 1 + } + switch args[0] { + case "show": + contents, err := os.ReadFile(args[len(args)-1]) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return 1 + } + fmt.Println(string(contents)) + case "plan": + for _, arg := range args[1:] { + if path, found := strings.CutPrefix(arg, "-out="); found { + if err := os.WriteFile(path, []byte(os.Getenv("_ATMOS_TEST_PLAN_DIFF_FRESH_JSON")), 0o600); err != nil { + fmt.Fprintln(os.Stderr, err) + return 1 + } + return 0 + } + } + return 1 + case "version": + fmt.Println(`{"terraform_version":"1.11.0","platform":"test","provider_selections":{}}`) + case "workspace": + if len(args) > 1 && args[1] == "show" { + fmt.Println("test") + } + } + return 0 +} + +var ( + WithStdoutCapture = component.WithStdoutCapture + WithStderrCapture = component.WithStderrCapture + WithProcessContext = component.WithProcessContext +) + +var ( + WithProcessStreams = component.WithProcessStreams + WithEnvironment = component.WithEnvironment + WithStdoutOverride = component.WithStdoutOverride +) + +type ( + ShellOptions = tf.ShellOptions + ProcessingOptions = tf.ProcessingOptions +) + +const componentInfoKey = "component_info" + +func intPtr(v int) *int { return &v } + +const terraformConfigKey = "terraform_config" + +type ( + DescribeAffectedCmdArgs = component.DescribeAffectedCmdArgs + DescribeStacksErrorOptions = component.DescribeStacksErrorOptions +) + +// NewTestExecutor is injected by the external host fixture to avoid a component-to-host dependency. +var ( + NewTestExecutor func() *Executor + testExecutors sync.Map +) + +func testExecutor(t testing.TB) *Executor { + t.Helper() + if instance, ok := testExecutors.Load(t); ok { + return instance.(*Executor) + } + executor := NewTestExecutor() + instance, loaded := testExecutors.LoadOrStore(t, executor) + if !loaded { + t.Cleanup(func() { testExecutors.Delete(t) }) + } + return instance.(*Executor) +} diff --git a/internal/exec/terraform_utils.go b/pkg/component/terraform/utils.go similarity index 57% rename from internal/exec/terraform_utils.go rename to pkg/component/terraform/utils.go index d125a5dc7bd..cbee5047ebf 100644 --- a/internal/exec/terraform_utils.go +++ b/pkg/component/terraform/utils.go @@ -1,7 +1,6 @@ -package exec +package terraform import ( - "context" "errors" "os" "path/filepath" @@ -10,20 +9,12 @@ import ( "strings" cfg "github.com/cloudposse/atmos/pkg/config" - "github.com/cloudposse/atmos/pkg/generator" - "github.com/cloudposse/atmos/pkg/generator/providers" - "github.com/cloudposse/atmos/pkg/generator/required_providers" - log "github.com/cloudposse/atmos/pkg/logger" "github.com/cloudposse/atmos/pkg/naming" "github.com/cloudposse/atmos/pkg/perf" "github.com/cloudposse/atmos/pkg/schema" - tfbackend "github.com/cloudposse/atmos/pkg/terraform/backend" - tfoutput "github.com/cloudposse/atmos/pkg/terraform/output" u "github.com/cloudposse/atmos/pkg/utils" ) -const commandStr = "command" - // shouldProcessStacks determines whether to process stacks and check stack configuration. // Based on the command type and provided arguments. func shouldProcessStacks(info *schema.ConfigAndStacksInfo) (shouldProcess bool, shouldCheckStack bool) { @@ -40,57 +31,26 @@ func shouldProcessStacks(info *schema.ConfigAndStacksInfo) (shouldProcess bool, return true, true } +// Preserve the historical CLI error text during the component extraction. +// +//nolint:staticcheck // ST1005: changing capitalization would change existing command output. +var errTerraformBasePathMissing = errors.New("Base path to terraform components must be provided in 'components.terraform.base_path' config or " + + "'ATMOS_COMPONENTS_TERRAFORM_BASE_PATH' ENV variable") + +//nolint:gocritic // Preserve the existing value-based configuration contract. func checkTerraformConfig(atmosConfig schema.AtmosConfiguration) error { if len(atmosConfig.Components.Terraform.BasePath) < 1 { - return errors.New("Base path to terraform components must be provided in 'components.terraform.base_path' config or " + - "'ATMOS_COMPONENTS_TERRAFORM_BASE_PATH' ENV variable") + return errTerraformBasePathMissing } return nil } -// cleanTerraformWorkspace deletes the `.terraform/environment` file from the component directory. -// The `.terraform/environment` file contains the name of the currently selected workspace, -// helping Terraform identify the active workspace context for managing your infrastructure. -// We delete the file to prevent the Terraform prompt asking to select the default or the -// previously used workspace. This happens when different backends are used for the same component. -func cleanTerraformWorkspace(atmosConfig schema.AtmosConfiguration, componentPath string) { - // Get `TF_DATA_DIR` ENV variable, default to `.terraform` if not set - tfDataDir := os.Getenv("TF_DATA_DIR") - if tfDataDir == "" { - tfDataDir = ".terraform" - } - - // Convert relative path to absolute - if !filepath.IsAbs(tfDataDir) { - tfDataDir = filepath.Join(componentPath, tfDataDir) - } - - // Ensure the path is cleaned properly - tfDataDir = filepath.Clean(tfDataDir) - - // Construct the full file path - filePath := filepath.Join(tfDataDir, "environment") - - // Check if the file exists before attempting deletion - if _, err := os.Stat(filePath); err == nil { - log.Debug("Terraform environment file found. Proceeding with deletion.", "file", filePath) - - // Use retry logic on Windows to handle file locking - deleteErr := tfoutput.RetryOnWindows(func() error { - return os.Remove(filePath) - }) - - if deleteErr != nil { - log.Debug("Failed to delete Terraform environment file.", "file", filePath, "error", deleteErr) - } else { - log.Debug("Successfully deleted Terraform environment file.", "file", filePath) - } - } else if os.IsNotExist(err) { - log.Debug("Terraform environment file not found. No action needed.", "file", filePath) - } else { - log.Debug("Error checking Terraform environment file.", "file", filePath, "error", err) - } +// cleanTerraformWorkspace delegates workspace cleanup to the Terraform component. +// +//nolint:gocritic // Preserve the existing value-based workspace cleanup contract. +func cleanTerraformWorkspace(_ schema.AtmosConfiguration, componentPath string) { + CleanTerraformWorkspace(componentPath) } // isTerraformCurrentWorkspace reports whether the given workspace name matches the workspace @@ -106,8 +66,8 @@ func cleanTerraformWorkspace(atmosConfig schema.AtmosConfiguration, componentPat // process env, ensuring this helper reads the same data directory that the terraform subprocess // would use. A relative TF_DATA_DIR is joined to componentPath, matching Terraform's own // resolution relative to the process working directory. -func isTerraformCurrentWorkspace(componentPath, workspace string, envList []string) bool { - tfDataDir := envVarFromList(envList, "TF_DATA_DIR") +func (e *Executor) isTerraformCurrentWorkspace(componentPath, workspace string, envList []string) bool { + tfDataDir := e.services.EnvVarFromList(envList, "TF_DATA_DIR") if tfDataDir == "" { //nolint:forbidigo // TF_DATA_DIR is a Terraform convention, not an Atmos config var. tfDataDir = os.Getenv("TF_DATA_DIR") @@ -119,7 +79,7 @@ func isTerraformCurrentWorkspace(componentPath, workspace string, envList []stri tfDataDir = filepath.Join(componentPath, tfDataDir) } envFile := filepath.Join(filepath.Clean(tfDataDir), "environment") - data, err := os.ReadFile(envFile) //nolint:gosec // Path is constructed from componentPath + TF_DATA_DIR, not user input. + data, err := os.ReadFile(envFile) if err != nil { // Only treat a missing file as "default workspace active". // Other errors (permission denied, I/O error) are not equivalent @@ -137,67 +97,19 @@ func isTerraformCurrentWorkspace(componentPath, workspace string, envList []stri return recorded == workspace } -func generateBackendConfig(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { - return tfbackend.GenerateFile(atmosConfig, info, workingDir) +// generateBackendConfig delegates artifact generation to the Terraform component. +func generateBackendConfig(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { + return GenerateBackendConfig(config, info, workingDir) } -// generateProviderOverrides writes `providers_override.tf.json` from the component's `providers` -// section, and removes a previously generated file when that section is no longer configured. -func generateProviderOverrides(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { - // Let registered provider-config contributors (e.g. an emulator binding) deep-merge - // provider fragments UNDER the explicit `providers:` section before generation. - genCtx := generator.NewGeneratorContext(atmosConfig, info, workingDir) - if merged, err := generator.ApplyProviderContributors(context.Background(), genCtx); err != nil { - return err - } else if merged != nil { - info.ComponentProvidersSection = merged - } - - // Generate `providers_override.tf.json` file if the `providers` section is configured. - if len(info.ComponentProvidersSection) > 0 { - providerOverrideFileName := filepath.Join(workingDir, providers.DefaultFilenameConst) - - log.Debug("Writing the provider overrides to file.", "file", providerOverrideFileName) - - if !info.DryRun { - providerOverrides := generateComponentProviderOverrides(info.ComponentProvidersSection, info.AuthContext) - err := u.WriteToFileAsJSON(providerOverrideFileName, providerOverrides, 0o600) - return err - } - } else if !info.DryRun { - // Remove a stale file, otherwise an earlier stack's providers apply to this one. - return generator.RemoveGenerated(workingDir, providers.DefaultFilenameConst) - } - return nil +// generateProviderOverrides delegates artifact generation to the Terraform component. +func generateProviderOverrides(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { + return GenerateProviderOverrides(config, info, workingDir) } -// generateRequiredProviders generates the terraform_override.tf.json file with required_version -// and required_providers blocks from stack configuration (DEV-3124). -func generateRequiredProviders(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { - defer perf.Track(atmosConfig, "exec.generateRequiredProviders")() - - // Skip if no required_version or required_providers configured, removing a stale file - // so an earlier stack's version pins don't apply to this one. - if info.RequiredVersion == "" && len(info.RequiredProviders) == 0 { - if info.DryRun { - return nil - } - return generator.RemoveGenerated(workingDir, required_providers.DefaultFilenameConst) - } - - requiredProvidersFileName := filepath.Join(workingDir, required_providers.DefaultFilenameConst) - - log.Debug("Writing the required_providers to file.", "file", requiredProvidersFileName) - - if info.DryRun { - return nil - } - - // Create generator context. - genCtx := generator.NewGeneratorContext(atmosConfig, info, workingDir) - - // Generate and write using the generator package. - return generator.Generate(context.Background(), required_providers.Name, genCtx, generator.NewFileWriter()) +// generateRequiredProviders delegates artifact generation to the Terraform component. +func generateRequiredProviders(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { + return GenerateRequiredProviders(config, info, workingDir) } // needProcessTemplatesAndYamlFunctions checks if a Terraform command requires the `Go` templates and Atmos YAML functions to be processed. @@ -287,10 +199,10 @@ type ComponentStack struct { // Order is unspecified — it suits order-independent operations like cache mirroring // (which, unlike plan/apply, has no inter-component dependencies). Auth and YAML // functions are disabled: only structural component selection is needed. -func ListTerraformComponentTargets(atmosConfig *schema.AtmosConfiguration, filterStack string, components []string, query string) ([]ComponentStack, error) { +func (e *Executor) ListTerraformComponentTargets(atmosConfig *schema.AtmosConfiguration, filterStack string, components []string, query string) ([]ComponentStack, error) { defer perf.Track(atmosConfig, "exec.ListTerraformComponentTargets")() - stacks, err := ExecuteDescribeStacksWithAuthDisabled( + stacks, err := e.services.ExecuteDescribeStacksWithAuthDisabled( atmosConfig, filterStack, components, []string{cfg.TerraformComponentType}, nil, false, false, false, false, nil, nil, true, ) @@ -300,7 +212,7 @@ func ListTerraformComponentTargets(atmosConfig *schema.AtmosConfiguration, filte var targets []ComponentStack walkErr := walkTerraformComponents(stacks, func(stackName, componentName string, componentSection map[string]any) error { - include, err := mirrorTargetIncluded(atmosConfig, componentName, componentSection, query) + include, err := e.mirrorTargetIncluded(atmosConfig, componentName, componentSection, query) if err != nil { return err } @@ -333,12 +245,12 @@ func sortComponentStacks(targets []ComponentStack) { // mirrorTargetIncluded reports whether a component should be a mirror target: it must // be concrete (not abstract), enabled, and match the optional YQ query. -func mirrorTargetIncluded(atmosConfig *schema.AtmosConfiguration, componentName string, componentSection map[string]any, query string) (bool, error) { +func (e *Executor) mirrorTargetIncluded(atmosConfig *schema.AtmosConfiguration, componentName string, componentSection map[string]any, query string) (bool, error) { if metadataSection, ok := componentSection[cfg.MetadataSectionName].(map[string]any); ok { if metadataType, ok := metadataSection["type"].(string); ok && metadataType == "abstract" { return false, nil } - if !isComponentEnabled(metadataSection, componentName) { + if !e.services.IsComponentEnabled(metadataSection, componentName) { return false, nil } } diff --git a/internal/exec/terraform_utils_test.go b/pkg/component/terraform/utils_test.go similarity index 92% rename from internal/exec/terraform_utils_test.go rename to pkg/component/terraform/utils_test.go index 7d43d9d5e7c..7a7a3606d3e 100644 --- a/internal/exec/terraform_utils_test.go +++ b/pkg/component/terraform/utils_test.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "context" @@ -109,7 +109,7 @@ func TestExecuteTerraformQuery(t *testing.T) { os.Unsetenv("ATMOS_CLI_CONFIG_PATH") // Define the work directory and change to it. - workDir := "../../tests/fixtures/scenarios/terraform-apply-affected" + workDir := "../../../tests/fixtures/scenarios/terraform-apply-affected" t.Chdir(workDir) oldStd := os.Stderr @@ -131,14 +131,14 @@ func TestExecuteTerraformQuery(t *testing.T) { Query: ".vars.tags.team == \"eks\"", } - err := ExecuteTerraformQuery(&info) + err := testExecutor(t).ExecuteTerraformQuery(&info) // Restore stderr before checking error. w.Close() os.Stderr = oldStd if err != nil { - t.Fatalf("Failed to execute 'ExecuteTerraformQuery': %v", err) + t.Fatalf("Failed to testExecutor(t).execute 'testExecutor(t).ExecuteTerraformQuery': %v", err) } } @@ -150,7 +150,7 @@ func TestExecuteTerraformQueryNoMatches(t *testing.T) { os.Unsetenv("ATMOS_CLI_CONFIG_PATH") // Define the work directory and change to it. - workDir := "../../tests/fixtures/scenarios/terraform-apply-affected" + workDir := "../../../tests/fixtures/scenarios/terraform-apply-affected" t.Chdir(workDir) oldStd := os.Stderr @@ -173,7 +173,7 @@ func TestExecuteTerraformQueryNoMatches(t *testing.T) { Query: ".vars.tags.team == \"nonexistent-team\"", } - err := ExecuteTerraformQuery(&info) + err := testExecutor(t).ExecuteTerraformQuery(&info) // Restore stderr before checking error. w.Close() @@ -181,7 +181,7 @@ func TestExecuteTerraformQueryNoMatches(t *testing.T) { // The function should succeed even when no components match. if err != nil { - t.Fatalf("ExecuteTerraformQuery should succeed when no components match: %v", err) + t.Fatalf("testExecutor(t).ExecuteTerraformQuery should succeed when no components match: %v", err) } } @@ -209,7 +209,7 @@ func TestExecuteTerraformQueryRoutesThroughSchedulerAdapter(t *testing.T) { require.True(t, processStacks) return schema.AtmosConfiguration{}, nil }) - patches.ApplyFunc(ExecuteDescribeStacksWithMocks, func( + patches.ApplyFuncVar(&testExecutor(t).services.ExecuteDescribeStacksWithMocks, func( atmosConfig *schema.AtmosConfiguration, filterByStack string, components []string, @@ -264,7 +264,7 @@ func TestExecuteTerraformQueryRoutesThroughSchedulerAdapter(t *testing.T) { ProcessFunctions: true, Skip: []string{"skip-me"}, } - require.NoError(t, ExecuteTerraformQuery(info)) + require.NoError(t, testExecutor(t).ExecuteTerraformQuery(info)) require.True(t, described) require.True(t, scheduled) } @@ -303,7 +303,7 @@ func TestExecuteTerraformAffectedRoutesThroughSchedulerAdapter(t *testing.T) { require.True(t, processStacks) return schema.AtmosConfiguration{}, nil }) - patches.ApplyFunc(GetAffectedComponents, func(args *DescribeAffectedCmdArgs) ([]schema.Affected, error) { + patches.ApplyFuncVar(&testExecutor(t).services.GetAffectedComponents, func(args *DescribeAffectedCmdArgs) ([]schema.Affected, error) { describedAffected = true require.NotNil(t, args.CLIConfig) require.Equal(t, repoPath, args.RepoPath) @@ -319,7 +319,7 @@ func TestExecuteTerraformAffectedRoutesThroughSchedulerAdapter(t *testing.T) { {Component: "deleted", Stack: "dev", ComponentType: cfg.TerraformComponentType, Deleted: true}, }, nil }) - patches.ApplyFunc(ExecuteDescribeStacksWithAuthDisabledAndMocks, func( + patches.ApplyFuncVar(&testExecutor(t).services.ExecuteDescribeStacksWithAuthDisabledAndMocks, func( atmosConfig *schema.AtmosConfiguration, filterByStack string, components []string, @@ -381,7 +381,7 @@ func TestExecuteTerraformAffectedRoutesThroughSchedulerAdapter(t *testing.T) { ProcessYamlFunctions: true, Skip: []string{"skip-me"}, } - require.NoError(t, ExecuteTerraformAffectedWithContext(context.Background(), args, info)) + require.NoError(t, testExecutor(t).ExecuteTerraformAffectedWithContext(context.Background(), args, info)) require.True(t, describedAffected) require.True(t, describedStacks) require.True(t, scheduled) @@ -390,6 +390,7 @@ func TestExecuteTerraformAffectedRoutesThroughSchedulerAdapter(t *testing.T) { func TestExecuteTerraformQueryPropagatesSetupErrors(t *testing.T) { skipGomonkeyOnDarwinARM64(t) + executor := testExecutor(t) var describedStacks map[string]any var describeErr error var initErr error @@ -399,7 +400,7 @@ func TestExecuteTerraformQueryPropagatesSetupErrors(t *testing.T) { describePatches.ApplyFunc(cfg.InitCliConfig, func(schema.ConfigAndStacksInfo, bool) (schema.AtmosConfiguration, error) { return schema.AtmosConfiguration{}, initErr }) - describePatches.ApplyFunc(ExecuteDescribeStacksWithMocks, func( + describePatches.ApplyFuncVar(&executor.services.ExecuteDescribeStacksWithMocks, func( *schema.AtmosConfiguration, string, []string, @@ -426,7 +427,7 @@ func TestExecuteTerraformQueryPropagatesSetupErrors(t *testing.T) { expectedErr := errors.New("config failed") initErr = expectedErr - err := ExecuteTerraformQuery(&schema.ConfigAndStacksInfo{}) + err := executor.ExecuteTerraformQuery(&schema.ConfigAndStacksInfo{}) require.ErrorIs(t, err, expectedErr) }) @@ -442,7 +443,7 @@ func TestExecuteTerraformQueryPropagatesSetupErrors(t *testing.T) { authManagerFactory = oldAuthManagerFactory }() - err := ExecuteTerraformQuery(&schema.ConfigAndStacksInfo{}) + err := executor.ExecuteTerraformQuery(&schema.ConfigAndStacksInfo{}) require.ErrorIs(t, err, expectedErr) }) @@ -459,7 +460,7 @@ func TestExecuteTerraformQueryPropagatesSetupErrors(t *testing.T) { defer func() { authManagerFactory = oldAuthManagerFactory }() - err := ExecuteTerraformQuery(&schema.ConfigAndStacksInfo{}) + err := executor.ExecuteTerraformQuery(&schema.ConfigAndStacksInfo{}) require.ErrorIs(t, err, expectedErr) }) @@ -477,11 +478,41 @@ func TestExecuteTerraformQueryPropagatesSetupErrors(t *testing.T) { defer func() { authManagerFactory = oldAuthManagerFactory }() - err := ExecuteTerraformQuery(&schema.ConfigAndStacksInfo{}) + err := executor.ExecuteTerraformQuery(&schema.ConfigAndStacksInfo{}) require.ErrorIs(t, err, expectedErr) }) } +// TestExecuteTerraformQueryPropagatesDescribeError exercises the injected service without +// monkey-patching, including on platforms where the setup-error test must be skipped. +func TestExecuteTerraformQueryPropagatesDescribeError(t *testing.T) { + t.Chdir(filepath.Join("..", "..", "..", "tests", "fixtures", "scenarios", "terraform-apply-affected")) + t.Setenv("ATMOS_BASE_PATH", "") + t.Setenv("ATMOS_CLI_CONFIG_PATH", "") + + expectedErr := errors.New("describe failed") + executor := testExecutor(t) + calls := 0 + executor.services.ExecuteDescribeStacksWithMocks = func( + *schema.AtmosConfiguration, string, []string, []string, []string, + bool, bool, bool, bool, []string, auth.AuthManager, bool, + []string, map[string]string, DescribeStacksErrorOptions, + ) (map[string]any, error) { + calls++ + return nil, expectedErr + } + ctrl := gomock.NewController(t) + mockFactory := NewMockAuthManagerQueryFactory(ctrl) + mockFactory.EXPECT().Create(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil) + previousFactory := authManagerFactory + authManagerFactory = mockFactory + t.Cleanup(func() { authManagerFactory = previousFactory }) + + err := executor.ExecuteTerraformQuery(&schema.ConfigAndStacksInfo{}) + require.ErrorIs(t, err, expectedErr) + require.Equal(t, 1, calls) +} + func TestCreateQueryAuthManagerPropagatesFactoryError(t *testing.T) { expectedErr := errors.New("auth failed") ctrl := gomock.NewController(t) @@ -1578,7 +1609,7 @@ func TestIsTerraformCurrentWorkspace(t *testing.T) { require.NoError(t, os.MkdirAll(tfDir, 0o755)) require.NoError(t, os.WriteFile(filepath.Join(tfDir, "environment"), []byte("nonprod"), 0o644)) - assert.True(t, isTerraformCurrentWorkspace(dir, "nonprod", nil)) + assert.True(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "nonprod", nil)) }) t.Run("returns true when environment file has trailing newline", func(t *testing.T) { @@ -1587,7 +1618,7 @@ func TestIsTerraformCurrentWorkspace(t *testing.T) { require.NoError(t, os.MkdirAll(tfDir, 0o755)) require.NoError(t, os.WriteFile(filepath.Join(tfDir, "environment"), []byte("nonprod\n"), 0o644)) - assert.True(t, isTerraformCurrentWorkspace(dir, "nonprod", nil)) + assert.True(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "nonprod", nil)) }) t.Run("returns false when workspace does not match", func(t *testing.T) { @@ -1596,7 +1627,7 @@ func TestIsTerraformCurrentWorkspace(t *testing.T) { require.NoError(t, os.MkdirAll(tfDir, 0o755)) require.NoError(t, os.WriteFile(filepath.Join(tfDir, "environment"), []byte("staging"), 0o644)) - assert.False(t, isTerraformCurrentWorkspace(dir, "nonprod", nil)) + assert.False(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "nonprod", nil)) }) t.Run("returns false when environment file does not exist", func(t *testing.T) { @@ -1604,13 +1635,13 @@ func TestIsTerraformCurrentWorkspace(t *testing.T) { // Create .terraform dir but omit the environment file — distinct from the // directory-absent case covered by the next sub-test. require.NoError(t, os.MkdirAll(filepath.Join(dir, ".terraform"), 0o755)) - assert.False(t, isTerraformCurrentWorkspace(dir, "nonprod", nil)) + assert.False(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "nonprod", nil)) }) t.Run("returns false when .terraform directory does not exist", func(t *testing.T) { dir := t.TempDir() // No .terraform directory at all — environment file is implicitly absent. - assert.False(t, isTerraformCurrentWorkspace(dir, "nonprod", nil)) + assert.False(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "nonprod", nil)) }) t.Run("respects TF_DATA_DIR env var", func(t *testing.T) { @@ -1620,13 +1651,13 @@ func TestIsTerraformCurrentWorkspace(t *testing.T) { require.NoError(t, os.WriteFile(filepath.Join(customDir, "environment"), []byte("nonprod"), 0o644)) t.Setenv("TF_DATA_DIR", customDir) - assert.True(t, isTerraformCurrentWorkspace(dir, "nonprod", nil)) + assert.True(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "nonprod", nil)) }) t.Run("respects relative TF_DATA_DIR env var", func(t *testing.T) { // Terraform resolves TF_DATA_DIR relative to the component path (the process CWD // at invocation time, which atmos sets to componentPath). Verify that - // isTerraformCurrentWorkspace applies the same resolution. + // testExecutor(t).isTerraformCurrentWorkspace applies the same resolution. dir := t.TempDir() relDir := "custom-tf-dir" customDir := filepath.Join(dir, relDir) @@ -1634,7 +1665,7 @@ func TestIsTerraformCurrentWorkspace(t *testing.T) { require.NoError(t, os.WriteFile(filepath.Join(customDir, "environment"), []byte("nonprod"), 0o644)) t.Setenv("TF_DATA_DIR", relDir) - assert.True(t, isTerraformCurrentWorkspace(dir, "nonprod", nil)) + assert.True(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "nonprod", nil)) }) // Default workspace: Terraform never writes the environment file for "default". @@ -1642,12 +1673,12 @@ func TestIsTerraformCurrentWorkspace(t *testing.T) { t.Run("returns true for default workspace when environment file is absent", func(t *testing.T) { dir := t.TempDir() // No .terraform directory or environment file — default workspace is implied. - assert.True(t, isTerraformCurrentWorkspace(dir, "default", nil)) + assert.True(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "default", nil)) }) t.Run("returns false for non-default workspace when environment file is absent", func(t *testing.T) { dir := t.TempDir() - assert.False(t, isTerraformCurrentWorkspace(dir, "nonprod", nil)) + assert.False(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "nonprod", nil)) }) t.Run("returns true for default workspace when environment file is empty", func(t *testing.T) { @@ -1656,7 +1687,7 @@ func TestIsTerraformCurrentWorkspace(t *testing.T) { require.NoError(t, os.MkdirAll(tfDir, 0o755)) require.NoError(t, os.WriteFile(filepath.Join(tfDir, "environment"), []byte(""), 0o644)) - assert.True(t, isTerraformCurrentWorkspace(dir, "default", nil)) + assert.True(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "default", nil)) }) t.Run("returns false for non-default workspace when environment file is empty", func(t *testing.T) { @@ -1665,11 +1696,11 @@ func TestIsTerraformCurrentWorkspace(t *testing.T) { require.NoError(t, os.MkdirAll(tfDir, 0o755)) require.NoError(t, os.WriteFile(filepath.Join(tfDir, "environment"), []byte(""), 0o644)) - assert.False(t, isTerraformCurrentWorkspace(dir, "nonprod", nil)) + assert.False(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "nonprod", nil)) }) t.Run("TF_DATA_DIR absolute path is used directly without joining componentPath", func(t *testing.T) { - // When TF_DATA_DIR is set to an absolute path, isTerraformCurrentWorkspace must read + // When TF_DATA_DIR is set to an absolute path, testExecutor(t).isTerraformCurrentWorkspace must read // the environment file from that absolute path rather than joining componentPath. // This documents the invariant for the filepath.IsAbs guard in the implementation. absDataDir := t.TempDir() @@ -1684,7 +1715,7 @@ func TestIsTerraformCurrentWorkspace(t *testing.T) { // componentPath is a different directory — the environment file is under absDataDir, not here. componentPath := t.TempDir() - assert.True(t, isTerraformCurrentWorkspace(componentPath, workspace, nil), + assert.True(t, testExecutor(t).isTerraformCurrentWorkspace(componentPath, workspace, nil), "absolute TF_DATA_DIR must resolve the environment file without joining componentPath") }) @@ -1704,7 +1735,7 @@ func TestIsTerraformCurrentWorkspace(t *testing.T) { // Pass TF_DATA_DIR via envList — simulates ComponentEnvList. envList := []string{"TF_DATA_DIR=" + customDir} - assert.True(t, isTerraformCurrentWorkspace(dir, "nonprod", envList), + assert.True(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "nonprod", envList), "envList TF_DATA_DIR must be used when set in subprocess env") }) @@ -1725,9 +1756,9 @@ func TestIsTerraformCurrentWorkspace(t *testing.T) { require.NoError(t, os.WriteFile(filepath.Join(subprocessDir, "environment"), []byte("nonprod"), 0o644)) envList := []string{"TF_DATA_DIR=" + subprocessDir} - assert.True(t, isTerraformCurrentWorkspace(dir, "nonprod", envList), + assert.True(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "nonprod", envList), "envList TF_DATA_DIR must override process env TF_DATA_DIR") - assert.False(t, isTerraformCurrentWorkspace(dir, "staging", envList), + assert.False(t, testExecutor(t).isTerraformCurrentWorkspace(dir, "staging", envList), "process env TF_DATA_DIR must not be used when envList overrides it") }) } diff --git a/internal/exec/terraform_verify_plan.go b/pkg/component/terraform/verify_plan.go similarity index 84% rename from internal/exec/terraform_verify_plan.go rename to pkg/component/terraform/verify_plan.go index b4ec7f67224..b6c66f9feb4 100644 --- a/internal/exec/terraform_verify_plan.go +++ b/pkg/component/terraform/verify_plan.go @@ -1,4 +1,4 @@ -package exec +package terraform import ( "encoding/json" @@ -23,7 +23,7 @@ import ( // info.UseTerraformPlan is set to true so the subsequent apply uses the verified fresh plan. // The stored planfile is cleaned up. When drift is detected and mode is fail, it returns an // error wrapping ErrPlanVerificationFailed; when mode is warn, it logs the drift and proceeds. -func VerifyPlanfile(info *schema.ConfigAndStacksInfo, storedPlanFile string, mode schema.PlanfileVerifyMode) error { +func (s *Operations) VerifyPlanfile(info *schema.ConfigAndStacksInfo, storedPlanFile string, mode schema.PlanfileVerifyMode) error { defer perf.Track(nil, "exec.VerifyPlanfile")() // Validate stored planfile exists on disk. @@ -31,7 +31,7 @@ func VerifyPlanfile(info *schema.ConfigAndStacksInfo, storedPlanFile string, mod return fmt.Errorf("%w: stored planfile does not exist: %s", errUtils.ErrPlanVerificationFailed, storedPlanFile) } - vc, err := resolveVerificationContext(info) + vc, err := s.resolveVerificationContext(info) if err != nil { return err } @@ -40,7 +40,7 @@ func VerifyPlanfile(info *schema.ConfigAndStacksInfo, storedPlanFile string, mod if err != nil { return err } - plans, err := loadStoredAndFreshPlans(&vc.atmosConfig, &vc.processedInfo, vc.componentPath, storedPlanFile) + plans, err := s.loadStoredAndFreshPlans(&vc.atmosConfig, &vc.processedInfo, vc.componentPath, storedPlanFile) if err != nil { os.Remove(storedPlanFile) return err @@ -66,7 +66,7 @@ type verificationContext struct { // resolveVerificationContext initializes the CLI config, processes stacks, and // resolves the component working directory used to generate the fresh plan. -func resolveVerificationContext(info *schema.ConfigAndStacksInfo) (verificationContext, error) { +func (s *Operations) resolveVerificationContext(info *schema.ConfigAndStacksInfo) (verificationContext, error) { defer perf.Track(nil, "exec.resolveVerificationContext")() vc := verificationContext{} @@ -78,7 +78,7 @@ func resolveVerificationContext(info *schema.ConfigAndStacksInfo) (verificationC vc.atmosConfig = atmosConfig // Process stacks to resolve component metadata (metadata.component, component_folder_prefix). - processedInfo, err := ProcessStacks(&atmosConfig, *info, true, true, true, nil, nil) + processedInfo, err := s.host.ProcessStacks(&atmosConfig, *info, true, true, true, nil, nil) if err != nil { return vc, fmt.Errorf("error processing stacks for plan verification: %w", err) } @@ -112,25 +112,25 @@ type comparisonPlans struct { // loadStoredAndFreshPlans returns the parsed JSON of the stored plan and of a // freshly generated plan (plus the fresh plan file path) for comparison. -func loadStoredAndFreshPlans(atmosConfig *schema.AtmosConfiguration, processedInfo *schema.ConfigAndStacksInfo, componentPath, storedPlanFile string) (comparisonPlans, error) { +func (s *Operations) loadStoredAndFreshPlans(atmosConfig *schema.AtmosConfiguration, processedInfo *schema.ConfigAndStacksInfo, componentPath, storedPlanFile string) (comparisonPlans, error) { defer perf.Track(nil, "exec.loadStoredAndFreshPlans")() var plans comparisonPlans - storedPlanJSON, err := getTerraformPlanJSON(atmosConfig, processedInfo, componentPath, storedPlanFile) + storedPlanJSON, err := s.getTerraformPlanJSON(atmosConfig, processedInfo, componentPath, storedPlanFile) if err != nil { return plans, fmt.Errorf("error getting JSON for stored plan: %w", err) } // Generate a fresh plan at the canonical planfile path in the component dir, // where terraform expects it, rather than in a temp dir. - freshPlanPath := constructTerraformComponentPlanfilePath(atmosConfig, processedInfo) - plans.freshPlanFile, err = generateNewPlanFile(atmosConfig, processedInfo, componentPath, filepath.Dir(freshPlanPath)) + freshPlanPath := ConstructTerraformComponentPlanfilePath(atmosConfig, processedInfo) + plans.freshPlanFile, err = s.GenerateNewPlanFile(atmosConfig, processedInfo, componentPath, filepath.Dir(freshPlanPath)) if err != nil { return plans, fmt.Errorf("error generating fresh plan for verification: %w", err) } - freshPlanJSON, err := getTerraformPlanJSON(atmosConfig, processedInfo, componentPath, plans.freshPlanFile) + freshPlanJSON, err := s.getTerraformPlanJSON(atmosConfig, processedInfo, componentPath, plans.freshPlanFile) if err != nil { return plans, fmt.Errorf("error getting JSON for fresh plan: %w", err) } From a7493788c3a029ba587bdb294c7e14cea45b1a51 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 16:35:20 -0600 Subject: [PATCH 02/25] fix(terraform): classify only the final init attempt and bound providers lock Reset the smart-init diagnostic buffer before each retried init attempt so a diagnostic printed by an earlier attempt cannot make RecoverInit re-run init with -upgrade/-reconfigure. Pass the after-init hook's five-minute deadline to the providers-lock subprocess so a hung lock cannot block plan/apply. Co-Authored-By: Claude Opus 5.5 --- .../execute_helpers_recovery_test.go | 40 ++++++++++++++++++- pkg/component/terraform/execute_init.go | 14 +++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/pkg/component/terraform/execute_helpers_recovery_test.go b/pkg/component/terraform/execute_helpers_recovery_test.go index 07d98a3e084..941192d64af 100644 --- a/pkg/component/terraform/execute_helpers_recovery_test.go +++ b/pkg/component/terraform/execute_helpers_recovery_test.go @@ -331,6 +331,36 @@ func TestRetryMainCommand_PropagatesRetryError(t *testing.T) { assert.Same(t, retryErr, err) } +// TestRunTerraformInitSubprocessClassifiesOnlyFinalAttempt verifies that output from an +// earlier retried init attempt does not reach smart-init recovery classification. +func TestRunTerraformInitSubprocessClassifiesOnlyFinalAttempt(t *testing.T) { + executor := testExecutor(t) + attempts := 0 + executor.services.ExecuteShellCommand = func(_ schema.AtmosConfiguration, _ string, _ []string, _ string, _ []string, _ bool, _ string, opts ...component.ExecutionOption) error { + attempts++ + out, _ := streamingCaptureWriters(opts) + require.NotNil(t, out) + if attempts == 1 { + _, _ = out.Write([]byte("must use terraform init -upgrade; 502 Bad Gateway")) + return errors.New("transient") + } + _, _ = out.Write([]byte("Error: invalid backend configuration")) + return errors.New("init failed") + } + info := &schema.ConfigAndStacksInfo{ + SubCommand: "plan", + ComponentRetrySection: &schema.RetryConfig{ + MaxAttempts: intPtr(2), + Conditions: []string{"/Bad Gateway/"}, + }, + } + + output, err := executor.runTerraformInitSubprocess(&schema.AtmosConfiguration{}, info, t.TempDir(), []string{"init"}) + require.Error(t, err) + assert.Equal(t, 2, attempts) + assert.Equal(t, "Error: invalid backend configuration", output) +} + // TestImplicitInitRecoversAndLocksProviders proves the implicit init path retries with // the diagnosed flag, records only successful init, and keeps lock failures best-effort. func TestImplicitInitRecoversAndLocksProviders(t *testing.T) { @@ -356,7 +386,15 @@ func TestImplicitInitRecoversAndLocksProviders(t *testing.T) { require.Equal(t, "test-terraform", command) require.Equal(t, dir, gotDir) require.Equal(t, info.ComponentEnvList, env) - require.Same(t, t.Context(), component.ContextFromOptions(opts...)) + gotCtx := component.ContextFromOptions(opts...) + if len(calls) <= 2 { + require.Same(t, t.Context(), gotCtx) + } else { + // The after-init hook's deadline must reach the providers-lock subprocess. + _, hasDeadline := gotCtx.Deadline() + require.True(t, hasDeadline, "providers lock must run under the hook deadline") + require.NoError(t, gotCtx.Err()) + } switch len(calls) { case 1: out, _ := streamingCaptureWriters(opts) diff --git a/pkg/component/terraform/execute_init.go b/pkg/component/terraform/execute_init.go index b267c54a63b..82760f277d7 100644 --- a/pkg/component/terraform/execute_init.go +++ b/pkg/component/terraform/execute_init.go @@ -239,6 +239,9 @@ func (e *Executor) runTerraformInitSubprocess(atmosConfig *schema.AtmosConfigura info, "init", func(o ...component.ExecutionOption) error { + // Classify only the final attempt: a diagnostic printed by an earlier retried + // attempt must not make RecoverInit re-run init with recovery flags. + buf.Reset() return e.executeStreamingOrShell(atmosConfig, info, &streamingExecRequest{ componentPath: componentPath, args: args, @@ -320,6 +323,12 @@ func (e *Executor) executeTerraformInitCommand(atmosConfig *schema.AtmosConfigur // "Selected … workspace" lines. Lock completion is best-effort: a failure is logged, not // propagated, so it never fails the user's plan/apply. func (e *Executor) dispatchAfterInit(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string, opts ...component.ExecutionOption) { + ctx, cancel := context.WithTimeout(component.ContextFromOptions(opts...), 5*time.Minute) + defer cancel() + + // The hook deadline must reach the subprocess too, so a hung `providers lock` + // cannot block plan/apply past the hook's timeout. + runOpts := append(slices.Clone(opts), component.WithProcessContext(ctx)) execCtx := &provisioner.TerraformExecContext{ WorkingDir: componentPath, Run: func(args []string) error { @@ -329,14 +338,11 @@ func (e *Executor) dispatchAfterInit(atmosConfig *schema.AtmosConfiguration, inf gatePhase: subcommandInit, subCommand: subcommandProvidersLock, redirectStdErr: info.RedirectStdErr, - shellOpts: opts, + shellOpts: runOpts, }) }, } - ctx, cancel := context.WithTimeout(component.ContextFromOptions(opts...), 5*time.Minute) - defer cancel() - if err := provisioner.ExecuteProvisioners( ctx, provisioner.HookEvent(afterTerraformInitEvent), From 451c724235a819aeb43f906bd5eb0b71dc5c54aa Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 16:36:36 -0600 Subject: [PATCH 03/25] fix(terraform): harden the component runtime pipeline - Skip plan/varfile cleanup on dry runs so a dry-run apply or destroy does not delete artifacts saved by an earlier run. - Recheck the component directory after auto-generation creates it, so the first command on a generated component proceeds. - Invalidate the state and output reference caches after every Terraform command, including failures that may have persisted partial changes. - Filter the plugin-cache env into a new slice instead of rewriting a caller's shared SanitizedEnv in place. - Register temporary RC cleanup before preparation (execute and shell paths), so a later preparation failure cannot leave the CLI config behind. - Forward execution options to the version handler. - Gate the apply TTY requirement on the effective subprocess stdin, honoring streams supplied through WithProcessStreams. Co-Authored-By: Claude Opus 5.5 --- pkg/component/terraform/execute.go | 35 ++++++------- pkg/component/terraform/execute_command.go | 28 +++++++++-- .../terraform/execute_environment.go | 15 +++++- .../execute_helpers_coverage_test.go | 29 +++++++++++ .../terraform/execute_helpers_test.go | 50 +++++++++++++++++++ .../execute_helpers_workspace_test.go | 14 ++++++ pkg/component/terraform/execute_pipeline.go | 2 +- pkg/component/terraform/execute_prepare.go | 5 ++ pkg/component/terraform/plugin_cache_test.go | 13 +++++ pkg/component/terraform/shell.go | 17 +++---- 10 files changed, 171 insertions(+), 37 deletions(-) diff --git a/pkg/component/terraform/execute.go b/pkg/component/terraform/execute.go index 5ac79ac83ef..2908599c5e0 100644 --- a/pkg/component/terraform/execute.go +++ b/pkg/component/terraform/execute.go @@ -106,7 +106,7 @@ func startManagedTerraformCache(atmosConfig *schema.AtmosConfiguration, info *sc // execute runs the Terraform component lifecycle behind the registered provider. // Optional ShellCommandOption values are forwarded to the final ExecuteShellCommand call. // -//nolint:revive,cyclop,funlen,gocritic,gocognit // Existing pipeline complexity; reporting adds only a deferred snapshot. +//nolint:revive,cyclop,funlen,gocritic // Existing pipeline complexity; reporting adds only a deferred snapshot. func (e *Executor) execute(info schema.ConfigAndStacksInfo, opts ...component.ExecutionOption) (resultErr error) { defer perf.Track(nil, "exec.ExecuteTerraform")() defer e.services.AttachComponentReporting(&resultErr, &info, "terraform", info.SubCommand) @@ -147,7 +147,7 @@ func (e *Executor) execute(info schema.ConfigAndStacksInfo, opts ...component.Ex // Short-circuit for `terraform version` – no stack processing required. if info.SubCommand == "version" { - return e.handleVersionSubcommand(&atmosConfig, &info) + return e.handleVersionSubcommand(&atmosConfig, &info, opts...) } // Test fixtures may start the emulator that authentication needs. Provision @@ -202,6 +202,11 @@ func (e *Executor) execute(info schema.ConfigAndStacksInfo, opts ...component.Ex defer closeCache() } + // Remove the temporary Terraform CLI config (TF_CLI_CONFIG_FILE) after the whole + // pipeline (init, workspace, plan/apply) completes. Registered before preparation, + // which creates the file, so it is also removed when a later preparation step fails. + defer cleanupTerraformRC(&info) + // Resolve paths, install toolchain, write varfiles, validate, run hooks, and build env. execCtx, err := e.prepareComponentExecution(component.ContextFromOptions(opts...), executionOutputWriters(opts...), &atmosConfig, &info, shouldProcess) if err != nil { @@ -209,17 +214,6 @@ func (e *Executor) execute(info schema.ConfigAndStacksInfo, opts ...component.Ex } execCtx.planPolicy = planPolicy - // Remove the temporary Terraform CLI config (TF_CLI_CONFIG_FILE) after the whole - // pipeline (init, workspace, plan/apply) completes. Registered here, not inside the - // pipeline, so the file survives every subprocess and is cleaned up on early errors. - if info.RCCleanup != nil { - defer func() { - if cleanupErr := info.RCCleanup(); cleanupErr != nil { - log.Debug("Failed to remove temporary Terraform CLI config", "error", cleanupErr) - } - }() - } - // Persist auth context so PostRunE hooks (e.g. store hooks that read // terraform outputs) can reuse the credentials established during this // execution. Without this, hooks create a fresh info with no auth. @@ -236,12 +230,11 @@ func (e *Executor) execute(info schema.ConfigAndStacksInfo, opts ...component.Ex // Forward caller-provided options (e.g. CI stdout/stderr capture) alongside the environment option. opts = append(opts, component.WithEnvironment(info.SanitizedEnv)) err = e.executeCommandPipeline(&atmosConfig, &info, execCtx, opts...) - if err == nil { - // A successful Terraform command can create, change, or remove state. Drop - // any preflight snapshot so a dependent graph node reads the current outputs. - invalidateTerraformStateCache(info.Stack, info.ComponentFromArg) - tfoutput.InvalidateComponentOutputs(info.Stack, info.ComponentFromArg) - } + // A Terraform command can create, change, or remove state, and a failed apply can + // still persist partial changes. Drop any preflight snapshot either way so a later + // graph node (including one in a keep-going run) reads the current outputs. + invalidateTerraformStateCache(info.Stack, info.ComponentFromArg) + tfoutput.InvalidateComponentOutputs(info.Stack, info.ComponentFromArg) captureExecMetadataSync(&atmosConfig, originalSubCommand, &info, execMetadataSyncParams{ Cmd: component.InvokingCommandFromOptions(opts...), @@ -421,7 +414,9 @@ func removeEnvKeys(env []string, keys ...string) []string { skip[key] = struct{}{} } - filtered := env[:0] + // Allocate: callers may share the input slice (e.g. a reused SanitizedEnv), + // and filtering in place would corrupt it for later or concurrent executions. + filtered := make([]string, 0, len(env)) for _, entry := range env { key := envKey(entry) if _, ok := skip[key]; ok { diff --git a/pkg/component/terraform/execute_command.go b/pkg/component/terraform/execute_command.go index 40ec335ec94..e526464a463 100644 --- a/pkg/component/terraform/execute_command.go +++ b/pkg/component/terraform/execute_command.go @@ -26,9 +26,11 @@ import ( ) // checkTTYRequirement returns an error when `terraform apply` is invoked without -// -auto-approve in a non-interactive environment (stdin is nil). -func checkTTYRequirement(info *schema.ConfigAndStacksInfo) error { - if os.Stdin != nil { +// -auto-approve in a non-interactive environment (the subprocess stdin is nil). +// The subprocess stdin is the stream supplied through component.WithProcessStreams, +// falling back to os.Stdin, matching how the shell command runner resolves it. +func checkTTYRequirement(info *schema.ConfigAndStacksInfo, opts ...component.ExecutionOption) error { + if effectiveSubprocessStdin(opts...) != nil { return nil } if info.SubCommand == subcommandApply && !slices.Contains(info.AdditionalArgsAndFlags, autoApproveFlag) { @@ -41,6 +43,21 @@ func checkTTYRequirement(info *schema.ConfigAndStacksInfo) error { return nil } +// effectiveSubprocessStdin returns the stdin the Terraform subprocess will read. +func effectiveSubprocessStdin(opts ...component.ExecutionOption) io.Reader { + var probe component.ExecutionOptions + for _, opt := range opts { + opt(&probe) + } + if probe.Streams != nil && probe.Streams.Stdin != nil { + return probe.Streams.Stdin + } + if os.Stdin == nil { + return nil + } + return os.Stdin +} + // addRegionEnvVarForImport appends AWS_REGION to the component env list when the // subcommand is `import` and the component has a `region` variable configured. func addRegionEnvVarForImport(info *schema.ConfigAndStacksInfo) { @@ -236,6 +253,11 @@ func (e *Executor) uploadCommandStatus( // Failures are logged at Trace level and not propagated, since cleanup errors should // not mask the result of the main command. func cleanupTerraformFiles(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) { + // A dry run consumes nothing, so it must not delete a plan or varfile saved by an earlier run. + if info.DryRun { + return + } + // Only a completed apply/destroy consumes the default plan. Inspection commands // also pass through this pipeline and must preserve plans saved for later use. if (info.SubCommand == subcommandApply || info.SubCommand == "destroy") && info.PlanFile == "" { diff --git a/pkg/component/terraform/execute_environment.go b/pkg/component/terraform/execute_environment.go index 67f1d5e03e0..14ce13500b7 100644 --- a/pkg/component/terraform/execute_environment.go +++ b/pkg/component/terraform/execute_environment.go @@ -154,6 +154,17 @@ func configureTerraformRC(atmosConfig *schema.AtmosConfiguration, info *schema.C return nil } +// cleanupTerraformRC removes the temporary Terraform CLI config, if one was generated. +// It reads info.RCCleanup when it runs, so callers can defer it before preparation. +func cleanupTerraformRC(info *schema.ConfigAndStacksInfo) { + if info == nil || info.RCCleanup == nil { + return + } + if cleanupErr := info.RCCleanup(); cleanupErr != nil { + log.Debug("Failed to remove temporary Terraform CLI config", "error", cleanupErr) + } +} + // mergeCacheContribution merges the registry cache's CLI-config directives into // rcMap. The cache owns provider_installation (it must be the single network-mirror // egress); host overrides are merged per-host so user-declared hosts are preserved. @@ -246,7 +257,7 @@ func resolveTerraformCommand(atmosConfig *schema.AtmosConfiguration, info *schem // handleVersionSubcommand executes the `terraform version` command and returns the result. // It resolves the toolchain binary and delegates directly to the shell, bypassing // full stack processing. -func (e *Executor) handleVersionSubcommand(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { +func (e *Executor) handleVersionSubcommand(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, baseOpts ...component.ExecutionOption) error { tenv, err := dependencies.ForComponent(atmosConfig, "terraform", nil, nil) if err != nil { return err @@ -262,7 +273,7 @@ func (e *Executor) handleVersionSubcommand(atmosConfig *schema.AtmosConfiguratio info.RedirectStdErr, opts..., ) - }) + }, baseOpts...) } func (e *Executor) setupTerraformAuth(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (auth.AuthManager, error) { diff --git a/pkg/component/terraform/execute_helpers_coverage_test.go b/pkg/component/terraform/execute_helpers_coverage_test.go index eeef8455dba..bf4a2d549b7 100644 --- a/pkg/component/terraform/execute_helpers_coverage_test.go +++ b/pkg/component/terraform/execute_helpers_coverage_test.go @@ -510,6 +510,35 @@ func TestCleanupTerraformFiles_ApplyRemovesVarfileForReal(t *testing.T) { assert.NoFileExists(t, varfilePath) } +// TestCleanupTerraformFiles_DryRunKeepsPlanAndVarfile verifies that a dry-run apply +// does not consume (and so must not delete) a plan or varfile saved by an earlier run. +func TestCleanupTerraformFiles_DryRunKeepsPlanAndVarfile(t *testing.T) { + atmosConfig := schema.AtmosConfiguration{BasePath: t.TempDir()} + info := schema.ConfigAndStacksInfo{ + SubCommand: "apply", + ContextPrefix: "ctx", + Component: "mycomp", + DryRun: true, + } + + planfilePath := ConstructTerraformComponentPlanfilePath(&atmosConfig, &info) + varfilePath := ConstructTerraformComponentVarfilePath(&atmosConfig, &info) + for _, path := range []string{planfilePath, varfilePath} { + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + require.NoError(t, os.WriteFile(path, []byte("saved"), 0o644)) + } + + cleanupTerraformFiles(&atmosConfig, &info) + assert.FileExists(t, planfilePath) + assert.FileExists(t, varfilePath) + + // The same apply without dry-run consumes both files. + info.DryRun = false + cleanupTerraformFiles(&atmosConfig, &info) + assert.NoFileExists(t, planfilePath) + assert.NoFileExists(t, varfilePath) +} + // TestCleanupTerraformFiles_NonPlanShow_RemovesPlanfile creates an actual planfile // and verifies it is removed after cleanupTerraformFiles for a destroy command. func TestCleanupTerraformFiles_NonPlanShow_RemovesPlanfile(t *testing.T) { diff --git a/pkg/component/terraform/execute_helpers_test.go b/pkg/component/terraform/execute_helpers_test.go index 15c3a304134..8f57a6265f1 100644 --- a/pkg/component/terraform/execute_helpers_test.go +++ b/pkg/component/terraform/execute_helpers_test.go @@ -509,6 +509,56 @@ func TestResolveAndProvisionComponentPathPropagatesOutputSuppression(t *testing. require.True(t, observedSuppression.Load()) } +// TestResolveAndProvisionComponentPath_RechecksPathAfterAutoGeneration verifies that a +// missing component directory created by auto-generation is accepted on the same run. +func TestResolveAndProvisionComponentPath_RechecksPathAfterAutoGeneration(t *testing.T) { + originalProvisioner := provisionAndResolveTerraformComponentPath + t.Cleanup(func() { provisionAndResolveTerraformComponentPath = originalProvisioner }) + + componentDir := filepath.Join(t.TempDir(), "generated") + provisionAndResolveTerraformComponentPath = func(_ context.Context, _ provisioner.OutputWriters, _ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _ string, _ string) (string, bool, error) { + return componentDir, false, nil + } + + var generatedIn string + executor := NewExecutor(Services{ + GenerateFilesForComponent: func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, workingDir string) error { + generatedIn = workingDir + return nil + }, + }) + atmosConfig := schema.AtmosConfiguration{BasePath: t.TempDir()} + atmosConfig.Components.Terraform.AutoGenerateFiles = true + info := schema.ConfigAndStacksInfo{ + FinalComponent: "generated", + ComponentSection: map[string]any{"generate": map[string]any{"main.tf": "# generated"}}, + } + + path, err := executor.resolveAndProvisionComponentPath(t.Context(), provisioner.OutputWriters{}, &atmosConfig, &info) + require.NoError(t, err) + assert.Equal(t, componentDir, path) + assert.Equal(t, componentDir, generatedIn) + assert.DirExists(t, componentDir) +} + +// TestResolveAndProvisionComponentPath_MissingWithoutGenerationFails verifies the recheck +// does not mask a component directory that is still missing after preparation. +func TestResolveAndProvisionComponentPath_MissingWithoutGenerationFails(t *testing.T) { + originalProvisioner := provisionAndResolveTerraformComponentPath + t.Cleanup(func() { provisionAndResolveTerraformComponentPath = originalProvisioner }) + + componentDir := filepath.Join(t.TempDir(), "missing") + provisionAndResolveTerraformComponentPath = func(_ context.Context, _ provisioner.OutputWriters, _ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _ string, _ string) (string, bool, error) { + return componentDir, false, nil + } + + atmosConfig := schema.AtmosConfiguration{BasePath: t.TempDir()} + info := schema.ConfigAndStacksInfo{FinalComponent: "missing"} + + _, err := NewExecutor(Services{}).resolveAndProvisionComponentPath(t.Context(), provisioner.OutputWriters{}, &atmosConfig, &info) + require.ErrorIs(t, err, errUtils.ErrInvalidTerraformComponent) +} + // ────────────────────────────────────────────────────────────────────────────── // handleDeploySubcommand // ────────────────────────────────────────────────────────────────────────────── diff --git a/pkg/component/terraform/execute_helpers_workspace_test.go b/pkg/component/terraform/execute_helpers_workspace_test.go index 54b9845228a..e7e7346d631 100644 --- a/pkg/component/terraform/execute_helpers_workspace_test.go +++ b/pkg/component/terraform/execute_helpers_workspace_test.go @@ -11,6 +11,7 @@ import ( "errors" "os" "path/filepath" + "strings" "testing" "github.com/cloudposse/atmos/pkg/component" @@ -166,6 +167,19 @@ func TestCheckTTYRequirement_NilStdin_NonApply_NoError(t *testing.T) { } } +// TestCheckTTYRequirement_NilStdin_ProcessStreamStdin_NoError verifies that an interactive +// stream supplied through WithProcessStreams satisfies apply even when os.Stdin is nil. +func TestCheckTTYRequirement_NilStdin_ProcessStreamStdin_NoError(t *testing.T) { + origStdin := os.Stdin + os.Stdin = nil + t.Cleanup(func() { os.Stdin = origStdin }) + + info := schema.ConfigAndStacksInfo{SubCommand: "apply"} + require.NoError(t, checkTTYRequirement(&info, component.WithProcessStreams(process.Streams{Stdin: strings.NewReader("yes\n")}))) + // Streams without stdin fall back to os.Stdin, which is still nil here. + require.ErrorIs(t, checkTTYRequirement(&info, component.WithProcessStreams(process.Streams{})), errUtils.ErrNoTty) +} + // ────────────────────────────────────────────────────────────────────────────── // testExecutor(t).executeMainTerraformCommand (bare-workspace short-circuit) // ────────────────────────────────────────────────────────────────────────────── diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index fb1b8f9d4b4..4e4750fdc85 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -190,7 +190,7 @@ func (e *Executor) executeCommandPipeline( return err } - if err = checkTTYRequirement(info); err != nil { + if err = checkTTYRequirement(info, opts...); err != nil { return err } diff --git a/pkg/component/terraform/execute_prepare.go b/pkg/component/terraform/execute_prepare.go index a399fdb1831..f6ff262a816 100644 --- a/pkg/component/terraform/execute_prepare.go +++ b/pkg/component/terraform/execute_prepare.go @@ -59,6 +59,11 @@ func (e *Executor) resolveAndProvisionComponentPath(ctx context.Context, writers return "", err } + // Auto-generation may have just created a missing component directory. + if !componentPathExists { + componentPathExists = u.FileOrDirExists(componentPath) + } + if !componentPathExists { basePath, _ := u.GetComponentBasePath(atmosConfig, cfg.TerraformComponentType) return "", fmt.Errorf( diff --git a/pkg/component/terraform/plugin_cache_test.go b/pkg/component/terraform/plugin_cache_test.go index 4a081a3b2de..7c6304e677c 100644 --- a/pkg/component/terraform/plugin_cache_test.go +++ b/pkg/component/terraform/plugin_cache_test.go @@ -234,6 +234,19 @@ func TestDisableTerraformPluginCacheForExecutionFiltersProcessEnvWhenUnsanitized assert.True(t, envListContainsKey(info.SanitizedEnv, "ATMOS_TEST_KEEP_ENV")) } +func TestDisableTerraformPluginCacheForExecutionKeepsCallerEnvSlice(t *testing.T) { + // A provider caller may reuse one SanitizedEnv across executions; filtering must not + // rewrite the caller's backing array. + shared := []string{terraformPluginCacheDirEnv + "=/shared/cache", "KEEP_SHARED=1"} + original := append([]string(nil), shared...) + info := &schema.ConfigAndStacksInfo{DisablePluginCache: true, SanitizedEnv: shared} + + disableTerraformPluginCacheForExecution(&schema.AtmosConfiguration{Env: map[string]string{}}, info) + + assert.Equal(t, original, shared) + assert.Equal(t, []string{"KEEP_SHARED=1"}, info.SanitizedEnv) +} + func TestDisableTerraformPluginCacheForExecutionNoopWithoutFlag(t *testing.T) { atmosConfig := &schema.AtmosConfiguration{ Components: schema.Components{ diff --git a/pkg/component/terraform/shell.go b/pkg/component/terraform/shell.go index bd9942b3588..a89e8436910 100644 --- a/pkg/component/terraform/shell.go +++ b/pkg/component/terraform/shell.go @@ -176,19 +176,14 @@ func (e *Executor) ExecuteTerraformShell(opts *tf.ShellOptions, atmosConfig *sch func (e *Executor) runShellSession(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, cfg *shellConfig, withSecrets bool) error { defer perf.Track(atmosConfig, "exec.runShellSession")() - if err := e.prepareShellExecution(atmosConfig, info, cfg, withSecrets); err != nil { - return err - } - // Remove the temporary Terraform CLI config (TF_CLI_CONFIG_FILE) after init, workspace, and // the whole interactive shell session complete. Deferred here (not in prepareShellExecution) - // so the file survives every subprocess and the shell. - if info.RCCleanup != nil { - defer func() { - if cleanupErr := info.RCCleanup(); cleanupErr != nil { - log.Debug("Failed to remove temporary Terraform CLI config", "error", cleanupErr) - } - }() + // so the file survives every subprocess and the shell, and registered before preparation + // (which creates the file) so it is also removed when a later preparation step fails. + defer cleanupTerraformRC(info) + + if err := e.prepareShellExecution(atmosConfig, info, cfg, withSecrets); err != nil { + return err } return e.executeShellLifecycle(atmosConfig, info, cfg) From 23b92bb92e3a9ff0a70bf8d8c58e2361a0f54081 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 16:38:24 -0600 Subject: [PATCH 04/25] fix(terraform): reconcile provider request metadata with execution info Executor.Execute now folds the request's Component, Stack, Command, SubCommand, and Args into ConfigAndStacksInfo when the info leaves them empty, and rejects a request whose metadata conflicts with the info. Co-Authored-By: Claude Opus 5.5 --- pkg/component/terraform/provider.go | 38 ++++++++++++++++++++- pkg/component/terraform/provider_test.go | 42 ++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/pkg/component/terraform/provider.go b/pkg/component/terraform/provider.go index 6db0136b1fa..897afe4048c 100644 --- a/pkg/component/terraform/provider.go +++ b/pkg/component/terraform/provider.go @@ -160,8 +160,44 @@ var _ Runtime = (*Executor)(nil) // Execute preserves caller options while making the provider request's context authoritative. func (e *Executor) Execute(ctx *component.ExecutionContext) error { defer perf.Track(nil, "terraform.Executor.Execute")() + info, err := reconcileExecutionRequest(ctx) + if err != nil { + return err + } opts := append(slices.Clone(ctx.Options), component.WithProcessContext(ctx.GoContext())) - return e.execute(ctx.ConfigAndStacksInfo, opts...) + return e.execute(info, opts...) +} + +// reconcileExecutionRequest folds the provider request's top-level metadata into the +// execution info, so a caller can name the component, stack, and subcommand once on the +// request. A field set in both places must agree; a mismatch is rejected rather than +// silently preferring either side. +func reconcileExecutionRequest(ctx *component.ExecutionContext) (schema.ConfigAndStacksInfo, error) { + info := ctx.ConfigAndStacksInfo + fields := []struct { + name string + request string + target *string + }{ + {"component", ctx.Component, &info.ComponentFromArg}, + {"stack", ctx.Stack, &info.Stack}, + {"command", ctx.Command, &info.Command}, + {"subcommand", ctx.SubCommand, &info.SubCommand}, + } + for _, field := range fields { + switch { + case field.request == "": + case *field.target == "": + *field.target = field.request + case *field.target != field.request: + return info, fmt.Errorf("%w: request %s %q conflicts with execution info %s %q", + errUtils.ErrComponentConfigInvalid, field.name, field.request, field.name, *field.target) + } + } + if info.AdditionalArgsAndFlags == nil && len(ctx.Args) > 0 { + info.AdditionalArgsAndFlags = slices.Clone(ctx.Args) + } + return info, nil } // GenerateArtifacts writes configuration in the prepared component working directory. diff --git a/pkg/component/terraform/provider_test.go b/pkg/component/terraform/provider_test.go index dd8cfc1f77f..a9bc3ec1da7 100644 --- a/pkg/component/terraform/provider_test.go +++ b/pkg/component/terraform/provider_test.go @@ -87,6 +87,48 @@ func TestProviderRejectsIncompleteRequests(t *testing.T) { require.ErrorIs(t, p.GenerateArtifacts(&component.ExecutionContext{AtmosConfig: &schema.AtmosConfiguration{}}), errUtils.ErrComponentConfigInvalid) } +func TestReconcileExecutionRequestFillsInfoFromRequest(t *testing.T) { + info, err := reconcileExecutionRequest(&component.ExecutionContext{ + Component: "app", Stack: "dev", Command: "tofu", SubCommand: "plan", Args: []string{"-lock=false"}, + }) + require.NoError(t, err) + assert.Equal(t, "app", info.ComponentFromArg) + assert.Equal(t, "dev", info.Stack) + assert.Equal(t, "tofu", info.Command) + assert.Equal(t, "plan", info.SubCommand) + assert.Equal(t, []string{"-lock=false"}, info.AdditionalArgsAndFlags) +} + +func TestReconcileExecutionRequestKeepsMatchingInfo(t *testing.T) { + request := &component.ExecutionContext{ + Component: "app", Stack: "dev", SubCommand: "plan", Args: []string{"-input=false"}, + ConfigAndStacksInfo: schema.ConfigAndStacksInfo{ + ComponentFromArg: "app", Stack: "dev", SubCommand: "plan", Command: "terraform", + AdditionalArgsAndFlags: []string{"-refresh=false"}, + }, + } + info, err := reconcileExecutionRequest(request) + require.NoError(t, err) + assert.Equal(t, "terraform", info.Command) + assert.Equal(t, []string{"-refresh=false"}, info.AdditionalArgsAndFlags) + // The caller's request is not mutated. + assert.Empty(t, request.Command) +} + +func TestReconcileExecutionRequestRejectsConflicts(t *testing.T) { + for name, request := range map[string]*component.ExecutionContext{ + "component": {Component: "app", ConfigAndStacksInfo: schema.ConfigAndStacksInfo{ComponentFromArg: "vpc"}}, + "stack": {Stack: "dev", ConfigAndStacksInfo: schema.ConfigAndStacksInfo{Stack: "prod"}}, + "subcommand": {SubCommand: "plan", ConfigAndStacksInfo: schema.ConfigAndStacksInfo{SubCommand: "apply"}}, + } { + t.Run(name, func(t *testing.T) { + _, err := reconcileExecutionRequest(request) + require.ErrorIs(t, err, errUtils.ErrComponentConfigInvalid) + assert.Contains(t, err.Error(), name) + }) + } +} + func TestOperationsKeepInjectedHostsIndependent(t *testing.T) { t.Parallel() From cc316f63c66ed2541e150a1bfcb5bdf24d4036bc Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 16:40:19 -0600 Subject: [PATCH 05/25] fix(terraform): drop the atmos.Component cache for a target after it runs The host dispatcher now invalidates componentFuncSyncMap entries for the Terraform target after every lifecycle command, so a later dependent does not render its variables from preflight outputs. Co-Authored-By: Claude Opus 5.5 --- internal/exec/terraform.go | 17 ++++++++++++ .../exec/terraform_component_provider_test.go | 26 +++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/internal/exec/terraform.go b/internal/exec/terraform.go index eee36a8b1be..ca455173119 100644 --- a/internal/exec/terraform.go +++ b/internal/exec/terraform.go @@ -15,6 +15,7 @@ import ( "github.com/cloudposse/atmos/pkg/dependency" "github.com/cloudposse/atmos/pkg/perf" schema "github.com/cloudposse/atmos/pkg/schema" + refcache "github.com/cloudposse/atmos/pkg/terraform/refcache" ) // The host wires shared Atmos services into the Terraform component and registers it. @@ -87,6 +88,10 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo, opts ...ShellCommandOptio if !ok { return fmt.Errorf("%w: terraform", errUtils.ErrComponentProviderNotFound) } + // A Terraform command can change the target's outputs, even when it fails part-way. + // Drop the host's atmos.Component() snapshot of the target so a later dependent + // renders its variables from current outputs instead of a preflight value. + defer invalidateComponentFuncCache(&info) return provider.Execute(&component.ExecutionContext{ Context: shellCommandContext(opts...), Options: opts, ComponentType: "terraform", Component: info.ComponentFromArg, @@ -96,6 +101,18 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo, opts ...ShellCommandOptio }) } +// invalidateComponentFuncCache removes every cached atmos.Component() result for the +// Terraform target, across auth scopes and inspection modes. +func invalidateComponentFuncCache(info *schema.ConfigAndStacksInfo) { + if info.ComponentFromArg == "" { + return + } + refcache.DeleteTarget(&componentFuncSyncMap, info.Stack, info.ComponentFromArg) + if info.StackFromArg != "" && info.StackFromArg != info.Stack { + refcache.DeleteTarget(&componentFuncSyncMap, info.StackFromArg, info.ComponentFromArg) + } +} + var terraformExecutor *terraformcomponent.Executor var defaultMergedAuthConfigGetter = getMergedAuthConfig diff --git a/internal/exec/terraform_component_provider_test.go b/internal/exec/terraform_component_provider_test.go index 5d3c560b2d8..3b218281524 100644 --- a/internal/exec/terraform_component_provider_test.go +++ b/internal/exec/terraform_component_provider_test.go @@ -13,6 +13,7 @@ import ( component "github.com/cloudposse/atmos/pkg/component" terraformcomponent "github.com/cloudposse/atmos/pkg/component/terraform" schema "github.com/cloudposse/atmos/pkg/schema" + refcache "github.com/cloudposse/atmos/pkg/terraform/refcache" cobra "github.com/spf13/cobra" assert "github.com/stretchr/testify/assert" require "github.com/stretchr/testify/require" @@ -66,6 +67,31 @@ func TestTerraformRegistryDispatch(t *testing.T) { require.ErrorIs(t, err, errUtils.ErrPlanHasDiff) } +func TestTerraformDispatchInvalidatesComponentFuncCache(t *testing.T) { + provider := replaceTerraformProvider(t) + t.Cleanup(func() { componentFuncSyncMap.Clear() }) + + target := refcache.Key{Stack: "live", Component: "app"} + inspection := refcache.Key{Stack: "live", Component: "app", Inspection: true} + byStackArg := refcache.Key{Stack: "live-arg", Component: "app"} + other := refcache.Key{Stack: "live", Component: "network"} + for _, key := range []refcache.Key{target, inspection, byStackArg, other} { + componentFuncSyncMap.Store(key, map[string]any{"outputs": "preflight"}) + } + + // A failed command can still change outputs, so the snapshot is dropped either way. + provider.EXPECT().Execute(gomock.Any()).Return(errUtils.ErrPlanHasDiff) + err := ExecuteTerraform(schema.ConfigAndStacksInfo{ComponentFromArg: "app", Stack: "live", StackFromArg: "live-arg", SubCommand: "apply"}) + require.ErrorIs(t, err, errUtils.ErrPlanHasDiff) + + for _, key := range []refcache.Key{target, inspection, byStackArg} { + _, ok := componentFuncSyncMap.Load(key) + assert.False(t, ok, "cached target %+v must be invalidated", key) + } + _, ok := componentFuncSyncMap.Load(other) + assert.True(t, ok, "unrelated components must keep their cache") +} + func TestRegisteredTerraformCancellation(t *testing.T) { ctx, cancel := context.WithCancel(t.Context()) cancel() From 1b1d5755ca0e38d76e123e1fd8a29ab8efb1214b Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 16:42:10 -0600 Subject: [PATCH 06/25] test(terraform): honor the fake subprocess exit code after writing output Co-Authored-By: Claude Opus 5.5 --- pkg/component/terraform/testmain_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/component/terraform/testmain_test.go b/pkg/component/terraform/testmain_test.go index 34034a3c032..0c89ca204e8 100644 --- a/pkg/component/terraform/testmain_test.go +++ b/pkg/component/terraform/testmain_test.go @@ -159,18 +159,18 @@ func TestMain(m *testing.M) { _, _ = os.Stderr.WriteString(stderr) wroteOutput = true } - if wroteOutput { - os.Exit(0) - } - // Subprocess helper: when the test binary is invoked as the "terraform" command, // this env var causes it to exit N immediately, for exit-code-neutralization - // tests needing a code other than 0/1 (e.g. -detailed-exitcode's 2). + // tests needing a code other than 0/1 (e.g. -detailed-exitcode's 2). It is checked + // after the output helpers so a test can combine output with a non-zero exit code. if code := os.Getenv("_ATMOS_TEST_EXIT_CODE"); code != "" { if n, convErr := strconv.Atoi(code); convErr == nil { os.Exit(n) } } + if wroteOutput { + os.Exit(0) + } // Isolate the Terraform provider plugin cache for this package's tests. // Terraform's plugin cache is NOT safe for concurrent use, and `go test ./...` From cdd533ddc94c96e92cfd10126414739b321970df Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 21:31:47 -0600 Subject: [PATCH 07/25] ci(docker): allow get.trivy.dev for the Trivy installer setup-trivy's install.sh for v0.70.0 downloads from get.trivy.dev, which redirects to the GitHub release asset. The [lint] Dockerfile job's egress allowlist did not include it, so on a binary cache miss curl failed to connect (exit 7) and the job failed on the missing trivy-config.sarif. Co-Authored-By: Claude Opus 5.5 --- .github/allowlist.yaml | 3 +++ .github/workflows/test.yml | 1 + 2 files changed, 4 insertions(+) diff --git a/.github/allowlist.yaml b/.github/allowlist.yaml index bc25d761033..3914d98aa10 100644 --- a/.github/allowlist.yaml +++ b/.github/allowlist.yaml @@ -38,6 +38,9 @@ opentofu: - registry.opentofu.org:443 helm: - get.helm.sh:443 +# Trivy installer (aquasecurity/setup-trivy); redirects to GitHub release assets. +trivy: + - get.trivy.dev:443 docker-hub: - auth.docker.io:443 - production.cloudfront.docker.com:443 diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5f8600a2538..9c8dc7bd9fd 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1354,6 +1354,7 @@ jobs: github github-archives github-releases + trivy - name: Check out code uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6.1.0 with: From b09d8c081fb2ac1a7711cab915bc5669b5e53c03 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 9 Nov 2026 21:05:31 -0600 Subject: [PATCH 08/25] fix(affected): honor --base and the error mode in GetAffectedComponents GetAffectedComponents ignored args.Base and args.ErrorMode, so callers such as terraform --affected compared against the wrong revision and always failed fast. A Base commit SHA now selects that commit (keeping Ref as the clone target), any other Base selects that ref, and ErrorMode maps to the lenient error options. Co-Authored-By: Claude Opus 5.5 --- internal/exec/describe_affected_helpers.go | 34 +++++++++--- internal/exec/terraform_affected_host_test.go | 52 +++++++++++++++++++ 2 files changed, 80 insertions(+), 6 deletions(-) diff --git a/internal/exec/describe_affected_helpers.go b/internal/exec/describe_affected_helpers.go index c35dce955f7..860a2584ca2 100644 --- a/internal/exec/describe_affected_helpers.go +++ b/internal/exec/describe_affected_helpers.go @@ -11,6 +11,7 @@ import ( errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/auth" + "github.com/cloudposse/atmos/pkg/ci" g "github.com/cloudposse/atmos/pkg/git" log "github.com/cloudposse/atmos/pkg/logger" "github.com/cloudposse/atmos/pkg/perf" @@ -444,11 +445,17 @@ func ExecuteDescribeAffectedWithTargetRepoPathWithOptions( // (repo path / clone-and-diff / local ref checkout). Exported so other // affected-target dispatchers (e.g. `atmos terraform lint --affected`) reuse // this switch instead of re-declaring it against the same three -// ExecuteDescribeAffectedWith* functions. +// ExecuteDescribeAffectedWith* functions. The args.ErrorMode field selects how recoverable +// discovery errors are handled; an empty or "strict" mode keeps the historical fail-fast behavior. func GetAffectedComponents(args *DescribeAffectedCmdArgs) ([]schema.Affected, error) { defer perf.Track(nil, "exec.GetAffectedComponents")() - return GetAffectedComponentsWithOptions(args, DescribeStacksErrorOptions{}) + if args == nil { + return nil, errUtils.ErrNilParam + } + errOptions, collector := ErrorOptionsFromMode(args.ErrorMode) + defer PrintErrorModeSummary(args.ErrorMode, collector) + return GetAffectedComponentsWithOptions(args, errOptions) } // GetAffectedComponentsWithOptions is GetAffectedComponents plus opt-in graceful degradation @@ -470,6 +477,7 @@ func GetAffectedComponentsWithOptions(args *DescribeAffectedCmdArgs, errOptions // //nolint:gocritic // Forward the value-based options consistently with the describe entry points below. func dispatchAffectedComponentsWithOptions(args *DescribeAffectedCmdArgs, errOptions DescribeStacksErrorOptions) ([]schema.Affected, error) { + ref, sha := affectedComparisonRevision(args) switch { case args.RepoPath != "": affectedList, _, _, _, err := ExecuteDescribeAffectedWithTargetRepoPathWithOptions( @@ -490,8 +498,8 @@ func dispatchAffectedComponentsWithOptions(args *DescribeAffectedCmdArgs, errOpt case args.CloneTargetRef: affectedList, _, _, _, err := ExecuteDescribeAffectedWithTargetRefCloneWithOptions( args.CLIConfig, - args.Ref, - args.SHA, + ref, + sha, args.SSHKeyPath, args.SSHKeyPassword, args.IncludeSpaceliftAdminStacks, @@ -509,8 +517,8 @@ func dispatchAffectedComponentsWithOptions(args *DescribeAffectedCmdArgs, errOpt default: affectedList, _, _, _, err := ExecuteDescribeAffectedWithTargetRefCheckoutWithOptions( args.CLIConfig, - args.Ref, - args.SHA, + ref, + sha, args.TargetBranch, args.IncludeSpaceliftAdminStacks, args.IncludeSettings, @@ -526,3 +534,17 @@ func dispatchAffectedComponentsWithOptions(args *DescribeAffectedCmdArgs, errOpt return affectedList, err } } + +// affectedComparisonRevision returns the Ref and SHA to compare against. A non-empty Base +// takes precedence over both, matching the --base flag: a commit SHA selects that commit +// (keeping Ref as the clone target), and anything else selects that ref. +func affectedComparisonRevision(args *DescribeAffectedCmdArgs) (string, string) { + switch { + case args.Base == "": + return args.Ref, args.SHA + case ci.IsCommitSHA(args.Base): + return args.Ref, args.Base + default: + return args.Base, "" + } +} diff --git a/internal/exec/terraform_affected_host_test.go b/internal/exec/terraform_affected_host_test.go index 379f178edb2..d8ae9374d5b 100644 --- a/internal/exec/terraform_affected_host_test.go +++ b/internal/exec/terraform_affected_host_test.go @@ -7,7 +7,9 @@ import ( "github.com/agiledragon/gomonkey/v2" "github.com/go-git/go-git/v5/plumbing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/auth" "github.com/cloudposse/atmos/pkg/schema" "github.com/cloudposse/atmos/tests" @@ -485,3 +487,53 @@ func BenchmarkGetAffectedComponents(b *testing.B) { _, _ = GetAffectedComponents(args) } } + +func TestAffectedComparisonRevision(t *testing.T) { + const commit = "0123456789abcdef0123456789abcdef01234567" + cases := []struct { + name string + args DescribeAffectedCmdArgs + ref, sha string + }{ + {name: "no base keeps ref and sha", args: DescribeAffectedCmdArgs{Ref: "refs/heads/main", SHA: commit}, ref: "refs/heads/main", sha: commit}, + {name: "ref base overrides ref and sha", args: DescribeAffectedCmdArgs{Base: "refs/heads/release", Ref: "refs/heads/main", SHA: commit}, ref: "refs/heads/release"}, + {name: "sha base overrides sha and keeps clone ref", args: DescribeAffectedCmdArgs{Base: commit, Ref: "refs/heads/main", SHA: "fedcba9876543210fedcba9876543210fedcba98"}, ref: "refs/heads/main", sha: commit}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + ref, sha := affectedComparisonRevision(&tc.args) + assert.Equal(t, tc.ref, ref) + assert.Equal(t, tc.sha, sha) + }) + } +} + +func TestGetAffectedComponentsNilArgs(t *testing.T) { + _, err := GetAffectedComponents(nil) + require.ErrorIs(t, err, errUtils.ErrNilParam) +} + +func TestGetAffectedComponentsHonorsErrorMode(t *testing.T) { + tests.SkipIfGomonkeyUnsafe(t, "uses gomonkey.ApplyFunc to observe GetAffectedComponentsWithOptions") + + for mode, lenient := range map[string]bool{"": false, "strict": false, "warn": true, "silent": true} { + t.Run("mode="+mode, func(t *testing.T) { + var got DescribeStacksErrorOptions + called := false + patch := gomonkey.ApplyFunc(GetAffectedComponentsWithOptions, + func(_ *DescribeAffectedCmdArgs, errOptions DescribeStacksErrorOptions) ([]schema.Affected, error) { + called, got = true, errOptions + return nil, nil + }) + defer patch.Reset() + + _, err := GetAffectedComponents(&DescribeAffectedCmdArgs{ErrorMode: mode}) + require.NoError(t, err) + if !called { + t.Skip("gomonkey function mocking failed (likely due to compiler optimizations or platform issues)") + } + assert.Equal(t, lenient, got.OnError == OnErrorWarn) + assert.Equal(t, lenient, got.OnWarning != nil) + }) + } +} From d9126f20971ddca4892c7b462110f3b73a2aa385 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 9 Nov 2026 21:08:07 -0600 Subject: [PATCH 09/25] fix(terraform): clean the workspace file in the component's TF_DATA_DIR Workspace cleanup read TF_DATA_DIR only from the Atmos process, so a TF_DATA_DIR set through the component env left a stale .terraform/environment where Terraform actually looks. Cleanup now resolves TF_DATA_DIR from ComponentEnvList first. Co-Authored-By: Claude Opus 5.5 --- pkg/component/terraform/execute_init.go | 43 ++++++++++++++++--------- pkg/component/terraform/plan_diff.go | 2 +- pkg/component/terraform/shell.go | 2 +- pkg/component/terraform/utils.go | 4 +-- pkg/component/terraform/utils_test.go | 24 +++++++++++++- 5 files changed, 55 insertions(+), 20 deletions(-) diff --git a/pkg/component/terraform/execute_init.go b/pkg/component/terraform/execute_init.go index 82760f277d7..e7f5a19a668 100644 --- a/pkg/component/terraform/execute_init.go +++ b/pkg/component/terraform/execute_init.go @@ -173,7 +173,7 @@ func (e *Executor) executeTerraformInitForced(atmosConfig *schema.AtmosConfigura func prepareInitExecution(ctx context.Context, writers provisioner.OutputWriters, atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath string) (string, error) { _, isWorkdir := info.ComponentSection[provWorkdir.WorkdirPathKey].(string) if !isWorkdir { - cleanTerraformWorkspace(*atmosConfig, componentPath) + cleanTerraformWorkspace(*atmosConfig, componentPath, info.ComponentEnvList) } if ctx == nil { @@ -555,25 +555,23 @@ func (e *Executor) createWorkspaceFallback(atmosConfig *schema.AtmosConfiguratio // helping Terraform identify the active workspace context for managing your infrastructure. // We delete the file to prevent the Terraform prompt asking to select the default or the // previously used workspace. This happens when different backends are used for the same component. -// -//nolint:nestif // Preserve workspace cleanup and Windows retry behavior during extraction. func CleanTerraformWorkspace(componentPath string) { defer perf.Track(nil, "terraform.CleanTerraformWorkspace")() - // Get `TF_DATA_DIR` ENV variable, default to `.terraform` if not set - //nolint:forbidigo // TF_DATA_DIR is a Terraform convention, not an Atmos configuration variable. - tfDataDir := os.Getenv("TF_DATA_DIR") - if tfDataDir == "" { - tfDataDir = ".terraform" - } + CleanTerraformWorkspaceWithEnv(componentPath, nil) +} - // Convert relative path to absolute - if !filepath.IsAbs(tfDataDir) { - tfDataDir = filepath.Join(componentPath, tfDataDir) - } +// CleanTerraformWorkspaceWithEnv is CleanTerraformWorkspace for a subprocess environment: +// a TF_DATA_DIR in envList (typically info.ComponentEnvList) wins over this process's own +// environment, so the cleanup targets the data directory Terraform will actually use. +// +//nolint:nestif // Preserve workspace cleanup and Windows retry behavior during extraction. +func CleanTerraformWorkspaceWithEnv(componentPath string, envList []string) { + defer perf.Track(nil, "terraform.CleanTerraformWorkspaceWithEnv")() - // Ensure the path is cleaned properly - tfDataDir = filepath.Clean(tfDataDir) + // Resolve TF_DATA_DIR from envList, then this process's environment, defaulting to + // `.terraform`; a relative value is joined to componentPath and cleaned. + tfDataDir := autoinit.DataDir(componentPath, envListLookup(envList)) // Construct the full file path filePath := filepath.Join(tfDataDir, "environment") @@ -598,3 +596,18 @@ func CleanTerraformWorkspace(componentPath string) { log.Debug("Error checking Terraform environment file.", "file", filePath, "error", err) } } + +// envListLookup looks keys up in a subprocess environment list. The last entry for a key wins, +// as it does for the launched process, and an explicit empty value is reported as present. +func envListLookup(envList []string) func(string) (string, bool) { + return func(key string) (string, bool) { + prefix := key + "=" + value, found := "", false + for _, entry := range envList { + if strings.HasPrefix(entry, prefix) { + value, found = entry[len(prefix):], true + } + } + return value, found + } +} diff --git a/pkg/component/terraform/plan_diff.go b/pkg/component/terraform/plan_diff.go index e8d21e9011e..45b5d180891 100644 --- a/pkg/component/terraform/plan_diff.go +++ b/pkg/component/terraform/plan_diff.go @@ -329,7 +329,7 @@ func (s *Operations) runTerraformInit(atmosConfig *schema.AtmosConfiguration, di } // Clean terraform workspace to prevent workspace selection prompt - CleanTerraformWorkspace(dir) + CleanTerraformWorkspaceWithEnv(dir, info.ComponentEnvList) // Create a copy of the info struct with init subcommand initInfo := *info diff --git a/pkg/component/terraform/shell.go b/pkg/component/terraform/shell.go index a89e8436910..7e6440068f5 100644 --- a/pkg/component/terraform/shell.go +++ b/pkg/component/terraform/shell.go @@ -201,7 +201,7 @@ func (e *Executor) executeShellLifecycle(atmosConfig *schema.AtmosConfiguration, // Mirror prepareInitExecution's non-workdir cleanup of .terraform/environment so // Terraform doesn't prompt for workspace selection. Skipped for workdir components. if _, isWorkdir := info.ComponentSection[provWorkdir.WorkdirPathKey].(string); !isWorkdir { - cleanTerraformWorkspace(*atmosConfig, cfg.componentPath) + cleanTerraformWorkspace(*atmosConfig, cfg.componentPath, info.ComponentEnvList) } if err := e.shellInitFn(atmosConfig, info, cfg.componentPath, cfg.varFile); err != nil { return err diff --git a/pkg/component/terraform/utils.go b/pkg/component/terraform/utils.go index cbee5047ebf..08b06d1b09e 100644 --- a/pkg/component/terraform/utils.go +++ b/pkg/component/terraform/utils.go @@ -49,8 +49,8 @@ func checkTerraformConfig(atmosConfig schema.AtmosConfiguration) error { // cleanTerraformWorkspace delegates workspace cleanup to the Terraform component. // //nolint:gocritic // Preserve the existing value-based workspace cleanup contract. -func cleanTerraformWorkspace(_ schema.AtmosConfiguration, componentPath string) { - CleanTerraformWorkspace(componentPath) +func cleanTerraformWorkspace(_ schema.AtmosConfiguration, componentPath string, envList []string) { + CleanTerraformWorkspaceWithEnv(componentPath, envList) } // isTerraformCurrentWorkspace reports whether the given workspace name matches the workspace diff --git a/pkg/component/terraform/utils_test.go b/pkg/component/terraform/utils_test.go index 7a7a3606d3e..fdc913df966 100644 --- a/pkg/component/terraform/utils_test.go +++ b/pkg/component/terraform/utils_test.go @@ -778,7 +778,7 @@ func TestCleanTerraformWorkspace(t *testing.T) { // Test the function. config := schema.AtmosConfiguration{} - cleanTerraformWorkspace(config, testDir) + cleanTerraformWorkspace(config, testDir, nil) // Verify the result. envFilePath := filepath.Join(tfDataDir, "environment") @@ -792,6 +792,28 @@ func TestCleanTerraformWorkspace(t *testing.T) { } } +// TestCleanTerraformWorkspaceHonorsComponentEnv verifies that a TF_DATA_DIR supplied by the +// component environment wins over the process environment, and that the process-level data +// directory is left untouched in that case. +func TestCleanTerraformWorkspaceHonorsComponentEnv(t *testing.T) { + testDir := t.TempDir() + t.Setenv("TF_DATA_DIR", "process-tf-dir") + + componentEnvFile := filepath.Join(testDir, "component-tf-dir", "environment") + processEnvFile := filepath.Join(testDir, "process-tf-dir", "environment") + for _, envFile := range []string{componentEnvFile, processEnvFile} { + require.NoError(t, os.MkdirAll(filepath.Dir(envFile), 0o755)) + require.NoError(t, os.WriteFile(envFile, []byte("test-workspace"), 0o644)) + } + + cleanTerraformWorkspace(schema.AtmosConfiguration{}, testDir, []string{"TF_DATA_DIR=component-tf-dir"}) + + _, statErr := os.Stat(componentEnvFile) + assert.True(t, os.IsNotExist(statErr), "component TF_DATA_DIR environment file should be removed") + _, statErr = os.Stat(processEnvFile) + assert.NoError(t, statErr, "process TF_DATA_DIR environment file should be untouched") +} + func TestShouldProcessStacks(t *testing.T) { tests := []struct { name string From 12ebad93999b4b68b1c10201633e07a293971ea5 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 9 Nov 2026 21:10:27 -0600 Subject: [PATCH 10/25] fix(terraform): keep sensitive root variables out of test varfiles test.vars for variables declared sensitive = true were written to the varfile unless their value matched a registered secret. They now go through TF_VAR_ env entries like other sensitive vars. The module is also re-inspected from the provisioned directory (JIT sources, workdirs) so sensitivity detection sees the module Terraform will run. Co-Authored-By: Claude Opus 5.5 --- pkg/component/terraform/execute_prepare.go | 55 +++++++++++--- .../terraform/generate_varfile_test.go | 72 +++++++++++++++++++ 2 files changed, 119 insertions(+), 8 deletions(-) diff --git a/pkg/component/terraform/execute_prepare.go b/pkg/component/terraform/execute_prepare.go index f6ff262a816..05ba101db50 100644 --- a/pkg/component/terraform/execute_prepare.go +++ b/pkg/component/terraform/execute_prepare.go @@ -63,6 +63,9 @@ func (e *Executor) resolveAndProvisionComponentPath(ctx context.Context, writers if !componentPathExists { componentPathExists = u.FileOrDirExists(componentPath) } + if componentPathExists { + refreshTerraformModuleInfo(info, componentPath) + } if !componentPathExists { basePath, _ := u.GetComponentBasePath(atmosConfig, cfg.TerraformComponentType) @@ -322,6 +325,28 @@ func terraformSensitiveVarKeys(info *schema.ConfigAndStacksInfo) map[string]bool return keys } +// refreshTerraformModuleInfo reloads component_info.terraform_config from the provisioned +// component directory when stack processing inspected a different or missing directory, as +// happens for JIT sources and workdirs. Sensitive-variable detection then reflects the module +// Terraform will actually run. A module that cannot be inspected leaves the existing value. +func refreshTerraformModuleInfo(info *schema.ConfigAndStacksInfo, componentPath string) { + componentInfo, ok := info.ComponentSection["component_info"].(map[string]any) + if !ok { + return + } + module, _ := componentInfo["terraform_config"].(*tfconfig.Module) + recordedPath, _ := componentInfo[cfg.ComponentPathSectionName].(string) + if module != nil && filepath.Clean(recordedPath) == filepath.Clean(componentPath) { + return + } + refreshed, diags := tfconfig.LoadModule(componentPath) + if diags.HasErrors() || refreshed == nil { + log.Debug("Could not inspect the provisioned Terraform module", "path", componentPath, "error", diags.Err()) + return + } + componentInfo["terraform_config"] = refreshed +} + // RejectComputedTerraformVars ensures display-only degraded values cannot cross // into Terraform through either tfvars JSON or TF_VAR_ environment variables. func RejectComputedTerraformVars(vars map[string]any) error { @@ -443,22 +468,36 @@ func HasTerraformTestVars(info *schema.ConfigAndStacksInfo) bool { func DiskSafeTerraformTestVars(info *schema.ConfigAndStacksInfo) map[string]any { defer perf.Track(nil, "terraform.DiskSafeTerraformTestVars")() - vars := TerraformTestVars(info) - if len(vars) == 0 { - return nil - } - safe, _ := tfvars.Partition(vars, atmosio.ContainsSecret) + safe, _ := partitionTerraformTestVars(info) return safe } // terraformTestSecretVars selects test variables that must be passed through the subprocess environment. func terraformTestSecretVars(info *schema.ConfigAndStacksInfo) map[string]any { + _, secret := partitionTerraformTestVars(info) + return secret +} + +// partitionTerraformTestVars splits test.vars into varfile-safe and environment-only values. +// Values containing a registered secret are environment-only, and so are values for root +// variables declared `sensitive = true`, matching ComputeTerraformSecretVarKeys (including +// its masking-disabled compatibility behavior). +func partitionTerraformTestVars(info *schema.ConfigAndStacksInfo) (map[string]any, map[string]any) { vars := TerraformTestVars(info) if len(vars) == 0 { - return nil + return nil, nil } - _, secret := tfvars.Partition(vars, atmosio.ContainsSecret) - return secret + safe, secret := tfvars.Partition(vars, atmosio.ContainsSecret) + if !atmosio.MaskingEnabled() { + return safe, secret + } + for k := range terraformSensitiveVarKeys(info) { + if v, supplied := safe[k]; supplied { + secret[k] = v + delete(safe, k) + } + } + return safe, secret } // TerraformTestSecretVarEnv encodes secret-bearing test variables as TF_VAR_ environment entries. diff --git a/pkg/component/terraform/generate_varfile_test.go b/pkg/component/terraform/generate_varfile_test.go index 3031e725182..6e853b786db 100644 --- a/pkg/component/terraform/generate_varfile_test.go +++ b/pkg/component/terraform/generate_varfile_test.go @@ -19,6 +19,7 @@ import ( schema "github.com/cloudposse/atmos/pkg/schema" store "github.com/cloudposse/atmos/pkg/store" tfoutput "github.com/cloudposse/atmos/pkg/terraform/output" + tfconfig "github.com/hashicorp/terraform-config-inspect/tfconfig" assert "github.com/stretchr/testify/assert" require "github.com/stretchr/testify/require" gomock "go.uber.org/mock/gomock" @@ -424,3 +425,74 @@ func TestSecretReferenceReachesTerraformPlan(t *testing.T) { assert.Contains(t, stderr.String(), "diagnostic credential: "+iolib.MaskReplacement) assert.NotContains(t, stdout.String()+stderr.String(), nativeReferenceSecret) } + +// writeSensitiveModule writes a root module declaring one sensitive and one plain input. +func writeSensitiveModule(t *testing.T, dir string) { + t.Helper() + require.NoError(t, os.MkdirAll(dir, 0o755)) + module := "variable \"db_password\" {\n sensitive = true\n}\n\nvariable \"region\" {}\n" + require.NoError(t, os.WriteFile(filepath.Join(dir, "variables.tf"), []byte(module), 0o600)) +} + +func TestRefreshTerraformModuleInfoLoadsProvisionedModule(t *testing.T) { + provisioned := filepath.Join(t.TempDir(), "workdir") + writeSensitiveModule(t, provisioned) + // Stack processing inspected the not-yet-provisioned source directory and found nothing. + info := &schema.ConfigAndStacksInfo{ComponentSection: map[string]any{ + "component_info": map[string]any{ + cfg.ComponentPathSectionName: filepath.Join(t.TempDir(), "missing"), + "terraform_config": nil, + }, + }} + require.Empty(t, terraformSensitiveVarKeys(info)) + + refreshTerraformModuleInfo(info, provisioned) + + assert.Equal(t, map[string]bool{"db_password": true}, terraformSensitiveVarKeys(info)) +} + +func TestRefreshTerraformModuleInfoKeepsInspectedModule(t *testing.T) { + componentPath := t.TempDir() + writeSensitiveModule(t, componentPath) + inspected := &tfconfig.Module{Variables: map[string]*tfconfig.Variable{}} + componentInfo := map[string]any{cfg.ComponentPathSectionName: componentPath, "terraform_config": inspected} + info := &schema.ConfigAndStacksInfo{ComponentSection: map[string]any{"component_info": componentInfo}} + + refreshTerraformModuleInfo(info, componentPath) + + assert.Same(t, inspected, componentInfo["terraform_config"], "a module inspected at the same path is not reloaded") +} + +func TestRefreshTerraformModuleInfoKeepsValueWhenUninspectable(t *testing.T) { + componentInfo := map[string]any{cfg.ComponentPathSectionName: "elsewhere", "terraform_config": nil} + info := &schema.ConfigAndStacksInfo{ComponentSection: map[string]any{"component_info": componentInfo}} + + refreshTerraformModuleInfo(info, filepath.Join(t.TempDir(), "absent")) + + assert.Nil(t, componentInfo["terraform_config"]) +} + +func TestTerraformTestVarsKeepSensitiveRootVariablesOutOfVarfile(t *testing.T) { + componentPath := t.TempDir() + writeSensitiveModule(t, componentPath) + module, diags := tfconfig.LoadModule(componentPath) + require.False(t, diags.HasErrors()) + info := &schema.ConfigAndStacksInfo{ + SubCommand: "test", + ComponentVarsSection: map[string]any{"db_password": "component-value", "region": "us-east-1"}, + ComponentSection: map[string]any{ + "component_info": map[string]any{cfg.ComponentPathSectionName: componentPath, "terraform_config": module}, + cfg.TestSectionName: map[string]any{ + cfg.VarsSectionName: map[string]any{"db_password": "unregistered-test-value", "region": "us-west-2"}, + }, + }, + } + require.True(t, iolib.MaskingEnabled(), "masking must be enabled for declaration-aware partitioning") + + assert.Equal(t, map[string]any{"region": "us-west-2"}, diskSafeTerraformTestVars(info)) + // The colliding component value is dropped too, so the test value's TF_VAR_ entry wins. + assert.NotContains(t, diskSafeVars(info), "db_password") + env, err := terraformTestSecretVarEnv(info) + require.NoError(t, err) + assert.Equal(t, []string{"TF_VAR_db_password=unregistered-test-value"}, env) +} From 17c557d6c88800141e64dad4e0e08fcf565afa4c Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 9 Nov 2026 21:14:04 -0600 Subject: [PATCH 11/25] fix(terraform): pass the execution context through artifact generation GenerateArtifacts dropped the execution context, so provider contributors and the required_providers generator ran on context.Background() and a cancellation did not stop later stages. Generation now uses the execution context and checks it between stages. Co-Authored-By: Claude Opus 5.5 --- pkg/component/terraform/provider.go | 48 ++++++++++++++++++------ pkg/component/terraform/provider_test.go | 20 ++++++++++ 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/pkg/component/terraform/provider.go b/pkg/component/terraform/provider.go index 897afe4048c..a109470695b 100644 --- a/pkg/component/terraform/provider.go +++ b/pkg/component/terraform/provider.go @@ -204,7 +204,7 @@ func reconcileExecutionRequest(ctx *component.ExecutionContext) (schema.ConfigAn func (e *Executor) GenerateArtifacts(ctx *component.ExecutionContext) error { defer perf.Track(nil, "terraform.Executor.GenerateArtifacts")() info := ctx.ConfigAndStacksInfo - return e.GenerateConfigFiles(ctx.AtmosConfig, &info, ctx.WorkingDir) + return e.GenerateConfigFilesWithContext(ctx.GoContext(), ctx.AtmosConfig, &info, ctx.WorkingDir) } // Services supplies the shared Atmos facilities used by one Terraform executor. @@ -348,17 +348,33 @@ func generateComponentBackendConfig(backendType string, backendConfig map[string func (s *Operations) GenerateConfigFiles(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { defer perf.Track(atmosConfig, "terraform.Operations.GenerateConfigFiles")() - if err := GenerateBackendConfig(atmosConfig, info, workingDir); err != nil { - return err + return s.GenerateConfigFilesWithContext(context.Background(), atmosConfig, info, workingDir) +} + +// GenerateConfigFilesWithContext is GenerateConfigFiles bound to ctx: contributors and +// generators receive ctx, and a cancellation stops generation before the next stage writes. +func (s *Operations) GenerateConfigFilesWithContext(ctx context.Context, atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { + defer perf.Track(atmosConfig, "terraform.Operations.GenerateConfigFilesWithContext")() + + if ctx == nil { + ctx = context.Background() } - if err := s.host.GenerateFilesForComponent(atmosConfig, info, workingDir); err != nil { - return err + stages := []func() error{ + func() error { return GenerateBackendConfig(atmosConfig, info, workingDir) }, + func() error { return s.host.GenerateFilesForComponent(atmosConfig, info, workingDir) }, + func() error { return generateProviderOverridesWithContext(ctx, atmosConfig, info, workingDir) }, + // Generate required_providers (terraform_override.tf.json) for version pinning (DEV-3124). + func() error { return generateRequiredProvidersWithContext(ctx, atmosConfig, info, workingDir) }, } - if err := GenerateProviderOverrides(atmosConfig, info, workingDir); err != nil { - return err + for _, stage := range stages { + if err := ctx.Err(); err != nil { + return err + } + if err := stage(); err != nil { + return err + } } - // Generate required_providers (terraform_override.tf.json) for version pinning (DEV-3124). - return GenerateRequiredProviders(atmosConfig, info, workingDir) + return nil } // GenerateBackendConfig delegates backend-file generation using the resolved configuration and working directory. @@ -373,10 +389,15 @@ func GenerateBackendConfig(atmosConfig *schema.AtmosConfiguration, info *schema. func GenerateProviderOverrides(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { defer perf.Track(atmosConfig, "terraform.GenerateProviderOverrides")() + return generateProviderOverridesWithContext(context.Background(), atmosConfig, info, workingDir) +} + +// generateProviderOverridesWithContext is GenerateProviderOverrides with ctx passed to contributors. +func generateProviderOverridesWithContext(ctx context.Context, atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { // Let registered provider-config contributors (e.g. an emulator binding) deep-merge // provider fragments UNDER the explicit `providers:` section before generation. genCtx := generator.NewGeneratorContext(atmosConfig, info, workingDir) - if merged, err := generator.ApplyProviderContributors(context.Background(), genCtx); err != nil { + if merged, err := generator.ApplyProviderContributors(ctx, genCtx); err != nil { return err } else if merged != nil { info.ComponentProvidersSection = merged @@ -405,6 +426,11 @@ func GenerateProviderOverrides(atmosConfig *schema.AtmosConfiguration, info *sch func GenerateRequiredProviders(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { defer perf.Track(atmosConfig, "exec.GenerateRequiredProviders")() + return generateRequiredProvidersWithContext(context.Background(), atmosConfig, info, workingDir) +} + +// generateRequiredProvidersWithContext is GenerateRequiredProviders with ctx passed to the generator. +func generateRequiredProvidersWithContext(ctx context.Context, atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, workingDir string) error { // Skip if no required_version or required_providers configured, removing a stale file // so an earlier stack's version pins don't apply to this one. if info.RequiredVersion == "" && len(info.RequiredProviders) == 0 { @@ -426,5 +452,5 @@ func GenerateRequiredProviders(atmosConfig *schema.AtmosConfiguration, info *sch genCtx := generator.NewGeneratorContext(atmosConfig, info, workingDir) // Generate and write using the generator package. - return generator.Generate(context.Background(), required_providers.Name, genCtx, generator.NewFileWriter()) + return generator.Generate(ctx, required_providers.Name, genCtx, generator.NewFileWriter()) } diff --git a/pkg/component/terraform/provider_test.go b/pkg/component/terraform/provider_test.go index a9bc3ec1da7..0d1662ce1ea 100644 --- a/pkg/component/terraform/provider_test.go +++ b/pkg/component/terraform/provider_test.go @@ -276,6 +276,26 @@ func TestOperationsGenerateArtifactsInOrder(t *testing.T) { require.NoDirExists(t, filepath.Join(workingDir, ".terraform"), "generation must not initialize the tool") } +func TestOperationsGenerateArtifactsStopsAfterCancellation(t *testing.T) { + t.Parallel() + + host := NewMockOperationServices(gomock.NewController(t)) + operations := NewOperations(host) + config, info := operationArtifactConfig() + workingDir := t.TempDir() + ctx, cancel := context.WithCancel(t.Context()) + host.EXPECT().GenerateFilesForComponent(config, info, workingDir).DoAndReturn(func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, string) error { + cancel() + return nil + }) + + err := operations.GenerateConfigFilesWithContext(ctx, config, info, workingDir) + require.ErrorIs(t, err, context.Canceled) + require.FileExists(t, filepath.Join(workingDir, "backend.tf.json"), "stages before cancellation still run") + require.NoFileExists(t, filepath.Join(workingDir, "providers_override.tf.json")) + require.NoFileExists(t, filepath.Join(workingDir, "terraform_override.tf.json")) +} + func TestOperationsArtifactFailureStopsLaterStages(t *testing.T) { t.Parallel() From 87abded371c5830f93943162adef20a7f23b9c7c Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 9 Nov 2026 21:49:04 -0600 Subject: [PATCH 12/25] docs(terraform): state that capture writers receive unmasked output WithStdoutCapture/WithStderrCapture tee the raw subprocess output; only the terminal copy passes through the masker. The comments claimed otherwise. Co-Authored-By: Claude Opus 5.5 --- internal/exec/shell_utils.go | 4 ++-- pkg/component/execution_options.go | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/internal/exec/shell_utils.go b/internal/exec/shell_utils.go index 268217dddd8..40cd23a3263 100644 --- a/internal/exec/shell_utils.go +++ b/internal/exec/shell_utils.go @@ -84,14 +84,14 @@ func execMetadataOutputCaptureFromOpts(opts ...ShellCommandOption) (io.Writer, i return component.MetadataOutputCaptureFromOptions(opts...) } -// WithStdoutCapture tees masked subprocess stdout to the supplied writer. +// WithStdoutCapture tees raw (unmasked) subprocess stdout to the supplied writer. func WithStdoutCapture(w io.Writer) ShellCommandOption { defer perf.Track(nil, "exec.WithStdoutCapture")() return component.WithStdoutCapture(w) } -// WithStderrCapture tees masked subprocess stderr to the supplied writer. +// WithStderrCapture tees raw (unmasked) subprocess stderr to the supplied writer. func WithStderrCapture(w io.Writer) ShellCommandOption { defer perf.Track(nil, "exec.WithStderrCapture")() diff --git a/pkg/component/execution_options.go b/pkg/component/execution_options.go index ac9ab55fbeb..b2147c65675 100644 --- a/pkg/component/execution_options.go +++ b/pkg/component/execution_options.go @@ -161,7 +161,8 @@ func MetadataOutputCaptureFromOptions(opts ...ExecutionOption) (io.Writer, io.Wr } // WithStdoutCapture returns an ExecutionOption that tees stdout to the provided writer. -// The captured output includes secret masking (post-MaskWriter). +// The captured output is the raw, unmasked subprocess output; only the terminal copy is +// masked. Callers that persist or display captured output must mask it themselves. func WithStdoutCapture(w io.Writer) ExecutionOption { defer perf.Track(nil, "component.WithStdoutCapture")() @@ -171,7 +172,8 @@ func WithStdoutCapture(w io.Writer) ExecutionOption { } // WithStderrCapture returns an ExecutionOption that tees stderr to the provided writer. -// The captured output includes secret masking (post-MaskWriter). +// The captured output is the raw, unmasked subprocess output; only the terminal copy is +// masked. Callers that persist or display captured output must mask it themselves. func WithStderrCapture(w io.Writer) ExecutionOption { defer perf.Track(nil, "component.WithStderrCapture")() From cd6442d7e9a8db2addaf903ee37beac97de70ff3 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Wed, 11 Nov 2026 11:20:55 -0600 Subject: [PATCH 13/25] fix(terraform): invalidate caches for stack aliases Clear execution snapshots under both the canonical stack and the supplied alias so later commands see current outputs. Strengthen tests for execution, cancellation, and lifecycle behavior. --- pkg/component/terraform/execute.go | 13 +++- .../execute_helpers_pipeline_test.go | 63 +++++++++++++++++-- .../terraform/generate_backend_test.go | 11 +++- pkg/component/terraform/mocks_test.go | 17 +++++ pkg/component/terraform/shell_test.go | 7 +++ 5 files changed, 101 insertions(+), 10 deletions(-) diff --git a/pkg/component/terraform/execute.go b/pkg/component/terraform/execute.go index 2908599c5e0..7a1206ac950 100644 --- a/pkg/component/terraform/execute.go +++ b/pkg/component/terraform/execute.go @@ -233,8 +233,7 @@ func (e *Executor) execute(info schema.ConfigAndStacksInfo, opts ...component.Ex // A Terraform command can create, change, or remove state, and a failed apply can // still persist partial changes. Drop any preflight snapshot either way so a later // graph node (including one in a keep-going run) reads the current outputs. - invalidateTerraformStateCache(info.Stack, info.ComponentFromArg) - tfoutput.InvalidateComponentOutputs(info.Stack, info.ComponentFromArg) + invalidateTerraformExecutionCaches(&info) captureExecMetadataSync(&atmosConfig, originalSubCommand, &info, execMetadataSyncParams{ Cmd: component.InvokingCommandFromOptions(opts...), @@ -245,6 +244,16 @@ func (e *Executor) execute(info schema.ConfigAndStacksInfo, opts ...component.Ex return err } +// invalidateTerraformExecutionCaches clears snapshots under both execution identities. +func invalidateTerraformExecutionCaches(info *schema.ConfigAndStacksInfo) { + invalidateTerraformStateCache(info.Stack, info.ComponentFromArg) + tfoutput.InvalidateComponentOutputs(info.Stack, info.ComponentFromArg) + if info.StackFromArg != "" && info.StackFromArg != info.Stack { + invalidateTerraformStateCache(info.StackFromArg, info.ComponentFromArg) + tfoutput.InvalidateComponentOutputs(info.StackFromArg, info.ComponentFromArg) + } +} + // captureExecMetadataSync reports an execution record to Atmos Pro for the // synchronous allowlist (terraform plan/apply/deploy), blocking briefly per // proexec.CaptureSync's own configurable timeout. No-op for every other diff --git a/pkg/component/terraform/execute_helpers_pipeline_test.go b/pkg/component/terraform/execute_helpers_pipeline_test.go index 0fef4e2eeaa..3ee37ea7c19 100644 --- a/pkg/component/terraform/execute_helpers_pipeline_test.go +++ b/pkg/component/terraform/execute_helpers_pipeline_test.go @@ -15,6 +15,7 @@ import ( "os" osexec "os/exec" "path/filepath" + "strconv" "strings" "testing" "time" @@ -715,7 +716,9 @@ var _ = component.ExecutionOptions{StdoutCapture: nil, StderrCapture: nil} func TestExecuteShellCommandWithRetry_CancellationStopsBackoff(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() - delay := 100 * time.Millisecond + delay := 10 * time.Second + waiting := make(chan struct{}) + ctx = &backoffSignalContext{Context: ctx, waiting: waiting} info := schema.ConfigAndStacksInfo{ ComponentRetrySection: &schema.RetryConfig{ MaxAttempts: intPtr(2), @@ -730,13 +733,63 @@ func TestExecuteShellCommandWithRetry_CancellationStopsBackoff(t *testing.T) { capture := applyOptsForTest(opts) _, err := capture.StderrCapture.Write([]byte("502 Bad Gateway")) require.NoError(t, err) - cancel() return errors.New("transient gateway failure") } - err := ExecuteShellCommandWithRetry(&schema.AtmosConfiguration{}, &info, "plan", invoke, - component.WithProcessContext(ctx)) + result := make(chan error, 1) + go func() { + result <- ExecuteShellCommandWithRetry(&schema.AtmosConfiguration{}, &info, "plan", invoke, component.WithProcessContext(ctx)) + }() + select { + case <-waiting: + case <-time.After(2 * time.Second): + t.Fatal("retry did not enter backoff") + } + cancel() + select { + case err := <-result: + require.ErrorIs(t, err, context.Canceled) + case <-time.After(2 * time.Second): + t.Fatal("cancellation did not interrupt backoff") + } - require.ErrorIs(t, err, context.Canceled) assert.Equal(t, 1, called, "cancellation during retry backoff must prevent another command") } + +// backoffSignalContext signals when the retry loop starts selecting on Done. +type backoffSignalContext struct { + context.Context + waiting chan struct{} +} + +func (c *backoffSignalContext) Done() <-chan struct{} { + close(c.waiting) + return c.Context.Done() +} + +func TestExecutorExecute_InvokesMainCommandOnce(t *testing.T) { + root := t.TempDir() + exe, err := os.Executable() + require.NoError(t, err) + write := func(name, content string) { + path := filepath.Join(root, name) + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o755)) + require.NoError(t, os.WriteFile(path, []byte(content), 0o600)) + } + write("atmos.yaml", "base_path: .\ncomponents:\n terraform:\n command: "+strconv.Quote(exe)+"\n base_path: components/terraform\nstacks:\n base_path: stacks\n included_paths: ['**/*.yaml']\n name_template: '{{ .vars.stage }}'\nsettings:\n telemetry:\n enabled: false\n") + write("stacks/test.yaml", "vars:\n stage: test\ncomponents:\n terraform:\n app:\n backend_type: http\n") + write("components/terraform/app/main.tf", "terraform {}\n") + counter := filepath.Join(root, "counter") + t.Setenv("ATMOS_BASE_PATH", root) + t.Setenv("ATMOS_CLI_CONFIG_PATH", root) + t.Setenv("_ATMOS_TEST_COUNTER_FILE", counter) + t.Setenv("_ATMOS_TEST_ARGS_FILE", filepath.Join(root, "args")) + t.Chdir(root) + err = testExecutor(t).Execute(&component.ExecutionContext{Context: t.Context(), ConfigAndStacksInfo: schema.ConfigAndStacksInfo{ + Stack: "test", ComponentType: "terraform", ComponentFromArg: "app", SubCommand: "plan", SkipInit: true, ProcessTemplates: true, ProcessFunctions: true, + }}) + require.NoError(t, err) + data, err := os.ReadFile(counter) + require.NoError(t, err) + assert.Equal(t, "x", string(data)) +} diff --git a/pkg/component/terraform/generate_backend_test.go b/pkg/component/terraform/generate_backend_test.go index 05192338016..7037b1c4e7a 100644 --- a/pkg/component/terraform/generate_backend_test.go +++ b/pkg/component/terraform/generate_backend_test.go @@ -174,12 +174,14 @@ func TestWriteBackendConfigFile(t *testing.T) { info *schema.ConfigAndStacksInfo config map[string]any wantPath string + wantAbsent string wantContains string }{ { - name: "dry-run skips writing", - info: &schema.ConfigAndStacksInfo{FinalComponent: "vpc", DryRun: true}, - config: map[string]any{"terraform": map[string]any{"backend": map[string]any{"s3": map[string]any{"bucket": "test-bucket"}}}}, + name: "dry-run skips writing", + info: &schema.ConfigAndStacksInfo{FinalComponent: "vpc", DryRun: true, ComponentSection: map[string]any{provWorkdir.WorkdirPathKey: filepath.Join(tempDir, "dry-run")}}, + wantAbsent: filepath.Join(tempDir, "dry-run", "backend.tf.json"), + config: map[string]any{"terraform": map[string]any{"backend": map[string]any{"s3": map[string]any{"bucket": "test-bucket"}}}}, }, { name: "writes to component dir", @@ -205,6 +207,9 @@ func TestWriteBackendConfigFile(t *testing.T) { err := writeBackendConfigFile(atmosConfig, tt.info, tt.config) assert.NoError(t, err) + if tt.wantAbsent != "" { + assert.NoFileExists(t, tt.wantAbsent) + } if tt.wantPath != "" { assert.FileExists(t, tt.wantPath) if tt.wantContains != "" { diff --git a/pkg/component/terraform/mocks_test.go b/pkg/component/terraform/mocks_test.go index 8b4abb735db..784076b645d 100644 --- a/pkg/component/terraform/mocks_test.go +++ b/pkg/component/terraform/mocks_test.go @@ -239,3 +239,20 @@ func setupTerraformYamlFunctionSandbox(t *testing.T, workDir string) { t.Setenv(k, v) } } + +func TestInvalidateTerraformExecutionCaches_StackAlias(t *testing.T) { + canonical := refcache.NewKey(nil, "canonical", "target") + alias := refcache.NewKey(nil, "alias", "target") + other := refcache.NewKey(nil, "other", "target") + for _, key := range []refcache.Key{canonical, alias, other} { + terraformStateCache.Store(key, "cached") + t.Cleanup(func() { terraformStateCache.Delete(key) }) + } + invalidateTerraformExecutionCaches(&schema.ConfigAndStacksInfo{Stack: "canonical", StackFromArg: "alias", ComponentFromArg: "target"}) + _, found := terraformStateCache.Load(canonical) + assert.False(t, found) + _, found = terraformStateCache.Load(alias) + assert.False(t, found) + _, found = terraformStateCache.Load(other) + assert.True(t, found) +} diff --git a/pkg/component/terraform/shell_test.go b/pkg/component/terraform/shell_test.go index 7ac5a5b33f3..5b2a27c7813 100644 --- a/pkg/component/terraform/shell_test.go +++ b/pkg/component/terraform/shell_test.go @@ -559,7 +559,14 @@ func TestExecuteShellLifecycle_WorkdirComponent_SkipsWorkspaceClean(t *testing.T info.ComponentSection = map[string]any{provWorkdir.WorkdirPathKey: tmpDir} cfg := &shellConfig{componentPath: tmpDir, workingDir: tmpDir, varFile: "dev-vpc.tfvars.json"} + t.Setenv("TF_DATA_DIR", "") + marker := filepath.Join(tmpDir, ".terraform", "environment") + require.NoError(t, os.MkdirAll(filepath.Dir(marker), 0o755)) + require.NoError(t, os.WriteFile(marker, []byte("myworkspace"), 0o600)) require.NoError(t, testExecutor(t).executeShellLifecycle(atmosConfig, &info, cfg)) + data, err := os.ReadFile(marker) + require.NoError(t, err) + assert.Equal(t, "myworkspace", string(data)) assert.Equal(t, []string{"init", "workspace", "shell"}, *calls, "workdir components still run init, workspace, and shell in order") } From cba896a3758ba00f507b93bd9ab6558b533d2313 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 13 Nov 2026 12:06:18 -0600 Subject: [PATCH 14/25] fix(terraform): filter mixed-case plugin cache env on Windows --- pkg/component/terraform/execute.go | 23 +++++++++++++++-- pkg/component/terraform/plugin_cache_test.go | 27 ++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/pkg/component/terraform/execute.go b/pkg/component/terraform/execute.go index 7a1206ac950..c0beecf0fcb 100644 --- a/pkg/component/terraform/execute.go +++ b/pkg/component/terraform/execute.go @@ -5,6 +5,8 @@ import ( "fmt" "maps" "os" + "runtime" + "strings" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -415,6 +417,11 @@ func disableTerraformPluginCacheForExecution(atmosConfig *schema.AtmosConfigurat } func removeEnvKeys(env []string, keys ...string) []string { + return removeEnvKeysForGOOS(env, runtime.GOOS, keys...) +} + +// removeEnvKeysForGOOS keeps the Windows matching behavior testable on any host. +func removeEnvKeysForGOOS(env []string, goos string, keys ...string) []string { if len(env) == 0 || len(keys) == 0 { return env } @@ -427,8 +434,7 @@ func removeEnvKeys(env []string, keys ...string) []string { // and filtering in place would corrupt it for later or concurrent executions. filtered := make([]string, 0, len(env)) for _, entry := range env { - key := envKey(entry) - if _, ok := skip[key]; ok { + if shouldRemoveEnvKey(envKey(entry), skip, goos) { continue } filtered = append(filtered, entry) @@ -436,6 +442,19 @@ func removeEnvKeys(env []string, keys ...string) []string { return filtered } +func shouldRemoveEnvKey(key string, skip map[string]struct{}, goos string) bool { + if _, ok := skip[key]; ok { + return true + } + if goos != "windows" { + return false + } + _, skipCacheDir := skip[terraformPluginCacheDirEnv] + _, skipCacheLockFile := skip[terraformPluginCacheMayBreakLockFileEnv] + return (skipCacheDir && strings.EqualFold(key, terraformPluginCacheDirEnv)) || + (skipCacheLockFile && strings.EqualFold(key, terraformPluginCacheMayBreakLockFileEnv)) +} + func envKey(entry string) string { for i := 0; i < len(entry); i++ { if entry[i] == '=' { diff --git a/pkg/component/terraform/plugin_cache_test.go b/pkg/component/terraform/plugin_cache_test.go index 7c6304e677c..18384cbb5ba 100644 --- a/pkg/component/terraform/plugin_cache_test.go +++ b/pkg/component/terraform/plugin_cache_test.go @@ -247,6 +247,33 @@ func TestDisableTerraformPluginCacheForExecutionKeepsCallerEnvSlice(t *testing.T assert.Equal(t, []string{"KEEP_SHARED=1"}, info.SanitizedEnv) } +func TestRemoveEnvKeysForGOOS(t *testing.T) { + env := []string{ + "tf_plugin_cache_dir=/cache", + "Tf_Plugin_Cache_May_Break_Dependency_Lock_File=true", + "TF_CLI_ARGS_plan=-lock=false", + "tf_cli_args_plan=other", + "KEEP_ME=1", + } + keys := []string{terraformPluginCacheDirEnv, terraformPluginCacheMayBreakLockFileEnv, "TF_CLI_ARGS_plan"} + + t.Run("windows matches only plugin cache names ignoring case", func(t *testing.T) { + got := removeEnvKeysForGOOS(env, "windows", keys...) + assert.Equal(t, []string{"tf_cli_args_plan=other", "KEEP_ME=1"}, got) + assert.Equal(t, "tf_plugin_cache_dir=/cache", env[0], "filter must not rewrite caller entries") + }) + + t.Run("other systems retain exact-case matching", func(t *testing.T) { + got := removeEnvKeysForGOOS(env, "linux", keys...) + assert.Equal(t, []string{ + "tf_plugin_cache_dir=/cache", + "Tf_Plugin_Cache_May_Break_Dependency_Lock_File=true", + "tf_cli_args_plan=other", + "KEEP_ME=1", + }, got) + }) +} + func TestDisableTerraformPluginCacheForExecutionNoopWithoutFlag(t *testing.T) { atmosConfig := &schema.AtmosConfiguration{ Components: schema.Components{ From 48bbcfbe24d46331184628ada8c35ce446eff60c Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sat, 14 Nov 2026 07:18:57 -0600 Subject: [PATCH 15/25] fix(lint): resolve golangci-lint findings in files changed by the provider-core refactor Remove unused helpers, add perf tracking to the exec option setters, wrap errors with %w, use static sentinels and named constants, and split the backend/varfile generators into helpers to clear nestif findings. Co-Authored-By: Claude Sonnet 5.5 --- internal/exec/path_utils.go | 10 - internal/exec/shell_utils.go | 36 +- .../terraform_reference_cache_auth_test.go | 2 +- pkg/component/terraform/cli_args_utils.go | 2 +- pkg/component/terraform/generate_backend.go | 2 +- pkg/component/terraform/generate_backends.go | 786 ++++++++++-------- pkg/component/terraform/generate_planfile.go | 12 +- pkg/component/terraform/generate_varfile.go | 2 +- pkg/component/terraform/generate_varfiles.go | 576 ++++++------- 9 files changed, 703 insertions(+), 725 deletions(-) diff --git a/internal/exec/path_utils.go b/internal/exec/path_utils.go index 9bddc8962e8..717936939e3 100644 --- a/internal/exec/path_utils.go +++ b/internal/exec/path_utils.go @@ -26,21 +26,11 @@ func constructTerraformComponentVarfileName(info *schema.ConfigAndStacksInfo) st return terraformcomponent.ConstructTerraformComponentVarfileName(info) } -// constructTerraformComponentTestVarfileName delegates to the Terraform component. -func constructTerraformComponentTestVarfileName(info *schema.ConfigAndStacksInfo) string { - return terraformcomponent.ConstructTerraformComponentTestVarfileName(info) -} - // constructTerraformComponentVarfilePath delegates to the Terraform component. func constructTerraformComponentVarfilePath(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) string { return terraformcomponent.ConstructTerraformComponentVarfilePath(atmosConfig, info) } -// constructTerraformComponentTestVarfilePath delegates to the Terraform component. -func constructTerraformComponentTestVarfilePath(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) string { - return terraformcomponent.ConstructTerraformComponentTestVarfilePath(atmosConfig, info) -} - // constructTerraformComponentPlanfilePath delegates to the Terraform component. func constructTerraformComponentPlanfilePath(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) string { return terraformcomponent.ConstructTerraformComponentPlanfilePath(atmosConfig, info) diff --git a/internal/exec/shell_utils.go b/internal/exec/shell_utils.go index 40cd23a3263..f7f32522478 100644 --- a/internal/exec/shell_utils.go +++ b/internal/exec/shell_utils.go @@ -29,7 +29,6 @@ import ( metricsprocess "github.com/cloudposse/atmos/pkg/metrics/process" "github.com/cloudposse/atmos/pkg/perf" process "github.com/cloudposse/atmos/pkg/process" - "github.com/cloudposse/atmos/pkg/provisioner" "github.com/cloudposse/atmos/pkg/schema" "github.com/cloudposse/atmos/pkg/shell" terminalpkg "github.com/cloudposse/atmos/pkg/terminal" @@ -52,11 +51,6 @@ func WithInvokingCommand(cmd *cobra.Command) ShellCommandOption { return component.WithInvokingCommand(cmd) } -// invokingCommandFromOpts returns the invoking command supplied through shared execution options. -func invokingCommandFromOpts(opts ...ShellCommandOption) *cobra.Command { - return component.InvokingCommandFromOptions(opts...) -} - // WithExecMetadataParser supplies the parser for command-specific execution results. func WithExecMetadataParser(fn func(string, int, string) any) ShellCommandOption { defer perf.Track(nil, "exec.WithExecMetadataParser")() @@ -71,17 +65,9 @@ func execMetadataParserFromOpts(opts ...ShellCommandOption) func(string, int, st // WithMetricsCallback supplies the callback that receives subprocess resource-usage metrics. func WithMetricsCallback(fn func(*metricsprocess.ProcessMetrics)) ShellCommandOption { - return component.WithMetricsCallback(fn) -} + defer perf.Track(nil, "exec.WithMetricsCallback")() -// withExecMetadataOutputCapture adds output writers scoped to the main command for metadata reporting. -func withExecMetadataOutputCapture(stdoutW, stderrW io.Writer) ShellCommandOption { - return component.WithMetadataOutputCapture(stdoutW, stderrW) -} - -// execMetadataOutputCaptureFromOpts retrieves the metadata writers so retries can reset their output. -func execMetadataOutputCaptureFromOpts(opts ...ShellCommandOption) (io.Writer, io.Writer) { - return component.MetadataOutputCaptureFromOptions(opts...) + return component.WithMetricsCallback(fn) } // WithStdoutCapture tees raw (unmasked) subprocess stdout to the supplied writer. @@ -107,11 +93,15 @@ func WithStdoutOverride(w io.Writer) ShellCommandOption { // WithProcessStreams supplies stdin, stdout, and stderr through the shared component options. func WithProcessStreams(streams process.Streams) ShellCommandOption { + defer perf.Track(nil, "exec.WithProcessStreams")() + return component.WithProcessStreams(streams) } // WithProcessContext supplies cancellation and deadlines for subprocess execution. func WithProcessContext(ctx context.Context) ShellCommandOption { + defer perf.Track(nil, "exec.WithProcessContext")() + return component.WithProcessContext(ctx) } @@ -127,18 +117,6 @@ func WithEnvironment(env []string) ShellCommandOption { return component.WithEnvironment(env) } -// shellCommandOutputWriters exposes explicitly supplied process writers to provisioning hooks. -func shellCommandOutputWriters(opts ...ShellCommandOption) provisioner.OutputWriters { - var options component.ExecutionOptions - for _, opt := range opts { - opt(&options) - } - if options.Streams == nil { - return provisioner.OutputWriters{} - } - return provisioner.OutputWriters{Stdout: options.Streams.Stdout, Stderr: options.Streams.Stderr} -} - // resolveMaskingDisabled decides whether output masking should be disabled. // The --mask/ATMOS_MASK flag/env override wins when set, otherwise // settings.terminal.mask.enabled (from atmos.yaml) applies. The presence @@ -242,7 +220,7 @@ func ExecuteShellCommand( // Set up stdout: masked output to terminal, optionally tee'd to a capture writer. // When stdoutOverride is set, use it instead of os.Stdout (e.g., redirect to stderr // for workspace select so it doesn't pollute data-producing commands like output). - var stdoutTarget io.Writer = streams.Stdout + stdoutTarget := streams.Stdout if cfg.StdoutOverride != nil { stdoutTarget = cfg.StdoutOverride } diff --git a/internal/exec/terraform_reference_cache_auth_test.go b/internal/exec/terraform_reference_cache_auth_test.go index b8d6feb2616..36a8aa9b337 100644 --- a/internal/exec/terraform_reference_cache_auth_test.go +++ b/internal/exec/terraform_reference_cache_auth_test.go @@ -59,7 +59,7 @@ func TestGetTerraformStateCacheSeparatesCallerCredentials(t *testing.T) { } // Invalidating a component removes the reads of every caller scope. -// atmos.Component() results are partitioned by the enclosing component's credentials and +// Results of atmos.Component() are partitioned by the enclosing component's credentials and // by whether authentication is disabled. func TestComponentFuncCacheSeparatesCallerCredentials(t *testing.T) { tests := []struct { diff --git a/pkg/component/terraform/cli_args_utils.go b/pkg/component/terraform/cli_args_utils.go index 832dbdcfa71..f0187ba43a1 100644 --- a/pkg/component/terraform/cli_args_utils.go +++ b/pkg/component/terraform/cli_args_utils.go @@ -120,7 +120,7 @@ func GetTerraformEnvCliVars() (map[string]any, error) { if filetype.IsJSON(part2) { v, err := u.ConvertFromJSON(part2) if err != nil { - return nil, fmt.Errorf("%w: parsing TF_CLI_ARGS -var JSON for %q: %v", errUtils.ErrTerraformEnvCliVarJSON, varName, err) + return nil, fmt.Errorf("%w: parsing TF_CLI_ARGS -var JSON for %q: %w", errUtils.ErrTerraformEnvCliVarJSON, varName, err) } varValue = v } else { diff --git a/pkg/component/terraform/generate_backend.go b/pkg/component/terraform/generate_backend.go index e25eb8c6d34..0503d958e77 100644 --- a/pkg/component/terraform/generate_backend.go +++ b/pkg/component/terraform/generate_backend.go @@ -105,7 +105,7 @@ func writeBackendConfigFile(atmosConfig *schema.AtmosConfiguration, info *schema } // ExecuteTerraformGenerateBackendCmd executes `terraform generate backend` command. -// Deprecated: Use ExecuteGenerateBackend with typed parameters instead. +// This function is superseded; use ExecuteGenerateBackend with typed parameters instead. func ExecuteTerraformGenerateBackendCmd(cmd interface{}, args []string) error { defer perf.Track(nil, "exec.ExecuteTerraformGenerateBackendCmd")() diff --git a/pkg/component/terraform/generate_backends.go b/pkg/component/terraform/generate_backends.go index 48c50c418bf..a008235fe46 100644 --- a/pkg/component/terraform/generate_backends.go +++ b/pkg/component/terraform/generate_backends.go @@ -3,6 +3,7 @@ package terraform import ( "errors" "fmt" + "maps" "path/filepath" "slices" "strings" @@ -10,6 +11,7 @@ import ( "github.com/go-viper/mapstructure/v2" errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/component" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/function/literalsource" log "github.com/cloudposse/atmos/pkg/logger" @@ -63,9 +65,211 @@ func CheckBackendTypeAfterProcessing(backendType string) error { return nil } +// ErrTemplatingDisabledWithTemplates is returned when stack manifests contain Go templates but templating is disabled. +var ErrTemplatingDisabledWithTemplates = errors.New("the stack manifests contain Go templates, but templating is disabled in atmos.yaml in 'templates.settings.enabled'\n" + + "to enable templating, refer to https://atmos.tools/core-concepts/stacks/templating") + +const ( + backendFormatJSON = "json" + backendFormatHCL = "hcl" + backendFormatBackendConfig = "backend-config" + abstractComponentType = "abstract" +) + +// generateRun holds the inputs shared by every component processed by a generate command. +type generateRun struct { + atmosConfig *schema.AtmosConfiguration + fileTemplate string + format string + stacks []string + rawStackConfigs map[string]map[string]any + deferredContexts component.AllStacksDeferredContexts + // processed tracks the terraform components whose backend was already written. + processed map[string]any +} + +// componentRef identifies the stack manifest, component, and terraform component being generated. +type componentRef struct { + stackFileName string + componentName string + terraformComponent string + terraformComponentPath string +} + +// generatorRender holds the inputs needed to render a component section for a generate command. +type generatorRender struct { + componentSection map[string]any + settingsSection map[string]any + rawStackConfigs map[string]map[string]any + stackFileName string + stackName string + templateName string +} + +// terraformComponentsInStack returns the terraform components section of a stack, or nil when absent. +func terraformComponentsInStack(stackSection any) map[string]any { + stackMap, _ := stackSection.(map[string]any) + componentsSection, _ := stackMap["components"].(map[string]any) + terraformSection, _ := componentsSection[cfg.TerraformSectionName].(map[string]any) + return terraformSection +} + +// componentSelected reports whether the `components` filter selects the component. +func componentSelected(components []string, componentName string) bool { + return len(components) == 0 || slices.Contains(components, componentName) +} + +// stackSelected reports whether the `stacks` filter selects the stack. +func stackSelected(stacks []string, stackFileName, stackName string) bool { + return len(stacks) == 0 || + // `stacks` filter can contain the names of the top-level stack config files: + // atmos terraform generate varfiles --stacks=orgs/cp/tenant1/staging/us-east-2,orgs/cp/tenant2/dev/us-east-2 + slices.Contains(stacks, stackFileName) || + // `stacks` filter can also contain the logical stack names (derived from the context vars): + // atmos terraform generate varfiles --stacks=tenant1-ue2-staging,tenant1-ue2-prod + slices.Contains(stacks, stackName) +} + +// isAbstractComponent reports whether the component metadata marks the component as abstract. +func isAbstractComponent(metadataSection map[string]any) bool { + componentType, _ := metadataSection["type"].(string) + return componentType == abstractComponentType +} + +// sectionOrEmpty returns the named map section, or an empty map when it is absent or not a map. +func sectionOrEmpty(componentSection map[string]any, name string) map[string]any { + if section, ok := componentSection[name].(map[string]any); ok { + return section + } + return map[string]any{} +} + +// terraformComponentAndPath finds the terraform component and its path. +// If the `component` attribute is present, it's the terraform component. +// Otherwise, the YAML component name is the terraform component. +func terraformComponentAndPath(atmosConfig *schema.AtmosConfiguration, componentName string, componentSection map[string]any) (string, string) { + terraformComponent := componentName + if componentAttribute, ok := componentSection[cfg.ComponentSectionName].(string); ok { + terraformComponent = componentAttribute + } + + return terraformComponent, filepath.Join( + atmosConfig.BasePath, + atmosConfig.Components.Terraform.BasePath, + terraformComponent, + ) +} + +// setGeneratorIdentity records the component, stack, and stack manifest file in the section. +func setGeneratorIdentity(section map[string]any, componentName, stackName, stackFileName string) { + section["atmos_component"] = componentName + section["atmos_stack"] = stackName + section["stack"] = stackName + section["atmos_stack_file"] = stackFileName + section["atmos_manifest"] = stackFileName +} + +// resolveGeneratorStackName computes the stack name from the name template or the name pattern. +func (s *Operations) resolveGeneratorStackName( + atmosConfig *schema.AtmosConfiguration, + templateName string, + stackFileName string, + info *schema.ConfigAndStacksInfo, +) (string, error) { + var stackName string + var err error + if atmosConfig.Stacks.NameTemplate != "" { + stackName, err = s.host.ProcessTmpl(atmosConfig, templateName, atmosConfig.Stacks.NameTemplate, info.ComponentSection, atmosConfig.Templates.Settings.IgnoreMissingTemplateValues) + } else { + stackName, err = cfg.GetContextPrefix(stackFileName, info.Context, atmosConfig.Stacks.NamePattern, stackFileName) + } + if err != nil { + return "", err + } + + // A computed name is not final yet. Keep derived metadata from being + // interpreted as another Starlark program during value evaluation. + if s.host.ContainsStarlark(stackName) { + stackName = stackFileName + } + return stackName, nil +} + +// withTemplatingDisabledHint joins the templating-disabled error when the manifest contains Go templates. +func withTemplatingDisabledHint(err error, atmosConfig *schema.AtmosConfiguration, componentSectionStr string) error { + if atmosConfig.Templates.Settings.Enabled { + return err + } + if !strings.Contains(componentSectionStr, "{{") && !strings.Contains(componentSectionStr, "}}") { + return err + } + return errors.Join(err, ErrTemplatingDisabledWithTemplates) +} + +// renderGeneratorComponent processes the Go templates and YAML functions of the component section. +// The rendered section, context, and stack name are stored in the provided info. +func (s *Operations) renderGeneratorComponent(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, req generatorRender) error { + // Process `Go` templates + componentSectionStr, err := atmosYaml.ConvertToYAMLPreservingDelimiters(req.componentSection, atmosConfig.Templates.Settings.Delimiters) + if err != nil { + return err + } + + var settingsSectionStruct schema.Settings + if err := mapstructure.Decode(req.settingsSection, &settingsSectionStruct); err != nil { + return err + } + + // Snapshot the component context for deferred-YAML-function template rendering + // below (a deferred value's own {{ }} expression, e.g. a parametrized + // !terraform.state stack argument, needs the same data source ProcessTmplWithDatasources + // uses here). + componentTemplateContext := maps.Clone(info.ComponentSection) + + componentSectionProcessed, err := s.host.ProcessTmplWithDatasources( + atmosConfig, + info, + settingsSectionStruct, + req.templateName, + componentSectionStr, + info.ComponentSection, + true, + ) + if err != nil { + return err + } + + componentSectionConverted, err := u.UnmarshalYAML[schema.AtmosSectionMapType](componentSectionProcessed) + if err != nil { + errUtils.CheckErrorPrintAndExit(withTemplatingDisabledHint(err, atmosConfig, componentSectionStr), "", "") + } + + info.ComponentSection = componentSectionConverted + info.StackLocalsSection = s.host.StackLocalsForComponent(req.rawStackConfigs, req.stackFileName, cfg.TerraformComponentType) + yamlSkip, finishValues := s.host.PrepareConfigurationValues(atmosConfig, info, nil, nil) + componentSectionFinal, err := s.host.ProcessCustomYamlTags(atmosConfig, componentSectionConverted, req.stackName, yamlSkip, info) + if err != nil { + return err + } + info.ComponentSection = componentSectionFinal + + // Resolve deferred YAML functions (!template, !labels, !tags, !terraform.output, + // etc.) and deep-merge their results against any concrete override at the same + // path. Without this, a section that only survived Stage 2's structural merge as an + // unresolved function string (or a placeholder) would silently lose the function's + // contribution here, the same #2888 data-loss bug the main describe/plan path fixes + // via this same call (see internal/exec/utils.go). + if err := s.host.ResolveDeferredYamlFunctions(atmosConfig, info, &settingsSectionStruct, componentTemplateContext, nil, nil); err != nil { + return err + } + if err := finishValues(); err != nil { + return err + } + info.ComponentSection = literalsource.RestoreMap(info.ComponentSection) + return s.refreshTerraformGeneratorContext(atmosConfig, info, req.stackFileName) +} + // ExecuteTerraformGenerateBackends generates backend configs for all terraform components. -// -//nolint:cyclop,funlen,gocognit,revive // Preserve the existing generation and evaluation order during package extraction. func (s *Operations) ExecuteTerraformGenerateBackends( atmosConfig *schema.AtmosConfiguration, fileTemplate string, @@ -80,355 +284,249 @@ func (s *Operations) ExecuteTerraformGenerateBackends( return err } - var ok bool - var componentsSection map[string]any - var terraformSection map[string]any - var componentSection map[string]any - var metadataSection map[string]any - var varsSection map[string]any - var settingsSection map[string]any - var envSection map[string]any - var providersSection map[string]any - var authSection map[string]any - var hooksSection map[string]any - var overridesSection map[string]any - var backendSection map[string]any - var backendTypeSection string - processedTerraformComponents := map[string]any{} - fileTemplateProvided := fileTemplate != "" + run := &generateRun{ + atmosConfig: atmosConfig, + fileTemplate: fileTemplate, + format: format, + stacks: stacks, + rawStackConfigs: rawStackConfigs, + deferredContexts: deferredContexts, + processed: map[string]any{}, + } for stackFileName, stackSection := range stacksMap { - if componentsSection, ok = stackSection.(map[string]any)["components"].(map[string]any); !ok { - continue - } - - if terraformSection, ok = componentsSection[cfg.TerraformSectionName].(map[string]any); !ok { - continue - } - - for componentName, compSection := range terraformSection { - if componentSection, ok = compSection.(map[string]any); !ok { + for componentName, compSection := range terraformComponentsInStack(stackSection) { + componentSection, ok := compSection.(map[string]any) + // Check if `components` filter is provided. + if !ok || !componentSelected(components, componentName) { continue } - - // Check if `components` filter is provided - if len(components) == 0 || - slices.Contains(components, componentName) { - // Component metadata - if metadataSection, ok = componentSection[cfg.MetadataSectionName].(map[string]any); ok { - if componentType, ok := metadataSection["type"].(string); ok { - // Don't include abstract components - if componentType == "abstract" { - continue - } - } - } - - // Extract backend configuration. - backend := ExtractBackendConfig(componentSection) - if backend.Err != nil { - log.Warn("Skipping backend generation: "+backend.Err.Error()+". "+ - "Set 'components.terraform.auto_generate_backend_file: false' in atmos.yaml to disable.", - "component", componentName, "stack", stackFileName) - continue - } - backendSection = backend.BackendSection - backendTypeSection = backend.BackendType - - if varsSection, ok = componentSection[cfg.VarsSectionName].(map[string]any); !ok { - varsSection = map[string]any{} - } - - if settingsSection, ok = componentSection[cfg.SettingsSectionName].(map[string]any); !ok { - settingsSection = map[string]any{} - } - - if envSection, ok = componentSection[cfg.EnvSectionName].(map[string]any); !ok { - envSection = map[string]any{} - } - - if providersSection, ok = componentSection[cfg.ProvidersSectionName].(map[string]any); !ok { - providersSection = map[string]any{} - } - - if authSection, ok = componentSection[cfg.AuthSectionName].(map[string]any); !ok { - authSection = map[string]any{} - } - - if hooksSection, ok = componentSection[cfg.HooksSectionName].(map[string]any); !ok { - hooksSection = map[string]any{} - } - - if overridesSection, ok = componentSection[cfg.OverridesSectionName].(map[string]any); !ok { - overridesSection = map[string]any{} - } - - // Find terraform component. - // If `component` attribute is present, it's the terraform component. - // Otherwise, the YAML component name is the terraform component. - terraformComponent := componentName - if componentAttribute, ok := componentSection[cfg.ComponentSectionName].(string); ok { - terraformComponent = componentAttribute - } - - // Path to the terraform component - terraformComponentPath := filepath.Join( - atmosConfig.BasePath, - atmosConfig.Components.Terraform.BasePath, - terraformComponent, - ) - - configAndStacksInfo := schema.ConfigAndStacksInfo{ - ComponentFromArg: componentName, - ComponentType: cfg.TerraformComponentType, - ComponentMetadataSection: metadataSection, - ComponentVarsSection: varsSection, - ComponentSettingsSection: settingsSection, - ComponentEnvSection: envSection, - ComponentAuthSection: authSection, - ComponentProvidersSection: providersSection, - ComponentHooksSection: hooksSection, - ComponentOverridesSection: overridesSection, - ComponentBackendSection: backendSection, - ComponentBackendType: backendTypeSection, - // Snapshot the complete merged component section (auth, required_providers, - // generate, etc. included), not just the sections this generator directly - // consumes — see cloneComponentSectionWithOverrides. - ComponentSection: s.host.CloneComponentSectionWithOverrides(componentSection, map[string]any{ - cfg.VarsSectionName: varsSection, - cfg.MetadataSectionName: metadataSection, - cfg.SettingsSectionName: settingsSection, - cfg.EnvSectionName: envSection, - cfg.AuthSectionName: authSection, - cfg.ProvidersSectionName: providersSection, - cfg.HooksSectionName: hooksSection, - cfg.OverridesSectionName: overridesSection, - cfg.BackendSectionName: backendSection, - cfg.BackendTypeSectionName: backendTypeSection, - }), - } - - if comp, ok := configAndStacksInfo.ComponentSection[cfg.ComponentSectionName].(string); !ok || comp == "" { - configAndStacksInfo.ComponentSection[cfg.ComponentSectionName] = componentName - } - - // Recover this component's deferred-merge contexts (per section: vars, settings, - // env, auth, providers, etc.) from the FindStacksMap cache so deferred YAML - // functions (!template, !labels, !tags, !terraform.output, etc.) can be resolved - // and deep-merged against a concrete override at the same path below, instead of - // silently losing data the way #2888 described for the main describe/plan path. - if compDctx, ok := deferredContexts[stackFileName][cfg.TerraformComponentType][componentName]; ok { - configAndStacksInfo.DeferredMergeContexts = compDctx - } - - // Context - context := cfg.GetContextFromVars(varsSection) - context.Component = strings.Replace(componentName, "/", "-", -1) - context.ComponentPath = terraformComponentPath - - // Stack name - var stackName string - if atmosConfig.Stacks.NameTemplate != "" { - stackName, err = s.host.ProcessTmpl(atmosConfig, "terraform-generate-backends-template", atmosConfig.Stacks.NameTemplate, configAndStacksInfo.ComponentSection, atmosConfig.Templates.Settings.IgnoreMissingTemplateValues) - if err != nil { - return err - } - } else { - stackName, err = cfg.GetContextPrefix(stackFileName, context, atmosConfig.Stacks.NamePattern, stackFileName) - if err != nil { - return err - } - } - - // A computed name is not final yet. Keep derived metadata from being - // interpreted as another Starlark program during value evaluation. - if s.host.ContainsStarlark(stackName) { - stackName = stackFileName - } - - configAndStacksInfo.ComponentSection["atmos_component"] = componentName - configAndStacksInfo.ComponentSection["atmos_stack"] = stackName - configAndStacksInfo.ComponentSection["stack"] = stackName - configAndStacksInfo.ComponentSection["atmos_stack_file"] = stackFileName - configAndStacksInfo.ComponentSection["atmos_manifest"] = stackFileName - - // Populate context and stack name for YAML/template processing - configAndStacksInfo.Context = context - configAndStacksInfo.Stack = stackName - - // Process `Go` templates - componentSectionStr, err := atmosYaml.ConvertToYAMLPreservingDelimiters(componentSection, atmosConfig.Templates.Settings.Delimiters) - if err != nil { - return err - } - - var settingsSectionStruct schema.Settings - err = mapstructure.Decode(settingsSection, &settingsSectionStruct) - if err != nil { - return err - } - - // Snapshot the component context for deferred-YAML-function template rendering - // below (a deferred value's own {{ }} expression, e.g. a parametrized - // !terraform.state stack argument, needs the same data source ProcessTmplWithDatasources - // uses here). - componentTemplateContext := make(map[string]any, len(configAndStacksInfo.ComponentSection)) - for k, v := range configAndStacksInfo.ComponentSection { - componentTemplateContext[k] = v - } - - componentSectionProcessed, err := s.host.ProcessTmplWithDatasources( - atmosConfig, - &configAndStacksInfo, - settingsSectionStruct, - "terraform-generate-backends", - componentSectionStr, - configAndStacksInfo.ComponentSection, - true, - ) - if err != nil { - return err - } - - componentSectionConverted, err := u.UnmarshalYAML[schema.AtmosSectionMapType](componentSectionProcessed) - if err != nil { - if !atmosConfig.Templates.Settings.Enabled { - if strings.Contains(componentSectionStr, "{{") || strings.Contains(componentSectionStr, "}}") { - errorMessage := "the stack manifests contain Go templates, but templating is disabled in atmos.yaml in 'templates.settings.enabled'\n" + - "to enable templating, refer to https://atmos.tools/core-concepts/stacks/templating" - err = errors.Join(err, errors.New(errorMessage)) - } - } - errUtils.CheckErrorPrintAndExit(err, "", "") - } - - configAndStacksInfo.ComponentSection = componentSectionConverted - configAndStacksInfo.StackLocalsSection = s.host.StackLocalsForComponent(rawStackConfigs, stackFileName, cfg.TerraformComponentType) - yamlSkip, finishValues := s.host.PrepareConfigurationValues(atmosConfig, &configAndStacksInfo, nil, nil) - componentSectionFinal, err := s.host.ProcessCustomYamlTags(atmosConfig, componentSectionConverted, stackName, yamlSkip, &configAndStacksInfo) - if err != nil { - return err - } - - componentSection = componentSectionFinal - configAndStacksInfo.ComponentSection = componentSectionFinal - - // Resolve deferred YAML functions (!template, !labels, !tags, !terraform.output, - // etc.) and deep-merge their results against any concrete override at the same - // path. Without this, a section that only survived Stage 2's structural merge as an - // unresolved function string (or a placeholder) would silently lose the function's - // contribution here, the same #2888 data-loss bug the main describe/plan path fixes - // via this same call (see internal/exec/utils.go). - if err := s.host.ResolveDeferredYamlFunctions(atmosConfig, &configAndStacksInfo, &settingsSectionStruct, componentTemplateContext, nil, nil); err != nil { - return err - } - if err := finishValues(); err != nil { - return err - } - configAndStacksInfo.ComponentSection = literalsource.RestoreMap(configAndStacksInfo.ComponentSection) - if err := s.refreshTerraformGeneratorContext(atmosConfig, &configAndStacksInfo, stackFileName); err != nil { - return err - } - context = configAndStacksInfo.Context - stackName = configAndStacksInfo.Stack - componentSection = configAndStacksInfo.ComponentSection - - if i, ok := componentSection[cfg.BackendSectionName].(map[string]any); ok { - backendSection = i - } - - if i, ok := componentSection[cfg.BackendTypeSectionName].(string); ok { - backendTypeSection = i - } - - // Skip if backend_type is empty after template processing. - if err := CheckBackendTypeAfterProcessing(backendTypeSection); err != nil { - log.Warn("Skipping backend generation: "+err.Error()+". "+ - "Set 'components.terraform.auto_generate_backend_file: false' in atmos.yaml to disable.", - "component", componentName, "stack", stackName) - continue - } - - var backendFilePath string - var backendFileAbsolutePath string - - // Check if `stacks` filter is provided - if len(stacks) == 0 || - // `stacks` filter can contain the names of the top-level stack config files: - // atmos terraform generate varfiles --stacks=orgs/cp/tenant1/staging/us-east-2,orgs/cp/tenant2/dev/us-east-2 - slices.Contains(stacks, stackFileName) || - // `stacks` filter can also contain the logical stack names (derived from the context vars): - // atmos terraform generate varfiles --stacks=tenant1-ue2-staging,tenant1-ue2-prod - slices.Contains(stacks, stackName) { - // If '--file-template' is not specified, don't check if we've already processed the terraform component, - // and write the backends to the terraform components folders - if !fileTemplateProvided { - // If the terraform component has been already processed, continue - if u.MapKeyExists(processedTerraformComponents, terraformComponent) { - continue - } - - processedTerraformComponents[terraformComponent] = terraformComponent - - backendFilePath = filepath.Join( - terraformComponentPath, - "backend.tf", - ) - - if format == "json" { - backendFilePath = backendFilePath + ".json" - } - - backendFileAbsolutePath, err = filepath.Abs(backendFilePath) - if err != nil { - return err - } - } else { - // Replace the tokens in the file template - // Supported context tokens: {namespace}, {tenant}, {environment}, {region}, {stage}, {base-component}, {component}, {component-path} - backendFilePath = cfg.ReplaceContextTokens(context, fileTemplate) - backendFileAbsolutePath, err = filepath.Abs(backendFilePath) - if err != nil { - return err - } - - // Create all the intermediate subdirectories - err = u.EnsureDir(backendFileAbsolutePath) - if err != nil { - return err - } - } - - // Write the backend config to the file - log.Debug("Writing backend config for the component to file", "component", terraformComponent, "file", backendFilePath) - - if format == "json" { - componentBackendConfig, err := generateComponentBackendConfig(backendTypeSection, backendSection, "", nil) - if err != nil { - return err - } - - err = u.WriteToFileAsJSON(backendFileAbsolutePath, componentBackendConfig, 0o644) - if err != nil { - return err - } - } else if format == "hcl" { - err = u.WriteTerraformBackendConfigToFileAsHcl(backendFileAbsolutePath, backendTypeSection, backendSection) - if err != nil { - return err - } - } else if format == "backend-config" { - err = u.WriteToFileAsHcl(backendFileAbsolutePath, backendSection, 0o644) - if err != nil { - return err - } - } else { - return fmt.Errorf("invalid '--format' argument '%s'. Valid values are 'hcl' (default), 'json' and 'backend-config'", format) - } - } + if err := s.generateComponentBackend(run, stackFileName, componentName, componentSection); err != nil { + return err } } } return nil } + +// newBackendGeneratorInfo builds the config and stacks info used to render a component for backend generation. +func (s *Operations) newBackendGeneratorInfo( + componentName string, + componentSection map[string]any, + metadataSection map[string]any, + backend BackendConfig, +) schema.ConfigAndStacksInfo { + varsSection := sectionOrEmpty(componentSection, cfg.VarsSectionName) + settingsSection := sectionOrEmpty(componentSection, cfg.SettingsSectionName) + envSection := sectionOrEmpty(componentSection, cfg.EnvSectionName) + providersSection := sectionOrEmpty(componentSection, cfg.ProvidersSectionName) + authSection := sectionOrEmpty(componentSection, cfg.AuthSectionName) + hooksSection := sectionOrEmpty(componentSection, cfg.HooksSectionName) + overridesSection := sectionOrEmpty(componentSection, cfg.OverridesSectionName) + + info := schema.ConfigAndStacksInfo{ + ComponentFromArg: componentName, + ComponentType: cfg.TerraformComponentType, + ComponentMetadataSection: metadataSection, + ComponentVarsSection: varsSection, + ComponentSettingsSection: settingsSection, + ComponentEnvSection: envSection, + ComponentAuthSection: authSection, + ComponentProvidersSection: providersSection, + ComponentHooksSection: hooksSection, + ComponentOverridesSection: overridesSection, + ComponentBackendSection: backend.BackendSection, + ComponentBackendType: backend.BackendType, + // Snapshot the complete merged component section (auth, required_providers, + // generate, etc. included), not just the sections this generator directly + // consumes — see cloneComponentSectionWithOverrides. + ComponentSection: s.host.CloneComponentSectionWithOverrides(componentSection, map[string]any{ + cfg.VarsSectionName: varsSection, + cfg.MetadataSectionName: metadataSection, + cfg.SettingsSectionName: settingsSection, + cfg.EnvSectionName: envSection, + cfg.AuthSectionName: authSection, + cfg.ProvidersSectionName: providersSection, + cfg.HooksSectionName: hooksSection, + cfg.OverridesSectionName: overridesSection, + cfg.BackendSectionName: backend.BackendSection, + cfg.BackendTypeSectionName: backend.BackendType, + }), + } + + if comp, ok := info.ComponentSection[cfg.ComponentSectionName].(string); !ok || comp == "" { + info.ComponentSection[cfg.ComponentSectionName] = componentName + } + + return info +} + +// warnSkipBackend logs a warning and returns true when the backend generation must be skipped due to the error. +func warnSkipBackend(err error, componentName, stackName string) bool { + if err == nil { + return false + } + log.Warn("Skipping backend generation: "+err.Error()+". "+ + "Set 'components.terraform.auto_generate_backend_file: false' in atmos.yaml to disable.", + "component", componentName, "stack", stackName) + return true +} + +// generateComponentBackend renders one component and writes its backend config when the stack filter selects it. +func (s *Operations) generateComponentBackend(run *generateRun, stackFileName, componentName string, componentSection map[string]any) error { + atmosConfig := run.atmosConfig + + // Component metadata. Don't include abstract components. + metadataSection, _ := componentSection[cfg.MetadataSectionName].(map[string]any) + if isAbstractComponent(metadataSection) { + return nil + } + + // Extract backend configuration. + backend := ExtractBackendConfig(componentSection) + if warnSkipBackend(backend.Err, componentName, stackFileName) { + return nil + } + + info := s.newBackendGeneratorInfo(componentName, componentSection, metadataSection, backend) + + // Recover this component's deferred-merge contexts (per section: vars, settings, + // env, auth, providers, etc.) from the FindStacksMap cache so deferred YAML + // functions (!template, !labels, !tags, !terraform.output, etc.) can be resolved + // and deep-merged against a concrete override at the same path below, instead of + // silently losing data the way #2888 described for the main describe/plan path. + if compDctx, ok := run.deferredContexts[stackFileName][cfg.TerraformComponentType][componentName]; ok { + info.DeferredMergeContexts = compDctx + } + + terraformComponent, terraformComponentPath := terraformComponentAndPath(atmosConfig, componentName, componentSection) + ref := componentRef{stackFileName, componentName, terraformComponent, terraformComponentPath} + + // Context + info.Context = cfg.GetContextFromVars(info.ComponentVarsSection) + info.Context.Component = strings.ReplaceAll(componentName, "/", "-") + info.Context.ComponentPath = terraformComponentPath + + stackName, err := s.resolveGeneratorStackName(atmosConfig, "terraform-generate-backends-template", stackFileName, &info) + if err != nil { + return err + } + + setGeneratorIdentity(info.ComponentSection, componentName, stackName, stackFileName) + + // Populate stack name for YAML/template processing + info.Stack = stackName + + err = s.renderGeneratorComponent(atmosConfig, &info, generatorRender{ + componentSection: componentSection, + settingsSection: info.ComponentSettingsSection, + rawStackConfigs: run.rawStackConfigs, + stackFileName: stackFileName, + stackName: stackName, + templateName: "terraform-generate-backends", + }) + if err != nil { + return err + } + + return s.writeComponentBackend(run, &info, backend, ref) +} + +// writeComponentBackend writes the backend config of a rendered component when the stack filter selects it. +func (s *Operations) writeComponentBackend( + run *generateRun, + info *schema.ConfigAndStacksInfo, + backend BackendConfig, + ref componentRef, +) error { + backendSection := backend.BackendSection + if i, ok := info.ComponentSection[cfg.BackendSectionName].(map[string]any); ok { + backendSection = i + } + + backendTypeSection := backend.BackendType + if i, ok := info.ComponentSection[cfg.BackendTypeSectionName].(string); ok { + backendTypeSection = i + } + + // Skip if backend_type is empty after template processing. + if warnSkipBackend(CheckBackendTypeAfterProcessing(backendTypeSection), ref.componentName, info.Stack) { + return nil + } + + // Check if `stacks` filter is provided. + if !stackSelected(run.stacks, ref.stackFileName, info.Stack) { + return nil + } + + backendFilePath, skip := resolveBackendFilePath(run, info, ref) + if skip { + return nil + } + + backendFileAbsolutePath, err := filepath.Abs(backendFilePath) + if err != nil { + return err + } + + // Create all the intermediate subdirectories when the file template is used. + if run.fileTemplate != "" { + if err := u.EnsureDir(backendFileAbsolutePath); err != nil { + return err + } + } + + // Write the backend config to the file + log.Debug("Writing backend config for the component to file", "component", ref.terraformComponent, "file", backendFilePath) + + return writeBackendFile(run.format, backendFileAbsolutePath, backendTypeSection, backendSection) +} + +// resolveBackendFilePath returns the backend file path. +// The skip result is true when the terraform component backend was already written. +func resolveBackendFilePath(run *generateRun, info *schema.ConfigAndStacksInfo, ref componentRef) (string, bool) { + // If '--file-template' is specified, replace the tokens in the file template. + // Supported context tokens: {namespace}, {tenant}, {environment}, {region}, {stage}, {base-component}, {component}, {component-path} + if run.fileTemplate != "" { + return cfg.ReplaceContextTokens(info.Context, run.fileTemplate), false + } + + // If '--file-template' is not specified, don't check if we've already processed the terraform component, + // and write the backends to the terraform components folders. + // If the terraform component has been already processed, skip it. + if u.MapKeyExists(run.processed, ref.terraformComponent) { + return "", true + } + + run.processed[ref.terraformComponent] = ref.terraformComponent + + backendFilePath := filepath.Join( + ref.terraformComponentPath, + "backend.tf", + ) + + if run.format == backendFormatJSON { + backendFilePath += ".json" + } + + return backendFilePath, false +} + +// writeBackendFile writes the backend config to the file in the requested format. +func writeBackendFile(format, backendFileAbsolutePath, backendType string, backendSection map[string]any) error { + switch format { + case backendFormatJSON: + componentBackendConfig, err := generateComponentBackendConfig(backendType, backendSection, "", nil) + if err != nil { + return err + } + return u.WriteToFileAsJSON(backendFileAbsolutePath, componentBackendConfig, filePermissions) + case backendFormatHCL: + return u.WriteTerraformBackendConfigToFileAsHcl(backendFileAbsolutePath, backendType, backendSection) + case backendFormatBackendConfig: + return u.WriteToFileAsHcl(backendFileAbsolutePath, backendSection, filePermissions) + default: + return fmt.Errorf("%w: invalid '--format' argument '%s'. Valid values are 'hcl' (default), 'json' and 'backend-config'", errUtils.ErrInvalidFlag, format) + } +} diff --git a/pkg/component/terraform/generate_planfile.go b/pkg/component/terraform/generate_planfile.go index 9191ca7b27a..a0f3ffb8341 100644 --- a/pkg/component/terraform/generate_planfile.go +++ b/pkg/component/terraform/generate_planfile.go @@ -47,8 +47,8 @@ func (s *Operations) ExecuteGeneratePlanfile(opts *PlanfileOptions, atmosConfig ComponentFromArg: opts.Component, Stack: opts.Stack, StackFromArg: opts.Stack, - ComponentType: "terraform", - CliArgs: []string{"terraform", "generate", "planfile"}, + ComponentType: cfg.TerraformComponentType, + CliArgs: []string{cfg.TerraformComponentType, "generate", "planfile"}, } return s.ExecuteTerraformGeneratePlanfile(opts, info) @@ -94,12 +94,12 @@ func (s *Operations) ExecuteTerraformGeneratePlanfileOld(cmd *cobra.Command, arg return err } - info, err := s.host.ProcessCommandLineArgs("terraform", cmd, args, nil) + info, err := s.host.ProcessCommandLineArgs(cfg.TerraformComponentType, cmd, args, nil) if err != nil { return err } - info.CliArgs = []string{"terraform", "generate", "planfile"} + info.CliArgs = []string{cfg.TerraformComponentType, "generate", "planfile"} component := args[0] @@ -135,7 +135,7 @@ func (s *Operations) ExecuteTerraformGeneratePlanfile( info.ComponentFromArg = options.Component info.Stack = options.Stack - info.ComponentType = "terraform" + info.ComponentType = cfg.TerraformComponentType info.NeedHelp = false // Process templates and Atmos YAML functions. @@ -245,7 +245,7 @@ func resolvePlanfilePath(componentPath, format string, customFile string, info * func writePlanfile(planFilePath, format string, planJSON string) error { d, err := u.ConvertFromJSON(planJSON) if err != nil { - return fmt.Errorf("%w: %s", ErrConvertingJsonToGoType, err) + return fmt.Errorf("%w: %w", ErrConvertingJsonToGoType, err) } const fileMode = 0o644 diff --git a/pkg/component/terraform/generate_varfile.go b/pkg/component/terraform/generate_varfile.go index 8377f321730..a72fb716c78 100644 --- a/pkg/component/terraform/generate_varfile.go +++ b/pkg/component/terraform/generate_varfile.go @@ -184,7 +184,7 @@ func VarfileVarsToWrite(info *schema.ConfigAndStacksInfo, withSecrets bool, varF } // ExecuteTerraformGenerateVarfileCmd executes `terraform generate varfile` command. -// Deprecated: Use ExecuteGenerateVarfile with typed parameters instead. +// This function is superseded; use ExecuteGenerateVarfile with typed parameters instead. func ExecuteTerraformGenerateVarfileCmd(cmd interface{}, args []string) error { defer perf.Track(nil, "exec.ExecuteTerraformGenerateVarfileCmd")() diff --git a/pkg/component/terraform/generate_varfiles.go b/pkg/component/terraform/generate_varfiles.go index 1e8eb6a8628..46bd19765d0 100644 --- a/pkg/component/terraform/generate_varfiles.go +++ b/pkg/component/terraform/generate_varfiles.go @@ -4,26 +4,25 @@ import ( "errors" "fmt" "path/filepath" - "slices" "strings" - "github.com/go-viper/mapstructure/v2" - errUtils "github.com/cloudposse/atmos/errors" cfg "github.com/cloudposse/atmos/pkg/config" - "github.com/cloudposse/atmos/pkg/function/literalsource" atmosio "github.com/cloudposse/atmos/pkg/io" log "github.com/cloudposse/atmos/pkg/logger" "github.com/cloudposse/atmos/pkg/perf" "github.com/cloudposse/atmos/pkg/schema" "github.com/cloudposse/atmos/pkg/terraform/tfvars" u "github.com/cloudposse/atmos/pkg/utils" - atmosYaml "github.com/cloudposse/atmos/pkg/yaml" +) + +const ( + varfileFormatYAML = "yaml" + varfileFormatJSON = "json" + varfileFormatHCL = "hcl" ) // ExecuteTerraformGenerateVarfiles generates varfiles for all terraform components in all stacks. -// -//nolint:cyclop,funlen,gocognit,revive // Preserve the existing generation and evaluation order during package extraction. func (s *Operations) ExecuteTerraformGenerateVarfiles( atmosConfig *schema.AtmosConfiguration, fileTemplate string, @@ -38,338 +37,251 @@ func (s *Operations) ExecuteTerraformGenerateVarfiles( return err } - var ok bool - var componentsSection map[string]any - var terraformSection map[string]any - var componentSection map[string]any - var metadataSection map[string]any - var varsSection map[string]any - var settingsSection map[string]any - var envSection map[string]any - var providersSection map[string]any - var authSection map[string]any - var overridesSection map[string]any - var backendSection map[string]any - var backendTypeSection string + run := &generateRun{ + atmosConfig: atmosConfig, + fileTemplate: fileTemplate, + format: format, + stacks: stacks, + rawStackConfigs: rawStackConfigs, + deferredContexts: deferredContexts, + } for stackFileName, stackSection := range stacksMap { - if componentsSection, ok = stackSection.(map[string]any)["components"].(map[string]any); !ok { - continue - } - - if terraformSection, ok = componentsSection[cfg.TerraformSectionName].(map[string]any); !ok { - continue - } - - for componentName, compSection := range terraformSection { - if componentSection, ok = compSection.(map[string]any); !ok { + for componentName, compSection := range terraformComponentsInStack(stackSection) { + componentSection, ok := compSection.(map[string]any) + // Check if `components` filter is provided. + if !ok || !componentSelected(components, componentName) { continue } - - // Check if `components` filter is provided - if len(components) == 0 || - slices.Contains(components, componentName) { - // Component vars - if varsSection, ok = componentSection[cfg.VarsSectionName].(map[string]any); !ok { - continue - } - - // Component metadata - if metadataSection, ok = componentSection[cfg.MetadataSectionName].(map[string]any); ok { - if componentType, ok := metadataSection["type"].(string); ok { - // Don't include abstract components - if componentType == "abstract" { - continue - } - } - } - - if settingsSection, ok = componentSection[cfg.SettingsSectionName].(map[string]any); !ok { - settingsSection = map[string]any{} - } - - if envSection, ok = componentSection[cfg.EnvSectionName].(map[string]any); !ok { - envSection = map[string]any{} - } - - if authSection, ok = componentSection[cfg.AuthSectionName].(map[string]any); !ok { - authSection = map[string]any{} - } - - if providersSection, ok = componentSection[cfg.ProvidersSectionName].(map[string]any); !ok { - providersSection = map[string]any{} - } - - if overridesSection, ok = componentSection[cfg.OverridesSectionName].(map[string]any); !ok { - overridesSection = map[string]any{} - } - - // Component backend - if backendSection, ok = componentSection[cfg.BackendSectionName].(map[string]any); !ok { - backendSection = map[string]any{} - } - - // Backend type - if backendTypeSection, ok = componentSection[cfg.BackendTypeSectionName].(string); !ok { - backendTypeSection = "" - } - - // Find terraform component. - // If `component` attribute is present, it's the terraform component. - // Otherwise, the YAML component name is the terraform component. - terraformComponent := componentName - if componentAttribute, ok := componentSection[cfg.ComponentSectionName].(string); ok { - terraformComponent = componentAttribute - } - - // Absolute path to the terraform component - terraformComponentPath := filepath.Join( - atmosConfig.BasePath, - atmosConfig.Components.Terraform.BasePath, - terraformComponent, - ) - - configAndStacksInfo := schema.ConfigAndStacksInfo{ - ComponentFromArg: componentName, - ComponentType: cfg.TerraformComponentType, - ComponentMetadataSection: metadataSection, - ComponentVarsSection: varsSection, - ComponentSettingsSection: settingsSection, - ComponentEnvSection: envSection, - ComponentAuthSection: authSection, - ComponentProvidersSection: providersSection, - ComponentOverridesSection: overridesSection, - ComponentBackendSection: backendSection, - ComponentBackendType: backendTypeSection, - // Snapshot the complete merged component section (hooks, generate, - // required_providers, etc. included), not just the sections this generator - // directly consumes — see cloneComponentSectionWithOverrides. - ComponentSection: s.host.CloneComponentSectionWithOverrides(componentSection, map[string]any{ - cfg.VarsSectionName: varsSection, - cfg.MetadataSectionName: metadataSection, - cfg.SettingsSectionName: settingsSection, - cfg.EnvSectionName: envSection, - cfg.AuthSectionName: authSection, - cfg.ProvidersSectionName: providersSection, - cfg.OverridesSectionName: overridesSection, - cfg.BackendSectionName: backendSection, - cfg.BackendTypeSectionName: backendTypeSection, - }), - } - - if comp, ok := configAndStacksInfo.ComponentSection[cfg.ComponentSectionName].(string); !ok || comp == "" { - configAndStacksInfo.ComponentSection[cfg.ComponentSectionName] = componentName - } - - // Recover this component's deferred-merge contexts (per section: vars, settings, - // env, auth, providers, etc.) from the FindStacksMap cache so deferred YAML - // functions (!template, !labels, !tags, !terraform.output, etc.) can be resolved - // and deep-merged against a concrete override at the same path below, instead of - // silently losing data the way #2888 described for the main describe/plan path. - if compDctx, ok := deferredContexts[stackFileName][cfg.TerraformComponentType][componentName]; ok { - configAndStacksInfo.DeferredMergeContexts = compDctx - } - - // Context - context := cfg.GetContextFromVars(varsSection) - context.Component = strings.Replace(componentName, "/", "-", -1) - context.ComponentPath = terraformComponentPath - - // Stack name - var stackName string - if atmosConfig.Stacks.NameTemplate != "" { - stackName, err = s.host.ProcessTmpl(atmosConfig, "terraform-generate-varfiles-template", atmosConfig.Stacks.NameTemplate, configAndStacksInfo.ComponentSection, atmosConfig.Templates.Settings.IgnoreMissingTemplateValues) - if err != nil { - return err - } - } else { - stackName, err = cfg.GetContextPrefix(stackFileName, context, atmosConfig.Stacks.NamePattern, stackFileName) - if err != nil { - return err - } - } - - // A computed name is not final yet. Keep derived metadata from being - // interpreted as another Starlark program during value evaluation. - if s.host.ContainsStarlark(stackName) { - stackName = stackFileName - } - - configAndStacksInfo.Context = context - configAndStacksInfo.Stack = stackName - configAndStacksInfo.ComponentSection["atmos_component"] = componentName - configAndStacksInfo.ComponentSection["atmos_stack"] = stackName - configAndStacksInfo.ComponentSection["stack"] = stackName - configAndStacksInfo.ComponentSection["atmos_stack_file"] = stackFileName - configAndStacksInfo.ComponentSection["atmos_manifest"] = stackFileName - - // Terraform workspace. Batch generation starts from a physical manifest before - // evaluating computed vars. Keep its provisional workspace physical as well; - // BuildTerraformWorkspace runs again after finishValues with the resolved context. - workspace, err := s.host.BuildTerraformWorkspace(atmosConfig, configAndStacksInfo) - if err != nil && !errors.Is(err, errUtils.ErrStarlarkStackIdentity) { - return err - } - if errors.Is(err, errUtils.ErrStarlarkStackIdentity) || s.host.ContainsStarlark(workspace) { - workspace = strings.ReplaceAll(stackFileName, "/", "-") - } - componentSection["workspace"] = workspace - configAndStacksInfo.ComponentSection["workspace"] = workspace - - // Atmos component, stack, and stack manifest file - componentSection["atmos_component"] = componentName - componentSection["atmos_stack"] = stackName - componentSection["stack"] = stackName - componentSection["atmos_stack_file"] = stackFileName - componentSection["atmos_manifest"] = stackFileName - - // Process `Go` templates - componentSectionStr, err := atmosYaml.ConvertToYAMLPreservingDelimiters(componentSection, atmosConfig.Templates.Settings.Delimiters) - if err != nil { - return err - } - - var settingsSectionStruct schema.Settings - err = mapstructure.Decode(settingsSection, &settingsSectionStruct) - if err != nil { - return err - } - - // Snapshot the component context for deferred-YAML-function template rendering - // below (a deferred value's own {{ }} expression, e.g. a parametrized - // !terraform.state stack argument, needs the same data source ProcessTmplWithDatasources - // uses here). - componentTemplateContext := make(map[string]any, len(configAndStacksInfo.ComponentSection)) - for k, v := range configAndStacksInfo.ComponentSection { - componentTemplateContext[k] = v - } - - componentSectionProcessed, err := s.host.ProcessTmplWithDatasources( - atmosConfig, - &configAndStacksInfo, - settingsSectionStruct, - "terraform-generate-varfiles", - componentSectionStr, - configAndStacksInfo.ComponentSection, - true, - ) - if err != nil { - return err - } - - componentSectionConverted, err := u.UnmarshalYAML[schema.AtmosSectionMapType](componentSectionProcessed) - if err != nil { - if !atmosConfig.Templates.Settings.Enabled { - if strings.Contains(componentSectionStr, "{{") || strings.Contains(componentSectionStr, "}}") { - errorMessage := "the stack manifests contain Go templates, but templating is disabled in atmos.yaml in 'templates.settings.enabled'\n" + - "to enable templating, refer to https://atmos.tools/core-concepts/stacks/templating" - err = errors.Join(err, errors.New(errorMessage)) - } - } - errUtils.CheckErrorPrintAndExit(err, "", "") - } - - configAndStacksInfo.ComponentSection = componentSectionConverted - configAndStacksInfo.StackLocalsSection = s.host.StackLocalsForComponent(rawStackConfigs, stackFileName, cfg.TerraformComponentType) - yamlSkip, finishValues := s.host.PrepareConfigurationValues(atmosConfig, &configAndStacksInfo, nil, nil) - componentSectionFinal, err := s.host.ProcessCustomYamlTags(atmosConfig, componentSectionConverted, stackName, yamlSkip, &configAndStacksInfo) - if err != nil { - return err - } - - componentSection = componentSectionFinal - configAndStacksInfo.ComponentSection = componentSectionFinal - - // Resolve deferred YAML functions (!template, !labels, !tags, !terraform.output, - // etc.) and deep-merge their results against any concrete override at the same - // path. Without this, a section that only survived Stage 2's structural merge as an - // unresolved function string (or a placeholder) would silently lose the function's - // contribution here, the same #2888 data-loss bug the main describe/plan path fixes - // via this same call (see internal/exec/utils.go). - if err := s.host.ResolveDeferredYamlFunctions(atmosConfig, &configAndStacksInfo, &settingsSectionStruct, componentTemplateContext, nil, nil); err != nil { - return err - } - if err := finishValues(); err != nil { - return err - } - configAndStacksInfo.ComponentSection = literalsource.RestoreMap(configAndStacksInfo.ComponentSection) - if err := s.refreshTerraformGeneratorContext(atmosConfig, &configAndStacksInfo, stackFileName); err != nil { - return err - } - context = configAndStacksInfo.Context - stackName = configAndStacksInfo.Stack - workspace, err = s.host.BuildTerraformWorkspace(atmosConfig, configAndStacksInfo) - if err != nil { - return err - } - configAndStacksInfo.ComponentSection["workspace"] = workspace - componentSection = configAndStacksInfo.ComponentSection - - if i, ok := componentSection[cfg.VarsSectionName].(map[string]any); ok { - varsSection = i - } - - // Check if `stacks` filter is provided - if len(stacks) == 0 || - // `stacks` filter can contain the names of the top-level stack config files: - // atmos terraform generate varfiles --stacks=orgs/cp/tenant1/staging/us-east-2,orgs/cp/tenant2/dev/us-east-2 - slices.Contains(stacks, stackFileName) || - // `stacks` filter can also contain the logical stack names (derived from the context vars): - // atmos terraform generate varfiles --stacks=tenant1-ue2-staging,tenant1-ue2-prod - slices.Contains(stacks, stackName) { - // Replace the tokens in the file template - // Supported context tokens: {namespace}, {tenant}, {environment}, {region}, {stage}, {base-component}, {component}, {component-path} - fileName := cfg.ReplaceContextTokens(context, fileTemplate) - fileAbsolutePath, err := filepath.Abs(fileName) - if err != nil { - return err - } - - // Create all the intermediate subdirectories - err = u.EnsureDir(fileAbsolutePath) - if err != nil { - return err - } - - // Keep resolved secrets out of generated varfiles so plaintext secrets - // never hit disk. Batch generation has no env-injection target, so - // secret-bearing variables are omitted (use the singular - // `generate varfile --with-secrets` to export them deliberately). - varsToWrite, secretVars := tfvars.Partition(varsSection, atmosio.ContainsSecret) - if len(secretVars) > 0 { - log.Warn("Omitting secret-bearing variables from the generated varfile", - "file", fileName, "count", len(secretVars)) - } - - // Write the varfile - if format == "yaml" { - err = u.WriteToFileAsYAML(fileAbsolutePath, varsToWrite, filePermissions) - if err != nil { - return err - } - } else if format == "json" { - err = u.WriteToFileAsJSON(fileAbsolutePath, varsToWrite, filePermissions) - if err != nil { - return err - } - } else if format == "hcl" { - err = u.WriteToFileAsHcl(fileAbsolutePath, varsToWrite, filePermissions) - if err != nil { - return err - } - } else { - return fmt.Errorf("%w: invalid '--format' argument '%s'. Valid values are 'json' (default), 'yaml' and 'hcl'", errUtils.ErrInvalidFlag, format) - } - - log.Debug("varfile", fileName) - log.Debug("terraform component", terraformComponent) - log.Debug("atmos component", componentName) - log.Debug("atmos stack", stackName) - log.Debug("stack config file", stackFileName) - } + if err := s.generateComponentVarfile(run, stackFileName, componentName, componentSection); err != nil { + return err } } } return nil } + +// newVarfileGeneratorInfo builds the config and stacks info used to render a component for varfile generation. +func (s *Operations) newVarfileGeneratorInfo( + componentName string, + componentSection map[string]any, + metadataSection map[string]any, + varsSection map[string]any, +) schema.ConfigAndStacksInfo { + settingsSection := sectionOrEmpty(componentSection, cfg.SettingsSectionName) + envSection := sectionOrEmpty(componentSection, cfg.EnvSectionName) + authSection := sectionOrEmpty(componentSection, cfg.AuthSectionName) + providersSection := sectionOrEmpty(componentSection, cfg.ProvidersSectionName) + overridesSection := sectionOrEmpty(componentSection, cfg.OverridesSectionName) + + // Component backend and backend type. + backendSection := sectionOrEmpty(componentSection, cfg.BackendSectionName) + backendTypeSection, _ := componentSection[cfg.BackendTypeSectionName].(string) + + info := schema.ConfigAndStacksInfo{ + ComponentFromArg: componentName, + ComponentType: cfg.TerraformComponentType, + ComponentMetadataSection: metadataSection, + ComponentVarsSection: varsSection, + ComponentSettingsSection: settingsSection, + ComponentEnvSection: envSection, + ComponentAuthSection: authSection, + ComponentProvidersSection: providersSection, + ComponentOverridesSection: overridesSection, + ComponentBackendSection: backendSection, + ComponentBackendType: backendTypeSection, + // Snapshot the complete merged component section (hooks, generate, + // required_providers, etc. included), not just the sections this generator + // directly consumes — see cloneComponentSectionWithOverrides. + ComponentSection: s.host.CloneComponentSectionWithOverrides(componentSection, map[string]any{ + cfg.VarsSectionName: varsSection, + cfg.MetadataSectionName: metadataSection, + cfg.SettingsSectionName: settingsSection, + cfg.EnvSectionName: envSection, + cfg.AuthSectionName: authSection, + cfg.ProvidersSectionName: providersSection, + cfg.OverridesSectionName: overridesSection, + cfg.BackendSectionName: backendSection, + cfg.BackendTypeSectionName: backendTypeSection, + }), + } + + if comp, ok := info.ComponentSection[cfg.ComponentSectionName].(string); !ok || comp == "" { + info.ComponentSection[cfg.ComponentSectionName] = componentName + } + + return info +} + +// provisionalWorkspace computes the workspace before computed vars are evaluated. +// Batch generation starts from a physical manifest before evaluating computed vars. Keep its +// provisional workspace physical as well; BuildTerraformWorkspace runs again after finishValues +// with the resolved context. +func (s *Operations) provisionalWorkspace(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, stackFileName string) (string, error) { + workspace, err := s.host.BuildTerraformWorkspace(atmosConfig, *info) + if err != nil && !errors.Is(err, errUtils.ErrStarlarkStackIdentity) { + return "", err + } + if errors.Is(err, errUtils.ErrStarlarkStackIdentity) || s.host.ContainsStarlark(workspace) { + workspace = strings.ReplaceAll(stackFileName, "/", "-") + } + return workspace, nil +} + +// generateComponentVarfile renders one component and writes its varfile when the stack filter selects it. +func (s *Operations) generateComponentVarfile(run *generateRun, stackFileName, componentName string, componentSection map[string]any) error { + atmosConfig := run.atmosConfig + + // Component vars + varsSection, ok := componentSection[cfg.VarsSectionName].(map[string]any) + if !ok { + return nil + } + + // Component metadata. Don't include abstract components. + metadataSection, _ := componentSection[cfg.MetadataSectionName].(map[string]any) + if isAbstractComponent(metadataSection) { + return nil + } + + info, ref, err := s.prepareVarfileComponent(run, componentRef{stackFileName: stackFileName, componentName: componentName}, componentSection, metadataSection, varsSection) + if err != nil { + return err + } + + err = s.renderGeneratorComponent(atmosConfig, &info, generatorRender{ + componentSection: componentSection, + settingsSection: info.ComponentSettingsSection, + rawStackConfigs: run.rawStackConfigs, + stackFileName: stackFileName, + stackName: info.Stack, + templateName: "terraform-generate-varfiles", + }) + if err != nil { + return err + } + + workspace, err := s.host.BuildTerraformWorkspace(atmosConfig, info) + if err != nil { + return err + } + info.ComponentSection["workspace"] = workspace + + if i, ok := info.ComponentSection[cfg.VarsSectionName].(map[string]any); ok { + varsSection = i + } + + // Check if `stacks` filter is provided. + if !stackSelected(run.stacks, stackFileName, info.Stack) { + return nil + } + + return writeComponentVarfile(run, &info, varsSection, ref) +} + +// prepareVarfileComponent builds the info for a component and resolves its context, stack name, and provisional workspace. +func (s *Operations) prepareVarfileComponent( + run *generateRun, + ref componentRef, + componentSection map[string]any, + metadataSection map[string]any, + varsSection map[string]any, +) (schema.ConfigAndStacksInfo, componentRef, error) { + atmosConfig := run.atmosConfig + info := s.newVarfileGeneratorInfo(ref.componentName, componentSection, metadataSection, varsSection) + + // Recover this component's deferred-merge contexts (per section: vars, settings, + // env, auth, providers, etc.) from the FindStacksMap cache so deferred YAML + // functions (!template, !labels, !tags, !terraform.output, etc.) can be resolved + // and deep-merged against a concrete override at the same path below, instead of + // silently losing data the way #2888 described for the main describe/plan path. + if compDctx, ok := run.deferredContexts[ref.stackFileName][cfg.TerraformComponentType][ref.componentName]; ok { + info.DeferredMergeContexts = compDctx + } + + ref.terraformComponent, ref.terraformComponentPath = terraformComponentAndPath(atmosConfig, ref.componentName, componentSection) + + // Context + info.Context = cfg.GetContextFromVars(varsSection) + info.Context.Component = strings.ReplaceAll(ref.componentName, "/", "-") + info.Context.ComponentPath = ref.terraformComponentPath + + stackName, err := s.resolveGeneratorStackName(atmosConfig, "terraform-generate-varfiles-template", ref.stackFileName, &info) + if err != nil { + return info, ref, err + } + + info.Stack = stackName + setGeneratorIdentity(info.ComponentSection, ref.componentName, stackName, ref.stackFileName) + + // Terraform workspace. + workspace, err := s.provisionalWorkspace(atmosConfig, &info, ref.stackFileName) + if err != nil { + return info, ref, err + } + componentSection["workspace"] = workspace + info.ComponentSection["workspace"] = workspace + + // Atmos component, stack, and stack manifest file + setGeneratorIdentity(componentSection, ref.componentName, stackName, ref.stackFileName) + + return info, ref, nil +} + +// writeComponentVarfile writes the varfile of a rendered component. +func writeComponentVarfile( + run *generateRun, + info *schema.ConfigAndStacksInfo, + varsSection map[string]any, + ref componentRef, +) error { + // Replace the tokens in the file template + // Supported context tokens: {namespace}, {tenant}, {environment}, {region}, {stage}, {base-component}, {component}, {component-path} + fileName := cfg.ReplaceContextTokens(info.Context, run.fileTemplate) + fileAbsolutePath, err := filepath.Abs(fileName) + if err != nil { + return err + } + + // Create all the intermediate subdirectories + if err := u.EnsureDir(fileAbsolutePath); err != nil { + return err + } + + // Keep resolved secrets out of generated varfiles so plaintext secrets + // never hit disk. Batch generation has no env-injection target, so + // secret-bearing variables are omitted (use the singular + // `generate varfile --with-secrets` to export them deliberately). + varsToWrite, secretVars := tfvars.Partition(varsSection, atmosio.ContainsSecret) + if len(secretVars) > 0 { + log.Warn("Omitting secret-bearing variables from the generated varfile", + "file", fileName, "count", len(secretVars)) + } + + // Write the varfile + if err := writeVarfile(run.format, fileAbsolutePath, varsToWrite); err != nil { + return err + } + + log.Debug("varfile", fileName) + log.Debug("terraform component", ref.terraformComponent) + log.Debug("atmos component", ref.componentName) + log.Debug("atmos stack", info.Stack) + log.Debug("stack config file", ref.stackFileName) + return nil +} + +// writeVarfile writes the vars to the file in the requested format. +func writeVarfile(format, fileAbsolutePath string, vars map[string]any) error { + switch format { + case varfileFormatYAML: + return u.WriteToFileAsYAML(fileAbsolutePath, vars, filePermissions) + case varfileFormatJSON: + return u.WriteToFileAsJSON(fileAbsolutePath, vars, filePermissions) + case varfileFormatHCL: + return u.WriteToFileAsHcl(fileAbsolutePath, vars, filePermissions) + default: + return fmt.Errorf("%w: invalid '--format' argument '%s'. Valid values are 'json' (default), 'yaml' and 'hcl'", errUtils.ErrInvalidFlag, format) + } +} From f03540c2b32dd15cf5a9e62d0866a33457dd0ee2 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sat, 14 Nov 2026 10:31:43 -0600 Subject: [PATCH 16/25] fix(ci): filter golangci-lint with git on pull requests only-new-issues downloads the PR diff from the GitHub API, which refuses diffs over 20000 lines. The action then lints the whole repository and uploads every pre-existing finding to code scanning, so large PRs show thousands of alerts on code they do not change. Use --new-from-merge-base on pull_request events and keep only-new-issues for the other events. Move the plan-diff link update in the native CI PRD up the stack to keep this PR under the review file limit. Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/codeql.yml | 11 +++++++++-- docs/prd/native-ci/overview.md | 2 +- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 656851b46e5..1848d081b7f 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -393,7 +393,7 @@ jobs: # Key settings: # - install-mode: none - Tells the action to use the binary already in PATH # (our custom-gcl that we just installed) - # - only-new-issues: true - Only report issues in changed code + # - only-new-issues / --new-from-merge-base - Only report issues in changed code # - SARIF output - For GitHub Advanced Security / CodeQL integration # - issues-exit-code: 0 - Always exit 0 so we upload SARIF to CodeQL # @@ -416,10 +416,17 @@ jobs: # can fail transiently. The subsequent lint command still parses and # validates the configuration, so skip the duplicate network preflight. verify: false - only-new-issues: true + # Pull requests filter with git (--new-from-merge-base) instead of the + # action's only-new-issues. only-new-issues downloads the PR diff from the + # GitHub API, which refuses diffs over 20000 lines; the action then silently + # falls back to linting the whole repository and uploads every pre-existing + # finding to code scanning. Git has no such limit. The two options cannot be + # combined, so only-new-issues stays on for the other events (merge queue). + only-new-issues: ${{ github.event_name != 'pull_request' }} args: > --output.sarif.path=golangci-lint.sarif --issues-exit-code=0 + ${{ github.event_name == 'pull_request' && format('--new-from-merge-base=origin/{0}', github.base_ref) || '' }} - name: Upload filtered SARIF results # merge_group's ephemeral gh-readonly-queue/... ref is deleted as soon diff --git a/docs/prd/native-ci/overview.md b/docs/prd/native-ci/overview.md index 4a7f218b0c7..01e0d349472 100644 --- a/docs/prd/native-ci/overview.md +++ b/docs/prd/native-ci/overview.md @@ -190,7 +190,7 @@ Users currently using the GitHub Actions can migrate incrementally: - [Existing CI Detection](../../../pkg/telemetry/ci.go) - Detects 24+ CI providers - [Lifecycle Hooks](../../../pkg/hooks/) - Hook system for terraform events -- [Plan-Diff](../../../pkg/component/terraform/plan_diff.go) - Semantic plan comparison (implemented) +- [Plan-Diff](../../../internal/exec/terraform_plan_diff.go) - Semantic plan comparison (implemented) - [Store Registry](../../../pkg/store/registry.go) - Pattern for planfile stores - Terraform Output Package (`pkg/terraform/output/`) - Output formatting and `FlattenMap()` for CI export - [tfcmt](https://github.com/suzuki-shunsuke/tfcmt) - Inspiration for PR comments From 8c43f7d669bce7e18b677390884d7bd64b70ba13 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sat, 14 Nov 2026 10:36:41 -0600 Subject: [PATCH 17/25] revert(ci): move the golangci-lint filter change up the stack Keeps this PR within the review file limit; the change is carried by the next layer. Co-Authored-By: Claude Sonnet 5.5 --- .github/workflows/codeql.yml | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 1848d081b7f..656851b46e5 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -393,7 +393,7 @@ jobs: # Key settings: # - install-mode: none - Tells the action to use the binary already in PATH # (our custom-gcl that we just installed) - # - only-new-issues / --new-from-merge-base - Only report issues in changed code + # - only-new-issues: true - Only report issues in changed code # - SARIF output - For GitHub Advanced Security / CodeQL integration # - issues-exit-code: 0 - Always exit 0 so we upload SARIF to CodeQL # @@ -416,17 +416,10 @@ jobs: # can fail transiently. The subsequent lint command still parses and # validates the configuration, so skip the duplicate network preflight. verify: false - # Pull requests filter with git (--new-from-merge-base) instead of the - # action's only-new-issues. only-new-issues downloads the PR diff from the - # GitHub API, which refuses diffs over 20000 lines; the action then silently - # falls back to linting the whole repository and uploads every pre-existing - # finding to code scanning. Git has no such limit. The two options cannot be - # combined, so only-new-issues stays on for the other events (merge queue). - only-new-issues: ${{ github.event_name != 'pull_request' }} + only-new-issues: true args: > --output.sarif.path=golangci-lint.sarif --issues-exit-code=0 - ${{ github.event_name == 'pull_request' && format('--new-from-merge-base=origin/{0}', github.base_ref) || '' }} - name: Upload filtered SARIF results # merge_group's ephemeral gh-readonly-queue/... ref is deleted as soon From 30feca84d3f900a4b55344e320b5a6b699beeae0 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sat, 14 Nov 2026 22:00:26 -0600 Subject: [PATCH 18/25] fix(terraform): exclude declared-sensitive variables from batch varfiles Batch varfile generation only dropped registered secrets, so a variable declared sensitive = true with a plain literal value reached disk. Omit declared-sensitive keys when masking is enabled, matching the singular generator. Batch renders from the raw stacks map, which has no component_info, so the module is inspected from the component directory. Co-Authored-By: Claude Sonnet 5.5 --- pkg/component/terraform/execute_prepare.go | 20 +++++++ .../terraform/generate_varfile_test.go | 56 +++++++++++++++++++ pkg/component/terraform/generate_varfiles.go | 5 ++ 3 files changed, 81 insertions(+) diff --git a/pkg/component/terraform/execute_prepare.go b/pkg/component/terraform/execute_prepare.go index 05ba101db50..a8e705cd654 100644 --- a/pkg/component/terraform/execute_prepare.go +++ b/pkg/component/terraform/execute_prepare.go @@ -316,6 +316,11 @@ func terraformSensitiveVarKeys(info *schema.ConfigAndStacksInfo) map[string]bool return nil } + return sensitiveModuleVarKeys(module) +} + +// sensitiveModuleVarKeys returns the variable names a module declares `sensitive = true`. +func sensitiveModuleVarKeys(module *tfconfig.Module) map[string]bool { keys := make(map[string]bool) for name, variable := range module.Variables { if variable != nil && variable.Sensitive { @@ -325,6 +330,21 @@ func terraformSensitiveVarKeys(info *schema.ConfigAndStacksInfo) map[string]bool return keys } +// declaredSensitiveVarKeys returns the sensitive variable names for a component, preferring the +// module inspected during stack processing. Batch generation renders components from the raw stacks +// map, which has no component_info, so it falls back to inspecting the component directory. +func declaredSensitiveVarKeys(info *schema.ConfigAndStacksInfo, componentPath string) map[string]bool { + if keys := terraformSensitiveVarKeys(info); keys != nil { + return keys + } + module, diags := tfconfig.LoadModule(componentPath) + if diags.HasErrors() || module == nil { + log.Debug("Could not inspect the Terraform module for sensitive variables", "path", componentPath, "error", diags.Err()) + return nil + } + return sensitiveModuleVarKeys(module) +} + // refreshTerraformModuleInfo reloads component_info.terraform_config from the provisioned // component directory when stack processing inspected a different or missing directory, as // happens for JIT sources and workdirs. Sensitive-variable detection then reflects the module diff --git a/pkg/component/terraform/generate_varfile_test.go b/pkg/component/terraform/generate_varfile_test.go index 6e853b786db..50d70c05a3f 100644 --- a/pkg/component/terraform/generate_varfile_test.go +++ b/pkg/component/terraform/generate_varfile_test.go @@ -496,3 +496,59 @@ func TestTerraformTestVarsKeepSensitiveRootVariablesOutOfVarfile(t *testing.T) { require.NoError(t, err) assert.Equal(t, []string{"TF_VAR_db_password=unregistered-test-value"}, env) } + +func TestWriteComponentVarfileOmitsDeclaredSensitiveVariables(t *testing.T) { + componentPath := t.TempDir() + writeSensitiveModule(t, componentPath) + require.True(t, iolib.MaskingEnabled(), "masking must be enabled for declaration-aware partitioning") + + vars := map[string]any{"db_password": "unregistered-literal-value", "region": "us-east-1"} + // Batch generation renders components from the raw stacks map, so the section carries no + // component_info and the module must be inspected from the component path. + newRun := func(dir string) *generateRun { + return &generateRun{ + atmosConfig: &schema.AtmosConfiguration{}, + fileTemplate: filepath.Join(dir, "out.tfvars.json"), + format: varfileFormatJSON, + } + } + ref := componentRef{terraformComponentPath: componentPath} + + t.Run("masking enabled drops declared-sensitive keys", func(t *testing.T) { + dir := t.TempDir() + info := &schema.ConfigAndStacksInfo{ComponentSection: map[string]any{}} + + require.NoError(t, writeComponentVarfile(newRun(dir), info, vars, ref)) + + written, err := os.ReadFile(filepath.Join(dir, "out.tfvars.json")) + require.NoError(t, err) + assert.NotContains(t, string(written), "unregistered-literal-value") + assert.Contains(t, string(written), "us-east-1") + }) + + t.Run("module inspected during stack processing is reused", func(t *testing.T) { + dir := t.TempDir() + module, diags := tfconfig.LoadModule(componentPath) + require.False(t, diags.HasErrors()) + info := &schema.ConfigAndStacksInfo{ComponentSection: map[string]any{ + "component_info": map[string]any{"terraform_config": module}, + }} + + require.NoError(t, writeComponentVarfile(newRun(dir), info, vars, componentRef{terraformComponentPath: filepath.Join(dir, "missing")})) + + written, err := os.ReadFile(filepath.Join(dir, "out.tfvars.json")) + require.NoError(t, err) + assert.NotContains(t, string(written), "unregistered-literal-value") + }) + + t.Run("component without a module keeps every variable", func(t *testing.T) { + dir := t.TempDir() + info := &schema.ConfigAndStacksInfo{ComponentSection: map[string]any{}} + + require.NoError(t, writeComponentVarfile(newRun(dir), info, vars, componentRef{terraformComponentPath: filepath.Join(dir, "missing")})) + + written, err := os.ReadFile(filepath.Join(dir, "out.tfvars.json")) + require.NoError(t, err) + assert.Contains(t, string(written), "unregistered-literal-value") + }) +} diff --git a/pkg/component/terraform/generate_varfiles.go b/pkg/component/terraform/generate_varfiles.go index 46bd19765d0..fe0f49adffb 100644 --- a/pkg/component/terraform/generate_varfiles.go +++ b/pkg/component/terraform/generate_varfiles.go @@ -254,6 +254,11 @@ func writeComponentVarfile( // secret-bearing variables are omitted (use the singular // `generate varfile --with-secrets` to export them deliberately). varsToWrite, secretVars := tfvars.Partition(varsSection, atmosio.ContainsSecret) + // Variables declared `sensitive = true` are omitted too, matching the singular + // generator, because a plain literal value has no registered secret to match. + if atmosio.MaskingEnabled() { + varsToWrite = varsWithoutKeys(varsToWrite, declaredSensitiveVarKeys(info, ref.terraformComponentPath)) + } if len(secretVars) > 0 { log.Warn("Omitting secret-bearing variables from the generated varfile", "file", fileName, "count", len(secretVars)) From efc8fb752b9701aea70c718b602658a991ca4a13 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 15 Nov 2026 11:30:42 -0600 Subject: [PATCH 19/25] fix(terraform): reload the module after generation and redact secret vars in debug logs Generated files can declare sensitive variables in a directory stack processing already inspected, so reload the cached module after generation. Debug logging of component and CLI variables now redacts secret-bearing and declared-sensitive keys instead of printing their literals. Co-Authored-By: Claude Sonnet 5.5 --- pkg/component/terraform/execute_prepare.go | 46 ++++++++++++- pkg/component/terraform/provider_test.go | 76 ++++++++++++++++++++++ 2 files changed, 119 insertions(+), 3 deletions(-) diff --git a/pkg/component/terraform/execute_prepare.go b/pkg/component/terraform/execute_prepare.go index a8e705cd654..9844fae08e9 100644 --- a/pkg/component/terraform/execute_prepare.go +++ b/pkg/component/terraform/execute_prepare.go @@ -31,6 +31,9 @@ import ( // The provision-and-resolve component function is a seam for testing JIT provisioning context propagation. var provisionAndResolveTerraformComponentPath = component.ProvisionAndResolveComponentPath +// redactedVarValue replaces the value of a secret variable in debug output. +const redactedVarValue = "" + // The before-init provisioner function is a seam for testing context propagation. var executeBeforeInitProvisioners = provisioner.ExecuteProvisioners @@ -94,7 +97,13 @@ func (e *Executor) autoGenerateComponentFiles(atmosConfig *schema.AtmosConfigura if mkdirErr := os.MkdirAll(componentPath, dirPermissions); mkdirErr != nil { return errors.Join(errUtils.ErrCreateDirectory, fmt.Errorf("auto-generation: %w", mkdirErr)) } - return e.services.GenerateFilesForComponent(atmosConfig, info, componentPath) + if err := e.services.GenerateFilesForComponent(atmosConfig, info, componentPath); err != nil { + return err + } + // Generated files can declare variables, so a module stack processing inspected at this same + // path is stale: reload it, or sensitive declarations in the generated files would be missed. + loadTerraformModuleInfo(info, componentPath) + return nil } // checkComponentRestrictions returns an error when the requested subcommand is not @@ -189,7 +198,7 @@ func terraformTestSecretVarEnv(info *schema.ConfigAndStacksInfo) ([]string, erro func logAndWriteComponentVars(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { log.Debug("Variables for the component in the stack", logFieldComponent, info.ComponentFromArg, "stack", info.Stack) if atmosConfig.Logs.Level == u.LogLevelTrace || atmosConfig.Logs.Level == u.LogLevelDebug { - if err := u.PrintAsYAMLToFileDescriptor(atmosConfig, info.ComponentVarsSection); err != nil { + if err := u.PrintAsYAMLToFileDescriptor(atmosConfig, redactSecretVars(info, info.ComponentVarsSection)); err != nil { return err } } @@ -206,6 +215,27 @@ func logAndWriteComponentVars(atmosConfig *schema.AtmosConfiguration, info *sche return nil } +// redactSecretVars returns a copy of vars with the values of secret-bearing and declared-sensitive +// keys replaced by a placeholder, so debug output never carries literals that are kept out of the +// varfile. The input map is never modified, and it is returned as is when nothing is secret. +func redactSecretVars(info *schema.ConfigAndStacksInfo, vars map[string]any) map[string]any { + declared := map[string]bool(nil) + if atmosio.MaskingEnabled() { + declared = terraformSensitiveVarKeys(info) + } + if len(info.TerraformSecretVarKeys) == 0 && len(declared) == 0 { + return vars + } + redacted := make(map[string]any, len(vars)) + for k, v := range vars { + if info.TerraformSecretVarKeys[k] || declared[k] { + v = redactedVarValue + } + redacted[k] = v + } + return redacted +} + func writeTerraformTestVars(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { if !hasTerraformTestVars(info) { return nil @@ -226,7 +256,7 @@ func logCliVarsOverrides(atmosConfig *schema.AtmosConfiguration, info *schema.Co } log.Debug("CLI variables (will override the variables defined in the stack manifests):") if atmosConfig.Logs.Level == u.LogLevelTrace || atmosConfig.Logs.Level == u.LogLevelDebug { - if err := u.PrintAsYAMLToFileDescriptor(atmosConfig, cliVars); err != nil { + if err := u.PrintAsYAMLToFileDescriptor(atmosConfig, redactSecretVars(info, cliVars)); err != nil { return err } } @@ -359,6 +389,16 @@ func refreshTerraformModuleInfo(info *schema.ConfigAndStacksInfo, componentPath if module != nil && filepath.Clean(recordedPath) == filepath.Clean(componentPath) { return } + loadTerraformModuleInfo(info, componentPath) +} + +// loadTerraformModuleInfo unconditionally reloads component_info.terraform_config from the +// component directory. A module that cannot be inspected leaves the existing value. +func loadTerraformModuleInfo(info *schema.ConfigAndStacksInfo, componentPath string) { + componentInfo, ok := info.ComponentSection["component_info"].(map[string]any) + if !ok { + return + } refreshed, diags := tfconfig.LoadModule(componentPath) if diags.HasErrors() || refreshed == nil { log.Debug("Could not inspect the provisioned Terraform module", "path", componentPath, "error", diags.Err()) diff --git a/pkg/component/terraform/provider_test.go b/pkg/component/terraform/provider_test.go index 0d1662ce1ea..18653bdf1d5 100644 --- a/pkg/component/terraform/provider_test.go +++ b/pkg/component/terraform/provider_test.go @@ -12,10 +12,12 @@ import ( errUtils "github.com/cloudposse/atmos/errors" component "github.com/cloudposse/atmos/pkg/component" + cfg "github.com/cloudposse/atmos/pkg/config" log "github.com/cloudposse/atmos/pkg/logger" provisioner "github.com/cloudposse/atmos/pkg/provisioner" scheduleradapters "github.com/cloudposse/atmos/pkg/scheduler/adapters" schema "github.com/cloudposse/atmos/pkg/schema" + tfconfig "github.com/hashicorp/terraform-config-inspect/tfconfig" assert "github.com/stretchr/testify/assert" require "github.com/stretchr/testify/require" gomock "go.uber.org/mock/gomock" @@ -474,6 +476,80 @@ func TestValidationServicePreservesResultAndError(t *testing.T) { } } +// TestGenerationReloadsModuleInspectedByStackProcessing proves that a sensitive variable declared by a +// generated file reaches the secret keys even when stack processing already inspected the same directory. +func TestGenerationReloadsModuleInspectedByStackProcessing(t *testing.T) { + for _, mode := range []string{"generation enabled", "generation disabled", "dry run"} { + t.Run(mode, func(t *testing.T) { + path := filepath.Join(t.TempDir(), "component") + require.NoError(t, os.MkdirAll(path, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(path, "main.tf"), []byte("variable \"region\" {}\n"), 0o600)) + module, diags := tfconfig.LoadModule(path) + require.False(t, diags.HasErrors()) + config := &schema.AtmosConfiguration{} + config.Components.Terraform.AutoGenerateFiles = mode != "generation disabled" + info := &schema.ConfigAndStacksInfo{ + DryRun: mode == "dry run", + ComponentVarsSection: map[string]any{"db_password": "unregistered-literal-value", "region": "us-east-1"}, + ComponentSection: map[string]any{ + "generate": map[string]any{"sensitive.tf": "content"}, + "component_info": map[string]any{cfg.ComponentPathSectionName: path, "terraform_config": module}, + }, + } + executor := NewExecutor(Services{GenerateFilesForComponent: func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, dir string) error { + return os.WriteFile(filepath.Join(dir, "sensitive.tf"), []byte("variable \"db_password\" {\n sensitive = true\n}\n"), 0o600) + }}) + + require.NoError(t, executor.autoGenerateComponentFiles(config, info, path)) + refreshTerraformModuleInfo(info, path) + ComputeTerraformSecretVarKeys(info) + + if mode == "generation enabled" { + assert.Equal(t, map[string]bool{"db_password": true}, info.TerraformSecretVarKeys) + return + } + assert.Empty(t, info.TerraformSecretVarKeys, "the same-path guard keeps the inspected module when nothing was generated") + }) + } +} + +// TestDebugVarsLogRedactsSecretVariables proves the debug dump of component variables never prints +// literals that are kept out of the varfile because the module declares them sensitive. +func TestDebugVarsLogRedactsSecretVariables(t *testing.T) { + componentPath := t.TempDir() + writeSensitiveModule(t, componentPath) + module, diags := tfconfig.LoadModule(componentPath) + require.False(t, diags.HasErrors()) + config := &schema.AtmosConfiguration{BasePath: t.TempDir()} + config.Logs.Level = "Debug" + info := &schema.ConfigAndStacksInfo{ + DryRun: true, + ComponentVarsSection: map[string]any{"db_password": "unregistered-literal-value", "region": "us-east-1"}, + ComponentSection: map[string]any{ + "component_info": map[string]any{cfg.ComponentPathSectionName: componentPath, "terraform_config": module}, + cfg.TerraformCliVarsSectionName: map[string]any{"db_password": "cli-literal-value", "zone": "a"}, + }, + } + ComputeTerraformSecretVarKeys(info) + + var output bytes.Buffer + logger := log.Default() + originalLevel := logger.GetLevel() + logger.SetLevel(log.DebugLevel) + logger.SetOutput(&output) + t.Cleanup(func() { logger.SetLevel(originalLevel); logger.SetOutput(os.Stderr) }) + + require.NoError(t, logAndWriteComponentVars(config, info)) + require.NoError(t, logCliVarsOverrides(config, info)) + + assert.NotContains(t, output.String(), "unregistered-literal-value") + assert.NotContains(t, output.String(), "cli-literal-value") + assert.Contains(t, output.String(), "db_password") + assert.Contains(t, output.String(), "us-east-1") + assert.Contains(t, output.String(), "zone") + assert.Equal(t, "unregistered-literal-value", info.ComponentVarsSection["db_password"], "redaction must not mutate the real variables") +} + func TestConflictingEnvironmentWarningNamesVariablesWithoutValues(t *testing.T) { t.Setenv("TF_WORKSPACE", "private-workspace-value") t.Setenv("TF_VAR_db_password", "secret-password-value") From c68a6ac7d1d2a82f129ed474f31815f23a296dac Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 15 Nov 2026 22:44:48 -0600 Subject: [PATCH 20/25] fix(terraform): reject computed test vars and encode secret env by declared type - Apply the computed-value guard to test.vars before the test varfile is written, so display-only placeholders are never serialized as inputs. - Encode string secrets for TF_VAR_ the way Terraform reads them: quoted HCL with template sequences escaped when the variable is declared any or with a complex type, and verbatim when it is a primitive or has no type. Co-Authored-By: Claude Sonnet 5.5 --- .../terraform/execute_environment.go | 50 ++++++++ pkg/component/terraform/execute_prepare.go | 9 +- .../terraform/generate_varfile_test.go | 111 ++++++++++++++++++ 3 files changed, 168 insertions(+), 2 deletions(-) diff --git a/pkg/component/terraform/execute_environment.go b/pkg/component/terraform/execute_environment.go index ef6d8239a28..9c0ef1ed026 100644 --- a/pkg/component/terraform/execute_environment.go +++ b/pkg/component/terraform/execute_environment.go @@ -1,6 +1,7 @@ package terraform import ( + "encoding/json" errors "errors" fmt "fmt" os "os" @@ -20,6 +21,7 @@ import ( authbridge "github.com/cloudposse/atmos/pkg/store/authbridge" tfcache "github.com/cloudposse/atmos/pkg/terraform/cache" rc "github.com/cloudposse/atmos/pkg/terraform/rc" + tfvars "github.com/cloudposse/atmos/pkg/terraform/tfvars" ) // warnOnConflictingEnvVars inspects the current process environment for variables @@ -438,3 +440,51 @@ func (e *Executor) SetupComponentAuthForCLI(atmosConfig *schema.AtmosConfigurati return e.setupTerraformAuth(atmosConfig, info) } + +// terraformVarEnvPrefix is the prefix Terraform and OpenTofu read variable values from the environment with. +const terraformVarEnvPrefix = "TF_VAR_" + +// secretEnvForDeclaredTypes renders secret variables as TF_VAR_ entries, encoding each string for the way +// Terraform reads it. Terraform parses a TF_VAR_ value as HCL when the variable is declared `any` or with a +// complex type, and uses it as a literal when the variable is declared string, number, or bool, or has no +// type. A string for an HCL-parsed variable is therefore written as a quoted HCL string, with template +// sequences escaped so that the value is never interpreted as an interpolation. Every other value is +// encoded by tfvars.SecretEnv. +func secretEnvForDeclaredTypes(componentSection map[string]any, secret map[string]any) ([]string, error) { + plain := make(map[string]any, len(secret)) + var quoted []string + for name, value := range secret { + if text, isString := value.(string); isString { + if typeText, declared := TerraformDeclaredVarType(componentSection, name); declared && terraformParsesAsHCL(typeText) { + quoted = append(quoted, terraformVarEnvPrefix+name+"="+quoteHCLString(text)) + continue + } + } + plain[name] = value + } + env, err := tfvars.SecretEnv(plain) + if err != nil { + return nil, err + } + return append(env, quoted...), nil +} + +// terraformParsesAsHCL reports whether Terraform parses a TF_VAR_ value for a variable declared with this type +// as HCL rather than as a literal: every type except the primitives string, number, and bool. +func terraformParsesAsHCL(typeText string) bool { + switch strings.TrimSpace(typeText) { + case "string", "number", "bool": + return false + default: + return true + } +} + +// quoteHCLString renders text as a quoted HCL string literal. The JSON string escapes are valid HCL escapes, +// and `${` and `%{` are doubled so that the value cannot start a template interpolation or directive. +func quoteHCLString(text string) string { + encoded, _ := json.Marshal(text) //nolint:errchkjson // A string always encodes. + quoted := string(encoded) + quoted = strings.ReplaceAll(quoted, "${", "$${") + return strings.ReplaceAll(quoted, "%{", "%%{") +} diff --git a/pkg/component/terraform/execute_prepare.go b/pkg/component/terraform/execute_prepare.go index 9844fae08e9..0277924831c 100644 --- a/pkg/component/terraform/execute_prepare.go +++ b/pkg/component/terraform/execute_prepare.go @@ -240,6 +240,11 @@ func writeTerraformTestVars(atmosConfig *schema.AtmosConfiguration, info *schema if !hasTerraformTestVars(info) { return nil } + // Display-only computed placeholders must not be serialized as inputs, in the varfile or through the + // environment that carries the secret test variables. + if err := rejectComputedTerraformVars(TerraformTestVars(info)); err != nil { + return err + } varFilePath := ConstructTerraformComponentTestVarfilePath(atmosConfig, info) log.Debug("Writing Terraform test variables", "file", varFilePath) if info.DryRun { @@ -496,7 +501,7 @@ func SecretVarEnv(info *schema.ConfigAndStacksInfo) ([]string, error) { secret[k] = v } } - return tfvars.SecretEnv(secret) + return secretEnvForDeclaredTypes(info.ComponentSection, secret) } // TerraformTestVars returns test.vars for the test subcommand, or nil for other commands and absent values. @@ -568,7 +573,7 @@ func TerraformTestSecretVarEnv(info *schema.ConfigAndStacksInfo) ([]string, erro if len(secret) == 0 { return nil, nil } - return tfvars.SecretEnv(secret) + return secretEnvForDeclaredTypes(info.ComponentSection, secret) } // ConstructTerraformComponentWorkingDir constructs the working dir for a terraform component in a stack. diff --git a/pkg/component/terraform/generate_varfile_test.go b/pkg/component/terraform/generate_varfile_test.go index 50d70c05a3f..2afcb4a218d 100644 --- a/pkg/component/terraform/generate_varfile_test.go +++ b/pkg/component/terraform/generate_varfile_test.go @@ -552,3 +552,114 @@ func TestWriteComponentVarfileOmitsDeclaredSensitiveVariables(t *testing.T) { assert.Contains(t, string(written), "unregistered-literal-value") }) } + +func TestWriteTerraformTestVars_RejectsComputedValues(t *testing.T) { + for _, test := range []struct { + name string + vars map[string]any + dryRun bool + reject bool + }{ + {name: "computed placeholder", vars: map[string]any{"fixture": degradation.AtmosComputedValue{}}, reject: true}, + {name: "computed placeholder nested", vars: map[string]any{"fixture": []any{map[string]any{"id": degradation.AtmosComputedValue{}}}}, reject: true}, + {name: "computed placeholder in a dry run", vars: map[string]any{"fixture": degradation.AtmosComputedValue{}}, dryRun: true, reject: true}, + {name: "resolved values", vars: map[string]any{"fixture": "vpc-123"}}, + } { + t.Run(test.name, func(t *testing.T) { + atmosConfig := schema.AtmosConfiguration{BasePath: t.TempDir()} + info := schema.ConfigAndStacksInfo{ + SubCommand: "test", + ContextPrefix: "ctx", + Component: "mycomp", + DryRun: test.dryRun, + ComponentSection: map[string]any{ + cfg.TestSectionName: map[string]any{cfg.VarsSectionName: test.vars}, + }, + } + varfile := ConstructTerraformComponentTestVarfilePath(&atmosConfig, &info) + require.NoError(t, os.MkdirAll(filepath.Dir(varfile), 0o755)) + + err := writeTerraformTestVars(&atmosConfig, &info) + + if test.reject { + require.ErrorIs(t, err, errUtils.ErrUnresolvedComputedTerraformVar) + assert.NoFileExists(t, varfile, "a rejected computed value is never written") + return + } + require.NoError(t, err) + assert.FileExists(t, varfile) + }) + } +} + +func TestSecretVarEnv_QuotesStringsForVariablesTerraformParsesAsHCL(t *testing.T) { + secret := "declared-type-secret-${value}-5b3e" + iolib.RegisterSecret(secret) + module := &tfconfig.Module{Variables: map[string]*tfconfig.Variable{ + "any_token": {Name: "any_token", Type: "any"}, + "string_token": {Name: "string_token", Type: "string"}, + }} + info := &schema.ConfigAndStacksInfo{ + ComponentVarsSection: map[string]any{"any_token": secret, "string_token": secret, "untyped_token": secret}, + ComponentSection: map[string]any{ + "component_info": map[string]any{"terraform_config": module}, + }, + } + ComputeTerraformSecretVarKeys(info) + + env, err := SecretVarEnv(info) + + require.NoError(t, err) + assert.ElementsMatch(t, []string{ + `TF_VAR_any_token="declared-type-secret-$${value}-5b3e"`, + "TF_VAR_string_token=" + secret, + "TF_VAR_untyped_token=" + secret, + }, env) +} + +func TestSecretEnvForDeclaredTypesEncodesStringsForTheDeclaredType(t *testing.T) { + module := &tfconfig.Module{Variables: map[string]*tfconfig.Variable{}} + for name, typeText := range map[string]string{ + "typed_string": "string", "typed_number": "number", "typed_any": "any", "typed_list": "list(string)", + "typed_object": "object({ token = string })", "spaced_any": " any ", + } { + module.Variables[name] = &tfconfig.Variable{Name: name, Type: typeText} + } + section := map[string]any{"component_info": map[string]any{"terraform_config": module}} + + for _, test := range []struct { + name string + value any + want string + }{ + {name: "typed_string", value: "plain", want: "plain"}, + {name: "typed_number", value: "42", want: "42"}, + {name: "undeclared", value: "plain", want: "plain"}, + {name: "typed_any", value: "plain", want: `"plain"`}, + {name: "spaced_any", value: "plain", want: `"plain"`}, + {name: "typed_list", value: "plain", want: `"plain"`}, + {name: "typed_object", value: "plain", want: `"plain"`}, + {name: "typed_any", value: `say "hi"\n`, want: `"say \"hi\"\\n"`}, + {name: "typed_any", value: "token-${secret}", want: `"token-$${secret}"`}, + {name: "typed_any", value: "token-%{if x}", want: `"token-%%{if x}"`}, + {name: "typed_any", value: "line1\nline2", want: `"line1\nline2"`}, + {name: "typed_object", value: map[string]any{"token": "s"}, want: `{"token":"s"}`}, + {name: "typed_list", value: []any{"a", "b"}, want: `["a","b"]`}, + {name: "typed_number", value: 7, want: "7"}, + {name: "undeclared", value: true, want: "true"}, + } { + t.Run(test.name+"/"+test.want, func(t *testing.T) { + env, err := secretEnvForDeclaredTypes(section, map[string]any{test.name: test.value}) + + require.NoError(t, err) + require.Equal(t, []string{"TF_VAR_" + test.name + "=" + test.want}, env) + }) + } +} + +func TestSecretEnvForDeclaredTypesWithoutAModuleKeepsStringsVerbatim(t *testing.T) { + env, err := secretEnvForDeclaredTypes(nil, map[string]any{"a": "plain", "b": `has "quotes"`}) + + require.NoError(t, err) + require.ElementsMatch(t, []string{"TF_VAR_a=plain", `TF_VAR_b=has "quotes"`}, env) +} From a4bf27e4a5189b0b195ad56f8eb684b0f6a81f07 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 16 Nov 2026 07:23:57 -0600 Subject: [PATCH 21/25] fix(terraform): escape nested secret strings before HCL environment transport Recursively escape ${ and %{ in nested strings and object keys of collection secrets for variables Terraform parses as HCL, and add object and list round-trip tests that parse the environment value as HCL. Co-Authored-By: Claude Sonnet 5.5 --- .../terraform/execute_environment.go | 64 ++++++++++++++++-- .../terraform/generate_varfile_test.go | 65 +++++++++++++++++++ 2 files changed, 123 insertions(+), 6 deletions(-) diff --git a/pkg/component/terraform/execute_environment.go b/pkg/component/terraform/execute_environment.go index 9c0ef1ed026..f0c8dd363f7 100644 --- a/pkg/component/terraform/execute_environment.go +++ b/pkg/component/terraform/execute_environment.go @@ -454,11 +454,17 @@ func secretEnvForDeclaredTypes(componentSection map[string]any, secret map[strin plain := make(map[string]any, len(secret)) var quoted []string for name, value := range secret { - if text, isString := value.(string); isString { - if typeText, declared := TerraformDeclaredVarType(componentSection, name); declared && terraformParsesAsHCL(typeText) { - quoted = append(quoted, terraformVarEnvPrefix+name+"="+quoteHCLString(text)) - continue + typeText, declared := TerraformDeclaredVarType(componentSection, name) + if text, isString := value.(string); isString && declared && terraformParsesAsHCL(typeText) { + quoted = append(quoted, terraformVarEnvPrefix+name+"="+quoteHCLString(text)) + continue + } + if _, isString := value.(string); !isString && declared && terraformParsesAsHCL(typeText) { + escaped, err := escapeHCLTemplateSequences(value) + if err != nil { + return nil, fmt.Errorf("encoding secret variable %q for TF_VAR_ injection: %w", name, err) } + value = escaped } plain[name] = value } @@ -485,6 +491,52 @@ func terraformParsesAsHCL(typeText string) bool { func quoteHCLString(text string) string { encoded, _ := json.Marshal(text) //nolint:errchkjson // A string always encodes. quoted := string(encoded) - quoted = strings.ReplaceAll(quoted, "${", "$${") - return strings.ReplaceAll(quoted, "%{", "%%{") + return escapeHCLTemplateText(quoted) +} + +// escapeHCLTemplateSequences recursively escapes `${` and `%{` in every string inside a collection value, both +// object values and object keys, so that Terraform parsing the JSON-encoded environment value as HCL keeps the +// text literal. Scalars other than strings are returned unchanged. +func escapeHCLTemplateSequences(value any) (any, error) { + switch value.(type) { + case nil, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: + return value, nil + } + // Normalize typed maps and slices to generic JSON values so the walk sees every nested string. + encoded, err := json.Marshal(value) + if err != nil { + return nil, err + } + var generic any + if err := json.Unmarshal(encoded, &generic); err != nil { + return nil, err + } + return walkEscapeHCLTemplateSequences(generic), nil +} + +// walkEscapeHCLTemplateSequences escapes template introducers in the strings of an already generic value. +func walkEscapeHCLTemplateSequences(value any) any { + switch typed := value.(type) { + case string: + return escapeHCLTemplateText(typed) + case map[string]any: + out := make(map[string]any, len(typed)) + for key, child := range typed { + out[escapeHCLTemplateText(key)] = walkEscapeHCLTemplateSequences(child) + } + return out + case []any: + out := make([]any, len(typed)) + for i, child := range typed { + out[i] = walkEscapeHCLTemplateSequences(child) + } + return out + default: + return value + } +} + +// escapeHCLTemplateText doubles `${` and `%{` so HCL does not start a template interpolation or directive. +func escapeHCLTemplateText(text string) string { + return strings.ReplaceAll(strings.ReplaceAll(text, "${", "$${"), "%{", "%%{") } diff --git a/pkg/component/terraform/generate_varfile_test.go b/pkg/component/terraform/generate_varfile_test.go index 2afcb4a218d..acb181df879 100644 --- a/pkg/component/terraform/generate_varfile_test.go +++ b/pkg/component/terraform/generate_varfile_test.go @@ -19,9 +19,12 @@ import ( schema "github.com/cloudposse/atmos/pkg/schema" store "github.com/cloudposse/atmos/pkg/store" tfoutput "github.com/cloudposse/atmos/pkg/terraform/output" + hcl "github.com/hashicorp/hcl/v2" + hclsyntax "github.com/hashicorp/hcl/v2/hclsyntax" tfconfig "github.com/hashicorp/terraform-config-inspect/tfconfig" assert "github.com/stretchr/testify/assert" require "github.com/stretchr/testify/require" + cty "github.com/zclconf/go-cty/cty" gomock "go.uber.org/mock/gomock" ) @@ -663,3 +666,65 @@ func TestSecretEnvForDeclaredTypesWithoutAModuleKeepsStringsVerbatim(t *testing. require.NoError(t, err) require.ElementsMatch(t, []string{"TF_VAR_a=plain", `TF_VAR_b=has "quotes"`}, env) } + +// TestSecretEnvForDeclaredTypesCollectionsRoundTripThroughHCL parses the environment value the way Terraform does +// and verifies that interpolation-like text in nested strings and object keys is unchanged. +func TestSecretEnvForDeclaredTypesCollectionsRoundTripThroughHCL(t *testing.T) { + module := &tfconfig.Module{Variables: map[string]*tfconfig.Variable{ + "typed_object": {Name: "typed_object", Type: "map(any)"}, + "typed_list": {Name: "typed_list", Type: "list(any)"}, + "typed_any": {Name: "typed_any", Type: "any"}, + "typed_typed": {Name: "typed_typed", Type: "map(list(string))"}, + }} + section := map[string]any{"component_info": map[string]any{"terraform_config": module}} + + for _, test := range []struct { + name string + value any + want any + }{ + {name: "typed_object", value: map[string]any{"k": "a${b}", "c%{if x}": "%{if y}z%{endif}"}, want: map[string]any{"k": "a${b}", "c%{if x}": "%{if y}z%{endif}"}}, + {name: "typed_list", value: []any{"${first}", "plain", "%{for x in y}"}, want: []any{"${first}", "plain", "%{for x in y}"}}, + {name: "typed_any", value: map[string]any{"outer": map[string]any{"${key}": []any{"x${y}", "$${already}"}}}, want: map[string]any{"outer": map[string]any{"${key}": []any{"x${y}", "$${already}"}}}}, + {name: "typed_typed", value: map[string][]string{"%{k}": {"${v}"}}, want: map[string]any{"%{k}": []any{"${v}"}}}, + } { + t.Run(test.name, func(t *testing.T) { + env, err := secretEnvForDeclaredTypes(section, map[string]any{test.name: test.value}) + require.NoError(t, err) + require.Len(t, env, 1) + + text := strings.TrimPrefix(env[0], "TF_VAR_"+test.name+"=") + expr, diags := hclsyntax.ParseExpression([]byte(text), "env", hcl.InitialPos) + require.False(t, diags.HasErrors(), diags.Error()) + got, diags := expr.Value(nil) + require.False(t, diags.HasErrors(), diags.Error()) + + // Render the evaluated value back to generic Go values for comparison. + assert.Equal(t, test.want, ctyToGeneric(t, got)) + }) + } +} + +// ctyToGeneric converts an evaluated HCL value into maps, slices, and strings. +func ctyToGeneric(t *testing.T, value cty.Value) any { + t.Helper() + switch { + case value.Type() == cty.String: + return value.AsString() + case value.Type().IsObjectType() || value.Type().IsMapType(): + out := map[string]any{} + for key, child := range value.AsValueMap() { + out[key] = ctyToGeneric(t, child) + } + return out + case value.Type().IsTupleType() || value.Type().IsListType(): + out := []any{} + for _, child := range value.AsValueSlice() { + out = append(out, ctyToGeneric(t, child)) + } + return out + default: + t.Fatalf("unsupported value type %s", value.Type().FriendlyName()) + return nil + } +} From a6af18597359415fa935a01ffa4d0ac04609507c Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 16 Nov 2026 10:11:53 -0600 Subject: [PATCH 22/25] fix(terraform): encode secret env strings with HCL-only escapes json.Marshal emits \b and \f, which HCL string literals reject, so secrets containing backspace or form feed did not round-trip through TF_VAR_ values for variables Terraform parses as HCL. Encode scalar, collection value and object key strings with HCL escapes only, keeping ${ and %{ escaped. Co-Authored-By: Claude Sonnet 5.5 --- .../terraform/execute_environment.go | 142 ++++++++++++------ .../terraform/generate_varfile_test.go | 59 ++++++++ 2 files changed, 158 insertions(+), 43 deletions(-) diff --git a/pkg/component/terraform/execute_environment.go b/pkg/component/terraform/execute_environment.go index f0c8dd363f7..1ee258d03bd 100644 --- a/pkg/component/terraform/execute_environment.go +++ b/pkg/component/terraform/execute_environment.go @@ -7,6 +7,8 @@ import ( os "os" filepath "path/filepath" slices "slices" + sort "sort" + strconv "strconv" strings "strings" errUtils "github.com/cloudposse/atmos/errors" @@ -444,27 +446,24 @@ func (e *Executor) SetupComponentAuthForCLI(atmosConfig *schema.AtmosConfigurati // terraformVarEnvPrefix is the prefix Terraform and OpenTofu read variable values from the environment with. const terraformVarEnvPrefix = "TF_VAR_" -// secretEnvForDeclaredTypes renders secret variables as TF_VAR_ entries, encoding each string for the way +// secretEnvForDeclaredTypes renders secret variables as TF_VAR_ entries, encoding each value for the way // Terraform reads it. Terraform parses a TF_VAR_ value as HCL when the variable is declared `any` or with a // complex type, and uses it as a literal when the variable is declared string, number, or bool, or has no -// type. A string for an HCL-parsed variable is therefore written as a quoted HCL string, with template -// sequences escaped so that the value is never interpreted as an interpolation. Every other value is -// encoded by tfvars.SecretEnv. +// type. A value for an HCL-parsed variable is therefore written as an HCL literal whose strings use only HCL +// escapes, with template sequences escaped so that the value is never interpreted as an interpolation, and +// whose numbers keep their exact digits. Every other value is encoded by tfvars.SecretEnv. func secretEnvForDeclaredTypes(componentSection map[string]any, secret map[string]any) ([]string, error) { plain := make(map[string]any, len(secret)) - var quoted []string + var literals []string for name, value := range secret { typeText, declared := TerraformDeclaredVarType(componentSection, name) - if text, isString := value.(string); isString && declared && terraformParsesAsHCL(typeText) { - quoted = append(quoted, terraformVarEnvPrefix+name+"="+quoteHCLString(text)) - continue - } - if _, isString := value.(string); !isString && declared && terraformParsesAsHCL(typeText) { - escaped, err := escapeHCLTemplateSequences(value) + if declared && terraformParsesAsHCL(typeText) { + literal, err := encodeHCLLiteral(value) if err != nil { return nil, fmt.Errorf("encoding secret variable %q for TF_VAR_ injection: %w", name, err) } - value = escaped + literals = append(literals, terraformVarEnvPrefix+name+"="+literal) + continue } plain[name] = value } @@ -472,7 +471,7 @@ func secretEnvForDeclaredTypes(componentSection map[string]any, secret map[strin if err != nil { return nil, err } - return append(env, quoted...), nil + return append(env, literals...), nil } // terraformParsesAsHCL reports whether Terraform parses a TF_VAR_ value for a variable declared with this type @@ -486,54 +485,111 @@ func terraformParsesAsHCL(typeText string) bool { } } -// quoteHCLString renders text as a quoted HCL string literal. The JSON string escapes are valid HCL escapes, -// and `${` and `%{` are doubled so that the value cannot start a template interpolation or directive. +const ( + // The value of asciiControlLimit is the first printable ASCII code point; lower code points are control characters. + asciiControlLimit = 0x20 + // The value of asciiDelete is the ASCII DEL control character. + asciiDelete = 0x7f + // The value of float64Bits is the bit size of a float64. + float64Bits = 64 +) + +// quoteHCLString renders text as a quoted HCL string literal. Only the escapes HCL defines are used (JSON +// also allows \b and \f, which HCL string literals do not), and `${` and `%{` are doubled so that the value +// cannot start a template interpolation or directive. func quoteHCLString(text string) string { - encoded, _ := json.Marshal(text) //nolint:errchkjson // A string always encodes. - quoted := string(encoded) - return escapeHCLTemplateText(quoted) + var b strings.Builder + b.WriteByte('"') + for _, r := range escapeHCLTemplateText(text) { + switch { + case r == '"': + b.WriteString(`\"`) + case r == '\\': + b.WriteString(`\\`) + case r == '\n': + b.WriteString(`\n`) + case r == '\r': + b.WriteString(`\r`) + case r == '\t': + b.WriteString(`\t`) + case r < asciiControlLimit || r == asciiDelete: + fmt.Fprintf(&b, `\u%04X`, r) + default: + b.WriteRune(r) + } + } + b.WriteByte('"') + return b.String() } -// escapeHCLTemplateSequences recursively escapes `${` and `%{` in every string inside a collection value, both -// object values and object keys, so that Terraform parsing the JSON-encoded environment value as HCL keeps the -// text literal. Scalars other than strings are returned unchanged. -func escapeHCLTemplateSequences(value any) (any, error) { - switch value.(type) { - case nil, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: - return value, nil +// encodeHCLLiteral renders any secret value as an HCL expression that Terraform can parse from a TF_VAR_ +// environment value. Typed maps and slices are first normalized to generic JSON values, keeping numbers as +// generic values. Strings and object keys are +// quoted with quoteHCLString. +func encodeHCLLiteral(value any) (string, error) { + if text, isString := value.(string); isString { + return quoteHCLString(text), nil } - // Normalize typed maps and slices to generic JSON values so the walk sees every nested string. encoded, err := json.Marshal(value) if err != nil { - return nil, err + return "", err } var generic any if err := json.Unmarshal(encoded, &generic); err != nil { - return nil, err + return "", err } - return walkEscapeHCLTemplateSequences(generic), nil + var b strings.Builder + writeHCLLiteral(&b, generic) + return b.String(), nil } -// walkEscapeHCLTemplateSequences escapes template introducers in the strings of an already generic value. -func walkEscapeHCLTemplateSequences(value any) any { +// writeHCLLiteral writes an already generic JSON value as an HCL expression with sorted object keys. +func writeHCLLiteral(b *strings.Builder, value any) { switch typed := value.(type) { + case nil: + b.WriteString("null") + case bool: + b.WriteString(strconv.FormatBool(typed)) + case float64: + b.WriteString(strconv.FormatFloat(typed, 'g', -1, float64Bits)) case string: - return escapeHCLTemplateText(typed) + b.WriteString(quoteHCLString(typed)) case map[string]any: - out := make(map[string]any, len(typed)) - for key, child := range typed { - out[escapeHCLTemplateText(key)] = walkEscapeHCLTemplateSequences(child) - } - return out + writeHCLObject(b, typed) case []any: - out := make([]any, len(typed)) - for i, child := range typed { - out[i] = walkEscapeHCLTemplateSequences(child) + writeHCLTuple(b, typed) + } +} + +// writeHCLObject writes a generic JSON object as an HCL object with sorted keys. +func writeHCLObject(b *strings.Builder, object map[string]any) { + keys := make([]string, 0, len(object)) + for key := range object { + keys = append(keys, key) + } + sort.Strings(keys) + b.WriteByte('{') + for i, key := range keys { + if i > 0 { + b.WriteByte(',') } - return out - default: - return value + b.WriteString(quoteHCLString(key)) + b.WriteByte(':') + writeHCLLiteral(b, object[key]) + } + b.WriteByte('}') +} + +// writeHCLTuple writes a generic JSON array as an HCL tuple. +func writeHCLTuple(b *strings.Builder, items []any) { + b.WriteByte('[') + for i, child := range items { + if i > 0 { + b.WriteByte(',') + } + writeHCLLiteral(b, child) } + b.WriteByte(']') } // escapeHCLTemplateText doubles `${` and `%{` so HCL does not start a template interpolation or directive. diff --git a/pkg/component/terraform/generate_varfile_test.go b/pkg/component/terraform/generate_varfile_test.go index acb181df879..555f2ea8fdf 100644 --- a/pkg/component/terraform/generate_varfile_test.go +++ b/pkg/component/terraform/generate_varfile_test.go @@ -705,6 +705,65 @@ func TestSecretEnvForDeclaredTypesCollectionsRoundTripThroughHCL(t *testing.T) { } } +// evalHCLEnvValue parses a TF_VAR_ environment entry the way Terraform does for an HCL-typed variable. +func evalHCLEnvValue(t *testing.T, entry, name string) cty.Value { + t.Helper() + text := strings.TrimPrefix(entry, "TF_VAR_"+name+"=") + expr, diags := hclsyntax.ParseExpression([]byte(text), "env", hcl.InitialPos) + require.False(t, diags.HasErrors(), diags.Error()) + got, diags := expr.Value(nil) + require.False(t, diags.HasErrors(), diags.Error()) + return got +} + +// TestSecretEnvForDeclaredTypesUsesOnlyHCLEscapes verifies that characters JSON escapes in ways HCL string +// literals do not accept (backspace and form feed) survive the round trip, for scalars, collection values, and +// object keys, and that primitive-typed and untyped variables stay verbatim. +func TestSecretEnvForDeclaredTypesUsesOnlyHCLEscapes(t *testing.T) { + module := &tfconfig.Module{Variables: map[string]*tfconfig.Variable{ + "typed_any": {Name: "typed_any", Type: "any"}, + "typed_map": {Name: "typed_map", Type: "map(string)"}, + "typed_list": {Name: "typed_list", Type: "list(string)"}, + "typed_string": {Name: "typed_string", Type: "string"}, + }} + section := map[string]any{"component_info": map[string]any{"terraform_config": module}} + const special = "a\bb\fc\x00d\x1fe\x7ff\ng\rh\ti\"j\\k${l}%{m}" + + t.Run("scalar", func(t *testing.T) { + env, err := secretEnvForDeclaredTypes(section, map[string]any{"typed_any": special}) + require.NoError(t, err) + require.Len(t, env, 1) + assert.Equal(t, special, evalHCLEnvValue(t, env[0], "typed_any").AsString()) + }) + + t.Run("collections with keys", func(t *testing.T) { + env, err := secretEnvForDeclaredTypes(section, map[string]any{ + "typed_map": map[string]string{"k\b\f": "v\f\b", special: special}, + "typed_list": []string{"\b", "\f", special}, + }) + require.NoError(t, err) + require.Len(t, env, 2) + for _, entry := range env { + name := "typed_map" + if strings.HasPrefix(entry, "TF_VAR_typed_list=") { + name = "typed_list" + } + got := ctyToGeneric(t, evalHCLEnvValue(t, entry, name)) + if name == "typed_map" { + assert.Equal(t, map[string]any{"k\b\f": "v\f\b", special: special}, got) + } else { + assert.Equal(t, []any{"\b", "\f", special}, got) + } + } + }) + + t.Run("primitive typed and untyped stay verbatim", func(t *testing.T) { + env, err := secretEnvForDeclaredTypes(section, map[string]any{"typed_string": "a\bb\f", "untyped": "a\bb\f"}) + require.NoError(t, err) + assert.ElementsMatch(t, []string{"TF_VAR_typed_string=a\bb\f", "TF_VAR_untyped=a\bb\f"}, env) + }) +} + // ctyToGeneric converts an evaluated HCL value into maps, slices, and strings. func ctyToGeneric(t *testing.T, value cty.Value) any { t.Helper() From 97093c08e5995b29a38ad813fc0e6c61aa208b1d Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 16 Nov 2026 10:15:25 -0600 Subject: [PATCH 23/25] fix(terraform): keep numeric precision in collection secret env values Normalizing a collection through json.Unmarshal into any turned every number into float64, which silently rounds integers above 2^53. Decode with UseNumber and write numbers as their exact text. Co-Authored-By: Claude Sonnet 5.5 --- .../terraform/execute_environment.go | 13 +++++----- .../terraform/generate_varfile_test.go | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/pkg/component/terraform/execute_environment.go b/pkg/component/terraform/execute_environment.go index 1ee258d03bd..689e26d89f1 100644 --- a/pkg/component/terraform/execute_environment.go +++ b/pkg/component/terraform/execute_environment.go @@ -1,6 +1,7 @@ package terraform import ( + "bytes" "encoding/json" errors "errors" fmt "fmt" @@ -490,8 +491,6 @@ const ( asciiControlLimit = 0x20 // The value of asciiDelete is the ASCII DEL control character. asciiDelete = 0x7f - // The value of float64Bits is the bit size of a float64. - float64Bits = 64 ) // quoteHCLString renders text as a quoted HCL string literal. Only the escapes HCL defines are used (JSON @@ -524,7 +523,7 @@ func quoteHCLString(text string) string { // encodeHCLLiteral renders any secret value as an HCL expression that Terraform can parse from a TF_VAR_ // environment value. Typed maps and slices are first normalized to generic JSON values, keeping numbers as -// generic values. Strings and object keys are +// their exact text so that integers beyond 2^53 are not rounded through float64. Strings and object keys are // quoted with quoteHCLString. func encodeHCLLiteral(value any) (string, error) { if text, isString := value.(string); isString { @@ -534,8 +533,10 @@ func encodeHCLLiteral(value any) (string, error) { if err != nil { return "", err } + decoder := json.NewDecoder(bytes.NewReader(encoded)) + decoder.UseNumber() var generic any - if err := json.Unmarshal(encoded, &generic); err != nil { + if err := decoder.Decode(&generic); err != nil { return "", err } var b strings.Builder @@ -550,8 +551,8 @@ func writeHCLLiteral(b *strings.Builder, value any) { b.WriteString("null") case bool: b.WriteString(strconv.FormatBool(typed)) - case float64: - b.WriteString(strconv.FormatFloat(typed, 'g', -1, float64Bits)) + case json.Number: + b.WriteString(typed.String()) case string: b.WriteString(quoteHCLString(typed)) case map[string]any: diff --git a/pkg/component/terraform/generate_varfile_test.go b/pkg/component/terraform/generate_varfile_test.go index 555f2ea8fdf..8d57e30c978 100644 --- a/pkg/component/terraform/generate_varfile_test.go +++ b/pkg/component/terraform/generate_varfile_test.go @@ -764,6 +764,30 @@ func TestSecretEnvForDeclaredTypesUsesOnlyHCLEscapes(t *testing.T) { }) } +// TestSecretEnvForDeclaredTypesKeepsLargeIntegersExact verifies that integers beyond 2^53 are not rounded +// through float64 inside nested collections, while other nested scalars are unchanged. +func TestSecretEnvForDeclaredTypesKeepsLargeIntegersExact(t *testing.T) { + module := &tfconfig.Module{Variables: map[string]*tfconfig.Variable{ + "typed_any": {Name: "typed_any", Type: "any"}, + }} + section := map[string]any{"component_info": map[string]any{"terraform_config": module}} + + env, err := secretEnvForDeclaredTypes(section, map[string]any{"typed_any": map[string]any{ + "id": int64(9007199254740993), + "nested": []any{map[string]any{"big": uint64(18446744073709551615), "small": 7, "ok": true, "none": nil, "ratio": 0.5}}, + }}) + require.NoError(t, err) + require.Len(t, env, 1) + assert.Contains(t, env[0], `"id":9007199254740993`) + assert.Contains(t, env[0], `"big":18446744073709551615`) + + got := evalHCLEnvValue(t, env[0], "typed_any") + id := got.GetAttr("id").AsBigFloat() + assert.Equal(t, "9007199254740993", id.Text('f', 0)) + big := got.GetAttr("nested").AsValueSlice()[0].GetAttr("big").AsBigFloat() + assert.Equal(t, "18446744073709551615", big.Text('f', 0)) +} + // ctyToGeneric converts an evaluated HCL value into maps, slices, and strings. func ctyToGeneric(t *testing.T, value cty.Value) any { t.Helper() From a529951f02be0dfb20215ecd9862a6b45b61a3ba Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Tue, 17 Nov 2026 06:42:12 -0600 Subject: [PATCH 24/25] fix(terraform): classify secret environment encoding failures --- pkg/component/terraform/execute_environment.go | 2 +- pkg/component/terraform/generate_varfile_test.go | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/pkg/component/terraform/execute_environment.go b/pkg/component/terraform/execute_environment.go index 689e26d89f1..288f73630c1 100644 --- a/pkg/component/terraform/execute_environment.go +++ b/pkg/component/terraform/execute_environment.go @@ -461,7 +461,7 @@ func secretEnvForDeclaredTypes(componentSection map[string]any, secret map[strin if declared && terraformParsesAsHCL(typeText) { literal, err := encodeHCLLiteral(value) if err != nil { - return nil, fmt.Errorf("encoding secret variable %q for TF_VAR_ injection: %w", name, err) + return nil, fmt.Errorf("%w: encoding secret variable %q for TF_VAR_ injection: %w", errUtils.ErrInvalidComponentVars, name, err) } literals = append(literals, terraformVarEnvPrefix+name+"="+literal) continue diff --git a/pkg/component/terraform/generate_varfile_test.go b/pkg/component/terraform/generate_varfile_test.go index 8d57e30c978..17de53ffd25 100644 --- a/pkg/component/terraform/generate_varfile_test.go +++ b/pkg/component/terraform/generate_varfile_test.go @@ -3,6 +3,7 @@ package terraform import ( bytes "bytes" context "context" + json "encoding/json" fmt "fmt" os "os" filepath "path/filepath" @@ -811,3 +812,17 @@ func ctyToGeneric(t *testing.T, value cty.Value) any { return nil } } + +// A secret value that cannot be encoded fails with the vars-section sentinel and keeps the encoder's +// error in the chain, so the cause stays visible to the caller. +func TestSecretEnvForDeclaredTypesWrapsEncodingFailures(t *testing.T) { + module := &tfconfig.Module{Variables: map[string]*tfconfig.Variable{"typed_any": {Name: "typed_any", Type: "any"}}} + section := map[string]any{"component_info": map[string]any{"terraform_config": module}} + + _, err := secretEnvForDeclaredTypes(section, map[string]any{"typed_any": map[string]any{"callback": func() {}}}) + + require.ErrorIs(t, err, errUtils.ErrInvalidComponentVars) + var unsupported *json.UnsupportedTypeError + require.ErrorAs(t, err, &unsupported, "the JSON encoding error stays in the chain") + assert.Contains(t, err.Error(), `"typed_any"`) +} From 5be4945a572429fb6b13a527c85e38708b57ada7 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Tue, 17 Nov 2026 12:10:21 -0600 Subject: [PATCH 25/25] test(git): separate HTTP cooldown timing from publisher contention Keep real Retry-After timing on the public Push path and exercise all twelve contending publishers with independent retry clocks and a real execution deadline. Drain all outcomes with publisher diagnostics while preserving attempt, budget, cancellation and commit-content checks. Consolidate unchanged component host declarations into the provider contract file to retain the PR's 149-file review limit. --- pkg/component/host_contracts.go | 129 ------------------ pkg/component/provider.go | 124 +++++++++++++++++ .../cli/rate_limit_integration_test.go | 52 ++++++- 3 files changed, 169 insertions(+), 136 deletions(-) delete mode 100644 pkg/component/host_contracts.go diff --git a/pkg/component/host_contracts.go b/pkg/component/host_contracts.go deleted file mode 100644 index dec78415ed8..00000000000 --- a/pkg/component/host_contracts.go +++ /dev/null @@ -1,129 +0,0 @@ -package component - -import ( - cobra "github.com/spf13/cobra" - - auth "github.com/cloudposse/atmos/pkg/auth" - degradation "github.com/cloudposse/atmos/pkg/degradation" - m "github.com/cloudposse/atmos/pkg/merge" - schema "github.com/cloudposse/atmos/pkg/schema" -) - -type ExecuteDescribeComponentParams struct { - // ResolveSecrets retrieves actual secrets for internal value-producing callers. - // The default preserves credential-free, masked inspection. - ResolveSecrets bool - AtmosConfig *schema.AtmosConfiguration // Optional: Use provided config instead of initializing new one. - Component string - Stack string - ComponentType string // Optional: if set, only try this component type. - ProcessTemplates bool - ProcessYamlFunctions bool - UseMocks bool - Skip []string - AuthManager auth.AuthManager - AuthDisabled bool - ErrorOptions DescribeStacksErrorOptions - // CommandPath names the CLI command that needs the component, such as - // ["secret", "shell"], for component admission. Empty means `describe component`. - CommandPath []string -} - -type ( - OnErrorMode string - DescribeStacksErrorOptions struct { - // StaticOnly inspects merged configuration without auth, expression evaluation, workspace - // synthesis, or evaluated-map cache reuse. Stack name templates allow only Go's pure built-ins. - StaticOnly bool - // EvaluationPaths optionally narrows list columns to fields within sections. - EvaluationPaths [][]string - OnError OnErrorMode - OnWarning func(degradation.Warning) - // OnProgress, when non-nil, is called once per stack file immediately before it's - // processed — stackFile is the file name, index is 0-based, total is the number of - // stack files describe-stacks will visit this call (both counted before filtering by - // includeEmptyStacks, so total is a stable upper bound even though some files are - // skipped). Bundled onto this struct (rather than a new parameter) purely for plumbing - // convenience — it shares the same threading path as OnError/OnWarning. Optional; a nil - // OnProgress is a no-op, matching every existing caller's behavior exactly. - OnProgress func(stackFile string, index, total int) - // StrictAuth, when true, keeps a component-specific auth-manager construction failure - // fatal even under OnErrorWarn (see describeStacksProcessor.resolveComponentAuthManager) -- - // only the recoverable per-value YAML-function errors (e.g. !terraform.state hitting a - // not-yet-provisioned backend) degrade. The zero value (false) preserves the original - // list/describe --error-mode=warn behavior, where a component whose own declared identity - // can't be resolved gracefully falls back to the parent AuthManager instead of aborting -- - // appropriate for merely listing/describing stacks, but not for a Terraform preflight - // about to run real commands with whatever credentials it resolves. - StrictAuth bool - // ExtraComponentTypes names additional component kinds, such as custom kinds declared - // by custom commands, to process after the built-in kinds. Each kind must already be - // registered with the component registry for its stack section to be available. The - // zero value leaves the described component kinds exactly as before. - ExtraComponentTypes []string - } -) - -type DescribeAffectedCmdArgs struct { - CLIConfig *schema.AtmosConfiguration - Base string // Unified base commit (ref or SHA). Takes precedence over Ref/SHA. - CloneTargetRef bool - Format string - IncludeDependents bool - IncludeSettings bool - IncludeSpaceliftAdminStacks bool - OutputFile string - GithubOutputFile string // Output file for $GITHUB_OUTPUT format (key=value). - Ref string - RepoPath string - SHA string - SSHKeyPath string - SSHKeyPassword string - Verbose bool - Upload bool - Stack string - Query string - ProcessTemplates bool - ProcessYamlFunctions bool - Skip []string - ExcludeLocked bool - Tags []string // Keep only components whose `metadata.tags` contain any of these (from --tags). - LabelsRaw string // Raw --labels value (comma-separated key=value pairs). - Labels map[string]string // Parsed --labels: keep only components whose `metadata.labels` contain all of these. - TagsEnvVar string // Environment variable that supplied Tags (for example ATMOS_TAGS); empty when Tags came from the command line or are unset. - LabelsEnvVar string // Environment variable that supplied Labels; empty when Labels came from the command line or are unset. - Flatten bool // Lift every dependent into the top-level list as `affected: dependent` (from --flatten); requires IncludeDependents. - FlattenEnvVar string // Environment variable that supplied Flatten; empty when it came from the command line or is unset. - AuthManager auth.AuthManager // Optional: Auth manager for credential management (from --identity flag). - AuthDisabled bool // True when --identity=false (or alias) explicitly disables authentication; routes stack resolution to ExecuteDescribeStacksWithAuthDisabled. - HeadSHAOverride string // PR head SHA from CI event payload, used for upload correlation with Atmos Pro. - CIEventType string // CI event type (e.g., "pull_request", "push") for upload validation. - TargetBranch string // PR target branch (e.g., "main") used to auto-fetch when refs are missing locally. - ErrorMode string // How to handle recoverable errors: "strict" (default), "warn", or "silent". - Cmd *cobra.Command // The invoking Cobra command, used to derive Flags for the exec-metadata sync capture (proexec.FlagsFromCommand). -} - -const ( - OnErrorStrict OnErrorMode = "strict" - OnErrorWarn OnErrorMode = "warn" -) - -// ComponentDeferredContexts holds the per-section *merge.DeferredMergeContext produced while -// merging a single component's configuration, keyed by section name (vars, settings, env, auth, -// providers, required_providers, hooks, test, generate — see the cfg.*SectionName constants used -// as keys). Each context tracks the deferred YAML functions (!template, !terraform.output, -// !labels, etc.) collected for that section during the raw structural merge; a later, -// per-invocation stage resolves them with a real YAMLFunctionProcessor and deep-merges the result -// against any concrete override at the same path. -type ComponentDeferredContexts map[string]*m.DeferredMergeContext - -// StackComponentDeferredContexts holds, for a single stack, every component's -// ComponentDeferredContexts keyed by [componentType][component] (e.g. -// ["terraform"]["vpc"]). It is the shape ProcessStackConfig produces per stack. -type StackComponentDeferredContexts map[string]map[string]ComponentDeferredContexts - -// AllStacksDeferredContexts holds every stack's deferred contexts keyed by -// [stack][componentType][component]. This is the shape threaded from -// ProcessYAMLConfigFiles through FindStacksMap to the per-invocation Stage 3 -// resolver (see resolveDeferredYamlFunctions). -type AllStacksDeferredContexts map[string]StackComponentDeferredContexts diff --git a/pkg/component/provider.go b/pkg/component/provider.go index a94d1360e1f..59c547d7ac9 100644 --- a/pkg/component/provider.go +++ b/pkg/component/provider.go @@ -3,6 +3,11 @@ package component import ( "context" + "github.com/spf13/cobra" + + "github.com/cloudposse/atmos/pkg/auth" + "github.com/cloudposse/atmos/pkg/degradation" + m "github.com/cloudposse/atmos/pkg/merge" "github.com/cloudposse/atmos/pkg/schema" ) @@ -81,3 +86,122 @@ type ComponentInfo struct { Commands []string Description string } + +type ExecuteDescribeComponentParams struct { + // ResolveSecrets retrieves actual secrets for internal value-producing callers. + // The default preserves credential-free, masked inspection. + ResolveSecrets bool + AtmosConfig *schema.AtmosConfiguration // Optional: Use provided config instead of initializing new one. + Component string + Stack string + ComponentType string // Optional: if set, only try this component type. + ProcessTemplates bool + ProcessYamlFunctions bool + UseMocks bool + Skip []string + AuthManager auth.AuthManager + AuthDisabled bool + ErrorOptions DescribeStacksErrorOptions + // CommandPath names the CLI command that needs the component, such as + // ["secret", "shell"], for component admission. Empty means `describe component`. + CommandPath []string +} + +type ( + OnErrorMode string + DescribeStacksErrorOptions struct { + // StaticOnly inspects merged configuration without auth, expression evaluation, workspace + // synthesis, or evaluated-map cache reuse. Stack name templates allow only Go's pure built-ins. + StaticOnly bool + // EvaluationPaths optionally narrows list columns to fields within sections. + EvaluationPaths [][]string + OnError OnErrorMode + OnWarning func(degradation.Warning) + // OnProgress, when non-nil, is called once per stack file immediately before it's + // processed — stackFile is the file name, index is 0-based, total is the number of + // stack files describe-stacks will visit this call (both counted before filtering by + // includeEmptyStacks, so total is a stable upper bound even though some files are + // skipped). Bundled onto this struct (rather than a new parameter) purely for plumbing + // convenience — it shares the same threading path as OnError/OnWarning. Optional; a nil + // OnProgress is a no-op, matching every existing caller's behavior exactly. + OnProgress func(stackFile string, index, total int) + // StrictAuth, when true, keeps a component-specific auth-manager construction failure + // fatal even under OnErrorWarn (see describeStacksProcessor.resolveComponentAuthManager) -- + // only the recoverable per-value YAML-function errors (e.g. !terraform.state hitting a + // not-yet-provisioned backend) degrade. The zero value (false) preserves the original + // list/describe --error-mode=warn behavior, where a component whose own declared identity + // can't be resolved gracefully falls back to the parent AuthManager instead of aborting -- + // appropriate for merely listing/describing stacks, but not for a Terraform preflight + // about to run real commands with whatever credentials it resolves. + StrictAuth bool + // ExtraComponentTypes names additional component kinds, such as custom kinds declared + // by custom commands, to process after the built-in kinds. Each kind must already be + // registered with the component registry for its stack section to be available. The + // zero value leaves the described component kinds exactly as before. + ExtraComponentTypes []string + } +) + +type DescribeAffectedCmdArgs struct { + CLIConfig *schema.AtmosConfiguration + Base string // Unified base commit (ref or SHA). Takes precedence over Ref/SHA. + CloneTargetRef bool + Format string + IncludeDependents bool + IncludeSettings bool + IncludeSpaceliftAdminStacks bool + OutputFile string + GithubOutputFile string // Output file for $GITHUB_OUTPUT format (key=value). + Ref string + RepoPath string + SHA string + SSHKeyPath string + SSHKeyPassword string + Verbose bool + Upload bool + Stack string + Query string + ProcessTemplates bool + ProcessYamlFunctions bool + Skip []string + ExcludeLocked bool + Tags []string // Keep only components whose `metadata.tags` contain any of these (from --tags). + LabelsRaw string // Raw --labels value (comma-separated key=value pairs). + Labels map[string]string // Parsed --labels: keep only components whose `metadata.labels` contain all of these. + TagsEnvVar string // Environment variable that supplied Tags (for example ATMOS_TAGS); empty when Tags came from the command line or are unset. + LabelsEnvVar string // Environment variable that supplied Labels; empty when Labels came from the command line or are unset. + Flatten bool // Lift every dependent into the top-level list as `affected: dependent` (from --flatten); requires IncludeDependents. + FlattenEnvVar string // Environment variable that supplied Flatten; empty when it came from the command line or is unset. + AuthManager auth.AuthManager // Optional: Auth manager for credential management (from --identity flag). + AuthDisabled bool // True when --identity=false (or alias) explicitly disables authentication; routes stack resolution to ExecuteDescribeStacksWithAuthDisabled. + HeadSHAOverride string // PR head SHA from CI event payload, used for upload correlation with Atmos Pro. + CIEventType string // CI event type (e.g., "pull_request", "push") for upload validation. + TargetBranch string // PR target branch (e.g., "main") used to auto-fetch when refs are missing locally. + ErrorMode string // How to handle recoverable errors: "strict" (default), "warn", or "silent". + Cmd *cobra.Command // The invoking Cobra command, used to derive Flags for the exec-metadata sync capture (proexec.FlagsFromCommand). +} + +const ( + OnErrorStrict OnErrorMode = "strict" + OnErrorWarn OnErrorMode = "warn" +) + +// ComponentDeferredContexts holds the per-section *merge.DeferredMergeContext produced while +// merging a single component's configuration, keyed by section name (vars, settings, env, auth, +// providers, required_providers, hooks, test, generate — see the cfg.*SectionName constants used +// as keys). Each context tracks the deferred YAML functions (!template, !terraform.output, +// !labels, etc.) collected for that section during the raw structural merge; a later, +// per-invocation stage resolves them with a real YAMLFunctionProcessor and deep-merges the result +// against any concrete override at the same path. +type ComponentDeferredContexts map[string]*m.DeferredMergeContext + +// StackComponentDeferredContexts holds, for a single stack, every component's +// ComponentDeferredContexts keyed by [componentType][component] (e.g. +// ["terraform"]["vpc"]). It is the shape ProcessStackConfig produces per stack. +type StackComponentDeferredContexts map[string]map[string]ComponentDeferredContexts + +// AllStacksDeferredContexts holds every stack's deferred contexts keyed by +// [stack][componentType][component]. This is the shape threaded from +// ProcessYAMLConfigFiles through FindStacksMap to the per-invocation Stage 3 +// resolver (see resolveDeferredYamlFunctions). +type AllStacksDeferredContexts map[string]StackComponentDeferredContexts diff --git a/pkg/git/providers/cli/rate_limit_integration_test.go b/pkg/git/providers/cli/rate_limit_integration_test.go index 6516c53a3dd..7ca041d62cc 100644 --- a/pkg/git/providers/cli/rate_limit_integration_test.go +++ b/pkg/git/providers/cli/rate_limit_integration_test.go @@ -1,6 +1,7 @@ package cli import ( + "context" "fmt" "net/http" "net/http/cgi" @@ -11,16 +12,31 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" errUtils "github.com/cloudposse/atmos/errors" atmosgit "github.com/cloudposse/atmos/pkg/git" + "github.com/cloudposse/atmos/pkg/retry" "github.com/cloudposse/atmos/pkg/schema" ) +// Measure a real Retry-After interval through the public Push path without +// coupling the timing assertion to twelve competing Git subprocesses. +func TestHTTPRetryAfterWallClockCooldown(t *testing.T) { + testHTTPPublishers(t, 1, false) +} + // Exercise twelve independent publishers against the same branch over smart HTTP. // Each receives a 429 before contending for the ref; no lock serializes publication. -func TestIndependentPublishersHonorHTTPCooldown(t *testing.T) { +// Only retry scheduling is virtual: Git, HTTP, recovery, and the execution deadline +// remain real. TestHTTPRetryAfterWallClockCooldown separately checks elapsed time. +func TestHTTPConcurrentPublishersRecoverFromThrottling(t *testing.T) { + testHTTPPublishers(t, 12, true) +} + +func testHTTPPublishers(t *testing.T, publishers int, virtualScheduling bool) { + t.Helper() requireGitBinary(t) env := gitTestEnv(t) root := t.TempDir() @@ -56,7 +72,6 @@ func TestIndependentPublishersHonorHTTPCooldown(t *testing.T) { backend.ServeHTTP(w, r) })) defer server.Close() - const publishers = 12 workdirs := make([]string, publishers) for i := range workdirs { workdirs[i] = t.TempDir() @@ -66,22 +81,45 @@ func TestIndependentPublishersHonorHTTPCooldown(t *testing.T) { gitRun(t, env, workdirs[i], "commit", "-m", fmt.Sprintf("publish app %d", i)) } start := make(chan struct{}) - results := make(chan error, publishers) + type result struct { + publisher int + err error + waits []time.Duration + } + results := make(chan result, publishers) + ctx, cancel := context.WithTimeout(t.Context(), 30*time.Second) + defer cancel() for i, dir := range workdirs { go func() { <-start - results <- New().Push(t.Context(), &atmosgit.PushOptions{RepoContext: atmosgit.RepoContext{Workdir: dir, Env: env, Remote: fmt.Sprintf("%s/client-%d/repo.git", server.URL, i), Branch: "main"}, Retry: &schema.RetryConfig{MaxAttempts: new(16), MaxElapsedTime: new(30 * time.Second), RandomJitter: new(0.5), BackoffStrategy: schema.BackoffExponential, InitialDelay: new(50 * time.Millisecond), MaxDelay: new(time.Second)}}) + opts := &atmosgit.PushOptions{RepoContext: atmosgit.RepoContext{Workdir: dir, Env: env, Remote: fmt.Sprintf("%s/client-%d/repo.git", server.URL, i), Branch: "main"}, Retry: &schema.RetryConfig{MaxAttempts: new(16), MaxElapsedTime: new(30 * time.Second), RandomJitter: new(0.5), BackoffStrategy: schema.BackoffExponential, InitialDelay: new(50 * time.Millisecond), MaxDelay: new(time.Second)}} + outcome := result{publisher: i} + if virtualScheduling { + clock := &advancingPushClock{} + outcome.err = New().pushWithRetry(ctx, opts, opts.Retry, retry.WithClock(clock)) + outcome.waits = clock.waits + } else { + outcome.err = New().Push(ctx, opts) + } + results <- outcome }() } close(start) for range publishers { - require.NoError(t, <-results) + outcome := <-results + assert.NoError(t, outcome.err, "publisher %d; scheduled waits %v", outcome.publisher, outcome.waits) + if virtualScheduling && assert.NotEmpty(t, outcome.waits, "publisher %d", outcome.publisher) { + assert.GreaterOrEqual(t, outcome.waits[0], time.Second, "publisher %d", outcome.publisher) + assert.LessOrEqual(t, outcome.waits[0], 1500*time.Millisecond, "publisher %d", outcome.publisher) + } } mu.Lock() defer mu.Unlock() require.Len(t, second, publishers) - for client, elapsed := range second { - require.GreaterOrEqual(t, elapsed, time.Second, client) + if !virtualScheduling { + for client, elapsed := range second { + require.GreaterOrEqual(t, elapsed, time.Second, client) + } } for i := range publishers { require.Equal(t, "image: v2\n", gitRun(t, env, bare, "show", fmt.Sprintf("main:app-%d.yaml", i)))