From eac7507dbaea4f3493357f31269538d516ab9ab7 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Thu, 29 Oct 2026 21:33:06 -0500 Subject: [PATCH 01/52] docs(plugin): define package identity and repository naming --- docs/prd/plugins/authoring.md | 23 +++++++++++ plugins/terragrunt/schemas/config.json | 47 ++++++++++++++++++++- website/docs/cli/commands/plugin/usage.mdx | 48 ++++++++++++++++++++-- 3 files changed, 113 insertions(+), 5 deletions(-) diff --git a/docs/prd/plugins/authoring.md b/docs/prd/plugins/authoring.md index 1096e6a6af6..28f272c3529 100644 --- a/docs/prd/plugins/authoring.md +++ b/docs/prd/plugins/authoring.md @@ -22,6 +22,29 @@ SDK development stays in this repository under the nested `sdk/` module. `atmos Author testing, local `path` packages, publishing, and signing are also subject to [plugin policies](policy.md). A company policy that denies local development must explicitly permit the author workflow in its trusted development environment. +## Package identity and repository naming + +Use `/` for `metadata.name`. The publisher maintains the Atmos integration. Use the product name for the package and its default consumer alias. For a standalone source repository, use `atmos-plugin-` to identify its purpose outside the Atmos catalog. The repository prefix is not part of the package ID. + +| Purpose | Terragrunt example | +|---|---| +| Package ID (`metadata.name`) | `cloudposse/terragrunt` | +| Default product alias (`plugins.packages.`) | `terragrunt` | +| Source repository (`metadata.repository`) | `https://github.com/cloudposse/atmos` | +| Source directory | `plugins/terragrunt` | +| Release location | Explicit OCI reference, recorded with its digest in the lockfile | +| Standalone repository, if created | `cloudposse/atmos-plugin-terragrunt` | + +The independently versioned package is the identity unit. Several packages can share a source repository. Moving a package to a standalone repository must not require consumers to change its ID or alias. Repository naming is an authoring convention, not a requirement to move existing plugins out of the monorepo. + +This follows [Terraform's distinction between provider addresses and local names](https://developer.hashicorp.com/terraform/language/providers/requirements). Terraform's public registry uses a separate [`terraform-provider-` repository convention](https://developer.hashicorp.com/terraform/registry/providers/publishing). [TFLint](https://github.com/terraform-linters/tflint/blob/master/docs/user-guide/plugins.md) also uses a short plugin name, but its installation source directly identifies a repository and its name determines the executable. Atmos uses OCI package releases, so its identity does not require one Git repository per plugin. + +Toolchain's `owner/repo` identifies the executable distribution; a plugin's `publisher/package` identifies the Atmos integration. For example, `gruntwork-io/terragrunt` is the toolchain product, while `cloudposse/terragrunt` is Cloud Posse's integration. Both can use `terragrunt` as a short name within their respective configuration sections. Consumers can override the plugin alias with `plugin install --as`; the alias does not rename the package or its exported capabilities. + +The installer must use the product name as the default alias for a new declaration and require an explicit alias on a collision. This requires a follow-up implementation change: the current installer replaces `/` in the full package ID with `-`, producing `cloudposse-terragrunt`. Until then, use `--as terragrunt` to follow the convention. Preserve existing declarations when changing the default. + +A registry alias selects a discovery source and is separate from the publisher namespace. The installation selector `/` can therefore contain three segments, such as `team/acme/oidc`. Do not interpret the package ID as a GitHub URL or infer an OCI location from it. The configured source and lockfile identify the release; signature and publisher policy establish trust. Identical package names from different sources must not cause silent source substitution. + ## Scaffold a separate plugin repository (broader contract example) ```shell diff --git a/plugins/terragrunt/schemas/config.json b/plugins/terragrunt/schemas/config.json index c8ba9b95ae0..b86b276a57c 100644 --- a/plugins/terragrunt/schemas/config.json +++ b/plugins/terragrunt/schemas/config.json @@ -1 +1,46 @@ -{"$schema":"https://json-schema.org/draft/2020-12/schema","type":"object","properties":{"enabled":{"type":"boolean"},"provider":{"const":"terragrunt"},"mode":{"const":"native"},"discovery":{"type":"object","properties":{"enabled":{"type":"boolean"},"included_paths":{"type":"array","items":{"type":"string"},"minItems":1},"excluded_paths":{"type":"array","items":{"type":"string"}}},"required":["enabled","included_paths"],"additionalProperties":false}},"required":["enabled","discovery"],"additionalProperties":false} +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "provider": { + "const": "terragrunt" + }, + "mode": { + "const": "native" + }, + "discovery": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean" + }, + "included_paths": { + "type": "array", + "items": { + "type": "string" + }, + "minItems": 1 + }, + "excluded_paths": { + "type": "array", + "items": { + "type": "string" + } + } + }, + "required": [ + "enabled", + "included_paths" + ], + "additionalProperties": false + } + }, + "required": [ + "enabled", + "discovery" + ], + "additionalProperties": false +} diff --git a/website/docs/cli/commands/plugin/usage.mdx b/website/docs/cli/commands/plugin/usage.mdx index 6f5324d1cee..131d72097cc 100644 --- a/website/docs/cli/commands/plugin/usage.mdx +++ b/website/docs/cli/commands/plugin/usage.mdx @@ -102,10 +102,11 @@ separate identities. The generated README includes the configuration. ## Package identity and source metadata -The plugin manifest uses the shared Atmos envelope. `metadata.name` is the stable, -organization-qualified package ID; `display_name` is an optional human-readable -name. Authors can configure the description, version, maintainer, and source -repository: +The plugin manifest uses the shared Atmos envelope. `metadata.name` is the stable +package ID in the form `/`. The publisher maintains the Atmos +integration. Use the product name for the package and its consumer alias. +`display_name` is an optional human-readable name. Authors can configure the +description, version, maintainer, and source repository: ```yaml apiVersion: atmos/v1 @@ -123,6 +124,45 @@ Packing preserves these fields in every platform payload. `plugin info` exposes the installed manifest. The repository URL is supplied by the author; the consumer lockfile separately records the OCI source and verified release digests. +### Naming convention + +| Purpose | Terragrunt example | +|---|---| +| Package ID | `cloudposse/terragrunt` | +| Product alias (convention) | `terragrunt` | +| Source repository today | `https://github.com/cloudposse/atmos` | +| Source directory | `plugins/terragrunt` | +| Release location | Explicit OCI reference, recorded with its digest in the lockfile | +| Standalone repository, if created | `cloudposse/atmos-plugin-terragrunt` | + +For standalone plugin repositories, use `atmos-plugin-`. The prefix makes +the repository's purpose clear outside Atmos; omit it from the package ID. Several +plugins can share a repository. Moving a plugin into its own repository does not +change its package ID. Existing repository locations, such as the OIDC example +above, remain valid. + +This convention identifies an independently released package. It follows the +distinction between package addresses and local names used by +[Terraform providers](https://developer.hashicorp.com/terraform/language/providers/requirements). +Atmos toolchain uses `owner/repo` to identify an executable distribution; plugins +use `publisher/package` to identify an integration. For example, +`gruntwork-io/terragrunt` identifies the toolchain product and +`cloudposse/terragrunt` identifies Cloud Posse's Atmos integration. + +The `plugins.packages.` key is a project-local name. Use +[`plugin install --as terragrunt`](/cli/commands/plugin/install) to select the +product alias. The current installer derives `cloudposse-terragrunt` from the full +package ID when no alias is supplied for a new declaration. Short product aliases +are the naming convention; automatic selection of that default is planned. +Changing it does not change the package ID or the plugin's exported capability +names. Plugin aliases and toolchain aliases belong to separate configuration +sections. + +A registry alias selects where to search. It is separate from the publisher in a +package ID: `team/acme/oidc` selects package `acme/oidc` from registry alias `team`. +The package ID does not imply a GitHub URL or OCI download location. Installation +uses the selected OCI source, and the lockfile records the release digests. + ## Configure a consumer Add provider and identity settings to the consumer project's `atmos.yaml`: From f4b527ae17fe76c86dce63b137536ec9ec00b64b Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 30 Oct 2026 18:02:26 -0500 Subject: [PATCH 02/52] feat(terragrunt): run hooks, saved plans, and state under native adaptation Evaluate extra_arguments and before/after/error hooks in the plugin and run them in the host with the invocation's identity, environment, toolchain, and cancellation; an implicit init matches hooks for init. Bind saved plans to plan bytes, provider identity, engine, options, sources, preparation, and working directory. Dispatch !terraform.state and !terraform.output through configuration providers. Add terragrunt:// imports, revision-aware source watches, backend and generated-file ownership, and Type/Engine instance columns. Fix toolchain PATH handling, stale retained workdirs for local module edits, and use product names as default plugin aliases. Co-Authored-By: Claude Sonnet 5.5 --- .../demo/fixtures/terragrunt-native/plan.yaml | 89 +++ .../fixtures/terragrunt-native/atmos.yaml | 26 + .../live/demo/terragrunt.hcl | 19 + .../terragrunt-native/modules/demo/main.tf | 11 + .../scripts/validate-terragrunt-native.star | 31 + docs/prd/plugins/authoring.md | 2 +- .../plugins/extensions/component-engines.md | 8 +- errors/plugin.go | 4 + examples/terragrunt-native/.gitignore | 7 + examples/terragrunt-native/README.md | 84 +++ examples/terragrunt-native/atmos.yaml | 15 + .../live/demo/terragrunt.hcl | 18 + .../terragrunt-native/modules/demo/main.tf | 17 + internal/exec/configuration_imports.go | 35 + internal/exec/configuration_imports_test.go | 129 ++++ internal/exec/describe_affected_components.go | 6 +- .../exec/describe_affected_configuration.go | 36 ++ internal/exec/stack_processor_utils.go | 6 + pkg/component/configuration/files.go | 15 +- pkg/component/configuration/ownership.go | 68 ++ pkg/component/configuration/ownership_test.go | 88 +++ pkg/component/configuration/registry.go | 4 +- pkg/component/execution/arguments.go | 143 +++++ pkg/component/execution/arguments_test.go | 184 ++++++ pkg/component/execution/execution.go | 114 ++++ pkg/component/execution/lifecycle.go | 149 +++++ .../execution/lifecycle_cancellation_test.go | 89 +++ pkg/component/execution/lifecycle_test.go | 262 ++++++++ pkg/component/terraform/configuration.go | 38 +- pkg/component/terraform/configuration_plan.go | 317 +++++++++ .../configuration_plan_failures_test.go | 183 ++++++ .../configuration_plan_pipeline_test.go | 174 +++++ .../terraform/configuration_plan_test.go | 170 +++++ pkg/component/terraform/configuration_test.go | 35 +- .../configuration_toolchain_path_test.go | 32 + .../terraform/configuration_toolchain_test.go | 520 +++++++++++++++ pkg/component/terraform/execute_command.go | 17 +- .../terraform/execute_environment.go | 52 +- pkg/component/terraform/execute_hooks.go | 211 ++++++ .../terraform/execute_hooks_process_test.go | 325 ++++++++++ pkg/component/terraform/execute_hooks_test.go | 461 ++++++++++++++ pkg/component/terraform/execute_init.go | 47 +- pkg/component/terraform/execute_pipeline.go | 42 +- .../terraform/state_configuration_test.go | 41 ++ pkg/component/terraform/state_utils.go | 7 + pkg/component/terraform/testmain_test.go | 11 + pkg/component/workdir_path.go | 10 + pkg/component/workdir_path_test.go | 28 + pkg/dependencies/environment.go | 5 +- pkg/dependencies/environment_order_test.go | 33 + pkg/dependencies/installer.go | 6 +- pkg/edition/journal.yaml | 7 + pkg/list/column/required_sections.go | 2 +- pkg/list/extract/instances.go | 1 + pkg/list/extract/instances_test.go | 3 + pkg/list/extract/metadata.go | 1 + pkg/list/instance_engine.go | 42 ++ pkg/list/instance_engine_test.go | 104 +++ pkg/list/list_instances.go | 15 +- pkg/plugin/adapter/component/identity.go | 47 ++ pkg/plugin/adapter/component/identity_test.go | 108 ++++ pkg/plugin/adapter/component/responses.go | 2 +- pkg/plugin/adapter/configuration/discovery.go | 7 +- pkg/plugin/adapter/configuration/execution.go | 68 ++ pkg/plugin/adapter/configuration/imports.go | 68 ++ .../adapter/configuration/imports_test.go | 105 +++ pkg/plugin/adapter/configuration/plan.go | 151 +++++ pkg/plugin/adapter/configuration/plan_test.go | 278 ++++++++ .../adapter/configuration/preparation.go | 55 +- .../preparation_execution_test.go | 101 +++ .../adapter/configuration/preparation_test.go | 109 ++++ pkg/plugin/adapter/configuration/revision.go | 14 +- .../adapter/configuration/revision_test.go | 28 + pkg/plugin/adapter/configuration/session.go | 47 +- .../adapter/configuration/session_test.go | 5 +- pkg/plugin/adapter/configuration/state.go | 49 ++ .../adapter/configuration/state_test.go | 73 +++ .../configuration/toolchain_boundary_test.go | 108 ++++ pkg/plugin/manage/lifecycle_test.go | 12 +- pkg/plugin/manage/manage.go | 2 +- pkg/plugin/manage/pin.go | 7 + pkg/plugin/manage/pin_test.go | 11 +- pkg/plugin/proto/atmos/plugin/v1/plugin.pb.go | 591 ++++++++++++----- pkg/schema/component_configuration.go | 18 +- pkg/schema/component_configuration_import.go | 20 + pkg/schema/component_configuration_plan.go | 15 + pkg/schema/component_configuration_state.go | 17 + pkg/schema/component_execution.go | 39 ++ pkg/schema/instance.go | 1 + pkg/schema/schema.go | 6 + pkg/terraform/output/config.go | 4 + pkg/terraform/output/configuration_state.go | 121 ++++ .../configuration_state_protocol_test.go | 71 +++ .../output/configuration_state_test.go | 156 +++++ pkg/terraform/output/environment.go | 18 + pkg/terraform/output/executor.go | 28 +- pkg/terraform/output/executor_init.go | 30 +- .../output/executor_toolchain_test.go | 221 +++++++ plugins/releases.yaml | 9 + plugins/terragrunt/README.md | 18 +- plugins/terragrunt/bridge/backend.go | 34 + plugins/terragrunt/bridge/bridge.go | 6 + plugins/terragrunt/bridge/generation.go | 45 ++ plugins/terragrunt/go.mod | 2 +- plugins/terragrunt/internal/engine/backend.go | 34 + .../internal/engine/backend_test.go | 108 ++++ .../terragrunt/internal/engine/discovery.go | 8 +- .../internal/engine/environment_test.go | 3 + .../terragrunt/internal/engine/execution.go | 138 ++++ .../internal/engine/execution_test.go | 200 ++++++ .../terragrunt/internal/engine/generation.go | 64 ++ .../internal/engine/generation_test.go | 123 ++++ .../terragrunt/internal/engine/policies.go | 12 + plugins/terragrunt/internal/engine/resolve.go | 79 ++- .../internal/engine/resolve_test.go | 12 +- .../internal/engine/source_watches.go | 77 +++ .../internal/engine/source_watches_test.go | 71 +++ .../engine/toolchain_dependency_test.go | 119 ++++ .../internal/engine/toolchain_helper_test.go | 106 +++ .../internal/engine/toolchain_test.go | 219 +++++++ plugins/terragrunt/internal/model/model.go | 35 + .../terragrunt/internal/provider/discovery.go | 61 +- .../internal/provider/fixtures_test.go | 5 +- .../internal/provider/imports_test.go | 62 ++ .../internal/provider/preparation.go | 49 +- .../terragrunt/internal/provider/provider.go | 7 +- .../internal/provider/provider_test.go | 64 ++ .../internal/provider/toolchain_test.go | 119 ++++ plugins/terragrunt/plugin.tests.yaml | 36 ++ plugins/terragrunt/scripts/affected.star | 58 ++ plugins/terragrunt/scripts/fixtures.star | 15 +- .../scripts/lifecycle_extended.star | 495 +++++++++++++++ plugins/terragrunt/scripts/release.star | 69 ++ proto/atmos/plugin/v1/plugin.proto | 33 + sdk/proto/atmos/plugin/v1/plugin.pb.go | 591 ++++++++++++----- ...-10-29-native-terragrunt-compatibility.mdx | 98 +++ .../docs/cli/commands/list/list-instances.mdx | 16 + website/docs/cli/commands/plugin/install.mdx | 5 +- website/docs/cli/commands/plugin/usage.mdx | 7 +- website/docs/migration/terragrunt.mdx | 64 ++ website/src/data/roadmap.js | 3 +- .../demo/fixtures/terragrunt-native/plan.cast | 601 ++++++++++++++++++ 142 files changed, 10897 insertions(+), 493 deletions(-) create mode 100644 demo/casts/atmos.d/demo/fixtures/terragrunt-native/plan.yaml create mode 100644 demo/casts/fixtures/terragrunt-native/atmos.yaml create mode 100644 demo/casts/fixtures/terragrunt-native/live/demo/terragrunt.hcl create mode 100644 demo/casts/fixtures/terragrunt-native/modules/demo/main.tf create mode 100644 demo/casts/scripts/validate-terragrunt-native.star create mode 100644 examples/terragrunt-native/.gitignore create mode 100644 examples/terragrunt-native/README.md create mode 100644 examples/terragrunt-native/atmos.yaml create mode 100644 examples/terragrunt-native/live/demo/terragrunt.hcl create mode 100644 examples/terragrunt-native/modules/demo/main.tf create mode 100644 internal/exec/configuration_imports.go create mode 100644 internal/exec/configuration_imports_test.go create mode 100644 pkg/component/configuration/ownership.go create mode 100644 pkg/component/configuration/ownership_test.go create mode 100644 pkg/component/execution/arguments.go create mode 100644 pkg/component/execution/arguments_test.go create mode 100644 pkg/component/execution/execution.go create mode 100644 pkg/component/execution/lifecycle.go create mode 100644 pkg/component/execution/lifecycle_cancellation_test.go create mode 100644 pkg/component/execution/lifecycle_test.go create mode 100644 pkg/component/terraform/configuration_plan.go create mode 100644 pkg/component/terraform/configuration_plan_failures_test.go create mode 100644 pkg/component/terraform/configuration_plan_pipeline_test.go create mode 100644 pkg/component/terraform/configuration_plan_test.go create mode 100644 pkg/component/terraform/configuration_toolchain_path_test.go create mode 100644 pkg/component/terraform/configuration_toolchain_test.go create mode 100644 pkg/component/terraform/execute_hooks.go create mode 100644 pkg/component/terraform/execute_hooks_process_test.go create mode 100644 pkg/component/terraform/execute_hooks_test.go create mode 100644 pkg/component/terraform/state_configuration_test.go create mode 100644 pkg/dependencies/environment_order_test.go create mode 100644 pkg/list/instance_engine.go create mode 100644 pkg/list/instance_engine_test.go create mode 100644 pkg/plugin/adapter/component/identity.go create mode 100644 pkg/plugin/adapter/component/identity_test.go create mode 100644 pkg/plugin/adapter/configuration/execution.go create mode 100644 pkg/plugin/adapter/configuration/imports.go create mode 100644 pkg/plugin/adapter/configuration/imports_test.go create mode 100644 pkg/plugin/adapter/configuration/plan.go create mode 100644 pkg/plugin/adapter/configuration/plan_test.go create mode 100644 pkg/plugin/adapter/configuration/preparation_execution_test.go create mode 100644 pkg/plugin/adapter/configuration/preparation_test.go create mode 100644 pkg/plugin/adapter/configuration/state.go create mode 100644 pkg/plugin/adapter/configuration/state_test.go create mode 100644 pkg/plugin/adapter/configuration/toolchain_boundary_test.go create mode 100644 pkg/schema/component_configuration_import.go create mode 100644 pkg/schema/component_configuration_plan.go create mode 100644 pkg/schema/component_configuration_state.go create mode 100644 pkg/schema/component_execution.go create mode 100644 pkg/terraform/output/configuration_state.go create mode 100644 pkg/terraform/output/configuration_state_protocol_test.go create mode 100644 pkg/terraform/output/configuration_state_test.go create mode 100644 pkg/terraform/output/executor_toolchain_test.go create mode 100644 plugins/terragrunt/bridge/backend.go create mode 100644 plugins/terragrunt/bridge/generation.go create mode 100644 plugins/terragrunt/internal/engine/backend.go create mode 100644 plugins/terragrunt/internal/engine/backend_test.go create mode 100644 plugins/terragrunt/internal/engine/execution.go create mode 100644 plugins/terragrunt/internal/engine/execution_test.go create mode 100644 plugins/terragrunt/internal/engine/generation.go create mode 100644 plugins/terragrunt/internal/engine/generation_test.go create mode 100644 plugins/terragrunt/internal/engine/source_watches.go create mode 100644 plugins/terragrunt/internal/engine/source_watches_test.go create mode 100644 plugins/terragrunt/internal/engine/toolchain_dependency_test.go create mode 100644 plugins/terragrunt/internal/engine/toolchain_helper_test.go create mode 100644 plugins/terragrunt/internal/engine/toolchain_test.go create mode 100644 plugins/terragrunt/internal/provider/imports_test.go create mode 100644 plugins/terragrunt/internal/provider/toolchain_test.go create mode 100644 plugins/terragrunt/scripts/lifecycle_extended.star create mode 100644 plugins/terragrunt/scripts/release.star create mode 100644 website/blog/2026-10-29-native-terragrunt-compatibility.mdx create mode 100644 website/static/casts/demo/fixtures/terragrunt-native/plan.cast diff --git a/demo/casts/atmos.d/demo/fixtures/terragrunt-native/plan.yaml b/demo/casts/atmos.d/demo/fixtures/terragrunt-native/plan.yaml new file mode 100644 index 00000000000..6b7239f7728 --- /dev/null +++ b/demo/casts/atmos.d/demo/fixtures/terragrunt-native/plan.yaml @@ -0,0 +1,89 @@ +commands: + - name: casts generate demo fixtures terragrunt-native plan + description: Record native Terragrunt discovery and an OpenTofu plan + env: + PATH: '{{ env "PWD" }}/../../build:{{ env "PATH" }}' + ATMOS_TELEMETRY_ENABLED: 'false' + steps: + # Build the host and package together so their protocol stays in sync. + - type: shell + name: build-host + working_directory: ../.. + command: go build -o build/atmos . + output: none + - type: shell + name: build-plugin + working_directory: ../.. + command: build/atmos plugin build plugins/terragrunt + output: none + - type: workdir + name: fixture + source: fixtures/terragrunt-native + path: ../../.context/cast-fixtures/terragrunt-native + reset: true + # Resolve the development package outside the recording; no machine paths + # belong in the published demo. The blog explains this prerequisite. + - type: shell + name: package + working_directory: ../../.context/cast-fixtures/terragrunt-native + command: atmos config set plugins.packages.terragrunt.path "../../../plugins/terragrunt" + output: none + - type: cast + name: plan + working_directory: ../../.context/cast-fixtures/terragrunt-native + title: Native Terragrunt discovery and planning + mode: steps + defaults: + cast: !include cast-defaults.yaml .cast + simulate: !include cast-defaults.yaml .simulate + show: + labels: false + output: + mode: raw + cast: ../../website/static/casts/demo/fixtures/terragrunt-native/plan.cast + env: !include cast-defaults.yaml .env.recording + steps: + - type: simulate + text: '# The native plugin is built; enable it for the existing live/ tree.' + - type: simulate + text: &enable atmos config set components.terraform.terragrunt.enabled true + - type: shell + command: *enable + - type: simulate + text: &mode atmos config set components.terraform.terragrunt.mode native + - type: shell + command: *mode + - type: simulate + text: &scope atmos config set --type=yaml components.terraform.terragrunt.discovery.included_paths '["live/**"]' + - type: shell + command: *scope + - type: simulate + text: &discover atmos config set components.terraform.terragrunt.discovery.enabled true + - type: shell + command: *discover + - type: simulate + text: &stacks atmos list stacks + - type: shell + command: *stacks + - type: simulate + text: &instances atmos list instances + - type: shell + command: *instances + - type: simulate + text: '# HCL evaluates the greeting; Atmos runs OpenTofu. No cloud credentials needed.' + - type: simulate + text: &plan atmos terraform plan demo -s live + - type: shell + command: *plan + interval: 2s + - type: script + name: validate + interpreter: starlark + script: !include scripts/validate-terragrunt-native.star + - name: casts validate demo fixtures terragrunt-native plan + description: Validate native Terragrunt discovery and plan evidence + steps: + - type: script + name: validate + interpreter: starlark + script: !include scripts/validate-terragrunt-native.star diff --git a/demo/casts/fixtures/terragrunt-native/atmos.yaml b/demo/casts/fixtures/terragrunt-native/atmos.yaml new file mode 100644 index 00000000000..b27d7c5861e --- /dev/null +++ b/demo/casts/fixtures/terragrunt-native/atmos.yaml @@ -0,0 +1,26 @@ +base_path: . + +components: + terraform: + base_path: components/terraform + command: tofu + terragrunt: + enabled: false + mode: native + discovery: + enabled: false + included_paths: [live/**] + +logs: + level: Warning + +# Keep the local-development warning, but redact the recorder's checkout path. +settings: + metrics: + enabled: false + terminal: + mask: + enabled: true + replacement: '[LOCAL_PLUGIN]' + patterns: + - '/(Users|home)/[^[:space:]]+/plugins/terragrunt' diff --git a/demo/casts/fixtures/terragrunt-native/live/demo/terragrunt.hcl b/demo/casts/fixtures/terragrunt-native/live/demo/terragrunt.hcl new file mode 100644 index 00000000000..fe165aaea2f --- /dev/null +++ b/demo/casts/fixtures/terragrunt-native/live/demo/terragrunt.hcl @@ -0,0 +1,19 @@ +# Keep the existing Terragrunt layout and evaluate its inputs natively. +terraform { + source = "../../modules/demo" +} + +remote_state { + backend = "local" + config = { + path = "${get_terragrunt_dir()}/terraform.tfstate" + } + generate = { + path = "backend.tf" + if_exists = "overwrite_terragrunt" + } +} + +inputs = { + greeting = upper("hello from terragrunt") +} diff --git a/demo/casts/fixtures/terragrunt-native/modules/demo/main.tf b/demo/casts/fixtures/terragrunt-native/modules/demo/main.tf new file mode 100644 index 00000000000..58f01ff0c2e --- /dev/null +++ b/demo/casts/fixtures/terragrunt-native/modules/demo/main.tf @@ -0,0 +1,11 @@ +variable "greeting" { + type = string +} + +resource "terraform_data" "demo" { + input = var.greeting +} + +output "greeting" { + value = terraform_data.demo.output +} diff --git a/demo/casts/scripts/validate-terragrunt-native.star b/demo/casts/scripts/validate-terragrunt-native.star new file mode 100644 index 00000000000..5c4c5bbb2f9 --- /dev/null +++ b/demo/casts/scripts/validate-terragrunt-native.star @@ -0,0 +1,31 @@ +"""Check actual CLI output in the native Terragrunt recording.""" + +load("../cast_checks.star", "load_text", "strip_ansi", "assert_no_error_output", "assert_no_local_paths", "assert_colored") + + +def validate(): + text = load_text("../../website/static/casts/demo/fixtures/terragrunt-native/plan.cast") + plain = strip_ansi(text) + assert_no_error_output(text) + assert_no_local_paths(text) + assert_colored(text, "Plan:") + # Commands alone are not proof: require the inventory and real engine plan. + for needle in [ + "components.terraform.terragrunt.discovery.enabled true", + "atmos list stacks", + "atmos list instances", + "Component", "Stack", "Type", "Engine", "opentofu", + "atmos terraform plan demo -s live", + "terraform_data.demo", + "HELLO FROM TERRAGRUNT", + "Plan: 1 to add, 0 to change, 0 to destroy.", + ]: + if needle not in plain: + fail("native Terragrunt cast missing {!r}".format(needle)) + for marker in ["Error:", "unknown flag", "rpc error", "exit status 1"]: + if marker in plain: + fail("native Terragrunt cast contains {!r}".format(marker)) + ui.success("Native Terragrunt discovery and plan cast validated") + + +validate() diff --git a/docs/prd/plugins/authoring.md b/docs/prd/plugins/authoring.md index 28f272c3529..46483689de7 100644 --- a/docs/prd/plugins/authoring.md +++ b/docs/prd/plugins/authoring.md @@ -41,7 +41,7 @@ This follows [Terraform's distinction between provider addresses and local names Toolchain's `owner/repo` identifies the executable distribution; a plugin's `publisher/package` identifies the Atmos integration. For example, `gruntwork-io/terragrunt` is the toolchain product, while `cloudposse/terragrunt` is Cloud Posse's integration. Both can use `terragrunt` as a short name within their respective configuration sections. Consumers can override the plugin alias with `plugin install --as`; the alias does not rename the package or its exported capabilities. -The installer must use the product name as the default alias for a new declaration and require an explicit alias on a collision. This requires a follow-up implementation change: the current installer replaces `/` in the full package ID with `-`, producing `cloudposse-terragrunt`. Until then, use `--as terragrunt` to follow the convention. Preserve existing declarations when changing the default. +The installer uses the product name as the default alias for a new declaration and requires an explicit alias on a collision. Plugin management has not appeared in a stable release, so the product alias is the only default and needs no edition journal entry; existing declarations always retain their names. A registry alias selects a discovery source and is separate from the publisher namespace. The installation selector `/` can therefore contain three segments, such as `team/acme/oidc`. Do not interpret the package ID as a GitHub URL or infer an OCI location from it. The configured source and lockfile identify the release; signature and publisher policy establish trust. Identical package names from different sources must not cause silent source substitution. diff --git a/docs/prd/plugins/extensions/component-engines.md b/docs/prd/plugins/extensions/component-engines.md index bbcf48d68b9..e664a53f588 100644 --- a/docs/prd/plugins/extensions/component-engines.md +++ b/docs/prd/plugins/extensions/component-engines.md @@ -28,7 +28,7 @@ The checked-in [protocol](../../../../proto/atmos/plugin/v1/plugin.proto) is aut |---|---|---| | `component.discovery` | `ComponentDiscovery.Discover` | Resolve imports or discover logical instances, source watches, and dependency edges in the requested root/revision. | | `component.validation` | `ComponentValidation.Validate` | Reject unsupported operations, modes, and provider options before effects. | -| `component.preparation` | `ComponentPreparation.Prepare` | Reevaluate one selected unit before its operation; return inputs, module, state identity, and literal generated files. | +| `component.preparation` | `ComponentPreparation.Prepare` | Reevaluate one selected unit before its operation; return inputs, module, state identity, literal generated files, and optional execution metadata. | | `component.state` | `ComponentState.Resolve` | Resolve backend/workspace identity without preparation or execution. | The following contracts still require implementation: @@ -71,7 +71,11 @@ Use static metadata for help without starting plugins. Use `pkg/flags.NewStandar - Include effective options in cache and saved-plan identity. Never log sensitive values. - Reject unsupported native options. Executable passthrough must be explicit and must not create a second scheduler. -`ComponentOperation` currently transmits resolved provider options. It does not implement parsing or authorize arbitrary passthrough. +`ComponentOperation` transmits resolved provider options and, separately, the resolved engine arguments (`terraform_arguments`) so configuration semantics can inspect flags such as `-destroy`. It does not implement parsing or authorize arbitrary passthrough. + +### Execution metadata + +`PrepareComponentResponse.execution` returns evaluated, ordered command metadata as data: extra arguments (commands, arguments, required and optional var files, environment) and before, after, and error hooks (commands, argv, working directory, `run_on_error`, `suppress_stdout`, `on_errors`). The plugin never runs hooks. The host runs them with the invocation's environment, identity, toolchain, cancellation, and I/O, matches them against the command being run (an implicit `init` is its own command), and records the metadata with a saved plan so applying that plan reuses it. A plugin that cannot honor a lifecycle must reject the unit instead of omitting it. `PrepareComponentResponse.backend_generated` tells the host that a returned generated file owns the backend block, so the host must not emit its default backend file. ## Host integration diff --git a/errors/plugin.go b/errors/plugin.go index 43178f5a043..5d0ed377307 100644 --- a/errors/plugin.go +++ b/errors/plugin.go @@ -72,6 +72,10 @@ var ( ErrPluginUnlockedInCI = errors.New("plugin is declared but not locked; automatic installation is disabled in CI") // ErrPluginFlagConflict identifies flags that cannot be combined. ErrPluginFlagConflict = errors.New("plugin flags cannot be combined") + // ErrComponentHookFailed identifies a provider-supplied hook that exited unsuccessfully or could not run. + ErrComponentHookFailed = errors.New("component hook failed") + // ErrComponentVarFileMissing identifies a required variable file that does not exist. + ErrComponentVarFileMissing = errors.New("required variable file does not exist") ErrInvalidCredentialCodec = errors.New("invalid credential codec") ErrDuplicateCredentialCodec = errors.New("duplicate credential codec") diff --git a/examples/terragrunt-native/.gitignore b/examples/terragrunt-native/.gitignore new file mode 100644 index 00000000000..6677c487514 --- /dev/null +++ b/examples/terragrunt-native/.gitignore @@ -0,0 +1,7 @@ +.atmos/ +.workdir/ +**/.terraform/ +**/.terraform.lock.hcl +**/.terragrunt-cache/ +**/state/ +**/*.planfile diff --git a/examples/terragrunt-native/README.md b/examples/terragrunt-native/README.md new file mode 100644 index 00000000000..9c91f90a3c3 --- /dev/null +++ b/examples/terragrunt-native/README.md @@ -0,0 +1,84 @@ +--- +title: Native Terragrunt +tags: [Components, Stacks] +related_docs: + - label: Terragrunt migration + url: /migration/terragrunt + - label: Plugin usage + url: /cli/commands/plugin/usage +--- + +# Native Terragrunt + +Use an existing Terragrunt HCL unit as an Atmos [Terraform component](https://atmos.tools/components/terraform) without adding +Atmos stack YAML. The example discovers component `demo` in stack `live`, evaluates +its HCL with Terragrunt Go modules, and executes OpenTofu through Atmos. It does not +run the Terragrunt executable. + +The module uses the built-in `terraform_data` resource and local state. No cloud +credentials or provider downloads are needed. + +## Build the development package + +This example requires the experimental Atmos branch and its local Terragrunt +plugin. See [plugin usage](https://atmos.tools/cli/commands/plugin/usage) for package configuration. The plugin is not yet published. Install Go and OpenTofu, then run these +commands from the root of the Atmos checkout containing this example: + +```shell +go build -o build/atmos . +export PATH="$PWD/build:$PATH" +atmos plugin build plugins/terragrunt +export ATMOS_TERRAGRUNT_PLUGIN_PATH="$PWD/plugins/terragrunt" +``` + +Copy the example into a new directory, then configure the package path: + +```shell +atmos init ./examples/terragrunt-native ./terragrunt-native --copy --no-git +cd terragrunt-native +atmos config set plugins.packages.terragrunt.path "$ATMOS_TERRAGRUNT_PLUGIN_PATH" +``` + +The example already enables native discovery. To enable the same behavior in an +existing project, run these commands from that project's root: + +```shell +atmos config set plugins.packages.terragrunt.path "$ATMOS_TERRAGRUNT_PLUGIN_PATH" +atmos config set components.terraform.command tofu +atmos config set components.terraform.terragrunt.enabled true +atmos config set components.terraform.terragrunt.mode native +atmos config set --type=yaml components.terraform.terragrunt.discovery.included_paths '["live/**"]' +atmos config set components.terraform.terragrunt.discovery.enabled true +``` + +The environment variable above is only a shell convenience for these commands. +The plugin path is stored in `atmos.yaml`; Atmos does not require that variable on +subsequent runs. Adjust `included_paths` to match your Terragrunt directory layout. +If you use Terraform instead, set `components.terraform.command` to `terraform` +and install Terraform 1.4 or newer. + +## Discover and plan + +```shell +atmos list stacks +atmos list instances +atmos terraform plan demo -s live +``` + +The plan contains one `terraform_data.demo` resource with the input +`HELLO FROM TERRAGRUNT`, evaluated by HCL's `upper` function. Atmos prepares an isolated workdir; the source HCL and +module stay in place. The local backend retains the unit's state path at +`live/demo/state/terraform.tfstate`. + +## Apply and clean up + +```shell +atmos terraform apply demo -s live -- -auto-approve +atmos terraform output demo -s live -- -raw greeting +atmos terraform plan demo -s live -- -detailed-exitcode +atmos terraform destroy demo -s live -- -auto-approve +``` + +After apply, output prints the greeting and the unchanged plan exits with code +zero. Destroy removes the local test resource. This example demonstrates one local +unit; it does not establish compatibility with all Terragrunt constructs. diff --git a/examples/terragrunt-native/atmos.yaml b/examples/terragrunt-native/atmos.yaml new file mode 100644 index 00000000000..796c7971848 --- /dev/null +++ b/examples/terragrunt-native/atmos.yaml @@ -0,0 +1,15 @@ +# Keep existing Terragrunt units in HCL; Atmos discovers them without stack YAML. +base_path: . + +components: + terraform: + base_path: components/terraform + command: tofu + terragrunt: + enabled: true + mode: native + discovery: + enabled: true + included_paths: [live/**] + +# Set plugins.packages.terragrunt.path to your built local package before running. diff --git a/examples/terragrunt-native/live/demo/terragrunt.hcl b/examples/terragrunt-native/live/demo/terragrunt.hcl new file mode 100644 index 00000000000..49d4b658c76 --- /dev/null +++ b/examples/terragrunt-native/live/demo/terragrunt.hcl @@ -0,0 +1,18 @@ +terraform { + source = "${get_terragrunt_dir()}/../../modules/demo" +} + +remote_state { + backend = "local" + config = { + path = "${get_terragrunt_dir()}/state/terraform.tfstate" + } + generate = { + path = "backend.tf" + if_exists = "overwrite_terragrunt" + } +} + +inputs = { + greeting = upper("hello from terragrunt") +} diff --git a/examples/terragrunt-native/modules/demo/main.tf b/examples/terragrunt-native/modules/demo/main.tf new file mode 100644 index 00000000000..9dd3de0152e --- /dev/null +++ b/examples/terragrunt-native/modules/demo/main.tf @@ -0,0 +1,17 @@ +terraform { + required_version = ">= 1.4.0" +} + +variable "greeting" { + description = "Message supplied by the Terragrunt unit." + type = string +} + +# The built-in resource needs no provider download or cloud credentials. +resource "terraform_data" "demo" { + input = var.greeting +} + +output "greeting" { + value = terraform_data.demo.output +} diff --git a/internal/exec/configuration_imports.go b/internal/exec/configuration_imports.go new file mode 100644 index 00000000000..158183d7f6b --- /dev/null +++ b/internal/exec/configuration_imports.go @@ -0,0 +1,35 @@ +package exec + +import ( + "context" + "fmt" + "strings" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/schema" +) + +// registerConfigurationImport routes configured provider schemes before YAML or +// remote-source resolution. Providers add instances to the shared inventory; +// importing a root does not rename its stacks or merge HCL into YAML variables. +func registerConfigurationImport(config *schema.AtmosConfiguration, uri string, declaration *schema.StackImport) (bool, error) { + scheme, _, found := strings.Cut(uri, "://") + if !found || config == nil { + return false, nil + } + settings, configured := config.Components.Terraform.ConfigurationProviders[scheme] + if !configured { + return false, nil + } + if !settings.Enabled { + return true, fmt.Errorf("%w: configuration provider %s is disabled", errUtils.ErrPluginInvalidConfig, scheme) + } + if len(declaration.Context) != 0 || declaration.SkipIfMissing || declaration.TTL != "" || declaration.NestedImports != "" { + return true, fmt.Errorf("%w: provider imports do not support context, skip_if_missing, TTL, or nested_imports", errUtils.ErrPluginUnsupportedExtension) + } + imports, ok := config.ComponentConfigurations.(schema.ComponentConfigurationImportRuntime) + if !ok { + return true, fmt.Errorf("%w: provider %s has no import runtime", errUtils.ErrPluginUnsupportedExtension, scheme) + } + return imports.RegisterImport(context.Background(), uri) +} diff --git a/internal/exec/configuration_imports_test.go b/internal/exec/configuration_imports_test.go new file mode 100644 index 00000000000..bf34bb1849e --- /dev/null +++ b/internal/exec/configuration_imports_test.go @@ -0,0 +1,129 @@ +package exec + +import ( + "context" + "errors" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/schema" +) + +type importRuntime struct { + *MockComponentConfigurationRuntime + imports []string + err error +} + +func (r *importRuntime) RegisterImport(_ context.Context, uri string) (bool, error) { + r.imports = append(r.imports, uri) + return true, r.err +} + +func TestConfigurationImportRouting(t *testing.T) { + config := &schema.AtmosConfiguration{} + for _, uri := range []string{"ordinary", "https://example.com/config.yaml"} { + handled, err := registerConfigurationImport(config, uri, &schema.StackImport{}) + require.NoError(t, err) + require.False(t, handled) + } + handled, err := registerConfigurationImport(nil, "terragrunt://live", &schema.StackImport{}) + require.NoError(t, err) + require.False(t, handled) + config.Components.Terraform.ConfigurationProviders = map[string]schema.ComponentConfiguration{"terragrunt": {}} + _, err = registerConfigurationImport(config, "terragrunt://live", &schema.StackImport{}) + require.ErrorContains(t, err, "disabled") + config.Components.Terraform.ConfigurationProviders["terragrunt"] = schema.ComponentConfiguration{Enabled: true} + _, err = registerConfigurationImport(config, "terragrunt://live", &schema.StackImport{}) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + runtime := &importRuntime{MockComponentConfigurationRuntime: NewMockComponentConfigurationRuntime(gomock.NewController(t))} + config.ComponentConfigurations = runtime + for _, declaration := range []schema.StackImport{{Context: map[string]any{"stage": "dev"}}, {SkipIfMissing: true}, {TTL: "1h"}, {NestedImports: "local"}} { + _, err := registerConfigurationImport(config, "terragrunt://live", &declaration) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + } + handled, err = registerConfigurationImport(config, "terragrunt://live", &schema.StackImport{}) + require.NoError(t, err) + require.True(t, handled) + require.Equal(t, []string{"terragrunt://live"}, runtime.imports) + runtime.err = errors.New("registration failed") + _, err = registerConfigurationImport(config, "terragrunt://live", &schema.StackImport{}) + require.ErrorIs(t, err, runtime.err) +} + +func TestYAMLImportsRegisterBeforeDiscovery(t *testing.T) { + root := t.TempDir() + path := filepath.Join(root, "adopt.yaml") + require.NoError(t, os.WriteFile(path, []byte("import:\n - terragrunt://live\nvars:\n stage: dev\n"), 0o600)) + runtime := &importRuntime{MockComponentConfigurationRuntime: NewMockComponentConfigurationRuntime(gomock.NewController(t))} + config := &schema.AtmosConfiguration{BasePathAbsolute: root, ComponentConfigurations: runtime} + config.Components.Terraform.ConfigurationProviders = map[string]schema.ComponentConfiguration{"terragrunt": {Enabled: true}} + _, err := ProcessYAMLConfigFile(config, root, path, map[string]map[string]any{}, nil, false, true, false, false, nil, nil, nil, nil, "") + require.NoError(t, err) + require.Equal(t, []string{"terragrunt://live"}, runtime.imports) +} + +func TestConfigurationWatchExclusionsDoNotChangeSharedIndex(t *testing.T) { + root := t.TempDir() + config := &schema.AtmosConfiguration{BasePathAbsolute: root} + index := newChangedFilesIndex(config, []string{filepath.Join("live", ".terragrunt-cache", "generated.tf"), filepath.Join("live", "new.yaml")}, root) + settings := map[string]any{"configuration": map[string]any{"watch_excludes": []any{"**/.terragrunt-cache/**"}}} + filtered, err := configurationWatchIndex(config, settings, index) + require.NoError(t, err) + require.Equal(t, []string{filepath.Join(root, "live", "new.yaml")}, filtered.getAllFiles()) + require.Len(t, index.getAllFiles(), 2) + unchanged, err := configurationWatchIndex(config, nil, index) + require.NoError(t, err) + require.Same(t, index, unchanged) + for _, invalid := range []any{"[", 42} { + settings["configuration"].(map[string]any)["watch_excludes"] = []any{invalid} + _, err = configurationWatchIndex(config, settings, index) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) + } +} + +func TestConfigurationWatchExclusionsFailClosedOnPathsOutsideTheProject(t *testing.T) { + // A relative project root cannot be compared with absolute changed files; the watch must not silently match nothing. + config := &schema.AtmosConfiguration{BasePathAbsolute: "relative-root"} + absolute := filepath.Join(t.TempDir(), "live", "new.yaml") + index := newChangedFilesIndex(config, []string{absolute}, "") + settings := map[string]any{"configuration": map[string]any{"watch_excludes": []any{"**/*.tf"}}} + filtered, err := configurationWatchIndex(config, settings, index) + require.Error(t, err) + require.Nil(t, filtered) +} + +func TestDependencyChangeCheckReportsInvalidWatchExclusions(t *testing.T) { + root := t.TempDir() + config := &schema.AtmosConfiguration{BasePathAbsolute: root} + index := newChangedFilesIndex(config, []string{filepath.Join("live", "changed.hcl")}, root) + component := map[string]any{} + settings := map[string]any{"configuration": map[string]any{"watch_excludes": []any{"["}}} + var affected []schema.Affected + err := checkDependencyChangesIndexed(&affected, config, "app", "dev", "terraform", &component, settings, index, false, nil, false) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) + require.Empty(t, affected, "an invalid exclusion must not be treated as a clean comparison") +} + +func TestYAMLImportsRejectProviderImportsThatCannotBeRegistered(t *testing.T) { + root := t.TempDir() + path := filepath.Join(root, "adopt.yaml") + require.NoError(t, os.WriteFile(path, []byte("import:\n - terragrunt://live\nvars:\n stage: dev\n"), 0o600)) + runtime := &importRuntime{MockComponentConfigurationRuntime: NewMockComponentConfigurationRuntime(gomock.NewController(t)), err: errUtils.ErrPluginInvalidConfig} + config := &schema.AtmosConfiguration{BasePathAbsolute: root, ComponentConfigurations: runtime} + config.Components.Terraform.ConfigurationProviders = map[string]schema.ComponentConfiguration{"terragrunt": {Enabled: true}} + _, err := ProcessYAMLConfigFile(config, root, path, map[string]map[string]any{}, nil, false, true, false, false, nil, nil, nil, nil, "") + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig, "a rejected provider import must fail the stack load instead of being skipped") + require.Equal(t, []string{"terragrunt://live"}, runtime.imports) + + // A disabled provider fails the same way before any plugin is consulted. + config.Components.Terraform.ConfigurationProviders = map[string]schema.ComponentConfiguration{"terragrunt": {Enabled: false}} + _, err = ProcessYAMLConfigFile(config, root, path, map[string]map[string]any{}, nil, false, true, false, false, nil, nil, nil, nil, "") + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) + require.Len(t, runtime.imports, 1, "the disabled provider is never asked to register the import") +} diff --git a/internal/exec/describe_affected_components.go b/internal/exec/describe_affected_components.go index f9b5184631d..3919313250a 100644 --- a/internal/exec/describe_affected_components.go +++ b/internal/exec/describe_affected_components.go @@ -1174,8 +1174,12 @@ func checkDependencyChangesIndexed( // Get file/folder dependencies from dependencies.components or settings.depends_on. deps := getFileFolderDependencies(*componentSection, settingsSection) provider := configurationWatchPaths(atmosConfig, settingsSection, deps) + watchIndex, err := configurationWatchIndex(atmosConfig, settingsSection, filesIndex) + if err != nil { + return err + } if len(deps) > 0 { - index, matched := configurationMatchInputs(filesIndex, deps, provider) + index, matched := configurationMatchInputs(watchIndex, deps, provider) isFolderOrFileChanged, changedType, changedFileOrFolder, err := isComponentDependentFolderOrFileChangedIndexed( index, matched, diff --git a/internal/exec/describe_affected_configuration.go b/internal/exec/describe_affected_configuration.go index 600fda2082d..4b706ec7b98 100644 --- a/internal/exec/describe_affected_configuration.go +++ b/internal/exec/describe_affected_configuration.go @@ -6,6 +6,8 @@ import ( "slices" "sync/atomic" + "github.com/bmatcuk/doublestar/v4" + errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/schema" ) @@ -28,6 +30,40 @@ func rebaseConfigurationDiscovery(config *schema.AtmosConfiguration) error { return nil } +// configurationWatchIndex applies provider-declared cache exclusions only to its +// file watches. It never changes YAML dependencies or the shared Git change set. +func configurationWatchIndex(config *schema.AtmosConfiguration, settings map[string]any, original *changedFilesIndex) (*changedFilesIndex, error) { + provider, _ := settings["configuration"].(map[string]any) + excludes, _ := provider["watch_excludes"].([]any) + if len(excludes) == 0 { + return original, nil + } + patterns := make([]string, 0, len(excludes)) + for _, value := range excludes { + pattern, ok := value.(string) + if !ok || !doublestar.ValidatePattern(pattern) { + return nil, errUtils.ErrPluginInvalidConfig + } + patterns = append(patterns, pattern) + } + files := make([]string, 0, len(original.getAllFiles())) + for _, path := range original.getAllFiles() { + relative, err := filepath.Rel(config.BasePathAbsolute, path) + if err != nil { + return nil, err + } + excluded := false + for _, pattern := range patterns { + matched, _ := doublestar.Match(pattern, filepath.ToSlash(relative)) + excluded = excluded || matched + } + if !excluded { + files = append(files, path) + } + } + return newChangedFilesIndex(config, files, config.BasePathAbsolute), nil +} + // configurationWatchPaths resolves provider-owned watches against the project // root without putting checkout-specific absolute paths into stack comparisons. // Existing YAML dependency path semantics remain unchanged. It reports whether diff --git a/internal/exec/stack_processor_utils.go b/internal/exec/stack_processor_utils.go index 70556c20359..5022336b16f 100644 --- a/internal/exec/stack_processor_utils.go +++ b/internal/exec/stack_processor_utils.go @@ -1634,6 +1634,12 @@ func processYAMLConfigFileWithContextInternal( return nil, nil, impErr } imp = renderedImp + if handled, importErr := registerConfigurationImport(atmosConfig, imp, &importStruct); handled || importErr != nil { + if importErr != nil { + return nil, nil, importErr + } + continue + } // Snapshot the accumulated context for the child import. The outer loop is // sequential, so this captures only the sections from earlier siblings. diff --git a/pkg/component/configuration/files.go b/pkg/component/configuration/files.go index d3bd4fd7235..d707675e918 100644 --- a/pkg/component/configuration/files.go +++ b/pkg/component/configuration/files.go @@ -34,7 +34,7 @@ func WriteFiles(directory string, files []schema.ComponentConfigurationFile) err if !filepath.IsLocal(file.Path) { return errUtils.ErrPluginProtocol } - if file.IfExists != "error" && file.IfExists != "skip" && file.IfExists != "overwrite" { + if !validFilePolicy(file.IfExists) { return errUtils.ErrPluginUnsupportedExtension } } @@ -47,6 +47,17 @@ func WriteFiles(directory string, files []schema.ComponentConfigurationFile) err } func writeFile(root *os.Root, file schema.ComponentConfigurationFile) error { + remove, err := checkFileOwnership(root, file) + if err != nil { + return err + } + if remove { + err := root.Remove(file.Path) + if errors.Is(err, os.ErrNotExist) { + return nil + } + return err + } mode := os.FileMode(fileMode) if file.Sensitive { mode = secretMode @@ -56,7 +67,7 @@ func writeFile(root *os.Root, file schema.ComponentConfigurationFile) error { return err } flags := os.O_WRONLY | os.O_CREATE | os.O_EXCL - if file.IfExists == "overwrite" { + if file.IfExists == "overwrite" || file.IfExists == "overwrite_terragrunt" { flags = os.O_WRONLY | os.O_CREATE | os.O_TRUNC } output, err := root.OpenFile(file.Path, flags, mode) diff --git a/pkg/component/configuration/ownership.go b/pkg/component/configuration/ownership.go new file mode 100644 index 00000000000..90336cf0d12 --- /dev/null +++ b/pkg/component/configuration/ownership.go @@ -0,0 +1,68 @@ +package configuration + +import ( + "bufio" + "errors" + "fmt" + "io" + "os" + "strings" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/schema" +) + +// This marker is part of the pinned Terragrunt generated-file ownership contract. +const terragruntSignature = "Generated by Terragrunt. Sig: nIlQXj57tbuaRZEa" + +func validFilePolicy(policy string) bool { + switch policy { + case "error", "skip", "overwrite", "overwrite_terragrunt", "remove", "remove_terragrunt": + return true + default: + return false + } +} + +// checkFileOwnership never follows a final symlink when deciding ownership. +// The os.Root handle additionally confines all ancestor traversal to the provisioned directory. +func checkFileOwnership(root *os.Root, file schema.ComponentConfigurationFile) (bool, error) { + remove := file.IfExists == "remove" || file.IfExists == "remove_terragrunt" + stat, err := root.Lstat(file.Path) + if errors.Is(err, os.ErrNotExist) { + return remove, nil + } + if err != nil { + return false, err + } + if !stat.Mode().IsRegular() { + return false, fmt.Errorf("%w: generated path %s is not a regular file", errUtils.ErrPluginInvalidConfig, file.Path) + } + if file.IfExists != "overwrite_terragrunt" && file.IfExists != "remove_terragrunt" { + return remove, nil + } + if err := verifyTerragruntSignature(root, file.Path); err != nil { + return false, err + } + return remove, nil +} + +// verifyTerragruntSignature requires the first line of the generated file to carry the Terragrunt signature. +func verifyTerragruntSignature(root *os.Root, path string) error { + input, err := root.Open(path) + if err != nil { + return err + } + first, readErr := bufio.NewReader(input).ReadString('\n') + closeErr := input.Close() + if readErr != nil && !errors.Is(readErr, io.EOF) { + return errors.Join(readErr, closeErr) + } + if closeErr != nil { + return closeErr + } + if !strings.HasSuffix(strings.TrimSpace(first), terragruntSignature) { + return fmt.Errorf("%w: generated path %s is not owned by Terragrunt", errUtils.ErrPluginInvalidConfig, path) + } + return nil +} diff --git a/pkg/component/configuration/ownership_test.go b/pkg/component/configuration/ownership_test.go new file mode 100644 index 00000000000..9c8e7986857 --- /dev/null +++ b/pkg/component/configuration/ownership_test.go @@ -0,0 +1,88 @@ +package configuration + +import ( + "os" + "path/filepath" + "runtime" + "testing" + + "github.com/stretchr/testify/require" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/schema" +) + +func TestTerragruntOwnershipPolicies(t *testing.T) { + for _, policy := range []string{"overwrite_terragrunt", "remove_terragrunt", "remove"} { + for _, existing := range []string{"", "unsigned", "# " + terragruntSignature, "// " + terragruntSignature + "\nold"} { + t.Run(policy+"/"+existing, func(t *testing.T) { + directory := t.TempDir() + path := filepath.Join(directory, "generated.tf") + if existing != "" { + require.NoError(t, os.WriteFile(path, []byte(existing), 0o600)) + } + file := schema.ComponentConfigurationFile{Path: "generated.tf", Content: []byte("replacement"), IfExists: policy} + err := WriteFiles(directory, []schema.ComponentConfigurationFile{file}) + if existing == "unsigned" && policy != "remove" { + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) + contents, err := os.ReadFile(path) + require.NoError(t, err) + require.Equal(t, existing, string(contents)) + return + } + require.NoError(t, err) + if policy == "overwrite_terragrunt" { + contents, err := os.ReadFile(path) + require.NoError(t, err) + require.Equal(t, "replacement", string(contents)) + } else { + require.NoFileExists(t, path) + } + }) + } + } +} + +func TestGeneratedOwnershipRejectsSymlinksAndDirectories(t *testing.T) { + root := t.TempDir() + require.NoError(t, os.Mkdir(filepath.Join(root, "directory.tf"), 0o700)) + require.Error(t, WriteFiles(root, []schema.ComponentConfigurationFile{{Path: "directory.tf", IfExists: "remove"}})) + path := filepath.Join(root, "target.tf") + original := "# " + terragruntSignature + "\nkeep" + require.NoError(t, os.WriteFile(path, []byte(original), 0o600)) + if err := os.Symlink(path, filepath.Join(root, "link.tf")); err != nil { + t.Skipf("symlink unavailable: %v", err) + } + for _, policy := range []string{"overwrite", "overwrite_terragrunt", "remove", "remove_terragrunt"} { + require.Error(t, WriteFiles(root, []schema.ComponentConfigurationFile{{Path: "link.tf", IfExists: policy}})) + } + contents, err := os.ReadFile(path) + require.NoError(t, err) + require.Equal(t, original, string(contents)) +} + +func TestTerragruntOwnershipFailsClosedWhenSignatureCannotBeRead(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("file permissions are not enforced on Windows") + } + for _, policy := range []string{"overwrite_terragrunt", "remove_terragrunt"} { + t.Run(policy, func(t *testing.T) { + directory := t.TempDir() + path := filepath.Join(directory, "generated.tf") + original := "# " + terragruntSignature + "\nkeep" + require.NoError(t, os.WriteFile(path, []byte(original), 0o600)) + require.NoError(t, os.Chmod(path, 0)) + t.Cleanup(func() { _ = os.Chmod(path, 0o600) }) + if file, err := os.Open(path); err == nil { + _ = file.Close() + t.Skip("permissions are not enforced for this user") + } + err := WriteFiles(directory, []schema.ComponentConfigurationFile{{Path: "generated.tf", Content: []byte("replacement"), IfExists: policy}}) + require.ErrorIs(t, err, os.ErrPermission, "ownership that cannot be proven must not allow replacement or removal") + require.NoError(t, os.Chmod(path, 0o600)) + contents, err := os.ReadFile(path) + require.NoError(t, err) + require.Equal(t, original, string(contents)) + }) + } +} diff --git a/pkg/component/configuration/registry.go b/pkg/component/configuration/registry.go index ec27287cac6..9564ea4277a 100644 --- a/pkg/component/configuration/registry.go +++ b/pkg/component/configuration/registry.go @@ -34,11 +34,11 @@ func Register(name string, factory Factory) error { return nil } -// Attach creates private state only when automatic configuration discovery is enabled. +// Attach creates private state only when a configuration provider is enabled. func Attach(ctx context.Context, config *schema.AtmosConfiguration, operation string) error { defer perf.Track(config, "configuration.Attach")() - if !config.HasComponentDiscovery() { + if !config.HasComponentConfiguration() { return nil } if err := ctx.Err(); err != nil { diff --git a/pkg/component/execution/arguments.go b/pkg/component/execution/arguments.go new file mode 100644 index 00000000000..a02a94f8b08 --- /dev/null +++ b/pkg/component/execution/arguments.go @@ -0,0 +1,143 @@ +package execution + +import ( + "fmt" + "maps" + "os" + "path/filepath" + "slices" + "strings" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/perf" + "github.com/cloudposse/atmos/pkg/schema" +) + +const ( + varFileFlag = "-var-file=" + varFlagPrefix = "-var" +) + +// ArgumentsRequest describes one Terraform invocation for ApplyExtraArguments. +type ArgumentsRequest struct { + // Command is the matching name from CommandName. + Command string + // Args is the full argument vector, starting with the command. + Args []string + // InsertAt is the index after the command and its subcommands where extra arguments go. + InsertAt int + // PlanFileArgument reports that the last argument is a saved plan. Terraform rejects -var + // options beside a saved plan, so they are dropped. + PlanFileArgument bool + // WorkingDirectory resolves relative variable file paths. Terraform runs there. + WorkingDirectory string +} + +// ArgumentsResult is the rewritten invocation. +type ArgumentsResult struct { + Args []string + // Environment holds KEY=value entries sorted by key. Later blocks override earlier ones. + Environment []string +} + +// ApplyExtraArguments adds the arguments, variable files, and environment of every block that +// lists the command, in declaration order. The input arguments are never modified. +func ApplyExtraArguments(items []schema.ComponentExtraArguments, request *ArgumentsRequest) (ArgumentsResult, error) { + defer perf.Track(nil, "execution.ApplyExtraArguments")() + + var inserted []string + environment := map[string]string{} + for i := range items { + item := &items[i] + if !slices.Contains(item.Commands, request.Command) { + continue + } + maps.Copy(environment, item.Environment) + arguments, err := blockArguments(item, request) + if err != nil { + return ArgumentsResult{}, err + } + inserted = append(inserted, arguments...) + } + position := min(max(request.InsertAt, 0), len(request.Args)) + args := slices.Concat(request.Args[:position], inserted, request.Args[position:]) + return ArgumentsResult{Args: args, Environment: sortedEnvironment(environment)}, nil +} + +func blockArguments(item *schema.ComponentExtraArguments, request *ArgumentsRequest) ([]string, error) { + arguments := slices.Clone(item.Arguments) + if request.PlanFileArgument { + return dropVariableArguments(arguments), nil + } + required, err := requiredVarFiles(item.RequiredVarFiles, request.WorkingDirectory) + if err != nil { + return nil, err + } + for _, file := range required { + arguments = append(arguments, varFileFlag+file) + } + for _, file := range keepLast(item.OptionalVarFiles) { + if fileExists(resolve(request.WorkingDirectory, file)) { + arguments = append(arguments, varFileFlag+file) + } + } + return arguments, nil +} + +// dropVariableArguments removes -var and -var-file options. A bare option takes the next +// argument as its value ("-var", "name=value"), so that value goes with it; otherwise it +// would be left behind as an unexpected positional argument. +func dropVariableArguments(arguments []string) []string { + var kept []string + for i := 0; i < len(arguments); i++ { + if !strings.HasPrefix(arguments[i], varFlagPrefix) { + kept = append(kept, arguments[i]) + continue + } + if !strings.Contains(arguments[i], "=") { + i++ + } + } + return kept +} + +func requiredVarFiles(files []string, directory string) ([]string, error) { + files = keepLast(files) + for _, file := range files { + if !fileExists(resolve(directory, file)) { + return nil, fmt.Errorf("%w: %s", errUtils.ErrComponentVarFileMissing, file) + } + } + return files, nil +} + +// keepLast removes duplicates and keeps the last occurrence of each item, as Terragrunt does. +func keepLast(items []string) []string { + var result []string + for i, item := range items { + if !slices.Contains(items[i+1:], item) { + result = append(result, item) + } + } + return result +} + +func resolve(directory, path string) string { + if filepath.IsAbs(path) || directory == "" { + return path + } + return filepath.Join(directory, path) +} + +func fileExists(path string) bool { + info, err := os.Stat(path) + return err == nil && !info.IsDir() +} + +func sortedEnvironment(environment map[string]string) []string { + var result []string + for _, key := range slices.Sorted(maps.Keys(environment)) { + result = append(result, key+"="+environment[key]) + } + return result +} diff --git a/pkg/component/execution/arguments_test.go b/pkg/component/execution/arguments_test.go new file mode 100644 index 00000000000..347c80612b6 --- /dev/null +++ b/pkg/component/execution/arguments_test.go @@ -0,0 +1,184 @@ +package execution + +import ( + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/schema" +) + +func TestCommandName(t *testing.T) { + for _, test := range []struct { + name string + command string + flags []string + want string + }{ + {"plain plan", "plan", nil, "plan"}, + {"plan with destroy flag only plans a destroy", "plan", []string{"-destroy"}, "plan"}, + {"destroy", "destroy", nil, "destroy"}, + {"apply with destroy flag", "apply", []string{"-destroy"}, "destroy"}, + {"apply with double dash destroy", "apply", []string{"--destroy"}, "destroy"}, + {"apply with explicit true", "apply", []string{"-destroy=true"}, "destroy"}, + {"apply with explicit false", "apply", []string{"-destroy=false"}, "apply"}, + {"last destroy flag wins", "apply", []string{"-destroy", "-destroy=false"}, "apply"}, + {"init", "init", nil, "init"}, + } { + t.Run(test.name, func(t *testing.T) { + require.Equal(t, test.want, CommandName(test.command, test.flags)) + }) + } +} + +func extra(commands ...string) schema.ComponentExtraArguments { + return schema.ComponentExtraArguments{Commands: commands} +} + +func TestApplyExtraArgumentsKeepsDeclarationOrder(t *testing.T) { + dir := t.TempDir() + required := filepath.Join(dir, "required.tfvars") + optional := filepath.Join(dir, "optional.tfvars") + require.NoError(t, os.WriteFile(required, nil, 0o600)) + require.NoError(t, os.WriteFile(optional, nil, 0o600)) + + first := extra("plan") + first.Arguments = []string{"-lock-timeout=5m"} + first.RequiredVarFiles = []string{required} + second := extra("plan", "apply") + second.Arguments = []string{"-compact-warnings"} + second.OptionalVarFiles = []string{optional, filepath.Join(dir, "absent.tfvars")} + other := extra("apply") + other.Arguments = []string{"-never-for-plan"} + + got, err := ApplyExtraArguments([]schema.ComponentExtraArguments{first, other, second}, &ArgumentsRequest{ + Command: "plan", Args: []string{"plan", "-out", "plan.tfplan"}, InsertAt: 1, WorkingDirectory: dir, + }) + require.NoError(t, err) + require.Equal(t, []string{ + "plan", "-lock-timeout=5m", "-var-file=" + required, "-compact-warnings", "-var-file=" + optional, + "-out", "plan.tfplan", + }, got.Args) +} + +func TestApplyExtraArgumentsDoesNotMutateInput(t *testing.T) { + item := extra("plan") + item.Arguments = []string{"-x"} + args := []string{"plan", "-out", "p"} + got, err := ApplyExtraArguments([]schema.ComponentExtraArguments{item}, &ArgumentsRequest{Command: "plan", Args: args, InsertAt: 1}) + require.NoError(t, err) + require.Equal(t, []string{"plan", "-out", "p"}, args) + got.Args[0] = "changed" + require.Equal(t, "plan", args[0]) + require.Equal(t, []string{"-x"}, item.Arguments) +} + +func TestApplyExtraArgumentsIgnoresNonListedCommands(t *testing.T) { + item := extra("apply") + item.Arguments = []string{"-never"} + item.Environment = map[string]string{"NEVER": "1"} + item.RequiredVarFiles = []string{"does-not-exist.tfvars"} + got, err := ApplyExtraArguments([]schema.ComponentExtraArguments{item}, &ArgumentsRequest{Command: "init", Args: []string{"init", "-input=false"}, InsertAt: 1}) + require.NoError(t, err) + require.Equal(t, []string{"init", "-input=false"}, got.Args) + require.Empty(t, got.Environment) +} + +func TestApplyExtraArgumentsMatchesInitIndependently(t *testing.T) { + forInit := extra("init") + forInit.Arguments = []string{"-backend-config=x"} + forPlan := extra("plan") + forPlan.Arguments = []string{"-lock=false"} + items := []schema.ComponentExtraArguments{forInit, forPlan} + + initArgs, err := ApplyExtraArguments(items, &ArgumentsRequest{Command: "init", Args: []string{"init"}, InsertAt: 1}) + require.NoError(t, err) + require.Equal(t, []string{"init", "-backend-config=x"}, initArgs.Args) + planArgs, err := ApplyExtraArguments(items, &ArgumentsRequest{Command: "plan", Args: []string{"plan"}, InsertAt: 1}) + require.NoError(t, err) + require.Equal(t, []string{"plan", "-lock=false"}, planArgs.Args) +} + +func TestApplyExtraArgumentsRequiredVarFileMustExist(t *testing.T) { + dir := t.TempDir() + item := extra("plan") + item.RequiredVarFiles = []string{"relative-missing.tfvars"} + _, err := ApplyExtraArguments([]schema.ComponentExtraArguments{item}, &ArgumentsRequest{Command: "plan", Args: []string{"plan"}, InsertAt: 1, WorkingDirectory: dir}) + require.ErrorIs(t, err, errUtils.ErrComponentVarFileMissing) + require.ErrorContains(t, err, "relative-missing.tfvars") +} + +func TestApplyExtraArgumentsResolvesRelativeFilesInWorkingDirectory(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "here.tfvars"), nil, 0o600)) + item := extra("plan") + item.RequiredVarFiles = []string{"here.tfvars"} + item.OptionalVarFiles = []string{"missing.tfvars"} + got, err := ApplyExtraArguments([]schema.ComponentExtraArguments{item}, &ArgumentsRequest{Command: "plan", Args: []string{"plan"}, InsertAt: 1, WorkingDirectory: dir}) + require.NoError(t, err) + // Terraform runs in the same directory, so the path is passed as written. + require.Equal(t, []string{"plan", "-var-file=here.tfvars"}, got.Args) +} + +func TestApplyExtraArgumentsKeepsLastDuplicateVarFile(t *testing.T) { + dir := t.TempDir() + for _, name := range []string{"a", "b"} { + require.NoError(t, os.WriteFile(filepath.Join(dir, name), nil, 0o600)) + } + item := extra("plan") + item.RequiredVarFiles = []string{"a", "b", "a"} + got, err := ApplyExtraArguments([]schema.ComponentExtraArguments{item}, &ArgumentsRequest{Command: "plan", Args: []string{"plan"}, InsertAt: 1, WorkingDirectory: dir}) + require.NoError(t, err) + require.Equal(t, []string{"plan", "-var-file=b", "-var-file=a"}, got.Args) +} + +func TestApplyExtraArgumentsEnvironmentLaterBlocksWin(t *testing.T) { + first := extra("plan") + first.Environment = map[string]string{"TF_LOG": "INFO", "KEEP": "1"} + second := extra("plan") + second.Environment = map[string]string{"TF_LOG": "DEBUG", "ADD": "2"} + got, err := ApplyExtraArguments([]schema.ComponentExtraArguments{first, second}, &ArgumentsRequest{Command: "plan", Args: []string{"plan"}, InsertAt: 1}) + require.NoError(t, err) + require.Equal(t, []string{"ADD=2", "KEEP=1", "TF_LOG=DEBUG"}, got.Environment) +} + +func TestApplyExtraArgumentsSavedPlanDropsVariableArguments(t *testing.T) { + item := extra("apply") + item.Arguments = []string{"-var=a=b", "-var-file=x.tfvars", "-parallelism=1"} + item.RequiredVarFiles = []string{"never-checked.tfvars"} + item.Environment = map[string]string{"STILL": "applied"} + args := []string{"apply", "-auto-approve", "plan.tfplan"} + + withPlan, err := ApplyExtraArguments([]schema.ComponentExtraArguments{item}, &ArgumentsRequest{Command: "apply", Args: args, InsertAt: 1, PlanFileArgument: true}) + require.NoError(t, err) + require.Equal(t, []string{"apply", "-parallelism=1", "-auto-approve", "plan.tfplan"}, withPlan.Args) + require.Equal(t, []string{"STILL=applied"}, withPlan.Environment) + + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "never-checked.tfvars"), nil, 0o600)) + withoutPlan, err := ApplyExtraArguments([]schema.ComponentExtraArguments{item}, &ArgumentsRequest{Command: "apply", Args: []string{"apply"}, InsertAt: 1, WorkingDirectory: dir}) + require.NoError(t, err) + require.Equal(t, []string{"apply", "-var=a=b", "-var-file=x.tfvars", "-parallelism=1", "-var-file=never-checked.tfvars"}, withoutPlan.Args) +} + +func TestApplyExtraArgumentsInsertsAfterSubcommands(t *testing.T) { + item := extra("state") + item.Arguments = []string{"-lock=false"} + got, err := ApplyExtraArguments([]schema.ComponentExtraArguments{item}, &ArgumentsRequest{Command: "state", Args: []string{"state", "list", "addr"}, InsertAt: 2}) + require.NoError(t, err) + require.Equal(t, []string{"state", "list", "-lock=false", "addr"}, got.Args) +} + +func TestApplyExtraArgumentsSavedPlanDropsVariableValuesGivenAsSeparateArguments(t *testing.T) { + item := extra("apply") + item.Arguments = []string{"-var", "name=value", "-parallelism=1", "-var-file", "x.tfvars", "-lock=false"} + args := []string{"apply", "plan.tfplan"} + + result, err := ApplyExtraArguments([]schema.ComponentExtraArguments{item}, &ArgumentsRequest{Command: "apply", Args: args, InsertAt: 1, PlanFileArgument: true}) + + require.NoError(t, err) + require.Equal(t, []string{"apply", "-parallelism=1", "-lock=false", "plan.tfplan"}, result.Args) +} diff --git a/pkg/component/execution/execution.go b/pkg/component/execution/execution.go new file mode 100644 index 00000000000..4bbb2adf9be --- /dev/null +++ b/pkg/component/execution/execution.go @@ -0,0 +1,114 @@ +// Package execution applies provider-evaluated hooks and extra arguments to Terraform commands. +// +// A configuration provider (for example the native Terragrunt plugin) evaluates hooks and extra +// arguments but never runs them. The host owns execution: it matches them against the command +// being run, runs hooks with the invocation's environment, working directory, cancellation, and +// I/O, and surfaces failures with their exit codes. The rules follow Terragrunt's own: a command +// is matched by name, `apply -destroy` counts as `destroy`, before hooks gate the command, after +// hooks need run_on_error to follow a failure, and error hooks match on the failure text. +package execution + +import ( + "fmt" + "regexp" + "strconv" + "strings" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/perf" + "github.com/cloudposse/atmos/pkg/schema" +) + +const ( + commandApply = "apply" + commandDestroy = "destroy" + destroyFlag = "-destroy" +) + +// HookKind names the block a hook was declared in. +type HookKind string + +// Hook kinds in the order their lifecycle runs. +const ( + KindBefore HookKind = "before_hook" + KindAfter HookKind = "after_hook" + KindError HookKind = "error_hook" +) + +// CommandName returns the name that hooks and extra arguments match against. +// It is "destroy" for `destroy` and for `apply` with the destroy flag set, and the command +// itself otherwise, so `plan -destroy` still matches "plan". +func CommandName(command string, flags []string) string { + defer perf.Track(nil, "execution.CommandName")() + + if command == commandDestroy || (command == commandApply && hasDestroyFlag(flags)) { + return commandDestroy + } + return command +} + +// hasDestroyFlag reads the flag as Terraform does: one or two dashes, an optional boolean +// value, and the last occurrence wins. +func hasDestroyFlag(flags []string) bool { + destroy := false + for _, flag := range flags { + name, raw, hasValue := strings.Cut(flag, "=") + if name != destroyFlag && name != "-"+destroyFlag { + continue + } + if !hasValue { + destroy = true + continue + } + parsed, err := strconv.ParseBool(raw) + destroy = err == nil && parsed + } + return destroy +} + +// Validate rejects hooks the host cannot run, so a malformed provider response fails before any +// command starts instead of silently skipping a hook. +func Validate(config *schema.ComponentExecution) error { + defer perf.Track(nil, "execution.Validate")() + + if config == nil { + return nil + } + for _, group := range []struct { + kind HookKind + hooks []schema.ComponentHook + }{{KindBefore, config.BeforeHooks}, {KindAfter, config.AfterHooks}, {KindError, config.ErrorHooks}} { + for i := range group.hooks { + if err := validateHook(group.kind, &group.hooks[i]); err != nil { + return err + } + } + } + return nil +} + +func validateHook(kind HookKind, hook *schema.ComponentHook) error { + if len(hook.Execute) == 0 || strings.TrimSpace(hook.Execute[0]) == "" { + return fmt.Errorf("%w: %s %q has no command to execute", errUtils.ErrPluginProtocol, kind, hook.Name) + } + for _, pattern := range hook.OnErrors { + if _, err := regexp.Compile(pattern); err != nil { + return fmt.Errorf("%w: %s %q on_errors is not a valid regular expression", errUtils.ErrPluginProtocol, kind, hook.Name) + } + } + return nil +} + +// hooksFor keeps declaration order and selects the hooks that list the command. +func hooksFor(hooks []schema.ComponentHook, command string) []*schema.ComponentHook { + var selected []*schema.ComponentHook + for i := range hooks { + for _, listed := range hooks[i].Commands { + if listed == command { + selected = append(selected, &hooks[i]) + break + } + } + } + return selected +} diff --git a/pkg/component/execution/lifecycle.go b/pkg/component/execution/lifecycle.go new file mode 100644 index 00000000000..64e7ca02a05 --- /dev/null +++ b/pkg/component/execution/lifecycle.go @@ -0,0 +1,149 @@ +package execution + +import ( + "context" + "errors" + "fmt" + "regexp" + "strings" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/perf" + "github.com/cloudposse/atmos/pkg/schema" +) + +// Runner executes one hook and reports its failure and captured output. +// The output feeds error hook matching and is never printed again. +type Runner interface { + RunHook(ctx context.Context, kind HookKind, hook *schema.ComponentHook) (output string, err error) +} + +// failure is a failed phase with the output it produced. +type failure struct { + err error + output string +} + +// Run executes the command lifecycle: before hooks, the action, after hooks, then error hooks. +// +// A failing before hook stops the action. After hooks run after a failure only when they set +// run_on_error. Error hooks run when any earlier phase failed and an on_errors expression matches +// that phase's error or output. Cancellation stops every remaining hook. The action's error is +// returned unchanged when nothing else failed. +func Run(ctx context.Context, config *schema.ComponentExecution, command string, runner Runner, action func() (string, error)) error { + defer perf.Track(nil, "execution.Run")() + + if config == nil || (len(config.BeforeHooks)+len(config.AfterHooks)+len(config.ErrorHooks) == 0) { + _, err := action() + return err + } + if err := ctx.Err(); err != nil { + return err + } + + failures := runHooks(ctx, runner, KindBefore, hooksFor(config.BeforeHooks, command), nil) + if len(failures) == 0 { + if output, err := action(); err != nil { + failures = append(failures, failure{err: err, output: output}) + } + } + if err := ctx.Err(); err != nil { + return joinFailures(failures, err) + } + + failures = append(failures, runHooks(ctx, runner, KindAfter, hooksFor(config.AfterHooks, command), failures)...) + if err := ctx.Err(); err != nil { + return joinFailures(failures, err) + } + failures = append(failures, runErrorHooks(ctx, runner, hooksFor(config.ErrorHooks, command), failures)...) + return joinFailures(failures, ctx.Err()) +} + +// runHooks mirrors Terragrunt: every hook sees the failures before it, including earlier hooks +// of the same kind, and runs only when there are none or it sets run_on_error. +func runHooks(ctx context.Context, runner Runner, kind HookKind, hooks []*schema.ComponentHook, prior []failure) []failure { + var failures []failure + for _, hook := range hooks { + if len(prior)+len(failures) > 0 && !hook.RunOnError { + continue + } + if err := ctx.Err(); err != nil { + // Record the cancellation so the action does not start after an interrupted hook. + failures = append(failures, failure{err: err}) + break + } + if output, err := runner.RunHook(ctx, kind, hook); err != nil { + failures = append(failures, failure{err: hookError(kind, hook, err), output: output}) + } + } + return failures +} + +func runErrorHooks(ctx context.Context, runner Runner, hooks []*schema.ComponentHook, prior []failure) []failure { + if len(hooks) == 0 || len(prior) == 0 { + return nil + } + var text strings.Builder + for _, item := range prior { + text.WriteString(item.err.Error() + "\n" + item.output + "\n") + } + var failures []failure + for _, hook := range hooks { + if ctx.Err() != nil { + break + } + matched, err := matchesAny(hook.OnErrors, text.String()) + if err != nil { + failures = append(failures, failure{err: fmt.Errorf("matching on_errors of error hook %q: %w", hook.Name, err)}) + continue + } + if !matched { + continue + } + if output, err := runner.RunHook(ctx, KindError, hook); err != nil { + failures = append(failures, failure{err: hookError(KindError, hook, err), output: output}) + } + } + return failures +} + +func matchesAny(patterns []string, text string) (bool, error) { + for _, pattern := range patterns { + matched, err := regexp.MatchString(pattern, text) + if err != nil { + return false, err + } + if matched { + return true, nil + } + } + return false, nil +} + +func hookError(kind HookKind, hook *schema.ComponentHook, err error) error { + return fmt.Errorf("%w: %s %q: %w", errUtils.ErrComponentHookFailed, kind, hook.Name, err) +} + +// joinFailures keeps a lone error intact so callers can still inspect its concrete type. +func joinFailures(failures []failure, extra error) error { + errs := make([]error, 0, len(failures)+1) + for _, item := range failures { + errs = append(errs, item.err) + } + if extra != nil && !containsError(errs, extra) { + errs = append(errs, extra) + } + if len(errs) == 1 { + return errs[0] + } + return errors.Join(errs...) +} + +func containsError(errs []error, target error) bool { + for _, err := range errs { + if errors.Is(err, target) { + return true + } + } + return false +} diff --git a/pkg/component/execution/lifecycle_cancellation_test.go b/pkg/component/execution/lifecycle_cancellation_test.go new file mode 100644 index 00000000000..1fc72c2e98d --- /dev/null +++ b/pkg/component/execution/lifecycle_cancellation_test.go @@ -0,0 +1,89 @@ +package execution + +import ( + "context" + "errors" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cloudposse/atmos/pkg/schema" +) + +func errorHook(name string) schema.ComponentHook { + return schema.ComponentHook{Name: name, Commands: []string{"plan"}, OnErrors: []string{".*"}, Execute: []string{"tool", name}} +} + +func TestRunCancellationDuringActionIsReported(t *testing.T) { + failure := errors.New("exit status 1") + for _, test := range []struct { + name string + actionErr error + wantFailure bool + }{ + {"successful action", nil, false}, + {"failed action", failure, true}, + } { + t.Run(test.name, func(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + r := &recorder{} + config := &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{hook("b", "plan")}, + AfterHooks: []schema.ComponentHook{{Name: "a", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, + ErrorHooks: []schema.ComponentHook{errorHook("e")}, + } + err := Run(ctx, config, "plan", r, func() (string, error) { + r.calls = append(r.calls, "action") + cancel() + return "", test.actionErr + }) + require.ErrorIs(t, err, context.Canceled) + require.Equal(t, test.wantFailure, errors.Is(err, failure), "the action's own failure must stay visible") + require.Equal(t, []string{"before_hook:b", "action"}, r.calls, "no later hook may run after cancellation") + }) + } +} + +func TestRunCancellationDuringAfterHookSkipsErrorHooks(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + failure := errors.New("exit status 2") + r := &recorder{failures: map[string]error{"action": failure}, onCall: func(name string) { + if name == "after_hook:a" { + cancel() + } + }} + config := &schema.ComponentExecution{ + AfterHooks: []schema.ComponentHook{{Name: "a", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, + ErrorHooks: []schema.ComponentHook{errorHook("e")}, + } + err := Run(ctx, config, "plan", r, r.action) + require.ErrorIs(t, err, context.Canceled) + require.ErrorIs(t, err, failure) + require.Equal(t, []string{"action", "after_hook:a"}, r.calls) +} + +func TestRunCancellationBetweenErrorHooksStopsRemainingHooks(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + failure := errors.New("exit status 3") + r := &recorder{failures: map[string]error{"action": failure}, onCall: func(name string) { + if name == "error_hook:e1" { + cancel() + } + }} + config := &schema.ComponentExecution{ErrorHooks: []schema.ComponentHook{errorHook("e1"), errorHook("e2")}} + err := Run(ctx, config, "plan", r, r.action) + require.ErrorIs(t, err, context.Canceled) + require.ErrorIs(t, err, failure) + require.Equal(t, []string{"action", "error_hook:e1"}, r.calls, "the second error hook must not start once canceled") +} + +func TestJoinFailuresKeepsLoneErrorIntact(t *testing.T) { + lone := errors.New("only") + require.Same(t, lone, joinFailures([]failure{{err: lone}}, nil)) + require.Same(t, lone, joinFailures([]failure{{err: lone}}, lone), "an already-recorded cause is not duplicated") + require.NoError(t, joinFailures(nil, nil)) + other := errors.New("other") + joined := joinFailures([]failure{{err: lone}}, other) + require.ErrorIs(t, joined, lone) + require.ErrorIs(t, joined, other) +} diff --git a/pkg/component/execution/lifecycle_test.go b/pkg/component/execution/lifecycle_test.go new file mode 100644 index 00000000000..e6e10660041 --- /dev/null +++ b/pkg/component/execution/lifecycle_test.go @@ -0,0 +1,262 @@ +package execution + +import ( + "context" + "errors" + "strings" + "testing" + + "github.com/stretchr/testify/require" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/schema" +) + +// recorder implements Runner and records every hook and the action in call order. +type recorder struct { + calls []string + // failures maps a hook name (or "action") to the error it returns. + failures map[string]error + // output maps a hook name (or "action") to the output it produces. + output map[string]string + // onCall runs on every call, for cancellation tests. + onCall func(name string) +} + +func (r *recorder) RunHook(_ context.Context, kind HookKind, hook *schema.ComponentHook) (string, error) { + name := string(kind) + ":" + hook.Name + r.calls = append(r.calls, name) + if r.onCall != nil { + r.onCall(name) + } + return r.output[hook.Name], r.failures[hook.Name] +} + +func (r *recorder) action() (string, error) { + r.calls = append(r.calls, "action") + if r.onCall != nil { + r.onCall("action") + } + return r.output["action"], r.failures["action"] +} + +func hook(name string, commands ...string) schema.ComponentHook { + return schema.ComponentHook{Name: name, Commands: commands, Execute: []string{"tool", name}} +} + +func runLifecycle(t *testing.T, config *schema.ComponentExecution, command string, r *recorder) error { + t.Helper() + return Run(t.Context(), config, command, r, r.action) +} + +func TestRunOrdersBeforeActionAfter(t *testing.T) { + r := &recorder{} + config := &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{hook("b1", "plan"), hook("b2", "plan"), hook("skip", "apply")}, + AfterHooks: []schema.ComponentHook{hook("a1", "plan"), hook("a2", "plan")}, + ErrorHooks: []schema.ComponentHook{{Name: "e1", Commands: []string{"plan"}, OnErrors: []string{".*"}, Execute: []string{"tool"}}}, + } + require.NoError(t, runLifecycle(t, config, "plan", r)) + require.Equal(t, []string{"before_hook:b1", "before_hook:b2", "action", "after_hook:a1", "after_hook:a2"}, r.calls) +} + +func TestRunWithoutExecutionJustRunsAction(t *testing.T) { + for _, config := range []*schema.ComponentExecution{nil, {}} { + r := &recorder{} + require.NoError(t, runLifecycle(t, config, "plan", r)) + require.Equal(t, []string{"action"}, r.calls) + } +} + +func TestRunDoesNotFireHooksForOtherCommands(t *testing.T) { + r := &recorder{failures: map[string]error{"action": errors.New("boom")}} + config := &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{hook("b", "init")}, + AfterHooks: []schema.ComponentHook{{Name: "a", Commands: []string{"init"}, Execute: []string{"tool"}, RunOnError: true}}, + ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"init"}, OnErrors: []string{".*"}, Execute: []string{"tool"}}}, + } + err := runLifecycle(t, config, "plan", r) + require.ErrorContains(t, err, "boom") + require.Equal(t, []string{"action"}, r.calls) +} + +func TestRunBeforeHookFailureAbortsActionAndKeepsExitCode(t *testing.T) { + r := &recorder{failures: map[string]error{"b1": errUtils.ExitCodeError{Code: 23}}} + config := &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{hook("b1", "apply"), hook("b2", "apply")}, + AfterHooks: []schema.ComponentHook{hook("a", "apply")}, + } + err := runLifecycle(t, config, "apply", r) + require.ErrorIs(t, err, errUtils.ErrComponentHookFailed) + var exit errUtils.ExitCodeError + require.ErrorAs(t, err, &exit) + require.Equal(t, 23, exit.Code) + require.Equal(t, []string{"before_hook:b1"}, r.calls) +} + +func TestRunRunOnErrorHooksStillRunAfterBeforeHookFailure(t *testing.T) { + r := &recorder{failures: map[string]error{"b1": errors.New("first")}} + always := hook("b2", "plan") + always.RunOnError = true + config := &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{hook("b1", "plan"), hook("b-skipped", "plan"), always}, + AfterHooks: []schema.ComponentHook{hook("a-skipped", "plan"), {Name: "a-always", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, + } + err := runLifecycle(t, config, "plan", r) + require.Error(t, err) + require.Equal(t, []string{"before_hook:b1", "before_hook:b2", "after_hook:a-always"}, r.calls) +} + +func TestRunAfterHooksFollowRunOnError(t *testing.T) { + for _, test := range []struct { + name string + runOnError bool + want []string + }{ + {"skipped after failure", false, []string{"action"}}, + {"runs after failure when requested", true, []string{"action", "after_hook:a"}}, + } { + t.Run(test.name, func(t *testing.T) { + r := &recorder{failures: map[string]error{"action": errors.New("boom")}} + after := hook("a", "plan") + after.RunOnError = test.runOnError + err := runLifecycle(t, &schema.ComponentExecution{AfterHooks: []schema.ComponentHook{after}}, "plan", r) + require.ErrorContains(t, err, "boom") + require.Equal(t, test.want, r.calls) + }) + } +} + +func TestRunFailedAfterHookSuppressesLaterOrdinaryAfterHooks(t *testing.T) { + r := &recorder{failures: map[string]error{"a1": errors.New("after failed")}} + config := &schema.ComponentExecution{AfterHooks: []schema.ComponentHook{hook("a1", "plan"), hook("a2", "plan")}} + err := runLifecycle(t, config, "plan", r) + require.ErrorIs(t, err, errUtils.ErrComponentHookFailed) + require.Equal(t, []string{"action", "after_hook:a1"}, r.calls) +} + +func TestRunErrorHooks(t *testing.T) { + for _, test := range []struct { + name string + command string + patterns []string + failures map[string]error + output map[string]string + withBeforeOn bool + want []string + }{ + {name: "matches action error text", command: "apply", patterns: []string{"timeout"}, failures: map[string]error{"action": errors.New("request timeout")}, want: []string{"action", "error_hook:e"}}, + {name: "matches action output", command: "apply", patterns: []string{"AccessDenied"}, failures: map[string]error{"action": errors.New("exit status 1")}, output: map[string]string{"action": "Error: AccessDenied"}, want: []string{"action", "error_hook:e"}}, + {name: "any of several patterns", command: "apply", patterns: []string{"nope", "^exit"}, failures: map[string]error{"action": errors.New("exit status 1")}, want: []string{"action", "error_hook:e"}}, + {name: "no match", command: "apply", patterns: []string{"timeout"}, failures: map[string]error{"action": errors.New("other")}, want: []string{"action"}}, + {name: "no failure", command: "apply", patterns: []string{".*"}, want: []string{"action"}}, + {name: "other command", command: "plan", patterns: []string{".*"}, failures: map[string]error{"action": errors.New("boom")}, want: []string{"action"}}, + {name: "before hook failure", command: "apply", patterns: []string{"setup broke"}, failures: map[string]error{"b": errors.New("setup broke")}, withBeforeOn: true, want: []string{"before_hook:b", "error_hook:e"}}, + {name: "no patterns never fires", command: "apply", failures: map[string]error{"action": errors.New("boom")}, want: []string{"action"}}, + } { + t.Run(test.name, func(t *testing.T) { + r := &recorder{failures: test.failures, output: test.output} + config := &schema.ComponentExecution{ + ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"apply"}, OnErrors: test.patterns, Execute: []string{"tool"}}}, + } + if test.withBeforeOn { + config.BeforeHooks = []schema.ComponentHook{hook("b", "apply")} + } + _ = runLifecycle(t, config, test.command, r) + require.Equal(t, test.want, r.calls) + }) + } +} + +func TestRunErrorHooksSeeAfterHookFailures(t *testing.T) { + r := &recorder{failures: map[string]error{"a": errors.New("cleanup exploded")}} + config := &schema.ComponentExecution{ + AfterHooks: []schema.ComponentHook{hook("a", "plan")}, + ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"plan"}, OnErrors: []string{"cleanup exploded"}, Execute: []string{"tool"}}}, + } + require.Error(t, runLifecycle(t, config, "plan", r)) + require.Equal(t, []string{"action", "after_hook:a", "error_hook:e"}, r.calls) +} + +func TestRunInvalidErrorPatternIsReportedAndOthersStillRun(t *testing.T) { + r := &recorder{failures: map[string]error{"action": errors.New("boom")}} + config := &schema.ComponentExecution{ErrorHooks: []schema.ComponentHook{ + {Name: "bad", Commands: []string{"plan"}, OnErrors: []string{"("}, Execute: []string{"tool"}}, + {Name: "good", Commands: []string{"plan"}, OnErrors: []string{"boom"}, Execute: []string{"tool"}}, + }} + err := runLifecycle(t, config, "plan", r) + require.ErrorContains(t, err, `error hook "bad"`) + require.Equal(t, []string{"action", "error_hook:good"}, r.calls) +} + +func TestRunErrorHookFailureIsJoinedWithOriginalError(t *testing.T) { + r := &recorder{failures: map[string]error{"action": errors.New("boom"), "e": errUtils.ExitCodeError{Code: 9}}} + config := &schema.ComponentExecution{ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"plan"}, OnErrors: []string{"boom"}, Execute: []string{"tool"}}}} + err := runLifecycle(t, config, "plan", r) + require.ErrorContains(t, err, "boom") + require.ErrorIs(t, err, errUtils.ErrComponentHookFailed) +} + +func TestRunPassesFailedOutputToErrorHooksOnlyThroughMatching(t *testing.T) { + r := &recorder{failures: map[string]error{"b": errors.New("x")}, output: map[string]string{"b": "hook said NEEDLE"}} + config := &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{hook("b", "plan")}, + ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"plan"}, OnErrors: []string{"NEEDLE"}, Execute: []string{"tool"}}}, + } + _ = runLifecycle(t, config, "plan", r) + require.Equal(t, []string{"before_hook:b", "error_hook:e"}, r.calls) +} + +func TestRunStopsWhenCanceled(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + r := &recorder{onCall: func(name string) { + if name == "before_hook:b1" { + cancel() + } + }} + config := &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{hook("b1", "plan"), hook("b2", "plan")}, + AfterHooks: []schema.ComponentHook{{Name: "a", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, + ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"plan"}, OnErrors: []string{".*"}, Execute: []string{"tool"}}}, + } + err := Run(ctx, config, "plan", r, r.action) + require.ErrorIs(t, err, context.Canceled) + require.Equal(t, []string{"before_hook:b1"}, r.calls) +} + +func TestRunDoesNotStartWhenAlreadyCanceled(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + cancel() + r := &recorder{} + err := Run(ctx, &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{hook("b", "plan")}}, "plan", r, r.action) + require.ErrorIs(t, err, context.Canceled) + require.Empty(t, r.calls) +} + +func TestRunErrorMessageNamesTheHook(t *testing.T) { + r := &recorder{failures: map[string]error{"lint": errors.New("exit status 3")}} + err := runLifecycle(t, &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{hook("lint", "plan")}}, "plan", r) + require.Error(t, err) + require.True(t, strings.Contains(err.Error(), "lint")) + require.True(t, strings.Contains(err.Error(), "before_hook")) +} + +func TestValidate(t *testing.T) { + require.NoError(t, Validate(nil)) + require.NoError(t, Validate(&schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{hook("ok", "plan")}, + ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"plan"}, OnErrors: []string{"^x$"}, Execute: []string{"tool"}}}, + })) + for _, test := range []struct { + name string + config *schema.ComponentExecution + }{ + {"empty execute", &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{{Name: "h", Commands: []string{"plan"}}}}}, + {"blank executable", &schema.ComponentExecution{AfterHooks: []schema.ComponentHook{{Name: "h", Commands: []string{"plan"}, Execute: []string{""}}}}}, + {"bad pattern", &schema.ComponentExecution{ErrorHooks: []schema.ComponentHook{{Name: "h", Commands: []string{"plan"}, Execute: []string{"t"}, OnErrors: []string{"("}}}}}, + } { + t.Run(test.name, func(t *testing.T) { + require.ErrorIs(t, Validate(test.config), errUtils.ErrPluginProtocol) + }) + } +} diff --git a/pkg/component/terraform/configuration.go b/pkg/component/terraform/configuration.go index a98f7ef4082..05f1d6733eb 100644 --- a/pkg/component/terraform/configuration.go +++ b/pkg/component/terraform/configuration.go @@ -8,10 +8,14 @@ import ( "maps" "os/exec" "path/filepath" + "strings" errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/component" + cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/dependencies" "github.com/cloudposse/atmos/pkg/schema" + u "github.com/cloudposse/atmos/pkg/utils" ) func prepareConfiguration(ctx context.Context, config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (*schema.PreparedComponentConfiguration, error) { @@ -38,11 +42,20 @@ func prepareConfiguration(ctx context.Context, config *schema.AtmosConfiguration return nil, fmt.Errorf("%w: %w", errUtils.ErrPluginInvalidConfig, err) } info.Command = binary - prepared, err := config.ComponentConfigurations.Prepare(ctx, info) + var prepared *schema.PreparedComponentConfiguration + if usesConfigurationPlan(info) { + if err = useRetainedWorkdir(config, info); err != nil { + return nil, err + } + prepared, err = restoreConfigurationPlan(ctx, config, info) + } else { + prepared, err = config.ComponentConfigurations.Prepare(ctx, info) + } if err != nil || prepared == nil { return prepared, err } info.ComponentVarsSection = prepared.Inputs + info.ComponentExecution = prepared.Execution info.ComponentBackendType, info.ComponentBackendSection = prepared.BackendType, prepared.Backend info.TerraformWorkspace = prepared.Workspace info.ComponentSection["vars"] = prepared.Inputs @@ -52,13 +65,34 @@ func prepareConfiguration(ctx context.Context, config *schema.AtmosConfiguration source = make(map[string]any) } source["uri"] = prepared.Source + if isLocalModuleSource(prepared.Source) { + // A local module changes without its location changing, so the retained workdir + // would keep the code of the first run. Refreshing is a cheap local copy. + source["ttl"] = "0s" + } info.ComponentSection["source"] = source info.ComponentSection["backend_type"] = prepared.BackendType info.ComponentSection["backend"] = prepared.Backend - config.Components.Terraform.AutoGenerateBackendFile = true + config.Components.Terraform.AutoGenerateBackendFile = !prepared.BackendGenerated return prepared, nil } +// isLocalModuleSource reports whether the evaluated module source is a path on this machine. +func isLocalModuleSource(uri string) bool { + return strings.HasPrefix(uri, ".") || filepath.IsAbs(uri) +} + +// useRetainedWorkdir points the invocation at the directory the reviewed plan was created in. +// A new process has not provisioned anything, so the path is derived the way provisioning does. +func useRetainedWorkdir(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { + fallback, err := u.GetComponentPath(config, cfg.TerraformComponentType, info.ComponentFolderPrefix, info.FinalComponent) + if err != nil { + return fmt.Errorf("failed to resolve component path: %w", err) + } + _, err = component.ResolveRetainedWorkdir(config, info, cfg.TerraformComponentType, fallback) + return err +} + func configurationArtifactName(info *schema.ConfigAndStacksInfo, suffix string) (string, bool) { if _, owned := info.ComponentSettingsSection["configuration"]; !owned { return "", false diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go new file mode 100644 index 00000000000..00d42aba7cd --- /dev/null +++ b/pkg/component/terraform/configuration_plan.go @@ -0,0 +1,317 @@ +package terraform + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "io" + "io/fs" + "os" + "path/filepath" + "reflect" + "strings" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/schema" +) + +const configurationPlanSuffix = ".atmos.json" + +type configurationPlan struct { + Version int + Stack, Component, Identity, EngineDigest, PlanDigest, WorkingDirectory, WorkingDigest string + Prepared *schema.PreparedComponentConfiguration +} + +func usesConfigurationPlan(info *schema.ConfigAndStacksInfo) bool { + return info.UseTerraformPlan || info.PlanFile != "" || info.StoredPlanFile != "" +} + +func planRuntime(config *schema.AtmosConfiguration) (schema.ComponentConfigurationPlanRuntime, error) { + runtime, ok := config.ComponentConfigurations.(schema.ComponentConfigurationPlanRuntime) + if !ok { + return nil, fmt.Errorf("%w: provider cannot bind saved plans", errUtils.ErrPluginUnsupportedExtension) + } + return runtime, nil +} + +func configurationPlanPath(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) string { + if info.PlanFile != "" { + if filepath.IsAbs(info.PlanFile) { + return info.PlanFile + } + return filepath.Join(ConstructTerraformComponentWorkingDir(config, info), info.PlanFile) + } + return ConstructTerraformComponentPlanfilePath(config, info) +} + +// restoreConfigurationPlan verifies the reviewed artifact before returning its original preparation. +// The original execution directory is retained; transport to another runner requires an artifact adapter. +func restoreConfigurationPlan(ctx context.Context, config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (*schema.PreparedComponentConfiguration, error) { + if !restoresConfigurationPlan(info) { + return nil, fmt.Errorf("%w: plugin saved plans require direct apply of the original artifact", errUtils.ErrPluginUnsupportedExtension) + } + runtime, err := planRuntime(config) + if err != nil { + return nil, err + } + path := configurationPlanPath(config, info) + plan, err := loadConfigurationPlan(path, info) + if err != nil { + return nil, err + } + identity, err := runtime.PlanIdentity(ctx, info, plan.Prepared.Sources) + if err != nil { + return nil, err + } + artifactMatches, err := configurationArtifactMatches(config, info, plan, path) + if err != nil { + return nil, err + } + if identity != plan.Identity || !artifactMatches { + return nil, fmt.Errorf("%w: plugin, source, engine, workdir, or reviewed artifact changed; create a new plan", errUtils.ErrPlanVerificationFailed) + } + if err := verifyReviewedBackendAndWorkdir(ctx, config, info, plan, path); err != nil { + return nil, err + } + // Terraform runs inside the workdir, so a path relative to the project would not resolve there. + absolute, err := filepath.Abs(path) + if err != nil { + return nil, err + } + info.UseTerraformPlan = true + info.PlanFile = absolute + return plan.Prepared, nil +} + +// verifyReviewedBackendAndWorkdir rejects a changed backend identity or a changed retained working directory. +func verifyReviewedBackendAndWorkdir(ctx context.Context, config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, plan *configurationPlan, path string) error { + if err := verifyConfigurationPlanState(ctx, config, info, plan); err != nil { + return err + } + return verifyConfigurationPlanWorkdir(plan, path) +} + +// restoresConfigurationPlan reports whether the invocation applies the original, locally stored artifact. +func restoresConfigurationPlan(info *schema.ConfigAndStacksInfo) bool { + return info.StoredPlanFile == "" && (info.SubCommand == "apply" || info.SubCommand == "deploy") +} + +// loadConfigurationPlan reads the companion binding and checks it targets this component. +func loadConfigurationPlan(path string, info *schema.ConfigAndStacksInfo) (*configurationPlan, error) { + data, err := os.ReadFile(path + configurationPlanSuffix) + if err != nil { + return nil, fmt.Errorf("%w: plugin plan binding is missing: %w", errUtils.ErrPlanVerificationFailed, err) + } + var plan configurationPlan + decoder := json.NewDecoder(strings.NewReader(string(data))) + decoder.UseNumber() + decoder.DisallowUnknownFields() + if err := decoder.Decode(&plan); err != nil { + return nil, errUtils.ErrPlanVerificationFailed + } + if plan.Version != 1 || plan.Prepared == nil || plan.Stack != info.Stack || plan.Component != info.ComponentFromArg { + return nil, errUtils.ErrPlanVerificationFailed + } + return &plan, nil +} + +// configurationArtifactMatches reports whether the plan file, engine binary, and working directory path are unchanged. +func configurationArtifactMatches(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, plan *configurationPlan, path string) (bool, error) { + digest, err := fileDigest(path) + if err != nil { + return false, err + } + engine, err := fileDigest(info.Command) + if err != nil { + return false, err + } + workdir := filepath.Clean(ConstructTerraformComponentWorkingDir(config, info)) + return digest == plan.PlanDigest && engine == plan.EngineDigest && workdir == plan.WorkingDirectory, nil +} + +// verifyConfigurationPlanWorkdir rejects a working directory whose retained bytes changed since review. +func verifyConfigurationPlanWorkdir(plan *configurationPlan, path string) error { + workingDigest, err := configurationWorkdirDigest(plan.WorkingDirectory, path) + if err != nil { + return err + } + if workingDigest != plan.WorkingDigest { + return fmt.Errorf("%w: reviewed working directory changed", errUtils.ErrPlanVerificationFailed) + } + return nil +} + +// recordConfigurationPlan stores a private companion beside the completed plan. +func recordConfigurationPlan(ctx context.Context, config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, execCtx *componentExecContext, args []string) error { + path := recordedPlanPath(info, execCtx, args) + if path == "" { + return nil + } + runtime, err := planRuntime(config) + if err != nil { + return err + } + identity, err := runtime.PlanIdentity(ctx, info, execCtx.preparedConfiguration.Sources) + if err != nil { + return err + } + plan, err := newConfigurationPlan(info, execCtx, identity, path) + if err != nil { + return err + } + data, err := json.Marshal(plan) + if err != nil { + return err + } + return writePlanBinding(path+configurationPlanSuffix, data) +} + +// recordedPlanPath returns the plan file to bind, or empty when this invocation produces none. +func recordedPlanPath(info *schema.ConfigAndStacksInfo, execCtx *componentExecContext, args []string) string { + if execCtx.preparedConfiguration == nil || info.DryRun || info.SubCommand != "plan" { + return "" + } + return plannedOutputPath(execCtx.componentPath, args) +} + +// newConfigurationPlan binds the identity to the digests of the plan, engine, and working directory. +func newConfigurationPlan(info *schema.ConfigAndStacksInfo, execCtx *componentExecContext, identity, path string) (*configurationPlan, error) { + planDigest, err := fileDigest(path) + if err != nil { + return nil, err + } + engineDigest, err := fileDigest(info.Command) + if err != nil { + return nil, err + } + workingDigest, err := configurationWorkdirDigest(execCtx.workingDir, path) + if err != nil { + return nil, err + } + return &configurationPlan{WorkingDigest: workingDigest, Version: 1, Stack: info.Stack, Component: info.ComponentFromArg, Identity: identity, EngineDigest: engineDigest, PlanDigest: planDigest, WorkingDirectory: filepath.Clean(execCtx.workingDir), Prepared: execCtx.preparedConfiguration}, nil +} + +func plannedOutputPath(directory string, args []string) string { + for i, arg := range args { + path := "" + if arg == "-out" && i+1 < len(args) { + path = args[i+1] + } + if strings.HasPrefix(arg, "-out=") { + path = strings.TrimPrefix(arg, "-out=") + } + if path != "" { + if filepath.IsAbs(path) { + return path + } + return filepath.Join(directory, path) + } + } + return "" +} + +func fileDigest(path string) (string, error) { + file, err := os.Open(path) + if err != nil { + return "", err + } + defer file.Close() + h := sha256.New() + if _, err := io.Copy(h, file); err != nil { + return "", err + } + return hex.EncodeToString(h.Sum(nil)), nil +} + +func writePlanBinding(path string, data []byte) error { + file, err := os.CreateTemp(filepath.Dir(path), ".atmos-plan-*") + if err != nil { + return err + } + defer os.Remove(file.Name()) + if _, err := file.Write(data); err != nil { + file.Close() + return err + } + if err := file.Close(); err != nil { + return err + } + return os.Rename(file.Name(), path) +} + +// verifyConfigurationPlanState rejects a changed backend without reading dependency outputs. +func verifyConfigurationPlanState(ctx context.Context, config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, plan *configurationPlan) error { + runtime, ok := config.ComponentConfigurations.(schema.ComponentConfigurationStateRuntime) + if !ok { + return errUtils.ErrPluginUnsupportedExtension + } + state, err := runtime.ResolveState(ctx, info) + if err != nil { + return err + } + if state == nil || state.BackendType != plan.Prepared.BackendType || state.Workspace != plan.Prepared.Workspace || !reflect.DeepEqual(state.Backend, plan.Prepared.Backend) { + return fmt.Errorf("%w: reviewed backend identity changed", errUtils.ErrPlanVerificationFailed) + } + return nil +} + +// configurationWorkdirDigest binds retained module/configuration bytes, excluding mutable execution data. +func configurationWorkdirDigest(root, planPath string) (string, error) { + files := make(map[string]string) + err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error { + if err != nil { + return err + } + if entry.IsDir() { + if skipWorkdirDirectory(root, path, entry.Name()) { + return filepath.SkipDir + } + return nil + } + if ignoredWorkdirFile(path, planPath, entry.Name()) { + return nil + } + return recordWorkdirFile(root, path, entry, files) + }) + if err != nil { + return "", err + } + data, err := json.Marshal(files) + if err != nil { + return "", err + } + digest := sha256.Sum256(data) + return hex.EncodeToString(digest[:]), nil +} + +// skipWorkdirDirectory reports whether a nested directory holds only mutable or cached data. +// The .atmos directory is provisioner bookkeeping (access timestamps and metadata lock files +// that even a read-only lookup of the retained directory creates), never executed content. +func skipWorkdirDirectory(root, path, name string) bool { + return path != root && (name == ".terraform" || name == ".git" || name == ".terragrunt-cache" || name == ".atmos") +} + +// ignoredWorkdirFile reports whether a file is the plan itself, its binding, or mutable state. +func ignoredWorkdirFile(path, planPath, name string) bool { + return path == planPath || strings.HasSuffix(name, configurationPlanSuffix) || strings.HasPrefix(name, ".atmos-plan-") || strings.HasPrefix(name, "terraform.tfstate") +} + +// recordWorkdirFile digests one retained file by its slash-separated path relative to the root. +func recordWorkdirFile(root, path string, entry fs.DirEntry, files map[string]string) error { + if entry.Type()&os.ModeSymlink != 0 { + return fmt.Errorf("%w: saved-plan workdir contains symlink", errUtils.ErrPluginUnsupportedExtension) + } + digest, err := fileDigest(path) + if err != nil { + return err + } + relative, err := filepath.Rel(root, path) + if err != nil { + return err + } + files[filepath.ToSlash(relative)] = digest + return nil +} diff --git a/pkg/component/terraform/configuration_plan_failures_test.go b/pkg/component/terraform/configuration_plan_failures_test.go new file mode 100644 index 00000000000..4bc598a8fff --- /dev/null +++ b/pkg/component/terraform/configuration_plan_failures_test.go @@ -0,0 +1,183 @@ +package terraform + +import ( + "context" + "errors" + "os" + "path/filepath" + "runtime" + "testing" + + "github.com/stretchr/testify/require" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/schema" +) + +// identityOnlyRuntime can bind plans but cannot resolve backend state. +type identityOnlyRuntime struct { + schema.ComponentConfigurationRuntime +} + +func (identityOnlyRuntime) PlanIdentity(context.Context, *schema.ConfigAndStacksInfo, []schema.ComponentConfigurationSource) (string, error) { + return "provider-source-options", nil +} + +// stateFailureRuntime accepts the plan identity but fails to resolve the backend. +type stateFailureRuntime struct { + *planRuntimeFixture + stateErr error +} + +func (r stateFailureRuntime) ResolveState(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return nil, r.stateErr +} + +// recordedPlan records a plan companion and prepares the invocation for a direct apply. +func recordedPlan(t *testing.T) (*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, *componentExecContext, *planRuntimeFixture, string) { + t.Helper() + config, info, execCtx, runtime, path := planFixture(t) + require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"plan", "-out=" + path})) + info.SubCommand = "apply" + info.UseTerraformPlan = true + info.PlanFile = path + return config, info, execCtx, runtime, path +} + +func TestRestoreConfigurationPlanRequiresPlanBindingRuntime(t *testing.T) { + config, info, _, _, _ := recordedPlan(t) + config.ComponentConfigurations = nil + prepared, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + require.Nil(t, prepared) +} + +func TestRestoreConfigurationPlanRequiresStateRuntime(t *testing.T) { + config, info, _, _, _ := recordedPlan(t) + config.ComponentConfigurations = identityOnlyRuntime{} + prepared, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension, "a provider that cannot prove the backend is unchanged must not apply a saved plan") + require.Nil(t, prepared) +} + +func TestRestoreConfigurationPlanBackendResolution(t *testing.T) { + failure := errors.New("backend unreachable") + t.Run("resolution error is propagated", func(t *testing.T) { + config, info, _, runtime, _ := recordedPlan(t) + config.ComponentConfigurations = stateFailureRuntime{planRuntimeFixture: runtime, stateErr: failure} + _, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, failure) + require.NotErrorIs(t, err, errUtils.ErrPlanVerificationFailed, "an unreachable backend is not a verification mismatch") + }) + t.Run("missing state is a verification failure", func(t *testing.T) { + config, info, _, runtime, _ := recordedPlan(t) + runtime.state = nil + _, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + }) + t.Run("backend type change is a verification failure", func(t *testing.T) { + config, info, _, runtime, _ := recordedPlan(t) + runtime.state.BackendType = "s3" + _, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + }) +} + +func TestRestoreConfigurationPlanReportsUnreadableArtifacts(t *testing.T) { + t.Run("missing engine binary", func(t *testing.T) { + config, info, _, _, _ := recordedPlan(t) + require.NoError(t, os.Remove(info.Command)) + prepared, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, os.ErrNotExist) + require.Nil(t, prepared) + }) + t.Run("symlink added to the reviewed workdir", func(t *testing.T) { + config, info, execCtx, _, _ := recordedPlan(t) + if err := os.Symlink(filepath.Join(execCtx.workingDir, "main.tf"), filepath.Join(execCtx.workingDir, "alias.tf")); err != nil { + t.Skipf("symlinks unavailable: %v", err) + } + prepared, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + require.Nil(t, prepared) + }) + t.Run("unchanged inputs are accepted", func(t *testing.T) { + config, info, execCtx, _, path := recordedPlan(t) + prepared, err := restoreConfigurationPlan(t.Context(), config, info) + require.NoError(t, err) + require.Equal(t, execCtx.preparedConfiguration.Backend, prepared.Backend) + require.Equal(t, path, info.PlanFile) + }) +} + +func TestRecordConfigurationPlanFailsWhenWorkdirCannotBeDigested(t *testing.T) { + config, info, execCtx, _, path := planFixture(t) + execCtx.workingDir = filepath.Join(t.TempDir(), "absent") + err := recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"plan", "-out=" + path}) + require.ErrorIs(t, err, os.ErrNotExist) + require.NoFileExists(t, path+configurationPlanSuffix, "no binding may be written for an unverifiable workdir") +} + +func TestRecordConfigurationPlanReplacesBindingAtomically(t *testing.T) { + config, info, execCtx, runtime, path := planFixture(t) + args := []string{"plan", "-out=" + path} + require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, args)) + first, err := os.ReadFile(path + configurationPlanSuffix) + require.NoError(t, err) + runtime.identity = "changed provider" + require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, args)) + second, err := os.ReadFile(path + configurationPlanSuffix) + require.NoError(t, err) + require.NotEqual(t, first, second, "re-planning must replace the earlier binding") + require.Contains(t, string(second), "changed provider") + entries, err := os.ReadDir(filepath.Dir(path)) + require.NoError(t, err) + for _, entry := range entries { + require.NotContains(t, entry.Name(), ".atmos-plan-", "temporary binding files are cleaned up") + } +} + +func TestConfigurationWorkdirDigestSkipsOnlyNestedMutableDirectories(t *testing.T) { + root := t.TempDir() + plan := filepath.Join(root, "reviewed.plan") + write := func(name string) { + t.Helper() + path := filepath.Join(root, filepath.FromSlash(name)) + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700)) + require.NoError(t, os.WriteFile(path, []byte(name), 0o600)) + } + write("main.tf") + baseline, err := configurationWorkdirDigest(root, plan) + require.NoError(t, err) + + for _, ignored := range []string{".git/HEAD", ".terragrunt-cache/x/main.tf", ".terraform/modules.json", "sub/.terraform/x", ".atmos/metadata.json", ".atmos/metadata.json.lock", "sub/.atmos/source-files.json", ".atmos-plan-temp", "terraform.tfstate.backup", "other.planfile.atmos.json"} { + write(ignored) + } + same, err := configurationWorkdirDigest(root, plan) + require.NoError(t, err) + require.Equal(t, baseline, same, "mutable data does not alter the retained-bytes digest") + + for _, retained := range []string{"modules/child/main.tf", "terragrunt.hcl", ".terraform.lock.hcl"} { + write(retained) + changed, err := configurationWorkdirDigest(root, plan) + require.NoError(t, err) + require.NotEqual(t, baseline, changed, retained) + baseline = changed + } +} + +func TestConfigurationWorkdirDigestFailsForUnreadableFile(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("file permissions are not enforced on Windows") + } + root := t.TempDir() + locked := filepath.Join(root, "locked.tf") + require.NoError(t, os.WriteFile(locked, []byte("secret"), 0o600)) + require.NoError(t, os.Chmod(locked, 0)) + t.Cleanup(func() { _ = os.Chmod(locked, 0o600) }) + if file, err := os.Open(locked); err == nil { + _ = file.Close() + t.Skip("permissions are not enforced for this user") + } + _, err := configurationWorkdirDigest(root, filepath.Join(root, "reviewed.plan")) + require.ErrorIs(t, err, os.ErrPermission) +} diff --git a/pkg/component/terraform/configuration_plan_pipeline_test.go b/pkg/component/terraform/configuration_plan_pipeline_test.go new file mode 100644 index 00000000000..7308ed66952 --- /dev/null +++ b/pkg/component/terraform/configuration_plan_pipeline_test.go @@ -0,0 +1,174 @@ +package terraform + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/provisioner" + "github.com/cloudposse/atmos/pkg/schema" +) + +// reviewedPlanRuntime is a configuration provider that can prepare, bind plans, and resolve state. +type reviewedPlanRuntime struct { + *MockComponentConfigurationRuntime + state *schema.ComponentConfigurationState +} + +func (reviewedPlanRuntime) PlanIdentity(context.Context, *schema.ConfigAndStacksInfo, []schema.ComponentConfigurationSource) (string, error) { + return "provider-source-options", nil +} + +func (r reviewedPlanRuntime) ResolveState(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return r.state, nil +} + +// reviewedPlanProject is an isolated workdir-enabled component whose module can change after review. +type reviewedPlanProject struct { + root, module, workdir, state string + config *schema.AtmosConfiguration + provider *MockComponentConfigurationRuntime +} + +func newReviewedPlanProject(t *testing.T) *reviewedPlanProject { + t.Helper() + root := t.TempDir() + project := &reviewedPlanProject{root: root, module: filepath.Join(root, "module"), workdir: filepath.Join(root, "chosen"), state: filepath.Join(root, "state", "terraform.tfstate")} + require.NoError(t, os.MkdirAll(project.module, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(project.module, "main.tf"), []byte("# reviewed module"), 0o644)) + project.config = &schema.AtmosConfiguration{BasePath: root, BasePathAbsolute: root, TerraformDirAbsolutePath: filepath.Join(root, "components")} + project.config.Components.Terraform.BasePath = "components" + project.provider = NewMockComponentConfigurationRuntime(gomock.NewController(t)) + state := &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{"path": project.state}, Workspace: "default"} + project.config.ComponentConfigurations = reviewedPlanRuntime{MockComponentConfigurationRuntime: project.provider, state: state} + return project +} + +func (p *reviewedPlanProject) info(t *testing.T, subCommand string) *schema.ConfigAndStacksInfo { + t.Helper() + binary, err := os.Executable() + require.NoError(t, err) + return &schema.ConfigAndStacksInfo{ + Stack: "dev", ComponentFromArg: "app", Component: "app", FinalComponent: "app", Command: binary, SubCommand: subCommand, + ComponentSettingsSection: map[string]any{"configuration": true}, ComponentEnvSection: map[string]any{}, + ComponentSection: map[string]any{ + "component": "app", "atmos_component": "app", "atmos_stack": "dev", "source": map[string]any{"uri": p.module}, + "provision": map[string]any{"workdir": map[string]any{"enabled": true}}, + "metadata": map[string]any{"working_directory": p.workdir}, + }, + } +} + +func (p *reviewedPlanProject) prepared() *schema.PreparedComponentConfiguration { + return &schema.PreparedComponentConfiguration{Engine: "terraform", Source: p.module, Inputs: map[string]any{"value": "reviewed"}, BackendType: "local", Backend: map[string]any{"path": p.state}, Workspace: "default"} +} + +// review runs plan and records its binding, returning the plan file path. +func (p *reviewedPlanProject) review(t *testing.T) string { + t.Helper() + info := p.info(t, "plan") + p.provider.EXPECT().Prepare(gomock.Any(), info).Return(p.prepared(), nil) + execution, err := testExecutor(t).prepareComponentExecution(t.Context(), provisioner.OutputWriters{}, p.config, info, false) + require.NoError(t, err) + require.Equal(t, p.workdir, execution.workingDir) + planPath := filepath.Join(execution.workingDir, "reviewed.planfile") + require.NoError(t, os.WriteFile(planPath, []byte("reviewed plan"), 0o600)) + require.NoError(t, recordConfigurationPlan(t.Context(), p.config, info, execution, []string{"-out=" + planPath})) + return planPath +} + +// reviewedApply returns an apply invocation of the reviewed plan, as a new process would build it: +// nothing has been provisioned, so the retained workdir has to be located from configuration. +func (p *reviewedPlanProject) reviewedApply(t *testing.T, planPath string) *schema.ConfigAndStacksInfo { + t.Helper() + apply := p.info(t, "apply") + apply.UseTerraformPlan, apply.PlanFile = true, planPath + return apply +} + +func TestApplyOfReviewedPlanReusesReviewedPreparationAndWorkdir(t *testing.T) { + project := newReviewedPlanProject(t) + planPath := project.review(t) + + // Prepare is expected exactly once (for the plan); applying must restore the reviewed preparation instead. + apply := project.reviewedApply(t, planPath) + execution, err := testExecutor(t).prepareComponentExecution(t.Context(), provisioner.OutputWriters{}, project.config, apply, false) + require.NoError(t, err) + + require.Equal(t, project.workdir, execution.componentPath, "a reviewed plan runs in the retained directory") + require.Equal(t, execution.workingDir, execution.componentPath) + require.Equal(t, map[string]any{"value": "reviewed"}, execution.preparedConfiguration.Inputs) + retained, err := os.ReadFile(filepath.Join(project.workdir, "main.tf")) + require.NoError(t, err) + require.Equal(t, "# reviewed module", string(retained)) + require.Equal(t, planPath, apply.PlanFile) + require.True(t, apply.UseTerraformPlan) +} + +func TestApplyOfReviewedPlanRejectsChangedWorkdir(t *testing.T) { + project := newReviewedPlanProject(t) + planPath := project.review(t) + require.NoError(t, os.WriteFile(filepath.Join(project.workdir, "main.tf"), []byte("# edited after review"), 0o644)) + + apply := project.reviewedApply(t, planPath) + execution, err := testExecutor(t).prepareComponentExecution(t.Context(), provisioner.OutputWriters{}, project.config, apply, false) + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + require.Nil(t, execution) +} + +func TestPrepareConfigurationRestoresReviewedPlanWithoutPreparing(t *testing.T) { + project := newReviewedPlanProject(t) + planPath := project.review(t) + apply := project.reviewedApply(t, planPath) + + prepared, err := prepareConfiguration(t.Context(), project.config, apply) + require.NoError(t, err) + require.Equal(t, map[string]any{"value": "reviewed"}, apply.ComponentVarsSection) + require.Equal(t, prepared.Inputs, apply.ComponentSection["vars"]) + + // A tampered plan is rejected before any of the reviewed state is applied to the invocation. + require.NoError(t, os.WriteFile(planPath, []byte("unreviewed plan"), 0o600)) + rejected := project.reviewedApply(t, planPath) + result, err := prepareConfiguration(t.Context(), project.config, rejected) + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + require.Nil(t, result) + require.Nil(t, rejected.ComponentVarsSection) +} + +func TestPrepareInitExecutionKeepsReviewedPlanWorkspace(t *testing.T) { + for _, test := range []struct { + name string + owned bool + savedPlan bool + wantCleaned bool + wantSameRoot bool + }{ + {"provider-owned saved plan", true, true, false, true}, + {"provider-owned without a saved plan", true, false, true, true}, + {"ordinary component with a saved plan", false, true, true, true}, + } { + t.Run(test.name, func(t *testing.T) { + directory := t.TempDir() + environment := filepath.Join(directory, ".terraform", "environment") + require.NoError(t, os.MkdirAll(filepath.Dir(environment), 0o755)) + require.NoError(t, os.WriteFile(environment, []byte("workspace"), 0o644)) + info := schema.ConfigAndStacksInfo{ComponentSection: map[string]any{}, UseTerraformPlan: test.savedPlan} + if test.owned { + info.ComponentSettingsSection = map[string]any{"configuration": true} + } + result, err := prepareInitExecution(t.Context(), provisioner.OutputWriters{}, &schema.AtmosConfiguration{}, &info, directory) + require.NoError(t, err) + require.Equal(t, directory, result) + if test.wantCleaned { + require.NoFileExists(t, environment) + } else { + require.FileExists(t, environment, "the reviewed plan's workspace selection must be retained") + } + }) + } +} diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go new file mode 100644 index 00000000000..4f70192c24f --- /dev/null +++ b/pkg/component/terraform/configuration_plan_test.go @@ -0,0 +1,170 @@ +package terraform + +import ( + "context" + "errors" + "os" + "path/filepath" + "testing" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/schema" + "github.com/stretchr/testify/require" +) + +type planRuntimeFixture struct { + schema.ComponentConfigurationRuntime + identity string + state *schema.ComponentConfigurationState + err error +} + +func (p *planRuntimeFixture) PlanIdentity(context.Context, *schema.ConfigAndStacksInfo, []schema.ComponentConfigurationSource) (string, error) { + return p.identity, p.err +} + +func (p *planRuntimeFixture) ResolveState(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return p.state, p.err +} + +func planFixture(t *testing.T) (*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, *componentExecContext, *planRuntimeFixture, string) { + t.Helper() + root := t.TempDir() + config := &schema.AtmosConfiguration{BasePath: root, Components: schema.Components{Terraform: schema.Terraform{BasePath: "components"}}} + info := &schema.ConfigAndStacksInfo{Stack: "live", ComponentFromArg: "network", FinalComponent: "network", SubCommand: "plan", ComponentSettingsSection: map[string]any{"configuration": true}} + working := ConstructTerraformComponentWorkingDir(config, info) + require.NoError(t, os.MkdirAll(working, 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(working, "main.tf"), []byte("original module"), 0o600)) + binary := filepath.Join(root, "terraform.exe") + require.NoError(t, os.WriteFile(binary, []byte("selected engine"), 0o700)) + info.Command = binary + path := filepath.Join(working, "reviewed.planfile") + require.NoError(t, os.WriteFile(path, []byte("reviewed plan"), 0o600)) + prepared := &schema.PreparedComponentConfiguration{Engine: "terraform", Source: root, Inputs: map[string]any{"value": "reviewed"}, BackendType: "local", Backend: map[string]any{"path": filepath.Join(root, "state")}, Workspace: "default"} + runtime := &planRuntimeFixture{identity: "provider-source-options", state: &schema.ComponentConfigurationState{BackendType: prepared.BackendType, Backend: prepared.Backend, Workspace: prepared.Workspace}} + config.ComponentConfigurations = runtime + execCtx := &componentExecContext{componentPath: working, workingDir: working, preparedConfiguration: prepared} + return config, info, execCtx, runtime, path +} + +func TestConfigurationPlanRestoresReviewedInputs(t *testing.T) { + config, info, execCtx, _, path := planFixture(t) + err := recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"plan", "-out", filepath.Base(path)}) + require.NoError(t, err) + stat, err := os.Stat(path + configurationPlanSuffix) + require.NoError(t, err) + require.Equal(t, os.FileMode(0), stat.Mode().Perm()&0o077) + info.SubCommand = "apply" + info.UseTerraformPlan = true + info.PlanFile = path + prepared, err := restoreConfigurationPlan(t.Context(), config, info) + require.NoError(t, err) + require.Equal(t, execCtx.preparedConfiguration.Inputs, prepared.Inputs) + require.True(t, info.UseTerraformPlan) + require.Equal(t, path, info.PlanFile) + require.FileExists(t, path) +} + +func TestConfigurationPlanRejectsChangedBinding(t *testing.T) { + for _, change := range []string{"plan", "engine", "provider", "workdir", "backend", "workspace", "coordinates", "missing binding", "corrupt binding", "missing plan", "runtime error", "unsupported operation", "stored plan"} { + t.Run(change, func(t *testing.T) { + config, info, execCtx, runtime, path := planFixture(t) + require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"plan", "-out=" + path})) + info.SubCommand = "apply" + info.UseTerraformPlan = true + info.PlanFile = path + switch change { + case "plan": + require.NoError(t, os.WriteFile(path, []byte("unreviewed"), 0o600)) + case "engine": + require.NoError(t, os.WriteFile(info.Command, []byte("changed executable"), 0o700)) + case "provider": + runtime.identity = "other provider" + case "workdir": + require.NoError(t, os.WriteFile(filepath.Join(execCtx.workingDir, "main.tf"), []byte("modified"), 0o600)) + case "backend": + runtime.state.Backend = map[string]any{"path": "other"} + case "workspace": + runtime.state.Workspace = "other" + case "coordinates": + info.Stack = "other" + case "missing binding": + require.NoError(t, os.Remove(path+configurationPlanSuffix)) + case "corrupt binding": + require.NoError(t, os.WriteFile(path+configurationPlanSuffix, []byte("not json"), 0o600)) + case "missing plan": + require.NoError(t, os.Remove(path)) + case "runtime error": + runtime.err = context.Canceled + case "unsupported operation": + info.SubCommand = "destroy" + case "stored plan": + info.StoredPlanFile = path + } + prepared, err := restoreConfigurationPlan(t.Context(), config, info) + require.Error(t, err) + require.Nil(t, prepared) + }) + } +} + +func TestConfigurationPlanRecordingGuards(t *testing.T) { + config, info, execCtx, runtime, path := planFixture(t) + for _, args := range [][]string{nil, {"-out"}, {"-out="}} { + require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, args)) + } + info.DryRun = true + require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"-out", path})) + info.DryRun = false + info.SubCommand = "apply" + require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"-out", path})) + info.SubCommand = "plan" + config.ComponentConfigurations = nil + require.ErrorIs(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"-out", path}), errUtils.ErrPluginUnsupportedExtension) + config.ComponentConfigurations = runtime + runtime.err = context.Canceled + require.ErrorIs(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"-out", path}), context.Canceled) + runtime.err = nil + require.Error(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"-out", path + "missing"})) + info.Command = path + "missing" + require.Error(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"-out", path})) +} + +func TestConfigurationPlanPathsAndFileErrors(t *testing.T) { + config, info, _, _, path := planFixture(t) + require.Equal(t, ConstructTerraformComponentPlanfilePath(config, info), configurationPlanPath(config, info)) + info.PlanFile = filepath.Base(path) + require.Equal(t, path, configurationPlanPath(config, info)) + info.PlanFile = path + require.Equal(t, path, configurationPlanPath(config, info)) + require.Error(t, writePlanBinding(filepath.Join(t.TempDir(), "absent", "binding"), nil)) + _, err := fileDigest(t.TempDir()) + require.Error(t, err) + _, err = configurationWorkdirDigest(filepath.Join(t.TempDir(), "absent"), path) + require.Error(t, err) + require.True(t, usesConfigurationPlan(info)) + require.False(t, usesConfigurationPlan(&schema.ConfigAndStacksInfo{})) +} + +func TestConfigurationWorkdirDigestIgnoresMutableFiles(t *testing.T) { + root := t.TempDir() + plan := filepath.Join(root, "reviewed.plan") + first, err := configurationWorkdirDigest(root, plan) + require.NoError(t, err) + require.NoError(t, os.Mkdir(filepath.Join(root, ".terraform"), 0o700)) + for _, name := range []string{"terraform.tfstate", "reviewed.plan", "reviewed.plan.atmos.json"} { + require.NoError(t, os.WriteFile(filepath.Join(root, name), []byte("mutable"), 0o600)) + } + second, err := configurationWorkdirDigest(root, plan) + require.NoError(t, err) + require.Equal(t, first, second) + require.NoError(t, os.WriteFile(filepath.Join(root, "main.tf"), []byte("source"), 0o600)) + third, err := configurationWorkdirDigest(root, plan) + require.NoError(t, err) + require.NotEqual(t, first, third) + if err := os.Symlink(filepath.Join(root, "main.tf"), filepath.Join(root, "linked.tf")); err != nil { + t.Skip("symlinks unavailable") + } + _, err = configurationWorkdirDigest(root, plan) + require.True(t, errors.Is(err, errUtils.ErrPluginUnsupportedExtension)) +} diff --git a/pkg/component/terraform/configuration_test.go b/pkg/component/terraform/configuration_test.go index 0f9621bf038..7d0fb06ef44 100644 --- a/pkg/component/terraform/configuration_test.go +++ b/pkg/component/terraform/configuration_test.go @@ -20,19 +20,50 @@ func TestPrepareConfiguration(t *testing.T) { binary, err := os.Executable() require.NoError(t, err) info := &schema.ConfigAndStacksInfo{Command: binary, SubCommand: "apply", Stack: "live/dev", ComponentFromArg: "app", ComponentSection: map[string]any{}, ComponentSettingsSection: map[string]any{"configuration": true}} - expected := &schema.PreparedComponentConfiguration{Engine: "terraform", Source: "/module", Inputs: map[string]any{"fresh": "output"}, BackendType: "local", Backend: map[string]any{"path": "state.tfstate"}, Workspace: "default"} + expected := &schema.PreparedComponentConfiguration{Engine: "terraform", Source: filepath.Join(t.TempDir(), "module"), Inputs: map[string]any{"fresh": "output"}, BackendType: "local", Backend: map[string]any{"path": "state.tfstate"}, Workspace: "default"} runtime.EXPECT().Prepare(gomock.Any(), info).Return(expected, nil) result, err := prepareConfiguration(t.Context(), config, info) require.NoError(t, err) require.Same(t, expected, result) require.True(t, filepath.IsAbs(info.Command)) require.Equal(t, expected.Inputs, info.ComponentVarsSection) - require.Equal(t, map[string]any{"uri": expected.Source}, info.ComponentSection["source"]) + // A local module is refreshed on every run; see TestPrepareConfigurationRefreshesOnlyLocalModules. + require.Equal(t, map[string]any{"uri": expected.Source, "ttl": "0s"}, info.ComponentSection["source"]) require.Equal(t, expected.Backend, info.ComponentBackendSection) require.Equal(t, "default", info.TerraformWorkspace) require.True(t, config.Components.Terraform.AutoGenerateBackendFile) } +func TestPrepareConfigurationRefreshesOnlyLocalModules(t *testing.T) { + for _, test := range []struct { + name string + source string + ttl any + }{ + {name: "relative path", source: "./modules/unit", ttl: "0s"}, + {name: "absolute path", source: filepath.Join(t.TempDir(), "module"), ttl: "0s"}, + {name: "git", source: "git::https://example.invalid/modules.git//unit?ref=v1"}, + {name: "archive", source: "https://example.invalid/unit.zip"}, + } { + t.Run(test.name, func(t *testing.T) { + runtime := NewMockComponentConfigurationRuntime(gomock.NewController(t)) + config := &schema.AtmosConfiguration{ComponentConfigurations: runtime} + binary, err := os.Executable() + require.NoError(t, err) + info := &schema.ConfigAndStacksInfo{Command: binary, SubCommand: "apply", ComponentSection: map[string]any{}, ComponentSettingsSection: map[string]any{"configuration": true}} + runtime.EXPECT().Prepare(gomock.Any(), info).Return(&schema.PreparedComponentConfiguration{Engine: "terraform", Source: test.source}, nil) + + _, err = prepareConfiguration(t.Context(), config, info) + + require.NoError(t, err) + source, ok := info.ComponentSection["source"].(map[string]any) + require.True(t, ok) + require.Equal(t, test.source, source["uri"]) + require.Equal(t, test.ttl, source["ttl"]) + }) + } +} + func TestPrepareConfigurationGuards(t *testing.T) { for _, test := range []struct { name string diff --git a/pkg/component/terraform/configuration_toolchain_path_test.go b/pkg/component/terraform/configuration_toolchain_path_test.go new file mode 100644 index 00000000000..5a4b499b5e1 --- /dev/null +++ b/pkg/component/terraform/configuration_toolchain_path_test.go @@ -0,0 +1,32 @@ +package terraform + +import ( + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cloudposse/atmos/pkg/schema" +) + +// When the process PATH already starts with the toolchain directories (for example an Atmos run +// nested inside a toolchain shell), there is nothing left to prepend: the selected PATH is used as is. +func TestToolchainPathEnvWithToolchainDirectoriesAlreadyInAmbientPath(t *testing.T) { + harness := newToolchainHarness(t, "1.0.0") + info := harness.info(t, &toolchainScenario{identity: "development", tofuVersion: "1.0.0", toolVersion: "1.0.0"}, filepath.Join(t.TempDir(), "probe.jsonl")) + tenv, err := resolveAndInstallToolchainDeps(harness.config, info) + require.NoError(t, err) + full := tenv.PATH() + require.NotEmpty(t, full) + t.Setenv("PATH", full) + + selected := filepath.Join(t.TempDir(), "selected") + got := toolchainPathEnv(&schema.ConfigAndStacksInfo{ComponentEnvList: []string{"PATH=" + selected}}, tenv) + require.Equal(t, []string{"PATH=" + selected}, got) + + // An empty selected PATH is the same case with nothing to extend: only the toolchain prefix remains, which is empty. + got = toolchainPathEnv(&schema.ConfigAndStacksInfo{ComponentEnvList: []string{"PATH="}}, tenv) + require.Equal(t, []string{"PATH="}, got) + require.False(t, strings.Contains(got[0], full), "the ambient toolchain directories must not be re-added") +} diff --git a/pkg/component/terraform/configuration_toolchain_test.go b/pkg/component/terraform/configuration_toolchain_test.go new file mode 100644 index 00000000000..1b5916492b1 --- /dev/null +++ b/pkg/component/terraform/configuration_toolchain_test.go @@ -0,0 +1,520 @@ +package terraform + +import ( + "bytes" + "context" + "encoding/json" + "os" + "path/filepath" + "runtime" + "strings" + "sync" + "testing" + + "github.com/stretchr/testify/require" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/dependencies" + adapter "github.com/cloudposse/atmos/pkg/plugin/adapter/component" + configadapter "github.com/cloudposse/atmos/pkg/plugin/adapter/configuration" + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" + "github.com/cloudposse/atmos/pkg/schema" + "github.com/cloudposse/atmos/pkg/toolchain" +) + +// testEnvToolchainProbe names a file. A process started with it set records how it was +// invoked into that file and exits, which lets a test observe the executable, directory, +// arguments, and environment of every Terraform subprocess without any Unix-only binary. +const testEnvToolchainProbe = "_ATMOS_TEST_TOOLCHAIN_PROBE" + +// toolchainProbe is one recorded subprocess invocation. +type toolchainProbe struct { + Executable string `json:"executable"` + Directory string `json:"directory"` + Arguments []string `json:"arguments"` + Path string `json:"path"` + Profile string `json:"profile"` + Custom string `json:"custom"` + Token string `json:"token"` + Ambient string `json:"ambient"` + Identity string `json:"identity"` +} + +func runToolchainProbeForTest(path string) int { + executable, _ := os.Executable() + directory, _ := os.Getwd() + probe := toolchainProbe{ + Executable: executable, Directory: directory, Arguments: os.Args[1:], Path: os.Getenv("PATH"), + Profile: os.Getenv("AWS_PROFILE"), Custom: os.Getenv("CUSTOM_VALUE"), Token: os.Getenv("AWS_WEB_IDENTITY_TOKEN_FILE"), + Ambient: os.Getenv("ATMOS_TOOLCHAIN_AMBIENT_ONLY"), Identity: os.Getenv("ATMOS_IDENTITY"), + } + file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) + if err != nil { + return 1 + } + defer file.Close() + if err := json.NewEncoder(file).Encode(probe); err != nil { + return 1 + } + return 0 +} + +func readToolchainProbes(t *testing.T, path string) []toolchainProbe { + t.Helper() + data, err := os.ReadFile(path) + if os.IsNotExist(err) { + return nil + } + require.NoError(t, err) + var probes []toolchainProbe + decoder := json.NewDecoder(bytes.NewReader(data)) + for decoder.More() { + var probe toolchainProbe + require.NoError(t, decoder.Decode(&probe)) + probes = append(probes, probe) + } + return probes +} + +// installToolchainFixture places a hard link (or copy) of the test binary where the real +// toolchain installer would have installed owner/repo at version, so no network is used. +func installToolchainFixture(t *testing.T, installPath, owner, repo, version, name string) string { + t.Helper() + if runtime.GOOS == "windows" { + name += ".exe" + } + target := filepath.Join(installPath, "bin", owner, repo, version, name) + require.NoError(t, os.MkdirAll(filepath.Dir(target), 0o755)) + executable, err := os.Executable() + require.NoError(t, err) + if err := os.Link(executable, target); err == nil { + return target + } + content, err := os.ReadFile(executable) + require.NoError(t, err) + require.NoError(t, os.WriteFile(target, content, 0o755)) + return target +} + +func physical(t *testing.T, path string) string { + t.Helper() + resolved, err := filepath.EvalSymlinks(path) + require.NoError(t, err) + return filepath.Clean(resolved) +} + +// pathContains reports whether the fixture tool is an executable file in one of the PATH directories. +func pathContains(path string) (string, bool) { + name := "fixture-tool" + if runtime.GOOS == "windows" { + name += ".exe" + } + for _, dir := range filepath.SplitList(path) { + candidate := filepath.Join(dir, name) + if info, err := os.Stat(candidate); err == nil && !info.IsDir() { + return candidate, true + } + } + return "", false +} + +// recordingConfigurationProvider is a native configuration provider that records what the +// host sent over the plugin boundary. It stands in only for the out-of-process plugin. +type recordingConfigurationProvider struct { + mu sync.Mutex + events *[]string + requests []*pluginv1.ComponentRequest +} + +func (p *recordingConfigurationProvider) ComponentType() string { return "terraform" } + +func (p *recordingConfigurationProvider) Discover(context.Context, *pluginv1.DiscoverComponentsRequest) (*pluginv1.DiscoverComponentsResponse, error) { + configuration, err := adapter.EncodeObject(map[string]any{"source": "./module", "vars": map[string]any{}}) + if err != nil { + return nil, err + } + return &pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{{ + Address: &pluginv1.ComponentAddress{Stack: "live", Component: "app", ComponentType: "terraform"}, + Provider: "terragrunt", Engine: "opentofu", Source: "live/app/terragrunt.hcl", Configuration: configuration, + }}}, nil +} + +func (p *recordingConfigurationProvider) Prepare(_ context.Context, request *pluginv1.ComponentRequest) (*pluginv1.PrepareComponentResponse, error) { + p.mu.Lock() + defer p.mu.Unlock() + *p.events = append(*p.events, "evaluate") + p.requests = append(p.requests, request) + inputs, err := adapter.EncodeObject(map[string]any{}) + if err != nil { + return nil, err + } + backend, err := adapter.EncodeObject(map[string]any{"path": "terraform.tfstate"}) + if err != nil { + return nil, err + } + return &pluginv1.PrepareComponentResponse{Engine: "opentofu", ModuleSource: "./module", Inputs: inputs, State: &pluginv1.ComponentStateIdentity{BackendType: "local", Backend: backend, Workspace: "default"}}, nil +} + +// toolchainScenario is one component execution: an identity and the tool versions it pins. +type toolchainScenario struct { + identity, tofuVersion, toolVersion, stackValue string + subCommand string // Defaults to plan. + stackPath string // Optional PATH set in the stack env section. +} + +// toolchainHarness wires the real authentication hook, the real toolchain resolver over a +// pre-seeded install directory, the real plugin adapter session, and the real command pipeline. +type toolchainHarness struct { + config *schema.AtmosConfiguration + executor *Executor + provider *recordingConfigurationProvider + events []string + tools map[string]string // "name@version" -> installed path. + ambient string + afterAuth func(*schema.ConfigAndStacksInfo) // Optional identity-environment adjustment. +} + +func newToolchainHarness(t *testing.T, versions ...string) *toolchainHarness { + t.Helper() + previous := toolchain.GetAtmosConfig() + t.Cleanup(func() { toolchain.SetAtmosConfig(previous) }) + + // Credentials and config written by the mock identity stay inside the test. + home := t.TempDir() + for _, key := range []string{"HOME", "USERPROFILE", "XDG_CONFIG_HOME", "XDG_DATA_HOME", "XDG_CACHE_HOME"} { + t.Setenv(key, home) + } + // The ambient PATH has neither the toolchain directories nor either tool. + ambient := t.TempDir() + t.Setenv("PATH", ambient) + t.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", "ambient-token-must-not-return") + t.Setenv("ATMOS_TOOLCHAIN_AMBIENT_ONLY", "ambient") + t.Setenv("CUSTOM_VALUE", "ambient") + + root := t.TempDir() + config := &schema.AtmosConfiguration{BasePath: root, BasePathAbsolute: root} + config.Toolchain = schema.Toolchain{ + Install: schema.ToolchainInstallNever, InstallPath: filepath.Join(root, "tools"), + Aliases: map[string]string{"tofu": "opentofu/opentofu", "fixture-tool": "acme/fixture-tool"}, + } + config.Components.Terraform.Command = "tofu" + config.Components.Terraform.ConfigurationProviders = map[string]schema.ComponentConfiguration{ + "terragrunt": {Enabled: true, Discovery: schema.ComponentDiscoveryConfig{Enabled: true, IncludedPaths: []string{"live/**"}}}, + } + harness := &toolchainHarness{config: config, executor: testExecutor(t), tools: map[string]string{}, ambient: ambient} + for _, version := range versions { + harness.tools["tofu@"+version] = installToolchainFixture(t, config.Toolchain.InstallPath, "opentofu", "opentofu", version, "tofu") + harness.tools["fixture-tool@"+version] = installToolchainFixture(t, config.Toolchain.InstallPath, "acme", "fixture-tool", version, "fixture-tool") + } + + harness.provider = &recordingConfigurationProvider{events: &harness.events} + session, err := configadapter.New(t.Context(), config, "plan", func(context.Context, []string) (map[string]configadapter.Provider, error) { + return map[string]configadapter.Provider{"terragrunt": harness.provider}, nil + }) + require.NoError(t, err) + config.ComponentConfigurations = session + + // Observe the real auth hook and the real toolchain resolver without replacing them. + authenticate, resolve := harness.executor.prepareAuthEnvironmentFn, harness.executor.resolveConfigurationToolsFn + harness.executor.prepareAuthEnvironmentFn = func(c *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) error { + harness.events = append(harness.events, "authenticate") + if err := authenticate(c, info); err != nil { + return err + } + // Real cloud identities remove ambient credential variables from the sanitized + // environment; the mock identity does not. Do the same so the test can prove the + // host never reintroduces them downstream. + sanitized := info.SanitizedEnv[:0:0] + for _, entry := range info.SanitizedEnv { + if !strings.HasPrefix(entry, "AWS_WEB_IDENTITY_TOKEN_FILE=") { + sanitized = append(sanitized, entry) + } + } + info.SanitizedEnv = sanitized + if harness.afterAuth != nil { + harness.afterAuth(info) + } + return nil + } + harness.executor.resolveConfigurationToolsFn = func(c *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (*dependencies.ToolchainEnvironment, error) { + harness.events = append(harness.events, "toolchain") + require.NotEmpty(t, info.SanitizedEnv, "the identity environment must exist before the toolchain is resolved") + return resolve(c, info) + } + return harness +} + +func (h *toolchainHarness) info(t *testing.T, s *toolchainScenario, probe string) *schema.ConfigAndStacksInfo { + t.Helper() + identities := map[string]any{} + for _, name := range []string{"development", "production"} { + identities[name] = map[string]any{"kind": "mock/aws", "default": name == "development", "via": map[string]any{"provider": "mock-provider"}, "principal": map[string]any{"account": map[string]any{"id": "123456789012"}}} + } + subCommand := s.subCommand + if subCommand == "" { + subCommand = "plan" + } + env := map[string]any{"CUSTOM_VALUE": s.stackValue, testEnvToolchainProbe: probe} + if s.stackPath != "" { + env["PATH"] = s.stackPath + } + return &schema.ConfigAndStacksInfo{ + Stack: "live", ComponentFromArg: "app", Command: "tofu", SubCommand: subCommand, Identity: s.identity, + AdditionalArgsAndFlags: autoApproveFor(subCommand), + ComponentAuthSection: map[string]any{ + "providers": map[string]any{"mock-provider": map[string]any{"kind": "mock/aws"}}, + "identities": identities, + }, + ComponentSettingsSection: map[string]any{"configuration": true}, + ComponentSection: map[string]any{"dependencies": map[string]any{"tools": map[string]any{ + "tofu": s.tofuVersion, "fixture-tool": s.toolVersion, + }}}, + ComponentEnvSection: env, + } +} + +func autoApproveFor(subCommand string) []string { + if subCommand == "apply" || subCommand == "destroy" { + return []string{"-auto-approve"} + } + return nil +} + +// run performs the host's ordered preparation for one component and then its Terraform pipeline. +func (h *toolchainHarness) run(t *testing.T, s *toolchainScenario) (*schema.ConfigAndStacksInfo, []toolchainProbe, *pluginv1.ComponentRequest) { + t.Helper() + probePath := filepath.Join(t.TempDir(), "probe.jsonl") + info := h.info(t, s, probePath) + h.events = nil + h.provider.requests = nil + + _, err := h.executor.prepareConfigurationEnvironment(t.Context(), h.config, info, true) + require.NoError(t, err) + prepared, err := prepareConfiguration(t.Context(), h.config, info) + require.NoError(t, err) + require.NotNil(t, prepared) + require.Len(t, h.provider.requests, 1) + + // Module vendoring is not under test; without a source the init provisioners have nothing to fetch. + delete(info.ComponentSection, "source") + + // Implicit initialization, workspace selection, and the operation all run in the pipeline. + origStdin := os.Stdin + os.Stdin = nil + t.Cleanup(func() { os.Stdin = origStdin }) + dir := t.TempDir() + execCtx := &componentExecContext{componentPath: dir, workingDir: dir, varFile: "app.terraform.tfvars.json", planFile: "app.planfile"} + // execute() hands the identity's sanitized environment to the pipeline exactly like this. + require.NoError(t, h.executor.executeCommandPipeline(h.config, info, execCtx, WithEnvironment(info.SanitizedEnv))) + return info, readToolchainProbes(t, probePath), h.provider.requests[0] +} + +// Authentication, then toolchain resolution, then evaluation. Every Terraform subprocess +// (implicit init, workspace selection, and the operation itself) runs the pinned executable +// with the identity's environment, and a dependencies.tools executable is reachable on the +// PATH that crosses the plugin boundary even though it is absent from the ambient PATH. +func TestConfigurationToolchainEndToEndThroughAuthPluginAndPipeline(t *testing.T) { + harness := newToolchainHarness(t, "1.0.0", "2.0.0") + scenario := toolchainScenario{identity: "production", tofuVersion: "1.0.0", toolVersion: "1.0.0", stackValue: "from-stack"} + + info, probes, request := harness.run(t, &scenario) + + require.Equal(t, []string{"authenticate", "toolchain", "evaluate"}, harness.events) + + pinned := harness.tools["tofu@1.0.0"] + tool := harness.tools["fixture-tool@1.0.0"] + require.Equal(t, pinned, info.Command, "info.Command must be the pinned toolchain executable") + require.True(t, filepath.IsAbs(info.Command)) + + environment := request.Invocation.Environment + require.Equal(t, pinned, environment["ATMOS_TERRAFORM_BINARY"]) + require.Equal(t, "production", environment["AWS_PROFILE"], "identity-provided environment crosses the boundary") + require.Equal(t, "production", environment["ATMOS_IDENTITY"]) + require.Equal(t, "from-stack", environment["CUSTOM_VALUE"], "stack env beats ambient env") + require.NotContains(t, environment, "AWS_WEB_IDENTITY_TOKEN_FILE", "ambient credentials must not return behind an identity") + + wirePath := environment["PATH"] + found, ok := pathContains(wirePath) + require.True(t, ok, "dependencies.tools executables must be on the PATH sent to the plugin: %s", wirePath) + require.Equal(t, physical(t, tool), physical(t, found)) + _, ambientHasTool := pathContains(os.Getenv("PATH")) + require.False(t, ambientHasTool, "the tool must be absent from the ambient PATH for this test to mean anything") + // The two toolchain directories precede the ambient PATH (their relative order is unspecified). + entries := filepath.SplitList(wirePath) + require.Len(t, entries, 3) + require.ElementsMatch(t, []string{filepath.Dir(pinned), filepath.Dir(tool)}, entries[:2], "toolchain directories precede everything else") + require.Equal(t, harness.ambient, entries[2]) + + // Every spawned subprocess is the pinned executable, with the identity and toolchain PATH. + require.NotEmpty(t, probes) + commands := []string{} + for _, probe := range probes { + require.Equal(t, physical(t, pinned), physical(t, probe.Executable), "args %v", probe.Arguments) + require.Equal(t, wirePath, probe.Path, "args %v", probe.Arguments) + require.Equal(t, "production", probe.Profile) + require.Equal(t, "from-stack", probe.Custom) + require.Empty(t, probe.Token, "ambient credentials must not reach Terraform") + commands = append(commands, strings.Join(probe.Arguments, " ")) + } + require.True(t, containsPrefix(commands, "init"), "implicit init must run the pinned executable: %v", commands) + require.True(t, containsPrefix(commands, "plan"), "the operation must run the pinned executable: %v", commands) +} + +func containsPrefix(values []string, prefix string) bool { + for _, value := range values { + if strings.HasPrefix(value, prefix) { + return true + } + } + return false +} + +// Two components with different identities and different pinned tool versions run one after the +// other and at the same time in one process. Neither sees the other's PATH, tools, or identity. +func TestConfigurationToolchainIsolatesIdentitiesAndVersions(t *testing.T) { + harness := newToolchainHarness(t, "1.0.0", "2.0.0") + scenarios := []toolchainScenario{ + {identity: "development", tofuVersion: "1.0.0", toolVersion: "1.0.0", stackValue: "dev-value"}, + {identity: "production", tofuVersion: "2.0.0", toolVersion: "2.0.0", stackValue: ""}, + } + check := func(t *testing.T, s toolchainScenario) { + t.Helper() + info, probes, request := harness.run(t, &s) + pinned := harness.tools["tofu@"+s.tofuVersion] + require.Equal(t, pinned, info.Command) + environment := request.Invocation.Environment + require.Equal(t, s.identity, environment["AWS_PROFILE"]) + require.Equal(t, s.stackValue, environment["CUSTOM_VALUE"], "an explicitly empty stack value must not fall back to the ambient value") + found, ok := pathContains(environment["PATH"]) + require.True(t, ok) + require.Equal(t, physical(t, harness.tools["fixture-tool@"+s.toolVersion]), physical(t, found), "only this component's tool version is on its PATH") + require.NotEmpty(t, probes) + for _, probe := range probes { + require.Equal(t, physical(t, pinned), physical(t, probe.Executable)) + require.Equal(t, s.identity, probe.Profile) + require.Equal(t, s.stackValue, probe.Custom) + } + } + for _, s := range []toolchainScenario{scenarios[0], scenarios[1], scenarios[0]} { + check(t, s) + } + // The ambient process environment is never mutated to select an identity or tool. + require.Equal(t, harness.ambient, os.Getenv("PATH")) + require.Equal(t, "ambient", os.Getenv("CUSTOM_VALUE")) + require.Equal(t, "ambient-token-must-not-return", os.Getenv("AWS_WEB_IDENTITY_TOKEN_FILE")) +} + +// Authentication failure and cancellation stop the pipeline before the toolchain or the evaluator run. +func TestConfigurationToolchainStopsOnAuthenticationFailure(t *testing.T) { + harness := newToolchainHarness(t, "1.0.0") + info := harness.info(t, &toolchainScenario{identity: "does-not-exist", tofuVersion: "1.0.0", toolVersion: "1.0.0"}, filepath.Join(t.TempDir(), "probe.jsonl")) + harness.events = nil + _, err := harness.executor.prepareConfigurationEnvironment(t.Context(), harness.config, info, true) + require.Error(t, err) + require.Equal(t, []string{"authenticate"}, harness.events, "the toolchain and the evaluator must not run after an auth failure") + require.Empty(t, harness.provider.requests) + require.NotErrorIs(t, err, errUtils.ErrPluginInvalidConfig) +} + +// Every Terraform operation, including a bare init and the implicit init and workspace steps +// that precede plan, apply, and destroy, runs the executable pinned through dependencies.tools. +func TestConfigurationToolchainPinsExecutableForEveryOperation(t *testing.T) { + harness := newToolchainHarness(t, "1.0.0", "2.0.0") + for _, subCommand := range []string{"init", "plan", "apply", "destroy", "output", "validate"} { + t.Run(subCommand, func(t *testing.T) { + info, probes, request := harness.run(t, &toolchainScenario{identity: "development", tofuVersion: "2.0.0", toolVersion: "2.0.0", stackValue: "v", subCommand: subCommand}) + pinned := harness.tools["tofu@2.0.0"] + require.Equal(t, pinned, info.Command) + require.Equal(t, pinned, request.Invocation.Environment["ATMOS_TERRAFORM_BINARY"]) + require.Equal(t, subCommand, request.Operation.Command) + require.NotEmpty(t, probes) + sawOperation := false + for _, probe := range probes { + require.Equal(t, physical(t, pinned), physical(t, probe.Executable), "every subprocess uses the pinned executable: %v", probe.Arguments) + require.Equal(t, request.Invocation.Environment["PATH"], probe.Path, "evaluation and execution share one PATH: %v", probe.Arguments) + sawOperation = sawOperation || (len(probe.Arguments) > 0 && probe.Arguments[0] == subCommand) + } + require.True(t, sawOperation, "the %s operation itself must run", subCommand) + }) + } +} + +// A PATH set in the stack env section must survive behind the toolchain directories, so a +// component can still reach its own tools. The toolchain directories come first. +func TestConfigurationToolchainPreservesStackPathEntries(t *testing.T) { + harness := newToolchainHarness(t, "1.0.0") + custom := filepath.Join(t.TempDir(), "custom-bin") + _, probes, request := harness.run(t, &toolchainScenario{identity: "development", tofuVersion: "1.0.0", toolVersion: "1.0.0", stackPath: custom}) + + for name, path := range map[string]string{"plugin boundary": request.Invocation.Environment["PATH"], "terraform subprocess": probes[0].Path} { + entries := filepath.SplitList(path) + require.Contains(t, entries, custom, "%s: the stack PATH entry must not be discarded", name) + _, ok := pathContains(path) + require.True(t, ok, "%s: the toolchain tool must still be reachable", name) + require.Less(t, indexOf(entries, filepath.Dir(harness.tools["tofu@1.0.0"])), indexOf(entries, custom), "%s: toolchain precedes stack PATH", name) + } +} + +func indexOf(values []string, want string) int { + for i, value := range values { + if value == want { + return i + } + } + return -1 +} + +// toolchainPathEnv decides which PATH wins; exercise every source against a real toolchain environment. +func TestToolchainPathEnvSelectsThePathToExtend(t *testing.T) { + harness := newToolchainHarness(t, "1.0.0") + info := harness.info(t, &toolchainScenario{identity: "development", tofuVersion: "1.0.0", toolVersion: "1.0.0"}, filepath.Join(t.TempDir(), "probe.jsonl")) + tenv, err := resolveAndInstallToolchainDeps(harness.config, info) + require.NoError(t, err) + dirs := strings.Join(tenv.ToolchainDirs(), string(os.PathListSeparator)) + require.NotEmpty(t, dirs) + ambient := os.Getenv("PATH") + join := func(parts ...string) string { return strings.Join(parts, string(os.PathListSeparator)) } + + for _, test := range []struct { + name string + component []string + sanitized []string + want string + }{ + {name: "no selected PATH keeps the toolchain PATH", want: join(dirs, ambient)}, + {name: "selected equal to the ambient PATH", component: []string{"PATH=" + ambient}, want: join(dirs, ambient)}, + {name: "component PATH is extended", component: []string{"PATH=/component"}, want: join(dirs, "/component")}, + {name: "last component PATH wins", component: []string{"PATH=/first", "PATH=/last"}, want: join(dirs, "/last")}, + {name: "identity PATH is extended", sanitized: []string{"AWS_PROFILE=x", "PATH=/identity"}, want: join(dirs, "/identity")}, + {name: "component PATH beats identity PATH", component: []string{"PATH=/component"}, sanitized: []string{"PATH=/identity"}, want: join(dirs, "/component")}, + {name: "explicitly empty PATH leaves only toolchain directories", component: []string{"PATH="}, want: dirs}, + } { + t.Run(test.name, func(t *testing.T) { + got := toolchainPathEnv(&schema.ConfigAndStacksInfo{ComponentEnvList: test.component, SanitizedEnv: test.sanitized}, tenv) + require.Equal(t, []string{"PATH=" + test.want}, got) + }) + } + + t.Run("without toolchain directories nothing is overridden", func(t *testing.T) { + empty := &dependencies.ToolchainEnvironment{} + require.Nil(t, toolchainPathEnv(&schema.ConfigAndStacksInfo{ComponentEnvList: []string{"PATH=/component"}}, empty)) + }) +} + +// An identity may carry its own PATH; the toolchain directories extend it for both the +// evaluator and Terraform instead of replacing it with the host PATH. +func TestConfigurationToolchainPreservesIdentityPath(t *testing.T) { + harness := newToolchainHarness(t, "1.0.0") + identityBin := filepath.Join(t.TempDir(), "identity-bin") + harness.afterAuth = func(info *schema.ConfigAndStacksInfo) { + info.SanitizedEnv = append(info.SanitizedEnv, "PATH="+identityBin+string(os.PathListSeparator)+harness.ambient) + } + _, probes, request := harness.run(t, &toolchainScenario{identity: "development", tofuVersion: "1.0.0", toolVersion: "1.0.0"}) + for name, path := range map[string]string{"plugin boundary": request.Invocation.Environment["PATH"], "terraform subprocess": probes[0].Path} { + entries := filepath.SplitList(path) + require.Contains(t, entries, identityBin, name) + _, ok := pathContains(path) + require.True(t, ok, name) + require.Less(t, indexOf(entries, filepath.Dir(harness.tools["tofu@1.0.0"])), indexOf(entries, identityBin), name) + } +} diff --git a/pkg/component/terraform/execute_command.go b/pkg/component/terraform/execute_command.go index 036886350d1..a0e8545624f 100644 --- a/pkg/component/terraform/execute_command.go +++ b/pkg/component/terraform/execute_command.go @@ -130,22 +130,7 @@ func (e *Executor) runMainTerraformCommand( capturedMetrics = m })) - err := ExecuteShellCommandWithRetry( - atmosConfig, - info, - info.SubCommand, - func(o ...component.ExecutionOption) error { - return e.executeStreamingOrShell(atmosConfig, executionInfo, &streamingExecRequest{ - componentPath: componentPath, - args: allArgsAndFlags, - gatePhase: info.SubCommand, - subCommand: info.SubCommand, - redirectStdErr: info.RedirectStdErr, - shellOpts: o, - }) - }, - allOpts..., - ) + err := e.runMainShellCommand(atmosConfig, executionInfo, allArgsAndFlags, componentPath, allOpts) // An explicit `atmos terraform init` reaches this main-command path rather // than executeTerraformInitCommand. Keep its lifecycle equivalent to an diff --git a/pkg/component/terraform/execute_environment.go b/pkg/component/terraform/execute_environment.go index 6b051c3e62b..2bb3bd60419 100644 --- a/pkg/component/terraform/execute_environment.go +++ b/pkg/component/terraform/execute_environment.go @@ -22,6 +22,13 @@ import ( rc "github.com/cloudposse/atmos/pkg/terraform/rc" ) +const ( + // Process executable search path variable. + envPath = "PATH" + // Separator between the name and the value in a KEY=value environment entry. + envAssign = "=" +) + // 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. @@ -31,7 +38,7 @@ func warnOnConflictingEnvVars() { var problematicVars []string for _, envVar := range os.Environ() { - parts := strings.SplitN(envVar, "=", 2) + parts := strings.SplitN(envVar, envAssign, 2) if len(parts) != 2 { continue } @@ -99,7 +106,7 @@ func assembleComponentEnvVars(atmosConfig *schema.AtmosConfiguration, info *sche // Toolchain PATH must come last so it takes precedence over all other PATH entries. if tenv != nil { - info.ComponentEnvList = append(info.ComponentEnvList, tenv.EnvVars()...) + info.ComponentEnvList = append(info.ComponentEnvList, toolchainPathEnv(info, tenv)...) } if len(info.ComponentEnvList) > 0 { @@ -112,6 +119,47 @@ func assembleComponentEnvVars(atmosConfig *schema.AtmosConfiguration, info *sche return appendTerraformSecretEnv(info) } +// toolchainPathEnv returns the PATH entry that puts the toolchain directories first. +// +// The toolchain PATH is built from the host process PATH. A PATH selected for this component +// (the stack or component env section, or the identity's sanitized environment) must not be +// discarded by it, so the toolchain directories are prepended to that PATH instead. Without a +// component-specific PATH this is exactly the toolchain PATH. +func toolchainPathEnv(info *schema.ConfigAndStacksInfo, tenv *dependencies.ToolchainEnvironment) []string { + full := tenv.PATH() + if full == "" { + return tenv.EnvVars() + } + selected, ok := lastEnvListValue(info.ComponentEnvList, envPath) + if !ok { + selected, ok = lastEnvListValue(info.SanitizedEnv, envPath) + } + ambient := os.Getenv(envPath) //nolint:forbidigo // PATH is the process search path, not an Atmos configuration variable. + if !ok || selected == ambient || !strings.HasSuffix(full, ambient) { + return tenv.EnvVars() + } + // The toolchain PATH is ""; swap the ambient tail. + prefix := strings.TrimSuffix(strings.TrimSuffix(full, ambient), string(os.PathListSeparator)) + if selected == "" { + return []string{envPath + envAssign + prefix} + } + if prefix == "" { + return []string{envPath + envAssign + selected} + } + return []string{envPath + envAssign + prefix + string(os.PathListSeparator) + selected} +} + +// lastEnvListValue returns the value of the last KEY=value entry for key; later entries win. +func lastEnvListValue(list []string, key string) (string, bool) { + value, found := "", false + for _, entry := range list { + if k, v, ok := strings.Cut(entry, envAssign); ok && k == key { + value, found = v, true + } + } + return value, found +} + // 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 diff --git a/pkg/component/terraform/execute_hooks.go b/pkg/component/terraform/execute_hooks.go new file mode 100644 index 00000000000..60d4e1048c9 --- /dev/null +++ b/pkg/component/terraform/execute_hooks.go @@ -0,0 +1,211 @@ +package terraform + +import ( + "bytes" + "context" + "fmt" + "io" + "path/filepath" + "slices" + "strings" + + "github.com/cloudposse/atmos/pkg/component" + "github.com/cloudposse/atmos/pkg/component/execution" + log "github.com/cloudposse/atmos/pkg/logger" + "github.com/cloudposse/atmos/pkg/perf" + "github.com/cloudposse/atmos/pkg/process" + "github.com/cloudposse/atmos/pkg/schema" + "github.com/cloudposse/atmos/pkg/ui" +) + +// Hook context variables. They name Terragrunt's contract because hooks written for Terragrunt +// read them; they are set for every provider-supplied hook. +const ( + hookEnvTerraformPath = "TG_CTX_TF_PATH" + hookEnvCommand = "TG_CTX_COMMAND" + hookEnvHookName = "TG_CTX_HOOK_NAME" +) + +// executionPhase describes one Terraform subprocess that provider-supplied hooks and extra +// arguments may wrap. Each phase is matched on its own command: an implicit init is "init" +// whatever command the user requested, and workspace selection or provider locking are not +// phases at all, so a hook listed for "plan" never fires for them. +type executionPhase struct { + // command is the name hooks and extra arguments list (init, plan, destroy, ...). + command string + // args is the full argument vector, starting with the Terraform command. + args []string + // insertAt is where extra arguments go: after the command and its subcommands. + insertAt int + // planFileArgument reports that the last argument is a saved plan. + planFileArgument bool + // componentPath is the provisioned module directory. + componentPath string +} + +// runMainShellCommand runs the requested Terraform subprocess, wrapped by the component's +// provider-supplied hooks and extra arguments. They match the requested command, which is +// "destroy" for `apply -destroy`. +func (e *Executor) runMainShellCommand(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, args []string, componentPath string, opts []component.ExecutionOption) error { + position := extraArgumentsPosition(info, args) + phase := &executionPhase{ + command: execution.CommandName(info.SubCommand, args[position:]), + args: args, + insertAt: position, + planFileArgument: info.SubCommand == subcommandApply && info.UseTerraformPlan, + componentPath: componentPath, + } + return e.runExecutionPhase(atmosConfig, info, phase, opts, func(phaseInfo *schema.ConfigAndStacksInfo, phaseArgs []string, phaseOpts []component.ExecutionOption) error { + return ExecuteShellCommandWithRetry( + atmosConfig, + phaseInfo, + info.SubCommand, + func(o ...component.ExecutionOption) error { + return e.executeStreamingOrShell(atmosConfig, phaseInfo, &streamingExecRequest{ + componentPath: componentPath, + args: phaseArgs, + gatePhase: info.SubCommand, + subCommand: info.SubCommand, + redirectStdErr: info.RedirectStdErr, + shellOpts: o, + }) + }, + phaseOpts..., + ) + }) +} + +// phaseAction runs the wrapped Terraform subprocess with the phase's arguments, environment, and options. +type phaseAction func(info *schema.ConfigAndStacksInfo, args []string, opts []component.ExecutionOption) error + +// runExecutionPhase applies the component's extra arguments and runs its hooks around action. +// A component without provider-supplied execution runs action unchanged. Dry runs apply the +// arguments but never run hooks. +func (e *Executor) runExecutionPhase(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, phase *executionPhase, opts []component.ExecutionOption, action phaseAction) error { + config := info.ComponentExecution + if config.IsEmpty() { + return action(info, phase.args, opts) + } + + applied, err := execution.ApplyExtraArguments(config.ExtraArguments, &execution.ArgumentsRequest{ + Command: phase.command, + Args: phase.args, + InsertAt: phase.insertAt, + PlanFileArgument: phase.planFileArgument, + WorkingDirectory: phase.componentPath, + }) + if err != nil { + return err + } + // Extra environment applies to this phase and to its hooks, as it does for Terragrunt. + phaseInfo := *info + phaseInfo.ComponentEnvList = append(slices.Clone(info.ComponentEnvList), applied.Environment...) + + if info.DryRun { + log.Debug("Skipping hooks in dry-run mode", "command", phase.command) + return action(&phaseInfo, applied.Args, opts) + } + + runner := &hookRunner{executor: e, atmosConfig: atmosConfig, info: &phaseInfo, phase: phase, opts: hookOptions(opts)} + return execution.Run(component.ContextFromOptions(opts...), config, phase.command, runner, func() (string, error) { + if len(config.ErrorHooks) == 0 { + return "", action(&phaseInfo, applied.Args, opts) + } + // Error hooks match against the failed command's output, which the caller may also be capturing. + var output bytes.Buffer + stdout, stderr := composeRetryCaptureWriters(opts, &output) + err := action(&phaseInfo, applied.Args, append(slices.Clone(opts), component.WithStdoutCapture(stdout), component.WithStderrCapture(stderr))) + return output.String(), err + }) +} + +// hookOptions carries over only what a hook shares with the command: cancellation, the process +// environment chosen by the identity, and the terminal streams. Output capture stays with the +// command so hook output never reaches plan parsers, CI summaries, or retry matching. +func hookOptions(opts []component.ExecutionOption) []component.ExecutionOption { + var base component.ExecutionOptions + for _, opt := range opts { + opt(&base) + } + var result []component.ExecutionOption + if base.Ctx != nil { + result = append(result, component.WithProcessContext(base.Ctx)) + } + if base.ProcessEnv != nil { + result = append(result, component.WithEnvironment(base.ProcessEnv)) + } + if base.Streams != nil { + result = append(result, component.WithProcessStreams(*base.Streams)) + } + return result +} + +// hookRunner runs provider-supplied hooks through the same process facility as Terraform itself, +// so they get the invocation's environment (identity, toolchain PATH, stack and component env), +// secret masking, cancellation, and dry-run handling. +type hookRunner struct { + executor *Executor + atmosConfig *schema.AtmosConfiguration + info *schema.ConfigAndStacksInfo + phase *executionPhase + opts []component.ExecutionOption +} + +// RunHook runs one hook and returns its captured output for error hook matching. +// Hook stdout goes to the UI channel (stderr), never to the data channel, unless suppressed. +func (r *hookRunner) RunHook(_ context.Context, kind execution.HookKind, hook *schema.ComponentHook) (string, error) { + defer perf.Track(r.atmosConfig, "terraform.hookRunner.RunHook")() + + ui.Info(fmt.Sprintf("Executing %s: %s", strings.ReplaceAll(string(kind), "_", " "), hook.Name)) + + var base component.ExecutionOptions + for _, opt := range r.opts { + opt(&base) + } + stdout := process.OSStreams().Stderr + if base.Streams != nil && base.Streams.Stderr != nil { + stdout = base.Streams.Stderr + } + if hook.SuppressStdout { + stdout = io.Discard + } + + var output bytes.Buffer + captured := &syncWriter{w: &output} + opts := append( + slices.Clone(r.opts), + component.WithStdoutOverride(stdout), + component.WithStdoutCapture(captured), + component.WithStderrCapture(captured), + ) + env := append( + slices.Clone(r.info.ComponentEnvList), + hookEnvTerraformPath+"="+r.info.Command, + hookEnvCommand+"="+r.phase.command, + hookEnvHookName+"="+hook.Name, + ) + err := r.executor.services.ExecuteShellCommand(*r.atmosConfig, hook.Execute[0], hook.Execute[1:], r.workingDirectory(hook), env, false, "", opts...) + return output.String(), err +} + +// workingDirectory defaults to the provisioned module directory; relative paths resolve there. +func (r *hookRunner) workingDirectory(hook *schema.ComponentHook) string { + switch { + case hook.WorkingDirectory == "": + return r.phase.componentPath + case filepath.IsAbs(hook.WorkingDirectory): + return hook.WorkingDirectory + default: + return filepath.Join(r.phase.componentPath, hook.WorkingDirectory) + } +} + +// extraArgumentsPosition is the index after the Terraform command and its subcommands, so extra +// arguments precede the caller's flags exactly as Terragrunt inserts them. +func extraArgumentsPosition(info *schema.ConfigAndStacksInfo, args []string) int { + position := min(len(strings.Fields(info.SubCommand)), len(args)) + if info.SubCommand2 != "" && position < len(args) && args[position] == info.SubCommand2 { + position++ + } + return position +} diff --git a/pkg/component/terraform/execute_hooks_process_test.go b/pkg/component/terraform/execute_hooks_process_test.go new file mode 100644 index 00000000000..cab1f611d80 --- /dev/null +++ b/pkg/component/terraform/execute_hooks_process_test.go @@ -0,0 +1,325 @@ +package terraform + +// These tests run real subprocesses through the host's process facility. The test binary is +// every executable: it is Terraform (info.Command), each hook, and a toolchain-only tool, so +// nothing platform-specific is needed. A child records its invocation as one JSON line, which +// gives an ordered timeline of everything the pipeline started. + +import ( + "bytes" + "context" + "encoding/json" + "fmt" + "os" + "path/filepath" + "runtime" + "slices" + "strconv" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/require" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/component" + "github.com/cloudposse/atmos/pkg/process" + "github.com/cloudposse/atmos/pkg/schema" +) + +const ( + testEnvHookChild = "_ATMOS_TEST_HOOK_CHILD" + testEnvHookLog = "_ATMOS_TEST_HOOK_LOG" + // The child whose first argument equals this value exits 1. + testEnvHookFail = "_ATMOS_TEST_HOOK_FAIL" +) + +// hookRecord is what a child observed about its own invocation. +type hookRecord struct { + Executable string `json:"executable"` + Arguments []string `json:"arguments"` + Directory string `json:"directory"` + Path string `json:"path"` + Env map[string]string `json:"env"` +} + +// runHookChildForTest records the invocation and then acts on scripted arguments: +// exit=N, stdout=TEXT, stderr=TEXT, sleep=MILLISECONDS. +func runHookChildForTest() int { + args := os.Args[1:] + directory, _ := os.Getwd() + executable, _ := os.Executable() + record := hookRecord{Executable: executable, Arguments: args, Directory: directory, Path: os.Getenv("PATH"), Env: map[string]string{}} + for _, entry := range os.Environ() { + if key, value, ok := strings.Cut(entry, "="); ok && (strings.HasPrefix(key, "TG_CTX_") || strings.HasPrefix(key, "HOOKTEST_")) { + record.Env[key] = value + } + } + if path := os.Getenv(testEnvHookLog); path != "" { + file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600) + if err != nil { + return 1 + } + _ = json.NewEncoder(file).Encode(record) + _ = file.Close() + } + if len(args) > 0 && args[0] == os.Getenv(testEnvHookFail) { + fmt.Fprintf(os.Stderr, "child %s failed with Error: intentional\n", args[0]) + return 1 + } + code := 0 + for _, token := range args { + key, value, _ := strings.Cut(token, "=") + switch key { + case "exit": + code, _ = strconv.Atoi(value) + case "stdout": + fmt.Fprintln(os.Stdout, value) + case "stderr": + fmt.Fprintln(os.Stderr, value) + case "sleep": + ms, _ := strconv.Atoi(value) + time.Sleep(time.Duration(ms) * time.Millisecond) + } + } + return code +} + +// readHookLogQuietly is for goroutines, which must not fail the test directly. +func readHookLogQuietly(path string) []hookRecord { + data, err := os.ReadFile(path) + if err != nil { + return nil + } + var records []hookRecord + decoder := json.NewDecoder(bytes.NewReader(data)) + for decoder.More() { + var record hookRecord + if decoder.Decode(&record) != nil { + break + } + records = append(records, record) + } + return records +} + +func readHookLog(t *testing.T, path string) []hookRecord { + t.Helper() + data, err := os.ReadFile(path) + if os.IsNotExist(err) { + return nil + } + require.NoError(t, err) + var records []hookRecord + decoder := json.NewDecoder(bytes.NewReader(data)) + for decoder.More() { + var record hookRecord + require.NoError(t, decoder.Decode(&record)) + records = append(records, record) + } + return records +} + +// timeline renders each record as its first argument, which names the Terraform phase or hook. +func timeline(records []hookRecord) []string { + var result []string + for _, record := range records { + result = append(result, record.Arguments[0]) + } + return result +} + +// installHookTool places a copy of the test binary as an executable that only the toolchain PATH can reach. +func installHookTool(t *testing.T, dir, name string) { + t.Helper() + if runtime.GOOS == "windows" { + name += ".exe" + } + require.NoError(t, os.MkdirAll(dir, 0o755)) + executable, err := os.Executable() + require.NoError(t, err) + target := filepath.Join(dir, name) + if err := os.Link(executable, target); err == nil { + return + } + content, err := os.ReadFile(executable) + require.NoError(t, err) + require.NoError(t, os.WriteFile(target, content, 0o755)) +} + +type processPipeline struct { + info *schema.ConfigAndStacksInfo + execCtx *componentExecContext + log string + stdout *bytes.Buffer + stderr *bytes.Buffer + toolDir string +} + +// newProcessPipeline builds a component whose Terraform and hooks are all the test binary. +func newProcessPipeline(t *testing.T, execution *schema.ComponentExecution) *processPipeline { + t.Helper() + exe, err := os.Executable() + require.NoError(t, err) + dir := t.TempDir() + log := filepath.Join(t.TempDir(), "log.jsonl") + toolDir := filepath.Join(t.TempDir(), "toolchain", "bin") + installHookTool(t, toolDir, "hook-tool") + env := []string{ + testEnvHookChild + "=1", testEnvHookLog + "=" + log, + // The toolchain directory exists only in the invocation's PATH, never in the ambient one. + "PATH=" + toolDir + string(os.PathListSeparator) + os.Getenv("PATH"), + "HOOKTEST_STACK_ENV=from-stack", + } + return &processPipeline{ + log: log, toolDir: toolDir, stdout: &bytes.Buffer{}, stderr: &bytes.Buffer{}, + info: &schema.ConfigAndStacksInfo{ + SubCommand: "plan", Command: exe, TerraformWorkspace: "dev", ComponentEnvList: env, + ComponentExecution: execution, + }, + execCtx: &componentExecContext{componentPath: dir, workingDir: dir, varFile: "vars.tfvars", planFile: "plan.tfplan"}, + } +} + +func (p *processPipeline) run(t *testing.T, opts ...component.ExecutionOption) error { + t.Helper() + streams := process.Streams{Stdout: p.stdout, Stderr: p.stderr} + opts = append([]component.ExecutionOption{component.WithProcessStreams(streams)}, opts...) + return testExecutor(t).executeCommandPipeline(&schema.AtmosConfiguration{}, p.info, p.execCtx, opts...) +} + +func toolHook(name string, commands []string, args ...string) schema.ComponentHook { + return schema.ComponentHook{Name: name, Commands: commands, Execute: append([]string{"hook-tool", name}, args...)} +} + +func TestPipelineRunsHooksAroundEachPhaseSeparately(t *testing.T) { + scripted := toolHook("plan-before", []string{"plan"}, "stdout=hook says hello") + pipeline := newProcessPipeline(t, &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{ + {Commands: []string{"init"}, Arguments: []string{"-from-init-extra"}}, + {Commands: []string{"plan"}, Arguments: []string{"-from-plan-extra"}, Environment: map[string]string{"HOOKTEST_PLAN_EXTRA": "plan"}}, + }, + BeforeHooks: []schema.ComponentHook{toolHook("init-before", []string{"init"}), scripted, toolHook("workspace-before", []string{"workspace"})}, + AfterHooks: []schema.ComponentHook{toolHook("init-after", []string{"init"}), toolHook("plan-after", []string{"plan"})}, + }) + require.NoError(t, pipeline.run(t)) + + records := readHookLog(t, pipeline.log) + require.Equal(t, []string{ + "init-before", "init", "init-after", + "workspace", // Workspace selection is not a hook phase. + "plan-before", "plan", "plan-after", + }, timeline(records)) + require.Equal(t, []string{"init", "-from-init-extra"}, records[1].Arguments[:2]) + require.Equal(t, []string{"plan", "-from-plan-extra"}, records[5].Arguments[:2]) + + exe, err := os.Executable() + require.NoError(t, err) + initRecord, planBefore, planRecord := records[1], records[4], records[5] + require.Equal(t, filepath.Base(exe), filepath.Base(initRecord.Executable)) + require.Equal(t, physicalPath(t, pipeline.execCtx.componentPath), physicalPath(t, planBefore.Directory), "hooks run in the module directory by default") + require.Equal(t, "plan", planBefore.Env["TG_CTX_COMMAND"]) + require.Equal(t, "plan-before", planBefore.Env["TG_CTX_HOOK_NAME"]) + require.Equal(t, exe, planBefore.Env["TG_CTX_TF_PATH"]) + require.Equal(t, "from-stack", planBefore.Env["HOOKTEST_STACK_ENV"], "hooks see the stack and component env") + require.Equal(t, "plan", planRecord.Env["HOOKTEST_PLAN_EXTRA"]) + require.Equal(t, "init", records[0].Env["TG_CTX_COMMAND"]) + require.Empty(t, records[0].Env["HOOKTEST_PLAN_EXTRA"], "plan env_vars must not apply to the implicit init") + require.Contains(t, planBefore.Path, pipeline.toolDir, "hooks resolve tools on the invocation's toolchain PATH") + + require.Contains(t, pipeline.stderr.String(), "hook says hello", "hook stdout belongs on the UI channel") + require.NotContains(t, pipeline.stdout.String(), "hook says hello", "hook stdout must not mix into data stdout") +} + +func physicalPath(t *testing.T, path string) string { + t.Helper() + resolved, err := filepath.EvalSymlinks(path) + require.NoError(t, err) + return filepath.Clean(resolved) +} + +func TestPipelineSuppressStdoutKeepsHookOutputOffEveryTerminalStream(t *testing.T) { + hook := toolHook("quiet", []string{"plan"}, "stdout=secret-looking-output") + hook.SuppressStdout = true + pipeline := newProcessPipeline(t, &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{hook}}) + require.NoError(t, pipeline.run(t)) + require.NotContains(t, pipeline.stdout.String(), "secret-looking-output") + require.NotContains(t, pipeline.stderr.String(), "secret-looking-output") +} + +func TestPipelineFailingBeforeHookAbortsWithItsExitCode(t *testing.T) { + pipeline := newProcessPipeline(t, &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{toolHook("plan-before", []string{"plan"}, "exit=23", "stderr=hook failed loudly")}, + AfterHooks: []schema.ComponentHook{toolHook("plan-after", []string{"plan"})}, + }) + err := pipeline.run(t) + require.ErrorIs(t, err, errUtils.ErrComponentHookFailed) + require.Equal(t, 23, resolveExitCode(err)) + require.Equal(t, []string{"init", "workspace", "plan-before"}, timeline(readHookLog(t, pipeline.log))) + require.Contains(t, pipeline.stderr.String(), "hook failed loudly") +} + +func TestPipelineErrorHookRunsOnMatchingFailureOnly(t *testing.T) { + for _, test := range []struct { + name string + pattern string + wantHook bool + }{ + {"matching failure text", "intentional", true}, + {"non matching failure text", "something else entirely", false}, + } { + t.Run(test.name, func(t *testing.T) { + pipeline := newProcessPipeline(t, &schema.ComponentExecution{ + AfterHooks: []schema.ComponentHook{toolHook("skipped-after", []string{"plan"})}, + ErrorHooks: []schema.ComponentHook{ + {Name: "recover", Commands: []string{"plan"}, Execute: []string{"hook-tool", "recover"}, OnErrors: []string{test.pattern}}, + {Name: "other-command", Commands: []string{"apply"}, Execute: []string{"hook-tool", "other"}, OnErrors: []string{".*"}}, + }, + }) + pipeline.info.ComponentEnvList = append(pipeline.info.ComponentEnvList, testEnvHookFail+"=plan") + err := pipeline.run(t) + require.Equal(t, 1, resolveExitCode(err), "the command's own exit code survives") + names := timeline(readHookLog(t, pipeline.log)) + if test.wantHook { + require.Equal(t, []string{"init", "workspace", "plan", "recover"}, names) + } else { + require.Equal(t, []string{"init", "workspace", "plan"}, names) + } + }) + } +} + +func TestPipelineDryRunNeverStartsAProcess(t *testing.T) { + pipeline := newProcessPipeline(t, &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{toolHook("plan-before", []string{"plan"})}, + AfterHooks: []schema.ComponentHook{toolHook("plan-after", []string{"plan"})}, + }) + pipeline.info.DryRun = true + require.NoError(t, pipeline.run(t)) + require.Empty(t, readHookLog(t, pipeline.log), "dry-run must not run hooks (or Terraform)") +} + +func TestPipelineCancellationStopsAHookAndEverythingAfterIt(t *testing.T) { + pipeline := newProcessPipeline(t, &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{toolHook("slow", []string{"plan"}, "sleep=60000"), toolHook("never", []string{"plan"})}, + AfterHooks: []schema.ComponentHook{{Name: "cleanup", Commands: []string{"plan"}, Execute: []string{"hook-tool", "cleanup"}, RunOnError: true}}, + }) + ctx, cancel := context.WithTimeout(t.Context(), 2*time.Minute) + defer cancel() + started := time.Now() + go func() { + // Cancel as soon as the slow hook has started; the safety deadline above only bounds a broken run. + for ctx.Err() == nil { + if slices.Contains(timeline(readHookLogQuietly(pipeline.log)), "slow") { + cancel() + return + } + time.Sleep(20 * time.Millisecond) + } + }() + err := pipeline.run(t, component.WithProcessContext(ctx)) + require.Error(t, err) + require.Less(t, time.Since(started), 50*time.Second, "the sleeping hook must be stopped, not waited for") + names := timeline(readHookLog(t, pipeline.log)) + require.Equal(t, "slow", names[len(names)-1], "nothing may start after the canceled hook: %v", names) +} diff --git a/pkg/component/terraform/execute_hooks_test.go b/pkg/component/terraform/execute_hooks_test.go new file mode 100644 index 00000000000..b65b90fa7ee --- /dev/null +++ b/pkg/component/terraform/execute_hooks_test.go @@ -0,0 +1,461 @@ +package terraform + +import ( + "context" + "encoding/json" + "errors" + "io" + "os" + "path/filepath" + "strings" + "sync" + "testing" + + "github.com/stretchr/testify/require" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/component" + "github.com/cloudposse/atmos/pkg/process" + "github.com/cloudposse/atmos/pkg/schema" +) + +// shellCall is one invocation of the host's process facility. +type shellCall struct { + command string + args []string + dir string + env []string + dryRun bool + options component.ExecutionOptions +} + +type hookContextKey struct{} + +// fakeShell records every process the executor starts and lets a test script the outcome. +type fakeShell struct { + mu sync.Mutex + calls []shellCall + // run decides the result and may write to the option writers like a real process would. + run func(call *shellCall) error +} + +//nolint:gocritic // The signature is fixed by Services.ExecuteShellCommand. +func (f *fakeShell) execute(_ schema.AtmosConfiguration, command string, args []string, dir string, env []string, dryRun bool, _ string, opts ...component.ExecutionOption) error { + call := shellCall{command: command, args: append([]string(nil), args...), dir: dir, env: append([]string(nil), env...), dryRun: dryRun} + for _, opt := range opts { + opt(&call.options) + } + f.mu.Lock() + f.calls = append(f.calls, call) + f.mu.Unlock() + if f.run == nil { + return nil + } + return f.run(&call) +} + +// names lists " " per call, which is enough to read the timeline. +func (f *fakeShell) names() []string { + var names []string + for i := range f.calls { + call := &f.calls[i] + name := filepath.Base(call.command) + if len(call.args) > 0 { + name += " " + call.args[0] + } + names = append(names, name) + } + return names +} + +func hookExecutor(shell *fakeShell) *Executor { + return NewExecutor(Services{OperationServices: nil, ExecuteShellCommand: shell.execute}) +} + +func hookInfo(subCommand string, execution *schema.ComponentExecution) *schema.ConfigAndStacksInfo { + return &schema.ConfigAndStacksInfo{ + SubCommand: subCommand, + Command: "terraform", + ComponentEnvList: []string{"BASE=1", "PATH=/toolchain/bin"}, + ComponentExecution: execution, + } +} + +func execHook(name string, commands ...string) schema.ComponentHook { + return schema.ComponentHook{Name: name, Commands: commands, Execute: []string{"hook-" + name, "arg"}} +} + +func TestExtraArgumentsPosition(t *testing.T) { + for _, test := range []struct { + name string + info schema.ConfigAndStacksInfo + args []string + want int + }{ + {"plain command", schema.ConfigAndStacksInfo{SubCommand: "plan"}, []string{"plan", "-out", "p"}, 1}, + {"state subcommand", schema.ConfigAndStacksInfo{SubCommand: "state", SubCommand2: "list"}, []string{"state", "list", "x"}, 2}, + {"second subcommand not in arguments", schema.ConfigAndStacksInfo{SubCommand: "state", SubCommand2: "list"}, []string{"state", "-x"}, 1}, + {"no arguments", schema.ConfigAndStacksInfo{SubCommand: "plan"}, nil, 0}, + } { + t.Run(test.name, func(t *testing.T) { + require.Equal(t, test.want, extraArgumentsPosition(&test.info, test.args)) + }) + } +} + +func TestRunExecutionPhaseWithoutExecutionIsUnchanged(t *testing.T) { + shell := &fakeShell{} + e := hookExecutor(shell) + info := hookInfo("plan", nil) + called := 0 + phase := &executionPhase{command: "plan", args: []string{"plan", "-x"}, insertAt: 1, componentPath: t.TempDir()} + err := e.runExecutionPhase(&schema.AtmosConfiguration{}, info, phase, nil, func(got *schema.ConfigAndStacksInfo, args []string, _ []component.ExecutionOption) error { + called++ + require.Same(t, info, got) + require.Equal(t, []string{"plan", "-x"}, args) + return nil + }) + require.NoError(t, err) + require.Equal(t, 1, called) + require.Empty(t, shell.calls) +} + +func TestMainCommandAppliesExtraArgumentsAndEnvironment(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "required.tfvars"), nil, 0o600)) + shell := &fakeShell{} + e := hookExecutor(shell) + info := hookInfo("plan", &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{ + {Commands: []string{"plan"}, Arguments: []string{"-lock-timeout=1m"}, RequiredVarFiles: []string{"required.tfvars"}, Environment: map[string]string{"TF_LOG": "INFO"}}, + {Commands: []string{"apply"}, Arguments: []string{"-only-for-apply"}, Environment: map[string]string{"ONLY_APPLY": "1"}}, + }}) + require.NoError(t, e.runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"plan", "-out", "p.tfplan"}, dir)) + + require.Len(t, shell.calls, 1) + call := shell.calls[0] + require.Equal(t, []string{"plan", "-lock-timeout=1m", "-var-file=required.tfvars", "-out", "p.tfplan"}, call.args) + require.Equal(t, dir, call.dir) + require.Equal(t, []string{"BASE=1", "PATH=/toolchain/bin", "TF_LOG=INFO"}, call.env) + require.Equal(t, []string{"BASE=1", "PATH=/toolchain/bin"}, info.ComponentEnvList, "phase environment must not leak into the invocation") +} + +func TestMainCommandMissingRequiredVarFileFailsBeforeAnyProcess(t *testing.T) { + shell := &fakeShell{} + info := hookInfo("plan", &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, RequiredVarFiles: []string{"missing.tfvars"}}}, + BeforeHooks: []schema.ComponentHook{execHook("before", "plan")}, + }) + err := hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"plan"}, t.TempDir()) + require.ErrorIs(t, err, errUtils.ErrComponentVarFileMissing) + require.Empty(t, shell.calls) +} + +func TestHooksRunWithTheInvocationEnvironmentAndDirectory(t *testing.T) { + dir := t.TempDir() + absolute := t.TempDir() + shell := &fakeShell{} + e := hookExecutor(shell) + ctx := context.WithValue(t.Context(), hookContextKey{}, "marker") + info := hookInfo("plan", &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, Environment: map[string]string{"FROM_EXTRA": "1"}}}, + BeforeHooks: []schema.ComponentHook{ + execHook("default-dir", "plan"), + {Name: "relative", Commands: []string{"plan"}, Execute: []string{"hook-relative"}, WorkingDirectory: "sub"}, + {Name: "absolute", Commands: []string{"plan"}, Execute: []string{"hook-absolute"}, WorkingDirectory: absolute}, + }, + }) + require.NoError(t, e.runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"plan"}, dir, component.WithProcessContext(ctx), component.WithEnvironment([]string{"IDENTITY=1"}))) + + require.Equal(t, []string{"hook-default-dir arg", "hook-relative", "hook-absolute", "terraform plan"}, shell.names()) + for _, call := range shell.calls[:3] { + require.False(t, call.dryRun) + require.Equal(t, ctx, call.options.Ctx, "hooks must share the command's cancellation") + require.Equal(t, []string{"IDENTITY=1"}, call.options.ProcessEnv, "hooks must share the identity's process environment") + require.Subset(t, call.env, []string{"BASE=1", "PATH=/toolchain/bin", "FROM_EXTRA=1", "TG_CTX_TF_PATH=terraform", "TG_CTX_COMMAND=plan"}) + } + require.Equal(t, dir, shell.calls[0].dir) + require.Equal(t, filepath.Join(dir, "sub"), shell.calls[1].dir) + require.Equal(t, absolute, shell.calls[2].dir) + require.Contains(t, shell.calls[0].env, "TG_CTX_HOOK_NAME=default-dir") + require.Contains(t, shell.calls[1].env, "TG_CTX_HOOK_NAME=relative") + require.NotContains(t, shell.calls[3].env, "TG_CTX_HOOK_NAME=relative", "hook context variables belong to hooks only") +} + +func TestHookOutputGoesToTheUIStreamNotTheDataStream(t *testing.T) { + var data, ui strings.Builder + streams := process.Streams{Stdout: &data, Stderr: &ui} + shell := &fakeShell{run: func(call *shellCall) error { + if call.command == "hook-loud" || call.command == "hook-quiet" { + _, _ = io.WriteString(call.options.StdoutOverride, call.command+" says hi\n") + _, _ = io.WriteString(call.options.StdoutCapture, call.command+" says hi\n") + } + return nil + }} + info := hookInfo("plan", &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{ + {Name: "loud", Commands: []string{"plan"}, Execute: []string{"hook-loud"}}, {Name: "quiet", Commands: []string{"plan"}, Execute: []string{"hook-quiet"}, SuppressStdout: true}, + }}) + var commandCapture strings.Builder + require.NoError(t, hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"plan"}, t.TempDir(), + component.WithProcessStreams(streams), component.WithStdoutCapture(&commandCapture))) + + require.Equal(t, "hook-loud says hi\n", ui.String(), "visible hook stdout must reach the UI channel") + require.Empty(t, data.String(), "hook output must never reach the data channel") + require.Empty(t, commandCapture.String(), "hook output must not enter the command's capture") + require.Equal(t, io.Discard, shell.calls[1].options.StdoutOverride, "suppress_stdout discards the display copy") +} + +func TestFailingBeforeHookAbortsTheCommandAndSurfacesItsExitCode(t *testing.T) { + shell := &fakeShell{run: func(call *shellCall) error { + if call.command == "hook-b1" { + return errUtils.ExitCodeError{Code: 23} + } + return nil + }} + info := hookInfo("apply", &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{execHook("b1", "apply"), execHook("b2", "apply")}, + AfterHooks: []schema.ComponentHook{execHook("a1", "apply")}, + }) + err := hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"apply"}, t.TempDir()) + require.ErrorIs(t, err, errUtils.ErrComponentHookFailed) + require.Equal(t, 23, resolveExitCode(err)) + require.Equal(t, []string{"hook-b1 arg"}, shell.names(), "neither the command nor later hooks may run") +} + +func TestAfterHooksFollowRunOnErrorWhenTheCommandFails(t *testing.T) { + shell := &fakeShell{run: func(call *shellCall) error { + if call.command == "terraform" { + return errUtils.ExitCodeError{Code: 1} + } + return nil + }} + always := execHook("always", "plan") + always.RunOnError = true + info := hookInfo("plan", &schema.ComponentExecution{AfterHooks: []schema.ComponentHook{execHook("skipped", "plan"), always}}) + err := hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"plan"}, t.TempDir()) + require.Equal(t, 1, resolveExitCode(err)) + require.Equal(t, []string{"terraform plan", "hook-always arg"}, shell.names()) +} + +func TestErrorHookMatchesTheFailedCommandOutput(t *testing.T) { + for _, test := range []struct { + name string + patterns []string + stderr string + want []string + }{ + {"matching output runs the hook", []string{"AccessDenied"}, "Error: AccessDenied", []string{"terraform apply", "hook-recover arg"}}, + {"non matching output does not", []string{"AccessDenied"}, "Error: other", []string{"terraform apply"}}, + } { + t.Run(test.name, func(t *testing.T) { + shell := &fakeShell{run: func(call *shellCall) error { + if call.command != "terraform" { + return nil + } + _, _ = io.WriteString(call.options.StderrCapture, test.stderr) + return errUtils.ExitCodeError{Code: 1} + }} + info := hookInfo("apply", &schema.ComponentExecution{ErrorHooks: []schema.ComponentHook{ + {Name: "recover", Commands: []string{"apply"}, Execute: []string{"hook-recover", "arg"}, OnErrors: test.patterns}, + }}) + var callerCapture strings.Builder + err := hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"apply"}, t.TempDir(), component.WithStderrCapture(&callerCapture)) + require.Equal(t, 1, resolveExitCode(err)) + require.Equal(t, test.want, shell.names()) + require.Equal(t, test.stderr, callerCapture.String(), "the caller's own capture must keep receiving the command output") + }) + } +} + +func TestDryRunAppliesArgumentsButNeverRunsHooks(t *testing.T) { + shell := &fakeShell{} + info := hookInfo("plan", &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, Arguments: []string{"-lock=false"}}}, + BeforeHooks: []schema.ComponentHook{execHook("b", "plan")}, + AfterHooks: []schema.ComponentHook{execHook("a", "plan")}, + ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"plan"}, Execute: []string{"hook-e"}, OnErrors: []string{".*"}}}, + }) + info.DryRun = true + require.NoError(t, hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"plan"}, t.TempDir())) + require.Equal(t, []string{"terraform plan"}, shell.names()) + require.Equal(t, []string{"plan", "-lock=false"}, shell.calls[0].args) + require.True(t, shell.calls[0].dryRun) +} + +func TestCancellationStopsRemainingHooksAndTheCommand(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + shell := &fakeShell{run: func(call *shellCall) error { + if call.command == "hook-b1" { + cancel() + return context.Canceled + } + return nil + }} + always := execHook("always", "plan") + always.RunOnError = true + info := hookInfo("plan", &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{execHook("b1", "plan"), always}, + AfterHooks: []schema.ComponentHook{always}, + }) + err := hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"plan"}, t.TempDir(), component.WithProcessContext(ctx)) + require.ErrorIs(t, err, context.Canceled) + require.Equal(t, []string{"hook-b1 arg"}, shell.names()) +} + +func TestDestroyCommandNameMatchesApplyDestroy(t *testing.T) { + shell := &fakeShell{} + info := hookInfo("apply", &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{execHook("for-apply", "apply"), execHook("for-destroy", "destroy")}, + ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"destroy"}, Arguments: []string{"-parallelism=1"}}}, + }) + require.NoError(t, hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"apply", "-destroy", "-auto-approve"}, t.TempDir())) + require.Equal(t, []string{"hook-for-destroy arg", "terraform apply"}, shell.names()) + require.Equal(t, []string{"apply", "-parallelism=1", "-destroy", "-auto-approve"}, shell.calls[1].args) +} + +func TestSavedPlanApplyDropsVariableArguments(t *testing.T) { + shell := &fakeShell{} + info := hookInfo("apply", &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{ + {Commands: []string{"apply"}, Arguments: []string{"-var=a=b", "-parallelism=1"}, RequiredVarFiles: []string{"never-needed.tfvars"}}, + }}) + info.UseTerraformPlan = true + require.NoError(t, hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"apply", "-auto-approve", "plan.tfplan"}, t.TempDir())) + require.Equal(t, []string{"apply", "-parallelism=1", "-auto-approve", "plan.tfplan"}, shell.calls[0].args) +} + +// Hooks and extra arguments are matched per phase. The implicit init is "init" even when the +// user asked for plan, so plan hooks must not fire for it, and init hooks must not fire for plan. +func TestImplicitInitMatchesInitNotTheRequestedCommand(t *testing.T) { + shell := &fakeShell{} + e := hookExecutor(shell) + execution := &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{ + {Commands: []string{"init"}, Arguments: []string{"-input=false"}, Environment: map[string]string{"INIT_ONLY": "1"}}, + {Commands: []string{"plan"}, Arguments: []string{"-lock=false"}, Environment: map[string]string{"PLAN_ONLY": "1"}}, + }, + BeforeHooks: []schema.ComponentHook{execHook("init-before", "init"), execHook("plan-before", "plan")}, + AfterHooks: []schema.ComponentHook{execHook("init-after", "init"), execHook("plan-after", "plan")}, + } + info := hookInfo("plan", execution) + dir := t.TempDir() + + _, err := e.runTerraformInitSubprocess(&schema.AtmosConfiguration{}, info, dir, []string{"init", "-reconfigure"}) + require.NoError(t, err) + require.NoError(t, e.runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"plan", "-out", "p"}, dir)) + + require.Equal(t, []string{ + "hook-init-before arg", "terraform init", "hook-init-after arg", + "hook-plan-before arg", "terraform plan", "hook-plan-after arg", + }, shell.names()) + initCall, planCall := shell.calls[1], shell.calls[4] + require.Equal(t, []string{"init", "-input=false", "-reconfigure"}, initCall.args) + require.Contains(t, initCall.env, "INIT_ONLY=1") + require.NotContains(t, initCall.env, "PLAN_ONLY=1") + require.Equal(t, []string{"plan", "-lock=false", "-out", "p"}, planCall.args) + require.Contains(t, planCall.env, "PLAN_ONLY=1") + require.NotContains(t, planCall.env, "INIT_ONLY=1") + require.Contains(t, shell.calls[0].env, "TG_CTX_COMMAND=init") + require.Contains(t, shell.calls[3].env, "TG_CTX_COMMAND=plan") +} + +func TestHooksListedOnlyForOtherCommandsNeverFire(t *testing.T) { + shell := &fakeShell{run: func(call *shellCall) error { + if call.command == "terraform" { + return errUtils.ExitCodeError{Code: 1} + } + return nil + }} + e := hookExecutor(shell) + always := execHook("always", "apply") + always.RunOnError = true + info := hookInfo("plan", &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"apply"}, Arguments: []string{"-never"}}}, + BeforeHooks: []schema.ComponentHook{execHook("b", "apply")}, + AfterHooks: []schema.ComponentHook{always}, + ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"apply"}, Execute: []string{"hook-e"}, OnErrors: []string{".*"}}}, + }) + dir := t.TempDir() + _, initErr := e.runTerraformInitSubprocess(&schema.AtmosConfiguration{}, info, dir, []string{"init"}) + require.Error(t, initErr) + require.Error(t, e.runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"plan"}, dir)) + require.Equal(t, []string{"terraform init", "terraform plan"}, shell.names()) + require.Equal(t, []string{"init"}, shell.calls[0].args) + require.Equal(t, []string{"plan"}, shell.calls[1].args) +} + +func TestWorkspaceSelectionNeverRunsHooksOrExtraArguments(t *testing.T) { + shell := &fakeShell{run: func(*shellCall) error { return nil }} + e := hookExecutor(shell) + info := hookInfo("plan", &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan", "init", "workspace"}, Arguments: []string{"-never"}, Environment: map[string]string{"NEVER": "1"}}}, + BeforeHooks: []schema.ComponentHook{execHook("b", "plan", "init", "workspace")}, + AfterHooks: []schema.ComponentHook{execHook("a", "plan", "init", "workspace")}, + }) + info.TerraformWorkspace = "dev" + require.NoError(t, e.runWorkspaceSetup(&schema.AtmosConfiguration{}, info, t.TempDir())) + require.Equal(t, []string{"terraform workspace"}, shell.names()) + require.Equal(t, []string{"workspace", "select", "-or-create", "dev"}, shell.calls[0].args) + require.NotContains(t, shell.calls[0].env, "NEVER=1") +} + +func TestUnknownHookFailureIsNotMistakenForSuccess(t *testing.T) { + shell := &fakeShell{run: func(call *shellCall) error { + if call.command == "hook-b" { + return errors.New("executable file not found") + } + return nil + }} + info := hookInfo("plan", &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{{Name: "b", Commands: []string{"plan"}, Execute: []string{"hook-b"}}}}) + err := hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"plan"}, t.TempDir()) + require.ErrorIs(t, err, errUtils.ErrComponentHookFailed) + require.ErrorContains(t, err, "executable file not found") + require.Equal(t, []string{"hook-b"}, shell.names()) +} + +// fakeConfigurationRuntime is a provider session that returns a fixed preparation. +type fakeConfigurationRuntime struct { + prepared *schema.PreparedComponentConfiguration +} + +func (fakeConfigurationRuntime) Discover(context.Context) (map[string]map[string]any, error) { + return nil, nil +} + +func (f fakeConfigurationRuntime) Prepare(context.Context, *schema.ConfigAndStacksInfo) (*schema.PreparedComponentConfiguration, error) { + return f.prepared, nil +} + +func TestPrepareConfigurationHandsExecutionToThePipeline(t *testing.T) { + executable, err := os.Executable() + require.NoError(t, err) + execution := &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{execHook("b", "plan")}} + config := &schema.AtmosConfiguration{ComponentConfigurations: fakeConfigurationRuntime{prepared: &schema.PreparedComponentConfiguration{ + Source: "./module", BackendType: "local", Backend: map[string]any{}, Workspace: "default", Execution: execution, + }}} + info := &schema.ConfigAndStacksInfo{ + SubCommand: "plan", Command: executable, ComponentSection: map[string]any{}, + ComponentSettingsSection: map[string]any{"configuration": true}, + } + prepared, err := prepareConfiguration(t.Context(), config, info) + require.NoError(t, err) + require.NotNil(t, prepared) + require.Equal(t, execution, info.ComponentExecution) +} + +// A reviewed plan carries its hooks and extra arguments in the private companion, and the +// companion decoder rejects unknown fields, so the new data must survive that exact decode. +func TestSavedPlanBindingKeepsExecution(t *testing.T) { + plan := configurationPlan{Version: 1, Prepared: &schema.PreparedComponentConfiguration{ + Execution: &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"apply"}, Environment: map[string]string{"K": "v"}}}, + ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"apply"}, Execute: []string{"tool"}, OnErrors: []string{"x"}}}, + }, + }} + data, err := json.Marshal(plan) + require.NoError(t, err) + var decoded configurationPlan + decoder := json.NewDecoder(strings.NewReader(string(data))) + decoder.UseNumber() + decoder.DisallowUnknownFields() + require.NoError(t, decoder.Decode(&decoded)) + require.Equal(t, plan.Prepared.Execution, decoded.Prepared.Execution) +} diff --git a/pkg/component/terraform/execute_init.go b/pkg/component/terraform/execute_init.go index e7ee95c41c4..3f949b8a75f 100644 --- a/pkg/component/terraform/execute_init.go +++ b/pkg/component/terraform/execute_init.go @@ -177,6 +177,10 @@ func (e *Executor) executeTerraformInitForced(atmosConfig *schema.AtmosConfigura // 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) { + if _, owned := info.ComponentSettingsSection["configuration"]; owned && usesConfigurationPlan(info) { + // Saved plans retain their reviewed modules and generated files. + return componentPath, nil + } _, isWorkdir := info.ComponentSection[provWorkdir.WorkdirPathKey].(string) if !isWorkdir && !info.DryRun { cleanTerraformWorkspace(*atmosConfig, componentPath) @@ -247,25 +251,30 @@ func (e *Executor) runTerraformInitSubprocess(atmosConfig *schema.AtmosConfigura 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 { - // 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, - gatePhase: subcommandInit, - subCommand: subcommandInit, - redirectStdErr: info.RedirectStdErr, - shellOpts: o, - }) - }, - captureOpts..., - ) + // An implicit init is its own phase: provider-supplied hooks and extra arguments match the + // command "init" here, never the command the user asked for. + phase := &executionPhase{command: subcommandInit, args: args, insertAt: 1, componentPath: componentPath} + err := e.runExecutionPhase(atmosConfig, info, phase, captureOpts, func(phaseInfo *schema.ConfigAndStacksInfo, phaseArgs []string, phaseOpts []component.ExecutionOption) error { + return ExecuteShellCommandWithRetry( + atmosConfig, + phaseInfo, + "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, phaseInfo, &streamingExecRequest{ + componentPath: componentPath, + args: phaseArgs, + gatePhase: subcommandInit, + subCommand: subcommandInit, + redirectStdErr: info.RedirectStdErr, + shellOpts: o, + }) + }, + phaseOpts..., + ) + }) return buf.String(), err } diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index b643d0f8d15..2d47fef18cb 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -26,13 +26,14 @@ import ( // 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 + preparedConfiguration *schema.PreparedComponentConfiguration + 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: @@ -59,9 +60,15 @@ func (e *Executor) prepareComponentExecution( return nil, err } - componentPath, err := e.resolveAndProvisionComponentPath(ctx, writers, atmosConfig, info) - if err != nil { - return nil, err + var componentPath string + if prepared != nil && usesConfigurationPlan(info) { + // A reviewed plan uses the retained workdir; provisioning could replace its modules. + componentPath = ConstructTerraformComponentWorkingDir(atmosConfig, info) + } else { + componentPath, err = e.resolveAndProvisionComponentPath(ctx, writers, atmosConfig, info) + if err != nil { + return nil, err + } } if prepared != nil && !info.DryRun { @@ -95,12 +102,13 @@ func (e *Executor) prepareComponentExecution( } return &componentExecContext{ - componentPath: componentPath, - varFile: varFile, - testVarFile: testVarFile, - planFile: planFile, - workingDir: workingDir, - tenv: tenv, + preparedConfiguration: prepared, + componentPath: componentPath, + varFile: varFile, + testVarFile: testVarFile, + planFile: planFile, + workingDir: workingDir, + tenv: tenv, }, nil } @@ -260,7 +268,7 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every }) info.ExecMetadataRawOutput = combineExecMetadataOutput(&execMetadataStdoutBuf, &execMetadataStderrBuf) if mainErr == nil { - return nil + return recordConfigurationPlan(component.ContextFromOptions(opts...), atmosConfig, info, execCtx, allArgsAndFlags) } return e.recoverFromInitRequired(atmosConfig, info, execCtx, allArgsAndFlags, componentPath, uploadStatusFlag, diff --git a/pkg/component/terraform/state_configuration_test.go b/pkg/component/terraform/state_configuration_test.go new file mode 100644 index 00000000000..797ae1e0026 --- /dev/null +++ b/pkg/component/terraform/state_configuration_test.go @@ -0,0 +1,41 @@ +package terraform + +import ( + "context" + "errors" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + componentpkg "github.com/cloudposse/atmos/pkg/component" + "github.com/cloudposse/atmos/pkg/schema" +) + +// failingStateRuntime owns the component but cannot identify its backend. +type failingStateRuntime struct { + schema.ComponentConfigurationRuntime + err error +} + +func (r failingStateRuntime) ResolveState(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return nil, r.err +} + +func TestGetTerraformStateStopsWhenProviderCannotResolveBackend(t *testing.T) { + failure := errors.New("provider could not locate state") + executor := testExecutor(t) + executor.services.ExecuteDescribeComponent = func(params *componentpkg.ExecuteDescribeComponentParams) (map[string]any, error) { + assert.Equal(t, "network", params.Component) + assert.Equal(t, "live", params.Stack) + return map[string]any{ + "settings": map[string]any{"configuration": true}, + "backend": map[string]any{"path": "must-not-be-read"}, + }, nil + } + config := &schema.AtmosConfiguration{ComponentConfigurations: failingStateRuntime{err: failure}} + + result, err := executor.GetTerraformState(config, "!terraform.state", "live", "network", "vpc_id", true, nil, nil) + require.ErrorIs(t, err, failure, "the provider's failure is returned as is, not retried against a guessed backend") + require.Nil(t, result) +} diff --git a/pkg/component/terraform/state_utils.go b/pkg/component/terraform/state_utils.go index a0121c7c1e1..04724e07480 100644 --- a/pkg/component/terraform/state_utils.go +++ b/pkg/component/terraform/state_utils.go @@ -1,6 +1,7 @@ package terraform import ( + "context" "fmt" "strings" "sync" @@ -202,6 +203,12 @@ func (e *Executor) GetTerraformState( return nil, er } + state, err := tfoutput.ResolveConfigurationState(context.Background(), atmosConfig, &tfoutput.ConfigurationStateRequest{Sections: componentSections, Component: component, Stack: stack, AuthContext: resolvedAuthContext, AuthManagers: []any{resolvedAuthMgr}}) + if err != nil { + return nil, err + } + componentSections = tfoutput.ConfigurationStateSections(componentSections, state) + // Check if the component in the stack is configured with the 'static' remote state backend, in which case get the // `output` from the static remote state instead of executing `terraform output`. remoteStateBackendStaticTypeOutputs := GetComponentRemoteStateBackendStaticType(&componentSections) diff --git a/pkg/component/terraform/testmain_test.go b/pkg/component/terraform/testmain_test.go index 0c89ca204e8..fd1fc25ec7f 100644 --- a/pkg/component/terraform/testmain_test.go +++ b/pkg/component/terraform/testmain_test.go @@ -72,6 +72,17 @@ func TestMain(m *testing.M) { ui.InitFormatter(ioCtx) } + // A toolchain probe records how it was invoked (executable, directory, arguments, and + // environment) and exits 0. See configuration_toolchain_test.go. + if probe := os.Getenv(testEnvToolchainProbe); probe != "" { + os.Exit(runToolchainProbeForTest(probe)) + } + + // A hook child records its invocation and behaves as scripted. See execute_hooks_process_test.go. + if os.Getenv(testEnvHookChild) == "1" { + os.Exit(runHookChildForTest()) + } + if os.Getenv("_ATMOS_TEST_PLAN_DIFF_TERRAFORM") == "1" { os.Exit(runPlanDiffTerraformForTest()) } diff --git a/pkg/component/workdir_path.go b/pkg/component/workdir_path.go index 81fd9905ef6..037f7883af2 100644 --- a/pkg/component/workdir_path.go +++ b/pkg/component/workdir_path.go @@ -334,6 +334,16 @@ func resolveDryRunComponentPath(atmosConfig *schema.AtmosConfiguration, info *sc return fallback, exists, err } +// ResolveRetainedWorkdir records, without provisioning anything, the directory an earlier run +// populated and returns it. Reviewed saved plans use it to find their retained workdir in a new +// process, where only provisioning would otherwise set provWorkdir.WorkdirPathKey. +func ResolveRetainedWorkdir(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentType, fallbackComponentPath string) (string, error) { + defer perf.Track(atmosConfig, "component.ResolveRetainedWorkdir")() + + path, _, err := resolveDryRunComponentPath(atmosConfig, info, componentType, fallbackComponentPath) + return path, err +} + func dryRunSourceTarget(config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentType string) (string, error) { target, err := provSource.ResolveTarget(config, componentType, info.FinalComponent, info.ComponentSection) if err != nil { diff --git a/pkg/component/workdir_path_test.go b/pkg/component/workdir_path_test.go index 972f2f8e8de..c39f0acc3a0 100644 --- a/pkg/component/workdir_path_test.go +++ b/pkg/component/workdir_path_test.go @@ -602,6 +602,34 @@ func TestProvisionAndResolveComponentPathDryRunDoesNotCreateSourceOrWorkdir(t *t } } +func TestResolveRetainedWorkdirRecordsPathWithoutProvisioning(t *testing.T) { + base := t.TempDir() + fallback := filepath.Join(base, "components", "vpc") + config := &schema.AtmosConfiguration{BasePath: base} + section := map[string]any{ + "component": "vpc", "atmos_component": "vpc", "atmos_stack": "dev", + "provision": map[string]any{"workdir": map[string]any{"enabled": true}}, + "source": map[string]any{"uri": "./module"}, + } + info := &schema.ConfigAndStacksInfo{FinalComponent: "vpc", Stack: "dev", ComponentSection: section} + + path, err := ResolveRetainedWorkdir(config, info, cfg.TerraformComponentType, fallback) + + require.NoError(t, err) + assert.Equal(t, path, section[provWorkdir.WorkdirPathKey], "the retained workdir becomes the component's working directory") + assert.Contains(t, filepath.ToSlash(path), "/.workdir/terraform/dev-vpc-") + entries, err := os.ReadDir(base) + require.NoError(t, err) + assert.Empty(t, entries, "locating a retained workdir must not create or download anything") + + // Without a workdir the component keeps its own directory and nothing is recorded. + plain := &schema.ConfigAndStacksInfo{FinalComponent: "vpc", Stack: "dev", ComponentSection: map[string]any{}} + path, err = ResolveRetainedWorkdir(config, plain, cfg.TerraformComponentType, base) + require.NoError(t, err) + assert.Equal(t, base, path) + assert.NotContains(t, plain.ComponentSection, provWorkdir.WorkdirPathKey) +} + // TestProvisionAndResolveComponentPath_NoSourceReturnsFallback verifies the // short-circuit when no source is configured: the orchestrator must skip // AutoProvisionSource entirely and report whether the fallback exists. diff --git a/pkg/dependencies/environment.go b/pkg/dependencies/environment.go index 37c3fe74766..4eb2484dc83 100644 --- a/pkg/dependencies/environment.go +++ b/pkg/dependencies/environment.go @@ -6,6 +6,7 @@ import ( "os" execPkg "os/exec" "path/filepath" + "slices" "strings" log "github.com/cloudposse/atmos/pkg/logger" @@ -435,7 +436,9 @@ func resolveBinaryPaths(env *ToolchainEnvironment, cfg *envConfig, deps map[stri } seenDir := make(map[string]struct{}) - for tool, version := range deps { + // Tool name order keeps ToolchainDirs and same-named binaries deterministic. + for _, tool := range slices.Sorted(maps.Keys(deps)) { + version := deps[tool] owner, repo, err := resolveFunc(tool) if err != nil { log.Debug("Could not resolve tool for toolchain environment", "tool", tool, "error", err) diff --git a/pkg/dependencies/environment_order_test.go b/pkg/dependencies/environment_order_test.go new file mode 100644 index 00000000000..c96cec8cf9a --- /dev/null +++ b/pkg/dependencies/environment_order_test.go @@ -0,0 +1,33 @@ +package dependencies + +import ( + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cloudposse/atmos/pkg/schema" +) + +// The same dependencies must always produce the same PATH. Tools can provide executables +// with the same name, so directory order decides which one runs and must not depend on map +// iteration order. +func TestToolchainPathOrderIsDeterministic(t *testing.T) { + config, _ := defaultsFixture(t, "") + config.Toolchain.Install = schema.ToolchainInstallNever + deps := map[string]string{} + var expected []string + for _, name := range []string{"alpha", "bravo", "charlie", "delta", "echo", "foxtrot"} { + config.Toolchain.Aliases[name] = "acme/" + name + expected = append(expected, filepath.Dir(installedDefaultBinary(t, config, "acme", name, "1.0.0"))) + deps[name] = "1.0.0" + } + + for range 50 { + env, err := NewEnvironmentFromDeps(config, deps) + require.NoError(t, err) + require.Equal(t, expected, env.ToolchainDirs(), "directories follow tool name order") + require.True(t, strings.HasPrefix(env.PATH(), strings.Join(expected, string(filepath.ListSeparator))), "PATH must list them in the same order: %s", env.PATH()) + } +} diff --git a/pkg/dependencies/installer.go b/pkg/dependencies/installer.go index 9add36f059b..752779190dd 100644 --- a/pkg/dependencies/installer.go +++ b/pkg/dependencies/installer.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "maps" "os" execPkg "os/exec" "path/filepath" @@ -627,7 +628,10 @@ func collectToolchainDirs(locator *toolchain.Installer, resolver toolchain.ToolR var paths []string seen := make(map[string]struct{}) - for tool, version := range dependencies { + // Visit tools in name order: directory order decides which same-named executable runs, + // so it must not depend on map iteration order. + for _, tool := range slices.Sorted(maps.Keys(dependencies)) { + version := dependencies[tool] // Resolve tool to owner/repo using the shared resolver. owner, repo, err := resolver.Resolve(tool) if err != nil { diff --git a/pkg/edition/journal.yaml b/pkg/edition/journal.yaml index 2d9da39e750..2c39170a625 100644 --- a/pkg/edition/journal.yaml +++ b/pkg/edition/journal.yaml @@ -2,6 +2,13 @@ # Append changes here; keep old/new in the configuration field's type. # Quote dates and string values such as "true" and "false". # The CLI embeds this file; the website generates its index from the same file. +- date: "2026-10-29" + key: list.instances.engine_columns + kind: behavior + old: show Component and Stack as the default instance columns + new: show Component, Stack, Type, and Engine as the default instance columns + description: Instance inventory identifies component type and execution engine; explicit column overrides remain authoritative. + ref: https://github.com/cloudposse/atmos/pull/3576 - date: "2026-10-28" key: components.source.working_directory_precedence kind: behavior diff --git a/pkg/list/column/required_sections.go b/pkg/list/column/required_sections.go index 4c3707f8488..356c5c31b09 100644 --- a/pkg/list/column/required_sections.go +++ b/pkg/list/column/required_sections.go @@ -33,7 +33,7 @@ var derivedFields = map[string]struct{}{ "atmos_component_type": {}, "enabled": {}, "locked": {}, "abstract": {}, "file": {}, "name": {}, "workflow": {}, "description": {}, "steps": {}, "components": {}, "atmos_vendor_type": {}, "atmos_vendor_file": {}, "atmos_vendor_target": {}, - "status": {}, "status_text": {}, "component_type": {}, "component_folder": {}, + "engine": {}, "status": {}, "status_text": {}, "component_type": {}, "component_folder": {}, "component_base": {}, "inherits": {}, "type": {}, "tags": {}, "labels": {}, "stack_count": {}, } diff --git a/pkg/list/extract/instances.go b/pkg/list/extract/instances.go index 87a64a09d73..894cb8e9992 100644 --- a/pkg/list/extract/instances.go +++ b/pkg/list/extract/instances.go @@ -15,6 +15,7 @@ func Instances(instances []schema.Instance) []map[string]any { "component": instance.Component, "stack": instance.Stack, "component_type": instance.ComponentType, + "engine": instance.Engine, "vars": instance.Vars, "settings": instance.Settings, "env": instance.Env, diff --git a/pkg/list/extract/instances_test.go b/pkg/list/extract/instances_test.go index 04d34bea5cf..9a127445e36 100644 --- a/pkg/list/extract/instances_test.go +++ b/pkg/list/extract/instances_test.go @@ -42,6 +42,7 @@ func TestInstances(t *testing.T) { "component": "vpc", "stack": "plat-ue2-dev", "component_type": "terraform", + "engine": "", "vars": map[string]any{ "tenant": "plat", "environment": "ue2", @@ -89,6 +90,7 @@ func TestInstances(t *testing.T) { "component": "vpc", "stack": "plat-ue2-dev", "component_type": "terraform", + "engine": "", "vars": map[string]any{ "tenant": "plat", "stage": "dev", @@ -102,6 +104,7 @@ func TestInstances(t *testing.T) { "component": "eks", "stack": "plat-ue2-prod", "component_type": "terraform", + "engine": "", "vars": map[string]any{ "tenant": "plat", "stage": "prod", diff --git a/pkg/list/extract/metadata.go b/pkg/list/extract/metadata.go index 112e8cbc71b..90752ff467c 100644 --- a/pkg/list/extract/metadata.go +++ b/pkg/list/extract/metadata.go @@ -204,6 +204,7 @@ func buildMetadataMap(instance *schema.Instance, metadata *instanceMetadata) map "stack": instance.Stack, "component": instance.Component, "component_type": instance.ComponentType, + "engine": instance.Engine, "component_folder": metadata.componentFolder, // The actual component folder name. "type": metadata.metadataType, "enabled": metadata.enabled, diff --git a/pkg/list/instance_engine.go b/pkg/list/instance_engine.go new file mode 100644 index 00000000000..f6b69a3a5da --- /dev/null +++ b/pkg/list/instance_engine.go @@ -0,0 +1,42 @@ +package list + +import ( + "path/filepath" + "strings" + + "github.com/cloudposse/atmos/pkg/schema" +) + +// instanceEngine describes the selected executor without running or locating it. +// A configuration provider can declare an engine independently of its own name. +func instanceEngine(config *schema.AtmosConfiguration, kind string, component map[string]any) string { + settings, _ := component["settings"].(map[string]any) + provider, _ := settings["configuration"].(map[string]any) + if engine, ok := provider["engine"].(string); ok && engine != "" { + return engine + } + if kind != "terraform" { + return kind + } + command, _ := component["command"].(string) + if command == "" { + command = config.Components.Terraform.Command + } + if strings.TrimSuffix(filepath.Base(command), ".exe") == "tofu" { + return "opentofu" + } + return "terraform" +} + +// populateInstanceEngines uses the same resolved component command as execution. +// It does not start plugins, resolve credentials, or invoke a toolchain binary. +func populateInstanceEngines(config *schema.AtmosConfiguration, stacks map[string]any, instances []schema.Instance) { + for i := range instances { + instance := &instances[i] + stack, _ := stacks[instance.Stack].(map[string]any) + components, _ := stack["components"].(map[string]any) + family, _ := components[instance.ComponentType].(map[string]any) + component, _ := family[instance.Component].(map[string]any) + instance.Engine = instanceEngine(config, instance.ComponentType, component) + } +} diff --git a/pkg/list/instance_engine_test.go b/pkg/list/instance_engine_test.go new file mode 100644 index 00000000000..3689badaae6 --- /dev/null +++ b/pkg/list/instance_engine_test.go @@ -0,0 +1,104 @@ +package list + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" + + e "github.com/cloudposse/atmos/internal/exec" + "github.com/cloudposse/atmos/pkg/list/column" + "github.com/cloudposse/atmos/pkg/list/extract" + "github.com/cloudposse/atmos/pkg/schema" +) + +func TestInstanceEngineSelection(t *testing.T) { + config := &schema.AtmosConfiguration{} + config.Components.Terraform.Command = "tofu" + for _, tc := range []struct { + name, kind string + component map[string]any + want string + }{ + {"configured tofu", "terraform", nil, "opentofu"}, + {"unit terraform overrides default", "terraform", map[string]any{"command": "terraform"}, "terraform"}, + {"absolute Windows binary", "terraform", map[string]any{"command": filepath.Join(t.TempDir(), "tofu.exe")}, "opentofu"}, + {"native HCL remains tofu", "terraform", map[string]any{"settings": map[string]any{"configuration": map[string]any{"provider": "terragrunt", "engine": "opentofu"}}}, "opentofu"}, + {"external engine metadata", "terraform", map[string]any{"settings": map[string]any{"configuration": map[string]any{"engine": "terragrunt"}}}, "terragrunt"}, + {"other component family", "helmfile", nil, "helmfile"}, + } { + t.Run(tc.name, func(t *testing.T) { + require.Equal(t, tc.want, instanceEngine(config, tc.kind, tc.component)) + }) + } + config.Components.Terraform.Command = "" + require.Equal(t, "terraform", instanceEngine(config, "terraform", nil)) +} + +func TestInstanceDefaultColumnsRespectEditionsAndOverrides(t *testing.T) { + for _, tc := range []struct { + pin string + count int + }{{"", 4}, {"2026-10-28", 2}, {"2026-10-29", 4}} { + config := &schema.AtmosConfiguration{Edition: tc.pin} + columns, err := getInstanceColumns(config, nil) + require.NoError(t, err) + require.Len(t, columns, tc.count) + override, err := getInstanceColumns(config, []string{"Name={{ .component }}"}) + require.NoError(t, err) + require.Len(t, override, 1) + } + _, err := getInstanceColumns(&schema.AtmosConfiguration{Edition: "bad"}, nil) + require.Error(t, err) +} + +func TestPopulateInstanceEnginesDoesNotEvaluateConfiguration(t *testing.T) { + config := &schema.AtmosConfiguration{} + config.Components.Terraform.Command = "tofu" + instances := []schema.Instance{{Stack: "live", Component: "app", ComponentType: "terraform"}, {Stack: "missing", Component: "other", ComponentType: "helmfile"}} + stacks := map[string]any{"live": map[string]any{"components": map[string]any{"terraform": map[string]any{"app": map[string]any{"command": "terraform", "vars": map[string]any{"secret": "!terraform.output other live password"}}}}}} + populateInstanceEngines(config, stacks, instances) + require.Equal(t, "terraform", instances[0].Engine) + require.Equal(t, "helmfile", instances[1].Engine) +} + +func TestInstanceEngineReachesRenderedColumns(t *testing.T) { + config := &schema.AtmosConfiguration{} + config.Components.Terraform.Command = "tofu" + stacks := map[string]any{ + "live": map[string]any{ + "components": map[string]any{ + "terraform": map[string]any{ + "hcl": map[string]any{ + "settings": map[string]any{ + "configuration": map[string]any{"provider": "terragrunt"}, + }, + }, + "yaml": map[string]any{"command": "terraform"}, + }, + }, + }, + } + processor := e.NewMockStacksProcessor(gomock.NewController(t)) + processor.EXPECT(). + ExecuteDescribeStacks(config, "", nil, nil, nil, false, true, false, false, nil, nil). + Return(stacks, nil) + + instances, _, err := processInstancesWithDeps(config, processor, nil, true, false, nil, "", false, nil, nil, nil) + require.NoError(t, err) + columns, err := getInstanceColumns(config, nil) + require.NoError(t, err) + selector, err := column.NewSelector(columns, column.BuildColumnFuncMap()) + require.NoError(t, err) + + // The CLI uses metadata extraction, including for JSON/YAML row output. + // Testing only extract.Instances misses fields dropped on this path. + headers, rows, err := selector.Extract(extract.Metadata(instances)) + require.NoError(t, err) + require.Equal(t, []string{"Component", "Stack", "Type", "Engine"}, headers) + require.Equal(t, [][]string{ + {"hcl", "live", "terraform", "opentofu"}, + {"yaml", "live", "terraform", "terraform"}, + }, rows) +} diff --git a/pkg/list/list_instances.go b/pkg/list/list_instances.go index ae10e26c9de..44eb2938ad5 100644 --- a/pkg/list/list_instances.go +++ b/pkg/list/list_instances.go @@ -16,6 +16,7 @@ import ( authdeferred "github.com/cloudposse/atmos/pkg/auth/deferred" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/data" + "github.com/cloudposse/atmos/pkg/edition" "github.com/cloudposse/atmos/pkg/git" ghactions "github.com/cloudposse/atmos/pkg/github/actions" "github.com/cloudposse/atmos/pkg/list/column" @@ -41,6 +42,8 @@ import ( var defaultInstanceColumns = []column.Config{ {Name: "Component", Value: "{{ .component }}"}, {Name: "Stack", Value: "{{ .stack }}"}, + {Name: "Type", Value: "{{ .component_type }}"}, + {Name: "Engine", Value: "{{ .engine }}"}, } // InstancesCommandOptions contains options for the list instances command. @@ -504,7 +507,14 @@ func getInstanceColumns(atmosConfig *schema.AtmosConfiguration, columnsFlag []st return columns, nil } - // Return default columns. + // Preserve the historical table for projects pinned before this transition. + changed, err := edition.BehaviorChanged(atmosConfig.Edition, "list.instances.engine_columns") + if err != nil { + return nil, err + } + if !changed { + return defaultInstanceColumns[:2], nil + } return defaultInstanceColumns, nil } @@ -661,6 +671,8 @@ func processInstancesWithDeps( return nil, nil, err } + populateInstanceEngines(atmosConfig, stacksMap, instances) + // Sort instances. instances = sortInstances(instances) @@ -1300,6 +1312,7 @@ func processInstancesScopedClosure(atmosConfig *schema.AtmosConfiguration, opts if err != nil { return nil, nil, err } + populateInstanceEngines(atmosConfig, result.Stacks, instances) return sortInstances(instances), dependencies.Membership(result.Closure), nil } diff --git a/pkg/plugin/adapter/component/identity.go b/pkg/plugin/adapter/component/identity.go new file mode 100644 index 00000000000..6a6a38f73ea --- /dev/null +++ b/pkg/plugin/adapter/component/identity.go @@ -0,0 +1,47 @@ +package component + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "io" + "os" + "path/filepath" + + "google.golang.org/protobuf/proto" + + "github.com/cloudposse/atmos/pkg/perf" +) + +// Identity binds saved plans to executable bytes, schema, and negotiated package metadata. +func (p *Provider) Identity(ctx context.Context) (string, error) { + defer perf.Track(nil, "component.Provider.Identity")() + + h := sha256.New() + data, err := proto.MarshalOptions{Deterministic: true}.Marshal(p.expected) + if err != nil { + return "", err + } + _, _ = h.Write(data) + for _, path := range []string{p.executable, p.schemaPath} { + if err := ctx.Err(); err != nil { + return "", err + } + if !filepath.IsAbs(path) { + path = filepath.Join(p.directory, path) + } + file, err := os.Open(path) + if err != nil { + return "", err + } + _, err = io.Copy(h, file) + closeErr := file.Close() + if err != nil { + return "", err + } + if closeErr != nil { + return "", closeErr + } + } + return hex.EncodeToString(h.Sum(nil)), nil +} diff --git a/pkg/plugin/adapter/component/identity_test.go b/pkg/plugin/adapter/component/identity_test.go new file mode 100644 index 00000000000..cc71dacda34 --- /dev/null +++ b/pkg/plugin/adapter/component/identity_test.go @@ -0,0 +1,108 @@ +package component + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "google.golang.org/protobuf/proto" + + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" +) + +// identityProvider returns a provider whose executable and schema live in one private directory, +// so tests can change their bytes without touching the shared test binary. +func identityProvider(t *testing.T) *Provider { + t.Helper() + provider := fixtureProvider(t) + require.NoError(t, os.WriteFile(filepath.Join(provider.directory, "plugin.bin"), []byte("executable-v1"), 0o600)) + provider.executable = "plugin.bin" + return provider +} + +func TestProviderIdentityBindsExecutableSchemaAndNegotiatedMetadata(t *testing.T) { + t.Parallel() + provider := identityProvider(t) + baseline, err := provider.Identity(t.Context()) + require.NoError(t, err) + require.Len(t, baseline, 64, "identity is a hex SHA-256 digest") + + again, err := provider.Identity(t.Context()) + require.NoError(t, err) + require.Equal(t, baseline, again, "unchanged inputs must produce an identical identity") + + for name, change := range map[string]func(*testing.T, *Provider){ + "executable bytes": func(t *testing.T, p *Provider) { + require.NoError(t, os.WriteFile(filepath.Join(p.directory, "plugin.bin"), []byte("executable-v2"), 0o600)) + }, + "schema bytes": func(t *testing.T, p *Provider) { + require.NoError(t, os.WriteFile(filepath.Join(p.directory, p.schemaPath), []byte(`{"type":"object"}`), 0o600)) + }, + "negotiated manifest": func(_ *testing.T, p *Provider) { + p.expected = proto.Clone(p.expected).(*pluginv1.DescribeResponse) + p.expected.ManifestDigest = "sha256:other" + }, + } { + t.Run(name, func(t *testing.T) { + t.Parallel() + provider := identityProvider(t) + before, err := provider.Identity(t.Context()) + require.NoError(t, err) + change(t, provider) + after, err := provider.Identity(t.Context()) + require.NoError(t, err) + require.NotEqual(t, before, after) + }) + } +} + +func TestProviderIdentityResolvesAbsoluteAndRelativePathsIdentically(t *testing.T) { + t.Parallel() + relative := identityProvider(t) + absolute := identityProvider(t) + absolute.directory = relative.directory + absolute.expected = proto.Clone(relative.expected).(*pluginv1.DescribeResponse) + absolute.schemaPath = relative.schemaPath + absolute.executable = filepath.Join(relative.directory, "plugin.bin") + + want, err := relative.Identity(t.Context()) + require.NoError(t, err) + got, err := absolute.Identity(t.Context()) + require.NoError(t, err) + require.Equal(t, want, got) +} + +func TestProviderIdentityFailsClosed(t *testing.T) { + t.Parallel() + t.Run("missing executable", func(t *testing.T) { + t.Parallel() + provider := identityProvider(t) + provider.executable = "absent.bin" + _, err := provider.Identity(t.Context()) + require.ErrorIs(t, err, os.ErrNotExist) + }) + t.Run("missing schema", func(t *testing.T) { + t.Parallel() + provider := identityProvider(t) + require.NoError(t, os.Remove(filepath.Join(provider.directory, provider.schemaPath))) + _, err := provider.Identity(t.Context()) + require.ErrorIs(t, err, os.ErrNotExist) + }) + t.Run("executable is a directory", func(t *testing.T) { + t.Parallel() + provider := identityProvider(t) + require.NoError(t, os.Mkdir(filepath.Join(provider.directory, "dir.bin"), 0o700)) + provider.executable = "dir.bin" + _, err := provider.Identity(t.Context()) + require.Error(t, err, "a directory cannot be hashed as executable bytes") + }) + t.Run("canceled context", func(t *testing.T) { + t.Parallel() + ctx, cancel := context.WithCancel(t.Context()) + cancel() + _, err := identityProvider(t).Identity(ctx) + require.ErrorIs(t, err, context.Canceled) + }) +} diff --git a/pkg/plugin/adapter/component/responses.go b/pkg/plugin/adapter/component/responses.go index 8827a82cefb..d92afc9bf04 100644 --- a/pkg/plugin/adapter/component/responses.go +++ b/pkg/plugin/adapter/component/responses.go @@ -90,7 +90,7 @@ func checkGeneratedFiles(files []*pluginv1.ComponentGeneratedFile) error { } seen[filepath.Clean(file.Path)] = true switch file.IfExists { - case "error", "skip", "overwrite", "overwrite_terragrunt": + case "error", "skip", "overwrite", "overwrite_terragrunt", "remove", "remove_terragrunt": default: return errUtils.ErrPluginProtocol } diff --git a/pkg/plugin/adapter/configuration/discovery.go b/pkg/plugin/adapter/configuration/discovery.go index 53ade788235..697d8684a81 100644 --- a/pkg/plugin/adapter/configuration/discovery.go +++ b/pkg/plugin/adapter/configuration/discovery.go @@ -67,7 +67,12 @@ func addInstance(definitions map[string]map[string]any, instance *pluginv1.Disco if settings == nil { settings = make(map[string]any) } - settings["configuration"] = map[string]any{"provider": instance.Provider, "engine": instance.Engine, "source": instance.Source} + configuration, _ := settings["configuration"].(map[string]any) + if configuration == nil { + configuration = make(map[string]any) + } + configuration["provider"], configuration["engine"], configuration["source"] = instance.Provider, instance.Engine, instance.Source + settings["configuration"] = configuration config["settings"] = settings config["dependencies"] = mergeDependencies(config["dependencies"], instanceDependencies(instance)) definition := definitions[address.Stack] diff --git a/pkg/plugin/adapter/configuration/execution.go b/pkg/plugin/adapter/configuration/execution.go new file mode 100644 index 00000000000..b9550447c15 --- /dev/null +++ b/pkg/plugin/adapter/configuration/execution.go @@ -0,0 +1,68 @@ +package configuration + +import ( + "maps" + "slices" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/component/execution" + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" + "github.com/cloudposse/atmos/pkg/schema" +) + +// preparedExecution copies the provider's hooks and extra arguments, preserving declaration +// order. The host runs them, so anything it cannot run is a protocol error, never a silent skip. +func preparedExecution(source *pluginv1.ComponentExecutionConfiguration) (*schema.ComponentExecution, error) { + if source == nil { + return nil, nil + } + result := &schema.ComponentExecution{} + for _, item := range source.GetExtraArguments() { + if item == nil { + return nil, errUtils.ErrPluginProtocol + } + result.ExtraArguments = append(result.ExtraArguments, schema.ComponentExtraArguments{ + Commands: slices.Clone(item.GetCommands()), + Arguments: slices.Clone(item.GetArguments()), + RequiredVarFiles: slices.Clone(item.GetRequiredVarFiles()), + OptionalVarFiles: slices.Clone(item.GetOptionalVarFiles()), + Environment: maps.Clone(item.GetEnvironment()), + }) + } + var err error + if result.BeforeHooks, err = preparedHooks(source.GetBeforeHooks()); err != nil { + return nil, err + } + if result.AfterHooks, err = preparedHooks(source.GetAfterHooks()); err != nil { + return nil, err + } + if result.ErrorHooks, err = preparedHooks(source.GetErrorHooks()); err != nil { + return nil, err + } + if err := execution.Validate(result); err != nil { + return nil, err + } + if result.IsEmpty() { + return nil, nil + } + return result, nil +} + +func preparedHooks(hooks []*pluginv1.ComponentHook) ([]schema.ComponentHook, error) { + var result []schema.ComponentHook + for _, hook := range hooks { + if hook == nil { + return nil, errUtils.ErrPluginProtocol + } + result = append(result, schema.ComponentHook{ + Name: hook.GetName(), + Commands: slices.Clone(hook.GetCommands()), + Execute: slices.Clone(hook.GetExecute()), + WorkingDirectory: hook.GetWorkingDirectory(), + RunOnError: hook.GetRunOnError(), + SuppressStdout: hook.GetSuppressStdout(), + OnErrors: slices.Clone(hook.GetOnErrors()), + }) + } + return result, nil +} diff --git a/pkg/plugin/adapter/configuration/imports.go b/pkg/plugin/adapter/configuration/imports.go new file mode 100644 index 00000000000..8b936d0a0ba --- /dev/null +++ b/pkg/plugin/adapter/configuration/imports.go @@ -0,0 +1,68 @@ +package configuration + +import ( + "context" + "fmt" + "path/filepath" + "strings" + + "google.golang.org/protobuf/proto" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/perf" + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" +) + +// RegisterImport records an explicit root without starting a plugin. The normal +// discovery pass evaluates the combined scope once after YAML imports are read. +func (s *Session) RegisterImport(ctx context.Context, uri string) (bool, error) { + defer perf.Track(nil, "configuration.Session.RegisterImport")() + + name, path, found := strings.Cut(uri, "://") + if !found { + return false, nil + } + s.importMu.Lock() + defer s.importMu.Unlock() + request := s.importProviders[name] + if request == nil { + return false, nil + } + if err := ctx.Err(); err != nil { + return true, err + } + if err := s.ctx.Err(); err != nil { + return true, err + } + if !filepath.IsLocal(path) || strings.ContainsAny(path, "\\?#*") { + return true, fmt.Errorf("%w: provider import must name a project-relative file or directory", errUtils.ErrPluginInvalidConfig) + } + path = filepath.ToSlash(filepath.Clean(path)) + key := name + "://" + path + if s.imports[key] { + return true, nil + } + if s.started { + return true, fmt.Errorf("%w: cannot add an import after configuration discovery", errUtils.ErrPluginInvalidConfig) + } + selected := s.discoveryRequest(request) + selected.Imports = append(selected.Imports, key) + s.imports[key] = true + return true, nil +} + +// discoveryRequest returns the pending discovery request for the template's provider, +// cloning the template into the session when none exists yet. The caller holds importMu. +func (s *Session) discoveryRequest(template *pluginv1.DiscoverComponentsRequest) *pluginv1.DiscoverComponentsRequest { + var selected *pluginv1.DiscoverComponentsRequest + for _, existing := range s.requests { + if existing.Provider == template.Provider { + selected = existing + } + } + if selected == nil { + selected = proto.Clone(template).(*pluginv1.DiscoverComponentsRequest) + s.requests = append(s.requests, selected) + } + return selected +} diff --git a/pkg/plugin/adapter/configuration/imports_test.go b/pkg/plugin/adapter/configuration/imports_test.go new file mode 100644 index 00000000000..b24534211d7 --- /dev/null +++ b/pkg/plugin/adapter/configuration/imports_test.go @@ -0,0 +1,105 @@ +package configuration + +import ( + "context" + "path/filepath" + "sync" + "testing" + + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" + + errUtils "github.com/cloudposse/atmos/errors" + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" + "github.com/cloudposse/atmos/pkg/schema" +) + +func TestExplicitImportsWithoutAutomaticDiscovery(t *testing.T) { + config := enabledConfig(t) + settings := config.Components.Terraform.ConfigurationProviders["terragrunt"] + settings.Discovery.Enabled = false + config.Components.Terraform.ConfigurationProviders["terragrunt"] = settings + provider := NewMockProvider(gomock.NewController(t)) + provider.EXPECT().ComponentType().Return("terraform") + provider.EXPECT().Discover(gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, req *pluginv1.DiscoverComponentsRequest) (*pluginv1.DiscoverComponentsResponse, error) { + require.Empty(t, req.IncludePaths) + require.Equal(t, []string{"terragrunt://live", "terragrunt://other/terragrunt.hcl"}, req.Imports) + require.Nil(t, req.Effects) + return &pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{unit(t, "app")}}, nil + }) + loads := 0 + session, err := New(t.Context(), config, "list", func(_ context.Context, names []string) (map[string]Provider, error) { + loads++ + require.Equal(t, []string{"terragrunt"}, names) + return map[string]Provider{"terragrunt": provider}, nil + }) + require.NoError(t, err) + for _, uri := range []string{"terragrunt://live", "terragrunt://live/.", "terragrunt://other/terragrunt.hcl"} { + handled, err := session.RegisterImport(t.Context(), uri) + require.NoError(t, err) + require.True(t, handled) + } + require.Zero(t, loads) + definitions, err := session.Discover(t.Context()) + require.NoError(t, err) + require.Contains(t, definitions, "live") + handled, err := session.RegisterImport(t.Context(), "terragrunt://live") + require.NoError(t, err) + require.True(t, handled) + _, err = session.RegisterImport(t.Context(), "terragrunt://late") + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) +} + +func TestImportValidationCancellationAndRevision(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + session, err := New(ctx, enabledConfig(t), "list", func(context.Context, []string) (map[string]Provider, error) { return nil, nil }) + require.NoError(t, err) + for _, uri := range []string{"ordinary", "https://example.com/config.yaml"} { + handled, err := session.RegisterImport(t.Context(), uri) + require.NoError(t, err) + require.False(t, handled) + } + for _, path := range []string{"", "../outside", "/absolute", `live\app`, "live?ref=main", "live#fragment", "live/*"} { + handled, err := session.RegisterImport(t.Context(), "terragrunt://"+path) + require.True(t, handled) + require.Error(t, err) + } + _, err = session.RegisterImport(t.Context(), "terragrunt://live") + require.NoError(t, err) + rebased, err := session.ForRevision(filepath.Join(t.TempDir(), "base")) + require.NoError(t, err) + revision := rebased.(*Session) + require.Empty(t, revision.requests[0].Imports) + require.Equal(t, []string{"live/**"}, revision.requests[0].IncludePaths) + _, err = revision.RegisterImport(t.Context(), "terragrunt://previous") + require.NoError(t, err) + require.Equal(t, []string{"terragrunt://previous"}, revision.requests[0].Imports) + require.Equal(t, []string{"terragrunt://live"}, session.requests[0].Imports) + canceled, stop := context.WithCancel(t.Context()) + stop() + _, err = session.RegisterImport(canceled, "terragrunt://live") + require.ErrorIs(t, err, context.Canceled) + cancel() + _, err = session.RegisterImport(t.Context(), "terragrunt://live") + require.ErrorIs(t, err, context.Canceled) +} + +func TestConcurrentImportRegistrationDeduplicates(t *testing.T) { + session, err := New(t.Context(), enabledConfig(t), "list", func(context.Context, []string) (map[string]Provider, error) { return nil, nil }) + require.NoError(t, err) + var workers sync.WaitGroup + for range 10 { + workers.Add(1) + go func() { + defer workers.Done() + _, err := session.RegisterImport(t.Context(), "terragrunt://live") + if err != nil { + t.Error(err) + } + }() + } + workers.Wait() + require.Len(t, session.requests, 1) + require.Equal(t, []string{"terragrunt://live"}, session.requests[0].Imports) + var _ schema.ComponentConfigurationImportRuntime = session +} diff --git a/pkg/plugin/adapter/configuration/plan.go b/pkg/plugin/adapter/configuration/plan.go new file mode 100644 index 00000000000..605be961696 --- /dev/null +++ b/pkg/plugin/adapter/configuration/plan.go @@ -0,0 +1,151 @@ +package configuration + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "fmt" + "io/fs" + "os" + "path/filepath" + "strings" + + "google.golang.org/protobuf/proto" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/perf" + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" + "github.com/cloudposse/atmos/pkg/schema" +) + +type identityProvider interface { + Identity(context.Context) (string, error) +} + +// PlanIdentity fingerprints provider/options and watched source bytes without preparing the unit. +func (s *Session) PlanIdentity(ctx context.Context, info *schema.ConfigAndStacksInfo, sources []schema.ComponentConfigurationSource) (string, error) { + defer perf.Track(nil, "configuration.Session.PlanIdentity")() + + selected, err := s.componentRequest(ctx, info) + if err != nil { + return "", err + } + request := selected.request + identified, ok := selected.provider.(identityProvider) + if !ok { + return "", errUtils.ErrPluginUnsupportedExtension + } + identity, err := identified.Identity(ctx) + if err != nil { + return "", err + } + // Apply uses the preparation reviewed by plan, not another HCL evaluation. + request.Operation = &pluginv1.ComponentOperation{Command: "plan", Mode: request.Operation.Mode, Options: request.Operation.Options} + request.Invocation = nil + request.RootDirectory = "" + raw, err := (proto.MarshalOptions{Deterministic: true}).Marshal(request) + if err != nil { + return "", err + } + files, err := sourceDigests(ctx, s.root, sources) + if err != nil { + return "", err + } + payload, err := json.Marshal(struct { + Provider string + Request string + Sources map[string]string + }{identity, string(raw), files}) + if err != nil { + return "", err + } + sum := sha256.Sum256(payload) + return hex.EncodeToString(sum[:]), nil +} + +func sourceDigests(ctx context.Context, root string, sources []schema.ComponentConfigurationSource) (map[string]string, error) { + root, err := filepath.EvalSymlinks(root) + if err != nil { + return nil, err + } + result := map[string]string{} + for _, source := range sources { + if !filepath.IsLocal(source.Path) { + return nil, errUtils.ErrPluginInvalidConfig + } + path := filepath.Join(root, filepath.FromSlash(source.Path)) + if !source.Directory { + err = digestSource(ctx, root, path, result) + } else { + err = digestSourceDirectory(ctx, root, source.Path, result) + } + if err != nil { + return nil, err + } + } + return result, nil +} + +// digestSourceDirectory digests every non-excluded file below a watched directory source. +// A directory that does not exist is recorded as absent. +func digestSourceDirectory(ctx context.Context, root, sourcePath string, result map[string]string) error { + top := filepath.Join(root, filepath.FromSlash(sourcePath)) + err := filepath.WalkDir(top, func(path string, entry fs.DirEntry, err error) error { + if err != nil { + return err + } + if entry.IsDir() { + if path != top && excludedPlanSource(entry.Name()) { + return filepath.SkipDir + } + return nil + } + if excludedPlanSource(entry.Name()) { + return nil + } + return digestSource(ctx, root, path, result) + }) + if os.IsNotExist(err) { + result[sourcePath] = "absent" + return nil + } + return err +} + +func excludedPlanSource(name string) bool { + return name == ".atmos" || name == ".git" || name == ".terraform" || name == ".terragrunt-cache" || name == ".terragrunt-stack" || strings.HasPrefix(name, "terraform.tfstate") || strings.HasSuffix(name, ".planfile") || strings.HasSuffix(name, ".atmos.json") +} + +func digestSource(ctx context.Context, root, path string, result map[string]string) error { + if err := ctx.Err(); err != nil { + return err + } + real, err := filepath.EvalSymlinks(path) + if os.IsNotExist(err) { + relative, relErr := filepath.Rel(root, path) + if relErr != nil { + return relErr + } + result[filepath.ToSlash(relative)] = "absent" + return nil + } + if err != nil { + return err + } + relative, err := filepath.Rel(root, real) + if err != nil || !filepath.IsLocal(relative) { + return fmt.Errorf("%w: source leaves project root", errUtils.ErrPluginInvalidConfig) + } + data, err := os.ReadFile(real) + if err != nil { + return err + } + sum := sha256.Sum256(data) + relative, err = filepath.Rel(root, path) + if err != nil { + return err + } + result[filepath.ToSlash(relative)] = hex.EncodeToString(sum[:]) + return nil +} diff --git a/pkg/plugin/adapter/configuration/plan_test.go b/pkg/plugin/adapter/configuration/plan_test.go new file mode 100644 index 00000000000..9d74f0a33ea --- /dev/null +++ b/pkg/plugin/adapter/configuration/plan_test.go @@ -0,0 +1,278 @@ +package configuration + +import ( + "context" + "errors" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" + + errUtils "github.com/cloudposse/atmos/errors" + adapter "github.com/cloudposse/atmos/pkg/plugin/adapter/component" + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" + "github.com/cloudposse/atmos/pkg/schema" +) + +// identifiedProvider adds the optional Identity capability to the generated provider mock. +type identifiedProvider struct { + Provider + identity func(context.Context) (string, error) +} + +func (p *identifiedProvider) Identity(ctx context.Context) (string, error) { return p.identity(ctx) } + +// planCase describes every input that a saved-plan binding is expected to cover. +type planCase struct { + identity string + source string + component string + options map[string]any + args []string + subCommand string + env []string +} + +func defaultPlanCase() *planCase { + return &planCase{identity: "provider-v1", source: "./modules/unit", component: "app", options: map[string]any{"retries": 1}, args: []string{"-var=a=1"}, subCommand: "plan", env: []string{"TOKEN=one"}} +} + +// planSession builds a discovered session whose provider reports the case's identity. +func planSession(t *testing.T, c *planCase) (*Session, *schema.ConfigAndStacksInfo) { + t.Helper() + config := enabledConfig(t) + settings := config.Components.Terraform.ConfigurationProviders["terragrunt"] + settings.Options = c.options + config.Components.Terraform.ConfigurationProviders["terragrunt"] = settings + mock := NewMockProvider(gomock.NewController(t)) + mock.EXPECT().ComponentType().Return("terraform").AnyTimes() + component := unit(t, c.component) + configuration, err := adapter.EncodeObject(map[string]any{"source": c.source}) + require.NoError(t, err) + component.Configuration = configuration + mock.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{component}}, nil) + provider := &identifiedProvider{Provider: mock, identity: func(context.Context) (string, error) { return c.identity, nil }} + session, err := New(t.Context(), config, "plan", func(context.Context, []string) (map[string]Provider, error) { + return map[string]Provider{"terragrunt": provider}, nil + }) + require.NoError(t, err) + info := &schema.ConfigAndStacksInfo{Stack: "live", ComponentFromArg: c.component, SubCommand: c.subCommand, AdditionalArgsAndFlags: c.args, ComponentEnvList: c.env} + return session, info +} + +func planIdentity(t *testing.T, c *planCase) string { + t.Helper() + session, info := planSession(t, c) + identity, err := session.PlanIdentity(t.Context(), info, nil) + require.NoError(t, err) + require.Len(t, identity, 64, "identity is a hex SHA-256 digest") + return identity +} + +func TestPlanIdentityDetectsEveryBoundInputAndIgnoresInvocationOnlyInputs(t *testing.T) { + baseline := planIdentity(t, defaultPlanCase()) + + for name, change := range map[string]func(*planCase){ + "provider identity": func(c *planCase) { c.identity = "provider-v2" }, + "module source": func(c *planCase) { c.source = "./modules/other" }, + "component address": func(c *planCase) { c.component = "worker" }, + "provider options": func(c *planCase) { c.options = map[string]any{"retries": 2} }, + } { + t.Run("changed "+name, func(t *testing.T) { + c := defaultPlanCase() + change(c) + require.NotEqual(t, baseline, planIdentity(t, c), "a saved plan must not be applied after this input changes") + }) + } + + for name, change := range map[string]func(*planCase){ + "apply of the reviewed plan": func(c *planCase) { c.subCommand = "apply" }, + "ambient environment": func(c *planCase) { c.env = []string{"TOKEN=two"} }, + "apply-only arguments": func(c *planCase) { c.subCommand, c.args = "apply", []string{"-auto-approve"} }, + } { + t.Run("unchanged "+name, func(t *testing.T) { + c := defaultPlanCase() + change(c) + require.Equal(t, baseline, planIdentity(t, c), "apply must accept the plan reviewed with the same bound inputs") + }) + } +} + +func TestPlanIdentityIsStableAcrossProjectRoots(t *testing.T) { + // Different checkouts of identical content must accept each other's saved plans. + first, info := planSession(t, defaultPlanCase()) + second, secondInfo := planSession(t, defaultPlanCase()) + sources := []schema.ComponentConfigurationSource{{Path: "root.hcl"}, {Path: "modules", Directory: true}} + for _, root := range []string{first.root, second.root} { + require.NoError(t, os.MkdirAll(filepath.Join(root, "modules"), 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(root, "root.hcl"), []byte("root"), 0o600)) + require.NoError(t, os.WriteFile(filepath.Join(root, "modules", "main.tf"), []byte("module"), 0o600)) + } + one, err := first.PlanIdentity(t.Context(), info, sources) + require.NoError(t, err) + two, err := second.PlanIdentity(t.Context(), secondInfo, sources) + require.NoError(t, err) + require.Equal(t, one, two) +} + +func TestPlanIdentityBindsWatchedSources(t *testing.T) { + session, info := planSession(t, defaultPlanCase()) + root := session.root + write := func(name, content string) { + t.Helper() + path := filepath.Join(root, filepath.FromSlash(name)) + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700)) + require.NoError(t, os.WriteFile(path, []byte(content), 0o600)) + } + reset := func() { + t.Helper() + entries, err := os.ReadDir(root) + require.NoError(t, err) + for _, entry := range entries { + require.NoError(t, os.RemoveAll(filepath.Join(root, entry.Name()))) + } + write("root.hcl", "root") + write("modules/main.tf", "main") + } + reset() + sources := []schema.ComponentConfigurationSource{{Path: "root.hcl"}, {Path: "modules", Directory: true}} + identity := func(sources []schema.ComponentConfigurationSource) string { + t.Helper() + result, err := session.PlanIdentity(t.Context(), info, sources) + require.NoError(t, err) + return result + } + baseline := identity(sources) + require.Equal(t, baseline, identity(sources), "unchanged inputs are accepted") + require.Equal(t, baseline, identity([]schema.ComponentConfigurationSource{sources[1], sources[0]}), "source order is not significant") + require.NotEqual(t, baseline, identity(nil), "dropping a watched source changes the binding") + + // Runtime artifacts and state never invalidate a plan, at any depth. + for _, ignored := range []string{ + "modules/.terraform/providers.json", "modules/.git/HEAD", "modules/terraform.tfstate", "modules/terraform.tfstate.backup", + "modules/saved.planfile", "modules/unit.atmos.json", "modules/.terragrunt-cache/x/main.tf", "modules/.terragrunt-stack/stack.hcl", + "modules/nested/.atmos/state", "modules/nested/.terraform/x", + } { + write(ignored, "noise") + } + require.Equal(t, baseline, identity(sources), "excluded artifacts must not affect the binding") + reset() + + for _, change := range []struct { + name string + do func() + }{ + {"file edited", func() { write("root.hcl", "root changed") }}, + {"directory file edited", func() { write("modules/main.tf", "main changed") }}, + {"directory file added", func() { write("modules/extra.tf", "extra") }}, + {"nested directory file added", func() { write("modules/nested/deep.tf", "deep") }}, + {"watched file removed", func() { require.NoError(t, os.Remove(filepath.Join(root, "root.hcl"))) }}, + {"directory file removed", func() { require.NoError(t, os.Remove(filepath.Join(root, "modules", "main.tf"))) }}, + {"directory tree removed", func() { require.NoError(t, os.RemoveAll(filepath.Join(root, "modules"))) }}, + } { + change.do() + require.NotEqual(t, baseline, identity(sources), change.name) + reset() + require.Equal(t, baseline, identity(sources), "restoring the original bytes after %q is accepted again", change.name) + } +} + +func TestPlanIdentityRejectsUnboundableRequests(t *testing.T) { + t.Run("provider without identity", func(t *testing.T) { + session, info := planSession(t, defaultPlanCase()) + _, err := session.Discover(t.Context()) + require.NoError(t, err) + session.registry["terragrunt"] = NewMockProvider(gomock.NewController(t)) + _, err = session.PlanIdentity(t.Context(), info, nil) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + }) + t.Run("provider identity failure", func(t *testing.T) { + session, info := planSession(t, defaultPlanCase()) + _, err := session.Discover(t.Context()) + require.NoError(t, err) + failure := errors.New("executable unreadable") + session.registry["terragrunt"] = &identifiedProvider{Provider: NewMockProvider(gomock.NewController(t)), identity: func(context.Context) (string, error) { return "", failure }} + _, err = session.PlanIdentity(t.Context(), info, nil) + require.ErrorIs(t, err, failure) + }) + t.Run("undiscovered component", func(t *testing.T) { + session, info := planSession(t, defaultPlanCase()) + info.ComponentFromArg = "absent" + _, err := session.PlanIdentity(t.Context(), info, nil) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) + }) + t.Run("source escapes the project", func(t *testing.T) { + session, info := planSession(t, defaultPlanCase()) + for _, path := range []string{"../outside", filepath.Join(session.root, "absolute")} { + _, err := session.PlanIdentity(t.Context(), info, []schema.ComponentConfigurationSource{{Path: path}}) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig, path) + } + }) +} + +func TestSourceDigests(t *testing.T) { + t.Run("symlink leaving the project is rejected", func(t *testing.T) { + root, outside := t.TempDir(), t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(outside, "secret.hcl"), []byte("secret"), 0o600)) + if err := os.Symlink(filepath.Join(outside, "secret.hcl"), filepath.Join(root, "link.hcl")); err != nil { + t.Skipf("symlinks unavailable: %v", err) + } + _, err := sourceDigests(t.Context(), root, []schema.ComponentConfigurationSource{{Path: "link.hcl"}}) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) + }) + t.Run("symlink inside the project digests its target under the link path", func(t *testing.T) { + root := t.TempDir() + target := filepath.Join(root, "shared.hcl") + require.NoError(t, os.WriteFile(target, []byte("one"), 0o600)) + if err := os.Symlink(target, filepath.Join(root, "link.hcl")); err != nil { + t.Skipf("symlinks unavailable: %v", err) + } + sources := []schema.ComponentConfigurationSource{{Path: "link.hcl"}} + before, err := sourceDigests(t.Context(), root, sources) + require.NoError(t, err) + require.Contains(t, before, "link.hcl") + require.NoError(t, os.WriteFile(target, []byte("two"), 0o600)) + after, err := sourceDigests(t.Context(), root, sources) + require.NoError(t, err) + require.NotEqual(t, before["link.hcl"], after["link.hcl"], "edits behind a symlink must change the digest") + }) + t.Run("missing sources are recorded as absent", func(t *testing.T) { + got, err := sourceDigests(t.Context(), t.TempDir(), []schema.ComponentConfigurationSource{{Path: "missing.hcl"}, {Path: "nodir", Directory: true}}) + require.NoError(t, err) + require.Equal(t, map[string]string{"missing.hcl": "absent", "nodir": "absent"}, got) + }) + t.Run("missing project root fails", func(t *testing.T) { + _, err := sourceDigests(t.Context(), filepath.Join(t.TempDir(), "gone"), nil) + require.ErrorIs(t, err, os.ErrNotExist) + }) + t.Run("directory watched as a file fails", func(t *testing.T) { + root := t.TempDir() + require.NoError(t, os.Mkdir(filepath.Join(root, "modules"), 0o700)) + _, err := sourceDigests(t.Context(), root, []schema.ComponentConfigurationSource{{Path: "modules"}}) + require.Error(t, err) + }) + t.Run("canceled context stops digesting", func(t *testing.T) { + root := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(root, "a.hcl"), []byte("a"), 0o600)) + require.NoError(t, os.Mkdir(filepath.Join(root, "dir"), 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(root, "dir", "b.tf"), []byte("b"), 0o600)) + ctx, cancel := context.WithCancel(t.Context()) + cancel() + for _, source := range []schema.ComponentConfigurationSource{{Path: "a.hcl"}, {Path: "dir", Directory: true}} { + _, err := sourceDigests(ctx, root, []schema.ComponentConfigurationSource{source}) + require.ErrorIs(t, err, context.Canceled, source.Path) + } + }) +} + +func TestExcludedPlanSource(t *testing.T) { + for name, excluded := range map[string]bool{ + ".atmos": true, ".git": true, ".terraform": true, ".terragrunt-cache": true, ".terragrunt-stack": true, + "terraform.tfstate": true, "terraform.tfstate.backup": true, "a.planfile": true, "b.atmos.json": true, + "main.tf": false, "terragrunt.hcl": false, ".terraform.lock.hcl": false, "state.json": false, + } { + require.Equal(t, excluded, excludedPlanSource(name), name) + } +} diff --git a/pkg/plugin/adapter/configuration/preparation.go b/pkg/plugin/adapter/configuration/preparation.go index 84288a68b6f..871db483981 100644 --- a/pkg/plugin/adapter/configuration/preparation.go +++ b/pkg/plugin/adapter/configuration/preparation.go @@ -5,6 +5,7 @@ import ( "fmt" "os" "path/filepath" + "slices" "strings" errUtils "github.com/cloudposse/atmos/errors" @@ -24,6 +25,30 @@ func (s *Session) Prepare(ctx context.Context, info *schema.ConfigAndStacksInfo) if info.UseTerraformPlan || info.PlanFile != "" || info.StoredPlanFile != "" { return nil, fmt.Errorf("%w: plugin saved-plan binding is not implemented", errUtils.ErrPluginUnsupportedExtension) } + selected, err := s.componentRequest(ctx, info) + if err != nil { + return nil, err + } + selected.request.Effects = &pluginv1.ComponentEffects{Commands: true, Network: true, Secrets: true, Writes: true, StateReads: true} + result, err := selected.provider.Prepare(ctx, selected.request) + if err != nil { + return nil, err + } + if result.Engine != selected.instance.Engine { + return nil, errUtils.ErrPluginProtocol + } + return preparedConfiguration(result) +} + +// selectedComponent bundles the request, owning provider, and discovered instance for one component. +type selectedComponent struct { + request *pluginv1.ComponentRequest + provider Provider + instance *pluginv1.DiscoveredComponent +} + +// componentRequest shares coordinates and invocation environment, but grants no effects. +func (s *Session) componentRequest(ctx context.Context, info *schema.ConfigAndStacksInfo) (*selectedComponent, error) { if err := s.ensure(ctx); err != nil { return nil, err } @@ -35,24 +60,16 @@ func (s *Session) Prepare(ctx context.Context, info *schema.ConfigAndStacksInfo) if !ok { return nil, errUtils.ErrPluginUnsupportedExtension } - settings := s.providerSettings(instance.Provider) env := preparationEnvironment(info) env["ATMOS_TERRAFORM_BINARY"] = info.Command env["TF_WORKSPACE"] = info.TerraformWorkspace - result, err := provider.Prepare(ctx, &pluginv1.ComponentRequest{ + request := &pluginv1.ComponentRequest{ Provider: instance.Provider, Address: instance.Address, Source: instance.Source, RootDirectory: s.root, - Configuration: instance.Configuration, ProviderConfiguration: settings, + Configuration: instance.Configuration, ProviderConfiguration: s.providerSettings(instance.Provider), Invocation: &pluginv1.Invocation{Environment: env}, - Operation: &pluginv1.ComponentOperation{Command: info.SubCommand, Mode: "native"}, - Effects: &pluginv1.ComponentEffects{Commands: true, Network: true, Secrets: true, Writes: true, StateReads: true}, - }) - if err != nil { - return nil, err + Operation: &pluginv1.ComponentOperation{Command: info.SubCommand, Mode: "native", TerraformArguments: slices.Clone(info.AdditionalArgsAndFlags)}, } - if result.Engine != instance.Engine { - return nil, errUtils.ErrPluginProtocol - } - return preparedConfiguration(result) + return &selectedComponent{request: request, provider: provider, instance: instance}, nil } func preparedConfiguration(result *pluginv1.PrepareComponentResponse) (*schema.PreparedComponentConfiguration, error) { @@ -67,13 +84,19 @@ func preparedConfiguration(result *pluginv1.PrepareComponentResponse) (*schema.P if result.State.BackendType == "local" { resolveLocalBackend(backend, result.State.WorkingDirectory) } - prepared := &schema.PreparedComponentConfiguration{Engine: result.Engine, Source: result.ModuleSource, Inputs: inputs, BackendType: result.State.BackendType, Backend: backend, Workspace: result.State.Workspace} + prepared := &schema.PreparedComponentConfiguration{Engine: result.Engine, Source: result.ModuleSource, Inputs: inputs, BackendType: result.State.BackendType, Backend: backend, Workspace: result.State.Workspace, BackendGenerated: result.BackendGenerated} + execution, err := preparedExecution(result.Execution) + if err != nil { + return nil, err + } + prepared.Execution = execution for _, file := range result.Files { - if file.IfExists == "overwrite_terragrunt" { - return nil, errUtils.ErrPluginUnsupportedExtension - } prepared.Files = append(prepared.Files, schema.ComponentConfigurationFile{Path: file.Path, Content: file.Content, IfExists: file.IfExists, Sensitive: file.Sensitive}) } + for _, source := range result.Sources { + prepared.Sources = append(prepared.Sources, schema.ComponentConfigurationSource{Path: source.Path, Directory: source.Kind == pluginv1.ComponentSource_DIRECTORY}) + } + return prepared, nil } diff --git a/pkg/plugin/adapter/configuration/preparation_execution_test.go b/pkg/plugin/adapter/configuration/preparation_execution_test.go new file mode 100644 index 00000000000..cde83005fda --- /dev/null +++ b/pkg/plugin/adapter/configuration/preparation_execution_test.go @@ -0,0 +1,101 @@ +package configuration + +import ( + "testing" + + "github.com/stretchr/testify/require" + "google.golang.org/protobuf/proto" + + errUtils "github.com/cloudposse/atmos/errors" + adapter "github.com/cloudposse/atmos/pkg/plugin/adapter/component" + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" + "github.com/cloudposse/atmos/pkg/schema" +) + +func executionResponse(t *testing.T, execution *pluginv1.ComponentExecutionConfiguration) *pluginv1.PrepareComponentResponse { + t.Helper() + backend, err := adapter.EncodeObject(map[string]any{}) + require.NoError(t, err) + return &pluginv1.PrepareComponentResponse{ + Engine: "opentofu", + State: &pluginv1.ComponentStateIdentity{BackendType: "s3", Backend: backend, Workspace: "default"}, + Execution: execution, + } +} + +// roundTrip sends the response over the wire format so the test covers field numbers and names. +func roundTrip(t *testing.T, response *pluginv1.PrepareComponentResponse) *pluginv1.PrepareComponentResponse { + t.Helper() + data, err := proto.Marshal(response) + require.NoError(t, err) + decoded := &pluginv1.PrepareComponentResponse{} + require.NoError(t, proto.Unmarshal(data, decoded)) + return decoded +} + +func TestPreparedConfigurationCarriesExecutionInDeclarationOrder(t *testing.T) { + response := roundTrip(t, executionResponse(t, &pluginv1.ComponentExecutionConfiguration{ + ExtraArguments: []*pluginv1.ComponentExtraArguments{ + {Commands: []string{"plan"}, Arguments: []string{"-lock=false"}}, + {Commands: []string{"apply", "plan"}, RequiredVarFiles: []string{"r.tfvars"}, OptionalVarFiles: []string{"o.tfvars"}, Environment: map[string]string{"TF_LOG": "INFO"}}, + }, + BeforeHooks: []*pluginv1.ComponentHook{ + {Name: "z-first", Commands: []string{"plan"}, Execute: []string{"tool", "1"}}, + {Name: "a-second", Commands: []string{"plan"}, Execute: []string{"tool", "2"}, WorkingDirectory: "sub", SuppressStdout: true}, + }, + AfterHooks: []*pluginv1.ComponentHook{{Name: "after", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, + ErrorHooks: []*pluginv1.ComponentHook{{Name: "oops", Commands: []string{"apply"}, Execute: []string{"tool"}, OnErrors: []string{"a", "b"}}}, + })) + got, err := preparedConfiguration(response) + require.NoError(t, err) + require.Equal(t, &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{ + {Commands: []string{"plan"}, Arguments: []string{"-lock=false"}}, + {Commands: []string{"apply", "plan"}, RequiredVarFiles: []string{"r.tfvars"}, OptionalVarFiles: []string{"o.tfvars"}, Environment: map[string]string{"TF_LOG": "INFO"}}, + }, + BeforeHooks: []schema.ComponentHook{ + {Name: "z-first", Commands: []string{"plan"}, Execute: []string{"tool", "1"}}, + {Name: "a-second", Commands: []string{"plan"}, Execute: []string{"tool", "2"}, WorkingDirectory: "sub", SuppressStdout: true}, + }, + AfterHooks: []schema.ComponentHook{{Name: "after", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, + ErrorHooks: []schema.ComponentHook{{Name: "oops", Commands: []string{"apply"}, Execute: []string{"tool"}, OnErrors: []string{"a", "b"}}}, + }, got.Execution) +} + +func TestPreparedConfigurationOmitsEmptyExecution(t *testing.T) { + for _, execution := range []*pluginv1.ComponentExecutionConfiguration{nil, {}} { + got, err := preparedConfiguration(roundTrip(t, executionResponse(t, execution))) + require.NoError(t, err) + require.Nil(t, got.Execution) + } +} + +func TestPreparedConfigurationExecutionIsIsolatedFromResponse(t *testing.T) { + response := executionResponse(t, &pluginv1.ComponentExecutionConfiguration{ + BeforeHooks: []*pluginv1.ComponentHook{{Name: "h", Commands: []string{"plan"}, Execute: []string{"tool", "a"}}}, + }) + got, err := preparedConfiguration(response) + require.NoError(t, err) + // Result to source. + got.Execution.BeforeHooks[0].Execute[1] = "changed" + require.Equal(t, []string{"tool", "a"}, response.Execution.BeforeHooks[0].Execute) + // Source to result. + response.Execution.BeforeHooks[0].Commands[0] = "apply" + require.Equal(t, []string{"plan"}, got.Execution.BeforeHooks[0].Commands) +} + +func TestPreparedConfigurationRejectsUnrunnableHooks(t *testing.T) { + for _, test := range []struct { + name string + execution *pluginv1.ComponentExecutionConfiguration + }{ + {"no command", &pluginv1.ComponentExecutionConfiguration{BeforeHooks: []*pluginv1.ComponentHook{{Name: "h", Commands: []string{"plan"}}}}}, + {"bad pattern", &pluginv1.ComponentExecutionConfiguration{ErrorHooks: []*pluginv1.ComponentHook{{Name: "h", Commands: []string{"plan"}, Execute: []string{"t"}, OnErrors: []string{"("}}}}}, + {"nil hook", &pluginv1.ComponentExecutionConfiguration{AfterHooks: []*pluginv1.ComponentHook{nil}}}, + } { + t.Run(test.name, func(t *testing.T) { + _, err := preparedConfiguration(executionResponse(t, test.execution)) + require.ErrorIs(t, err, errUtils.ErrPluginProtocol) + }) + } +} diff --git a/pkg/plugin/adapter/configuration/preparation_test.go b/pkg/plugin/adapter/configuration/preparation_test.go new file mode 100644 index 00000000000..b5da97f076f --- /dev/null +++ b/pkg/plugin/adapter/configuration/preparation_test.go @@ -0,0 +1,109 @@ +package configuration + +import ( + "context" + "errors" + "testing" + + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" + + errUtils "github.com/cloudposse/atmos/errors" + adapter "github.com/cloudposse/atmos/pkg/plugin/adapter/component" + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" + "github.com/cloudposse/atmos/pkg/schema" +) + +// preparationSession discovers one "app" component and returns the mock that will prepare it. +func preparationSession(t *testing.T) (*Session, *MockProvider, *schema.ConfigAndStacksInfo) { + t.Helper() + provider := NewMockProvider(gomock.NewController(t)) + provider.EXPECT().ComponentType().Return("terraform").AnyTimes() + provider.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{unit(t, "app")}}, nil).MaxTimes(1) + session, err := New(t.Context(), enabledConfig(t), "apply", func(context.Context, []string) (map[string]Provider, error) { + return map[string]Provider{"terragrunt": provider}, nil + }) + require.NoError(t, err) + info := &schema.ConfigAndStacksInfo{Stack: "live", ComponentFromArg: "app", SubCommand: "apply", ComponentSettingsSection: map[string]any{"configuration": true}} + return session, provider, info +} + +func TestPrepareTranslatesGeneratedArtifactsAndWatchedSources(t *testing.T) { + session, provider, info := preparationSession(t) + info.AdditionalArgsAndFlags = []string{"-var=a=1", "-lock=false"} + backend, err := adapter.EncodeObject(map[string]any{"bucket": "state"}) + require.NoError(t, err) + var sent *pluginv1.ComponentRequest + provider.EXPECT().Prepare(gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, request *pluginv1.ComponentRequest) (*pluginv1.PrepareComponentResponse, error) { + sent = request + return &pluginv1.PrepareComponentResponse{ + Engine: "opentofu", BackendGenerated: true, + State: &pluginv1.ComponentStateIdentity{BackendType: "s3", Backend: backend, Workspace: "stage"}, + Files: []*pluginv1.ComponentGeneratedFile{ + {Path: "backend.tf.json", Content: []byte("{}"), IfExists: "overwrite", Sensitive: true}, + {Path: "provider.tf", Content: []byte("p"), IfExists: "skip"}, + }, + Sources: []*pluginv1.ComponentSource{ + {Path: "root.hcl", Kind: pluginv1.ComponentSource_FILE}, + {Path: "modules", Kind: pluginv1.ComponentSource_DIRECTORY}, + }, + }, nil + }) + prepared, err := session.Prepare(t.Context(), info) + require.NoError(t, err) + + require.True(t, prepared.BackendGenerated) + require.Equal(t, []schema.ComponentConfigurationFile{ + {Path: "backend.tf.json", Content: []byte("{}"), IfExists: "overwrite", Sensitive: true}, + {Path: "provider.tf", Content: []byte("p"), IfExists: "skip"}, + }, prepared.Files) + require.Equal(t, []schema.ComponentConfigurationSource{{Path: "root.hcl"}, {Path: "modules", Directory: true}}, prepared.Sources) + + require.Equal(t, []string{"-var=a=1", "-lock=false"}, sent.Operation.TerraformArguments) + info.AdditionalArgsAndFlags[0] = "mutated" + require.Equal(t, "-var=a=1", sent.Operation.TerraformArguments[0], "the request owns a copy of the arguments") +} + +func TestPrepareFailsClosed(t *testing.T) { + failure := errors.New("provider crashed") + for _, test := range []struct { + name string + setup func(*MockProvider, *schema.ConfigAndStacksInfo) + want error + }{ + {"undiscovered component", func(_ *MockProvider, info *schema.ConfigAndStacksInfo) { info.ComponentFromArg = "absent" }, errUtils.ErrPluginInvalidConfig}, + {"provider failure", func(p *MockProvider, _ *schema.ConfigAndStacksInfo) { + p.EXPECT().Prepare(gomock.Any(), gomock.Any()).Return(nil, failure) + }, failure}, + {"engine changed by provider", func(p *MockProvider, _ *schema.ConfigAndStacksInfo) { + p.EXPECT().Prepare(gomock.Any(), gomock.Any()).Return(&pluginv1.PrepareComponentResponse{Engine: "terraform", State: &pluginv1.ComponentStateIdentity{}}, nil) + }, errUtils.ErrPluginProtocol}, + {"saved plan", func(_ *MockProvider, info *schema.ConfigAndStacksInfo) { info.UseTerraformPlan = true }, errUtils.ErrPluginUnsupportedExtension}, + } { + t.Run(test.name, func(t *testing.T) { + session, provider, info := preparationSession(t) + test.setup(provider, info) + prepared, err := session.Prepare(t.Context(), info) + require.ErrorIs(t, err, test.want) + require.Nil(t, prepared) + }) + } +} + +func TestPreparedExecutionRejectsNilEntriesInEveryList(t *testing.T) { + for _, test := range []struct { + name string + execution *pluginv1.ComponentExecutionConfiguration + }{ + {"extra arguments", &pluginv1.ComponentExecutionConfiguration{ExtraArguments: []*pluginv1.ComponentExtraArguments{nil}}}, + {"before hooks", &pluginv1.ComponentExecutionConfiguration{BeforeHooks: []*pluginv1.ComponentHook{nil}}}, + {"after hooks", &pluginv1.ComponentExecutionConfiguration{AfterHooks: []*pluginv1.ComponentHook{nil}}}, + {"error hooks", &pluginv1.ComponentExecutionConfiguration{ErrorHooks: []*pluginv1.ComponentHook{nil}}}, + } { + t.Run(test.name, func(t *testing.T) { + execution, err := preparedExecution(test.execution) + require.ErrorIs(t, err, errUtils.ErrPluginProtocol) + require.Nil(t, execution) + }) + } +} diff --git a/pkg/plugin/adapter/configuration/revision.go b/pkg/plugin/adapter/configuration/revision.go index 9ccf5733e3d..ac2a520237b 100644 --- a/pkg/plugin/adapter/configuration/revision.go +++ b/pkg/plugin/adapter/configuration/revision.go @@ -23,10 +23,22 @@ func (s *Session) ForRevision(root string) (schema.ComponentConfigurationRuntime if !filepath.IsAbs(root) { return nil, errUtils.ErrPluginInvalidConfig } - revision := &Session{ctx: s.ctx, root: root, command: s.command, loader: s.loader, done: make(chan struct{})} + s.importMu.Lock() + defer s.importMu.Unlock() + revision := &Session{ctx: s.ctx, root: root, command: s.command, loader: s.loader, done: make(chan struct{}), imports: make(map[string]bool), importProviders: make(map[string]*pluginv1.DiscoverComponentsRequest)} + // Explicit imports come from the revision's YAML, not the HEAD inventory. + for scheme, request := range s.importProviders { + cloned := proto.Clone(request).(*pluginv1.DiscoverComponentsRequest) + cloned.RootDirectory = root + revision.importProviders[scheme] = cloned + } for _, request := range s.requests { + if len(request.IncludePaths) == 0 { + continue + } cloned := proto.Clone(request).(*pluginv1.DiscoverComponentsRequest) cloned.RootDirectory = root + cloned.Imports = nil revision.requests = append(revision.requests, cloned) } return revision, nil diff --git a/pkg/plugin/adapter/configuration/revision_test.go b/pkg/plugin/adapter/configuration/revision_test.go index 079ab176b72..4ae1f2a9c89 100644 --- a/pkg/plugin/adapter/configuration/revision_test.go +++ b/pkg/plugin/adapter/configuration/revision_test.go @@ -58,3 +58,31 @@ func TestRevisionDiscoveryIsolated(t *testing.T) { _, err = revision.Discover(t.Context()) require.ErrorIs(t, err, context.Canceled) } + +func TestRevisionDropsImportOnlyScopeButKeepsImportProviders(t *testing.T) { + config := enabledConfig(t) + settings := config.Components.Terraform.ConfigurationProviders["terragrunt"] + settings.Discovery.Enabled = false + config.Components.Terraform.ConfigurationProviders["terragrunt"] = settings + head, err := New(t.Context(), config, "list", func(context.Context, []string) (map[string]Provider, error) { return nil, nil }) + require.NoError(t, err) + handled, err := head.RegisterImport(t.Context(), "terragrunt://live") + require.NoError(t, err) + require.True(t, handled) + require.Len(t, head.requests, 1, "HEAD imports create a pending import-only request") + + baseRoot := t.TempDir() + rebased, err := head.ForRevision(baseRoot) + require.NoError(t, err) + revision := rebased.(*Session) + require.Empty(t, revision.requests, "the revision must not inherit HEAD's import-only scope") + require.Equal(t, baseRoot, revision.importProviders["terragrunt"].RootDirectory) + require.Equal(t, config.BasePathAbsolute, head.importProviders["terragrunt"].RootDirectory, "HEAD's provider template is unchanged") + + // The revision accepts its own YAML imports independently of HEAD's. + handled, err = revision.RegisterImport(t.Context(), "terragrunt://previous") + require.NoError(t, err) + require.True(t, handled) + require.Equal(t, []string{"terragrunt://previous"}, revision.requests[0].Imports) + require.Equal(t, []string{"terragrunt://live"}, head.requests[0].Imports) +} diff --git a/pkg/plugin/adapter/configuration/session.go b/pkg/plugin/adapter/configuration/session.go index 80ada4de149..35f3f1b0fe9 100644 --- a/pkg/plugin/adapter/configuration/session.go +++ b/pkg/plugin/adapter/configuration/session.go @@ -10,6 +10,8 @@ import ( "strings" "sync" + "google.golang.org/protobuf/proto" + errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/merge" "github.com/cloudposse/atmos/pkg/perf" @@ -32,16 +34,20 @@ type Loader func(context.Context, []string) (map[string]Provider, error) // Session holds one command's discovery result; it is never a process-global cache. type Session struct { - ctx context.Context - root, command string - requests []*pluginv1.DiscoverComponentsRequest - loader Loader - once sync.Once - done chan struct{} - registry map[string]Provider - definitions map[string]map[string]any - instances map[[3]string]*pluginv1.DiscoveredComponent - err error + ctx context.Context + root, command string + requests []*pluginv1.DiscoverComponentsRequest + loader Loader + once sync.Once + done chan struct{} + registry map[string]Provider + definitions map[string]map[string]any + instances map[[3]string]*pluginv1.DiscoveredComponent + err error + importMu sync.Mutex + importProviders map[string]*pluginv1.DiscoverComponentsRequest + imports map[string]bool + started bool } // New snapshots the explicitly enabled scope without starting plugins or scanning directories. @@ -55,14 +61,15 @@ func New(ctx context.Context, config *schema.AtmosConfiguration, command string, if !filepath.IsAbs(root) || loader == nil { return nil, errUtils.ErrPluginInvalidConfig } - s := &Session{ctx: ctx, root: root, command: command, loader: loader, done: make(chan struct{})} + s := &Session{ctx: ctx, root: root, command: command, loader: loader, done: make(chan struct{}), importProviders: make(map[string]*pluginv1.DiscoverComponentsRequest), imports: make(map[string]bool)} seen := make(map[string]bool) for _, name := range slices.Sorted(maps.Keys(config.Components.Terraform.ConfigurationProviders)) { settings := config.Components.Terraform.ConfigurationProviders[name] - if !settings.Enabled || !settings.Discovery.Enabled { + if !settings.Enabled { continue } + scheme := name if settings.Provider != "" { name = settings.Provider } @@ -74,7 +81,10 @@ func New(ctx context.Context, config *schema.AtmosConfiguration, command string, if err != nil { return nil, err } - s.requests = append(s.requests, request) + s.importProviders[scheme] = proto.Clone(request).(*pluginv1.DiscoverComponentsRequest) + if settings.Discovery.Enabled { + s.requests = append(s.requests, request) + } } return s, nil } @@ -105,6 +115,9 @@ func (s *Session) ensure(ctx context.Context) error { return err } s.once.Do(func() { + s.importMu.Lock() + s.started = true + s.importMu.Unlock() go func() { defer close(s.done) call, cancel := context.WithCancel(s.ctx) @@ -139,7 +152,7 @@ func discoveryRequest(config *schema.AtmosConfiguration, settings *schema.Compon if settings.Mode != "" && settings.Mode != "native" { return nil, errUtils.ErrPluginUnsupportedExtension } - if len(settings.Discovery.IncludedPaths) == 0 { + if settings.Discovery.Enabled && len(settings.Discovery.IncludedPaths) == 0 { return nil, errUtils.ErrPluginInvalidConfig } for _, pattern := range append(slices.Clone(settings.Discovery.IncludedPaths), settings.Discovery.ExcludedPaths...) { @@ -151,7 +164,11 @@ func discoveryRequest(config *schema.AtmosConfiguration, settings *schema.Compon if err != nil { return nil, err } + includes := slices.Clone(settings.Discovery.IncludedPaths) + if !settings.Discovery.Enabled { + includes = nil + } env := environment() env["ATMOS_TERRAFORM_BINARY"] = config.Components.Terraform.Command - return &pluginv1.DiscoverComponentsRequest{Provider: name, RootDirectory: root, IncludePaths: slices.Clone(settings.Discovery.IncludedPaths), ExcludePaths: slices.Clone(settings.Discovery.ExcludedPaths), ProviderConfiguration: value, Operation: &pluginv1.ComponentOperation{Command: command, Mode: "native"}, Invocation: &pluginv1.Invocation{Environment: env}}, nil + return &pluginv1.DiscoverComponentsRequest{Provider: name, RootDirectory: root, IncludePaths: includes, ExcludePaths: slices.Clone(settings.Discovery.ExcludedPaths), ProviderConfiguration: value, Operation: &pluginv1.ComponentOperation{Command: command, Mode: "native"}, Invocation: &pluginv1.Invocation{Environment: env}}, nil } diff --git a/pkg/plugin/adapter/configuration/session_test.go b/pkg/plugin/adapter/configuration/session_test.go index bf9649591db..de248a07788 100644 --- a/pkg/plugin/adapter/configuration/session_test.go +++ b/pkg/plugin/adapter/configuration/session_test.go @@ -200,8 +200,9 @@ func TestPreparedConfigurationRejectsUnsupportedValues(t *testing.T) { require.NoError(t, err) require.Equal(t, filepath.Join(response.State.WorkingDirectory, "terraform.tfstate"), got.Backend["path"]) response.Files = []*pluginv1.ComponentGeneratedFile{{Path: "generated.tf", IfExists: "overwrite_terragrunt"}} - _, err = preparedConfiguration(response) - require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + got, err = preparedConfiguration(response) + require.NoError(t, err) + require.Equal(t, "overwrite_terragrunt", got.Files[0].IfExists) response.Files = nil response.Inputs = &pluginv1.ComponentValue{} _, err = preparedConfiguration(response) diff --git a/pkg/plugin/adapter/configuration/state.go b/pkg/plugin/adapter/configuration/state.go new file mode 100644 index 00000000000..cb0e6ee150c --- /dev/null +++ b/pkg/plugin/adapter/configuration/state.go @@ -0,0 +1,49 @@ +package configuration + +import ( + "context" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/perf" + adapter "github.com/cloudposse/atmos/pkg/plugin/adapter/component" + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" + "github.com/cloudposse/atmos/pkg/schema" +) + +// StateProvider is optional; discovery and preparation do not authorize state resolution. +type StateProvider interface { + ResolveState(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) +} + +// ResolveState locates state with no evaluator commands, writes, network, or output reads. +func (s *Session) ResolveState(ctx context.Context, info *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + defer perf.Track(nil, "configuration.Session.ResolveState")() + + if _, owned := info.ComponentSettingsSection["configuration"]; !owned { + return nil, nil + } + selected, err := s.componentRequest(ctx, info) + if err != nil { + return nil, err + } + resolver, ok := selected.provider.(StateProvider) + if !ok { + return nil, errUtils.ErrPluginUnsupportedExtension + } + result, err := resolver.ResolveState(ctx, selected.request) + if err != nil { + return nil, err + } + if result == nil || result.State == nil { + return nil, errUtils.ErrPluginProtocol + } + state := result.State + backend, err := adapter.DecodeObject(state.Backend) + if err != nil { + return nil, err + } + if state.BackendType == "local" { + resolveLocalBackend(backend, state.WorkingDirectory) + } + return &schema.ComponentConfigurationState{BackendType: state.BackendType, Backend: backend, Workspace: state.Workspace, WorkingDirectory: state.WorkingDirectory}, nil +} diff --git a/pkg/plugin/adapter/configuration/state_test.go b/pkg/plugin/adapter/configuration/state_test.go new file mode 100644 index 00000000000..1c648719a9e --- /dev/null +++ b/pkg/plugin/adapter/configuration/state_test.go @@ -0,0 +1,73 @@ +package configuration + +import ( + "context" + "path/filepath" + "testing" + + errUtils "github.com/cloudposse/atmos/errors" + adapter "github.com/cloudposse/atmos/pkg/plugin/adapter/component" + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" + "github.com/cloudposse/atmos/pkg/schema" + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" +) + +type stateProviderFixture struct { + Provider + resolve func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) +} + +func (p *stateProviderFixture) ResolveState(ctx context.Context, req *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + return p.resolve(ctx, req) +} + +func TestSessionResolvesStateWithoutPreparation(t *testing.T) { + config := enabledConfig(t) + mock := NewMockProvider(gomock.NewController(t)) + mock.EXPECT().ComponentType().Return("terraform") + mock.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{unit(t, "network")}}, nil) + backend, err := adapter.EncodeObject(map[string]any{"path": "state.tfstate"}) + require.NoError(t, err) + provider := &stateProviderFixture{Provider: mock, resolve: func(_ context.Context, req *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + require.Nil(t, req.Effects) + require.Equal(t, "output", req.Operation.Command) + require.Equal(t, "selected", req.Invocation.Environment["IDENTITY"]) + require.Equal(t, "stage", req.Invocation.Environment["TF_WORKSPACE"]) + return &pluginv1.ResolveComponentStateResponse{State: &pluginv1.ComponentStateIdentity{BackendType: "local", Backend: backend, Workspace: "stage", WorkingDirectory: config.BasePathAbsolute}}, nil + }} + session, err := New(t.Context(), config, "describe", func(context.Context, []string) (map[string]Provider, error) { + return map[string]Provider{"terragrunt": provider}, nil + }) + require.NoError(t, err) + info := &schema.ConfigAndStacksInfo{Stack: "live", ComponentFromArg: "network", SubCommand: "output", TerraformWorkspace: "stage", ComponentSettingsSection: map[string]any{"configuration": true}, SanitizedEnv: []string{"IDENTITY=selected"}} + state, err := session.ResolveState(t.Context(), info) + require.NoError(t, err) + require.Equal(t, filepath.Join(config.BasePathAbsolute, "state.tfstate"), state.Backend["path"]) + require.Equal(t, "stage", state.Workspace) + provider.resolve = func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + return nil, context.Canceled + } + _, err = session.ResolveState(t.Context(), info) + require.ErrorIs(t, err, context.Canceled) + provider.resolve = func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + return &pluginv1.ResolveComponentStateResponse{}, nil + } + _, err = session.ResolveState(t.Context(), info) + require.ErrorIs(t, err, errUtils.ErrPluginProtocol) + provider.resolve = func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + return &pluginv1.ResolveComponentStateResponse{State: &pluginv1.ComponentStateIdentity{Backend: &pluginv1.ComponentValue{}}}, nil + } + _, err = session.ResolveState(t.Context(), info) + require.ErrorIs(t, err, errUtils.ErrPluginProtocol) + session.registry["terragrunt"] = mock + _, err = session.ResolveState(t.Context(), info) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + info.ComponentFromArg = "absent" + _, err = session.ResolveState(t.Context(), info) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) + info.ComponentSettingsSection = nil + state, err = session.ResolveState(t.Context(), info) + require.NoError(t, err) + require.Nil(t, state) +} diff --git a/pkg/plugin/adapter/configuration/toolchain_boundary_test.go b/pkg/plugin/adapter/configuration/toolchain_boundary_test.go new file mode 100644 index 00000000000..45450f4d70e --- /dev/null +++ b/pkg/plugin/adapter/configuration/toolchain_boundary_test.go @@ -0,0 +1,108 @@ +package configuration + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" + "google.golang.org/protobuf/proto" + + adapter "github.com/cloudposse/atmos/pkg/plugin/adapter/component" + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" + "github.com/cloudposse/atmos/pkg/schema" +) + +// preparedRequest runs Session.Prepare against a recording provider and returns the exact +// request the plugin would receive. The request is then round-tripped through the wire +// encoding, because the environment crosses a process boundary in production. +func preparedRequest(t *testing.T, info *schema.ConfigAndStacksInfo) *pluginv1.ComponentRequest { + t.Helper() + config := enabledConfig(t) + provider := NewMockProvider(gomock.NewController(t)) + provider.EXPECT().ComponentType().Return("terraform").AnyTimes() + component := unit(t, "app") + provider.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{component}}, nil) + inputs, err := adapter.EncodeObject(map[string]any{}) + require.NoError(t, err) + backend, err := adapter.EncodeObject(map[string]any{"path": "state.tfstate"}) + require.NoError(t, err) + var captured *pluginv1.ComponentRequest + provider.EXPECT().Prepare(gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, request *pluginv1.ComponentRequest) (*pluginv1.PrepareComponentResponse, error) { + wire, err := proto.Marshal(request) + require.NoError(t, err) + captured = &pluginv1.ComponentRequest{} + require.NoError(t, proto.Unmarshal(wire, captured)) + return &pluginv1.PrepareComponentResponse{Engine: "opentofu", ModuleSource: "/module", Inputs: inputs, State: &pluginv1.ComponentStateIdentity{BackendType: "local", Backend: backend, Workspace: "default", WorkingDirectory: config.BasePathAbsolute}}, nil + }) + session, err := New(t.Context(), config, "plan", func(context.Context, []string) (map[string]Provider, error) { + return map[string]Provider{"terragrunt": provider}, nil + }) + require.NoError(t, err) + info.Stack, info.ComponentFromArg, info.SubCommand = "live", "app", "plan" + info.ComponentSettingsSection = map[string]any{"configuration": true} + _, err = session.Prepare(t.Context(), info) + require.NoError(t, err) + require.NotNil(t, captured) + return captured +} + +// The toolchain PATH, the pinned executable, and the identity environment must survive +// the host-to-plugin boundary exactly as the host assembled them, and nothing ambient may +// be added once an identity has produced an authoritative environment. +func TestToolchainEnvironmentCrossesThePluginBoundary(t *testing.T) { + toolchain := filepath.Join(t.TempDir(), "bin") + ambient := t.TempDir() + t.Setenv("PATH", ambient) + t.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", "ambient-token-must-not-return") + t.Setenv("ATMOS_AMBIENT_ONLY", "ambient") + pinned := filepath.Join(toolchain, "tofu") + toolchainPath := toolchain + string(os.PathListSeparator) + ambient + + request := preparedRequest(t, &schema.ConfigAndStacksInfo{ + Command: pinned, + Identity: "production", + // The sanitized environment is what the selected identity produced. + SanitizedEnv: []string{"PATH=" + ambient, "AWS_PROFILE=production", "SHARED=identity"}, + // Component environment follows, and the toolchain PATH is appended last. + ComponentEnvList: []string{ + "SHARED=component", "CUSTOM_EMPTY=", "ATMOS_TERRAFORM_BINARY=stack-must-not-win", + "PATH=" + toolchainPath, + }, + TerraformWorkspace: "stage", + }) + environment := request.Invocation.Environment + + require.Equal(t, toolchainPath, environment["PATH"], "the toolchain PATH must reach the plugin intact") + require.Equal(t, pinned, environment["ATMOS_TERRAFORM_BINARY"], "the pinned executable is host-owned and cannot be overridden by stack env") + require.Equal(t, "production", environment["AWS_PROFILE"]) + require.Equal(t, "component", environment["SHARED"], "component environment wins over the identity environment") + value, present := environment["CUSTOM_EMPTY"] + require.True(t, present, "an explicitly empty value must be preserved") + require.Empty(t, value) + require.Equal(t, "stage", environment["TF_WORKSPACE"]) + for _, leaked := range []string{"AWS_WEB_IDENTITY_TOKEN_FILE", "ATMOS_AMBIENT_ONLY"} { + require.NotContains(t, environment, leaked, "ambient values must not return behind an identity") + } +} + +// Without an identity the ambient environment is the base, the component PATH still wins, +// and two preparations in one process never share environment state. +func TestToolchainEnvironmentWithoutIdentityAndAcrossPreparations(t *testing.T) { + ambient := t.TempDir() + t.Setenv("PATH", ambient) + t.Setenv("ATMOS_AMBIENT_ONLY", "ambient") + first := preparedRequest(t, &schema.ConfigAndStacksInfo{Command: "/first/tofu", ComponentEnvList: []string{"PATH=/first/bin", "ONLY_FIRST=1"}}) + second := preparedRequest(t, &schema.ConfigAndStacksInfo{Command: "/second/tofu", ComponentEnvList: []string{"PATH=/second/bin"}}) + + require.Equal(t, "/first/bin", first.Invocation.Environment["PATH"]) + require.Equal(t, "/first/tofu", first.Invocation.Environment["ATMOS_TERRAFORM_BINARY"]) + require.Equal(t, "1", first.Invocation.Environment["ONLY_FIRST"]) + require.Equal(t, "ambient", first.Invocation.Environment["ATMOS_AMBIENT_ONLY"]) + + require.Equal(t, "/second/bin", second.Invocation.Environment["PATH"]) + require.Equal(t, "/second/tofu", second.Invocation.Environment["ATMOS_TERRAFORM_BINARY"]) + require.NotContains(t, second.Invocation.Environment, "ONLY_FIRST", "one preparation's environment must not leak into the next") +} diff --git a/pkg/plugin/manage/lifecycle_test.go b/pkg/plugin/manage/lifecycle_test.go index c339ee85596..5a904302fae 100644 --- a/pkg/plugin/manage/lifecycle_test.go +++ b/pkg/plugin/manage/lifecycle_test.go @@ -35,22 +35,22 @@ func TestLifecyclePreservesDeclarationAndRemovesRelease(t *testing.T) { m, source, _ := setup(t) require.NoError(t, m.Add(t.Context(), manage.AddOptions{Source: source})) reloadConfiguration(t, m) - require.Contains(t, m.Config.Plugins.Packages, "example-fixture") + require.Contains(t, m.Config.Plugins.Packages, "fixture") // Settings the user wrote beside source and version must survive a version change. - configured, err := edit.SetJSON(readConfig(t, m), "plugins.packages.example-fixture.config", []byte(`{"issuer":"https://issuer.example.com"}`)) + configured, err := edit.SetJSON(readConfig(t, m), "plugins.packages.fixture.config", []byte(`{"issuer":"https://issuer.example.com"}`)) require.NoError(t, err) require.NoError(t, os.WriteFile(filepath.Join(m.Project, m.ConfigFile), configured, 0o644)) reloadConfiguration(t, m) - require.NoError(t, m.Add(t.Context(), manage.AddOptions{Alias: "example-fixture", Source: source, Constraint: "^1"})) + require.NoError(t, m.Add(t.Context(), manage.AddOptions{Alias: "fixture", Source: source, Constraint: "^1"})) reloadConfiguration(t, m) - require.Equal(t, map[string]any{"issuer": "https://issuer.example.com"}, m.Config.Plugins.Packages["example-fixture"].Config) - require.NoError(t, m.Restore(t.Context(), manage.RestoreOptions{Update: true, Alias: "example-fixture"})) + require.Equal(t, map[string]any{"issuer": "https://issuer.example.com"}, m.Config.Plugins.Packages["fixture"].Config) + require.NoError(t, m.Restore(t.Context(), manage.RestoreOptions{Update: true, Alias: "fixture"})) require.ErrorIs(t, m.Restore(t.Context(), manage.RestoreOptions{Alias: "missing"}), errUtils.ErrPluginNotInstalled) require.ErrorIs(t, m.Add(t.Context(), manage.AddOptions{Alias: "invalid alias", Source: source}), errUtils.ErrPluginInvalidConfig) require.ErrorIs(t, m.Add(t.Context(), manage.AddOptions{Alias: "duplicate", Source: source}), errUtils.ErrDuplicatePluginExtension) require.ErrorIs(t, m.Remove(t.Context(), "invalid alias"), errUtils.ErrPluginInvalidConfig) require.ErrorIs(t, m.Remove(t.Context(), "missing"), errUtils.ErrPluginNotInstalled) - require.NoError(t, m.Remove(t.Context(), "example-fixture")) + require.NoError(t, m.Remove(t.Context(), "fixture")) reloadConfiguration(t, m) require.Empty(t, m.Config.Plugins.Packages) locked, err := pluginlock.Load(m.Project) diff --git a/pkg/plugin/manage/manage.go b/pkg/plugin/manage/manage.go index 23075f44e7b..7189b816272 100644 --- a/pkg/plugin/manage/manage.go +++ b/pkg/plugin/manage/manage.go @@ -65,7 +65,7 @@ func (m *Manager) Add(ctx context.Context, options AddOptions) error { } alias := plan.alias if alias == "" { - alias = strings.ReplaceAll(entry.Plugin, "/", "-") + alias = defaultAlias(entry.Plugin) if err := m.checkDerivedAlias(alias, options.Source, file); err != nil { return nil, err } diff --git a/pkg/plugin/manage/pin.go b/pkg/plugin/manage/pin.go index f3b8b8e667d..8902387afbf 100644 --- a/pkg/plugin/manage/pin.go +++ b/pkg/plugin/manage/pin.go @@ -4,6 +4,7 @@ import ( "fmt" "maps" "slices" + "strings" "github.com/Masterminds/semver/v3" @@ -98,3 +99,9 @@ func invalidAlias(alias string) error { WithHint("Aliases start with a lowercase letter and contain only lowercase letters, digits, `-` and `_`."). Err() } + +// defaultAlias derives the short product alias used when no alias is given. +func defaultAlias(packageID string) string { + _, product, _ := strings.Cut(packageID, "/") + return product +} diff --git a/pkg/plugin/manage/pin_test.go b/pkg/plugin/manage/pin_test.go index e861ca5ba50..53acee214fb 100644 --- a/pkg/plugin/manage/pin_test.go +++ b/pkg/plugin/manage/pin_test.go @@ -121,7 +121,7 @@ func TestDerivedAliasCannotReplaceAnotherSource(t *testing.T) { for _, owner := range []string{"declaration and lock", "declaration only", "lock only"} { t.Run(owner, func(t *testing.T) { manager, source, directory := setup(t) - require.NoError(t, manager.Add(t.Context(), manage.AddOptions{Alias: "example-fixture", Source: source})) + require.NoError(t, manager.Add(t.Context(), manage.AddOptions{Alias: "fixture", Source: source})) reloadConfiguration(t, manager) other := publishVersions(t, manager, source, directory, "mirror", "1.0.0") switch owner { @@ -160,3 +160,12 @@ func TestExplicitAliasMayReplaceAnotherSource(t *testing.T) { require.NoError(t, err) require.Equal(t, other, locked.Spec.Packages["example-fixture"].Source) } + +func TestDefaultAliasUsesProductName(t *testing.T) { + manager, source, _ := setup(t) + require.NoError(t, manager.Add(t.Context(), manage.AddOptions{Source: source})) + locked, err := pluginlock.Load(manager.Project) + require.NoError(t, err) + require.Contains(t, locked.Spec.Packages, "fixture") + require.NotContains(t, locked.Spec.Packages, "example-fixture") +} diff --git a/pkg/plugin/proto/atmos/plugin/v1/plugin.pb.go b/pkg/plugin/proto/atmos/plugin/v1/plugin.pb.go index 5ec7330748a..5228242498d 100644 --- a/pkg/plugin/proto/atmos/plugin/v1/plugin.pb.go +++ b/pkg/plugin/proto/atmos/plugin/v1/plugin.pb.go @@ -2326,12 +2326,15 @@ func (x *ComponentEffects) GetStateReads() bool { // distinguishes an unset option from an explicit false, zero, or empty value. // These are not Terraform argv. The provider validates supported semantics. type ComponentOperation struct { - state protoimpl.MessageState `protogen:"open.v1"` - Command string `protobuf:"bytes,1,opt,name=command,proto3" json:"command,omitempty"` - Mode string `protobuf:"bytes,2,opt,name=mode,proto3" json:"mode,omitempty"` - Options map[string]*ComponentValue `protobuf:"bytes,3,rep,name=options,proto3" json:"options,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache + state protoimpl.MessageState `protogen:"open.v1"` + Command string `protobuf:"bytes,1,opt,name=command,proto3" json:"command,omitempty"` + Mode string `protobuf:"bytes,2,opt,name=mode,proto3" json:"mode,omitempty"` + Options map[string]*ComponentValue `protobuf:"bytes,3,rep,name=options,proto3" json:"options,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` + // Resolved engine arguments are distinct from provider options. They let + // configuration semantics inspect flags such as plan/apply -destroy. + TerraformArguments []string `protobuf:"bytes,4,rep,name=terraform_arguments,json=terraformArguments,proto3" json:"terraform_arguments,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache } func (x *ComponentOperation) Reset() { @@ -2385,6 +2388,13 @@ func (x *ComponentOperation) GetOptions() map[string]*ComponentValue { return nil } +func (x *ComponentOperation) GetTerraformArguments() []string { + if x != nil { + return x.TerraformArguments + } + return nil +} + type DiscoverComponentsRequest struct { state protoimpl.MessageState `protogen:"open.v1"` Invocation *Invocation `protobuf:"bytes,1,opt,name=invocation,proto3" json:"invocation,omitempty"` @@ -2803,6 +2813,246 @@ func (x *ComponentGeneratedFile) GetSensitive() bool { return false } +// Evaluated command metadata; the host executes it with the invocation's +// environment, toolchain, cancellation, and I/O. Lists retain declaration order. +type ComponentExecutionConfiguration struct { + state protoimpl.MessageState `protogen:"open.v1"` + ExtraArguments []*ComponentExtraArguments `protobuf:"bytes,1,rep,name=extra_arguments,json=extraArguments,proto3" json:"extra_arguments,omitempty"` + BeforeHooks []*ComponentHook `protobuf:"bytes,2,rep,name=before_hooks,json=beforeHooks,proto3" json:"before_hooks,omitempty"` + AfterHooks []*ComponentHook `protobuf:"bytes,3,rep,name=after_hooks,json=afterHooks,proto3" json:"after_hooks,omitempty"` + ErrorHooks []*ComponentHook `protobuf:"bytes,4,rep,name=error_hooks,json=errorHooks,proto3" json:"error_hooks,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ComponentExecutionConfiguration) Reset() { + *x = ComponentExecutionConfiguration{} + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[40] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ComponentExecutionConfiguration) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ComponentExecutionConfiguration) ProtoMessage() {} + +func (x *ComponentExecutionConfiguration) ProtoReflect() protoreflect.Message { + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[40] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ComponentExecutionConfiguration.ProtoReflect.Descriptor instead. +func (*ComponentExecutionConfiguration) Descriptor() ([]byte, []int) { + return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{40} +} + +func (x *ComponentExecutionConfiguration) GetExtraArguments() []*ComponentExtraArguments { + if x != nil { + return x.ExtraArguments + } + return nil +} + +func (x *ComponentExecutionConfiguration) GetBeforeHooks() []*ComponentHook { + if x != nil { + return x.BeforeHooks + } + return nil +} + +func (x *ComponentExecutionConfiguration) GetAfterHooks() []*ComponentHook { + if x != nil { + return x.AfterHooks + } + return nil +} + +func (x *ComponentExecutionConfiguration) GetErrorHooks() []*ComponentHook { + if x != nil { + return x.ErrorHooks + } + return nil +} + +type ComponentExtraArguments struct { + state protoimpl.MessageState `protogen:"open.v1"` + Commands []string `protobuf:"bytes,1,rep,name=commands,proto3" json:"commands,omitempty"` + Arguments []string `protobuf:"bytes,2,rep,name=arguments,proto3" json:"arguments,omitempty"` + RequiredVarFiles []string `protobuf:"bytes,3,rep,name=required_var_files,json=requiredVarFiles,proto3" json:"required_var_files,omitempty"` + OptionalVarFiles []string `protobuf:"bytes,4,rep,name=optional_var_files,json=optionalVarFiles,proto3" json:"optional_var_files,omitempty"` + Environment map[string]string `protobuf:"bytes,5,rep,name=environment,proto3" json:"environment,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ComponentExtraArguments) Reset() { + *x = ComponentExtraArguments{} + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[41] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ComponentExtraArguments) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ComponentExtraArguments) ProtoMessage() {} + +func (x *ComponentExtraArguments) ProtoReflect() protoreflect.Message { + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[41] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ComponentExtraArguments.ProtoReflect.Descriptor instead. +func (*ComponentExtraArguments) Descriptor() ([]byte, []int) { + return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{41} +} + +func (x *ComponentExtraArguments) GetCommands() []string { + if x != nil { + return x.Commands + } + return nil +} + +func (x *ComponentExtraArguments) GetArguments() []string { + if x != nil { + return x.Arguments + } + return nil +} + +func (x *ComponentExtraArguments) GetRequiredVarFiles() []string { + if x != nil { + return x.RequiredVarFiles + } + return nil +} + +func (x *ComponentExtraArguments) GetOptionalVarFiles() []string { + if x != nil { + return x.OptionalVarFiles + } + return nil +} + +func (x *ComponentExtraArguments) GetEnvironment() map[string]string { + if x != nil { + return x.Environment + } + return nil +} + +type ComponentHook struct { + state protoimpl.MessageState `protogen:"open.v1"` + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Commands []string `protobuf:"bytes,2,rep,name=commands,proto3" json:"commands,omitempty"` + Execute []string `protobuf:"bytes,3,rep,name=execute,proto3" json:"execute,omitempty"` + // Empty means the provisioned module directory; relative paths resolve there. + WorkingDirectory string `protobuf:"bytes,4,opt,name=working_directory,json=workingDirectory,proto3" json:"working_directory,omitempty"` + RunOnError bool `protobuf:"varint,5,opt,name=run_on_error,json=runOnError,proto3" json:"run_on_error,omitempty"` + SuppressStdout bool `protobuf:"varint,6,opt,name=suppress_stdout,json=suppressStdout,proto3" json:"suppress_stdout,omitempty"` + // Error hooks run when any pattern matches the errors from preceding phases. + OnErrors []string `protobuf:"bytes,7,rep,name=on_errors,json=onErrors,proto3" json:"on_errors,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ComponentHook) Reset() { + *x = ComponentHook{} + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[42] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ComponentHook) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ComponentHook) ProtoMessage() {} + +func (x *ComponentHook) ProtoReflect() protoreflect.Message { + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[42] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ComponentHook.ProtoReflect.Descriptor instead. +func (*ComponentHook) Descriptor() ([]byte, []int) { + return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{42} +} + +func (x *ComponentHook) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *ComponentHook) GetCommands() []string { + if x != nil { + return x.Commands + } + return nil +} + +func (x *ComponentHook) GetExecute() []string { + if x != nil { + return x.Execute + } + return nil +} + +func (x *ComponentHook) GetWorkingDirectory() string { + if x != nil { + return x.WorkingDirectory + } + return "" +} + +func (x *ComponentHook) GetRunOnError() bool { + if x != nil { + return x.RunOnError + } + return false +} + +func (x *ComponentHook) GetSuppressStdout() bool { + if x != nil { + return x.SuppressStdout + } + return false +} + +func (x *ComponentHook) GetOnErrors() []string { + if x != nil { + return x.OnErrors + } + return nil +} + type PrepareComponentResponse struct { state protoimpl.MessageState `protogen:"open.v1"` Engine string `protobuf:"bytes,1,opt,name=engine,proto3" json:"engine,omitempty"` @@ -2814,13 +3064,17 @@ type PrepareComponentResponse struct { Dependencies []*ComponentDependency `protobuf:"bytes,7,rep,name=dependencies,proto3" json:"dependencies,omitempty"` Sources []*ComponentSource `protobuf:"bytes,8,rep,name=sources,proto3" json:"sources,omitempty"` Diagnostics []*Diagnostic `protobuf:"bytes,9,rep,name=diagnostics,proto3" json:"diagnostics,omitempty"` + // A returned generated file owns the backend block. The host must not also + // emit its default backend file. + BackendGenerated bool `protobuf:"varint,10,opt,name=backend_generated,json=backendGenerated,proto3" json:"backend_generated,omitempty"` + Execution *ComponentExecutionConfiguration `protobuf:"bytes,11,opt,name=execution,proto3" json:"execution,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } func (x *PrepareComponentResponse) Reset() { *x = PrepareComponentResponse{} - mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[40] + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[43] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -2832,7 +3086,7 @@ func (x *PrepareComponentResponse) String() string { func (*PrepareComponentResponse) ProtoMessage() {} func (x *PrepareComponentResponse) ProtoReflect() protoreflect.Message { - mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[40] + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[43] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -2845,7 +3099,7 @@ func (x *PrepareComponentResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use PrepareComponentResponse.ProtoReflect.Descriptor instead. func (*PrepareComponentResponse) Descriptor() ([]byte, []int) { - return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{40} + return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{43} } func (x *PrepareComponentResponse) GetEngine() string { @@ -2911,6 +3165,20 @@ func (x *PrepareComponentResponse) GetDiagnostics() []*Diagnostic { return nil } +func (x *PrepareComponentResponse) GetBackendGenerated() bool { + if x != nil { + return x.BackendGenerated + } + return false +} + +func (x *PrepareComponentResponse) GetExecution() *ComponentExecutionConfiguration { + if x != nil { + return x.Execution + } + return nil +} + type ResolveComponentStateResponse struct { state protoimpl.MessageState `protogen:"open.v1"` State *ComponentStateIdentity `protobuf:"bytes,1,opt,name=state,proto3" json:"state,omitempty"` @@ -2922,7 +3190,7 @@ type ResolveComponentStateResponse struct { func (x *ResolveComponentStateResponse) Reset() { *x = ResolveComponentStateResponse{} - mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[41] + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[44] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -2934,7 +3202,7 @@ func (x *ResolveComponentStateResponse) String() string { func (*ResolveComponentStateResponse) ProtoMessage() {} func (x *ResolveComponentStateResponse) ProtoReflect() protoreflect.Message { - mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[41] + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[44] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -2947,7 +3215,7 @@ func (x *ResolveComponentStateResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use ResolveComponentStateResponse.ProtoReflect.Descriptor instead. func (*ResolveComponentStateResponse) Descriptor() ([]byte, []int) { - return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{41} + return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{44} } func (x *ResolveComponentStateResponse) GetState() *ComponentStateIdentity { @@ -3158,11 +3426,12 @@ const file_atmos_plugin_v1_plugin_proto_rawDesc = "" + "\asecrets\x18\x03 \x01(\bR\asecrets\x12\x16\n" + "\x06writes\x18\x04 \x01(\bR\x06writes\x12\x1f\n" + "\vstate_reads\x18\x05 \x01(\bR\n" + - "stateReads\"\xeb\x01\n" + + "stateReads\"\x9c\x02\n" + "\x12ComponentOperation\x12\x18\n" + "\acommand\x18\x01 \x01(\tR\acommand\x12\x12\n" + "\x04mode\x18\x02 \x01(\tR\x04mode\x12J\n" + - "\aoptions\x18\x03 \x03(\v20.atmos.plugin.v1.ComponentOperation.OptionsEntryR\aoptions\x1a[\n" + + "\aoptions\x18\x03 \x03(\v20.atmos.plugin.v1.ComponentOperation.OptionsEntryR\aoptions\x12/\n" + + "\x13terraform_arguments\x18\x04 \x03(\tR\x12terraformArguments\x1a[\n" + "\fOptionsEntry\x12\x10\n" + "\x03key\x18\x01 \x01(\tR\x03key\x125\n" + "\x05value\x18\x02 \x01(\v2\x1f.atmos.plugin.v1.ComponentValueR\x05value:\x028\x01\"\xf3\x03\n" + @@ -3206,7 +3475,32 @@ const file_atmos_plugin_v1_plugin_proto_rawDesc = "" + "\x04path\x18\x01 \x01(\tR\x04path\x12\x18\n" + "\acontent\x18\x02 \x01(\fR\acontent\x12\x1b\n" + "\tif_exists\x18\x03 \x01(\tR\bifExists\x12\x1c\n" + - "\tsensitive\x18\x04 \x01(\bR\tsensitive\"\x80\x04\n" + + "\tsensitive\x18\x04 \x01(\bR\tsensitive\"\xb9\x02\n" + + "\x1fComponentExecutionConfiguration\x12Q\n" + + "\x0fextra_arguments\x18\x01 \x03(\v2(.atmos.plugin.v1.ComponentExtraArgumentsR\x0eextraArguments\x12A\n" + + "\fbefore_hooks\x18\x02 \x03(\v2\x1e.atmos.plugin.v1.ComponentHookR\vbeforeHooks\x12?\n" + + "\vafter_hooks\x18\x03 \x03(\v2\x1e.atmos.plugin.v1.ComponentHookR\n" + + "afterHooks\x12?\n" + + "\verror_hooks\x18\x04 \x03(\v2\x1e.atmos.plugin.v1.ComponentHookR\n" + + "errorHooks\"\xcc\x02\n" + + "\x17ComponentExtraArguments\x12\x1a\n" + + "\bcommands\x18\x01 \x03(\tR\bcommands\x12\x1c\n" + + "\targuments\x18\x02 \x03(\tR\targuments\x12,\n" + + "\x12required_var_files\x18\x03 \x03(\tR\x10requiredVarFiles\x12,\n" + + "\x12optional_var_files\x18\x04 \x03(\tR\x10optionalVarFiles\x12[\n" + + "\venvironment\x18\x05 \x03(\v29.atmos.plugin.v1.ComponentExtraArguments.EnvironmentEntryR\venvironment\x1a>\n" + + "\x10EnvironmentEntry\x12\x10\n" + + "\x03key\x18\x01 \x01(\tR\x03key\x12\x14\n" + + "\x05value\x18\x02 \x01(\tR\x05value:\x028\x01\"\xee\x01\n" + + "\rComponentHook\x12\x12\n" + + "\x04name\x18\x01 \x01(\tR\x04name\x12\x1a\n" + + "\bcommands\x18\x02 \x03(\tR\bcommands\x12\x18\n" + + "\aexecute\x18\x03 \x03(\tR\aexecute\x12+\n" + + "\x11working_directory\x18\x04 \x01(\tR\x10workingDirectory\x12 \n" + + "\frun_on_error\x18\x05 \x01(\bR\n" + + "runOnError\x12'\n" + + "\x0fsuppress_stdout\x18\x06 \x01(\bR\x0esuppressStdout\x12\x1b\n" + + "\ton_errors\x18\a \x03(\tR\bonErrors\"\xfd\x04\n" + "\x18PrepareComponentResponse\x12\x16\n" + "\x06engine\x18\x01 \x01(\tR\x06engine\x12+\n" + "\x11working_directory\x18\x02 \x01(\tR\x10workingDirectory\x12#\n" + @@ -3216,7 +3510,10 @@ const file_atmos_plugin_v1_plugin_proto_rawDesc = "" + "\x05files\x18\x06 \x03(\v2'.atmos.plugin.v1.ComponentGeneratedFileR\x05files\x12H\n" + "\fdependencies\x18\a \x03(\v2$.atmos.plugin.v1.ComponentDependencyR\fdependencies\x12:\n" + "\asources\x18\b \x03(\v2 .atmos.plugin.v1.ComponentSourceR\asources\x12=\n" + - "\vdiagnostics\x18\t \x03(\v2\x1b.atmos.plugin.v1.DiagnosticR\vdiagnostics\"\xd9\x01\n" + + "\vdiagnostics\x18\t \x03(\v2\x1b.atmos.plugin.v1.DiagnosticR\vdiagnostics\x12+\n" + + "\x11backend_generated\x18\n" + + " \x01(\bR\x10backendGenerated\x12N\n" + + "\texecution\x18\v \x01(\v20.atmos.plugin.v1.ComponentExecutionConfigurationR\texecution\"\xd9\x01\n" + "\x1dResolveComponentStateResponse\x12=\n" + "\x05state\x18\x01 \x01(\v2'.atmos.plugin.v1.ComponentStateIdentityR\x05state\x12:\n" + "\asources\x18\x02 \x03(\v2 .atmos.plugin.v1.ComponentSourceR\asources\x12=\n" + @@ -3264,81 +3561,85 @@ func file_atmos_plugin_v1_plugin_proto_rawDescGZIP() []byte { var ( file_atmos_plugin_v1_plugin_proto_enumTypes = make([]protoimpl.EnumInfo, 2) - file_atmos_plugin_v1_plugin_proto_msgTypes = make([]protoimpl.MessageInfo, 49) + file_atmos_plugin_v1_plugin_proto_msgTypes = make([]protoimpl.MessageInfo, 53) file_atmos_plugin_v1_plugin_proto_goTypes = []any{ - ComponentSource_Kind(0), // 0: atmos.plugin.v1.ComponentSource.Kind - ComponentDependency_Kind(0), // 1: atmos.plugin.v1.ComponentDependency.Kind - (*Invocation)(nil), // 2: atmos.plugin.v1.Invocation - (*Credentials)(nil), // 3: atmos.plugin.v1.Credentials - (*ServiceVersionRange)(nil), // 4: atmos.plugin.v1.ServiceVersionRange - (*Export)(nil), // 5: atmos.plugin.v1.Export - (*DescribeRequest)(nil), // 6: atmos.plugin.v1.DescribeRequest - (*DescribeResponse)(nil), // 7: atmos.plugin.v1.DescribeResponse - (*ConfigureRequest)(nil), // 8: atmos.plugin.v1.ConfigureRequest - (*ProviderRequest)(nil), // 9: atmos.plugin.v1.ProviderRequest - (*IdentityRequest)(nil), // 10: atmos.plugin.v1.IdentityRequest - (*Progress)(nil), // 11: atmos.plugin.v1.Progress - (*Interaction)(nil), // 12: atmos.plugin.v1.Interaction - (*AuthenticateEvent)(nil), // 13: atmos.plugin.v1.AuthenticateEvent - (*Diagnostic)(nil), // 14: atmos.plugin.v1.Diagnostic - (*ValidateResponse)(nil), // 15: atmos.plugin.v1.ValidateResponse - (*Unsupported)(nil), // 16: atmos.plugin.v1.Unsupported - (*RefreshRequest)(nil), // 17: atmos.plugin.v1.RefreshRequest - (*RefreshResponse)(nil), // 18: atmos.plugin.v1.RefreshResponse - (*EnvironmentResponse)(nil), // 19: atmos.plugin.v1.EnvironmentResponse - (*PrepareEnvironmentRequest)(nil), // 20: atmos.plugin.v1.PrepareEnvironmentRequest - (*CredentialPath)(nil), // 21: atmos.plugin.v1.CredentialPath - (*PathsResponse)(nil), // 22: atmos.plugin.v1.PathsResponse - (*IdentityAuthenticateRequest)(nil), // 23: atmos.plugin.v1.IdentityAuthenticateRequest - (*IdentityPrepareRequest)(nil), // 24: atmos.plugin.v1.IdentityPrepareRequest - (*CommandRequest)(nil), // 25: atmos.plugin.v1.CommandRequest - (*CommandExit)(nil), // 26: atmos.plugin.v1.CommandExit - (*CommandEvent)(nil), // 27: atmos.plugin.v1.CommandEvent - (*ComponentValue)(nil), // 28: atmos.plugin.v1.ComponentValue - (*ComponentValuePath)(nil), // 29: atmos.plugin.v1.ComponentValuePath - (*ComponentValueStep)(nil), // 30: atmos.plugin.v1.ComponentValueStep - (*ComponentAddress)(nil), // 31: atmos.plugin.v1.ComponentAddress - (*ComponentSource)(nil), // 32: atmos.plugin.v1.ComponentSource - (*ComponentDependency)(nil), // 33: atmos.plugin.v1.ComponentDependency - (*DiscoveredComponent)(nil), // 34: atmos.plugin.v1.DiscoveredComponent - (*ComponentEffects)(nil), // 35: atmos.plugin.v1.ComponentEffects - (*ComponentOperation)(nil), // 36: atmos.plugin.v1.ComponentOperation - (*DiscoverComponentsRequest)(nil), // 37: atmos.plugin.v1.DiscoverComponentsRequest - (*DiscoverComponentsResponse)(nil), // 38: atmos.plugin.v1.DiscoverComponentsResponse - (*ComponentRequest)(nil), // 39: atmos.plugin.v1.ComponentRequest - (*ComponentStateIdentity)(nil), // 40: atmos.plugin.v1.ComponentStateIdentity - (*ComponentGeneratedFile)(nil), // 41: atmos.plugin.v1.ComponentGeneratedFile - (*PrepareComponentResponse)(nil), // 42: atmos.plugin.v1.PrepareComponentResponse - (*ResolveComponentStateResponse)(nil), // 43: atmos.plugin.v1.ResolveComponentStateResponse - nil, // 44: atmos.plugin.v1.Invocation.EnvironmentEntry - nil, // 45: atmos.plugin.v1.ConfigureRequest.BrokerIdsEntry - nil, // 46: atmos.plugin.v1.Interaction.FieldsEntry - nil, // 47: atmos.plugin.v1.EnvironmentResponse.EnvironmentEntry - nil, // 48: atmos.plugin.v1.PrepareEnvironmentRequest.EnvironmentEntry - nil, // 49: atmos.plugin.v1.IdentityPrepareRequest.EnvironmentEntry - nil, // 50: atmos.plugin.v1.ComponentOperation.OptionsEntry - (*timestamppb.Timestamp)(nil), // 51: google.protobuf.Timestamp - (*structpb.Struct)(nil), // 52: google.protobuf.Struct - (*emptypb.Empty)(nil), // 53: google.protobuf.Empty + ComponentSource_Kind(0), // 0: atmos.plugin.v1.ComponentSource.Kind + ComponentDependency_Kind(0), // 1: atmos.plugin.v1.ComponentDependency.Kind + (*Invocation)(nil), // 2: atmos.plugin.v1.Invocation + (*Credentials)(nil), // 3: atmos.plugin.v1.Credentials + (*ServiceVersionRange)(nil), // 4: atmos.plugin.v1.ServiceVersionRange + (*Export)(nil), // 5: atmos.plugin.v1.Export + (*DescribeRequest)(nil), // 6: atmos.plugin.v1.DescribeRequest + (*DescribeResponse)(nil), // 7: atmos.plugin.v1.DescribeResponse + (*ConfigureRequest)(nil), // 8: atmos.plugin.v1.ConfigureRequest + (*ProviderRequest)(nil), // 9: atmos.plugin.v1.ProviderRequest + (*IdentityRequest)(nil), // 10: atmos.plugin.v1.IdentityRequest + (*Progress)(nil), // 11: atmos.plugin.v1.Progress + (*Interaction)(nil), // 12: atmos.plugin.v1.Interaction + (*AuthenticateEvent)(nil), // 13: atmos.plugin.v1.AuthenticateEvent + (*Diagnostic)(nil), // 14: atmos.plugin.v1.Diagnostic + (*ValidateResponse)(nil), // 15: atmos.plugin.v1.ValidateResponse + (*Unsupported)(nil), // 16: atmos.plugin.v1.Unsupported + (*RefreshRequest)(nil), // 17: atmos.plugin.v1.RefreshRequest + (*RefreshResponse)(nil), // 18: atmos.plugin.v1.RefreshResponse + (*EnvironmentResponse)(nil), // 19: atmos.plugin.v1.EnvironmentResponse + (*PrepareEnvironmentRequest)(nil), // 20: atmos.plugin.v1.PrepareEnvironmentRequest + (*CredentialPath)(nil), // 21: atmos.plugin.v1.CredentialPath + (*PathsResponse)(nil), // 22: atmos.plugin.v1.PathsResponse + (*IdentityAuthenticateRequest)(nil), // 23: atmos.plugin.v1.IdentityAuthenticateRequest + (*IdentityPrepareRequest)(nil), // 24: atmos.plugin.v1.IdentityPrepareRequest + (*CommandRequest)(nil), // 25: atmos.plugin.v1.CommandRequest + (*CommandExit)(nil), // 26: atmos.plugin.v1.CommandExit + (*CommandEvent)(nil), // 27: atmos.plugin.v1.CommandEvent + (*ComponentValue)(nil), // 28: atmos.plugin.v1.ComponentValue + (*ComponentValuePath)(nil), // 29: atmos.plugin.v1.ComponentValuePath + (*ComponentValueStep)(nil), // 30: atmos.plugin.v1.ComponentValueStep + (*ComponentAddress)(nil), // 31: atmos.plugin.v1.ComponentAddress + (*ComponentSource)(nil), // 32: atmos.plugin.v1.ComponentSource + (*ComponentDependency)(nil), // 33: atmos.plugin.v1.ComponentDependency + (*DiscoveredComponent)(nil), // 34: atmos.plugin.v1.DiscoveredComponent + (*ComponentEffects)(nil), // 35: atmos.plugin.v1.ComponentEffects + (*ComponentOperation)(nil), // 36: atmos.plugin.v1.ComponentOperation + (*DiscoverComponentsRequest)(nil), // 37: atmos.plugin.v1.DiscoverComponentsRequest + (*DiscoverComponentsResponse)(nil), // 38: atmos.plugin.v1.DiscoverComponentsResponse + (*ComponentRequest)(nil), // 39: atmos.plugin.v1.ComponentRequest + (*ComponentStateIdentity)(nil), // 40: atmos.plugin.v1.ComponentStateIdentity + (*ComponentGeneratedFile)(nil), // 41: atmos.plugin.v1.ComponentGeneratedFile + (*ComponentExecutionConfiguration)(nil), // 42: atmos.plugin.v1.ComponentExecutionConfiguration + (*ComponentExtraArguments)(nil), // 43: atmos.plugin.v1.ComponentExtraArguments + (*ComponentHook)(nil), // 44: atmos.plugin.v1.ComponentHook + (*PrepareComponentResponse)(nil), // 45: atmos.plugin.v1.PrepareComponentResponse + (*ResolveComponentStateResponse)(nil), // 46: atmos.plugin.v1.ResolveComponentStateResponse + nil, // 47: atmos.plugin.v1.Invocation.EnvironmentEntry + nil, // 48: atmos.plugin.v1.ConfigureRequest.BrokerIdsEntry + nil, // 49: atmos.plugin.v1.Interaction.FieldsEntry + nil, // 50: atmos.plugin.v1.EnvironmentResponse.EnvironmentEntry + nil, // 51: atmos.plugin.v1.PrepareEnvironmentRequest.EnvironmentEntry + nil, // 52: atmos.plugin.v1.IdentityPrepareRequest.EnvironmentEntry + nil, // 53: atmos.plugin.v1.ComponentOperation.OptionsEntry + nil, // 54: atmos.plugin.v1.ComponentExtraArguments.EnvironmentEntry + (*timestamppb.Timestamp)(nil), // 55: google.protobuf.Timestamp + (*structpb.Struct)(nil), // 56: google.protobuf.Struct + (*emptypb.Empty)(nil), // 57: google.protobuf.Empty } ) var file_atmos_plugin_v1_plugin_proto_depIdxs = []int32{ - 51, // 0: atmos.plugin.v1.Invocation.deadline:type_name -> google.protobuf.Timestamp - 44, // 1: atmos.plugin.v1.Invocation.environment:type_name -> atmos.plugin.v1.Invocation.EnvironmentEntry - 51, // 2: atmos.plugin.v1.Credentials.expires_at:type_name -> google.protobuf.Timestamp - 52, // 3: atmos.plugin.v1.Credentials.data:type_name -> google.protobuf.Struct + 55, // 0: atmos.plugin.v1.Invocation.deadline:type_name -> google.protobuf.Timestamp + 47, // 1: atmos.plugin.v1.Invocation.environment:type_name -> atmos.plugin.v1.Invocation.EnvironmentEntry + 55, // 2: atmos.plugin.v1.Credentials.expires_at:type_name -> google.protobuf.Timestamp + 56, // 3: atmos.plugin.v1.Credentials.data:type_name -> google.protobuf.Struct 4, // 4: atmos.plugin.v1.DescribeRequest.host_supported:type_name -> atmos.plugin.v1.ServiceVersionRange 4, // 5: atmos.plugin.v1.DescribeResponse.services:type_name -> atmos.plugin.v1.ServiceVersionRange 5, // 6: atmos.plugin.v1.DescribeResponse.exports:type_name -> atmos.plugin.v1.Export - 45, // 7: atmos.plugin.v1.ConfigureRequest.broker_ids:type_name -> atmos.plugin.v1.ConfigureRequest.BrokerIdsEntry - 52, // 8: atmos.plugin.v1.ConfigureRequest.settings:type_name -> google.protobuf.Struct + 48, // 7: atmos.plugin.v1.ConfigureRequest.broker_ids:type_name -> atmos.plugin.v1.ConfigureRequest.BrokerIdsEntry + 56, // 8: atmos.plugin.v1.ConfigureRequest.settings:type_name -> google.protobuf.Struct 2, // 9: atmos.plugin.v1.ProviderRequest.invocation:type_name -> atmos.plugin.v1.Invocation - 52, // 10: atmos.plugin.v1.ProviderRequest.spec:type_name -> google.protobuf.Struct + 56, // 10: atmos.plugin.v1.ProviderRequest.spec:type_name -> google.protobuf.Struct 2, // 11: atmos.plugin.v1.IdentityRequest.invocation:type_name -> atmos.plugin.v1.Invocation - 52, // 12: atmos.plugin.v1.IdentityRequest.spec:type_name -> google.protobuf.Struct - 52, // 13: atmos.plugin.v1.IdentityRequest.principal:type_name -> google.protobuf.Struct - 46, // 14: atmos.plugin.v1.Interaction.fields:type_name -> atmos.plugin.v1.Interaction.FieldsEntry + 56, // 12: atmos.plugin.v1.IdentityRequest.spec:type_name -> google.protobuf.Struct + 56, // 13: atmos.plugin.v1.IdentityRequest.principal:type_name -> google.protobuf.Struct + 49, // 14: atmos.plugin.v1.Interaction.fields:type_name -> atmos.plugin.v1.Interaction.FieldsEntry 11, // 15: atmos.plugin.v1.AuthenticateEvent.progress:type_name -> atmos.plugin.v1.Progress 12, // 16: atmos.plugin.v1.AuthenticateEvent.interaction:type_name -> atmos.plugin.v1.Interaction 3, // 17: atmos.plugin.v1.AuthenticateEvent.result:type_name -> atmos.plugin.v1.Credentials @@ -3347,16 +3648,16 @@ var file_atmos_plugin_v1_plugin_proto_depIdxs = []int32{ 3, // 20: atmos.plugin.v1.RefreshRequest.current:type_name -> atmos.plugin.v1.Credentials 3, // 21: atmos.plugin.v1.RefreshResponse.credentials:type_name -> atmos.plugin.v1.Credentials 16, // 22: atmos.plugin.v1.RefreshResponse.unsupported:type_name -> atmos.plugin.v1.Unsupported - 47, // 23: atmos.plugin.v1.EnvironmentResponse.environment:type_name -> atmos.plugin.v1.EnvironmentResponse.EnvironmentEntry + 50, // 23: atmos.plugin.v1.EnvironmentResponse.environment:type_name -> atmos.plugin.v1.EnvironmentResponse.EnvironmentEntry 9, // 24: atmos.plugin.v1.PrepareEnvironmentRequest.provider:type_name -> atmos.plugin.v1.ProviderRequest - 48, // 25: atmos.plugin.v1.PrepareEnvironmentRequest.environment:type_name -> atmos.plugin.v1.PrepareEnvironmentRequest.EnvironmentEntry + 51, // 25: atmos.plugin.v1.PrepareEnvironmentRequest.environment:type_name -> atmos.plugin.v1.PrepareEnvironmentRequest.EnvironmentEntry 21, // 26: atmos.plugin.v1.PathsResponse.paths:type_name -> atmos.plugin.v1.CredentialPath 10, // 27: atmos.plugin.v1.IdentityAuthenticateRequest.identity:type_name -> atmos.plugin.v1.IdentityRequest 3, // 28: atmos.plugin.v1.IdentityAuthenticateRequest.upstream:type_name -> atmos.plugin.v1.Credentials 10, // 29: atmos.plugin.v1.IdentityPrepareRequest.identity:type_name -> atmos.plugin.v1.IdentityRequest - 49, // 30: atmos.plugin.v1.IdentityPrepareRequest.environment:type_name -> atmos.plugin.v1.IdentityPrepareRequest.EnvironmentEntry + 52, // 30: atmos.plugin.v1.IdentityPrepareRequest.environment:type_name -> atmos.plugin.v1.IdentityPrepareRequest.EnvironmentEntry 2, // 31: atmos.plugin.v1.CommandRequest.invocation:type_name -> atmos.plugin.v1.Invocation - 52, // 32: atmos.plugin.v1.CommandRequest.settings:type_name -> google.protobuf.Struct + 56, // 32: atmos.plugin.v1.CommandRequest.settings:type_name -> google.protobuf.Struct 26, // 33: atmos.plugin.v1.CommandEvent.exit:type_name -> atmos.plugin.v1.CommandExit 29, // 34: atmos.plugin.v1.ComponentValue.sensitive_paths:type_name -> atmos.plugin.v1.ComponentValuePath 30, // 35: atmos.plugin.v1.ComponentValuePath.steps:type_name -> atmos.plugin.v1.ComponentValueStep @@ -3368,7 +3669,7 @@ var file_atmos_plugin_v1_plugin_proto_depIdxs = []int32{ 28, // 41: atmos.plugin.v1.DiscoveredComponent.configuration:type_name -> atmos.plugin.v1.ComponentValue 32, // 42: atmos.plugin.v1.DiscoveredComponent.sources:type_name -> atmos.plugin.v1.ComponentSource 33, // 43: atmos.plugin.v1.DiscoveredComponent.dependencies:type_name -> atmos.plugin.v1.ComponentDependency - 50, // 44: atmos.plugin.v1.ComponentOperation.options:type_name -> atmos.plugin.v1.ComponentOperation.OptionsEntry + 53, // 44: atmos.plugin.v1.ComponentOperation.options:type_name -> atmos.plugin.v1.ComponentOperation.OptionsEntry 2, // 45: atmos.plugin.v1.DiscoverComponentsRequest.invocation:type_name -> atmos.plugin.v1.Invocation 36, // 46: atmos.plugin.v1.DiscoverComponentsRequest.operation:type_name -> atmos.plugin.v1.ComponentOperation 35, // 47: atmos.plugin.v1.DiscoverComponentsRequest.effects:type_name -> atmos.plugin.v1.ComponentEffects @@ -3382,61 +3683,67 @@ var file_atmos_plugin_v1_plugin_proto_depIdxs = []int32{ 28, // 55: atmos.plugin.v1.ComponentRequest.configuration:type_name -> atmos.plugin.v1.ComponentValue 28, // 56: atmos.plugin.v1.ComponentRequest.provider_configuration:type_name -> atmos.plugin.v1.ComponentValue 28, // 57: atmos.plugin.v1.ComponentStateIdentity.backend:type_name -> atmos.plugin.v1.ComponentValue - 28, // 58: atmos.plugin.v1.PrepareComponentResponse.inputs:type_name -> atmos.plugin.v1.ComponentValue - 40, // 59: atmos.plugin.v1.PrepareComponentResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity - 41, // 60: atmos.plugin.v1.PrepareComponentResponse.files:type_name -> atmos.plugin.v1.ComponentGeneratedFile - 33, // 61: atmos.plugin.v1.PrepareComponentResponse.dependencies:type_name -> atmos.plugin.v1.ComponentDependency - 32, // 62: atmos.plugin.v1.PrepareComponentResponse.sources:type_name -> atmos.plugin.v1.ComponentSource - 14, // 63: atmos.plugin.v1.PrepareComponentResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic - 40, // 64: atmos.plugin.v1.ResolveComponentStateResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity - 32, // 65: atmos.plugin.v1.ResolveComponentStateResponse.sources:type_name -> atmos.plugin.v1.ComponentSource - 14, // 66: atmos.plugin.v1.ResolveComponentStateResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic - 28, // 67: atmos.plugin.v1.ComponentOperation.OptionsEntry.value:type_name -> atmos.plugin.v1.ComponentValue - 6, // 68: atmos.plugin.v1.Info.Describe:input_type -> atmos.plugin.v1.DescribeRequest - 8, // 69: atmos.plugin.v1.Info.Configure:input_type -> atmos.plugin.v1.ConfigureRequest - 9, // 70: atmos.plugin.v1.AuthProvider.Validate:input_type -> atmos.plugin.v1.ProviderRequest - 9, // 71: atmos.plugin.v1.AuthProvider.Authenticate:input_type -> atmos.plugin.v1.ProviderRequest - 17, // 72: atmos.plugin.v1.AuthProvider.Refresh:input_type -> atmos.plugin.v1.RefreshRequest - 9, // 73: atmos.plugin.v1.AuthProvider.Logout:input_type -> atmos.plugin.v1.ProviderRequest - 9, // 74: atmos.plugin.v1.AuthProvider.Environment:input_type -> atmos.plugin.v1.ProviderRequest - 9, // 75: atmos.plugin.v1.AuthProvider.Paths:input_type -> atmos.plugin.v1.ProviderRequest - 20, // 76: atmos.plugin.v1.AuthProvider.PrepareEnvironment:input_type -> atmos.plugin.v1.PrepareEnvironmentRequest - 10, // 77: atmos.plugin.v1.AuthIdentity.Validate:input_type -> atmos.plugin.v1.IdentityRequest - 23, // 78: atmos.plugin.v1.AuthIdentity.Authenticate:input_type -> atmos.plugin.v1.IdentityAuthenticateRequest - 10, // 79: atmos.plugin.v1.AuthIdentity.Logout:input_type -> atmos.plugin.v1.IdentityRequest - 10, // 80: atmos.plugin.v1.AuthIdentity.Environment:input_type -> atmos.plugin.v1.IdentityRequest - 10, // 81: atmos.plugin.v1.AuthIdentity.Paths:input_type -> atmos.plugin.v1.IdentityRequest - 24, // 82: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:input_type -> atmos.plugin.v1.IdentityPrepareRequest - 25, // 83: atmos.plugin.v1.Commands.Run:input_type -> atmos.plugin.v1.CommandRequest - 37, // 84: atmos.plugin.v1.ComponentDiscovery.Discover:input_type -> atmos.plugin.v1.DiscoverComponentsRequest - 39, // 85: atmos.plugin.v1.ComponentValidation.Validate:input_type -> atmos.plugin.v1.ComponentRequest - 39, // 86: atmos.plugin.v1.ComponentPreparation.Prepare:input_type -> atmos.plugin.v1.ComponentRequest - 39, // 87: atmos.plugin.v1.ComponentState.Resolve:input_type -> atmos.plugin.v1.ComponentRequest - 7, // 88: atmos.plugin.v1.Info.Describe:output_type -> atmos.plugin.v1.DescribeResponse - 53, // 89: atmos.plugin.v1.Info.Configure:output_type -> google.protobuf.Empty - 15, // 90: atmos.plugin.v1.AuthProvider.Validate:output_type -> atmos.plugin.v1.ValidateResponse - 13, // 91: atmos.plugin.v1.AuthProvider.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent - 18, // 92: atmos.plugin.v1.AuthProvider.Refresh:output_type -> atmos.plugin.v1.RefreshResponse - 53, // 93: atmos.plugin.v1.AuthProvider.Logout:output_type -> google.protobuf.Empty - 19, // 94: atmos.plugin.v1.AuthProvider.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse - 22, // 95: atmos.plugin.v1.AuthProvider.Paths:output_type -> atmos.plugin.v1.PathsResponse - 19, // 96: atmos.plugin.v1.AuthProvider.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse - 15, // 97: atmos.plugin.v1.AuthIdentity.Validate:output_type -> atmos.plugin.v1.ValidateResponse - 13, // 98: atmos.plugin.v1.AuthIdentity.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent - 53, // 99: atmos.plugin.v1.AuthIdentity.Logout:output_type -> google.protobuf.Empty - 19, // 100: atmos.plugin.v1.AuthIdentity.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse - 22, // 101: atmos.plugin.v1.AuthIdentity.Paths:output_type -> atmos.plugin.v1.PathsResponse - 19, // 102: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse - 27, // 103: atmos.plugin.v1.Commands.Run:output_type -> atmos.plugin.v1.CommandEvent - 38, // 104: atmos.plugin.v1.ComponentDiscovery.Discover:output_type -> atmos.plugin.v1.DiscoverComponentsResponse - 15, // 105: atmos.plugin.v1.ComponentValidation.Validate:output_type -> atmos.plugin.v1.ValidateResponse - 42, // 106: atmos.plugin.v1.ComponentPreparation.Prepare:output_type -> atmos.plugin.v1.PrepareComponentResponse - 43, // 107: atmos.plugin.v1.ComponentState.Resolve:output_type -> atmos.plugin.v1.ResolveComponentStateResponse - 88, // [88:108] is the sub-list for method output_type - 68, // [68:88] is the sub-list for method input_type - 68, // [68:68] is the sub-list for extension type_name - 68, // [68:68] is the sub-list for extension extendee - 0, // [0:68] is the sub-list for field type_name + 43, // 58: atmos.plugin.v1.ComponentExecutionConfiguration.extra_arguments:type_name -> atmos.plugin.v1.ComponentExtraArguments + 44, // 59: atmos.plugin.v1.ComponentExecutionConfiguration.before_hooks:type_name -> atmos.plugin.v1.ComponentHook + 44, // 60: atmos.plugin.v1.ComponentExecutionConfiguration.after_hooks:type_name -> atmos.plugin.v1.ComponentHook + 44, // 61: atmos.plugin.v1.ComponentExecutionConfiguration.error_hooks:type_name -> atmos.plugin.v1.ComponentHook + 54, // 62: atmos.plugin.v1.ComponentExtraArguments.environment:type_name -> atmos.plugin.v1.ComponentExtraArguments.EnvironmentEntry + 28, // 63: atmos.plugin.v1.PrepareComponentResponse.inputs:type_name -> atmos.plugin.v1.ComponentValue + 40, // 64: atmos.plugin.v1.PrepareComponentResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity + 41, // 65: atmos.plugin.v1.PrepareComponentResponse.files:type_name -> atmos.plugin.v1.ComponentGeneratedFile + 33, // 66: atmos.plugin.v1.PrepareComponentResponse.dependencies:type_name -> atmos.plugin.v1.ComponentDependency + 32, // 67: atmos.plugin.v1.PrepareComponentResponse.sources:type_name -> atmos.plugin.v1.ComponentSource + 14, // 68: atmos.plugin.v1.PrepareComponentResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic + 42, // 69: atmos.plugin.v1.PrepareComponentResponse.execution:type_name -> atmos.plugin.v1.ComponentExecutionConfiguration + 40, // 70: atmos.plugin.v1.ResolveComponentStateResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity + 32, // 71: atmos.plugin.v1.ResolveComponentStateResponse.sources:type_name -> atmos.plugin.v1.ComponentSource + 14, // 72: atmos.plugin.v1.ResolveComponentStateResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic + 28, // 73: atmos.plugin.v1.ComponentOperation.OptionsEntry.value:type_name -> atmos.plugin.v1.ComponentValue + 6, // 74: atmos.plugin.v1.Info.Describe:input_type -> atmos.plugin.v1.DescribeRequest + 8, // 75: atmos.plugin.v1.Info.Configure:input_type -> atmos.plugin.v1.ConfigureRequest + 9, // 76: atmos.plugin.v1.AuthProvider.Validate:input_type -> atmos.plugin.v1.ProviderRequest + 9, // 77: atmos.plugin.v1.AuthProvider.Authenticate:input_type -> atmos.plugin.v1.ProviderRequest + 17, // 78: atmos.plugin.v1.AuthProvider.Refresh:input_type -> atmos.plugin.v1.RefreshRequest + 9, // 79: atmos.plugin.v1.AuthProvider.Logout:input_type -> atmos.plugin.v1.ProviderRequest + 9, // 80: atmos.plugin.v1.AuthProvider.Environment:input_type -> atmos.plugin.v1.ProviderRequest + 9, // 81: atmos.plugin.v1.AuthProvider.Paths:input_type -> atmos.plugin.v1.ProviderRequest + 20, // 82: atmos.plugin.v1.AuthProvider.PrepareEnvironment:input_type -> atmos.plugin.v1.PrepareEnvironmentRequest + 10, // 83: atmos.plugin.v1.AuthIdentity.Validate:input_type -> atmos.plugin.v1.IdentityRequest + 23, // 84: atmos.plugin.v1.AuthIdentity.Authenticate:input_type -> atmos.plugin.v1.IdentityAuthenticateRequest + 10, // 85: atmos.plugin.v1.AuthIdentity.Logout:input_type -> atmos.plugin.v1.IdentityRequest + 10, // 86: atmos.plugin.v1.AuthIdentity.Environment:input_type -> atmos.plugin.v1.IdentityRequest + 10, // 87: atmos.plugin.v1.AuthIdentity.Paths:input_type -> atmos.plugin.v1.IdentityRequest + 24, // 88: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:input_type -> atmos.plugin.v1.IdentityPrepareRequest + 25, // 89: atmos.plugin.v1.Commands.Run:input_type -> atmos.plugin.v1.CommandRequest + 37, // 90: atmos.plugin.v1.ComponentDiscovery.Discover:input_type -> atmos.plugin.v1.DiscoverComponentsRequest + 39, // 91: atmos.plugin.v1.ComponentValidation.Validate:input_type -> atmos.plugin.v1.ComponentRequest + 39, // 92: atmos.plugin.v1.ComponentPreparation.Prepare:input_type -> atmos.plugin.v1.ComponentRequest + 39, // 93: atmos.plugin.v1.ComponentState.Resolve:input_type -> atmos.plugin.v1.ComponentRequest + 7, // 94: atmos.plugin.v1.Info.Describe:output_type -> atmos.plugin.v1.DescribeResponse + 57, // 95: atmos.plugin.v1.Info.Configure:output_type -> google.protobuf.Empty + 15, // 96: atmos.plugin.v1.AuthProvider.Validate:output_type -> atmos.plugin.v1.ValidateResponse + 13, // 97: atmos.plugin.v1.AuthProvider.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent + 18, // 98: atmos.plugin.v1.AuthProvider.Refresh:output_type -> atmos.plugin.v1.RefreshResponse + 57, // 99: atmos.plugin.v1.AuthProvider.Logout:output_type -> google.protobuf.Empty + 19, // 100: atmos.plugin.v1.AuthProvider.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse + 22, // 101: atmos.plugin.v1.AuthProvider.Paths:output_type -> atmos.plugin.v1.PathsResponse + 19, // 102: atmos.plugin.v1.AuthProvider.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse + 15, // 103: atmos.plugin.v1.AuthIdentity.Validate:output_type -> atmos.plugin.v1.ValidateResponse + 13, // 104: atmos.plugin.v1.AuthIdentity.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent + 57, // 105: atmos.plugin.v1.AuthIdentity.Logout:output_type -> google.protobuf.Empty + 19, // 106: atmos.plugin.v1.AuthIdentity.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse + 22, // 107: atmos.plugin.v1.AuthIdentity.Paths:output_type -> atmos.plugin.v1.PathsResponse + 19, // 108: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse + 27, // 109: atmos.plugin.v1.Commands.Run:output_type -> atmos.plugin.v1.CommandEvent + 38, // 110: atmos.plugin.v1.ComponentDiscovery.Discover:output_type -> atmos.plugin.v1.DiscoverComponentsResponse + 15, // 111: atmos.plugin.v1.ComponentValidation.Validate:output_type -> atmos.plugin.v1.ValidateResponse + 45, // 112: atmos.plugin.v1.ComponentPreparation.Prepare:output_type -> atmos.plugin.v1.PrepareComponentResponse + 46, // 113: atmos.plugin.v1.ComponentState.Resolve:output_type -> atmos.plugin.v1.ResolveComponentStateResponse + 94, // [94:114] is the sub-list for method output_type + 74, // [74:94] is the sub-list for method input_type + 74, // [74:74] is the sub-list for extension type_name + 74, // [74:74] is the sub-list for extension extendee + 0, // [0:74] is the sub-list for field type_name } func init() { file_atmos_plugin_v1_plugin_proto_init() } @@ -3468,7 +3775,7 @@ func file_atmos_plugin_v1_plugin_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_atmos_plugin_v1_plugin_proto_rawDesc), len(file_atmos_plugin_v1_plugin_proto_rawDesc)), NumEnums: 2, - NumMessages: 49, + NumMessages: 53, NumExtensions: 0, NumServices: 8, }, diff --git a/pkg/schema/component_configuration.go b/pkg/schema/component_configuration.go index aeb0ca76ab8..99b17d5fef7 100644 --- a/pkg/schema/component_configuration.go +++ b/pkg/schema/component_configuration.go @@ -44,13 +44,17 @@ type ComponentConfigurationRevisionRuntime interface { // PreparedComponentConfiguration contains evaluated inputs and literal artifacts. type PreparedComponentConfiguration struct { - Engine string - Source string - Inputs map[string]any - BackendType string - Backend map[string]any - Workspace string - Files []ComponentConfigurationFile + Sources []ComponentConfigurationSource + Engine string + Source string + Inputs map[string]any + BackendType string + Backend map[string]any + Workspace string + Files []ComponentConfigurationFile + BackendGenerated bool + // Execution is data the host runs; it is nil when the provider adds no hooks or arguments. + Execution *ComponentExecution } // ComponentConfigurationFile must bypass template rendering. diff --git a/pkg/schema/component_configuration_import.go b/pkg/schema/component_configuration_import.go new file mode 100644 index 00000000000..dc9a6c3e3e2 --- /dev/null +++ b/pkg/schema/component_configuration_import.go @@ -0,0 +1,20 @@ +package schema + +import "context" + +// ComponentConfigurationImportRuntime accepts explicit provider imports before +// discovery. Imported definitions retain the provider's logical coordinates. +type ComponentConfigurationImportRuntime interface { + RegisterImport(context.Context, string) (bool, error) +} + +// HasComponentConfiguration reports provider enablement without starting plugins. +// An enabled provider may serve explicit imports with automatic discovery off. +func (c *AtmosConfiguration) HasComponentConfiguration() bool { + for _, provider := range c.Components.Terraform.ConfigurationProviders { + if provider.Enabled { + return true + } + } + return false +} diff --git a/pkg/schema/component_configuration_plan.go b/pkg/schema/component_configuration_plan.go new file mode 100644 index 00000000000..5176e145922 --- /dev/null +++ b/pkg/schema/component_configuration_plan.go @@ -0,0 +1,15 @@ +package schema + +import "context" + +// ComponentConfigurationPlanRuntime binds saved preparation to the selected provider and sources. +// Identity must not evaluate inputs, outputs, hooks, or commands. +type ComponentConfigurationPlanRuntime interface { + PlanIdentity(context.Context, *ConfigAndStacksInfo, []ComponentConfigurationSource) (string, error) +} + +// ComponentConfigurationSource records the files that contributed to a prepared configuration. +type ComponentConfigurationSource struct { + Path string + Directory bool +} diff --git a/pkg/schema/component_configuration_state.go b/pkg/schema/component_configuration_state.go new file mode 100644 index 00000000000..97dd0eaa2c6 --- /dev/null +++ b/pkg/schema/component_configuration_state.go @@ -0,0 +1,17 @@ +package schema + +import "context" + +// ComponentConfigurationStateRuntime resolves state without preparing or executing a unit. +// It is optional so discovery-only providers do not imply backend support. +type ComponentConfigurationStateRuntime interface { + ResolveState(context.Context, *ConfigAndStacksInfo) (*ComponentConfigurationState, error) +} + +// ComponentConfigurationState identifies the upstream backend independently of Atmos workdirs. +type ComponentConfigurationState struct { + BackendType string + Backend map[string]any + Workspace string + WorkingDirectory string +} diff --git a/pkg/schema/component_execution.go b/pkg/schema/component_execution.go new file mode 100644 index 00000000000..a61c9b42899 --- /dev/null +++ b/pkg/schema/component_execution.go @@ -0,0 +1,39 @@ +package schema + +// ComponentExecution is evaluated command metadata returned by a configuration provider. +// The host executes it with the invocation's environment, toolchain, working directory, +// cancellation, and I/O; the provider never runs these commands. Lists keep declaration order. +type ComponentExecution struct { + ExtraArguments []ComponentExtraArguments + BeforeHooks []ComponentHook + AfterHooks []ComponentHook + ErrorHooks []ComponentHook +} + +// ComponentExtraArguments adds arguments, variable files, and environment to the listed commands. +type ComponentExtraArguments struct { + Commands []string + Arguments []string + RequiredVarFiles []string + OptionalVarFiles []string + Environment map[string]string +} + +// ComponentHook is one command the host runs around a Terraform command. +type ComponentHook struct { + Name string + Commands []string + // Execute is an argv; the first element is resolved on the invocation's PATH. + Execute []string + // WorkingDirectory is empty for the provisioned module directory. Relative paths resolve there. + WorkingDirectory string + RunOnError bool + SuppressStdout bool + // OnErrors holds the regular expressions an error hook matches against preceding failures. + OnErrors []string +} + +// IsEmpty reports whether the execution adds nothing to a command. +func (e *ComponentExecution) IsEmpty() bool { + return e == nil || len(e.ExtraArguments)+len(e.BeforeHooks)+len(e.AfterHooks)+len(e.ErrorHooks) == 0 +} diff --git a/pkg/schema/instance.go b/pkg/schema/instance.go index 8662d9e9c87..6ca2b82a643 100644 --- a/pkg/schema/instance.go +++ b/pkg/schema/instance.go @@ -5,6 +5,7 @@ type Instance struct { Component string `yaml:"component" json:"component" mapstructure:"component"` Stack string `yaml:"stack" json:"stack" mapstructure:"stack"` ComponentType string `yaml:"component_type" json:"component_type" mapstructure:"component_type"` + Engine string `yaml:"engine" json:"engine" mapstructure:"engine"` Settings AtmosSectionMapType `yaml:"settings" json:"settings" mapstructure:"settings"` Vars AtmosSectionMapType `yaml:"vars" json:"vars" mapstructure:"vars"` Env AtmosSectionMapType `yaml:"env" json:"env" mapstructure:"env"` diff --git a/pkg/schema/schema.go b/pkg/schema/schema.go index 26d798c3a75..13009f2f634 100644 --- a/pkg/schema/schema.go +++ b/pkg/schema/schema.go @@ -2370,6 +2370,12 @@ type ConfigAndStacksInfo struct { // runtime state — not serialized. ExecMetadataRawOutput string `yaml:"-" json:"-" mapstructure:"-"` + // ComponentExecution holds hooks and extra arguments that a configuration provider + // evaluated for this component. The Terraform pipeline applies them per phase + // (implicit init and the requested command match independently). Transient + // runtime state — not serialized. + ComponentExecution *ComponentExecution `yaml:"-" json:"-" mapstructure:"-"` + // ExecMetadataRawMetrics holds a *process.ProcessMetrics (pkg/metrics/process) // combining the main plan/apply/deploy subprocess tree's resource usage with // atmos's own self-usage, captured by executeMainTerraformCommand. Typed as diff --git a/pkg/terraform/output/config.go b/pkg/terraform/output/config.go index ccd232d1c55..5902a82dd05 100644 --- a/pkg/terraform/output/config.go +++ b/pkg/terraform/output/config.go @@ -14,6 +14,10 @@ import ( // ComponentConfig holds validated configuration for terraform output execution. type ComponentConfig struct { + // BaseEnvironment is the authoritative sanitized identity environment when non-nil. + BaseEnvironment []string + // ReadOnlyState prevents output lookups from creating absent workspaces. + ReadOnlyState bool // Executable is the path to the terraform/opentofu executable. Executable string // Workspace is the terraform workspace name. diff --git a/pkg/terraform/output/configuration_state.go b/pkg/terraform/output/configuration_state.go new file mode 100644 index 00000000000..116e9ca763d --- /dev/null +++ b/pkg/terraform/output/configuration_state.go @@ -0,0 +1,121 @@ +package output + +import ( + "context" + "fmt" + "maps" + "os" + "path/filepath" + "slices" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/auth" + "github.com/cloudposse/atmos/pkg/perf" + "github.com/cloudposse/atmos/pkg/schema" +) + +// ConfigurationStateRequest identifies the component whose plugin-owned state is resolved. +type ConfigurationStateRequest struct { + // Sections are the component's described sections. + Sections map[string]any + // Component is the component name as the caller addresses it. + Component string + // Stack is the stack that contains the component. + Stack string + // AuthContext is the target's resolved authentication context. + AuthContext *schema.AuthContext + // AuthManagers optionally supply the sanitized base environment of the target's identity. + AuthManagers []any +} + +// ResolveConfigurationState locates plugin-owned state without evaluating inputs or preparing files. +// The caller supplies the target's resolved authentication context, just as for ordinary state reads. +func ResolveConfigurationState(ctx context.Context, config *schema.AtmosConfiguration, request *ConfigurationStateRequest) (*schema.ComponentConfigurationState, error) { + defer perf.Track(config, "output.ResolveConfigurationState")() + + sections, component, stack := request.Sections, request.Component, request.Stack + settings, _ := sections["settings"].(map[string]any) + if _, owned := settings["configuration"]; !owned { + return nil, nil + } + runtime, ok := config.ComponentConfigurations.(schema.ComponentConfigurationStateRuntime) + if !ok { + return nil, errUtils.ErrPluginUnsupportedExtension + } + env, _ := sections["env"].(map[string]any) + environment, err := (&defaultEnvironmentSetup{}).SetupEnvironment(&ComponentConfig{Env: env, BaseEnvironment: configurationEnvironment(request.AuthManagers)}, request.AuthContext) + if err != nil { + return nil, err + } + info := &schema.ConfigAndStacksInfo{Stack: stack, ComponentFromArg: component, SubCommand: "output", ComponentSettingsSection: settings, SanitizedEnv: make([]string, 0, len(environment))} + info.Command, _ = sections["command"].(string) + info.TerraformWorkspace, _ = sections["workspace"].(string) + for key, value := range environment { + info.SanitizedEnv = append(info.SanitizedEnv, key+"="+value) + } + state, err := runtime.ResolveState(ctx, info) + if err == nil && state == nil { + return nil, errUtils.ErrPluginProtocol + } + return state, err +} + +// ConfigurationStateSections adapts the upstream state location for existing backend readers. +// Local readers currently give an explicit path precedence over workspace, so select its exact file. +func ConfigurationStateSections(sections map[string]any, state *schema.ComponentConfigurationState) map[string]any { + defer perf.Track(nil, "output.ConfigurationStateSections")() + + if state == nil { + return sections + } + result := maps.Clone(sections) + backend := maps.Clone(state.Backend) + if state.BackendType == "local" && state.Workspace != "" && state.Workspace != "default" { + root, _ := backend["workspace_dir"].(string) + if root == "" { + root = filepath.Join(state.WorkingDirectory, "terraform.tfstate.d") + } + backend["path"] = filepath.Join(root, state.Workspace, "terraform.tfstate") + } + result["backend_type"], result["backend"], result["workspace"] = state.BackendType, backend, state.Workspace + // A state location does not require provisioning the source or execution workdir. + delete(result, "source") + delete(result, "provision") + delete(result, "remote_state_backend") + delete(result, "remote_state_backend_type") + return result +} + +// configurationOutputConfig uses a disposable backend-only directory. Output reads must not +// regenerate a unit, fetch its module, invoke hooks, or overwrite its existing execution directory. +func configurationOutputConfig(config *ComponentConfig, state *schema.ComponentConfigurationState) (func(), error) { + root, err := os.MkdirTemp("", "atmos-plugin-output-") + if err != nil { + return nil, fmt.Errorf("%w: create state reader directory: %w", errUtils.ErrTerraformOutputFailed, err) + } + config.ComponentPath = root + config.ReadOnlyState = true + config.Env = maps.Clone(config.Env) + if config.Env == nil { + config.Env = make(map[string]any) + } + config.Env["TF_DATA_DIR"] = filepath.Join(root, ".terraform") + config.BackendType, config.Backend, config.Workspace = state.BackendType, state.Backend, state.Workspace + config.AutoGenerateBackend = true + config.AutoProvisionWorkdirForOutputs = false + config.Providers, config.Vars = nil, nil + config.PassVars = false + return func() { _ = os.RemoveAll(root) }, nil +} + +// configurationEnvironment retains an explicitly empty sanitized environment. +func configurationEnvironment(managers []any) []string { + for _, candidate := range managers { + if manager, ok := candidate.(auth.AuthManager); ok && manager != nil { + if info := manager.GetStackInfo(); info != nil && info.SanitizedEnv != nil { + return slices.Clone(info.SanitizedEnv) + } + } + } + return nil +} diff --git a/pkg/terraform/output/configuration_state_protocol_test.go b/pkg/terraform/output/configuration_state_protocol_test.go new file mode 100644 index 00000000000..83d71b67982 --- /dev/null +++ b/pkg/terraform/output/configuration_state_protocol_test.go @@ -0,0 +1,71 @@ +package output + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/schema" +) + +func TestResolveConfigurationStateRejectsProviderWithoutStateOrError(t *testing.T) { + config := validAtmosConfig(t) + sections := validSections() + sections["settings"] = map[string]any{"configuration": true} + config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return nil, nil + }} + state, err := ResolveConfigurationState(t.Context(), config, &ConfigurationStateRequest{Sections: sections, Component: "network", Stack: "live"}) + require.ErrorIs(t, err, errUtils.ErrPluginProtocol, "a provider that owns a component must always identify its state") + require.Nil(t, state) +} + +func TestSetupEnvironmentFiltersProhibitedVariablesFromBaseEnvironment(t *testing.T) { + setup := &defaultEnvironmentSetup{} + result, err := setup.SetupEnvironment(&ComponentConfig{BaseEnvironment: []string{ + "KEEP=value", "WITH_EQUALS=a=b", "malformed-entry-without-separator", + "TF_INPUT=0", "TF_WORKSPACE=other", "TF_LOG=trace", + "TF_VAR_region=us-east-1", "TF_CLI_ARGS_plan=-lock=false", "TF_CLI_ARGS=-no-color", + }}, nil) + require.NoError(t, err) + assert.Equal(t, map[string]string{"KEEP": "value", "WITH_EQUALS": "a=b"}, result, "only terraform-exec owned variables are removed") +} + +func TestSetupEnvironmentBaseEnvironmentReplacesAmbientEnvironment(t *testing.T) { + t.Setenv("AMBIENT_ONLY", "must-not-leak") + result, err := (&defaultEnvironmentSetup{}).SetupEnvironment(&ComponentConfig{BaseEnvironment: []string{}}, nil) + require.NoError(t, err) + assert.NotContains(t, result, "AMBIENT_ONLY", "an explicitly empty sanitized base is authoritative") + assert.Empty(t, result) +} + +func TestPluginOutputFailsBeforeRunnerWhenScratchDirectoryCannotBeCreated(t *testing.T) { + config := validAtmosConfig(t) + stateRoot := t.TempDir() + config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{"path": filepath.Join(stateRoot, "default.tfstate")}, Workspace: "default", WorkingDirectory: stateRoot}, nil + }} + sections := validSections() + sections["settings"] = map[string]any{"configuration": true} + missing := filepath.Join(t.TempDir(), "does-not-exist") + t.Setenv("TMPDIR", missing) + t.Setenv("TMP", missing) + t.Setenv("TEMP", missing) + runnerCreated := false + executor := NewExecutor(nil, WithRunnerFactory(func(string, string) (TerraformRunner, error) { + runnerCreated = true + return nil, nil + })) + result, err := executor.execute(t.Context(), config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true) + require.Error(t, err) + require.Nil(t, result) + require.False(t, runnerCreated, "no Terraform runner may start without its isolated backend directory") + entries, readErr := os.ReadDir(stateRoot) + require.NoError(t, readErr) + require.Empty(t, entries, "the plugin's state directory is never used as scratch space") +} diff --git a/pkg/terraform/output/configuration_state_test.go b/pkg/terraform/output/configuration_state_test.go new file mode 100644 index 00000000000..0706b0e0752 --- /dev/null +++ b/pkg/terraform/output/configuration_state_test.go @@ -0,0 +1,156 @@ +package output + +import ( + "context" + "encoding/json" + "errors" + "os" + "path/filepath" + "testing" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/auth/types" + "github.com/cloudposse/atmos/pkg/schema" + "github.com/hashicorp/terraform-exec/tfexec" + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" +) + +type stateRuntimeFixture struct { + resolve func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) +} + +func (*stateRuntimeFixture) Discover(context.Context) (map[string]map[string]any, error) { + panic("unexpected discovery") +} + +func (*stateRuntimeFixture) Prepare(context.Context, *schema.ConfigAndStacksInfo) (*schema.PreparedComponentConfiguration, error) { + panic("state reads must not prepare") +} + +func (s *stateRuntimeFixture) ResolveState(ctx context.Context, info *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return s.resolve(ctx, info) +} + +func TestResolveConfigurationStateUsesTargetEnvironment(t *testing.T) { + t.Setenv("ATMOS_STATE_AMBIENT_SECRET", "must-not-leak") + ctrl := gomock.NewController(t) + manager := types.NewMockAuthManager(ctrl) + manager.EXPECT().GetStackInfo().Return(&schema.ConfigAndStacksInfo{SanitizedEnv: []string{}}) + sections := validSections() + sections["settings"] = map[string]any{"configuration": true} + sections["env"] = map[string]any{"EXPLICIT": "component"} + config := validAtmosConfig(t) + config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(_ context.Context, info *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + require.Equal(t, []string{"EXPLICIT=component"}, info.SanitizedEnv) + require.Equal(t, "output", info.SubCommand) + require.Equal(t, "network", info.ComponentFromArg) + return &schema.ComponentConfigurationState{BackendType: "local", Workspace: "default"}, nil + }} + state, err := ResolveConfigurationState(t.Context(), config, &ConfigurationStateRequest{Sections: sections, Component: "network", Stack: "live", AuthManagers: []any{manager}}) + require.NoError(t, err) + require.Equal(t, "local", state.BackendType) + config.ComponentConfigurations = nil + _, err = ResolveConfigurationState(t.Context(), config, &ConfigurationStateRequest{Sections: sections, Component: "network", Stack: "live"}) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + delete(sections, "settings") + state, err = ResolveConfigurationState(t.Context(), config, &ConfigurationStateRequest{Sections: sections, Component: "network", Stack: "live"}) + require.NoError(t, err) + require.Nil(t, state) +} + +func TestConfigurationStateSectionsSelectsNamedWorkspace(t *testing.T) { + root := t.TempDir() + sections := map[string]any{"source": map[string]any{"uri": "never-fetch"}, "provision": map[string]any{}, "remote_state_backend_type": "static"} + state := &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{"path": filepath.Join(root, "default.tfstate"), "workspace_dir": filepath.Join(root, "workspaces")}, Workspace: "prod", WorkingDirectory: root} + resolved := ConfigurationStateSections(sections, state) + require.Equal(t, filepath.Join(root, "workspaces", "prod", "terraform.tfstate"), resolved["backend"].(map[string]any)["path"]) + require.Equal(t, filepath.Join(root, "default.tfstate"), state.Backend["path"]) + require.Contains(t, sections, "source") + require.NotContains(t, resolved, "source") + delete(state.Backend, "workspace_dir") + resolved = ConfigurationStateSections(sections, state) + require.Equal(t, filepath.Join(root, "terraform.tfstate.d", "prod", "terraform.tfstate"), resolved["backend"].(map[string]any)["path"]) + state.Workspace = "default" + resolved = ConfigurationStateSections(sections, state) + require.Equal(t, state.Backend, resolved["backend"]) + require.Equal(t, sections, ConfigurationStateSections(sections, nil)) +} + +func TestPluginOutputUsesBackendOnlyScratchDirectory(t *testing.T) { + for _, fail := range []bool{false, true} { + t.Run(map[bool]string{false: "success", true: "missing workspace"}[fail], func(t *testing.T) { + ctrl := gomock.NewController(t) + runner := NewMockTerraformRunner(ctrl) + runner.EXPECT().SetEnv(gomock.Any()).DoAndReturn(func(env map[string]string) error { + require.NotEqual(t, "original-data", env["TF_DATA_DIR"]) + return nil + }) + runner.EXPECT().Init(gomock.Any(), gomock.Any()).Return(nil) + var selectErr error + if fail { + selectErr = errors.New("workspace prod does not exist") + } + runner.EXPECT().WorkspaceSelect(gomock.Any(), "prod").Return(selectErr) + if !fail { + runner.EXPECT().Output(gomock.Any()).Return(map[string]tfexec.OutputMeta{"result": {Type: json.RawMessage(`"string"`), Value: json.RawMessage(`"fresh"`)}}, nil) + } + config := validAtmosConfig(t) + stateRoot := t.TempDir() + config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{"path": filepath.Join(stateRoot, "default.tfstate"), "workspace_dir": filepath.Join(stateRoot, "workspaces")}, Workspace: "prod", WorkingDirectory: stateRoot}, nil + }} + sections := validSections() + sections["settings"] = map[string]any{"configuration": true} + sections["source"] = map[string]any{"uri": "https://must-not-fetch.invalid/module"} + sections["env"] = map[string]any{"TF_DATA_DIR": "original-data"} + binary, err := os.Executable() + require.NoError(t, err) + sections["command"] = binary + var scratch string + executor := NewExecutor(nil, WithRunnerFactory(func(dir, executable string) (TerraformRunner, error) { + scratch = dir + require.NotEqual(t, stateRoot, dir) + files, err := os.ReadDir(dir) + require.NoError(t, err) + require.Len(t, files, 1) + require.Equal(t, "backend.tf.json", files[0].Name()) + return runner, nil + })) + result, err := executor.execute(t.Context(), config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true) + if fail { + require.ErrorContains(t, err, "workspace prod does not exist") + } else { + require.NoError(t, err) + require.Equal(t, "fresh", result["result"]) + } + require.NoDirExists(t, scratch) + files, err := os.ReadDir(stateRoot) + require.NoError(t, err) + require.Empty(t, files) + }) + } +} + +func TestConfigurationStateFailureAndScratchCleanup(t *testing.T) { + config := validAtmosConfig(t) + sections := validSections() + sections["settings"] = map[string]any{"configuration": true} + config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return nil, context.Canceled + }} + _, err := NewExecutor(nil).execute(t.Context(), config, "network", "live", sections, nil, nil, true) + require.ErrorIs(t, err, context.Canceled) + badTemp := filepath.Join(t.TempDir(), "does-not-exist") + t.Setenv("TMPDIR", badTemp) + t.Setenv("TMP", badTemp) + t.Setenv("TEMP", badTemp) + _, err = configurationOutputConfig(&ComponentConfig{}, &schema.ComponentConfigurationState{}) + require.Error(t, err) +} + +func TestReadOnlyHTTPOutputDoesNotSelectWorkspace(t *testing.T) { + runner := NewMockTerraformRunner(gomock.NewController(t)) + err := ensureOutputWorkspace(t.Context(), nil, runner, &ComponentConfig{ReadOnlyState: true, BackendType: "http"}, &workspaceTarget{component: "network", stack: "live"}) + require.NoError(t, err) +} diff --git a/pkg/terraform/output/environment.go b/pkg/terraform/output/environment.go index b9e730a9856..704f02b9c2e 100644 --- a/pkg/terraform/output/environment.go +++ b/pkg/terraform/output/environment.go @@ -55,6 +55,24 @@ func (s *defaultEnvironmentSetup) SetupEnvironment(config *ComponentConfig, auth // Get all environment variables from parent process (excluding prohibited vars). environMap := envpkg.EnvironToMapFiltered(prohibitedEnvVars, prohibitedEnvVarPrefixes) + if config.BaseEnvironment != nil { + environMap = make(map[string]string) + for _, entry := range config.BaseEnvironment { + if key, value, ok := strings.Cut(entry, "="); ok { + environMap[key] = value + } + } + for _, key := range prohibitedEnvVars { + delete(environMap, key) + } + for key := range environMap { + for _, prefix := range prohibitedEnvVarPrefixes { + if strings.HasPrefix(key, prefix) { + delete(environMap, key) + } + } + } + } // Add auth-based environment variables if authContext is provided. if authContext != nil && authContext.AWS != nil { diff --git a/pkg/terraform/output/executor.go b/pkg/terraform/output/executor.go index 24704a15928..b674848d24b 100644 --- a/pkg/terraform/output/executor.go +++ b/pkg/terraform/output/executor.go @@ -264,7 +264,7 @@ func (e *Executor) GetOutput( ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5) defer cancel() - outputs, err := e.execute(ctx, atmosConfig, component, stack, sections, authContext, nil, true) + outputs, err := e.execute(ctx, atmosConfig, component, stack, sections, authContext, nil, true, authManager) if err != nil { outputLookupFailed(message) return nil, false, errUtils.Build(errUtils.ErrTerraformOutputFailed). @@ -362,7 +362,7 @@ func (e *Executor) GetOutputWithOptions( ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5) defer cancel() - outputs, err := e.execute(ctx, atmosConfig, component, stack, sections, authContext, opts, yamlFunctions) + outputs, err := e.execute(ctx, atmosConfig, component, stack, sections, authContext, opts, yamlFunctions, authManager) if err != nil { outputLookupFailed(message) return nil, false, errUtils.Build(errUtils.ErrTerraformOutputFailed). @@ -447,7 +447,7 @@ func (e *Executor) fetchAndCacheOutputs( ctx, cancel := context.WithTimeout(context.Background(), time.Minute*5) defer cancel() - outputs, err := e.execute(ctx, atmosConfig, component, stack, sections, authContext, opts, yamlFunctions) + outputs, err := e.execute(ctx, atmosConfig, component, stack, sections, authContext, opts, yamlFunctions, authManager) if err != nil { return nil, errUtils.Build(errUtils.ErrTerraformOutputFailed). WithCause(err). @@ -471,6 +471,7 @@ func (e *Executor) execute( authContext *schema.AuthContext, opts *OutputOptions, processYamlFunctions bool, + authManagers ...any, ) (map[string]any, error) { defer perf.Track(atmosConfig, "output.Executor.execute")() @@ -489,6 +490,27 @@ func (e *Executor) execute( return nil, err } + state, err := ResolveConfigurationState(ctx, atmosConfig, &ConfigurationStateRequest{Sections: sections, Component: component, Stack: stack, AuthContext: authContext, AuthManagers: authManagers}) + if err != nil { + return nil, err + } + if state != nil { + config.BaseEnvironment = configurationEnvironment(authManagers) + cleanup, err := configurationOutputConfig(config, state) + if err != nil { + return nil, err + } + defer cleanup() + // The scratch directory always needs init, even after an earlier apply. + copy := OutputOptions{} + if opts != nil { + copy = *opts + } + copy.SkipInit = false + opts = © + processYamlFunctions = true + } + // Step 2.5: Auto-provision JIT workdir if needed before toolchain resolution and init. // Must run first so the workdir directory exists when backend files are written into it. if err := e.ensureWorkdirProvisioned(ctx, atmosConfig, sections, authContext, component, stack, config); err != nil { diff --git a/pkg/terraform/output/executor_init.go b/pkg/terraform/output/executor_init.go index b8a4d008878..c73be2e408d 100644 --- a/pkg/terraform/output/executor_init.go +++ b/pkg/terraform/output/executor_init.go @@ -63,7 +63,9 @@ func (e *Executor) ensureInitialized( }) if decision.RunInit { - workspaceMgr.CleanWorkspace(atmosConfig, config.ComponentPath) + if !config.ReadOnlyState { + workspaceMgr.CleanWorkspace(atmosConfig, config.ComponentPath) + } if err := e.runInit(ctx, runner, config, component, stack, stderrCapture, pluginCache, decision.Reconfigure, decision.Upgrade); err != nil { return err } @@ -74,7 +76,7 @@ func (e *Executor) ensureInitialized( log.Debug("autoinit: skipping terraform init", "component", component, "stack", stack, "reason", string(decision.Reason)) } - return workspaceMgr.EnsureWorkspace(ctx, runner, config.Workspace, config.BackendType, component, stack, stderrCapture) + return ensureOutputWorkspace(ctx, workspaceMgr, runner, config, &workspaceTarget{component: component, stack: stack, stderr: stderrCapture}) } // runOutputWithInitRecovery runs `terraform output` and, if it fails with a @@ -122,11 +124,13 @@ func (e *Executor) runOutputWithInitRecovery( ) workspaceMgr := &defaultWorkspaceManager{} - workspaceMgr.CleanWorkspace(atmosConfig, config.ComponentPath) + if !config.ReadOnlyState { + workspaceMgr.CleanWorkspace(atmosConfig, config.ComponentPath) + } if err := e.runInit(ctx, runner, config, component, stack, stderrCapture, pluginCache, rec.WithReconfigure, rec.WithUpgrade); err != nil { return nil, err } - if err := workspaceMgr.EnsureWorkspace(ctx, runner, config.Workspace, config.BackendType, component, stack, stderrCapture); err != nil { + if err := ensureOutputWorkspace(ctx, workspaceMgr, runner, config, &workspaceTarget{component: component, stack: stack, stderr: stderrCapture}); err != nil { return nil, err } @@ -202,3 +206,21 @@ func initArgsForLog(reconfigure, upgrade bool) []string { } return args } + +// workspaceTarget identifies the component whose workspace is selected and captures its diagnostics. +type workspaceTarget struct { + component string + stack string + stderr *quietModeWriter +} + +// ensureOutputWorkspace never creates state while reading a plugin-owned backend. +func ensureOutputWorkspace(ctx context.Context, manager WorkspaceManager, runner TerraformRunner, config *ComponentConfig, target *workspaceTarget) error { + if config.ReadOnlyState { + if config.BackendType == "http" { + return nil + } + return runner.WorkspaceSelect(ctx, config.Workspace) + } + return manager.EnsureWorkspace(ctx, runner, config.Workspace, config.BackendType, target.component, target.stack, target.stderr) +} diff --git a/pkg/terraform/output/executor_toolchain_test.go b/pkg/terraform/output/executor_toolchain_test.go new file mode 100644 index 00000000000..09aacc58380 --- /dev/null +++ b/pkg/terraform/output/executor_toolchain_test.go @@ -0,0 +1,221 @@ +package output + +import ( + "context" + "os" + "path/filepath" + "runtime" + "strings" + "testing" + + "github.com/hashicorp/terraform-exec/tfexec" + "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" + + "github.com/cloudposse/atmos/pkg/auth/types" + cfg "github.com/cloudposse/atmos/pkg/config" + "github.com/cloudposse/atmos/pkg/schema" + "github.com/cloudposse/atmos/pkg/toolchain" +) + +// outputToolchain is a pre-seeded toolchain install: no tool is downloaded, and the install +// directories are absent from the ambient PATH. +type outputToolchain struct { + config *schema.AtmosConfiguration + tools map[string]string + ambient string +} + +func newOutputToolchain(t *testing.T, versions ...string) outputToolchain { + t.Helper() + previous := toolchain.GetAtmosConfig() + t.Cleanup(func() { toolchain.SetAtmosConfig(previous) }) + ambient := t.TempDir() + t.Setenv("PATH", ambient) + t.Setenv("CUSTOM_VALUE", "ambient") + t.Setenv("AWS_PROFILE", "ambient-profile") + + config := validAtmosConfig(t) + config.BasePathAbsolute = config.BasePath + config.Toolchain = schema.Toolchain{ + Install: schema.ToolchainInstallNever, InstallPath: filepath.Join(config.BasePath, "tools"), + Aliases: map[string]string{"tofu": "opentofu/opentofu", "fixture-tool": "acme/fixture-tool"}, + } + executable, err := os.Executable() + require.NoError(t, err) + content, err := os.ReadFile(executable) + require.NoError(t, err) + tools := map[string]string{} + for _, version := range versions { + for name, owner := range map[string]string{"tofu": "opentofu/opentofu", "fixture-tool": "acme/fixture-tool"} { + file := name + if runtime.GOOS == "windows" { + file += ".exe" + } + target := filepath.Join(config.Toolchain.InstallPath, "bin", filepath.FromSlash(owner), version, file) + require.NoError(t, os.MkdirAll(filepath.Dir(target), 0o755)) + require.NoError(t, os.WriteFile(target, content, 0o755)) + tools[name+"@"+version] = target + } + } + return outputToolchain{config: config, tools: tools, ambient: ambient} +} + +// outputRun is what one dependency output read handed to Terraform. +type outputRun struct { + executable string + env map[string]string + outputs map[string]any +} + +func (o outputToolchain) read(t *testing.T, version string, mutate func(sections map[string]any), auth *schema.AuthContext) outputRun { + t.Helper() + ctrl := gomock.NewController(t) + runner := NewMockTerraformRunner(ctrl) + run := outputRun{} + runner.EXPECT().SetEnv(gomock.Any()).DoAndReturn(func(env map[string]string) error { run.env = env; return nil }).AnyTimes() + runner.EXPECT().Init(gomock.Any(), gomock.Any()).Return(nil) + runner.EXPECT().WorkspaceSelect(gomock.Any(), "test-workspace").Return(nil) + runner.EXPECT().Output(gomock.Any()).Return(map[string]tfexec.OutputMeta{"id": {Value: []byte(`"value"`)}}, nil) + executor := NewExecutor(NewMockComponentDescriber(ctrl), WithRunnerFactory(func(_, executable string) (TerraformRunner, error) { + run.executable = executable + return runner, nil + })) + sections := validSections() + sections[cfg.CommandSectionName] = "tofu" + sections["dependencies"] = map[string]any{"tools": map[string]any{"tofu": version, "fixture-tool": version}} + if mutate != nil { + mutate(sections) + } + outputs, err := executor.ExecuteWithSections(o.config, "test-component", "test-stack", sections, auth) + require.NoError(t, err) + run.outputs = outputs + return run +} + +// hasFixtureTool reports whether the fixture tool executable is on the given PATH value. +func hasFixtureTool(path string) (string, bool) { + name := "fixture-tool" + if runtime.GOOS == "windows" { + name += ".exe" + } + for _, dir := range filepath.SplitList(path) { + candidate := filepath.Join(dir, name) + if info, err := os.Stat(candidate); err == nil && !info.IsDir() { + return candidate, true + } + } + return "", false +} + +// A dependency output read (the !terraform.output and atmos.Component path) runs the executable +// pinned through dependencies.tools, and a tool declared there is on the PATH Terraform sees +// even though neither is on the ambient PATH. Component env wins over ambient env. +func TestDependencyOutputReadUsesPinnedExecutableAndToolchainPath(t *testing.T) { + setup := newOutputToolchain(t, "1.0.0") + run := setup.read(t, "1.0.0", func(sections map[string]any) { + sections[cfg.EnvSectionName] = map[string]any{"CUSTOM_VALUE": "from-stack"} + }, nil) + + require.Equal(t, setup.tools["tofu@1.0.0"], run.executable, "the pinned toolchain executable, not the bare name") + found, ok := hasFixtureTool(run.env["PATH"]) + require.True(t, ok, "dependencies.tools executables must be on the Terraform PATH: %s", run.env["PATH"]) + require.Equal(t, setup.tools["fixture-tool@1.0.0"], found) + _, ambientHasTool := hasFixtureTool(os.Getenv("PATH")) + require.False(t, ambientHasTool) + require.Equal(t, "from-stack", run.env["CUSTOM_VALUE"], "component env beats ambient env") + require.Equal(t, "value", run.outputs["id"]) +} + +// A PATH set by the component survives behind the toolchain directories. +func TestDependencyOutputReadKeepsComponentPath(t *testing.T) { + setup := newOutputToolchain(t, "1.0.0") + custom := filepath.Join(t.TempDir(), "custom-bin") + run := setup.read(t, "1.0.0", func(sections map[string]any) { + sections[cfg.EnvSectionName] = map[string]any{"PATH": custom} + }, nil) + + entries := filepath.SplitList(run.env["PATH"]) + require.Contains(t, entries, custom) + _, ok := hasFixtureTool(run.env["PATH"]) + require.True(t, ok) + // Toolchain directories (their relative order is unspecified) precede the component PATH. + require.ElementsMatch(t, []string{filepath.Dir(setup.tools["tofu@1.0.0"]), filepath.Dir(setup.tools["fixture-tool@1.0.0"])}, entries[:2]) + require.Equal(t, custom, entries[2]) +} + +// The authenticated identity's profile replaces an ambient profile, and two consecutive reads with +// different identities and tool versions never share PATH entries, executables, or credentials. +func TestDependencyOutputReadIsolatesIdentitiesAndVersions(t *testing.T) { + setup := newOutputToolchain(t, "1.0.0", "2.0.0") + credentials, config := filepath.Join(t.TempDir(), "credentials"), filepath.Join(t.TempDir(), "config") + identity := func(profile string) *schema.AuthContext { + return &schema.AuthContext{AWS: &schema.AWSAuthContext{Profile: profile, CredentialsFile: credentials, ConfigFile: config, Region: "us-east-2"}} + } + first := setup.read(t, "1.0.0", nil, identity("development")) + second := setup.read(t, "2.0.0", nil, identity("production")) + + require.Equal(t, "development", first.env["AWS_PROFILE"]) + require.Equal(t, "production", second.env["AWS_PROFILE"]) + require.Equal(t, setup.tools["tofu@1.0.0"], first.executable) + require.Equal(t, setup.tools["tofu@2.0.0"], second.executable) + for version, run := range map[string]outputRun{"1.0.0": first, "2.0.0": second} { + found, ok := hasFixtureTool(run.env["PATH"]) + require.True(t, ok, version) + require.Equal(t, setup.tools["fixture-tool@"+version], found) + for _, entry := range filepath.SplitList(run.env["PATH"]) { + other := "1.0.0" + if version == "1.0.0" { + other = "2.0.0" + } + require.False(t, strings.Contains(entry, string(filepath.Separator)+other), "%s run must not see the %s toolchain: %s", version, other, entry) + } + } + // The ambient process environment is untouched. + require.Equal(t, "ambient-profile", os.Getenv("AWS_PROFILE")) + require.Equal(t, setup.ambient, os.Getenv("PATH")) +} + +// A plugin-owned component's output read starts from the identity's sanitized environment, never +// the ambient one, and still receives the pinned executable and the toolchain directories. +func TestPluginOwnedOutputReadUsesIdentityEnvironmentAndToolchain(t *testing.T) { + setup := newOutputToolchain(t, "1.0.0") + t.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", "ambient-token-must-not-return") + identityBin := filepath.Join(t.TempDir(), "identity-bin") + + ctrl := gomock.NewController(t) + manager := types.NewMockAuthManager(ctrl) + manager.EXPECT().GetStackInfo().Return(&schema.ConfigAndStacksInfo{SanitizedEnv: []string{"AWS_PROFILE=production", "PATH=" + identityBin + string(os.PathListSeparator) + setup.ambient}}).AnyTimes() + runner := NewMockTerraformRunner(ctrl) + var env map[string]string + runner.EXPECT().SetEnv(gomock.Any()).DoAndReturn(func(got map[string]string) error { env = got; return nil }).AnyTimes() + runner.EXPECT().Init(gomock.Any(), gomock.Any()).Return(nil) + runner.EXPECT().WorkspaceSelect(gomock.Any(), "prod").Return(nil) + runner.EXPECT().Output(gomock.Any()).Return(map[string]tfexec.OutputMeta{"id": {Value: []byte(`"value"`)}}, nil) + var executable string + executor := NewExecutor(NewMockComponentDescriber(ctrl), WithRunnerFactory(func(_, got string) (TerraformRunner, error) { + executable = got + return runner, nil + })) + stateRoot := t.TempDir() + setup.config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{"path": filepath.Join(stateRoot, "default.tfstate")}, Workspace: "prod", WorkingDirectory: stateRoot}, nil + }} + sections := validSections() + sections["settings"] = map[string]any{"configuration": true} + sections[cfg.CommandSectionName] = "tofu" + sections["dependencies"] = map[string]any{"tools": map[string]any{"tofu": "1.0.0", "fixture-tool": "1.0.0"}} + + outputs, err := executor.execute(t.Context(), setup.config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true, manager) + require.NoError(t, err) + require.Equal(t, "value", outputs["id"]) + + require.Equal(t, setup.tools["tofu@1.0.0"], executable) + require.Equal(t, "production", env["AWS_PROFILE"], "identity environment is the base") + require.NotContains(t, env, "AWS_WEB_IDENTITY_TOKEN_FILE", "ambient credentials must not return behind an identity") + entries := filepath.SplitList(env["PATH"]) + require.Contains(t, entries, identityBin, "the identity PATH is extended, not replaced") + found, ok := hasFixtureTool(env["PATH"]) + require.True(t, ok) + require.Equal(t, setup.tools["fixture-tool@1.0.0"], found) +} diff --git a/plugins/releases.yaml b/plugins/releases.yaml index 692569a6e30..d8d6dd1d21b 100644 --- a/plugins/releases.yaml +++ b/plugins/releases.yaml @@ -12,3 +12,12 @@ packages: - pkg/plugin/oci/** - sdk/** - proto/** + terragrunt: + path: plugins/terragrunt + repository: oci://ghcr.io/cloudposse/atmos/plugins/terragrunt + inputs: + - .github/workflows/plugin-packages.yaml + - pkg/plugin/build/** + - pkg/plugin/oci/** + - sdk/** + - proto/** diff --git a/plugins/terragrunt/README.md b/plugins/terragrunt/README.md index f2bb0dd0ec2..2a3b894351b 100644 --- a/plugins/terragrunt/README.md +++ b/plugins/terragrunt/README.md @@ -17,7 +17,15 @@ atmos build On Windows, use `build/atmos.exe`. CI bootstraps the host with `go build -o build/atmos.exe .`, then uses the same plugin commands. -The default suite runs Go unit tests and Starlark affected-selection fixtures. It requires Go and Git, but no Python, shell, Terraform, or Terragrunt executable. The integration suite requires OpenTofu or Terraform on PATH. It copies both fixtures into temporary directories, applies only built-in `terraform_data` resources with local state, checks unchanged plans and dependency refresh, then destroys those resources. Deferred cleanup removes the temporary directories even when an assertion fails. Neither suite calls the Terragrunt CLI. +The default suite runs Go unit tests, Starlark affected-selection fixtures, and the installed-release scenario. It requires Go and Git, but no Python, shell, Terraform, or Terragrunt executable. The integration suite requires OpenTofu or Terraform on PATH and applies only built-in `terraform_data` resources with local state in temporary directories. Its scenarios cover: + +- inventory, apply, unchanged plans, fresh dependency outputs, and reverse-order destroy (`native.star`); +- `before_hook`, `after_hook`, `error_hook`, and `extra_arguments` ordering, scoping, failure exit codes, and implicit init; +- saved-plan apply, including rejection of a replaced plan file, a changed working directory, and edited configuration; +- `!terraform.output` and `!terraform.state` reads of a Terragrunt unit, including the never-applied case, which must not create state; +- Git (`git::file://`) and local module sources, including re-provisioning after the source changes. + +Neither suite calls the Terragrunt CLI. Use `plugin test --build` after changing plugin code, because the suite runs the built plugin binary and a stale binary can still pass. Configure a project with: @@ -48,13 +56,17 @@ The `describe affected --base ` and `terraform plan --affected --ref ` The affected acceptance runner uses temporary Git repositories and dry runs. It checks additions, deletions, renames, changed dependency targets, shared includes, local module files, nested explicit stacks, mixed YAML/HCL graphs, stack filters, and dependent depth. Both output and ordering edges participate in `--include-dependents`. The runner requires Git but no Terraform or Terragrunt executable. -Source tracking currently covers files read during partial discovery, declared HCL, local module directories, and stack template provenance. Reads in unevaluated input expressions, arbitrary dynamic globs, and effect-dependent discovery need further work. This is not a claim that every Terragrunt expression has complete affected provenance. +Source tracking covers files read during partial discovery, declared HCL, local module directories, stack template provenance, and files and globs named by input expressions. A dynamic path that cannot be resolved without running the expression falls back to treating the unit as affected by any change in its scope. Effect-dependent discovery still fails. This is not a claim that every Terragrunt expression has complete affected provenance. ## Scope The fixtures cover local sources, local backend paths, includes, locals, nested stack values, literal generation, order and output dependencies, command-scoped mocks, fresh outputs, and native bulk execution. Unit tests cover absolute and relative workdir overrides, ownership conflicts, repeated provisioning without state loss, concurrent environment isolation, unit-relative child commands, cancellation, and rejected effects. The plugin workflow runs its unit tests on Linux, macOS, and Windows. These checks do not establish a compatibility percentage. -Current limits include remote module/catalog sources, non-local backends, Terragrunt hooks and extra arguments, IAM options, feature flags, error/retry blocks, prevention/exclusion rules, inline modules without `terraform.source`, named-workspace output dependencies, and saved-plan apply. Unsupported semantics must fail rather than switch executors. Explicit `terragrunt://` imports, state-function host dispatch, executable mode, engine table columns, UI/log adapters, and conversion commands remain in the PRDs. +The host executes `extra_arguments` and `before_hook`, `after_hook`, and `error_hook` blocks with the invocation's environment, identity, toolchain PATH, and cancellation. The plugin only evaluates them. Hooks run once per Atmos phase, and an implicit `init` matches hooks for `init`, not for the requested command. Hook output goes to stderr. Workspace selection, `providers lock`, dependency-output reads, and `atmos terraform shell` do not run hooks. A missing required var file fails before any process starts. Saved-plan apply reuses the execution data recorded at plan time and requires the original working directory. + +Remote module sources (`git::https`, `git::http`, `git::ssh`, `git::file`, `https`, `http`) keep their getter URI for the Atmos source provisioner. Local module sources are re-copied on every run (`source.ttl: 0s`) so edits take effect. Registry sources and remote stack catalogs are not supported. Backend configuration is adopted, including cloud backends, but Atmos does not provision the backend resources. + +Unsupported semantics fail before any infrastructure command and never switch executors. Currently rejected: `terraform_binary`; `errors`, `exclude`, feature flags, and IAM role or web-identity settings; `remote_state` with `disable_init` or encryption; inline modules without `terraform.source`; output dependencies with a non-default workspace during discovery; the `read-config`, `terragrunt-read-config`, and `init-from-module` hook lifecycles; hooks that run a built-in integration such as `tflint`; and an invalid `on_errors` expression. Executable mode, typed provider flags and help, exact cty types and sensitivity, scoped Terragrunt logging, migration and setup generation, and Atmos Pro integration remain in the PRDs. Only enabled/mode/provider/discovery settings are accepted by this package. Additional plugin settings and typed CLI option handling remain planned. Discovery that needs command execution, decryption, network access, or identity-dependent effects fails. The native plugin is trusted code, not an OS sandbox. diff --git a/plugins/terragrunt/bridge/backend.go b/plugins/terragrunt/bridge/backend.go new file mode 100644 index 00000000000..66950276c8f --- /dev/null +++ b/plugins/terragrunt/bridge/backend.go @@ -0,0 +1,34 @@ +package atmosbridge + +import ( + "maps" + + "github.com/gruntwork-io/terragrunt/internal/remotestate" + "github.com/gruntwork-io/terragrunt/internal/remotestate/backend/azurerm" + "github.com/gruntwork-io/terragrunt/internal/remotestate/backend/gcs" + "github.com/gruntwork-io/terragrunt/internal/remotestate/backend/s3" +) + +// BackendConfig strips upstream bootstrap-only settings without creating cloud +// resources. Values remain native so the host can generate JSON backend blocks. +func BackendConfig(remote *remotestate.Config) map[string]any { + var values map[string]any + switch remote.BackendName { + case "s3": + values = s3.Config(remote.BackendConfig).GetTFInitArgs() + // Upstream init arguments encode nested blocks as HCL strings. Preserve + // their original structure for Atmos's JSON backend generator instead. + for _, key := range []string{"assume_role", "assume_role_with_web_identity"} { + if original, ok := remote.BackendConfig[key].(map[string]any); ok { + values[key] = maps.Clone(original) + } + } + case "gcs": + values = gcs.Config(remote.BackendConfig).GetTFInitArgs() + case "azurerm": + values = azurerm.Config(remote.BackendConfig).GetTFInitArgs() + default: + values = maps.Clone(remote.BackendConfig) + } + return values +} diff --git a/plugins/terragrunt/bridge/bridge.go b/plugins/terragrunt/bridge/bridge.go index 03a78dd4f78..23e1198d1f2 100644 --- a/plugins/terragrunt/bridge/bridge.go +++ b/plugins/terragrunt/bridge/bridge.go @@ -9,6 +9,7 @@ import ( "io" "path/filepath" + "github.com/gruntwork-io/terragrunt/internal/iacargs" "github.com/gruntwork-io/terragrunt/internal/stacks/generate" "github.com/gruntwork-io/terragrunt/internal/tf" "github.com/gruntwork-io/terragrunt/internal/venv" @@ -59,3 +60,8 @@ func Generate(ctx context.Context, logger log.Logger, env *venv.Venv, path, tofu opts.TFPath = tofu return generate.NewGenerator().GenerateStacks(ctx, logger, env, opts, nil) } + +// IsDestroyOperation uses upstream boolean flag parsing, including last-value precedence. +func IsDestroyOperation(command string, arguments []string) bool { + return command == "destroy" || iacargs.HasDestroyFlag(arguments) +} diff --git a/plugins/terragrunt/bridge/generation.go b/plugins/terragrunt/bridge/generation.go new file mode 100644 index 00000000000..fc869536e09 --- /dev/null +++ b/plugins/terragrunt/bridge/generation.go @@ -0,0 +1,45 @@ +package atmosbridge + +import ( + "context" + "io" + "path/filepath" + + "github.com/gruntwork-io/terragrunt/internal/codegen" + "github.com/gruntwork-io/terragrunt/internal/remotestate" + "github.com/gruntwork-io/terragrunt/pkg/log" + "github.com/spf13/afero" +) + +// RenderGenerated uses upstream signature and formatting semantics in memory. +// The host applies ownership policies later, in the provisioned working directory. +func RenderGenerated(ctx context.Context, logger log.Logger, config codegen.GenerateConfig) ([]byte, error) { + fs := afero.NewMemMapFs() + directory := filepath.Join(string(filepath.Separator), "generated") + if err := fs.MkdirAll(filepath.Join(directory, filepath.Dir(config.Path)), 0o700); err != nil { + return nil, err + } + config.Disable = false + env := Environment(map[string]string{}, io.Discard, io.Discard).WithFS(fs) + if err := codegen.WriteToFile(ctx, logger, env, directory, &config); err != nil { + return nil, err + } + return afero.ReadFile(fs, filepath.Join(directory, config.Path)) +} + +// RenderBackend preserves upstream backend syntax and signatures without writing +// files or provisioning remote backend resources. +func RenderBackend(ctx context.Context, logger log.Logger, remote *remotestate.Config, backend map[string]any) ([]byte, error) { + fs := afero.NewMemMapFs() + directory := filepath.Join(string(filepath.Separator), "generated") + if err := fs.MkdirAll(filepath.Join(directory, filepath.Dir(remote.Generate.Path)), 0o700); err != nil { + return nil, err + } + resolved := *remote + resolved.BackendConfig = backend + env := Environment(map[string]string{}, io.Discard, io.Discard).WithFS(fs) + if err := remotestate.New(&resolved).GenerateOpenTofuCode(ctx, logger, env, directory); err != nil { + return nil, err + } + return afero.ReadFile(fs, filepath.Join(directory, remote.Generate.Path)) +} diff --git a/plugins/terragrunt/go.mod b/plugins/terragrunt/go.mod index 66df99a36e2..72620d64596 100644 --- a/plugins/terragrunt/go.mod +++ b/plugins/terragrunt/go.mod @@ -8,6 +8,7 @@ require ( github.com/gruntwork-io/terragrunt v1.2.1 github.com/gruntwork-io/terragrunt/pkg/atmosbridge v0.0.0-00010101000000-000000000000 github.com/hashicorp/go-version v1.9.0 + github.com/hashicorp/hcl/v2 v2.25.0 github.com/spf13/afero v1.15.0 github.com/stretchr/testify v1.12.1 github.com/zclconf/go-cty v1.19.0 @@ -138,7 +139,6 @@ require ( github.com/hashicorp/go-sockaddr v1.0.7 // indirect github.com/hashicorp/go-uuid v1.0.4 // indirect github.com/hashicorp/hcl v1.0.1-vault-7 // indirect - github.com/hashicorp/hcl/v2 v2.25.0 // indirect github.com/hashicorp/terraform-svchost v0.2.1 // indirect github.com/hashicorp/vault/api v1.23.0 // indirect github.com/hashicorp/yamux v0.1.2 // indirect diff --git a/plugins/terragrunt/internal/engine/backend.go b/plugins/terragrunt/internal/engine/backend.go new file mode 100644 index 00000000000..0ee6d64ebef --- /dev/null +++ b/plugins/terragrunt/internal/engine/backend.go @@ -0,0 +1,34 @@ +package engine + +import ( + "fmt" + + "github.com/gruntwork-io/terragrunt/pkg/atmosbridge" + "github.com/gruntwork-io/terragrunt/pkg/config" + + "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" +) + +func resolveBackend(cfg *config.TerragruntConfig, result *model.Resolution) error { + if cfg.RemoteState == nil { + return nil + } + if cfg.RemoteState.DisableInit || len(cfg.RemoteState.Encryption) > 0 { + return fmt.Errorf("native adaptation of remote_state disable_init or encryption is not implemented") + } + result.BackendType = cfg.RemoteState.BackendName + result.Backend = atmosbridge.BackendConfig(cfg.RemoteState.Config) + if result.BackendType != "local" { + return nil + } + // Local state remains relative to Terragrunt's source cache identity, + // independently of the execution directory chosen by Atmos. + for key, fallback := range map[string]string{"path": "terraform.tfstate", "workspace_dir": "terraform.tfstate.d"} { + value, _ := result.Backend[key].(string) + if value == "" { + value = fallback + } + result.Backend[key] = localPath(result.WorkingDir, value) + } + return nil +} diff --git a/plugins/terragrunt/internal/engine/backend_test.go b/plugins/terragrunt/internal/engine/backend_test.go new file mode 100644 index 00000000000..1b5656c6968 --- /dev/null +++ b/plugins/terragrunt/internal/engine/backend_test.go @@ -0,0 +1,108 @@ +package engine + +import ( + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestCloudBackendIdentityWithoutNetwork(t *testing.T) { + for _, test := range []struct { + name, config, excluded string + }{ + {"s3", `bucket = "existing-bucket", key = "env/state", region = "us-east-1", skip_bucket_versioning = true`, "skip_bucket_versioning"}, + {"gcs", `bucket = "existing-bucket", prefix = "env/state", skip_bucket_creation = true`, "skip_bucket_creation"}, + {"azurerm", `storage_account_name = "existing", container_name = "state", key = "env/state", resource_group_name = "existing", skip_storage_account_creation = true`, "skip_storage_account_creation"}, + {"http", `address = "http://127.0.0.1:1/state"`, "unused"}, + } { + t.Run(test.name, func(t *testing.T) { + request := nativeUnit(t, `remote_state { + backend = "`+test.name+`" + config = { `+test.config+` } +}`) + request.Phase = "backend" + result, err := Resolve(t.Context(), request) + require.NoError(t, err) + require.Equal(t, test.name, result.BackendType) + require.NotContains(t, result.Backend, test.excluded) + require.NotEmpty(t, result.Backend) + require.NoDirExists(t, filepath.Join(filepath.Dir(request.Path), ".terragrunt-cache")) + }) + } +} + +func TestRemoteBackendGenerationRetainsPathAndPolicy(t *testing.T) { + request := nativeUnit(t, `remote_state { + backend = "s3" + generate = { path = "custom/backend.tf", if_exists = "overwrite_terragrunt" } + config = { + bucket = "existing-bucket" + key = "env/state" + region = "us-east-1" + skip_bucket_versioning = true + assume_role = { role_arn = "arn:aws:iam::123456789012:role/state" } + } +}`) + result, err := Resolve(t.Context(), request) + require.NoError(t, err) + require.True(t, result.BackendGenerated) + path := filepath.Join("custom", "backend.tf") + require.Equal(t, "overwrite_terragrunt", result.GeneratedPolicies[path]) + require.Contains(t, result.Generated[path], `backend "s3"`) + require.Contains(t, result.Generated[path], `assume_role = {`) + require.NotContains(t, result.Generated[path], "skip_bucket_versioning") + require.IsType(t, map[string]any{}, result.Backend["assume_role"]) + require.NoDirExists(t, filepath.Join(filepath.Dir(request.Path), "custom")) +} + +func TestUnsupportedBackendSemanticsAndPaths(t *testing.T) { + for _, test := range []struct{ name, hcl, want string }{ + {"disable_init", `remote_state { + backend = "s3" + disable_init = true + config = { bucket = "existing" } +}`, "disable_init or encryption is not implemented"}, + {"encryption", `remote_state { + backend = "s3" + encryption = { key_provider = "pbkdf2", passphrase = "0123456789abcdef" } + config = { bucket = "existing" } +}`, "disable_init or encryption is not implemented"}, + {"backend path escapes", `remote_state { + backend = "local" + generate = { path = "../escape.tf", if_exists = "overwrite" } + config = { path = "state" } +}`, "unsupported backend generated path: ../escape.tf"}, + {"backend path is the directory", `remote_state { + backend = "local" + generate = { path = ".", if_exists = "overwrite" } + config = { path = "state" } +}`, "unsupported backend generated path: ."}, + {"backend path duplicates generate block", `remote_state { + backend = "local" + generate = { path = "same.tf", if_exists = "overwrite" } + config = { path = "state" } +} +generate "duplicate" { + path = "same.tf" + if_exists = "overwrite" + contents = "" +}`, "duplicate backend generated path: same.tf"}, + {"backend path duplicates a normalized generate path", `remote_state { + backend = "local" + generate = { path = "./same.tf", if_exists = "overwrite" } + config = { path = "state" } +} +generate "duplicate" { + path = "same.tf" + if_exists = "overwrite" + contents = "" +}`, "duplicate backend generated path: ./same.tf"}, + } { + t.Run(test.name, func(t *testing.T) { + result, err := Resolve(t.Context(), nativeUnit(t, test.hcl)) + require.ErrorContains(t, err, test.want) + require.Nil(t, result) + }) + } +} diff --git a/plugins/terragrunt/internal/engine/discovery.go b/plugins/terragrunt/internal/engine/discovery.go index 2dcde3c262f..9a231f5901b 100644 --- a/plugins/terragrunt/internal/engine/discovery.go +++ b/plugins/terragrunt/internal/engine/discovery.go @@ -103,7 +103,7 @@ func discoverProject(ctx context.Context, request model.Request) (*model.Resolut } return nil } - if len(resolved.Folders) == 0 { + if resolved.Source == "" && len(resolved.Folders) == 0 { entries, err := afero.ReadDir(overlay, filepath.Dir(path)) if err != nil { return err @@ -115,11 +115,11 @@ func discoverProject(ctx context.Context, request model.Request) (*model.Resolut } return nil } // Shared include, not a runnable local-source unit. - if !plainLocalSource(resolved.Folders[0]) { - return fmt.Errorf("discovery only supports local sources: %s", path) - } resolved.Files = append(resolved.Files, files...) resolved.Folders = append(resolved.Folders, folders...) + if err := trackInputSources(overlay, root, resolved); err != nil { + return err + } result.Inventory = append(result.Inventory, model.DiscoveredUnit{Stack: stack, Name: filepath.ToSlash(name), GenerationRoot: generationRoot, Config: *resolved}) return nil } diff --git a/plugins/terragrunt/internal/engine/environment_test.go b/plugins/terragrunt/internal/engine/environment_test.go index cd939c8c704..142b7d563e7 100644 --- a/plugins/terragrunt/internal/engine/environment_test.go +++ b/plugins/terragrunt/internal/engine/environment_test.go @@ -14,6 +14,9 @@ import ( ) func TestMain(m *testing.M) { + if runToolchainChild() { + os.Exit(0) + } if os.Getenv("ATMOS_TERRAGRUNT_VERSION_CHILD") != "" { _, _ = os.Stdout.WriteString(os.Getenv("ATMOS_TERRAGRUNT_VERSION_CHILD")) os.Exit(0) diff --git a/plugins/terragrunt/internal/engine/execution.go b/plugins/terragrunt/internal/engine/execution.go new file mode 100644 index 00000000000..85cad98b24a --- /dev/null +++ b/plugins/terragrunt/internal/engine/execution.go @@ -0,0 +1,138 @@ +package engine + +import ( + "errors" + "fmt" + "maps" + "regexp" + "slices" + + "github.com/gruntwork-io/terragrunt/pkg/config" + + "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" +) + +// ErrUnsupportedExecution identifies Terragrunt hook or extra_arguments semantics that the +// host-owned execution contract does not implement. Messages name only hook identifiers and +// command names, never the argv or evaluated values, so they are safe to show to the user. +var ErrUnsupportedExecution = errors.New("unsupported native Terragrunt execution configuration") + +// unsupportedLifecycles are hook commands that name Terragrunt's own lifecycle rather than a +// Terraform command. The host has no equivalent lifecycle yet, so accepting them would silently +// never run the hook. +var unsupportedLifecycles = map[string]string{ + "terragrunt-read-config": "runs after Terragrunt reads configuration", + "read-config": "runs after Terragrunt reads configuration", + "init-from-module": "runs while Terragrunt downloads the module source", +} + +// builtinHookExecutables are executable names Terragrunt intercepts instead of running them. +var builtinHookExecutables = map[string]struct{}{"tflint": {}} + +// executionFromConfig converts the evaluated terraform block into data the host executes. +// It returns nil when the unit declares no hooks or extra arguments. Evaluation never runs a hook. +func executionFromConfig(terraform *config.TerraformConfig) (*model.Execution, error) { + if terraform == nil { + return nil, nil + } + result := &model.Execution{} + for i := range terraform.ExtraArgs { + result.ExtraArguments = append(result.ExtraArguments, extraArguments(&terraform.ExtraArgs[i])) + } + var err error + if result.BeforeHooks, err = hooksFromConfig("before_hook", terraform.BeforeHooks); err != nil { + return nil, err + } + if result.AfterHooks, err = hooksFromConfig("after_hook", terraform.AfterHooks); err != nil { + return nil, err + } + if result.ErrorHooks, err = errorHooksFromConfig(terraform.ErrorHooks); err != nil { + return nil, err + } + if len(result.ExtraArguments)+len(result.BeforeHooks)+len(result.AfterHooks)+len(result.ErrorHooks) == 0 { + return nil, nil + } + return result, nil +} + +func extraArguments(source *config.TerraformExtraArguments) model.ExtraArguments { + result := model.ExtraArguments{Commands: slices.Clone(source.Commands)} + if source.Arguments != nil { + result.Arguments = slices.Clone(*source.Arguments) + } + if source.RequiredVarFiles != nil { + result.RequiredVarFiles = slices.Clone(*source.RequiredVarFiles) + } + if source.OptionalVarFiles != nil { + result.OptionalVarFiles = slices.Clone(*source.OptionalVarFiles) + } + if source.EnvVars != nil && len(*source.EnvVars) > 0 { + result.Environment = maps.Clone(*source.EnvVars) + } + return result +} + +func hooksFromConfig(kind string, source []config.Hook) ([]model.Hook, error) { + var result []model.Hook + for i := range source { + hook := &source[i] + // A false condition disables the hook, exactly as it does for Terragrunt itself. + if hook.If != nil && !*hook.If { + continue + } + converted := model.Hook{Name: hook.Name, Commands: slices.Clone(hook.Commands), Execute: slices.Clone(hook.Execute)} + if hook.WorkingDir != nil { + converted.WorkingDirectory = *hook.WorkingDir + } + if hook.RunOnError != nil { + converted.RunOnError = *hook.RunOnError + } + if hook.SuppressStdout != nil { + converted.SuppressStdout = *hook.SuppressStdout + } + if err := validateHook(kind, &converted); err != nil { + return nil, err + } + result = append(result, converted) + } + return result, nil +} + +func errorHooksFromConfig(source []config.ErrorHook) ([]model.Hook, error) { + var result []model.Hook + for i := range source { + hook := &source[i] + converted := model.Hook{Name: hook.Name, Commands: slices.Clone(hook.Commands), Execute: slices.Clone(hook.Execute), OnErrors: slices.Clone(hook.OnErrors)} + if hook.WorkingDir != nil { + converted.WorkingDirectory = *hook.WorkingDir + } + if hook.SuppressStdout != nil { + converted.SuppressStdout = *hook.SuppressStdout + } + if err := validateHook("error_hook", &converted); err != nil { + return nil, err + } + for _, pattern := range converted.OnErrors { + if _, err := regexp.Compile(pattern); err != nil { + return nil, fmt.Errorf("%w: error_hook %q on_errors is not a valid regular expression", ErrUnsupportedExecution, converted.Name) + } + } + result = append(result, converted) + } + return result, nil +} + +func validateHook(kind string, hook *model.Hook) error { + for _, command := range hook.Commands { + if reason, unsupported := unsupportedLifecycles[command]; unsupported { + return fmt.Errorf("%w: %s %q lists the %q lifecycle command, which %s; it is not implemented", ErrUnsupportedExecution, kind, hook.Name, command, reason) + } + } + if len(hook.Execute) == 0 || hook.Execute[0] == "" { + return fmt.Errorf("%w: %s %q must set a non-empty execute list", ErrUnsupportedExecution, kind, hook.Name) + } + if _, builtin := builtinHookExecutables[hook.Execute[0]]; builtin { + return fmt.Errorf("%w: %s %q runs the built-in %q integration, which is not implemented", ErrUnsupportedExecution, kind, hook.Name, hook.Execute[0]) + } + return nil +} diff --git a/plugins/terragrunt/internal/engine/execution_test.go b/plugins/terragrunt/internal/engine/execution_test.go new file mode 100644 index 00000000000..cee787d1d4e --- /dev/null +++ b/plugins/terragrunt/internal/engine/execution_test.go @@ -0,0 +1,200 @@ +package engine + +import ( + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" +) + +// executionUnit writes a unit whose terraform block carries body, so hooks and extra +// arguments live in the one block Terragrunt allows. +func executionUnit(t *testing.T, body string) model.Request { + t.Helper() + root := t.TempDir() + module := filepath.Join(root, "module") + require.NoError(t, os.MkdirAll(module, 0o755)) + source, err := json.Marshal(filepath.ToSlash(module)) + require.NoError(t, err) + path := filepath.Join(root, "terragrunt.hcl") + require.NoError(t, os.WriteFile(path, []byte("terraform {\n source = "+string(source)+"\n"+body+"\n}\n"), 0o644)) + return model.Request{Path: path, BasePath: root, Command: "plan", Tofu: "must-not-execute", Environment: map[string]string{}} +} + +func TestExecutionIsAbsentWithoutHooksOrArguments(t *testing.T) { + result, err := Resolve(t.Context(), executionUnit(t, "")) + require.NoError(t, err) + require.Nil(t, result.Execution) +} + +func TestExecutionPreservesDeclarationOrderAndFields(t *testing.T) { + result, err := Resolve(t.Context(), executionUnit(t, ` + extra_arguments "second_declared_first" { + commands = ["plan", "apply"] + arguments = ["-lock-timeout=5m", "-compact-warnings"] + } + extra_arguments "vars" { + commands = ["plan"] + required_var_files = ["required.tfvars", "required-b.tfvars"] + optional_var_files = ["optional.tfvars"] + env_vars = { TF_LOG = "INFO", ZED = "z" } + } + before_hook "b2" { + commands = ["plan"] + execute = ["tool", "one", "two"] + } + before_hook "b1" { + commands = ["plan", "apply"] + execute = ["tool", "three"] + working_dir = "sub" + suppress_stdout = true + } + after_hook "a1" { + commands = ["plan"] + execute = ["tool", "after"] + run_on_error = true + } + error_hook "e1" { + commands = ["apply"] + execute = ["tool", "error"] + on_errors = ["timeout", ".*denied.*"] + } +`)) + require.NoError(t, err) + require.NotNil(t, result.Execution) + + require.Equal(t, []model.ExtraArguments{ + {Commands: []string{"plan", "apply"}, Arguments: []string{"-lock-timeout=5m", "-compact-warnings"}}, + { + Commands: []string{"plan"}, + RequiredVarFiles: []string{"required.tfvars", "required-b.tfvars"}, + OptionalVarFiles: []string{"optional.tfvars"}, + Environment: map[string]string{"TF_LOG": "INFO", "ZED": "z"}, + }, + }, result.Execution.ExtraArguments) + + require.Equal(t, []model.Hook{ + {Name: "b2", Commands: []string{"plan"}, Execute: []string{"tool", "one", "two"}}, + {Name: "b1", Commands: []string{"plan", "apply"}, Execute: []string{"tool", "three"}, WorkingDirectory: "sub", SuppressStdout: true}, + }, result.Execution.BeforeHooks) + require.Equal(t, []model.Hook{ + {Name: "a1", Commands: []string{"plan"}, Execute: []string{"tool", "after"}, RunOnError: true}, + }, result.Execution.AfterHooks) + require.Equal(t, []model.Hook{ + {Name: "e1", Commands: []string{"apply"}, Execute: []string{"tool", "error"}, OnErrors: []string{"timeout", ".*denied.*"}}, + }, result.Execution.ErrorHooks) +} + +func TestExecutionSkipsHooksWhoseConditionIsFalse(t *testing.T) { + result, err := Resolve(t.Context(), executionUnit(t, ` + before_hook "off" { + commands = ["plan"] + execute = ["tool", "off"] + if = false + } + before_hook "on" { + commands = ["plan"] + execute = ["tool", "on"] + if = true + } +`)) + require.NoError(t, err) + require.Len(t, result.Execution.BeforeHooks, 1) + require.Equal(t, "on", result.Execution.BeforeHooks[0].Name) +} + +func TestExecutionRejectsUnimplementedHookSemantics(t *testing.T) { + for _, test := range []struct{ name, body, message string }{ + {"read-config before", `before_hook "x" { + commands = ["terragrunt-read-config"] + execute = ["tool"] + }`, "terragrunt-read-config"}, + {"read-config alias after", `after_hook "x" { + commands = ["plan", "read-config"] + execute = ["tool"] + }`, "read-config"}, + {"init-from-module error", `error_hook "x" { + commands = ["init-from-module"] + execute = ["tool"] + on_errors = [".*"] + }`, "init-from-module"}, + {"tflint built in", `before_hook "lint" { + commands = ["plan"] + execute = ["tflint", "--init"] + }`, "tflint"}, + {"invalid on_errors", `error_hook "bad" { + commands = ["plan"] + execute = ["tool"] + on_errors = ["("] + }`, "on_errors"}, + } { + t.Run(test.name, func(t *testing.T) { + _, err := Resolve(t.Context(), executionUnit(t, test.body)) + require.ErrorIs(t, err, ErrUnsupportedExecution) + require.ErrorContains(t, err, test.message) + }) + } +} + +// Terragrunt itself refuses a hook with nothing to run, so the unit never reaches the host. +func TestExecutionRejectsEmptyExecute(t *testing.T) { + _, err := Resolve(t.Context(), executionUnit(t, `before_hook "empty" { + commands = ["plan"] + execute = [] + }`)) + require.ErrorContains(t, err, "execute") +} + +func TestExecutionIsNotEvaluatedForBackendIdentity(t *testing.T) { + request := executionUnit(t, ` + before_hook "x" { + commands = ["terragrunt-read-config"] + execute = ["tool"] + }`) + request.Phase = "backend" + _, err := Resolve(t.Context(), request) + require.NoError(t, err) +} + +func TestExecutionErrorHooksCarryWorkingDirectoryAndSuppression(t *testing.T) { + result, err := Resolve(t.Context(), executionUnit(t, ` + error_hook "quiet" { + commands = ["apply"] + execute = ["tool", "report"] + on_errors = [".*"] + working_dir = "reports" + suppress_stdout = true + } + error_hook "plain" { + commands = ["apply"] + execute = ["tool", "plain"] + on_errors = [".*"] + } +`)) + require.NoError(t, err) + require.Equal(t, []model.Hook{ + {Name: "quiet", Commands: []string{"apply"}, Execute: []string{"tool", "report"}, OnErrors: []string{".*"}, WorkingDirectory: "reports", SuppressStdout: true}, + {Name: "plain", Commands: []string{"apply"}, Execute: []string{"tool", "plain"}, OnErrors: []string{".*"}}, + }, result.Execution.ErrorHooks) +} + +func TestExecutionFromConfigWithoutTerraformBlock(t *testing.T) { + result, err := executionFromConfig(nil) + require.NoError(t, err) + require.Nil(t, result, "a unit without a terraform block declares no hooks or arguments") +} + +func TestValidateHookRequiresRunnableExecuteList(t *testing.T) { + for name, execute := range map[string][]string{"missing": nil, "empty": {}, "blank executable": {""}} { + t.Run(name, func(t *testing.T) { + err := validateHook("before_hook", &model.Hook{Name: "h", Commands: []string{"plan"}, Execute: execute}) + require.ErrorIs(t, err, ErrUnsupportedExecution) + require.ErrorContains(t, err, "non-empty execute list") + }) + } + require.NoError(t, validateHook("before_hook", &model.Hook{Name: "h", Commands: []string{"plan"}, Execute: []string{"tool"}})) +} diff --git a/plugins/terragrunt/internal/engine/generation.go b/plugins/terragrunt/internal/engine/generation.go new file mode 100644 index 00000000000..b0c51184922 --- /dev/null +++ b/plugins/terragrunt/internal/engine/generation.go @@ -0,0 +1,64 @@ +package engine + +import ( + "context" + "fmt" + "path/filepath" + + "github.com/gruntwork-io/terragrunt/pkg/atmosbridge" + "github.com/gruntwork-io/terragrunt/pkg/config" + "github.com/gruntwork-io/terragrunt/pkg/log" + + "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" +) + +func generatedFiles(ctx context.Context, logger log.Logger, cfg *config.TerragruntConfig, result *model.Resolution) error { + result.GeneratedPolicies = make(map[string]string) + for _, generated := range cfg.GenerateConfigs { + if generated.Disable && (generated.IfDisabledStr == "" || generated.IfDisabledStr == "skip") { + continue + } + if !filepath.IsLocal(generated.Path) || generated.Path == "." { + return fmt.Errorf("unsupported generated path: %s", generated.Path) + } + path := filepath.Clean(generated.Path) + if _, exists := result.GeneratedPolicies[path]; exists { + return fmt.Errorf("duplicate generated path: %s", generated.Path) + } + policy := generated.IfExistsStr + if generated.Disable { + policy = generated.IfDisabledStr + result.Generated[path] = "" + } else { + content, err := atmosbridge.RenderGenerated(ctx, logger, generated) + if err != nil { + return err + } + result.Generated[path] = string(content) + } + result.GeneratedPolicies[path] = policy + } + return generatedBackend(ctx, logger, cfg, result) +} + +func generatedBackend(ctx context.Context, logger log.Logger, cfg *config.TerragruntConfig, result *model.Resolution) error { + if cfg.RemoteState == nil || cfg.RemoteState.Generate == nil { + return nil + } + generated := cfg.RemoteState.Generate + if !filepath.IsLocal(generated.Path) || generated.Path == "." { + return fmt.Errorf("unsupported backend generated path: %s", generated.Path) + } + path := filepath.Clean(generated.Path) + if _, exists := result.GeneratedPolicies[path]; exists { + return fmt.Errorf("duplicate backend generated path: %s", generated.Path) + } + content, err := atmosbridge.RenderBackend(ctx, logger, cfg.RemoteState.Config, result.Backend) + if err != nil { + return err + } + result.Generated[path] = string(content) + result.GeneratedPolicies[path] = generated.IfExists + result.BackendGenerated = true + return nil +} diff --git a/plugins/terragrunt/internal/engine/generation_test.go b/plugins/terragrunt/internal/engine/generation_test.go new file mode 100644 index 00000000000..b5f4d0cc89a --- /dev/null +++ b/plugins/terragrunt/internal/engine/generation_test.go @@ -0,0 +1,123 @@ +package engine + +import ( + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestGenerationUsesUpstreamFormattingAndOwnership(t *testing.T) { + request := nativeUnit(t, `generate "provider" { + path = "nested/provider.tf" + if_exists = "overwrite_terragrunt" + comment_prefix = "// " + contents = "locals { enabled=true }" +} +generate "disabled" { + path = "obsolete.tf" + if_exists = "error" + disable = true + if_disabled = "remove_terragrunt" + contents = "ignored" +} +generate "json" { + path = "data.json" + if_exists = "skip" + disable_signature = true + contents = "{\"a\":true}" +}`) + result, err := Resolve(t.Context(), request) + require.NoError(t, err) + require.True(t, strings.HasPrefix(result.Generated[filepath.Join("nested", "provider.tf")], "// Generated by Terragrunt. Sig: nIlQXj57tbuaRZEa\n")) + require.Contains(t, result.Generated[filepath.Join("nested", "provider.tf")], "enabled = true") + require.Equal(t, "overwrite_terragrunt", result.GeneratedPolicies[filepath.Join("nested", "provider.tf")]) + require.Equal(t, "remove_terragrunt", result.GeneratedPolicies["obsolete.tf"]) + require.Empty(t, result.Generated["obsolete.tf"]) + require.Equal(t, `{"a":true}`, result.Generated["data.json"]) + require.Equal(t, "skip", result.GeneratedPolicies["data.json"]) + require.NoDirExists(t, filepath.Join(filepath.Dir(request.Path), "nested")) +} + +func TestGenerationRejectsDuplicateTargetsAndDisabledEscape(t *testing.T) { + for _, extra := range []string{`generate "a" { + path = "same.tf" + if_exists = "overwrite" + contents = "" +} +generate "b" { + path = "./same.tf" + if_exists = "overwrite" + contents = "" +}`, `generate "a" { + path = "../escape.tf" + if_exists = "overwrite" + disable = true + if_disabled = "remove" + contents = "" +}`} { + _, err := Resolve(t.Context(), nativeUnit(t, extra)) + require.Error(t, err) + } +} + +func TestRemoteModuleSourceRemainsUnfetched(t *testing.T) { + request := nativeUnit(t, "") + uri := "git::https://example.invalid/repository.git//module?ref=v1.2.3" + require.NoError(t, os.WriteFile(request.Path, []byte("terraform { source = \""+uri+"\" }\n"), 0o600)) + for _, phase := range []string{"discover", "backend", ""} { + request.Phase = phase + result, err := Resolve(t.Context(), request) + require.NoError(t, err) + require.Equal(t, uri, result.Source) + require.Empty(t, result.Folders) + require.NoDirExists(t, filepath.Join(filepath.Dir(request.Path), ".terragrunt-cache")) + } +} + +func TestPreventDestroyUsesCommandAndBooleanArguments(t *testing.T) { + for _, test := range []struct { + command string + args []string + blocked bool + }{ + {"plan", nil, false}, + {"apply", nil, false}, + {"destroy", nil, true}, + {"plan", []string{"-destroy"}, true}, + {"apply", []string{"--destroy=true"}, true}, + {"apply", []string{"-destroy", "-destroy=false"}, false}, + {"apply", []string{"-destroy=false", "-destroy=1"}, true}, + } { + t.Run(test.command+strings.Join(test.args, " "), func(t *testing.T) { + request := nativeUnit(t, "prevent_destroy = true") + request.Command, request.Arguments = test.command, test.args + _, err := Resolve(t.Context(), request) + if test.blocked { + require.ErrorContains(t, err, "protected by prevent_destroy") + } else { + require.NoError(t, err) + } + }) + } +} + +func TestPreventDestroyRejectsBeforeInputEffects(t *testing.T) { + request := nativeUnit(t, `prevent_destroy = true +inputs = { value = run_cmd("must-not-execute") }`) + request.Command = "destroy" + _, err := Resolve(t.Context(), request) + require.ErrorContains(t, err, "protected by prevent_destroy") + require.NotContains(t, err.Error(), "must-not-execute") +} + +func TestPreventDestroyGuardFailsClosedWhenConfigCannotBeParsed(t *testing.T) { + request := nativeUnit(t, "inputs = {") + request.Command = "destroy" + result, err := Resolve(t.Context(), request) + require.Error(t, err, "an unreadable unit must not proceed to a destroy") + require.Nil(t, result) + require.NotContains(t, err.Error(), "protected by prevent_destroy", "the failure is a parse error, not a protection verdict") +} diff --git a/plugins/terragrunt/internal/engine/policies.go b/plugins/terragrunt/internal/engine/policies.go index bc8478443de..ce9952c6886 100644 --- a/plugins/terragrunt/internal/engine/policies.go +++ b/plugins/terragrunt/internal/engine/policies.go @@ -68,3 +68,15 @@ func checkVersions(ctx context.Context, request model.Request, terraform, terrag } return check("terraform_version_constraint", terraform, result.Version) } + +// validateRemoteSource preserves the getter URI for Atmos's source provisioner. +// Registry sources need Terragrunt version selection and are not interchangeable +// with direct archive or Git sources. +func validateRemoteSource(source string) error { + for _, prefix := range []string{"git::https://", "git::http://", "git::ssh://", "git::file://", "https://", "http://"} { + if strings.HasPrefix(source, prefix) { + return nil + } + } + return fmt.Errorf("native adapter does not support this module source transport") +} diff --git a/plugins/terragrunt/internal/engine/resolve.go b/plugins/terragrunt/internal/engine/resolve.go index 265d4b5cfad..8af2a8d700f 100644 --- a/plugins/terragrunt/internal/engine/resolve.go +++ b/plugins/terragrunt/internal/engine/resolve.go @@ -131,7 +131,11 @@ func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (* return nil, fmt.Errorf("native output dependencies with non-default workspaces require a dependency backend adapter: %s", path) } if cfg.Terraform != nil && cfg.Terraform.Source != nil { - result.Folders = []string{localPath(filepath.Dir(path), *cfg.Terraform.Source)} + result.Source = *cfg.Terraform.Source + if plainLocalSource(result.Source) { + result.Source = localPath(filepath.Dir(path), result.Source) + result.Folders = []string{result.Source} + } } for _, dep := range cfg.TerragruntDependencies { if dep.Enabled != nil && !*dep.Enabled { @@ -173,6 +177,16 @@ func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (* } return result, nil } + if request.Phase != "backend" && atmosbridge.IsDestroyOperation(request.Command, request.Arguments) { + guard, err := config.PartialParseConfigFile(ctx, logger, atmosbridge.Discovery(environment, false), + pctx.WithDecodeList(config.TerragruntFlags), path, nil) + if err != nil { + return nil, err + } + if guard.PreventDestroy != nil && *guard.PreventDestroy { + return nil, fmt.Errorf("unit is protected by prevent_destroy") + } + } var cfg *config.TerragruntConfig if request.Phase == "backend" { cfg, err = config.PartialParseConfigFile(ctx, logger, environment, pctx.WithDecodeList(config.TerraformSource, config.RemoteStateBlock), path, nil) @@ -193,24 +207,29 @@ func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (* if cfg.TerraformBinary != "" { return nil, fmt.Errorf("native adaptation of terraform_binary is not implemented") } - if len(cfg.Terraform.BeforeHooks)+len(cfg.Terraform.AfterHooks)+len(cfg.Terraform.ErrorHooks)+len(cfg.Terraform.ExtraArgs) > 0 { - return nil, fmt.Errorf("native adaptation of hooks/extra_arguments is not implemented") + // Hooks and extra arguments are returned as data for the host to run; evaluation never + // executes them. Backend identity is read from a partial parse that does not decode them. + var execution *model.Execution + if request.Phase != "backend" { + if execution, err = executionFromConfig(cfg.Terraform); err != nil { + return nil, err + } } - if cfg.PreventDestroy != nil && *cfg.PreventDestroy { - return nil, fmt.Errorf("native adaptation of prevent_destroy is not implemented") + if cfg.PreventDestroy != nil && *cfg.PreventDestroy && atmosbridge.IsDestroyOperation(request.Command, request.Arguments) { + return nil, fmt.Errorf("unit is protected by prevent_destroy") } if cfg.Errors != nil || cfg.Exclude != nil || len(cfg.FeatureFlags) > 0 || cfg.IamRole != "" || cfg.IamWebIdentityToken != "" { return nil, fmt.Errorf("native adaptation of errors, exclude, feature flags, or IAM settings is not implemented") } - result := &model.Resolution{Path: path, Inputs: cfg.Inputs, Source: *cfg.Terraform.Source, Generated: map[string]string{}, Files: append(pctx.FilesRead.Paths(), path)} - if !plainLocalSource(result.Source) { - return nil, fmt.Errorf("native adapter only adapts plain local directory sources") - } - if !filepath.IsAbs(result.Source) { - result.Source = filepath.Join(filepath.Dir(path), result.Source) - } - if info, err := os.Stat(result.Source); err != nil || !info.IsDir() { - return nil, fmt.Errorf("native adapter source must be an existing local directory: %s", result.Source) + result := &model.Resolution{Execution: execution, Path: path, Inputs: cfg.Inputs, Source: *cfg.Terraform.Source, Generated: map[string]string{}, Files: append(pctx.FilesRead.Paths(), path)} + if plainLocalSource(result.Source) { + result.Source = localPath(filepath.Dir(path), result.Source) + if info, err := os.Stat(result.Source); err != nil || !info.IsDir() { + return nil, fmt.Errorf("native adapter source must be an existing local directory: %s", result.Source) + } + result.Folders = []string{result.Source} + } else if err := validateRemoteSource(result.Source); err != nil { + return nil, err } download := pctx.DownloadDir if cfg.DownloadDir != "" { @@ -223,36 +242,14 @@ func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (* if err != nil { return nil, err } - result.Folders = []string{result.Source} result.Files = append(result.Files, request.OriginFiles...) result.Folders = append(result.Folders, request.OriginFolders...) - if cfg.RemoteState != nil { - result.BackendType = cfg.RemoteState.BackendName - result.Backend = cfg.RemoteState.BackendConfig - if result.BackendType != "local" { - return nil, fmt.Errorf("native adapter only adapts the local backend") - } - // Local backend paths are relative to Terragrunt's working directory, - // including the separate non-default-workspace state directory. - for key, fallback := range map[string]string{"path": "terraform.tfstate", "workspace_dir": "terraform.tfstate.d"} { - value, _ := result.Backend[key].(string) - if value == "" { - value = fallback - } - result.Backend[key] = localPath(result.WorkingDir, value) - } + if err := resolveBackend(cfg, result); err != nil { + return nil, err } - for _, generated := range cfg.GenerateConfigs { - if generated.Disable { - continue - } - if filepath.Base(generated.Path) != generated.Path || !strings.HasSuffix(generated.Path, ".tf") { - return nil, fmt.Errorf("unsupported generated path: %s", generated.Path) - } - if generated.IfExistsStr != "overwrite" || !generated.DisableSignature { - return nil, fmt.Errorf("native adapter generation requires overwrite and disable_signature") - } - result.Generated[generated.Path] = generated.Contents + + if err := generatedFiles(ctx, logger, cfg, result); err != nil { + return nil, err } return result, nil } diff --git a/plugins/terragrunt/internal/engine/resolve_test.go b/plugins/terragrunt/internal/engine/resolve_test.go index 48af1d022cc..74893cb8afc 100644 --- a/plugins/terragrunt/internal/engine/resolve_test.go +++ b/plugins/terragrunt/internal/engine/resolve_test.go @@ -46,18 +46,8 @@ inputs = { large = 9007199254740993, nested = { nullable = null, enabled = false func TestNativeRejectsUnsupportedSemantics(t *testing.T) { for _, test := range []struct{ name, extra, message string }{ - {"remote backend", `remote_state { - backend = "s3" - config = { bucket = "unsupported" } -}`, "only adapts the local backend"}, - {"prevent destroy", `prevent_destroy = true`, "prevent_destroy"}, {"binary override", `terraform_binary = "another-tool"`, "terraform_binary"}, {"IAM role", `iam_role = "unsupported"`, "IAM settings"}, - {"generation policy", `generate "x" { - path = "x.tf" - if_exists = "overwrite_terragrunt" - contents = "" -}`, "generation requires"}, {"generation escape", `generate "x" { path = "../x.tf" if_exists = "overwrite" @@ -151,7 +141,7 @@ generate "disabled" { require.NoError(t, os.WriteFile(request.Path, []byte("inputs = {}"), 0o644)) _, err = Resolve(t.Context(), request) require.ErrorContains(t, err, "requires terraform.source") - for _, source := range []string{"git::https://example.invalid/repo", "./absent"} { + for _, source := range []string{"tfr://example.invalid/repo", "./absent"} { encoded, err := json.Marshal(source) require.NoError(t, err) require.NoError(t, os.WriteFile(request.Path, []byte("terraform { source = "+string(encoded)+" }"), 0o644)) diff --git a/plugins/terragrunt/internal/engine/source_watches.go b/plugins/terragrunt/internal/engine/source_watches.go new file mode 100644 index 00000000000..240559780ec --- /dev/null +++ b/plugins/terragrunt/internal/engine/source_watches.go @@ -0,0 +1,77 @@ +package engine + +import ( + "fmt" + "path/filepath" + "slices" + "strings" + + "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/hclsyntax" + "github.com/spf13/afero" + "github.com/zclconf/go-cty/cty" + + "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" +) + +// trackInputSources inspects syntax, not values. Partial discovery intentionally +// leaves inputs unevaluated; syntax watches include conditional and optional reads +// without reading state, decrypting secrets, or invoking user commands. +func trackInputSources(fs afero.Fs, root string, result *model.Resolution) error { + for _, path := range slices.Clone(result.Files) { + if !strings.HasSuffix(path, ".hcl") { + continue + } + contents, err := afero.ReadFile(fs, path) + if err != nil { + return fmt.Errorf("track configuration sources %s: %w", path, err) + } + file, diagnostics := hclsyntax.ParseConfig(contents, path, hcl.InitialPos) + if diagnostics.HasErrors() { + return fmt.Errorf("cannot track configuration sources in %s", path) + } + inputs := file.Body.(*hclsyntax.Body).Attributes["inputs"] + if inputs == nil { + continue + } + walk := hclsyntax.VisitAll(inputs.Expr, func(node hclsyntax.Node) hcl.Diagnostics { + if call, ok := node.(*hclsyntax.FunctionCallExpr); ok { + trackSourceCall(root, filepath.Dir(path), call, result) + } + return nil + }) + if walk.HasErrors() { + return fmt.Errorf("cannot track configuration sources in %s", path) + } + } + result.Files = slices.Compact(slices.Sorted(slices.Values(result.Files))) + result.Folders = slices.Compact(slices.Sorted(slices.Values(result.Folders))) + return nil +} + +func trackSourceCall(root, directory string, call *hclsyntax.FunctionCallExpr, result *model.Resolution) { + switch call.Name { + case "file", "fileexists", "filebase64", "filemd5", "filesha1", "filesha256", "filesha512", + "filebase64sha256", "filebase64sha512", "templatefile", "read_tfvars_file", "read_terragrunt_config", + "sops_decrypt_file", "mark_as_read", "fileset", "find_in_parent_folders": + default: + return + } + // Unknown paths can refer anywhere in the project. A root directory watch is + // deliberately conservative and catches new files as well as changed files. + if len(call.Args) == 0 || call.Name == "find_in_parent_folders" { + result.Folders = append(result.Folders, root) + return + } + value, diagnostics := call.Args[0].Value(nil) + if diagnostics.HasErrors() || !value.IsKnown() || value.IsNull() || value.Type() != cty.String { + result.Folders = append(result.Folders, root) + return + } + path := localPath(directory, value.AsString()) + if call.Name == "fileset" { + result.Folders = append(result.Folders, path) + } else { + result.Files = append(result.Files, path) + } +} diff --git a/plugins/terragrunt/internal/engine/source_watches_test.go b/plugins/terragrunt/internal/engine/source_watches_test.go new file mode 100644 index 00000000000..ea73bc51039 --- /dev/null +++ b/plugins/terragrunt/internal/engine/source_watches_test.go @@ -0,0 +1,71 @@ +package engine + +import ( + "path/filepath" + "testing" + + "github.com/spf13/afero" + "github.com/stretchr/testify/require" + + "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" +) + +func TestInputWatchesDoNotEvaluateInputs(t *testing.T) { + root := t.TempDir() + path := filepath.Join(root, "live", "app", "terragrunt.hcl") + fs := afero.NewMemMapFs() + require.NoError(t, fs.MkdirAll(filepath.Dir(path), 0o700)) + require.NoError(t, afero.WriteFile(fs, path, []byte(` +include "root" { path = find_in_parent_folders("root.hcl") } +inputs = { + optional = fileexists("optional.json") ? jsondecode(file("optional.json")) : {} + members = fileset("config", "*.yaml") + template = templatefile("config.tpl", {}) + secret = sops_decrypt_file("secret.enc.yaml") + command = run_cmd("must-not-run") + unrelated = upper("text") +} +`), 0o600)) + result := &model.Resolution{Files: []string{path, filepath.Join(root, "data.json")}} + require.NoError(t, trackInputSources(fs, root, result)) + for _, name := range []string{"optional.json", "config.tpl", "secret.enc.yaml"} { + require.Contains(t, result.Files, filepath.Join(filepath.Dir(path), name)) + } + require.Equal(t, []string{filepath.Join(filepath.Dir(path), "config")}, result.Folders) + // Missing optional files and empty glob directories are watched before creation. + exists, err := afero.Exists(fs, filepath.Join(filepath.Dir(path), "optional.json")) + require.NoError(t, err) + require.False(t, exists) +} + +func TestInputWatchesConservativelyTrackDynamicPaths(t *testing.T) { + for _, expression := range []string{`file(local.path)`, `file("${get_terragrunt_dir()}/config.json")`, `fileset(local.root, "*.yaml")`, `find_in_parent_folders("optional.hcl")`, `file(null)`, `file(123)`, `file()`} { + t.Run(expression, func(t *testing.T) { + root := t.TempDir() + path := filepath.Join(root, "terragrunt.hcl") + fs := afero.NewMemMapFs() + require.NoError(t, fs.MkdirAll(root, 0o700)) + require.NoError(t, afero.WriteFile(fs, path, []byte("inputs = { value = "+expression+" }"), 0o600)) + result := &model.Resolution{Files: []string{path}} + require.NoError(t, trackInputSources(fs, root, result)) + require.Equal(t, []string{root}, result.Folders) + }) + } +} + +func TestInputWatchesFailuresAndIncludedInputs(t *testing.T) { + root := t.TempDir() + fs := afero.NewMemMapFs() + path := filepath.Join(root, "shared.hcl") + result := &model.Resolution{Files: []string{path}} + require.Error(t, trackInputSources(fs, root, result)) + require.NoError(t, fs.MkdirAll(root, 0o700)) + require.NoError(t, afero.WriteFile(fs, path, []byte("inputs = {"), 0o600)) + require.ErrorContains(t, trackInputSources(fs, root, result), "cannot track") + require.NoError(t, afero.WriteFile(fs, path, []byte(`locals { unrelated = "constant" }`), 0o600)) + require.NoError(t, trackInputSources(fs, root, result)) + require.Empty(t, result.Folders) + require.NoError(t, afero.WriteFile(fs, path, []byte(`inputs = { value = read_terragrunt_config("shared-input.hcl") }`), 0o600)) + require.NoError(t, trackInputSources(fs, root, result)) + require.Contains(t, result.Files, filepath.Join(root, "shared-input.hcl")) +} diff --git a/plugins/terragrunt/internal/engine/toolchain_dependency_test.go b/plugins/terragrunt/internal/engine/toolchain_dependency_test.go new file mode 100644 index 00000000000..ad5eab4df39 --- /dev/null +++ b/plugins/terragrunt/internal/engine/toolchain_dependency_test.go @@ -0,0 +1,119 @@ +package engine + +import ( + "bytes" + "encoding/json" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" +) + +func readCallLog(t *testing.T, path string) []toolchainReport { + t.Helper() + data, err := os.ReadFile(path) + if os.IsNotExist(err) { + return nil + } + require.NoError(t, err) + var calls []toolchainReport + decoder := json.NewDecoder(bytes.NewReader(data)) + for decoder.More() { + var call toolchainReport + require.NoError(t, decoder.Decode(&call)) + calls = append(calls, call) + } + return calls +} + +// A Terragrunt dependency read runs the selected absolute Terraform/OpenTofu executable +// with the invocation environment: that executable is not on any PATH, the dependency's +// own run_cmd uses the toolchain PATH, and nothing from the ambient process returns. +func TestDependencyOutputReadsUseSelectedExecutableAndInvocationEnvironment(t *testing.T) { + layout := newToolchainLayout(t, `dependency "network" { + config_path = "../network" +} +inputs = { network = dependency.network.outputs.report } +`) + network := filepath.Join(layout.root, "network") + require.NoError(t, os.MkdirAll(network, 0o755)) + source, err := json.Marshal(filepath.ToSlash(layout.module)) + require.NoError(t, err) + require.NoError(t, os.WriteFile(filepath.Join(network, "terragrunt.hcl"), []byte("terraform { source = "+string(source)+` } +locals { identity = jsondecode(run_cmd("fixture-tool")).profile } +remote_state { + backend = "local" + config = { path = "${local.identity}.tfstate" } +} +`), 0o644)) + toolchain := installFixtureExecutable(t, filepath.Join(layout.root, "toolchain", "bin"), toolchainToolName) + t.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", "ambient-token-must-not-return") + var observed []toolchainReport + for _, profile := range []string{"production", "development"} { + log := filepath.Join(layout.root, profile+".jsonl") + path := filepath.Dir(toolchain) + string(os.PathListSeparator) + layout.empty + env := map[string]string{"PATH": path, "AWS_PROFILE": profile, toolchainChildEnv: "1", toolchainCallLog: log} + result, err := Resolve(t.Context(), layout.request(env)) + require.NoError(t, err) + + // The dependency output reached the unit's inputs from the selected executable. + output := mustReport(t, result.Inputs["network"]) + require.Equal(t, []string{"output", "-json"}, output.Arguments) + require.Equal(t, physicalPath(t, layout.tofu), physicalPath(t, output.Executable)) + + calls := readCallLog(t, log) + require.NotEmpty(t, calls) + tofuCalls, toolCalls := 0, 0 + for _, call := range calls { + require.Equal(t, profile, call.Profile, "identity must reach every child: %v", call.Arguments) + require.Equal(t, path, call.Path, "toolchain PATH must reach every child: %v", call.Arguments) + require.Empty(t, call.Token, "ambient credentials must not return") + if physicalPath(t, call.Executable) == physicalPath(t, layout.tofu) { + tofuCalls++ + continue + } + toolCalls++ + require.Equal(t, physicalPath(t, toolchain), physicalPath(t, call.Executable)) + require.Equal(t, physicalPath(t, network), physicalPath(t, call.Directory), "a dependency command runs in the dependency directory") + } + require.GreaterOrEqual(t, tofuCalls, 2, "init and output both use the selected executable") + require.Positive(t, toolCalls, "the dependency's own HCL command must find the toolchain tool") + observed = append(observed, output) + } + // Consecutive identities do not share cached dependency outputs. + require.Equal(t, "production", observed[0].Profile) + require.Equal(t, "development", observed[1].Profile) +} + +// Terragrunt hooks belong to command execution, never to HCL evaluation. Whether the +// evaluator rejects them or reports them for the host to run, evaluating a unit must +// not spawn the hook command, including a hook that names an explicit working directory. +func TestEvaluationNeverRunsHooks(t *testing.T) { + layout := newToolchainLayout(t, "") + source, err := json.Marshal(filepath.ToSlash(layout.module)) + require.NoError(t, err) + require.NoError(t, os.WriteFile(filepath.Join(layout.unit, "terragrunt.hcl"), []byte("terraform {\n source = "+string(source)+` + before_hook "before" { + commands = ["plan"] + execute = ["fixture-tool", "before"] + working_dir = "." + } + after_hook "after" { + commands = ["plan"] + execute = ["./fixture-tool", "after"] + } +} +inputs = { value = "constant" } +`), 0o644)) + toolchain := installFixtureExecutable(t, filepath.Join(layout.root, "toolchain", "bin"), toolchainToolName) + installFixtureExecutable(t, layout.unit, toolchainToolName) + log := filepath.Join(layout.root, "hooks.jsonl") + env := map[string]string{"PATH": filepath.Dir(toolchain), toolchainChildEnv: "1", toolchainCallLog: log} + _, err = Resolve(t.Context(), layout.request(env)) + if err != nil { + // The hooks must have been parsed; a syntax error would make this test vacuous. + require.ErrorContains(t, err, "hook") + } + require.Empty(t, readCallLog(t, log), "hook commands must not run while evaluating configuration") +} diff --git a/plugins/terragrunt/internal/engine/toolchain_helper_test.go b/plugins/terragrunt/internal/engine/toolchain_helper_test.go new file mode 100644 index 00000000000..92b0acaf9a3 --- /dev/null +++ b/plugins/terragrunt/internal/engine/toolchain_helper_test.go @@ -0,0 +1,106 @@ +package engine + +import ( + "encoding/json" + "os" + "path/filepath" + "runtime" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +// toolchainChildEnv gates the re-executed test binary into a fake toolchain executable. +// The child is a "tofu" when its file name starts with toolchainTofuPrefix and an +// ordinary HCL tool otherwise, so one environment can reach both kinds of child. +const ( + toolchainChildEnv = "ATMOS_TERRAGRUNT_TOOLCHAIN_CHILD" + toolchainCallLog = "ATMOS_TERRAGRUNT_TOOLCHAIN_CALL_LOG" + toolchainTofuName = "fixture-tofu" + toolchainToolName = "fixture-tool" +) + +// toolchainReport is what every fake executable observed about its own invocation. +type toolchainReport struct { + Executable string `json:"executable"` + Directory string `json:"directory"` + Arguments []string `json:"arguments"` + Path string `json:"path"` + Profile string `json:"profile"` + Custom string `json:"custom"` + Token string `json:"token"` + Ambient string `json:"ambient"` + Environ map[string]string `json:"environ,omitempty"` +} + +func currentToolchainReport() toolchainReport { + executable, _ := os.Executable() + directory, _ := os.Getwd() + return toolchainReport{ + Executable: executable, Directory: directory, Arguments: os.Args[1:], Path: os.Getenv("PATH"), + Profile: os.Getenv("AWS_PROFILE"), Custom: os.Getenv("CUSTOM_VALUE"), Token: os.Getenv("AWS_WEB_IDENTITY_TOKEN_FILE"), + Ambient: os.Getenv("ATMOS_TERRAGRUNT_TOOLCHAIN_AMBIENT"), + } +} + +// runToolchainChild implements the fake executables. It reports whether this process is one. +func runToolchainChild() bool { + if os.Getenv(toolchainChildEnv) != "1" { + return false + } + report := currentToolchainReport() + if path := os.Getenv(toolchainCallLog); path != "" { + if file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o600); err == nil { + _ = json.NewEncoder(file).Encode(report) + _ = file.Close() + } + } + if !strings.HasPrefix(filepath.Base(os.Args[0]), toolchainTofuName) { + _ = json.NewEncoder(os.Stdout).Encode(report) + return true + } + if len(os.Args) > 1 && os.Args[1] == "output" { + encoded, _ := json.Marshal(report) + _ = json.NewEncoder(os.Stdout).Encode(map[string]any{"report": map[string]any{"value": string(encoded), "type": "string", "sensitive": false}}) + } + if len(os.Args) > 1 && os.Args[1] == "version" { + _, _ = os.Stdout.WriteString(`{"terraform_version":"1.9.0"}`) + } + return true +} + +// installFixtureExecutable places a hard link (or copy) of the test binary at dir/name. +func installFixtureExecutable(t *testing.T, dir, name string) string { + t.Helper() + if runtime.GOOS == "windows" { + name += ".exe" + } + require.NoError(t, os.MkdirAll(dir, 0o755)) + target := filepath.Join(dir, name) + executable, err := os.Executable() + require.NoError(t, err) + if err := os.Link(executable, target); err == nil { + return target + } + content, err := os.ReadFile(executable) + require.NoError(t, err) + require.NoError(t, os.WriteFile(target, content, 0o755)) + return target +} + +// physicalPath removes platform temp-directory symlinks so equal directories compare equal. +func physicalPath(t *testing.T, path string) string { + t.Helper() + resolved, err := filepath.EvalSymlinks(path) + require.NoError(t, err) + return filepath.Clean(resolved) +} + +// executableSuffix is the file name suffix of an executable on this platform. +func executableSuffix() string { + if runtime.GOOS == "windows" { + return ".exe" + } + return "" +} diff --git a/plugins/terragrunt/internal/engine/toolchain_test.go b/plugins/terragrunt/internal/engine/toolchain_test.go new file mode 100644 index 00000000000..92b5924bdfc --- /dev/null +++ b/plugins/terragrunt/internal/engine/toolchain_test.go @@ -0,0 +1,219 @@ +package engine + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + "sync" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" +) + +// toolchainLayout is a unit whose HCL runs an executable that exists only in a +// toolchain directory. The ambient process PATH never contains that directory. +type toolchainLayout struct { + root, unit, module, empty string + tofu string +} + +func newToolchainLayout(t *testing.T, hcl string) toolchainLayout { + t.Helper() + root, err := filepath.EvalSymlinks(t.TempDir()) + require.NoError(t, err) + layout := toolchainLayout{root: root, unit: filepath.Join(root, "unit"), module: filepath.Join(root, "module"), empty: filepath.Join(root, "empty")} + for _, dir := range []string{layout.unit, layout.module, layout.empty} { + require.NoError(t, os.MkdirAll(dir, 0o755)) + } + // The selected Terraform/OpenTofu executable is always an absolute path outside every PATH. + layout.tofu = installFixtureExecutable(t, filepath.Join(root, "selected"), toolchainTofuName) + source, err := json.Marshal(filepath.ToSlash(layout.module)) + require.NoError(t, err) + require.NoError(t, os.WriteFile(filepath.Join(layout.unit, "terragrunt.hcl"), []byte("terraform { source = "+string(source)+" }\n"+hcl), 0o644)) + // The ambient PATH has neither the toolchain directory nor the tool. + t.Setenv("PATH", layout.empty) + return layout +} + +func (l *toolchainLayout) request(environment map[string]string) model.Request { + return model.Request{Path: filepath.Join(l.unit, "terragrunt.hcl"), BasePath: l.root, Command: "plan", Tofu: l.tofu, Environment: environment} +} + +func decodeReport(t *testing.T, value any) (toolchainReport, error) { + t.Helper() + text, ok := value.(string) + if !ok { + encoded, err := json.Marshal(value) + if err != nil { + return toolchainReport{}, err + } + text = string(encoded) + } + var report toolchainReport + return report, json.Unmarshal([]byte(text), &report) +} + +func mustReport(t *testing.T, value any) toolchainReport { + t.Helper() + report, err := decodeReport(t, value) + require.NoError(t, err) + return report +} + +// A dependencies.tools executable is found through the invocation PATH even though the +// plugin process PATH cannot find it, and the ambient PATH never substitutes for it. +func TestRunCmdFindsToolchainToolAbsentFromAmbientPath(t *testing.T) { + layout := newToolchainLayout(t, `locals { tool = jsondecode(run_cmd("fixture-tool", "argument")) } +inputs = { tool = local.tool } +`) + toolchain := filepath.Join(layout.root, "toolchain", "bin") + tool := installFixtureExecutable(t, toolchain, toolchainToolName) + + t.Run("invocation PATH carries the tool directory", func(t *testing.T) { + env := map[string]string{"PATH": toolchain + string(os.PathListSeparator) + layout.empty, toolchainChildEnv: "1", "AWS_PROFILE": "development"} + result, err := Resolve(t.Context(), layout.request(env)) + require.NoError(t, err) + report := mustReport(t, result.Inputs["tool"]) + require.Equal(t, physicalPath(t, tool), physicalPath(t, report.Executable)) + require.Equal(t, []string{"argument"}, report.Arguments) + require.Equal(t, env["PATH"], report.Path, "the child must receive the invocation PATH unchanged") + require.Equal(t, "development", report.Profile) + }) + + t.Run("ambient PATH never substitutes for the invocation PATH", func(t *testing.T) { + // Now the tool is on the ambient PATH but absent from the invocation PATH. + t.Setenv("PATH", toolchain) + env := map[string]string{"PATH": layout.empty, toolchainChildEnv: "1"} + _, err := Resolve(t.Context(), layout.request(env)) + require.Error(t, err) + require.ErrorContains(t, err, toolchainToolName) + }) + + t.Run("a missing invocation PATH does not fall back to the ambient PATH", func(t *testing.T) { + t.Setenv("PATH", toolchain) + _, err := Resolve(t.Context(), layout.request(map[string]string{toolchainChildEnv: "1"})) + require.Error(t, err) + require.ErrorContains(t, err, toolchainToolName) + }) +} + +// Two tool versions and two identities evaluated at once in one process never see each other. +func TestToolchainAndIdentityIsolationAcrossConsecutiveAndConcurrentEvaluations(t *testing.T) { + layout := newToolchainLayout(t, `inputs = { + tool = jsondecode(run_cmd("fixture-tool")) + profile = get_env("AWS_PROFILE", "unset") + custom = get_env("CUSTOM_VALUE", "unset") + ambient = get_env("ATMOS_TERRAGRUNT_TOOLCHAIN_AMBIENT", "unset") +} +`) + t.Setenv("ATMOS_TERRAGRUNT_TOOLCHAIN_AMBIENT", "ambient-must-not-return") + t.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", "ambient-token-must-not-return") + versions := map[string]string{} + for _, version := range []string{"1.0.0", "2.0.0"} { + versions[version] = installFixtureExecutable(t, filepath.Join(layout.root, "toolchain", version, "bin"), toolchainToolName) + } + type scenario struct{ version, profile, custom string } + scenarios := []scenario{{"1.0.0", "development", "from-stack-dev"}, {"2.0.0", "production", ""}} + check := func(s scenario) error { + dir := filepath.Dir(versions[s.version]) + env := map[string]string{"PATH": dir, "AWS_PROFILE": s.profile, "CUSTOM_VALUE": s.custom, toolchainChildEnv: "1"} + result, err := Resolve(t.Context(), layout.request(env)) + if err != nil { + return err + } + report, err := decodeReport(t, result.Inputs["tool"]) + if err != nil { + return err + } + resolved, err := filepath.EvalSymlinks(report.Executable) + if err != nil { + return err + } + want, err := filepath.EvalSymlinks(versions[s.version]) + if err != nil { + return err + } + switch { + case resolved != want: + return fmt.Errorf("tool %s resolved %s, want %s", s.version, report.Executable, versions[s.version]) + case report.Path != dir: + return fmt.Errorf("PATH leaked: %q", report.Path) + case report.Profile != s.profile || result.Inputs["profile"] != s.profile: + return fmt.Errorf("profile leaked: child %q hcl %v", report.Profile, result.Inputs["profile"]) + case report.Custom != s.custom || result.Inputs["custom"] != s.custom: + return fmt.Errorf("explicit empty or custom value was replaced: child %q hcl %v", report.Custom, result.Inputs["custom"]) + case report.Token != "" || report.Ambient != "": + return fmt.Errorf("ambient environment returned: %+v", report) + case result.Inputs["ambient"] != "unset": + return fmt.Errorf("ambient value visible to get_env: %v", result.Inputs["ambient"]) + } + return nil + } + for _, s := range []scenario{scenarios[0], scenarios[1], scenarios[1], scenarios[0]} { + require.NoError(t, check(s), "consecutive") + } + var workers sync.WaitGroup + failures := make(chan error, 32) + for i := range 32 { + workers.Add(1) + go func() { + defer workers.Done() + if err := check(scenarios[i%2]); err != nil { + failures <- err + } + }() + } + workers.Wait() + close(failures) + for err := range failures { + require.NoError(t, err, "concurrent") + } + require.Equal(t, "ambient-must-not-return", os.Getenv("ATMOS_TERRAGRUNT_TOOLCHAIN_AMBIENT")) +} + +// Explicit relative commands and working directories resolve from the unit, not the plugin process. +func TestRunCmdRelativePathsAndWorkingDirectory(t *testing.T) { + // Explicit paths carry the platform's executable suffix; bare names are resolved through PATH. + suffix := executableSuffix() + layout := newToolchainLayout(t, `locals { + here = jsondecode(run_cmd("./tools/fixture-tool`+suffix+`")) + sibling = jsondecode(run_cmd("../shared/fixture-tool`+suffix+`")) + absolute = jsondecode(run_cmd("${get_terragrunt_dir()}/tools/fixture-tool`+suffix+`")) +} +inputs = { here = local.here, sibling = local.sibling, absolute = local.absolute, dir = get_terragrunt_dir() } +`) + here := installFixtureExecutable(t, filepath.Join(layout.unit, "tools"), toolchainToolName) + sibling := installFixtureExecutable(t, filepath.Join(layout.root, "shared"), toolchainToolName) + // The process working directory differs from the unit; a relative command must not use it. + t.Chdir(layout.empty) + result, err := Resolve(t.Context(), layout.request(map[string]string{"PATH": layout.empty, toolchainChildEnv: "1"})) + require.NoError(t, err) + for key, want := range map[string]string{"here": here, "sibling": sibling, "absolute": here} { + report := mustReport(t, result.Inputs[key]) + require.Equal(t, physicalPath(t, want), physicalPath(t, report.Executable), key) + require.Equal(t, physicalPath(t, layout.unit), physicalPath(t, report.Directory), "%s must run in the unit directory", key) + } + require.Equal(t, filepath.ToSlash(physicalPath(t, layout.unit)), result.Inputs["dir"]) +} + +// A parent configuration included by the unit evaluates commands in the unit directory. +func TestIncludedConfigurationRunsCommandsFromUnitDirectory(t *testing.T) { + layout := newToolchainLayout(t, `include "root" { + path = find_in_parent_folders("root.hcl") + expose = true +} +inputs = { tool = include.root.locals.tool } +`) + require.NoError(t, os.WriteFile(filepath.Join(layout.root, "root.hcl"), []byte(`locals { tool = jsondecode(run_cmd("fixture-tool")) } +`), 0o644)) + toolchain := filepath.Join(layout.root, "toolchain") + tool := installFixtureExecutable(t, toolchain, toolchainToolName) + result, err := Resolve(t.Context(), layout.request(map[string]string{"PATH": toolchain, toolchainChildEnv: "1"})) + require.NoError(t, err) + report := mustReport(t, result.Inputs["tool"]) + require.Equal(t, physicalPath(t, tool), physicalPath(t, report.Executable)) + require.Equal(t, physicalPath(t, layout.unit), physicalPath(t, report.Directory)) +} diff --git a/plugins/terragrunt/internal/model/model.go b/plugins/terragrunt/internal/model/model.go index adae75d9a9d..5f50d34f5aa 100644 --- a/plugins/terragrunt/internal/model/model.go +++ b/plugins/terragrunt/internal/model/model.go @@ -20,6 +20,7 @@ type Request struct { type Resolution struct { Inventory []DiscoveredUnit `json:"inventory,omitempty"` + Execution *Execution `json:"execution,omitempty"` Path string `json:"path,omitempty"` OutputDependencies []string `json:"output_dependencies,omitempty"` Dependencies []string `json:"dependencies,omitempty"` @@ -32,9 +33,11 @@ type Resolution struct { StackConfigs map[string]string `json:"stack_configs,omitempty"` Inputs map[string]any `json:"inputs"` Source string `json:"source"` + BackendGenerated bool `json:"backend_generated,omitempty"` BackendType string `json:"backend_type"` Backend map[string]any `json:"backend"` Generated map[string]string `json:"generated"` + GeneratedPolicies map[string]string `json:"generated_policies,omitempty"` Units map[string]string `json:"units,omitempty"` Stacks map[string]string `json:"stacks,omitempty"` } @@ -46,3 +49,35 @@ type DiscoveredUnit struct { GenerationRoot string `json:"generation_root,omitempty"` Config Resolution `json:"config"` } + +// Execution preserves evaluated HCL declaration order for host command dispatch. +// It is data only: the host runs hooks and applies extra arguments to its own commands. +type Execution struct { + ExtraArguments []ExtraArguments `json:"extra_arguments,omitempty"` + BeforeHooks []Hook `json:"before_hooks,omitempty"` + AfterHooks []Hook `json:"after_hooks,omitempty"` + ErrorHooks []Hook `json:"error_hooks,omitempty"` +} + +// ExtraArguments adds arguments, variable files, and environment to the listed commands. +type ExtraArguments struct { + Commands []string `json:"commands"` + Arguments []string `json:"arguments,omitempty"` + RequiredVarFiles []string `json:"required_var_files,omitempty"` + OptionalVarFiles []string `json:"optional_var_files,omitempty"` + Environment map[string]string `json:"environment,omitempty"` +} + +// Hook is one command the host runs around a Terraform command. +type Hook struct { + Name string `json:"name"` + // Commands lists the Terraform commands that trigger the hook. + Commands []string `json:"commands"` + // Execute is an argv, resolved on the invocation's PATH by the host. + Execute []string `json:"execute"` + // WorkingDirectory is empty for the provisioned module directory. + WorkingDirectory string `json:"working_directory,omitempty"` + RunOnError bool `json:"run_on_error,omitempty"` + SuppressStdout bool `json:"suppress_stdout,omitempty"` + OnErrors []string `json:"on_errors,omitempty"` +} diff --git a/plugins/terragrunt/internal/provider/discovery.go b/plugins/terragrunt/internal/provider/discovery.go index 676e087e344..e0c35f7154a 100644 --- a/plugins/terragrunt/internal/provider/discovery.go +++ b/plugins/terragrunt/internal/provider/discovery.go @@ -2,7 +2,10 @@ package provider import ( "context" + "os" + "path/filepath" "slices" + "strings" "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" pluginv1 "github.com/cloudposse/atmos/sdk/proto/atmos/plugin/v1" @@ -17,10 +20,11 @@ func (p *Provider) Discover(ctx context.Context, request *pluginv1.DiscoverCompo if err := settings(request.GetProviderConfiguration()); err != nil { return nil, rpcError(err) } - if len(request.IncludePaths) == 0 || len(request.Imports) != 0 { - return nil, rpcError(ErrUnsupported) + paths, err := importPaths(request) + if err != nil { + return nil, rpcError(err) } - req.Phase, req.IncludedPaths, req.ExcludedPaths = "inventory", request.IncludePaths, request.ExcludePaths + req.Phase, req.IncludedPaths, req.ExcludedPaths = "inventory", paths, request.ExcludePaths result, err := p.evaluate(ctx, req) if err != nil { return nil, rpcError(err) @@ -45,12 +49,20 @@ func discoveredUnit(req model.Request, unit model.DiscoveredUnit, addresses map[ if err != nil { return nil, err } - if len(unit.Config.Folders) == 0 { - return nil, ErrUnsupported + module := unit.Config.Source + if len(unit.Config.Folders) > 0 && (module == "" || filepath.IsAbs(module)) { + path := module + if path == "" { + path = unit.Config.Folders[0] + } + local, err := relative(req.BasePath, path) + if err != nil { + return nil, err + } + module = "./" + local } - module, err := relative(req.BasePath, unit.Config.Folders[0]) - if err != nil { - return nil, err + if module == "" { + return nil, ErrUnsupported } generation := "" if unit.GenerationRoot != "" { @@ -64,11 +76,14 @@ func discoveredUnit(req model.Request, unit model.DiscoveredUnit, addresses map[ workspace = "default" } config, err := encode(map[string]any{ - "source": "./" + module, "vars": map[string]any{}, + "source": module, "vars": map[string]any{}, "provision": map[string]any{"workdir": map[string]any{"enabled": true}}, "backend_type": "local", "backend": map[string]any{"local": map[string]any{}}, "metadata": map[string]any{"terraform_workspace": workspace}, - "settings": map[string]any{"terragrunt": map[string]any{"generation_root": generation}}, + "settings": map[string]any{ + "terragrunt": map[string]any{"generation_root": generation}, + "configuration": map[string]any{"watch_excludes": []string{"**/.terragrunt-cache/**", "**/.terragrunt-stack/**", "**/.terraform/**", "**/.atmos/**"}}, + }, }) if err != nil { return nil, err @@ -91,3 +106,29 @@ func discoveredUnit(req model.Request, unit model.DiscoveredUnit, addresses map[ } return instance, nil } + +// importPaths adds explicit files or directory roots to the same scoped inventory. +// No HCL evaluation occurs while determining which roots were requested. +func importPaths(request *pluginv1.DiscoverComponentsRequest) ([]string, error) { + paths := slices.Clone(request.IncludePaths) + for _, uri := range request.Imports { + path, found := strings.CutPrefix(uri, "terragrunt://") + if !found || !filepath.IsLocal(path) || strings.ContainsAny(path, "\\?#*[{") { + return nil, ErrUnsupported + } + info, err := os.Stat(filepath.Join(request.RootDirectory, filepath.FromSlash(path))) + if err != nil { + return nil, err + } + if info.IsDir() { + path = strings.TrimSuffix(filepath.ToSlash(path), "/") + "/**" + } else if info.Name() != "terragrunt.hcl" && info.Name() != "terragrunt.stack.hcl" { + return nil, ErrUnsupported + } + paths = append(paths, path) + } + if len(paths) == 0 { + return nil, ErrUnsupported + } + return paths, nil +} diff --git a/plugins/terragrunt/internal/provider/fixtures_test.go b/plugins/terragrunt/internal/provider/fixtures_test.go index d13c5fa781c..24f29e7abb4 100644 --- a/plugins/terragrunt/internal/provider/fixtures_test.go +++ b/plugins/terragrunt/internal/provider/fixtures_test.go @@ -83,6 +83,9 @@ func TestExplicitStackStateAndPreparation(t *testing.T) { require.NoError(t, decode(prepared.State.Backend, &after)) require.Equal(t, before, after) require.Equal(t, filepath.Join(root, "state/dev/network.tfstate"), after["path"]) - require.Empty(t, prepared.Files) + require.True(t, prepared.BackendGenerated) + require.Len(t, prepared.Files, 1) + require.Equal(t, "backend.tf", prepared.Files[0].Path) + require.Contains(t, string(prepared.Files[0].Content), `backend "local"`) require.NotNil(t, prepared.Inputs) } diff --git a/plugins/terragrunt/internal/provider/imports_test.go b/plugins/terragrunt/internal/provider/imports_test.go new file mode 100644 index 00000000000..59f73ec7815 --- /dev/null +++ b/plugins/terragrunt/internal/provider/imports_test.go @@ -0,0 +1,62 @@ +package provider + +import ( + "context" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" + pluginv1 "github.com/cloudposse/atmos/sdk/proto/atmos/plugin/v1" +) + +func TestImportsSelectExplicitFilesAndDirectories(t *testing.T) { + root, err := filepath.EvalSymlinks(t.TempDir()) + require.NoError(t, err) + require.NoError(t, os.MkdirAll(filepath.Join(root, "live"), 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(root, "live", "terragrunt.stack.hcl"), nil, 0o600)) + settings, err := encode(map[string]any{"enabled": true}) + require.NoError(t, err) + request := &pluginv1.DiscoverComponentsRequest{RootDirectory: root, Provider: "terragrunt", ProviderConfiguration: settings, Operation: &pluginv1.ComponentOperation{Mode: "native"}, Imports: []string{"terragrunt://live", "terragrunt://live/terragrunt.stack.hcl"}} + paths, err := importPaths(request) + require.NoError(t, err) + require.Equal(t, []string{"live/**", "live/terragrunt.stack.hcl"}, paths) + p := New(func(_ context.Context, req model.Request) (*model.Resolution, error) { + require.Equal(t, paths, req.IncludedPaths) + return &model.Resolution{}, nil + }) + _, err = p.Discover(t.Context(), request) + require.NoError(t, err) + for _, uri := range []string{"wrong://live", "terragrunt://../outside", "terragrunt://live?ref=main", "terragrunt://absent", "terragrunt://live/*", `terragrunt://live\child`} { + request.Imports = []string{uri} + _, err := importPaths(request) + require.Error(t, err) + _, err = p.Discover(t.Context(), request) + require.Error(t, err) + } + require.NoError(t, os.WriteFile(filepath.Join(root, "live", "other.txt"), nil, 0o600)) + request.Imports = []string{"terragrunt://live/other.txt"} + _, err = importPaths(request) + require.Error(t, err) + request.Imports = nil + _, err = importPaths(request) + require.Error(t, err) +} + +func TestRemoteModuleSourceIsNotAFileWatch(t *testing.T) { + root, err := filepath.EvalSymlinks(t.TempDir()) + require.NoError(t, err) + path := filepath.Join(root, "live", "app", "terragrunt.hcl") + remote := "git::https://example.com/modules.git//app?ref=v1.0.0" + unit := model.DiscoveredUnit{Config: model.Resolution{Path: path, Source: remote, Files: []string{path}}} + addresses := map[string]*pluginv1.ComponentAddress{path: {Stack: "live", Component: "app", ComponentType: "terraform"}} + instance, err := discoveredUnit(model.Request{BasePath: root}, unit, addresses) + require.NoError(t, err) + var configuration map[string]any + require.NoError(t, decode(instance.Configuration, &configuration)) + require.Equal(t, remote, configuration["source"]) + require.Len(t, instance.Sources, 1) + require.Equal(t, "live/app/terragrunt.hcl", instance.Sources[0].Path) +} diff --git a/plugins/terragrunt/internal/provider/preparation.go b/plugins/terragrunt/internal/provider/preparation.go index 5bdef4b4e92..c616aa4d097 100644 --- a/plugins/terragrunt/internal/provider/preparation.go +++ b/plugins/terragrunt/internal/provider/preparation.go @@ -79,9 +79,13 @@ func (p *Provider) Prepare(ctx context.Context, request *pluginv1.ComponentReque if err != nil { return nil, rpcError(err) } - response := &pluginv1.PrepareComponentResponse{Engine: engineName(req.Tofu), ModuleSource: result.Source, WorkingDirectory: result.WorkingDir, Inputs: inputs, State: state, Sources: watched} + response := &pluginv1.PrepareComponentResponse{Engine: engineName(req.Tofu), ModuleSource: result.Source, WorkingDirectory: result.WorkingDir, Inputs: inputs, State: state, Sources: watched, BackendGenerated: result.BackendGenerated, Execution: executionResponse(result.Execution)} for _, name := range slices.Sorted(maps.Keys(result.Generated)) { - response.Files = append(response.Files, &pluginv1.ComponentGeneratedFile{Path: name, Content: []byte(result.Generated[name]), IfExists: "overwrite"}) + policy := result.GeneratedPolicies[name] + if policy == "" { + policy = "overwrite" + } + response.Files = append(response.Files, &pluginv1.ComponentGeneratedFile{Path: name, Content: []byte(result.Generated[name]), IfExists: policy}) } return response, nil } @@ -127,3 +131,44 @@ func (p *Provider) Resolve(ctx context.Context, request *pluginv1.ComponentReque } return &pluginv1.ResolveComponentStateResponse{State: state, Sources: watched}, nil } + +// executionResponse hands evaluated hooks and extra arguments to the host unchanged and in +// declaration order. The plugin never runs them. +func executionResponse(execution *model.Execution) *pluginv1.ComponentExecutionConfiguration { + if execution == nil { + return nil + } + result := &pluginv1.ComponentExecutionConfiguration{ + BeforeHooks: hooksResponse(execution.BeforeHooks), + AfterHooks: hooksResponse(execution.AfterHooks), + ErrorHooks: hooksResponse(execution.ErrorHooks), + } + for i := range execution.ExtraArguments { + extra := &execution.ExtraArguments[i] + result.ExtraArguments = append(result.ExtraArguments, &pluginv1.ComponentExtraArguments{ + Commands: slices.Clone(extra.Commands), + Arguments: slices.Clone(extra.Arguments), + RequiredVarFiles: slices.Clone(extra.RequiredVarFiles), + OptionalVarFiles: slices.Clone(extra.OptionalVarFiles), + Environment: maps.Clone(extra.Environment), + }) + } + return result +} + +func hooksResponse(hooks []model.Hook) []*pluginv1.ComponentHook { + var result []*pluginv1.ComponentHook + for i := range hooks { + hook := &hooks[i] + result = append(result, &pluginv1.ComponentHook{ + Name: hook.Name, + Commands: slices.Clone(hook.Commands), + Execute: slices.Clone(hook.Execute), + WorkingDirectory: hook.WorkingDirectory, + RunOnError: hook.RunOnError, + SuppressStdout: hook.SuppressStdout, + OnErrors: slices.Clone(hook.OnErrors), + }) + } + return result +} diff --git a/plugins/terragrunt/internal/provider/provider.go b/plugins/terragrunt/internal/provider/provider.go index 4debfa3bbaf..ea6c58bdc28 100644 --- a/plugins/terragrunt/internal/provider/provider.go +++ b/plugins/terragrunt/internal/provider/provider.go @@ -7,6 +7,7 @@ import ( "errors" "maps" "path/filepath" + "slices" "strings" ctyjson "github.com/zclconf/go-cty/cty/json" @@ -105,7 +106,7 @@ func operation(root, name string, op *pluginv1.ComponentOperation, invocation *p if binary == "" { binary = "terraform" } - return model.Request{BasePath: root, Mode: "native", Command: op.Command, Tofu: binary, Environment: env}, nil + return model.Request{BasePath: root, Mode: "native", Command: op.Command, Arguments: slices.Clone(op.TerraformArguments), Tofu: binary, Environment: env}, nil } func engineName(binary string) string { @@ -161,6 +162,10 @@ func rpcError(err error) error { if errors.Is(err, ErrUnsupported) { return status.Error(codes.Unimplemented, ErrUnsupported.Error()) } + // Unsupported hook semantics name only hook identifiers and commands, so the reason is safe to show. + if errors.Is(err, engine.ErrUnsupportedExecution) { + return status.Error(codes.Unimplemented, err.Error()) + } return status.Error(codes.InvalidArgument, "Terragrunt evaluation failed") } diff --git a/plugins/terragrunt/internal/provider/provider_test.go b/plugins/terragrunt/internal/provider/provider_test.go index a2d8f16f7f1..cc004f36c79 100644 --- a/plugins/terragrunt/internal/provider/provider_test.go +++ b/plugins/terragrunt/internal/provider/provider_test.go @@ -3,6 +3,7 @@ package provider import ( "context" "errors" + "fmt" "path/filepath" "testing" @@ -10,6 +11,7 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "github.com/cloudposse/atmos/plugins/terragrunt/internal/engine" "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" pluginv1 "github.com/cloudposse/atmos/sdk/proto/atmos/plugin/v1" ) @@ -103,3 +105,65 @@ func TestEvaluatorErrorsDoNotExposePayloads(t *testing.T) { require.NotContains(t, err.Error(), "secret-evaluated-value") } } + +func TestPrepareReturnsExecutionInDeclarationOrder(t *testing.T) { + request := componentRequest(t) + execution := &model.Execution{ + ExtraArguments: []model.ExtraArguments{ + {Commands: []string{"plan"}, Arguments: []string{"-lock=false"}}, + {Commands: []string{"apply", "plan"}, RequiredVarFiles: []string{"a.tfvars"}, OptionalVarFiles: []string{"b.tfvars"}, Environment: map[string]string{"TF_LOG": "INFO"}}, + }, + BeforeHooks: []model.Hook{ + {Name: "second", Commands: []string{"plan"}, Execute: []string{"tool", "2"}}, + {Name: "first", Commands: []string{"plan"}, Execute: []string{"tool", "1"}, WorkingDirectory: "sub", SuppressStdout: true}, + }, + AfterHooks: []model.Hook{{Name: "after", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, + ErrorHooks: []model.Hook{{Name: "oops", Commands: []string{"apply"}, Execute: []string{"tool"}, OnErrors: []string{"x", "y"}}}, + } + p := New(func(_ context.Context, req model.Request) (*model.Resolution, error) { + return &model.Resolution{Execution: execution, Source: filepath.Join(request.RootDirectory, "module"), WorkingDir: request.RootDirectory, BackendType: "local", Backend: map[string]any{}}, nil + }) + result, err := p.Prepare(t.Context(), request) + require.NoError(t, err) + got := result.GetExecution() + require.NotNil(t, got) + + require.Len(t, got.ExtraArguments, 2) + require.Equal(t, []string{"-lock=false"}, got.ExtraArguments[0].Arguments) + require.Equal(t, []string{"apply", "plan"}, got.ExtraArguments[1].Commands) + require.Equal(t, []string{"a.tfvars"}, got.ExtraArguments[1].RequiredVarFiles) + require.Equal(t, []string{"b.tfvars"}, got.ExtraArguments[1].OptionalVarFiles) + require.Equal(t, map[string]string{"TF_LOG": "INFO"}, got.ExtraArguments[1].Environment) + + require.Len(t, got.BeforeHooks, 2) + require.Equal(t, "second", got.BeforeHooks[0].Name) + require.Equal(t, "first", got.BeforeHooks[1].Name) + require.Equal(t, "sub", got.BeforeHooks[1].WorkingDirectory) + require.True(t, got.BeforeHooks[1].SuppressStdout) + require.Equal(t, []string{"tool", "1"}, got.BeforeHooks[1].Execute) + require.Len(t, got.AfterHooks, 1) + require.True(t, got.AfterHooks[0].RunOnError) + require.Len(t, got.ErrorHooks, 1) + require.Equal(t, []string{"x", "y"}, got.ErrorHooks[0].OnErrors) +} + +func TestPrepareOmitsExecutionWithoutHooks(t *testing.T) { + request := componentRequest(t) + p := New(func(context.Context, model.Request) (*model.Resolution, error) { + return &model.Resolution{Source: request.RootDirectory, WorkingDir: request.RootDirectory, BackendType: "local", Backend: map[string]any{}}, nil + }) + result, err := p.Prepare(t.Context(), request) + require.NoError(t, err) + require.Nil(t, result.GetExecution()) +} + +// An unsupported hook must reach the user with its specific reason, unlike generic evaluator +// failures whose payloads can hold secrets. +func TestUnsupportedExecutionIsReportedSpecifically(t *testing.T) { + cause := fmt.Errorf("%w: before_hook %q lists the %q lifecycle command", engine.ErrUnsupportedExecution, "setup", "init-from-module") + p := New(func(context.Context, model.Request) (*model.Resolution, error) { return nil, cause }) + _, err := p.Prepare(t.Context(), componentRequest(t)) + require.Equal(t, codes.Unimplemented, status.Code(err)) + require.Contains(t, err.Error(), "init-from-module") + require.Contains(t, err.Error(), "setup") +} diff --git a/plugins/terragrunt/internal/provider/toolchain_test.go b/plugins/terragrunt/internal/provider/toolchain_test.go new file mode 100644 index 00000000000..a5e143a6ea0 --- /dev/null +++ b/plugins/terragrunt/internal/provider/toolchain_test.go @@ -0,0 +1,119 @@ +package provider + +import ( + "encoding/json" + "os" + "path/filepath" + "runtime" + "testing" + + "github.com/stretchr/testify/require" + + pluginv1 "github.com/cloudposse/atmos/sdk/proto/atmos/plugin/v1" +) + +const providerToolchainChildEnv = "ATMOS_TERRAGRUNT_PROVIDER_TOOLCHAIN_CHILD" + +// TestMain turns this test binary into the fake Terraform/OpenTofu and HCL tool used below. +func TestMain(m *testing.M) { + if os.Getenv(providerToolchainChildEnv) == "1" { + if len(os.Args) > 1 && os.Args[1] == "version" { + _, _ = os.Stdout.WriteString(`{"terraform_version":"1.9.0"}`) + os.Exit(0) + } + executable, _ := os.Executable() + directory, _ := os.Getwd() + _ = json.NewEncoder(os.Stdout).Encode(map[string]string{"executable": executable, "directory": directory, "path": os.Getenv("PATH"), "profile": os.Getenv("AWS_PROFILE"), "token": os.Getenv("AWS_WEB_IDENTITY_TOKEN_FILE"), "custom": os.Getenv("CUSTOM_VALUE")}) + os.Exit(0) + } + os.Exit(m.Run()) +} + +func installProviderExecutable(t *testing.T, dir, name string) string { + t.Helper() + if runtime.GOOS == "windows" { + name += ".exe" + } + require.NoError(t, os.MkdirAll(dir, 0o755)) + target := filepath.Join(dir, name) + executable, err := os.Executable() + require.NoError(t, err) + if err := os.Link(executable, target); err == nil { + return target + } + content, err := os.ReadFile(executable) + require.NoError(t, err) + require.NoError(t, os.WriteFile(target, content, 0o755)) + return target +} + +// The host sends the toolchain-resolved executable and PATH only in Invocation.Environment. +// This drives that exact wire shape through the real provider and evaluator. +func TestPrepareHonorsToolchainEnvironmentFromTheInvocation(t *testing.T) { + root, err := filepath.EvalSymlinks(t.TempDir()) + require.NoError(t, err) + ambient := filepath.Join(root, "ambient") + require.NoError(t, os.MkdirAll(ambient, 0o755)) + t.Setenv("PATH", ambient) + t.Setenv("AWS_WEB_IDENTITY_TOKEN_FILE", "ambient-token-must-not-return") + module := filepath.Join(root, "module") + require.NoError(t, os.MkdirAll(module, 0o755)) + unit := filepath.Join(root, "live", "app") + require.NoError(t, os.MkdirAll(unit, 0o755)) + source, err := json.Marshal(filepath.ToSlash(module)) + require.NoError(t, err) + require.NoError(t, os.WriteFile(filepath.Join(unit, "terragrunt.hcl"), []byte("terraform { source = "+string(source)+` } +terraform_version_constraint = ">= 1.8.0" +inputs = { + tool = jsondecode(run_cmd("fixture-tool")) + custom = get_env("CUSTOM_VALUE", "unset") +} +`), 0o644)) + selected := installProviderExecutable(t, filepath.Join(root, "selected"), "tofu") + toolDir := filepath.Join(root, "toolchain", "bin") + tool := installProviderExecutable(t, toolDir, "fixture-tool") + + configuration, err := encode(map[string]any{}) + require.NoError(t, err) + settings, err := encode(map[string]any{"enabled": true, "mode": "native", "discovery": map[string]any{"enabled": true}}) + require.NoError(t, err) + prepare := func(profile string, environment map[string]string) (map[string]any, *pluginv1.PrepareComponentResponse, error) { + environment["ATMOS_TERRAFORM_BINARY"] = selected + environment[providerToolchainChildEnv] = "1" + environment["AWS_PROFILE"] = profile + request := &pluginv1.ComponentRequest{ + Provider: "terragrunt", RootDirectory: root, Source: "live/app/terragrunt.hcl", Configuration: configuration, ProviderConfiguration: settings, + Operation: &pluginv1.ComponentOperation{Mode: "native", Command: "plan"}, + Effects: &pluginv1.ComponentEffects{Commands: true, Network: true, Secrets: true, Writes: true, StateReads: true}, + Invocation: &pluginv1.Invocation{Environment: environment}, + } + response, err := New(nil).Prepare(t.Context(), request) + if err != nil { + return nil, nil, err + } + var inputs map[string]any + require.NoError(t, decode(response.Inputs, &inputs)) + return inputs, response, nil + } + + for _, profile := range []string{"development", "production"} { + inputs, response, err := prepare(profile, map[string]string{"PATH": toolDir, "CUSTOM_VALUE": profile}) + require.NoError(t, err, profile) + require.Equal(t, "opentofu", response.Engine, "the engine follows the selected executable") + report := map[string]string{} + for key, value := range inputs["tool"].(map[string]any) { + report[key], _ = value.(string) + } + require.Equal(t, tool, report["executable"]) + require.Equal(t, toolDir, report["path"]) + require.Equal(t, profile, report["profile"]) + require.Equal(t, "", report["token"], "ambient credentials must not cross the plugin boundary") + require.Equal(t, profile, inputs["custom"]) + require.Equal(t, unit, report["directory"], "commands run from the unit directory") + } + + // Dropping PATH at the boundary must fail loudly instead of finding the ambient tool. + t.Setenv("PATH", toolDir) + _, _, err = prepare("development", map[string]string{"PATH": ambient}) + require.Error(t, err, "the evaluator error text is intentionally generic") +} diff --git a/plugins/terragrunt/plugin.tests.yaml b/plugins/terragrunt/plugin.tests.yaml index e8035a8606b..ee00aa57c8e 100644 --- a/plugins/terragrunt/plugin.tests.yaml +++ b/plugins/terragrunt/plugin.tests.yaml @@ -12,7 +12,43 @@ steps: script: | affected = module.load("scripts/affected.star") affected.main() + - name: installed-release + type: script + interpreter: starlark + script: | + release = module.load("scripts/release.star") + release.main() integration: - name: native-lifecycle type: script source: scripts/native.star + - name: hooks-and-extra-arguments + type: script + interpreter: starlark + script: | + lifecycle = module.load("scripts/lifecycle_extended.star") + lifecycle.hooks() + - name: saved-plan + type: script + interpreter: starlark + script: | + lifecycle = module.load("scripts/lifecycle_extended.star") + lifecycle.saved_plan() + - name: state-and-output-integration + type: script + interpreter: starlark + script: | + lifecycle = module.load("scripts/lifecycle_extended.star") + lifecycle.state_and_outputs() + - name: git-source-refresh + type: script + interpreter: starlark + script: | + lifecycle = module.load("scripts/lifecycle_extended.star") + lifecycle.git_source_refresh() + - name: local-source-refresh + type: script + interpreter: starlark + script: | + lifecycle = module.load("scripts/lifecycle_extended.star") + lifecycle.local_source_refresh() diff --git a/plugins/terragrunt/scripts/affected.star b/plugins/terragrunt/scripts/affected.star index f19d8c6f0f8..5bf56ec0a09 100644 --- a/plugins/terragrunt/scripts/affected.star +++ b/plugins/terragrunt/scripts/affected.star @@ -162,6 +162,60 @@ def test_project_root(root): expect_components(changed.rows, ["network"], "project root\n" + changed.stderr) +def test_input_watches(root): + # Inputs stay unevaluated, but optional files and glob membership affect the unit. + seed_units(root) + fixtures.write(root, "live/prod/app/terragrunt.hcl", unit("network") + ''' +inputs = { + value = file("input.json") + members = fileset("config", "*.yaml") + optional = fileexists("optional.txt") ? file("optional.txt") : "absent" +} +''') + base = fixtures.commit(root, "declare unevaluated file inputs") + fixtures.write(root, "live/prod/app/optional.txt", "new input") + fixtures.commit(root, "create optional file") + expect_components(affected(root, base), ["app"]) + base = fixtures.git(root, "rev-parse", "HEAD") + fixtures.write(root, "live/prod/app/config/added.yaml", "key: value") + fixtures.commit(root, "add glob member") + expect_components(affected(root, base), ["app"]) + base = fixtures.git(root, "rev-parse", "HEAD") + fixtures.git(root, "rm", "live/prod/app/config/added.yaml") + fixtures.commit(root, "delete glob member") + expect_components(affected(root, base), ["app"]) + + # Unknown path expressions watch project sources conservatively, never caches. + fixtures.write(root, "live/prod/app/terragrunt.hcl", unit("network") + ''' +locals { input_path = "config.json" } +inputs = { value = file(local.input_path) } +''') + base = fixtures.commit(root, "declare dynamic file input") + fixtures.write(root, "new-directory/config.json", "{}") + fixtures.commit(root, "create possible dynamic input") + expect_components(affected(root, base), ["app"]) + base = fixtures.git(root, "rev-parse", "HEAD") + fixtures.write(root, "live/prod/app/.terragrunt-cache/generated.tf", "# generated") + fixtures.commit(root, "record cache content") + expect_components(affected(root, base), []) + + +def test_explicit_imports(root, config): + # Explicit imports opt into roots without enabling automatic filesystem discovery. + seed_units(root) + config["components"]["terraform"]["terragrunt"]["discovery"]["enabled"] = False + config["stacks"] = {"base_path": "stacks", "included_paths": ["**/*"]} + fixtures.write(root, "atmos.yaml", json.encode(config)) + fixtures.write(root, "stacks/adopt.yaml", 'import: ["terragrunt://live", "terragrunt://live/prod/app/terragrunt.hcl"]\n') + base = fixtures.commit(root, "adopt roots explicitly") + instances = json.decode(fixtures.run(root, ["list", "instances", "--format=json"]).stdout) + fixtures.check(sorted([row["Component"] for row in instances]) == ["app", "network"], "Explicit imports changed instance names") + expect_components(affected(root, base), []) + fixtures.write(root, "live/prod/network/terragrunt.hcl", unit() + "# import-selected source changed\n") + fixtures.commit(root, "change imported dependency") + expect_components(affected(root, base, "--include-dependents", "--flatten"), ["app", "network"]) + + def main(): package = fs.resolve(".") work = fixtures.workspace("atmos-terragrunt-affected-") @@ -169,12 +223,16 @@ def main(): ("rewired-dependency", test_rewired_dependency), ("deleted-unit", test_deleted_unit), ("file-watches-and-rename", test_file_watches_and_rename), + ("input-watches", test_input_watches), ]: root = work + "/" + name fixtures.project(root, package) scenario(root) ui.success(name) + config = fixtures.project(work + "/imports", package) + test_explicit_imports(work + "/imports", config) + ui.success("Explicit imports without automatic discovery") fixtures.project(work + "/nested", package) test_nested_stacks(work + "/nested", package) ui.success("nested-stacks") diff --git a/plugins/terragrunt/scripts/fixtures.star b/plugins/terragrunt/scripts/fixtures.star index 2b757f8cf10..715be588dd6 100644 --- a/plugins/terragrunt/scripts/fixtures.star +++ b/plugins/terragrunt/scripts/fixtures.star @@ -52,9 +52,9 @@ def write(root, path, content): fs.write_file(target, content, overwrite=True) -def run(root, arguments, revision="r1"): +def invoke(root, arguments, revision="r1"): # Call the running Atmos binary with its normal process and output handling. - result = atmos.run( + return atmos.run( arguments, working_directory=root, env={ @@ -66,10 +66,21 @@ def run(root, arguments, revision="r1"): output="capture", check=False, ) + + +def run(root, arguments, revision="r1"): + result = invoke(root, arguments, revision) check(result.exit_code == 0, " ".join(arguments) + ":\n" + result.stdout + result.stderr) return result +def run_failing(root, arguments, revision="r1"): + # Negative paths: the command must be rejected, and its diagnostics are returned. + result = invoke(root, arguments, revision) + check(result.exit_code != 0, " ".join(arguments) + " unexpectedly succeeded:\n" + result.stdout + result.stderr) + return result + + def git(root, *arguments): # Disable signing only for disposable fixture commits, never the source repo. result = exec.run( diff --git a/plugins/terragrunt/scripts/lifecycle_extended.star b/plugins/terragrunt/scripts/lifecycle_extended.star new file mode 100644 index 00000000000..8a7eb9552f4 --- /dev/null +++ b/plugins/terragrunt/scripts/lifecycle_extended.star @@ -0,0 +1,495 @@ +# Opt-in CLI acceptance tests with real Terraform/OpenTofu and local state for native +# Terragrunt behavior that native.star does not cover: hooks and extra_arguments, saved +# plans, state and output consumers, and module sources. Every fixture is generated from +# this script, uses only built-in terraform_data resources, and never touches a cloud. +# +# Importing this module has no effects; each integration step calls one scenario function. +fixtures = module.load("fixtures.star") + +STACK = "live/dev" + +# Module whose output proves which arguments, variable files, and environment Terraform saw. +UNIT_MODULE = """ +variable "greeting" { + type = string + default = "unset" +} +variable "suffix" { + type = string + default = "" +} +variable "tag" { + type = string + default = "" +} +variable "envvar" { + type = string + default = "" +} +resource "terraform_data" "unit" { + input = "${var.greeting}|${var.suffix}|${var.tag}|${var.envvar}" +} +output "value" { + value = terraform_data.unit.output +} +""" + +# Terraform state is kept beside the unit so tests can read it without running Terraform. +BACKEND = """ +remote_state { + backend = "local" + config = { path = "${get_terragrunt_dir()}/state/terraform.tfstate" } + generate = { path = "backend.tf", if_exists = "overwrite" } +} +""" + +# Hooks run the selected Terraform executable itself, so they work on every platform and +# need no shell. Their output is the only side effect the tests read. +HOOKED_UNIT = """ +terraform { + source = "../../../modules/unit" + + extra_arguments "inline" { + commands = ["plan", "apply"] + arguments = ["-var", "greeting=@GREETING@"] + env_vars = { TF_VAR_envvar = "from-env" } + } + extra_arguments "files" { + commands = ["plan", "apply"] + required_var_files = ["${get_terragrunt_dir()}/required.tfvars"] + optional_var_files = ["${get_terragrunt_dir()}/optional.tfvars", "${get_terragrunt_dir()}/absent.tfvars"] + } + # Scoped to another command: it must never reach plan or apply. + extra_arguments "destroy-only" { + commands = ["destroy"] + arguments = ["-var", "greeting=destroy-only"] + } + + before_hook "init-hook" { + commands = ["init"] + execute = ["@TOOL@", "version"] + } + before_hook "plan-hook" { + commands = ["plan"] + execute = ["@TOOL@", "version"] + } + before_hook "apply-before" { + commands = ["apply"] + execute = ["@TOOL@", "output", "-no-color"] + } + after_hook "apply-after" { + commands = ["apply"] + execute = ["@TOOL@", "output", "-no-color"] + } + # Nothing fails here, so no error hook may run. + error_hook "never-fires" { + commands = ["apply"] + execute = ["@TOOL@", "version"] + on_errors = [".*"] + } +} +""" + +# A before hook that exits with Terraform fmt's "needs formatting" status gates the apply. +GATED_UNIT = """ +terraform { + source = "../../../modules/unit" + + before_hook "format-gate" { + commands = ["apply"] + execute = ["@TOOL@", "fmt", "-check", "${get_terragrunt_dir()}/unformatted.tf"] + } + after_hook "after-skipped" { + commands = ["apply"] + execute = ["@TOOL@", "version"] + } + after_hook "after-always" { + commands = ["apply"] + execute = ["@TOOL@", "version"] + run_on_error = true + } + error_hook "matching" { + commands = ["apply"] + execute = ["@TOOL@", "version"] + on_errors = ["format-gate"] + } + error_hook "non-matching" { + commands = ["apply"] + execute = ["@TOOL@", "version"] + on_errors = ["NO_SUCH_FAILURE_TEXT"] + } + error_hook "other-command" { + commands = ["plan"] + execute = ["@TOOL@", "version"] + on_errors = ["format-gate"] + } +} +""" + + +def hcl_path(path): + # HCL strings treat backslashes as escapes; forward slashes work on every platform. + return path.replace("\\", "/") + + +def project(work, name, package, binary, stacks=False): + root = work + "/" + name + config = fixtures.configuration(package, binary=binary) + if stacks: + config["stacks"] = {"base_path": "stacks", "included_paths": ["**/*.yaml"]} + fixtures.write(root, "atmos.yaml", json.encode(config)) + fixtures.write(root, "modules/unit/main.tf", UNIT_MODULE) + return root + + +def write_unit(root, name, content): + fixtures.write(root, "live/dev/" + name + "/terragrunt.hcl", content + BACKEND) + + +def terraform(root, operation, unit, arguments=[], flags=[], failing=False): + command = ["terraform", operation, unit, "-s", STACK] + flags + ["--"] + arguments + ["-no-color"] + if failing: + return fixtures.run_failing(root, command) + return fixtures.run(root, command) + + +def state_path(root, unit): + return root + "/live/dev/" + unit + "/state/terraform.tfstate" + + +def state_output(root, unit, name="value"): + # Read the local state Terraform wrote, independent of any Atmos function under test. + state = json.decode(fs.read_file(state_path(root, unit))) + return state["outputs"][name]["value"] + + +def expect_order(text, markers, context): + # Markers must appear in this order; every marker is required. + position = -1 + for marker in markers: + index = text.find(marker, position + 1) + fixtures.check(index >= 0, "Missing or misordered '" + marker + "' in " + context + ":\n" + text) + position = index + + +def expect_count(text, marker, count, context): + actual = text.count(marker) + fixtures.check(actual == count, "Expected " + str(count) + " x '" + marker + "', got " + str(actual) + " in " + context + ":\n" + text) + + +def tool_of(binary): + return hcl_path(binary) + + +def hooked_unit(binary, greeting): + return HOOKED_UNIT.replace("@TOOL@", tool_of(binary)).replace("@GREETING@", greeting) + + +def verify_hook_and_argument_ordering(root, binary): + fixtures.write(root, "live/dev/hooked/required.tfvars", 'tag = "required"\n') + fixtures.write(root, "live/dev/hooked/optional.tfvars", 'suffix = "optional"\n') + write_unit(root, "hooked", hooked_unit(binary, "one")) + + # The first plan initializes implicitly: init hooks fire for that init, plan hooks only for plan. + plan = terraform(root, "plan", "hooked") + expect_count(plan.stderr, "Executing before hook: init-hook", 1, "plan stderr") + expect_count(plan.stderr, "Executing before hook: plan-hook", 1, "plan stderr") + expect_order(plan.stderr, ["Executing before hook: init-hook", "Executing before hook: plan-hook"], "plan stderr") + expect_count(plan.stderr, "apply-before", 0, "plan stderr") + # Extra arguments, both variable files, and the extra environment reached OpenTofu. + fixtures.check("one|optional|required|from-env" in plan.stdout, "Extra arguments did not reach plan:\n" + plan.stdout) + fixtures.check("destroy-only" not in plan.stdout, "A destroy-only argument reached plan:\n" + plan.stdout) + fixtures.check(not fs.exists(state_path(root, "hooked")), "Plan created state") + + # Apply: init is already current, so no init hook; the plan-scoped hook must not fire. + apply = terraform(root, "apply", "hooked", ["-auto-approve"]) + expect_count(apply.stderr, "Executing before hook: plan-hook", 0, "apply stderr") + expect_count(apply.stderr, "Executing before hook: init-hook", 0, "apply stderr") + expect_count(apply.stderr, "Executing error hook", 0, "apply stderr") + # The before hook saw empty state; the after hook saw what the apply wrote. + expect_order(apply.stderr, [ + "Executing before hook: apply-before", + "No outputs found", + "Executing after hook: apply-after", + 'value = "one|optional|required|from-env"', + ], "apply stderr") + fixtures.check(state_output(root, "hooked") == "one|optional|required|from-env", "Unexpected applied value") + + # A second apply with a new argument: the before hook reads the old value, the after hook the new one. + write_unit(root, "hooked", hooked_unit(binary, "two")) + apply = terraform(root, "apply", "hooked", ["-auto-approve"]) + expect_order(apply.stderr, [ + "Executing before hook: apply-before", + 'value = "one|optional|required|from-env"', + "Executing after hook: apply-after", + 'value = "two|optional|required|from-env"', + ], "second apply stderr") + fixtures.check(state_output(root, "hooked") == "two|optional|required|from-env", "Second apply value is stale") + + +def verify_missing_required_var_file(root): + # A required variable file that is absent is rejected before OpenTofu runs; optional ones are not. + path = root + "/live/dev/hooked/required.tfvars" + content = fs.read_file(path) + fs.remove(path) + result = terraform(root, "plan", "hooked", failing=True) + fixtures.check("required.tfvars" in result.stderr + result.stdout, "Missing var file was not named:\n" + result.stderr) + fs.write_file(path, content) + + +def verify_failing_before_hook(root, binary): + write_unit(root, "gated", GATED_UNIT.replace("@TOOL@", tool_of(binary))) + fixtures.write(root, "live/dev/gated/unformatted.tf", 'variable "x" {}\n') + result = terraform(root, "apply", "gated", ["-auto-approve"], failing=True) + err = result.stderr + # OpenTofu fmt -check exits 3 when files need formatting; Atmos must preserve that status. + fixtures.check(result.exit_code == 3, "Hook exit code was not preserved: " + str(result.exit_code) + "\n" + err) + expect_count(err, "Executing before hook: format-gate", 1, "gated stderr") + # The failed before hook stopped the action; no apply output, no state. + fixtures.check("Apply complete" not in result.stdout, "Apply ran after a failing before hook") + fixtures.check(not fs.exists(state_path(root, "gated")), "Failing before hook still created state") + # After hooks run after a failure only with run_on_error. + expect_count(err, "Executing after hook: after-skipped", 0, "gated stderr") + expect_count(err, "Executing after hook: after-always", 1, "gated stderr") + # Error hooks run only for a matching error expression and a listed command. + expect_count(err, "Executing error hook: matching", 1, "gated stderr") + expect_count(err, "Executing error hook: non-matching", 0, "gated stderr") + expect_count(err, "Executing error hook: other-command", 0, "gated stderr") + expect_order(err, [ + "Executing before hook: format-gate", + "Executing after hook: after-always", + "Executing error hook: matching", + ], "gated stderr") + + +def hooks(): + package = fs.resolve(".") + binary = exec.which("tofu") or exec.which("terraform") + fixtures.check(binary != None, "Install OpenTofu or Terraform for integration tests") + root = project(fixtures.workspace("atmos-terragrunt-hooks-"), "project", package, binary) + verify_hook_and_argument_ordering(root, binary) + verify_missing_required_var_file(root) + verify_failing_before_hook(root, binary) + ui.success("Hooks and extra_arguments: ordering, scoping, exit codes, error hooks, var files, env") + + +PLAN_UNIT = """ +terraform { + source = "../../../modules/unit" + + extra_arguments "inline" { + commands = ["plan", "apply"] + arguments = ["-var", "greeting=@GREETING@"] + } +} +""" + + +def planfiles(root): + # Saved plans live beside the module Terraform ran in, next to their private binding. + return fs.glob(root + "/.workdir/terraform/" + STACK + "-planned-*/*.planfile") + + +def plan_unit(root, greeting): + write_unit(root, "planned", PLAN_UNIT.replace("@GREETING@", greeting)) + result = terraform(root, "plan", "planned") + files = planfiles(root) + fixtures.check(len(files) == 1, "Expected one saved plan, found " + str(files)) + fixtures.check(fs.exists(files[0] + ".atmos.json"), "Saved plan has no Atmos binding") + return struct(result=result, planfile=files[0]) + + +def apply_saved(root, failing=False): + return terraform(root, "apply", "planned", flags=["--from-plan"], failing=failing) + + +def verify_rejected(root, result, reason): + output = result.stdout + result.stderr + fixtures.check("plan verification failed" in output, "Rejection was not a plan verification failure:\n" + output) + fixtures.check(reason in output, "Rejection did not explain '" + reason + "':\n" + output) + fixtures.check("Apply complete" not in output, "A rejected plan was applied") + fixtures.check(not fs.exists(state_path(root, "planned")), "A rejected plan created state") + + +def saved_plan(): + package = fs.resolve(".") + binary = exec.which("tofu") or exec.which("terraform") + fixtures.check(binary != None, "Install OpenTofu or Terraform for integration tests") + root = project(fixtures.workspace("atmos-terragrunt-plan-"), "project", package, binary) + + # Tampering with the reviewed plan artifact is rejected before anything is applied. + saved = plan_unit(root, "reviewed") + fixtures.check("reviewed" in saved.result.stdout, "Plan did not show the reviewed value") + fixtures.check(not fs.exists(state_path(root, "planned")), "Planning created state") + fs.write_file(saved.planfile, "tampered", overwrite=True) + verify_rejected(root, apply_saved(root, failing=True), "create a new plan") + + # So is a changed working directory, even when the plan bytes are intact. + saved = plan_unit(root, "reviewed") + injected = saved.planfile.rsplit("/", 1)[0] + "/injected.tf" + fs.write_file(injected, 'output "injected" { value = "unreviewed" }\n') + verify_rejected(root, apply_saved(root, failing=True), "working directory changed") + fs.remove(injected) + + # Editing the unit after review changes the plan's identity, so the plan is rejected too. + saved = plan_unit(root, "reviewed") + write_unit(root, "planned", PLAN_UNIT.replace("@GREETING@", "edited-after-review")) + verify_rejected(root, apply_saved(root, failing=True), "create a new plan") + + # With the reviewed definition back, the plan applies exactly what was reviewed and does + # not plan again. + write_unit(root, "planned", PLAN_UNIT.replace("@GREETING@", "reviewed")) + applied = apply_saved(root) + fixtures.check("Plan:" not in applied.stdout, "Applying a saved plan planned again:\n" + applied.stdout) + fixtures.check("Apply complete! Resources: 1 added" in applied.stdout, applied.stdout) + fixtures.check(state_output(root, "planned") == "reviewed|||", "Applied value differs from the reviewed plan") + ui.success("Saved plans: exact apply without re-planning; tampered plan and workdir rejected") + + +CONSUMER_MODULE = """ +variable "from_output" { + type = string +} +variable "from_state" { + type = string +} +resource "terraform_data" "consumer" { + input = { output = var.from_output, state = var.from_state } +} +""" + +# An ordinary Atmos component reads a Terragrunt unit through both YAML functions. The stack +# is YAML text because the functions are YAML tags, which a JSON document cannot express. +CONSUMER_STACK = """ +components: + terraform: + consumer: + vars: + from_output: !terraform.output producer live/dev value + from_state: !terraform.state producer live/dev value + orphan-consumer: + metadata: + component: consumer + vars: + from_output: !terraform.output orphan live/dev value + from_state: !terraform.state orphan live/dev value +""" + + +def described_vars(root, component): + result = fixtures.run(root, ["describe", "component", component, "-s", "consumers", "--format=json"]) + return json.decode(result.stdout)["vars"] + + +def project_files(root): + # Every project file, including dot directories such as .workdir. + return sorted([path for path in fs.glob(root + "/**/*") if fs.stat(path).is_file] + fs.glob(root + "/.workdir/**/*")) + + +def verify_unprovisioned_state_is_rejected(root): + before = project_files(root) + fixtures.check(not fs.exists(root + "/.workdir"), "Fixture started with a workdir") + result = fixtures.run_failing(root, ["terraform", "plan", "orphan-consumer", "-s", "consumers", "--", "-no-color"]) + fixtures.check("terraform state not provisioned for component orphan in stack live/dev" in result.stderr + result.stdout, "Unclear error:\n" + result.stderr) + # Reading must not create workdirs, workspaces, or state for the unit that was never applied. + fixtures.check(project_files(root) == before, "Reading unprovisioned state changed project files") + fixtures.check(not fs.exists(root + "/.workdir"), "Reading unprovisioned state created a workdir") + fixtures.check(not fs.glob(root + "/**/terraform.tfstate*"), "Reading unprovisioned state created state") + fixtures.check(not fs.exists(state_path(root, "orphan")), "Reading unprovisioned state created unit state") + + +def state_and_outputs(): + package = fs.resolve(".") + binary = exec.which("tofu") or exec.which("terraform") + fixtures.check(binary != None, "Install OpenTofu or Terraform for integration tests") + root = project(fixtures.workspace("atmos-terragrunt-state-"), "project", package, binary, stacks=True) + fixtures.write(root, "components/terraform/consumer/main.tf", CONSUMER_MODULE) + fixtures.write(root, "stacks/consumers.yaml", CONSUMER_STACK) + write_unit(root, "producer", PLAN_UNIT.replace("@GREETING@", "first")) + write_unit(root, "orphan", PLAN_UNIT.replace("@GREETING@", "never-applied")) + + verify_unprovisioned_state_is_rejected(root) + + # After the unit is applied, both functions resolve to what Terraform stored. + terraform(root, "apply", "producer", ["-auto-approve"]) + resolved = described_vars(root, "consumer") + fixtures.check(resolved["from_output"] == "first|||", "Output function returned " + str(resolved)) + fixtures.check(resolved["from_state"] == "first|||", "State function returned " + str(resolved)) + + # The ordinary component consumes the values when it is applied. + fixtures.run(root, ["terraform", "apply", "consumer", "-s", "consumers", "--", "-auto-approve", "-no-color"]) + consumer_state = fs.glob(root + "/components/terraform/consumer/terraform.tfstate.d/*/terraform.tfstate") + fixtures.check(len(consumer_state) == 1, "Expected one consumer state file") + attributes = json.decode(fs.read_file(consumer_state[0]))["resources"][0]["instances"][0]["attributes"] + fixtures.check(attributes["input"]["value"] == {"output": "first|||", "state": "first|||"}, "Consumer received " + str(attributes)) + + # A later change to the unit is visible to the next resolution; nothing is cached on disk. + write_unit(root, "producer", PLAN_UNIT.replace("@GREETING@", "second")) + terraform(root, "apply", "producer", ["-auto-approve"]) + resolved = described_vars(root, "consumer") + fixtures.check(resolved["from_output"] == "second|||" and resolved["from_state"] == "second|||", "Stale value: " + str(resolved)) + ui.success("State and output integration: resolved values, consumer apply, fresh reads, unprovisioned state rejected") + + +def versioned_module(version): + return 'resource "terraform_data" "module" {\n input = "' + version + '"\n}\noutput "version" {\n value = terraform_data.module.output\n}\n' + + +def source_unit(source): + return 'terraform {\n source = "' + source + '"\n}\n' + + +def applied_version(root, unit): + return state_output(root, unit, "version") + + +def create_module_repository(work): + # A local repository with two tagged module revisions replaces any network module source. + repo = work + "/module-repository" + fs.mkdir(repo, parents=True) + fixtures.git(repo, "init", "-b", "main") + fixtures.git(repo, "config", "user.name", "Atmos fixture") + fixtures.git(repo, "config", "user.email", "fixture@example.invalid") + fixtures.write(repo, "module/main.tf", versioned_module("module-v1")) + fixtures.commit(repo, "module v1") + fixtures.git(repo, "tag", "v1") + fixtures.write(repo, "module/main.tf", versioned_module("module-v2")) + fixtures.commit(repo, "module v2") + fixtures.git(repo, "tag", "v2") + url = hcl_path(repo) + return "git::file://" + ("" if url.startswith("/") else "/") + url + "//module?ref=" + + +def git_source_refresh(): + package = fs.resolve(".") + binary = exec.which("tofu") or exec.which("terraform") + fixtures.check(binary != None, "Install OpenTofu or Terraform for integration tests") + work = fixtures.workspace("atmos-terragrunt-git-source-") + source = create_module_repository(work) + root = project(work, "project", package, binary) + write_unit(root, "remote", source_unit(source + "v1")) + terraform(root, "apply", "remote", ["-auto-approve"]) + fixtures.check(applied_version(root, "remote") == "module-v1", "The tagged module was not fetched and applied") + + # Moving the reference to a newer tag must replace the module code provisioned earlier. + write_unit(root, "remote", source_unit(source + "v2")) + terraform(root, "apply", "remote", ["-auto-approve"]) + fixtures.check(applied_version(root, "remote") == "module-v2", "A changed Git ref kept the previously provisioned module") + ui.success("Local Git module source: fetched at v1, refreshed at v2") + + +def local_source_refresh(): + package = fs.resolve(".") + binary = exec.which("tofu") or exec.which("terraform") + fixtures.check(binary != None, "Install OpenTofu or Terraform for integration tests") + root = project(fixtures.workspace("atmos-terragrunt-local-source-"), "project", package, binary) + fixtures.write(root, "modules/local/main.tf", versioned_module("local-v1")) + write_unit(root, "local", source_unit("../../../modules/local")) + terraform(root, "apply", "local", ["-auto-approve"]) + fixtures.check(applied_version(root, "local") == "local-v1", "The local module was not applied") + + # Editing the module in place must reach the next run through the retained workdir. + fixtures.write(root, "modules/local/main.tf", versioned_module("local-v2")) + terraform(root, "apply", "local", ["-auto-approve"]) + fixtures.check(applied_version(root, "local") == "local-v2", "An edited local module kept the previously provisioned code") + ui.success("Local path module source: applied at v1, refreshed at v2") diff --git a/plugins/terragrunt/scripts/release.star b/plugins/terragrunt/scripts/release.star new file mode 100644 index 00000000000..809d79c60c2 --- /dev/null +++ b/plugins/terragrunt/scripts/release.star @@ -0,0 +1,69 @@ +# Verify that discovery works from a packaged binary, without a development path. +fixtures = module.load("fixtures.star") + + +def copy_source(source, target): + # Keep local build products out of the disposable source tree. + fs.mkdir(target, parents=True) + for path in fs.glob(source + "/*"): + name = path.replace("\\", "/").rsplit("/", 1)[1] + if name in ["bin", ".git", ".atmos", ".terraform", ".terragrunt-cache", ".terragrunt-stack"]: + continue + if fs.stat(path).is_dir: + copy_source(path, target + "/" + name) + else: + fs.write_file(target + "/" + name, fs.read_file(path)) + + +def pack_native_release(work, package): + # Build against the exact manifest being packed: the handshake binds its digest. + target = exec.run(["go", "env", "GOOS", "GOARCH"], output="capture").stdout.split() + platform = target[0] + "/" + target[1] + payload = work + "/source/plugins/terragrunt" + copy_source(package, payload) + copy_source(fs.resolve("../../sdk"), work + "/source/sdk") + manifest = yaml.decode(fs.read_file(payload + "/plugin.yaml")) + executable = manifest["spec"]["runtime"]["executable"] + manifest["spec"]["platforms"] = {platform: {"executable": executable}} + fixtures.write(payload, "plugin.yaml", json.encode(manifest)) + fixtures.run(work, ["plugin", "build", payload]) + layout = work + "/release" + fixtures.run(work, ["plugin", "pack", payload, "--output", layout]) + return struct(layout=layout, version=manifest["metadata"]["version"]) + + +def install_and_discover(work, package, release): + project = work + "/project" + fixtures.copy_tree(package + "/fixtures/explicit-stack", project) + config = fixtures.configuration(package) + # Keep cache writes disposable and prove installation does not need a registry. + config["plugins"] = { + "packages": {}, + "cache_dir": work + "/cache", + "policy": {"offline": True}, + "discovery": {"default_registry": False}, + } + fixtures.write(project, "atmos.yaml", json.encode(config)) + source = "oci://registry.example.invalid/atmos/terragrunt" + fixtures.run(project, ["plugin", "install", source, "--as", "terragrunt", "--version", release.version, "--from-layout", release.layout]) + lock_path = project + "/.atmos.plugins.lock.yaml" + before = fs.read_file(lock_path) + fixtures.run(project, ["plugin", "install", "--frozen"]) + fixtures.check(before == fs.read_file(lock_path), "Frozen restore changed the release lock") + installed = yaml.decode(fs.read_file(project + "/atmos.yaml"))["plugins"]["packages"]["terragrunt"] + fixtures.check("path" not in installed, "Installed package still uses a development path") + result = fixtures.run(project, ["list", "instances", "--format=json"]) + rows = json.decode(result.stdout) + fixtures.check(len(rows) == 4, "Installed plugin did not discover four explicit-stack units") + fixtures.check(all([row["Type"] == "terraform" for row in rows]), "Installed units lost their component type") + fixtures.check(not fs.exists(project + "/live/.terragrunt-stack"), "Installed discovery wrote generated stacks") + + +def main(): + package = fs.resolve(".") + work = fixtures.workspace("atmos-terragrunt-release-") + # Packing needs no Terraform executable and does not evaluate HCL. + fixtures.write(work, "atmos.yaml", "{}") + release = pack_native_release(work, package) + install_and_discover(work, package, release) + ui.success("Native OCI package: offline install, frozen restore, and explicit-stack discovery") diff --git a/proto/atmos/plugin/v1/plugin.proto b/proto/atmos/plugin/v1/plugin.proto index 3d29a021107..3d64dbf1075 100644 --- a/proto/atmos/plugin/v1/plugin.proto +++ b/proto/atmos/plugin/v1/plugin.proto @@ -194,6 +194,9 @@ message ComponentOperation { string command = 1; string mode = 2; map options = 3; + // Resolved engine arguments are distinct from provider options. They let + // configuration semantics inspect flags such as plan/apply -destroy. + repeated string terraform_arguments = 4; } message DiscoverComponentsRequest { Invocation invocation = 1; @@ -250,6 +253,32 @@ message ComponentGeneratedFile { string if_exists = 3; bool sensitive = 4; } +// Evaluated command metadata; the host executes it with the invocation's +// environment, toolchain, cancellation, and I/O. Lists retain declaration order. +message ComponentExecutionConfiguration { + repeated ComponentExtraArguments extra_arguments = 1; + repeated ComponentHook before_hooks = 2; + repeated ComponentHook after_hooks = 3; + repeated ComponentHook error_hooks = 4; +} +message ComponentExtraArguments { + repeated string commands = 1; + repeated string arguments = 2; + repeated string required_var_files = 3; + repeated string optional_var_files = 4; + map environment = 5; +} +message ComponentHook { + string name = 1; + repeated string commands = 2; + repeated string execute = 3; + // Empty means the provisioned module directory; relative paths resolve there. + string working_directory = 4; + bool run_on_error = 5; + bool suppress_stdout = 6; + // Error hooks run when any pattern matches the errors from preceding phases. + repeated string on_errors = 7; +} message PrepareComponentResponse { string engine = 1; string working_directory = 2; @@ -260,6 +289,10 @@ message PrepareComponentResponse { repeated ComponentDependency dependencies = 7; repeated ComponentSource sources = 8; repeated Diagnostic diagnostics = 9; + // A returned generated file owns the backend block. The host must not also + // emit its default backend file. + bool backend_generated = 10; + ComponentExecutionConfiguration execution = 11; } // Reevaluate immediately before the selected operation, after its dependencies. // Preparing configuration does not authorize executing infrastructure commands. diff --git a/sdk/proto/atmos/plugin/v1/plugin.pb.go b/sdk/proto/atmos/plugin/v1/plugin.pb.go index 5ec7330748a..5228242498d 100644 --- a/sdk/proto/atmos/plugin/v1/plugin.pb.go +++ b/sdk/proto/atmos/plugin/v1/plugin.pb.go @@ -2326,12 +2326,15 @@ func (x *ComponentEffects) GetStateReads() bool { // distinguishes an unset option from an explicit false, zero, or empty value. // These are not Terraform argv. The provider validates supported semantics. type ComponentOperation struct { - state protoimpl.MessageState `protogen:"open.v1"` - Command string `protobuf:"bytes,1,opt,name=command,proto3" json:"command,omitempty"` - Mode string `protobuf:"bytes,2,opt,name=mode,proto3" json:"mode,omitempty"` - Options map[string]*ComponentValue `protobuf:"bytes,3,rep,name=options,proto3" json:"options,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache + state protoimpl.MessageState `protogen:"open.v1"` + Command string `protobuf:"bytes,1,opt,name=command,proto3" json:"command,omitempty"` + Mode string `protobuf:"bytes,2,opt,name=mode,proto3" json:"mode,omitempty"` + Options map[string]*ComponentValue `protobuf:"bytes,3,rep,name=options,proto3" json:"options,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` + // Resolved engine arguments are distinct from provider options. They let + // configuration semantics inspect flags such as plan/apply -destroy. + TerraformArguments []string `protobuf:"bytes,4,rep,name=terraform_arguments,json=terraformArguments,proto3" json:"terraform_arguments,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache } func (x *ComponentOperation) Reset() { @@ -2385,6 +2388,13 @@ func (x *ComponentOperation) GetOptions() map[string]*ComponentValue { return nil } +func (x *ComponentOperation) GetTerraformArguments() []string { + if x != nil { + return x.TerraformArguments + } + return nil +} + type DiscoverComponentsRequest struct { state protoimpl.MessageState `protogen:"open.v1"` Invocation *Invocation `protobuf:"bytes,1,opt,name=invocation,proto3" json:"invocation,omitempty"` @@ -2803,6 +2813,246 @@ func (x *ComponentGeneratedFile) GetSensitive() bool { return false } +// Evaluated command metadata; the host executes it with the invocation's +// environment, toolchain, cancellation, and I/O. Lists retain declaration order. +type ComponentExecutionConfiguration struct { + state protoimpl.MessageState `protogen:"open.v1"` + ExtraArguments []*ComponentExtraArguments `protobuf:"bytes,1,rep,name=extra_arguments,json=extraArguments,proto3" json:"extra_arguments,omitempty"` + BeforeHooks []*ComponentHook `protobuf:"bytes,2,rep,name=before_hooks,json=beforeHooks,proto3" json:"before_hooks,omitempty"` + AfterHooks []*ComponentHook `protobuf:"bytes,3,rep,name=after_hooks,json=afterHooks,proto3" json:"after_hooks,omitempty"` + ErrorHooks []*ComponentHook `protobuf:"bytes,4,rep,name=error_hooks,json=errorHooks,proto3" json:"error_hooks,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ComponentExecutionConfiguration) Reset() { + *x = ComponentExecutionConfiguration{} + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[40] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ComponentExecutionConfiguration) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ComponentExecutionConfiguration) ProtoMessage() {} + +func (x *ComponentExecutionConfiguration) ProtoReflect() protoreflect.Message { + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[40] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ComponentExecutionConfiguration.ProtoReflect.Descriptor instead. +func (*ComponentExecutionConfiguration) Descriptor() ([]byte, []int) { + return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{40} +} + +func (x *ComponentExecutionConfiguration) GetExtraArguments() []*ComponentExtraArguments { + if x != nil { + return x.ExtraArguments + } + return nil +} + +func (x *ComponentExecutionConfiguration) GetBeforeHooks() []*ComponentHook { + if x != nil { + return x.BeforeHooks + } + return nil +} + +func (x *ComponentExecutionConfiguration) GetAfterHooks() []*ComponentHook { + if x != nil { + return x.AfterHooks + } + return nil +} + +func (x *ComponentExecutionConfiguration) GetErrorHooks() []*ComponentHook { + if x != nil { + return x.ErrorHooks + } + return nil +} + +type ComponentExtraArguments struct { + state protoimpl.MessageState `protogen:"open.v1"` + Commands []string `protobuf:"bytes,1,rep,name=commands,proto3" json:"commands,omitempty"` + Arguments []string `protobuf:"bytes,2,rep,name=arguments,proto3" json:"arguments,omitempty"` + RequiredVarFiles []string `protobuf:"bytes,3,rep,name=required_var_files,json=requiredVarFiles,proto3" json:"required_var_files,omitempty"` + OptionalVarFiles []string `protobuf:"bytes,4,rep,name=optional_var_files,json=optionalVarFiles,proto3" json:"optional_var_files,omitempty"` + Environment map[string]string `protobuf:"bytes,5,rep,name=environment,proto3" json:"environment,omitempty" protobuf_key:"bytes,1,opt,name=key" protobuf_val:"bytes,2,opt,name=value"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ComponentExtraArguments) Reset() { + *x = ComponentExtraArguments{} + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[41] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ComponentExtraArguments) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ComponentExtraArguments) ProtoMessage() {} + +func (x *ComponentExtraArguments) ProtoReflect() protoreflect.Message { + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[41] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ComponentExtraArguments.ProtoReflect.Descriptor instead. +func (*ComponentExtraArguments) Descriptor() ([]byte, []int) { + return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{41} +} + +func (x *ComponentExtraArguments) GetCommands() []string { + if x != nil { + return x.Commands + } + return nil +} + +func (x *ComponentExtraArguments) GetArguments() []string { + if x != nil { + return x.Arguments + } + return nil +} + +func (x *ComponentExtraArguments) GetRequiredVarFiles() []string { + if x != nil { + return x.RequiredVarFiles + } + return nil +} + +func (x *ComponentExtraArguments) GetOptionalVarFiles() []string { + if x != nil { + return x.OptionalVarFiles + } + return nil +} + +func (x *ComponentExtraArguments) GetEnvironment() map[string]string { + if x != nil { + return x.Environment + } + return nil +} + +type ComponentHook struct { + state protoimpl.MessageState `protogen:"open.v1"` + Name string `protobuf:"bytes,1,opt,name=name,proto3" json:"name,omitempty"` + Commands []string `protobuf:"bytes,2,rep,name=commands,proto3" json:"commands,omitempty"` + Execute []string `protobuf:"bytes,3,rep,name=execute,proto3" json:"execute,omitempty"` + // Empty means the provisioned module directory; relative paths resolve there. + WorkingDirectory string `protobuf:"bytes,4,opt,name=working_directory,json=workingDirectory,proto3" json:"working_directory,omitempty"` + RunOnError bool `protobuf:"varint,5,opt,name=run_on_error,json=runOnError,proto3" json:"run_on_error,omitempty"` + SuppressStdout bool `protobuf:"varint,6,opt,name=suppress_stdout,json=suppressStdout,proto3" json:"suppress_stdout,omitempty"` + // Error hooks run when any pattern matches the errors from preceding phases. + OnErrors []string `protobuf:"bytes,7,rep,name=on_errors,json=onErrors,proto3" json:"on_errors,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache +} + +func (x *ComponentHook) Reset() { + *x = ComponentHook{} + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[42] + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + ms.StoreMessageInfo(mi) +} + +func (x *ComponentHook) String() string { + return protoimpl.X.MessageStringOf(x) +} + +func (*ComponentHook) ProtoMessage() {} + +func (x *ComponentHook) ProtoReflect() protoreflect.Message { + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[42] + if x != nil { + ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) + if ms.LoadMessageInfo() == nil { + ms.StoreMessageInfo(mi) + } + return ms + } + return mi.MessageOf(x) +} + +// Deprecated: Use ComponentHook.ProtoReflect.Descriptor instead. +func (*ComponentHook) Descriptor() ([]byte, []int) { + return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{42} +} + +func (x *ComponentHook) GetName() string { + if x != nil { + return x.Name + } + return "" +} + +func (x *ComponentHook) GetCommands() []string { + if x != nil { + return x.Commands + } + return nil +} + +func (x *ComponentHook) GetExecute() []string { + if x != nil { + return x.Execute + } + return nil +} + +func (x *ComponentHook) GetWorkingDirectory() string { + if x != nil { + return x.WorkingDirectory + } + return "" +} + +func (x *ComponentHook) GetRunOnError() bool { + if x != nil { + return x.RunOnError + } + return false +} + +func (x *ComponentHook) GetSuppressStdout() bool { + if x != nil { + return x.SuppressStdout + } + return false +} + +func (x *ComponentHook) GetOnErrors() []string { + if x != nil { + return x.OnErrors + } + return nil +} + type PrepareComponentResponse struct { state protoimpl.MessageState `protogen:"open.v1"` Engine string `protobuf:"bytes,1,opt,name=engine,proto3" json:"engine,omitempty"` @@ -2814,13 +3064,17 @@ type PrepareComponentResponse struct { Dependencies []*ComponentDependency `protobuf:"bytes,7,rep,name=dependencies,proto3" json:"dependencies,omitempty"` Sources []*ComponentSource `protobuf:"bytes,8,rep,name=sources,proto3" json:"sources,omitempty"` Diagnostics []*Diagnostic `protobuf:"bytes,9,rep,name=diagnostics,proto3" json:"diagnostics,omitempty"` + // A returned generated file owns the backend block. The host must not also + // emit its default backend file. + BackendGenerated bool `protobuf:"varint,10,opt,name=backend_generated,json=backendGenerated,proto3" json:"backend_generated,omitempty"` + Execution *ComponentExecutionConfiguration `protobuf:"bytes,11,opt,name=execution,proto3" json:"execution,omitempty"` unknownFields protoimpl.UnknownFields sizeCache protoimpl.SizeCache } func (x *PrepareComponentResponse) Reset() { *x = PrepareComponentResponse{} - mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[40] + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[43] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -2832,7 +3086,7 @@ func (x *PrepareComponentResponse) String() string { func (*PrepareComponentResponse) ProtoMessage() {} func (x *PrepareComponentResponse) ProtoReflect() protoreflect.Message { - mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[40] + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[43] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -2845,7 +3099,7 @@ func (x *PrepareComponentResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use PrepareComponentResponse.ProtoReflect.Descriptor instead. func (*PrepareComponentResponse) Descriptor() ([]byte, []int) { - return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{40} + return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{43} } func (x *PrepareComponentResponse) GetEngine() string { @@ -2911,6 +3165,20 @@ func (x *PrepareComponentResponse) GetDiagnostics() []*Diagnostic { return nil } +func (x *PrepareComponentResponse) GetBackendGenerated() bool { + if x != nil { + return x.BackendGenerated + } + return false +} + +func (x *PrepareComponentResponse) GetExecution() *ComponentExecutionConfiguration { + if x != nil { + return x.Execution + } + return nil +} + type ResolveComponentStateResponse struct { state protoimpl.MessageState `protogen:"open.v1"` State *ComponentStateIdentity `protobuf:"bytes,1,opt,name=state,proto3" json:"state,omitempty"` @@ -2922,7 +3190,7 @@ type ResolveComponentStateResponse struct { func (x *ResolveComponentStateResponse) Reset() { *x = ResolveComponentStateResponse{} - mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[41] + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[44] ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) ms.StoreMessageInfo(mi) } @@ -2934,7 +3202,7 @@ func (x *ResolveComponentStateResponse) String() string { func (*ResolveComponentStateResponse) ProtoMessage() {} func (x *ResolveComponentStateResponse) ProtoReflect() protoreflect.Message { - mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[41] + mi := &file_atmos_plugin_v1_plugin_proto_msgTypes[44] if x != nil { ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x)) if ms.LoadMessageInfo() == nil { @@ -2947,7 +3215,7 @@ func (x *ResolveComponentStateResponse) ProtoReflect() protoreflect.Message { // Deprecated: Use ResolveComponentStateResponse.ProtoReflect.Descriptor instead. func (*ResolveComponentStateResponse) Descriptor() ([]byte, []int) { - return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{41} + return file_atmos_plugin_v1_plugin_proto_rawDescGZIP(), []int{44} } func (x *ResolveComponentStateResponse) GetState() *ComponentStateIdentity { @@ -3158,11 +3426,12 @@ const file_atmos_plugin_v1_plugin_proto_rawDesc = "" + "\asecrets\x18\x03 \x01(\bR\asecrets\x12\x16\n" + "\x06writes\x18\x04 \x01(\bR\x06writes\x12\x1f\n" + "\vstate_reads\x18\x05 \x01(\bR\n" + - "stateReads\"\xeb\x01\n" + + "stateReads\"\x9c\x02\n" + "\x12ComponentOperation\x12\x18\n" + "\acommand\x18\x01 \x01(\tR\acommand\x12\x12\n" + "\x04mode\x18\x02 \x01(\tR\x04mode\x12J\n" + - "\aoptions\x18\x03 \x03(\v20.atmos.plugin.v1.ComponentOperation.OptionsEntryR\aoptions\x1a[\n" + + "\aoptions\x18\x03 \x03(\v20.atmos.plugin.v1.ComponentOperation.OptionsEntryR\aoptions\x12/\n" + + "\x13terraform_arguments\x18\x04 \x03(\tR\x12terraformArguments\x1a[\n" + "\fOptionsEntry\x12\x10\n" + "\x03key\x18\x01 \x01(\tR\x03key\x125\n" + "\x05value\x18\x02 \x01(\v2\x1f.atmos.plugin.v1.ComponentValueR\x05value:\x028\x01\"\xf3\x03\n" + @@ -3206,7 +3475,32 @@ const file_atmos_plugin_v1_plugin_proto_rawDesc = "" + "\x04path\x18\x01 \x01(\tR\x04path\x12\x18\n" + "\acontent\x18\x02 \x01(\fR\acontent\x12\x1b\n" + "\tif_exists\x18\x03 \x01(\tR\bifExists\x12\x1c\n" + - "\tsensitive\x18\x04 \x01(\bR\tsensitive\"\x80\x04\n" + + "\tsensitive\x18\x04 \x01(\bR\tsensitive\"\xb9\x02\n" + + "\x1fComponentExecutionConfiguration\x12Q\n" + + "\x0fextra_arguments\x18\x01 \x03(\v2(.atmos.plugin.v1.ComponentExtraArgumentsR\x0eextraArguments\x12A\n" + + "\fbefore_hooks\x18\x02 \x03(\v2\x1e.atmos.plugin.v1.ComponentHookR\vbeforeHooks\x12?\n" + + "\vafter_hooks\x18\x03 \x03(\v2\x1e.atmos.plugin.v1.ComponentHookR\n" + + "afterHooks\x12?\n" + + "\verror_hooks\x18\x04 \x03(\v2\x1e.atmos.plugin.v1.ComponentHookR\n" + + "errorHooks\"\xcc\x02\n" + + "\x17ComponentExtraArguments\x12\x1a\n" + + "\bcommands\x18\x01 \x03(\tR\bcommands\x12\x1c\n" + + "\targuments\x18\x02 \x03(\tR\targuments\x12,\n" + + "\x12required_var_files\x18\x03 \x03(\tR\x10requiredVarFiles\x12,\n" + + "\x12optional_var_files\x18\x04 \x03(\tR\x10optionalVarFiles\x12[\n" + + "\venvironment\x18\x05 \x03(\v29.atmos.plugin.v1.ComponentExtraArguments.EnvironmentEntryR\venvironment\x1a>\n" + + "\x10EnvironmentEntry\x12\x10\n" + + "\x03key\x18\x01 \x01(\tR\x03key\x12\x14\n" + + "\x05value\x18\x02 \x01(\tR\x05value:\x028\x01\"\xee\x01\n" + + "\rComponentHook\x12\x12\n" + + "\x04name\x18\x01 \x01(\tR\x04name\x12\x1a\n" + + "\bcommands\x18\x02 \x03(\tR\bcommands\x12\x18\n" + + "\aexecute\x18\x03 \x03(\tR\aexecute\x12+\n" + + "\x11working_directory\x18\x04 \x01(\tR\x10workingDirectory\x12 \n" + + "\frun_on_error\x18\x05 \x01(\bR\n" + + "runOnError\x12'\n" + + "\x0fsuppress_stdout\x18\x06 \x01(\bR\x0esuppressStdout\x12\x1b\n" + + "\ton_errors\x18\a \x03(\tR\bonErrors\"\xfd\x04\n" + "\x18PrepareComponentResponse\x12\x16\n" + "\x06engine\x18\x01 \x01(\tR\x06engine\x12+\n" + "\x11working_directory\x18\x02 \x01(\tR\x10workingDirectory\x12#\n" + @@ -3216,7 +3510,10 @@ const file_atmos_plugin_v1_plugin_proto_rawDesc = "" + "\x05files\x18\x06 \x03(\v2'.atmos.plugin.v1.ComponentGeneratedFileR\x05files\x12H\n" + "\fdependencies\x18\a \x03(\v2$.atmos.plugin.v1.ComponentDependencyR\fdependencies\x12:\n" + "\asources\x18\b \x03(\v2 .atmos.plugin.v1.ComponentSourceR\asources\x12=\n" + - "\vdiagnostics\x18\t \x03(\v2\x1b.atmos.plugin.v1.DiagnosticR\vdiagnostics\"\xd9\x01\n" + + "\vdiagnostics\x18\t \x03(\v2\x1b.atmos.plugin.v1.DiagnosticR\vdiagnostics\x12+\n" + + "\x11backend_generated\x18\n" + + " \x01(\bR\x10backendGenerated\x12N\n" + + "\texecution\x18\v \x01(\v20.atmos.plugin.v1.ComponentExecutionConfigurationR\texecution\"\xd9\x01\n" + "\x1dResolveComponentStateResponse\x12=\n" + "\x05state\x18\x01 \x01(\v2'.atmos.plugin.v1.ComponentStateIdentityR\x05state\x12:\n" + "\asources\x18\x02 \x03(\v2 .atmos.plugin.v1.ComponentSourceR\asources\x12=\n" + @@ -3264,81 +3561,85 @@ func file_atmos_plugin_v1_plugin_proto_rawDescGZIP() []byte { var ( file_atmos_plugin_v1_plugin_proto_enumTypes = make([]protoimpl.EnumInfo, 2) - file_atmos_plugin_v1_plugin_proto_msgTypes = make([]protoimpl.MessageInfo, 49) + file_atmos_plugin_v1_plugin_proto_msgTypes = make([]protoimpl.MessageInfo, 53) file_atmos_plugin_v1_plugin_proto_goTypes = []any{ - ComponentSource_Kind(0), // 0: atmos.plugin.v1.ComponentSource.Kind - ComponentDependency_Kind(0), // 1: atmos.plugin.v1.ComponentDependency.Kind - (*Invocation)(nil), // 2: atmos.plugin.v1.Invocation - (*Credentials)(nil), // 3: atmos.plugin.v1.Credentials - (*ServiceVersionRange)(nil), // 4: atmos.plugin.v1.ServiceVersionRange - (*Export)(nil), // 5: atmos.plugin.v1.Export - (*DescribeRequest)(nil), // 6: atmos.plugin.v1.DescribeRequest - (*DescribeResponse)(nil), // 7: atmos.plugin.v1.DescribeResponse - (*ConfigureRequest)(nil), // 8: atmos.plugin.v1.ConfigureRequest - (*ProviderRequest)(nil), // 9: atmos.plugin.v1.ProviderRequest - (*IdentityRequest)(nil), // 10: atmos.plugin.v1.IdentityRequest - (*Progress)(nil), // 11: atmos.plugin.v1.Progress - (*Interaction)(nil), // 12: atmos.plugin.v1.Interaction - (*AuthenticateEvent)(nil), // 13: atmos.plugin.v1.AuthenticateEvent - (*Diagnostic)(nil), // 14: atmos.plugin.v1.Diagnostic - (*ValidateResponse)(nil), // 15: atmos.plugin.v1.ValidateResponse - (*Unsupported)(nil), // 16: atmos.plugin.v1.Unsupported - (*RefreshRequest)(nil), // 17: atmos.plugin.v1.RefreshRequest - (*RefreshResponse)(nil), // 18: atmos.plugin.v1.RefreshResponse - (*EnvironmentResponse)(nil), // 19: atmos.plugin.v1.EnvironmentResponse - (*PrepareEnvironmentRequest)(nil), // 20: atmos.plugin.v1.PrepareEnvironmentRequest - (*CredentialPath)(nil), // 21: atmos.plugin.v1.CredentialPath - (*PathsResponse)(nil), // 22: atmos.plugin.v1.PathsResponse - (*IdentityAuthenticateRequest)(nil), // 23: atmos.plugin.v1.IdentityAuthenticateRequest - (*IdentityPrepareRequest)(nil), // 24: atmos.plugin.v1.IdentityPrepareRequest - (*CommandRequest)(nil), // 25: atmos.plugin.v1.CommandRequest - (*CommandExit)(nil), // 26: atmos.plugin.v1.CommandExit - (*CommandEvent)(nil), // 27: atmos.plugin.v1.CommandEvent - (*ComponentValue)(nil), // 28: atmos.plugin.v1.ComponentValue - (*ComponentValuePath)(nil), // 29: atmos.plugin.v1.ComponentValuePath - (*ComponentValueStep)(nil), // 30: atmos.plugin.v1.ComponentValueStep - (*ComponentAddress)(nil), // 31: atmos.plugin.v1.ComponentAddress - (*ComponentSource)(nil), // 32: atmos.plugin.v1.ComponentSource - (*ComponentDependency)(nil), // 33: atmos.plugin.v1.ComponentDependency - (*DiscoveredComponent)(nil), // 34: atmos.plugin.v1.DiscoveredComponent - (*ComponentEffects)(nil), // 35: atmos.plugin.v1.ComponentEffects - (*ComponentOperation)(nil), // 36: atmos.plugin.v1.ComponentOperation - (*DiscoverComponentsRequest)(nil), // 37: atmos.plugin.v1.DiscoverComponentsRequest - (*DiscoverComponentsResponse)(nil), // 38: atmos.plugin.v1.DiscoverComponentsResponse - (*ComponentRequest)(nil), // 39: atmos.plugin.v1.ComponentRequest - (*ComponentStateIdentity)(nil), // 40: atmos.plugin.v1.ComponentStateIdentity - (*ComponentGeneratedFile)(nil), // 41: atmos.plugin.v1.ComponentGeneratedFile - (*PrepareComponentResponse)(nil), // 42: atmos.plugin.v1.PrepareComponentResponse - (*ResolveComponentStateResponse)(nil), // 43: atmos.plugin.v1.ResolveComponentStateResponse - nil, // 44: atmos.plugin.v1.Invocation.EnvironmentEntry - nil, // 45: atmos.plugin.v1.ConfigureRequest.BrokerIdsEntry - nil, // 46: atmos.plugin.v1.Interaction.FieldsEntry - nil, // 47: atmos.plugin.v1.EnvironmentResponse.EnvironmentEntry - nil, // 48: atmos.plugin.v1.PrepareEnvironmentRequest.EnvironmentEntry - nil, // 49: atmos.plugin.v1.IdentityPrepareRequest.EnvironmentEntry - nil, // 50: atmos.plugin.v1.ComponentOperation.OptionsEntry - (*timestamppb.Timestamp)(nil), // 51: google.protobuf.Timestamp - (*structpb.Struct)(nil), // 52: google.protobuf.Struct - (*emptypb.Empty)(nil), // 53: google.protobuf.Empty + ComponentSource_Kind(0), // 0: atmos.plugin.v1.ComponentSource.Kind + ComponentDependency_Kind(0), // 1: atmos.plugin.v1.ComponentDependency.Kind + (*Invocation)(nil), // 2: atmos.plugin.v1.Invocation + (*Credentials)(nil), // 3: atmos.plugin.v1.Credentials + (*ServiceVersionRange)(nil), // 4: atmos.plugin.v1.ServiceVersionRange + (*Export)(nil), // 5: atmos.plugin.v1.Export + (*DescribeRequest)(nil), // 6: atmos.plugin.v1.DescribeRequest + (*DescribeResponse)(nil), // 7: atmos.plugin.v1.DescribeResponse + (*ConfigureRequest)(nil), // 8: atmos.plugin.v1.ConfigureRequest + (*ProviderRequest)(nil), // 9: atmos.plugin.v1.ProviderRequest + (*IdentityRequest)(nil), // 10: atmos.plugin.v1.IdentityRequest + (*Progress)(nil), // 11: atmos.plugin.v1.Progress + (*Interaction)(nil), // 12: atmos.plugin.v1.Interaction + (*AuthenticateEvent)(nil), // 13: atmos.plugin.v1.AuthenticateEvent + (*Diagnostic)(nil), // 14: atmos.plugin.v1.Diagnostic + (*ValidateResponse)(nil), // 15: atmos.plugin.v1.ValidateResponse + (*Unsupported)(nil), // 16: atmos.plugin.v1.Unsupported + (*RefreshRequest)(nil), // 17: atmos.plugin.v1.RefreshRequest + (*RefreshResponse)(nil), // 18: atmos.plugin.v1.RefreshResponse + (*EnvironmentResponse)(nil), // 19: atmos.plugin.v1.EnvironmentResponse + (*PrepareEnvironmentRequest)(nil), // 20: atmos.plugin.v1.PrepareEnvironmentRequest + (*CredentialPath)(nil), // 21: atmos.plugin.v1.CredentialPath + (*PathsResponse)(nil), // 22: atmos.plugin.v1.PathsResponse + (*IdentityAuthenticateRequest)(nil), // 23: atmos.plugin.v1.IdentityAuthenticateRequest + (*IdentityPrepareRequest)(nil), // 24: atmos.plugin.v1.IdentityPrepareRequest + (*CommandRequest)(nil), // 25: atmos.plugin.v1.CommandRequest + (*CommandExit)(nil), // 26: atmos.plugin.v1.CommandExit + (*CommandEvent)(nil), // 27: atmos.plugin.v1.CommandEvent + (*ComponentValue)(nil), // 28: atmos.plugin.v1.ComponentValue + (*ComponentValuePath)(nil), // 29: atmos.plugin.v1.ComponentValuePath + (*ComponentValueStep)(nil), // 30: atmos.plugin.v1.ComponentValueStep + (*ComponentAddress)(nil), // 31: atmos.plugin.v1.ComponentAddress + (*ComponentSource)(nil), // 32: atmos.plugin.v1.ComponentSource + (*ComponentDependency)(nil), // 33: atmos.plugin.v1.ComponentDependency + (*DiscoveredComponent)(nil), // 34: atmos.plugin.v1.DiscoveredComponent + (*ComponentEffects)(nil), // 35: atmos.plugin.v1.ComponentEffects + (*ComponentOperation)(nil), // 36: atmos.plugin.v1.ComponentOperation + (*DiscoverComponentsRequest)(nil), // 37: atmos.plugin.v1.DiscoverComponentsRequest + (*DiscoverComponentsResponse)(nil), // 38: atmos.plugin.v1.DiscoverComponentsResponse + (*ComponentRequest)(nil), // 39: atmos.plugin.v1.ComponentRequest + (*ComponentStateIdentity)(nil), // 40: atmos.plugin.v1.ComponentStateIdentity + (*ComponentGeneratedFile)(nil), // 41: atmos.plugin.v1.ComponentGeneratedFile + (*ComponentExecutionConfiguration)(nil), // 42: atmos.plugin.v1.ComponentExecutionConfiguration + (*ComponentExtraArguments)(nil), // 43: atmos.plugin.v1.ComponentExtraArguments + (*ComponentHook)(nil), // 44: atmos.plugin.v1.ComponentHook + (*PrepareComponentResponse)(nil), // 45: atmos.plugin.v1.PrepareComponentResponse + (*ResolveComponentStateResponse)(nil), // 46: atmos.plugin.v1.ResolveComponentStateResponse + nil, // 47: atmos.plugin.v1.Invocation.EnvironmentEntry + nil, // 48: atmos.plugin.v1.ConfigureRequest.BrokerIdsEntry + nil, // 49: atmos.plugin.v1.Interaction.FieldsEntry + nil, // 50: atmos.plugin.v1.EnvironmentResponse.EnvironmentEntry + nil, // 51: atmos.plugin.v1.PrepareEnvironmentRequest.EnvironmentEntry + nil, // 52: atmos.plugin.v1.IdentityPrepareRequest.EnvironmentEntry + nil, // 53: atmos.plugin.v1.ComponentOperation.OptionsEntry + nil, // 54: atmos.plugin.v1.ComponentExtraArguments.EnvironmentEntry + (*timestamppb.Timestamp)(nil), // 55: google.protobuf.Timestamp + (*structpb.Struct)(nil), // 56: google.protobuf.Struct + (*emptypb.Empty)(nil), // 57: google.protobuf.Empty } ) var file_atmos_plugin_v1_plugin_proto_depIdxs = []int32{ - 51, // 0: atmos.plugin.v1.Invocation.deadline:type_name -> google.protobuf.Timestamp - 44, // 1: atmos.plugin.v1.Invocation.environment:type_name -> atmos.plugin.v1.Invocation.EnvironmentEntry - 51, // 2: atmos.plugin.v1.Credentials.expires_at:type_name -> google.protobuf.Timestamp - 52, // 3: atmos.plugin.v1.Credentials.data:type_name -> google.protobuf.Struct + 55, // 0: atmos.plugin.v1.Invocation.deadline:type_name -> google.protobuf.Timestamp + 47, // 1: atmos.plugin.v1.Invocation.environment:type_name -> atmos.plugin.v1.Invocation.EnvironmentEntry + 55, // 2: atmos.plugin.v1.Credentials.expires_at:type_name -> google.protobuf.Timestamp + 56, // 3: atmos.plugin.v1.Credentials.data:type_name -> google.protobuf.Struct 4, // 4: atmos.plugin.v1.DescribeRequest.host_supported:type_name -> atmos.plugin.v1.ServiceVersionRange 4, // 5: atmos.plugin.v1.DescribeResponse.services:type_name -> atmos.plugin.v1.ServiceVersionRange 5, // 6: atmos.plugin.v1.DescribeResponse.exports:type_name -> atmos.plugin.v1.Export - 45, // 7: atmos.plugin.v1.ConfigureRequest.broker_ids:type_name -> atmos.plugin.v1.ConfigureRequest.BrokerIdsEntry - 52, // 8: atmos.plugin.v1.ConfigureRequest.settings:type_name -> google.protobuf.Struct + 48, // 7: atmos.plugin.v1.ConfigureRequest.broker_ids:type_name -> atmos.plugin.v1.ConfigureRequest.BrokerIdsEntry + 56, // 8: atmos.plugin.v1.ConfigureRequest.settings:type_name -> google.protobuf.Struct 2, // 9: atmos.plugin.v1.ProviderRequest.invocation:type_name -> atmos.plugin.v1.Invocation - 52, // 10: atmos.plugin.v1.ProviderRequest.spec:type_name -> google.protobuf.Struct + 56, // 10: atmos.plugin.v1.ProviderRequest.spec:type_name -> google.protobuf.Struct 2, // 11: atmos.plugin.v1.IdentityRequest.invocation:type_name -> atmos.plugin.v1.Invocation - 52, // 12: atmos.plugin.v1.IdentityRequest.spec:type_name -> google.protobuf.Struct - 52, // 13: atmos.plugin.v1.IdentityRequest.principal:type_name -> google.protobuf.Struct - 46, // 14: atmos.plugin.v1.Interaction.fields:type_name -> atmos.plugin.v1.Interaction.FieldsEntry + 56, // 12: atmos.plugin.v1.IdentityRequest.spec:type_name -> google.protobuf.Struct + 56, // 13: atmos.plugin.v1.IdentityRequest.principal:type_name -> google.protobuf.Struct + 49, // 14: atmos.plugin.v1.Interaction.fields:type_name -> atmos.plugin.v1.Interaction.FieldsEntry 11, // 15: atmos.plugin.v1.AuthenticateEvent.progress:type_name -> atmos.plugin.v1.Progress 12, // 16: atmos.plugin.v1.AuthenticateEvent.interaction:type_name -> atmos.plugin.v1.Interaction 3, // 17: atmos.plugin.v1.AuthenticateEvent.result:type_name -> atmos.plugin.v1.Credentials @@ -3347,16 +3648,16 @@ var file_atmos_plugin_v1_plugin_proto_depIdxs = []int32{ 3, // 20: atmos.plugin.v1.RefreshRequest.current:type_name -> atmos.plugin.v1.Credentials 3, // 21: atmos.plugin.v1.RefreshResponse.credentials:type_name -> atmos.plugin.v1.Credentials 16, // 22: atmos.plugin.v1.RefreshResponse.unsupported:type_name -> atmos.plugin.v1.Unsupported - 47, // 23: atmos.plugin.v1.EnvironmentResponse.environment:type_name -> atmos.plugin.v1.EnvironmentResponse.EnvironmentEntry + 50, // 23: atmos.plugin.v1.EnvironmentResponse.environment:type_name -> atmos.plugin.v1.EnvironmentResponse.EnvironmentEntry 9, // 24: atmos.plugin.v1.PrepareEnvironmentRequest.provider:type_name -> atmos.plugin.v1.ProviderRequest - 48, // 25: atmos.plugin.v1.PrepareEnvironmentRequest.environment:type_name -> atmos.plugin.v1.PrepareEnvironmentRequest.EnvironmentEntry + 51, // 25: atmos.plugin.v1.PrepareEnvironmentRequest.environment:type_name -> atmos.plugin.v1.PrepareEnvironmentRequest.EnvironmentEntry 21, // 26: atmos.plugin.v1.PathsResponse.paths:type_name -> atmos.plugin.v1.CredentialPath 10, // 27: atmos.plugin.v1.IdentityAuthenticateRequest.identity:type_name -> atmos.plugin.v1.IdentityRequest 3, // 28: atmos.plugin.v1.IdentityAuthenticateRequest.upstream:type_name -> atmos.plugin.v1.Credentials 10, // 29: atmos.plugin.v1.IdentityPrepareRequest.identity:type_name -> atmos.plugin.v1.IdentityRequest - 49, // 30: atmos.plugin.v1.IdentityPrepareRequest.environment:type_name -> atmos.plugin.v1.IdentityPrepareRequest.EnvironmentEntry + 52, // 30: atmos.plugin.v1.IdentityPrepareRequest.environment:type_name -> atmos.plugin.v1.IdentityPrepareRequest.EnvironmentEntry 2, // 31: atmos.plugin.v1.CommandRequest.invocation:type_name -> atmos.plugin.v1.Invocation - 52, // 32: atmos.plugin.v1.CommandRequest.settings:type_name -> google.protobuf.Struct + 56, // 32: atmos.plugin.v1.CommandRequest.settings:type_name -> google.protobuf.Struct 26, // 33: atmos.plugin.v1.CommandEvent.exit:type_name -> atmos.plugin.v1.CommandExit 29, // 34: atmos.plugin.v1.ComponentValue.sensitive_paths:type_name -> atmos.plugin.v1.ComponentValuePath 30, // 35: atmos.plugin.v1.ComponentValuePath.steps:type_name -> atmos.plugin.v1.ComponentValueStep @@ -3368,7 +3669,7 @@ var file_atmos_plugin_v1_plugin_proto_depIdxs = []int32{ 28, // 41: atmos.plugin.v1.DiscoveredComponent.configuration:type_name -> atmos.plugin.v1.ComponentValue 32, // 42: atmos.plugin.v1.DiscoveredComponent.sources:type_name -> atmos.plugin.v1.ComponentSource 33, // 43: atmos.plugin.v1.DiscoveredComponent.dependencies:type_name -> atmos.plugin.v1.ComponentDependency - 50, // 44: atmos.plugin.v1.ComponentOperation.options:type_name -> atmos.plugin.v1.ComponentOperation.OptionsEntry + 53, // 44: atmos.plugin.v1.ComponentOperation.options:type_name -> atmos.plugin.v1.ComponentOperation.OptionsEntry 2, // 45: atmos.plugin.v1.DiscoverComponentsRequest.invocation:type_name -> atmos.plugin.v1.Invocation 36, // 46: atmos.plugin.v1.DiscoverComponentsRequest.operation:type_name -> atmos.plugin.v1.ComponentOperation 35, // 47: atmos.plugin.v1.DiscoverComponentsRequest.effects:type_name -> atmos.plugin.v1.ComponentEffects @@ -3382,61 +3683,67 @@ var file_atmos_plugin_v1_plugin_proto_depIdxs = []int32{ 28, // 55: atmos.plugin.v1.ComponentRequest.configuration:type_name -> atmos.plugin.v1.ComponentValue 28, // 56: atmos.plugin.v1.ComponentRequest.provider_configuration:type_name -> atmos.plugin.v1.ComponentValue 28, // 57: atmos.plugin.v1.ComponentStateIdentity.backend:type_name -> atmos.plugin.v1.ComponentValue - 28, // 58: atmos.plugin.v1.PrepareComponentResponse.inputs:type_name -> atmos.plugin.v1.ComponentValue - 40, // 59: atmos.plugin.v1.PrepareComponentResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity - 41, // 60: atmos.plugin.v1.PrepareComponentResponse.files:type_name -> atmos.plugin.v1.ComponentGeneratedFile - 33, // 61: atmos.plugin.v1.PrepareComponentResponse.dependencies:type_name -> atmos.plugin.v1.ComponentDependency - 32, // 62: atmos.plugin.v1.PrepareComponentResponse.sources:type_name -> atmos.plugin.v1.ComponentSource - 14, // 63: atmos.plugin.v1.PrepareComponentResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic - 40, // 64: atmos.plugin.v1.ResolveComponentStateResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity - 32, // 65: atmos.plugin.v1.ResolveComponentStateResponse.sources:type_name -> atmos.plugin.v1.ComponentSource - 14, // 66: atmos.plugin.v1.ResolveComponentStateResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic - 28, // 67: atmos.plugin.v1.ComponentOperation.OptionsEntry.value:type_name -> atmos.plugin.v1.ComponentValue - 6, // 68: atmos.plugin.v1.Info.Describe:input_type -> atmos.plugin.v1.DescribeRequest - 8, // 69: atmos.plugin.v1.Info.Configure:input_type -> atmos.plugin.v1.ConfigureRequest - 9, // 70: atmos.plugin.v1.AuthProvider.Validate:input_type -> atmos.plugin.v1.ProviderRequest - 9, // 71: atmos.plugin.v1.AuthProvider.Authenticate:input_type -> atmos.plugin.v1.ProviderRequest - 17, // 72: atmos.plugin.v1.AuthProvider.Refresh:input_type -> atmos.plugin.v1.RefreshRequest - 9, // 73: atmos.plugin.v1.AuthProvider.Logout:input_type -> atmos.plugin.v1.ProviderRequest - 9, // 74: atmos.plugin.v1.AuthProvider.Environment:input_type -> atmos.plugin.v1.ProviderRequest - 9, // 75: atmos.plugin.v1.AuthProvider.Paths:input_type -> atmos.plugin.v1.ProviderRequest - 20, // 76: atmos.plugin.v1.AuthProvider.PrepareEnvironment:input_type -> atmos.plugin.v1.PrepareEnvironmentRequest - 10, // 77: atmos.plugin.v1.AuthIdentity.Validate:input_type -> atmos.plugin.v1.IdentityRequest - 23, // 78: atmos.plugin.v1.AuthIdentity.Authenticate:input_type -> atmos.plugin.v1.IdentityAuthenticateRequest - 10, // 79: atmos.plugin.v1.AuthIdentity.Logout:input_type -> atmos.plugin.v1.IdentityRequest - 10, // 80: atmos.plugin.v1.AuthIdentity.Environment:input_type -> atmos.plugin.v1.IdentityRequest - 10, // 81: atmos.plugin.v1.AuthIdentity.Paths:input_type -> atmos.plugin.v1.IdentityRequest - 24, // 82: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:input_type -> atmos.plugin.v1.IdentityPrepareRequest - 25, // 83: atmos.plugin.v1.Commands.Run:input_type -> atmos.plugin.v1.CommandRequest - 37, // 84: atmos.plugin.v1.ComponentDiscovery.Discover:input_type -> atmos.plugin.v1.DiscoverComponentsRequest - 39, // 85: atmos.plugin.v1.ComponentValidation.Validate:input_type -> atmos.plugin.v1.ComponentRequest - 39, // 86: atmos.plugin.v1.ComponentPreparation.Prepare:input_type -> atmos.plugin.v1.ComponentRequest - 39, // 87: atmos.plugin.v1.ComponentState.Resolve:input_type -> atmos.plugin.v1.ComponentRequest - 7, // 88: atmos.plugin.v1.Info.Describe:output_type -> atmos.plugin.v1.DescribeResponse - 53, // 89: atmos.plugin.v1.Info.Configure:output_type -> google.protobuf.Empty - 15, // 90: atmos.plugin.v1.AuthProvider.Validate:output_type -> atmos.plugin.v1.ValidateResponse - 13, // 91: atmos.plugin.v1.AuthProvider.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent - 18, // 92: atmos.plugin.v1.AuthProvider.Refresh:output_type -> atmos.plugin.v1.RefreshResponse - 53, // 93: atmos.plugin.v1.AuthProvider.Logout:output_type -> google.protobuf.Empty - 19, // 94: atmos.plugin.v1.AuthProvider.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse - 22, // 95: atmos.plugin.v1.AuthProvider.Paths:output_type -> atmos.plugin.v1.PathsResponse - 19, // 96: atmos.plugin.v1.AuthProvider.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse - 15, // 97: atmos.plugin.v1.AuthIdentity.Validate:output_type -> atmos.plugin.v1.ValidateResponse - 13, // 98: atmos.plugin.v1.AuthIdentity.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent - 53, // 99: atmos.plugin.v1.AuthIdentity.Logout:output_type -> google.protobuf.Empty - 19, // 100: atmos.plugin.v1.AuthIdentity.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse - 22, // 101: atmos.plugin.v1.AuthIdentity.Paths:output_type -> atmos.plugin.v1.PathsResponse - 19, // 102: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse - 27, // 103: atmos.plugin.v1.Commands.Run:output_type -> atmos.plugin.v1.CommandEvent - 38, // 104: atmos.plugin.v1.ComponentDiscovery.Discover:output_type -> atmos.plugin.v1.DiscoverComponentsResponse - 15, // 105: atmos.plugin.v1.ComponentValidation.Validate:output_type -> atmos.plugin.v1.ValidateResponse - 42, // 106: atmos.plugin.v1.ComponentPreparation.Prepare:output_type -> atmos.plugin.v1.PrepareComponentResponse - 43, // 107: atmos.plugin.v1.ComponentState.Resolve:output_type -> atmos.plugin.v1.ResolveComponentStateResponse - 88, // [88:108] is the sub-list for method output_type - 68, // [68:88] is the sub-list for method input_type - 68, // [68:68] is the sub-list for extension type_name - 68, // [68:68] is the sub-list for extension extendee - 0, // [0:68] is the sub-list for field type_name + 43, // 58: atmos.plugin.v1.ComponentExecutionConfiguration.extra_arguments:type_name -> atmos.plugin.v1.ComponentExtraArguments + 44, // 59: atmos.plugin.v1.ComponentExecutionConfiguration.before_hooks:type_name -> atmos.plugin.v1.ComponentHook + 44, // 60: atmos.plugin.v1.ComponentExecutionConfiguration.after_hooks:type_name -> atmos.plugin.v1.ComponentHook + 44, // 61: atmos.plugin.v1.ComponentExecutionConfiguration.error_hooks:type_name -> atmos.plugin.v1.ComponentHook + 54, // 62: atmos.plugin.v1.ComponentExtraArguments.environment:type_name -> atmos.plugin.v1.ComponentExtraArguments.EnvironmentEntry + 28, // 63: atmos.plugin.v1.PrepareComponentResponse.inputs:type_name -> atmos.plugin.v1.ComponentValue + 40, // 64: atmos.plugin.v1.PrepareComponentResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity + 41, // 65: atmos.plugin.v1.PrepareComponentResponse.files:type_name -> atmos.plugin.v1.ComponentGeneratedFile + 33, // 66: atmos.plugin.v1.PrepareComponentResponse.dependencies:type_name -> atmos.plugin.v1.ComponentDependency + 32, // 67: atmos.plugin.v1.PrepareComponentResponse.sources:type_name -> atmos.plugin.v1.ComponentSource + 14, // 68: atmos.plugin.v1.PrepareComponentResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic + 42, // 69: atmos.plugin.v1.PrepareComponentResponse.execution:type_name -> atmos.plugin.v1.ComponentExecutionConfiguration + 40, // 70: atmos.plugin.v1.ResolveComponentStateResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity + 32, // 71: atmos.plugin.v1.ResolveComponentStateResponse.sources:type_name -> atmos.plugin.v1.ComponentSource + 14, // 72: atmos.plugin.v1.ResolveComponentStateResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic + 28, // 73: atmos.plugin.v1.ComponentOperation.OptionsEntry.value:type_name -> atmos.plugin.v1.ComponentValue + 6, // 74: atmos.plugin.v1.Info.Describe:input_type -> atmos.plugin.v1.DescribeRequest + 8, // 75: atmos.plugin.v1.Info.Configure:input_type -> atmos.plugin.v1.ConfigureRequest + 9, // 76: atmos.plugin.v1.AuthProvider.Validate:input_type -> atmos.plugin.v1.ProviderRequest + 9, // 77: atmos.plugin.v1.AuthProvider.Authenticate:input_type -> atmos.plugin.v1.ProviderRequest + 17, // 78: atmos.plugin.v1.AuthProvider.Refresh:input_type -> atmos.plugin.v1.RefreshRequest + 9, // 79: atmos.plugin.v1.AuthProvider.Logout:input_type -> atmos.plugin.v1.ProviderRequest + 9, // 80: atmos.plugin.v1.AuthProvider.Environment:input_type -> atmos.plugin.v1.ProviderRequest + 9, // 81: atmos.plugin.v1.AuthProvider.Paths:input_type -> atmos.plugin.v1.ProviderRequest + 20, // 82: atmos.plugin.v1.AuthProvider.PrepareEnvironment:input_type -> atmos.plugin.v1.PrepareEnvironmentRequest + 10, // 83: atmos.plugin.v1.AuthIdentity.Validate:input_type -> atmos.plugin.v1.IdentityRequest + 23, // 84: atmos.plugin.v1.AuthIdentity.Authenticate:input_type -> atmos.plugin.v1.IdentityAuthenticateRequest + 10, // 85: atmos.plugin.v1.AuthIdentity.Logout:input_type -> atmos.plugin.v1.IdentityRequest + 10, // 86: atmos.plugin.v1.AuthIdentity.Environment:input_type -> atmos.plugin.v1.IdentityRequest + 10, // 87: atmos.plugin.v1.AuthIdentity.Paths:input_type -> atmos.plugin.v1.IdentityRequest + 24, // 88: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:input_type -> atmos.plugin.v1.IdentityPrepareRequest + 25, // 89: atmos.plugin.v1.Commands.Run:input_type -> atmos.plugin.v1.CommandRequest + 37, // 90: atmos.plugin.v1.ComponentDiscovery.Discover:input_type -> atmos.plugin.v1.DiscoverComponentsRequest + 39, // 91: atmos.plugin.v1.ComponentValidation.Validate:input_type -> atmos.plugin.v1.ComponentRequest + 39, // 92: atmos.plugin.v1.ComponentPreparation.Prepare:input_type -> atmos.plugin.v1.ComponentRequest + 39, // 93: atmos.plugin.v1.ComponentState.Resolve:input_type -> atmos.plugin.v1.ComponentRequest + 7, // 94: atmos.plugin.v1.Info.Describe:output_type -> atmos.plugin.v1.DescribeResponse + 57, // 95: atmos.plugin.v1.Info.Configure:output_type -> google.protobuf.Empty + 15, // 96: atmos.plugin.v1.AuthProvider.Validate:output_type -> atmos.plugin.v1.ValidateResponse + 13, // 97: atmos.plugin.v1.AuthProvider.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent + 18, // 98: atmos.plugin.v1.AuthProvider.Refresh:output_type -> atmos.plugin.v1.RefreshResponse + 57, // 99: atmos.plugin.v1.AuthProvider.Logout:output_type -> google.protobuf.Empty + 19, // 100: atmos.plugin.v1.AuthProvider.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse + 22, // 101: atmos.plugin.v1.AuthProvider.Paths:output_type -> atmos.plugin.v1.PathsResponse + 19, // 102: atmos.plugin.v1.AuthProvider.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse + 15, // 103: atmos.plugin.v1.AuthIdentity.Validate:output_type -> atmos.plugin.v1.ValidateResponse + 13, // 104: atmos.plugin.v1.AuthIdentity.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent + 57, // 105: atmos.plugin.v1.AuthIdentity.Logout:output_type -> google.protobuf.Empty + 19, // 106: atmos.plugin.v1.AuthIdentity.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse + 22, // 107: atmos.plugin.v1.AuthIdentity.Paths:output_type -> atmos.plugin.v1.PathsResponse + 19, // 108: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse + 27, // 109: atmos.plugin.v1.Commands.Run:output_type -> atmos.plugin.v1.CommandEvent + 38, // 110: atmos.plugin.v1.ComponentDiscovery.Discover:output_type -> atmos.plugin.v1.DiscoverComponentsResponse + 15, // 111: atmos.plugin.v1.ComponentValidation.Validate:output_type -> atmos.plugin.v1.ValidateResponse + 45, // 112: atmos.plugin.v1.ComponentPreparation.Prepare:output_type -> atmos.plugin.v1.PrepareComponentResponse + 46, // 113: atmos.plugin.v1.ComponentState.Resolve:output_type -> atmos.plugin.v1.ResolveComponentStateResponse + 94, // [94:114] is the sub-list for method output_type + 74, // [74:94] is the sub-list for method input_type + 74, // [74:74] is the sub-list for extension type_name + 74, // [74:74] is the sub-list for extension extendee + 0, // [0:74] is the sub-list for field type_name } func init() { file_atmos_plugin_v1_plugin_proto_init() } @@ -3468,7 +3775,7 @@ func file_atmos_plugin_v1_plugin_proto_init() { GoPackagePath: reflect.TypeOf(x{}).PkgPath(), RawDescriptor: unsafe.Slice(unsafe.StringData(file_atmos_plugin_v1_plugin_proto_rawDesc), len(file_atmos_plugin_v1_plugin_proto_rawDesc)), NumEnums: 2, - NumMessages: 49, + NumMessages: 53, NumExtensions: 0, NumServices: 8, }, diff --git a/website/blog/2026-10-29-native-terragrunt-compatibility.mdx b/website/blog/2026-10-29-native-terragrunt-compatibility.mdx new file mode 100644 index 00000000000..514727c715a --- /dev/null +++ b/website/blog/2026-10-29-native-terragrunt-compatibility.mdx @@ -0,0 +1,98 @@ +--- +slug: native-terragrunt-compatibility +title: "Native Terragrunt Integration Beyond Discovery" +authors: [osterman] +tags: [feature, experimental] +cast: "/casts/demo/fixtures/terragrunt-native/plan.cast" +castTitle: "Native Terragrunt discovery and planning" +--- +Teams adopting Atmos alongside Terragrunt need more than a list of units. Existing HCL must retain its backend identity, generated-file ownership, and dependencies when Atmos prepares an operation. This experimental extension builds on native discovery with additional host adapters and compatibility checks. + + + +import CastEmbed from '@site/src/components/CastEmbed'; +import TryExample from '@site/src/components/TryExample'; + + + +[View the full example](/examples/terragrunt-native) + +## Try it locally + +The example keeps a Terragrunt unit under `live/`, discovers it as an Atmos Terraform component, and plans a built-in `terraform_data` resource with OpenTofu. It requires no cloud credentials or Terragrunt executable. + +:::note Development build required +The plugin and example are not yet released. Build Atmos and the native plugin from this PR's checkout before running the commands below. The example initializer in the widget uses `main`; until this PR merges, copy the example from your checkout instead: + +```shell +# Run from the Atmos repository checkout for this PR. +go build -o build/atmos . +./build/atmos plugin build plugins/terragrunt +export PATH="$PWD/build:$PATH" +export ATMOS_TERRAGRUNT_PLUGIN_PATH="$PWD/plugins/terragrunt" +atmos init ./examples/terragrunt-native ./terragrunt-native --copy --no-git +cd terragrunt-native +``` + +Install OpenTofu before planning. The recording starts after the host and plugin have been built and the package path configured. +::: + + + +Set the package path to your built plugin, then inspect and plan the discovered unit: + +```shell +atmos config set plugins.packages.terragrunt.path "$ATMOS_TERRAGRUNT_PLUGIN_PATH" +atmos list stacks +atmos list instances +atmos terraform plan demo -s live +``` + +The example already enables native discovery. It stores state locally if you choose to apply it; its README includes apply, output, and cleanup commands. + + + +## Enable compatibility in an existing project + +From your project's root, register the built plugin and opt into native discovery with [config set](/cli/commands/config/config-set): + +```shell +atmos config set plugins.packages.terragrunt.path "$ATMOS_TERRAGRUNT_PLUGIN_PATH" +atmos config set components.terraform.command tofu +atmos config set components.terraform.terragrunt.enabled true +atmos config set components.terraform.terragrunt.mode native +atmos config set --type=yaml components.terraform.terragrunt.discovery.included_paths '["live/**"]' +atmos config set components.terraform.terragrunt.discovery.enabled true +atmos list stacks +atmos list instances +``` + +Adjust `live/**` to your Terragrunt directory layout. Keep your existing Terraform command setting if you use Terraform instead of OpenTofu. Discovery does not require per-unit Atmos YAML. Use the component and stack names from the inventory when running `atmos terraform plan -s `. + +## Inspect the executor + +The default instance table shows `Component`, `Stack`, `Type`, and `Engine`. A native Terragrunt unit remains a Terraform component. Its engine reflects the selected Terraform or OpenTofu executable. + +Existing column overrides remain authoritative. Projects pinned to editions before `2026-10-29` retain the two-column default. + +## Keep package names predictable + +New plugin installations use the product name as their local alias: `cloudposse/terragrunt` becomes `terragrunt`. Existing declarations keep their aliases, and a collision requires an explicit `--as` value. + +Package identity stays separate from source repository and OCI release location. A plugin can move from a monorepo into a standalone repository without changing its consumer identity. + +## Hooks and extra arguments run under Atmos + +Units that rely on `before_hook`, `after_hook`, `error_hook`, or `extra_arguments` no longer need to stay on the Terragrunt executable. The plugin evaluates them, and Atmos runs them with the same identity, environment, and toolchain as the Terraform command, so a tool pinned in `dependencies.tools` is available to a hook even when it is not on your PATH. A hook scoped to `plan` does not run for `apply`, and the implicit `init` matches hooks for `init` rather than for the command you requested. + +## Apply the plan you reviewed + +After `atmos terraform plan`, `atmos terraform apply --from-plan` applies that exact plan. Atmos rejects the apply if the plan file, the unit's configuration, or the working directory changed in between. The original working directory must still exist, so this does not yet cover applying a plan on a different runner. + +## Compatibility is explicit + +Native mode evaluates HCL through Terragrunt Go modules and executes through Atmos. It does not call the Terragrunt executable or silently fall back to it. Unsupported behavior produces an error before infrastructure execution: for example `terraform_binary`, `errors` and `exclude` blocks, feature flags, IAM role settings, and the `read-config` and `init-from-module` hook lifecycles. + +The package remains experimental. Real cloud provisioning, remote-runner plan relocation, and live Pro integration require their own acceptance evidence. Local fixture results do not establish a compatibility percentage. + +See the [Terragrunt migration guide](/migration/terragrunt), [plugin usage](/cli/commands/plugin/usage), and [instance listing](/cli/commands/list/list-instances). diff --git a/website/docs/cli/commands/list/list-instances.mdx b/website/docs/cli/commands/list/list-instances.mdx index bfb88d3e028..3bfa2e6dbf1 100644 --- a/website/docs/cli/commands/list/list-instances.mdx +++ b/website/docs/cli/commands/list/list-instances.mdx @@ -86,6 +86,22 @@ The `atmos list instances` command displays all component instances defined acro
Skip executing a specific YAML function in the Atmos stack manifests when processing stacks. Repeat the flag to skip multiple functions (for example, `--skip terraform.state --skip terraform.output`). Useful with `--upload` to bypass backend-resolving functions while leaving other YAML functions like `!template` enabled.
Environment variable: `ATMOS_SKIP`
+## Component type and engine + +The default columns are `Component`, `Stack`, `Type`, and `Engine`. Terragrunt units +executed natively have type `terraform` and engine `terraform` or `opentofu`, based +on the selected executor. The HCL configuration format does not determine the engine. +Inventory reads the resolved configuration without launching the executor. + +Use `.component_type` and `.engine` in custom column templates. JSON, YAML, CSV, +and TSV output use the selected columns too. Explicit CLI and configuration column +overrides remain authoritative. Editions before `2026-10-29` retain the historical +`Component`, `Stack` defaults. + +```shell +atmos list instances --columns 'Component={{ .component }},Stack={{ .stack }},Type={{ .component_type }},Engine={{ .engine }}' +``` + ## Examples List all instances (rendered as a table by default): diff --git a/website/docs/cli/commands/plugin/install.mdx b/website/docs/cli/commands/plugin/install.mdx index cb50df2a518..ce48353985b 100644 --- a/website/docs/cli/commands/plugin/install.mdx +++ b/website/docs/cli/commands/plugin/install.mdx @@ -105,7 +105,10 @@ for supported variant names and build defaults.
An oci:// repository, or a <registry>/<package> selector for a configured discovery source (repository discovery or a catalog). Omit to restore declarations.
--as
-
Package alias. Defaults to the existing alias bound to the same source, or to the package name. +
Package alias. Defaults to the existing alias bound to the same source, or to the product name + (for example, terragrunt for cloudposse/terragrunt). A collision requires + an explicit alias. Editions before 2026-10-29 retain the publisher-qualified default + cloudposse-terragrunt. Needs a source.
--version
Semantic version constraint, stored verbatim. Without it, a new declaration is pinned with a caret diff --git a/website/docs/cli/commands/plugin/usage.mdx b/website/docs/cli/commands/plugin/usage.mdx index 131d72097cc..5db143895c8 100644 --- a/website/docs/cli/commands/plugin/usage.mdx +++ b/website/docs/cli/commands/plugin/usage.mdx @@ -151,9 +151,10 @@ use `publisher/package` to identify an integration. For example, The `plugins.packages.` key is a project-local name. Use [`plugin install --as terragrunt`](/cli/commands/plugin/install) to select the -product alias. The current installer derives `cloudposse-terragrunt` from the full -package ID when no alias is supplied for a new declaration. Short product aliases -are the naming convention; automatic selection of that default is planned. +product alias explicitly. New declarations default to the product name. Editions +before `2026-10-29` retain the publisher-qualified default, such as +`cloudposse-terragrunt`. Existing declarations keep their aliases across upgrades. +A collision with another source requires an explicit alias. Changing it does not change the package ID or the plugin's exported capability names. Plugin aliases and toolchain aliases belong to separate configuration sections. diff --git a/website/docs/migration/terragrunt.mdx b/website/docs/migration/terragrunt.mdx index 0cf34a357d8..c164793daeb 100644 --- a/website/docs/migration/terragrunt.mdx +++ b/website/docs/migration/terragrunt.mdx @@ -880,6 +880,70 @@ See [YAML Functions](/functions/yaml) for the complete reference. +## Adopt Terragrunt Units Incrementally (Experimental) + +You do not have to convert every `terragrunt.hcl` before Atmos is useful. An experimental plugin reads your existing Terragrunt HCL and presents each unit as an Atmos Terraform component, so you can list, plan, apply, and target them with Atmos while you migrate the rest. The plugin evaluates the HCL with Terragrunt's Go modules. Atmos runs every command, so Atmos authentication, the toolchain, and `dependencies.tools` apply. The Terragrunt executable is never used. + +:::note Development build +The plugin is not yet published. Until it is, build it from the Atmos repository and point `plugins.packages` at its directory with `path`. See the [plugin README](https://github.com/cloudposse/atmos/blob/main/plugins/terragrunt/README.md) for the build commands. +::: + + +```yaml +components: + terraform: + base_path: components/terraform + command: tofu + terragrunt: + enabled: true + mode: native + discovery: + enabled: true + included_paths: [live/**] +plugins: + packages: + terragrunt: + path: /absolute/path/to/atmos/plugins/terragrunt +``` + + +Then work with the units as components: + +```shell +atmos list instances +atmos terraform plan --all +atmos terraform apply --all +atmos describe affected --base origin/main +``` + +To pull a unit into an existing stack file instead of discovering it automatically, import it: + +```yaml +import: + - terragrunt://live/dev/vpc/terragrunt.hcl +``` + +### What Atmos runs for you + +- `dependency` and `dependencies` blocks become ordering and output edges, and dependency outputs are refreshed in the same apply. +- `extra_arguments` and `before_hook`, `after_hook`, and `error_hook` blocks run with the same environment, identity, and toolchain as the Terraform command. A hook that lists `plan` does not run for `apply`. The implicit `init` matches hooks for `init`, not for the requested command. +- Generated files and backend configuration keep their Terragrunt identity, so existing state stays addressable. +- `atmos terraform plan` followed by `atmos terraform apply --from-plan` applies exactly the reviewed plan. Atmos rejects the apply if the plan file, the unit's configuration, or the working directory changed after the plan. The original working directory must still exist. +- Remote Git and HTTPS module sources are fetched by Atmos. Local module sources are re-copied on every run so edits take effect. +- `!terraform.output` and `!terraform.state` can read a Terragrunt unit like any other component. + +### What it does not support yet + +Unsupported settings fail before any infrastructure command runs. Atmos does not fall back to the Terragrunt executable. Currently rejected: + +- `terraform_binary`, `errors`, `exclude`, feature flags, and the `iam_role` and web-identity settings. +- `remote_state` with `disable_init` or encryption. Atmos does not create backend resources. +- Inline modules without `terraform.source`, and registry module sources. +- Hooks for the `read-config`, `terragrunt-read-config`, and `init-from-module` lifecycles, and hooks that run a built-in integration such as `tflint`. +- Output dependencies in a non-default workspace during discovery. + +Discovery runs without side effects, so a unit whose `locals` call `run_cmd` cannot be discovered. When a unit uses one of these features, keep it on Terragrunt for now or convert it with the steps below. + ## Migration Steps ### Step 1: Convert terragrunt.hcl to Stack YAML diff --git a/website/src/data/roadmap.js b/website/src/data/roadmap.js index 64c7ca41dbe..800a9f6b36e 100644 --- a/website/src/data/roadmap.js +++ b/website/src/data/roadmap.js @@ -508,7 +508,8 @@ export const roadmapConfig = { progress: 84, status: 'in-progress', milestones: [ - { label: 'Native Terragrunt unit and stack discovery', status: 'in-progress', quarter: 'q4-2026', experimental: true, pr: 3571, prd: 'terragrunt-support', changelog: 'native-terragrunt-discovery', description: 'Opt into local HCL discovery and native Terraform execution with preserved dependency ordering, state paths, scoped environments, and isolated workdirs. Published installation, affected selection, and broader compatibility remain in progress.' }, + { label: 'Native Terragrunt state and compatibility adapters', status: 'in-progress', quarter: 'q4-2026', experimental: true, pr: 3576, prd: 'terragrunt-support', changelog: 'native-terragrunt-compatibility', description: 'Extend native HCL adoption with hooks and extra arguments run under Atmos, state-function dispatch, reviewed-plan bindings, explicit imports, file provenance, backend and generation semantics, and installed-package acceptance.' }, + { label: 'Native Terragrunt unit and stack discovery', status: 'in-progress', quarter: 'q4-2026', experimental: true, pr: 3571, prd: 'terragrunt-support', changelog: 'native-terragrunt-discovery', description: 'Opt into local HCL discovery and native Terraform execution with preserved dependency ordering, state paths, scoped environments, and isolated workdirs. Revision-aware affected selection is implemented; published installation and broader compatibility remain in progress.' }, { label: 'Configurable concurrent YAML exec evaluation', status: 'shipped', quarter: 'q4-2026', changelog: 'parallel-yaml-exec', docs: '/cli/configuration/settings/functions/exec', description: 'Run ordinary !exec expressions concurrently within a document evaluation with a configurable limit, preserving result placement and serial execution by default.', benefits: 'Reduce time spent waiting on independent configuration lookups without new tags or grouping syntax.' }, { label: 'Native Go authentication plugins and OIDC starter', status: 'in-progress', quarter: 'q4-2026', experimental: true, changelog: 'native-auth-plugins', docs: '/cli/commands/plugin/usage', description: 'Scaffold and test a Go OIDC provider, chain its broker credentials into built-in identities, and distribute compiled plugins through OCI with explicit installation and release locks.' }, { label: 'Official public plugin catalog and secret-store authentication', status: 'in-progress', quarter: 'q4-2026', experimental: true, changelog: 'official-auth-secrets-plugins', docs: '/cli/configuration/plugins#official-secret-store-authentication', description: 'Discover official plugins through the default Cloud Posse OCI catalog and use auth-secrets to read explicitly configured secret-store credentials into an environment identity. Plugin manifests control independent package versions, with native platform validation and public OCI publication before catalog updates.', benefits: 'Install official plugins without configuring a registry, map selected secret values into child-process environment variables, and update individual plugins independently of Atmos releases.' }, diff --git a/website/static/casts/demo/fixtures/terragrunt-native/plan.cast b/website/static/casts/demo/fixtures/terragrunt-native/plan.cast new file mode 100644 index 00000000000..393875f6a5e --- /dev/null +++ b/website/static/casts/demo/fixtures/terragrunt-native/plan.cast @@ -0,0 +1,601 @@ +{"version":3,"term":{"cols":120,"rows":36,"type":"xterm-256color"},"timestamp":1793389887,"title":"Native Terragrunt discovery and planning","env":{"COLORTERM":"truecolor","SHELL":"/bin/zsh"}} +[0.001497917,"o","\u001b[1;38;2;0;163;224m\u003e \u001b[0m\u001b[?25h"] +[0.351139083,"o","\u001b[38;2;73;85;104m"] +[0.040848542,"o","#"] +[0.044921417,"o"," "] +[0.053984083,"o","T"] +[0.035938542,"o","h"] +[0.037847125,"o","e"] +[0.041917541,"o"," "] +[0.101443959,"o","n"] +[0.035286,"o","a"] +[0.034851666,"o","t"] +[0.038780417,"o","i"] +[0.038653333,"o","v"] +[0.034587792,"o","e"] +[0.046775875,"o"," "] +[0.08146625,"o","p"] +[0.035653958,"o","l"] +[0.031018125,"o","u"] +[0.043767459,"o","g"] +[0.039695833,"o","i"] +[0.044658125,"o","n"] +[0.040599833,"o"," "] +[0.055156042,"o","i"] +[0.037239042,"o","s"] +[0.041300791,"o"," "] +[0.08974675,"o","b"] +[0.0302015,"o","u"] +[0.034257542,"o","i"] +[0.03830075,"o","l"] +[0.044853167,"o","t"] +[0.046425125,"o",";"] +[0.04107525,"o"," "] +[0.059532125,"o","e"] +[0.0308565,"o","n"] +[0.046035166,"o","a"] +[0.041962959,"o","b"] +[0.031886041,"o","l"] +[0.047085667,"o","e"] +[0.043039125,"o"," "] +[0.069609042,"o","i"] +[0.048159541,"o","t"] +[0.043438167,"o"," "] +[0.100407625,"o","f"] +[0.033264333,"o","o"] +[0.040735084,"o","r"] +[0.044804041,"o"," "] +[0.091261917,"o","t"] +[0.030649917,"o","h"] +[0.037722125,"o","e"] +[0.041808041,"o"," "] +[0.0819345,"o","e"] +[0.046911042,"o","x"] +[0.031444583,"o","i"] +[0.046648625,"o","s"] +[0.039584959,"o","t"] +[0.035544375,"o","i"] +[0.034479833,"o","n"] +[0.029584708,"o","g"] +[0.04261475,"o"," "] +[0.069373542,"o","l"] +[0.0447535,"o","i"] +[0.04069375,"o","v"] +[0.039746,"o","e"] +[0.044580875,"o","/"] +[0.08098475,"o"," "] +[0.055886208,"o","t"] +[0.043527875,"o","r"] +[0.047311292,"o","e"] +[0.032403333,"o","e"] +[0.03643975,"o","."] +[0.000013959,"o","\u001b[0m"] +[0.551096791,"o","\n"] +[0.00020475,"o","\u001b[1;38;2;0;163;224m\u003e \u001b[0m\u001b[?25h"] +[0.350796667,"o","\u001b[38;2;255;255;255m"] +[0.0427215,"o","a"] +[0.02806,"o","t"] +[0.035322458,"o","m"] +[0.039007209,"o","o"] +[0.039978208,"o","s"] +[0.043687833,"o"," "] +[0.056195709,"o","c"] +[0.036254875,"o","o"] +[0.03724425,"o","n"] +[0.040924875,"o","f"] +[0.039976958,"o","i"] +[0.036292833,"o","g"] +[0.029837709,"o"," "] +[0.089148833,"o","s"] +[0.037241375,"o","e"] +[0.033555917,"o","t"] +[0.044654875,"o"," "] +[0.080329333,"o","c"] +[0.0279235,"o","o"] +[0.041738542,"o","m"] +[0.035026291,"o","p"] +[0.038733709,"o","o"] +[0.042421791,"o","n"] +[0.02862825,"o","e"] +[0.032292875,"o","n"] +[0.035977834,"o","t"] +[0.038737833,"o","s"] +[0.04336875,"o","."] +[0.029544375,"o","t"] +[0.033247,"o","e"] +[0.03673125,"o","r"] +[0.033022,"o","r"] +[0.029325167,"o","a"] +[0.043172125,"o","f"] +[0.034002916,"o","o"] +[0.030299834,"o","r"] +[0.044109541,"o","m"] +[0.039455542,"o","."] +[0.031248208,"o","t"] +[0.027538084,"o","e"] +[0.031878,"o","r"] +[0.035577041,"o","r"] +[0.042011,"o","a"] +[0.028182542,"o","g"] +[0.028566292,"o","r"] +[0.032879708,"o","u"] +[0.039291417,"o","n"] +[0.042980625,"o","t"] +[0.043190416,"o","."] +[0.030150959,"o","e"] +[0.033473625,"o","n"] +[0.029766708,"o","a"] +[0.040852917,"o","b"] +[0.037144833,"o","l"] +[0.0361925,"o","e"] +[0.032482042,"o","d"] +[0.043241875,"o"," "] +[0.07713575,"o","t"] +[0.028015208,"o","r"] +[0.041843125,"o","u"] +[0.041688208,"o","e"] +[0.000012417,"o","\u001b[0m"] +[0.551063,"o","\n"] +[0.225956167,"e","\u001b[38;2;72;187;120m✓\u001b[0m \u001b[38;2;247;250;252mUpdated \u001b[0m\u001b[38;2;0;255;255;1mcomponents.terraform.terragrunt.enabled\u001b[0m\u001b[38;2;247;250;252m to \u001b[0m\u001b[38;2;0;255;255;1mtrue\u001b[0m\u001b[38;2;247;250;252m in \u001b[0m\u001b[38;2;0;255;255;1matmos.yaml\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\n"] +[0.77467475,"o","\u001b[1;38;2;0;163;224m\u003e \u001b[0m\u001b[?25h"] +[0.351140583,"o","\u001b[38;2;255;255;255m"] +[0.040349792,"o","a"] +[0.036654333,"o","t"] +[0.030201792,"o","m"] +[0.044025625,"o","o"] +[0.042815458,"o","s"] +[0.038985458,"o"," "] +[0.057183792,"o","c"] +[0.028833417,"o","o"] +[0.034877125,"o","n"] +[0.03118025,"o","f"] +[0.027517166,"o","i"] +[0.031220542,"o","g"] +[0.034937333,"o"," "] +[0.073272125,"o","s"] +[0.030255542,"o","e"] +[0.033068958,"o","t"] +[0.037159292,"o"," "] +[0.0816665,"o","c"] +[0.032996417,"o","o"] +[0.036699583,"o","m"] +[0.036062583,"o","p"] +[0.033274125,"o","o"] +[0.043659959,"o","n"] +[0.040690291,"o","e"] +[0.039695834,"o","n"] +[0.0360035,"o","t"] +[0.029381666,"o","s"] +[0.043404167,"o","."] +[0.031520792,"o","t"] +[0.027787458,"o","e"] +[0.035261667,"o","r"] +[0.038971041,"o","r"] +[0.027897084,"o","a"] +[0.031583791,"o","f"] +[0.032539417,"o","o"] +[0.03534325,"o","r"] +[0.042655708,"o","m"] +[0.0288595,"o","."] +[0.0298245,"o","t"] +[0.033498334,"o","e"] +[0.040083041,"o","r"] +[0.036035084,"o","r"] +[0.03270275,"o","a"] +[0.029009083,"o","g"] +[0.037213333,"o","r"] +[0.033662542,"o","u"] +[0.029960708,"o","n"] +[0.043788084,"o","t"] +[0.034627916,"o","."] +[0.030270584,"o","m"] +[0.038396875,"o","o"] +[0.042086708,"o","d"] +[0.028318792,"o","e"] +[0.031959708,"o"," "] +[0.064667667,"o","n"] +[0.0393595,"o","a"] +[0.042429083,"o","t"] +[0.029249708,"o","i"] +[0.032933292,"o","v"] +[0.036613917,"o","e"] +[0.000020666,"o","\u001b[0m"] +[0.551066084,"o","\n"] +[0.239351791,"e","\u001b[38;2;72;187;120m✓\u001b[0m \u001b[38;2;247;250;252mUpdated \u001b[0m\u001b[38;2;0;255;255;1mcomponents.terraform.terragrunt.mode\u001b[0m\u001b[38;2;247;250;252m to \u001b[0m\u001b[38;2;0;255;255;1mnative\u001b[0m\u001b[38;2;247;250;252m in \u001b[0m\u001b[38;2;0;255;255;1matmos.yaml\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\n"] +[0.781510542,"o","\u001b[1;38;2;0;163;224m\u003e \u001b[0m\u001b[?25h"] +[0.351121958,"o","\u001b[38;2;255;255;255m"] +[0.039052584,"o","a"] +[0.035338875,"o","t"] +[0.031649791,"o","m"] +[0.027970417,"o","o"] +[0.041779125,"o","s"] +[0.038088583,"o"," "] +[0.061074792,"o","c"] +[0.030688292,"o","o"] +[0.04450225,"o","n"] +[0.040795708,"o","f"] +[0.037513708,"o","i"] +[0.041222792,"o","g"] +[0.029875042,"o"," "] +[0.059557166,"o","s"] +[0.034768292,"o","e"] +[0.038468833,"o","t"] +[0.027385459,"o"," "] +[0.051370625,"o","-"] +[0.04300025,"o","-"] +[0.029163708,"o","t"] +[0.035797375,"o","y"] +[0.032094875,"o","p"] +[0.028403417,"o","e"] +[0.042231583,"o","="] +[0.059393,"o","y"] +[0.034842417,"o","a"] +[0.031137125,"o","m"] +[0.02744575,"o","l"] +[0.029674375,"o"," "] +[0.090796125,"o","c"] +[0.034260208,"o","o"] +[0.037951042,"o","m"] +[0.041643458,"o","p"] +[0.027826833,"o","o"] +[0.036980292,"o","n"] +[0.040682167,"o","e"] +[0.044400625,"o","n"] +[0.030569958,"o","t"] +[0.028783875,"o","s"] +[0.032475333,"o","."] +[0.032554209,"o","t"] +[0.028858333,"o","e"] +[0.039938333,"o","r"] +[0.035789584,"o","r"] +[0.029817875,"o","a"] +[0.043639416,"o","f"] +[0.037211334,"o","o"] +[0.033519458,"o","r"] +[0.044608042,"o","m"] +[0.040905958,"o","."] +[0.0440285,"o","t"] +[0.029556833,"o","e"] +[0.036691292,"o","r"] +[0.040348333,"o","r"] +[0.029158042,"o","a"] +[0.032932375,"o","g"] +[0.03936225,"o","r"] +[0.043052833,"o","u"] +[0.031960834,"o","n"] +[0.035654458,"o","t"] +[0.042315167,"o","."] +[0.038606625,"o","d"] +[0.034914583,"o","i"] +[0.031212833,"o","s"] +[0.039596667,"o","c"] +[0.035908417,"o","o"] +[0.032205875,"o","v"] +[0.027591833,"o","e"] +[0.030269208,"o","r"] +[0.044089709,"o","y"] +[0.04061325,"o","."] +[0.043354,"o","i"] +[0.03048975,"o","n"] +[0.034177458,"o","c"] +[0.037901792,"o","l"] +[0.041585208,"o","u"] +[0.027765083,"o","d"] +[0.03148325,"o","e"] +[0.0285625,"o","d"] +[0.032253834,"o","_"] +[0.039147458,"o","p"] +[0.035382292,"o","a"] +[0.0289445,"o","t"] +[0.042753791,"o","h"] +[0.041806167,"o","s"] +[0.038111083,"o"," "] +[0.053849292,"o","'"] +[0.027969583,"o","["] +[0.0335845,"o","\""] +[0.029897667,"o","l"] +[0.037349625,"o","i"] +[0.041059667,"o","v"] +[0.029985916,"o","e"] +[0.033715,"o","/"] +[0.050330084,"o","*"] +[0.03831425,"o","*"] +[0.044733166,"o","\""] +[0.030922209,"o","]"] +[0.031925041,"o","'"] +[0.000021792,"o","\u001b[0m"] +[0.551068625,"o","\n"] +[0.229254042,"e","\u001b[38;2;72;187;120m✓\u001b[0m \u001b[38;2;247;250;252mUpdated \u001b[0m\u001b[38;2;0;255;255;1mcomponents.terraform.terragrunt.discovery.included_paths\u001b[0m\u001b[38;2;247;250;252m to \u001b[0m\u001b[38;2;0;255;255;1m[\"live/**\"]\u001b[0m\u001b[38;2;247;250;252m in \u001b[0m\u001b[38;2;0;255;255;1matmos.yaml\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\n"] +[0.775184416,"o","\u001b[1;38;2;0;163;224m\u003e \u001b[0m\u001b[?25h"] +[0.351153709,"o","\u001b[38;2;255;255;255m"] +[0.035361208,"o","a"] +[0.039191542,"o","t"] +[0.028150666,"o","m"] +[0.031817917,"o","o"] +[0.038221583,"o","s"] +[0.04191825,"o"," "] +[0.0516475,"o","c"] +[0.034513334,"o","o"] +[0.030005375,"o","n"] +[0.033704333,"o","f"] +[0.039902958,"o","i"] +[0.036257917,"o","g"] +[0.029780833,"o"," "] +[0.088941875,"o","s"] +[0.04264825,"o","e"] +[0.038945125,"o","t"] +[0.032526209,"o"," "] +[0.04668225,"o","c"] +[0.034463208,"o","o"] +[0.030778625,"o","m"] +[0.030474042,"o","p"] +[0.0338065,"o","o"] +[0.037877875,"o","n"] +[0.041554208,"o","e"] +[0.033195875,"o","n"] +[0.036899542,"o","t"] +[0.040607458,"o","s"] +[0.04401425,"o","."] +[0.035924875,"o","t"] +[0.039635875,"o","e"] +[0.032176583,"o","r"] +[0.027694625,"o","r"] +[0.042283209,"o","a"] +[0.03856725,"o","f"] +[0.034911,"o","o"] +[0.031219166,"o","r"] +[0.0271685,"o","m"] +[0.041342375,"o","."] +[0.037641292,"o","t"] +[0.033936625,"o","e"] +[0.044738667,"o","r"] +[0.030899125,"o","r"] +[0.037377958,"o","a"] +[0.04103625,"o","g"] +[0.02993625,"o","r"] +[0.033637292,"o","u"] +[0.040068416,"o","n"] +[0.043775667,"o","t"] +[0.039278833,"o","."] +[0.042587709,"o","d"] +[0.028773375,"o","i"] +[0.042595,"o","s"] +[0.035374833,"o","c"] +[0.032483375,"o","o"] +[0.043563917,"o","v"] +[0.039876375,"o","e"] +[0.033456291,"o","r"] +[0.029104084,"o","y"] +[0.034243833,"o","."] +[0.030530292,"o","e"] +[0.04042275,"o","n"] +[0.027512125,"o","a"] +[0.031207458,"o","b"] +[0.0348955,"o","l"] +[0.03859825,"o","e"] +[0.042289708,"o","d"] +[0.028459834,"o"," "] +[0.05513,"o","t"] +[0.035857416,"o","r"] +[0.039563959,"o","u"] +[0.043045416,"o","e"] +[0.000025167,"o","\u001b[0m"] +[0.550956167,"o","\n"] +[0.234784083,"e","\u001b[38;2;72;187;120m✓\u001b[0m \u001b[38;2;247;250;252mUpdated \u001b[0m\u001b[38;2;0;255;255;1mcomponents.terraform.terragrunt.discovery.enabled\u001b[0m\u001b[38;2;247;250;252m to \u001b[0m\u001b[38;2;0;255;255;1mtrue\u001b[0m\u001b[38;2;247;250;252m in \u001b[0m\u001b[38;2;0;255;255;1matmos.yaml\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\n"] +[0.775805542,"o","\u001b[1;38;2;0;163;224m\u003e \u001b[0m\u001b[?25h"] +[0.351126958,"o","\u001b[38;2;255;255;255m"] +[0.04246175,"o","a"] +[0.02864225,"o","t"] +[0.032315792,"o","m"] +[0.036007666,"o","o"] +[0.039701625,"o","s"] +[0.043417167,"o"," "] +[0.04855725,"o","l"] +[0.033288417,"o","i"] +[0.036978541,"o","s"] +[0.040674375,"o","t"] +[0.036456459,"o"," "] +[0.056671416,"o","s"] +[0.029051875,"o","t"] +[0.042875,"o","a"] +[0.033718625,"o","c"] +[0.030014084,"o","k"] +[0.04383375,"o","s"] +[0.000016625,"o","\u001b[0m"] +[0.551055125,"o","\n"] +[0.290875458,"e","\u001b[38;2;236;201;75m⚠\u001b[0m \u001b[38;2;247;250;252mPlugin \u001b[0m\u001b[38;2;0;255;255;1mterragrunt\u001b[0m\u001b[38;2;247;250;252m runs from local path \u001b[0m\u001b[38;2;0;255;255;1m[LOCAL_PLUGIN]\u001b[0m\u001b[38;2;247;250;252m; lock,\u001b[0m\u001b[0m\n"] +[0.012,"e"," \u001b[38;2;247;250;252msignature and version checks are\u001b[0m\u001b[38;2;247;250;252m bypassed.\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\n"] +[0.776846583,"o","\n"] +[0.012,"o"," \u001b[1;38;2;72;187;120mStack\u001b[0m\n"] +[0.012,"o","\u001b[38;2;73;85;104m───────\u001b[0m\n"] +[0.012,"o"," live\n"] +[0.012,"o","\n"] +[0.737501584,"o","\u001b[1;38;2;0;163;224m\u003e \u001b[0m\u001b[?25h"] +[0.351119333,"o","\u001b[38;2;255;255;255m"] +[0.039315708,"o","a"] +[0.035603417,"o","t"] +[0.031905958,"o","m"] +[0.0282095,"o","o"] +[0.04124625,"o","s"] +[0.038370125,"o"," "] +[0.061969,"o","l"] +[0.030968875,"o","i"] +[0.044802709,"o","s"] +[0.0410935,"o","t"] +[0.0418765,"o"," "] +[0.044624083,"o","i"] +[0.034300167,"o","n"] +[0.037994583,"o","s"] +[0.038930833,"o","t"] +[0.042830125,"o","a"] +[0.031717,"o","n"] +[0.035410792,"o","c"] +[0.029790625,"o","e"] +[0.03348,"o","s"] +[0.000023625,"o","\u001b[0m"] +[0.551053333,"o","\n"] +[0.342606542,"e","\u001b[38;2;236;201;75m⚠\u001b[0m \u001b[38;2;247;250;252mPlugin \u001b[0m\u001b[38;2;0;255;255;1mterragrunt\u001b[0m\u001b[38;2;247;250;252m runs from local path \u001b[0m\u001b[38;2;0;255;255;1m[LOCAL_PLUGIN]\u001b[0m\u001b[38;2;247;250;252m; lock,\u001b[0m\u001b[0m\n"] +[0.012,"e"," \u001b[38;2;247;250;252msignature and version checks are\u001b[0m\u001b[38;2;247;250;252m bypassed.\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\n"] +[0.026077167,"o","\n"] +[0.012,"o"," \u001b[1;38;2;72;187;120mComponent\u001b[0m \u001b[1;38;2;72;187;120mStack\u001b[0m \u001b[1;38;2;72;187;120mType \u001b[0m \u001b[1;38;2;72;187;120mEngine\u001b[0m\n"] +[0.012,"o","\u001b[38;2;73;85;104m───────────\u001b[0m\u001b[38;2;73;85;104m───────\u001b[0m\u001b[38;2;73;85;104m───────────\u001b[0m\u001b[38;2;73;85;104m──────────\u001b[0m\n"] +[0.012,"o"," demo live terraform opentofu\n"] +[0.012,"o","\n"] +[0.734314291,"o","\u001b[1;38;2;0;163;224m\u003e \u001b[0m\u001b[?25h"] +[0.351127292,"o","\u001b[38;2;73;85;104m"] +[0.044575958,"o","#"] +[0.040814959,"o"," "] +[0.056555,"o","H"] +[0.048924458,"o","C"] +[0.047859333,"o","L"] +[0.043795875,"o"," "] +[0.06443025,"o","e"] +[0.03265575,"o","v"] +[0.037933042,"o","a"] +[0.034799708,"o","l"] +[0.042020042,"o","u"] +[0.045385208,"o","a"] +[0.030910459,"o","t"] +[0.034952125,"o","e"] +[0.045037458,"o","s"] +[0.049100125,"o"," "] +[0.056988417,"o","t"] +[0.037966458,"o","h"] +[0.048055458,"o","e"] +[0.032837875,"o"," "] +[0.054111667,"o","g"] +[0.047869958,"o","r"] +[0.041151959,"o","e"] +[0.036102,"o","e"] +[0.035997375,"o","t"] +[0.031924166,"o","i"] +[0.044149959,"o","n"] +[0.040076333,"o","g"] +[0.046249375,"o",";"] +[0.065005667,"o"," "] +[0.050122416,"o","A"] +[0.034221,"o","t"] +[0.042257875,"o","m"] +[0.046330084,"o","o"] +[0.047385625,"o","s"] +[0.032200083,"o"," "] +[0.071433958,"o","r"] +[0.043323167,"o","u"] +[0.044212875,"o","n"] +[0.048266292,"o","s"] +[0.036467083,"o"," "] +[0.053105917,"o","O"] +[0.047569791,"o","p"] +[0.043521959,"o","e"] +[0.033038375,"o","n"] +[0.048632916,"o","T"] +[0.044558209,"o","o"] +[0.040511375,"o","f"] +[0.030030166,"o","u"] +[0.045643459,"o","."] +[0.034729583,"o"," "] +[0.070085458,"o","N"] +[0.042837834,"o","o"] +[0.046920291,"o"," "] +[0.051525834,"o","c"] +[0.035781166,"o","l"] +[0.0398335,"o","o"] +[0.043902959,"o","u"] +[0.0479655,"o","d"] +[0.032367708,"o"," "] +[0.074038,"o","c"] +[0.036103667,"o","r"] +[0.048322625,"o","e"] +[0.044238375,"o","d"] +[0.037148958,"o","e"] +[0.033095542,"o","n"] +[0.045313875,"o","t"] +[0.041244583,"o","i"] +[0.045690458,"o","a"] +[0.041632875,"o","l"] +[0.038311834,"o","s"] +[0.04237475,"o"," "] +[0.047800916,"o","n"] +[0.034227625,"o","e"] +[0.041310875,"o","e"] +[0.045392834,"o","d"] +[0.033163375,"o","e"] +[0.037240416,"o","d"] +[0.032282667,"o","."] +[0.000029417,"o","\u001b[0m"] +[0.551059625,"o","\n"] +[0.000217916,"o","\u001b[1;38;2;0;163;224m\u003e \u001b[0m\u001b[?25h"] +[0.35111525,"o","\u001b[38;2;255;255;255m"] +[0.044660542,"o","a"] +[0.040935958,"o","t"] +[0.034477042,"o","m"] +[0.03083125,"o","o"] +[0.041867833,"o","s"] +[0.038208709,"o"," "] +[0.053062875,"o","t"] +[0.028051125,"o","e"] +[0.032561916,"o","r"] +[0.028782084,"o","r"] +[0.028293916,"o","a"] +[0.032969792,"o","f"] +[0.035949083,"o","o"] +[0.040367084,"o","r"] +[0.044065583,"o","m"] +[0.030243042,"o"," "] +[0.059884916,"o","p"] +[0.037642125,"o","l"] +[0.034732625,"o","a"] +[0.038435417,"o","n"] +[0.038582958,"o"," "] +[0.062473292,"o","d"] +[0.028433167,"o","e"] +[0.041611708,"o","m"] +[0.035850458,"o","o"] +[0.032124209,"o"," "] +[0.087713125,"o","-"] +[0.039531958,"o","s"] +[0.033093542,"o"," "] +[0.047496791,"o","l"] +[0.032533959,"o","i"] +[0.036233083,"o","v"] +[0.042643125,"o","e"] +[0.000034458,"o","\u001b[0m"] +[0.550156875,"o","\n"] +[0.285905,"e","\u001b[38;2;236;201;75m⚠\u001b[0m \u001b[38;2;247;250;252mPlugin \u001b[0m\u001b[38;2;0;255;255;1mterragrunt\u001b[0m\u001b[38;2;247;250;252m runs from local path \u001b[0m\u001b[38;2;0;255;255;1m[LOCAL_PLUGIN]\u001b[0m\u001b[38;2;247;250;252m; lock,\u001b[0m\u001b[0m\n"] +[0.012,"e"," \u001b[38;2;247;250;252msignature and version checks are\u001b[0m\u001b[38;2;247;250;252m bypassed.\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\n"] +[0.142564084,"e","\u001b[38;2;236;201;75m⚠\u001b[0m \u001b[38;2;247;250;252mPlugin \u001b[0m\u001b[38;2;0;255;255;1mterragrunt\u001b[0m\u001b[38;2;247;250;252m runs from local path \u001b[0m\u001b[38;2;0;255;255;1m[LOCAL_PLUGIN]\u001b[0m\u001b[38;2;247;250;252m; lock,\u001b[0m\u001b[0m\n"] +[0.012,"e"," \u001b[38;2;247;250;252msignature and version checks are\u001b[0m\u001b[38;2;247;250;252m bypassed.\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\n"] +[0.020925916,"e","\u001b[38;2;236;201;75m⚠\u001b[0m \u001b[38;2;247;250;252mPlugin \u001b[0m\u001b[38;2;0;255;255;1mterragrunt\u001b[0m\u001b[38;2;247;250;252m runs from local path \u001b[0m\u001b[38;2;0;255;255;1m[LOCAL_PLUGIN]\u001b[0m\u001b[38;2;247;250;252m; lock,\u001b[0m\u001b[0m\n"] +[0.012,"e"," \u001b[38;2;247;250;252msignature and version checks are\u001b[0m\u001b[38;2;247;250;252m bypassed.\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\n"] +[0.061673875,"e","\u001b[?25l\u001b[?2004h\r\r\u001b[K\u001b[38;2;72;187;120m✓\u001b[0m \u001b[38;2;247;250;252mAuto-provisioned source to\u001b[0m\u001b[38;2;247;250;252m .workdir/terraform/live-demo-c32c613e\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\r\n\r\u001b[2K\r"] +[0.018883917,"o","\n"] +[0.012,"o","\u001b[0m\u001b[1mInitializing the backend...\u001b[0m\n"] +[0,"o","\u001b[0m\u001b[32m\n"] +[0.012,"o","Successfully configured the backend \"local\"! OpenTofu will automatically\n"] +[0.012,"o","use this backend unless the backend configuration changes.\u001b[0m\n"] +[0,"o","\n"] +[0.012,"o","\u001b[0m\u001b[1mInitializing provider plugins...\u001b[0m\n"] +[0.012,"o","- terraform.io/builtin/terraform is built in to OpenTofu\n"] +[0.012,"o","\n"] +[0,"o","\u001b[0m\u001b[1m\u001b[32mOpenTofu has been successfully initialized!\u001b[0m\u001b[32m\u001b[0m\n"] +[0,"o","\n"] +[0.012,"o","OpenTofu used the selected providers to generate the following execution\n"] +[0.012,"o","plan. Resource actions are indicated with the following symbols:\n"] +[0.012,"o"," \u001b[32m+\u001b[0m create\u001b[0m\n"] +[0.012,"o","\n"] +[0.012,"o","OpenTofu will perform the following actions:\n"] +[0.012,"o","\n"] +[0.012,"o","\u001b[1m # terraform_data.demo\u001b[0m will be created\n"] +[0.012,"o","\u001b[0m \u001b[32m+\u001b[0m\u001b[0m resource \"terraform_data\" \"demo\" {\n"] +[0.012,"o"," \u001b[32m+\u001b[0m\u001b[0m id = (known after apply)\n"] +[0.012,"o"," \u001b[32m+\u001b[0m\u001b[0m input = \"HELLO FROM TERRAGRUNT\"\n"] +[0.012,"o"," \u001b[32m+\u001b[0m\u001b[0m output = (known after apply)\n"] +[0.012,"o"," }\n"] +[0.012,"o","\n"] +[0.012,"o","\u001b[1mPlan:\u001b[0m 1 to add, 0 to change, 0 to destroy.\n"] +[0.012,"o","\u001b[0m\n"] +[0.012,"o","Changes to Outputs:\n"] +[0.012,"o"," \u001b[32m+\u001b[0m\u001b[0m greeting = (known after apply)\n"] +[0,"e","\u001b[?2004l\u001b[?25h\u001b[?1002l\u001b[?1003l\u001b[?1006l\u001b[38;2;236;201;75m⚠\u001b[0m \u001b[38;2;247;250;252mPlugin \u001b[0m\u001b[38;2;0;255;255;1mterragrunt\u001b[0m\u001b[38;2;247;250;252m runs from local path \u001b[0m\u001b[38;2;0;255;255;1m[LOCAL_PLUGIN]\u001b[0m\u001b[38;2;247;250;252m; lock,\u001b[0m\u001b[0m\n"] +[0.012,"e"," \u001b[38;2;247;250;252msignature and version checks are\u001b[0m\u001b[38;2;247;250;252m bypassed.\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\u001b[0m\n"] +[2.062011875,"o","\u001b[1;38;2;0;163;224m\u003e \u001b[0m\u001b[?25h"] From 791f67a27010b7b043d82284cbe17fa21a833217 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sat, 31 Oct 2026 18:43:25 -0500 Subject: [PATCH 03/52] test(list): refresh list instances snapshots for the Type and Engine columns List instances now shows each component's type and engine by default. Only those two columns are added; the existing rows and columns are unchanged. Regenerated with -regenerate-snapshots. --- ...TestCLICommands_atmos_list_instances.tty.golden | 14 +++++++------- ...mands_atmos_list_instances_no_tty.stdout.golden | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tests/snapshots/TestCLICommands_atmos_list_instances.tty.golden b/tests/snapshots/TestCLICommands_atmos_list_instances.tty.golden index a447b934da8..2c0c37ee837 100644 --- a/tests/snapshots/TestCLICommands_atmos_list_instances.tty.golden +++ b/tests/snapshots/TestCLICommands_atmos_list_instances.tty.golden @@ -1,8 +1,8 @@ ]11;?\ - Component  Stack -──────────────────────── - mock/disabled nonprod - mock/drift nonprod - mock/drift prod - mock/nodrift nonprod - mock/nodrift prod + Component  Stack  Type  Engine +───────────────────────────────────────────── + mock/disabled nonprod terraform opentofu + mock/drift nonprod terraform opentofu + mock/drift prod terraform opentofu + mock/nodrift nonprod terraform opentofu + mock/nodrift prod terraform opentofu diff --git a/tests/snapshots/TestCLICommands_atmos_list_instances_no_tty.stdout.golden b/tests/snapshots/TestCLICommands_atmos_list_instances_no_tty.stdout.golden index 24a232f0ea7..23cad528b55 100644 --- a/tests/snapshots/TestCLICommands_atmos_list_instances_no_tty.stdout.golden +++ b/tests/snapshots/TestCLICommands_atmos_list_instances_no_tty.stdout.golden @@ -1,6 +1,6 @@ -Component,Stack -mock/disabled,nonprod -mock/drift,nonprod -mock/drift,prod -mock/nodrift,nonprod -mock/nodrift,prod +Component,Stack,Type,Engine +mock/disabled,nonprod,terraform,opentofu +mock/drift,nonprod,terraform,opentofu +mock/drift,prod,terraform,opentofu +mock/nodrift,nonprod,terraform,opentofu +mock/nodrift,prod,terraform,opentofu From db714c9ab2b7607f94eff9fd24cc421b22ffdfad Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 1 Nov 2026 07:54:34 -0600 Subject: [PATCH 04/52] fix(terragrunt): repair CI for the compatibility layer - Run the terragrunt-native cast validator from the demo fixtures aggregates. Validators that no aggregate reaches fail the cast checks. - Compare get_terragrunt_dir() results in the engine toolchain test as native paths, which are backslash-separated on Windows. - Compare the resolved executable with the platform's path rules in the provider toolchain test, because Windows resolves the extension as spelled in PATHEXT (.EXE). - The auth-secrets version bump now lives in the lower layer that first changes its build inputs. Co-Authored-By: Claude Sonnet 5.5 --- demo/casts/atmos.d/demo/fixtures/all.yaml | 6 ++++++ .../terragrunt/internal/engine/toolchain_test.go | 5 ++++- .../terragrunt/internal/provider/toolchain_test.go | 14 +++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/demo/casts/atmos.d/demo/fixtures/all.yaml b/demo/casts/atmos.d/demo/fixtures/all.yaml index 507f25037e4..c92a4677ed4 100644 --- a/demo/casts/atmos.d/demo/fixtures/all.yaml +++ b/demo/casts/atmos.d/demo/fixtures/all.yaml @@ -17,6 +17,9 @@ commands: - type: shell name: casts-fixtures-native-terraform-output command: atmos casts generate demo fixtures native-terraform output + - type: shell + name: casts-fixtures-terragrunt-native-plan + command: atmos casts generate demo fixtures terragrunt-native plan - type: shell name: casts-fixtures-demo-vendoring-pull command: atmos casts generate demo fixtures demo-vendoring pull @@ -47,6 +50,9 @@ commands: - type: shell name: casts-fixtures-native-terraform-output command: atmos casts validate demo fixtures native-terraform output + - type: shell + name: casts-fixtures-terragrunt-native-plan + command: atmos casts validate demo fixtures terragrunt-native plan - type: shell name: casts-fixtures-demo-vendoring-pull command: atmos casts validate demo fixtures demo-vendoring pull diff --git a/plugins/terragrunt/internal/engine/toolchain_test.go b/plugins/terragrunt/internal/engine/toolchain_test.go index 92b5924bdfc..b178838b37a 100644 --- a/plugins/terragrunt/internal/engine/toolchain_test.go +++ b/plugins/terragrunt/internal/engine/toolchain_test.go @@ -196,7 +196,10 @@ inputs = { here = local.here, sibling = local.sibling, absolute = local.absolute require.Equal(t, physicalPath(t, want), physicalPath(t, report.Executable), key) require.Equal(t, physicalPath(t, layout.unit), physicalPath(t, report.Directory), "%s must run in the unit directory", key) } - require.Equal(t, filepath.ToSlash(physicalPath(t, layout.unit)), result.Inputs["dir"]) + // get_terragrunt_dir() returns native separators, which are backslashes on Windows. + dir, isString := result.Inputs["dir"].(string) + require.True(t, isString) + require.Equal(t, physicalPath(t, layout.unit), filepath.Clean(filepath.FromSlash(dir))) } // A parent configuration included by the unit evaluates commands in the unit directory. diff --git a/plugins/terragrunt/internal/provider/toolchain_test.go b/plugins/terragrunt/internal/provider/toolchain_test.go index a5e143a6ea0..52366dbfab8 100644 --- a/plugins/terragrunt/internal/provider/toolchain_test.go +++ b/plugins/terragrunt/internal/provider/toolchain_test.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "runtime" + "strings" "testing" "github.com/stretchr/testify/require" @@ -104,7 +105,7 @@ inputs = { for key, value := range inputs["tool"].(map[string]any) { report[key], _ = value.(string) } - require.Equal(t, tool, report["executable"]) + requireSamePath(t, tool, report["executable"]) require.Equal(t, toolDir, report["path"]) require.Equal(t, profile, report["profile"]) require.Equal(t, "", report["token"], "ambient credentials must not cross the plugin boundary") @@ -117,3 +118,14 @@ inputs = { _, _, err = prepare("development", map[string]string{"PATH": ambient}) require.Error(t, err, "the evaluator error text is intentionally generic") } + +// requireSamePath compares paths the way the platform does. Windows paths are case-insensitive, and an +// executable resolved through PATHEXT keeps the extension as spelled there (".EXE"). +func requireSamePath(t *testing.T, want, got string) { + t.Helper() + if runtime.GOOS == "windows" { + require.True(t, strings.EqualFold(filepath.Clean(want), filepath.Clean(got)), "want %s, got %s", want, got) + return + } + require.Equal(t, want, got) +} From 4ba7d3a6622de4a75fed64d2e1a0bbad1e08d50e Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 1 Nov 2026 08:31:11 -0600 Subject: [PATCH 05/52] fix(casts): reach the terragrunt-native cast validator from the cast aggregates The validator was a leaf that no aggregate reached, so casts validate all rejected it. Add a per-fixture aggregate, like native-terraform has, and call it from the generate and validate roots. Co-Authored-By: Claude Sonnet 5.5 --- demo/casts/atmos.d/all.yaml | 6 ++++++ .../demo/fixtures/terragrunt-native/all.yaml | 15 +++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 demo/casts/atmos.d/demo/fixtures/terragrunt-native/all.yaml diff --git a/demo/casts/atmos.d/all.yaml b/demo/casts/atmos.d/all.yaml index 78ffe114719..d3b8c6dd198 100644 --- a/demo/casts/atmos.d/all.yaml +++ b/demo/casts/atmos.d/all.yaml @@ -11,6 +11,9 @@ commands: - type: shell name: native-terraform command: atmos casts generate demo fixtures native-terraform + - type: shell + name: terragrunt-native + command: atmos casts generate demo fixtures terragrunt-native - type: shell name: streaming-ui command: atmos casts generate demo fixtures streaming-ui @@ -56,6 +59,9 @@ commands: - type: shell name: native-terraform command: atmos casts validate demo fixtures native-terraform + - type: shell + name: terragrunt-native + command: atmos casts validate demo fixtures terragrunt-native - type: shell name: streaming-ui command: atmos casts validate demo fixtures streaming-ui diff --git a/demo/casts/atmos.d/demo/fixtures/terragrunt-native/all.yaml b/demo/casts/atmos.d/demo/fixtures/terragrunt-native/all.yaml new file mode 100644 index 00000000000..7b44bfa7141 --- /dev/null +++ b/demo/casts/atmos.d/demo/fixtures/terragrunt-native/all.yaml @@ -0,0 +1,15 @@ +commands: + - name: casts generate demo fixtures terragrunt-native + description: Regenerate casts sourced from demo/casts/fixtures/terragrunt-native + env: !include cast-defaults.yaml .env.command + steps: + - type: shell + name: casts-fixtures-terragrunt-native-plan + command: atmos casts generate demo fixtures terragrunt-native plan + - name: casts validate demo fixtures terragrunt-native + description: Validate casts sourced from demo/casts/fixtures/terragrunt-native + env: !include cast-defaults.yaml .env.command + steps: + - type: shell + name: casts-fixtures-terragrunt-native-plan + command: atmos casts validate demo fixtures terragrunt-native plan From 35c2d4346e94e5b72235c296ca7498acfb30fa82 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 15:38:34 -0600 Subject: [PATCH 06/52] test(terraform): make the hook and toolchain process tests pass on Windows - Copy the test binary instead of hard-linking it on Windows. Windows refuses to delete a hard link to the running test binary, so every TempDir cleanup failed with "Access is denied". - Check the saved plan binding's private permissions only off Windows, which reports 0666 for every writable file. Co-Authored-By: Claude Opus 5.5 --- pkg/component/terraform/configuration_plan_test.go | 6 +++++- pkg/component/terraform/configuration_toolchain_test.go | 3 ++- pkg/component/terraform/execute_hooks_process_test.go | 3 ++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index 4f70192c24f..459dcaf3e9a 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -5,6 +5,7 @@ import ( "errors" "os" "path/filepath" + "runtime" "testing" errUtils "github.com/cloudposse/atmos/errors" @@ -53,7 +54,10 @@ func TestConfigurationPlanRestoresReviewedInputs(t *testing.T) { require.NoError(t, err) stat, err := os.Stat(path + configurationPlanSuffix) require.NoError(t, err) - require.Equal(t, os.FileMode(0), stat.Mode().Perm()&0o077) + // Windows reports 0666 for every writable file, so POSIX permission bits are only meaningful elsewhere. + if runtime.GOOS != "windows" { + require.Equal(t, os.FileMode(0), stat.Mode().Perm()&0o077) + } info.SubCommand = "apply" info.UseTerraformPlan = true info.PlanFile = path diff --git a/pkg/component/terraform/configuration_toolchain_test.go b/pkg/component/terraform/configuration_toolchain_test.go index 1b5916492b1..d77bbb0f0b6 100644 --- a/pkg/component/terraform/configuration_toolchain_test.go +++ b/pkg/component/terraform/configuration_toolchain_test.go @@ -87,7 +87,8 @@ func installToolchainFixture(t *testing.T, installPath, owner, repo, version, na require.NoError(t, os.MkdirAll(filepath.Dir(target), 0o755)) executable, err := os.Executable() require.NoError(t, err) - if err := os.Link(executable, target); err == nil { + // Windows refuses to delete a hard link to the running test binary, which breaks TempDir cleanup, so copy there. + if runtime.GOOS != "windows" && os.Link(executable, target) == nil { return target } content, err := os.ReadFile(executable) diff --git a/pkg/component/terraform/execute_hooks_process_test.go b/pkg/component/terraform/execute_hooks_process_test.go index cab1f611d80..70a4706230c 100644 --- a/pkg/component/terraform/execute_hooks_process_test.go +++ b/pkg/component/terraform/execute_hooks_process_test.go @@ -139,7 +139,8 @@ func installHookTool(t *testing.T, dir, name string) { executable, err := os.Executable() require.NoError(t, err) target := filepath.Join(dir, name) - if err := os.Link(executable, target); err == nil { + // Windows refuses to delete a hard link to the running test binary, which breaks TempDir cleanup, so copy there. + if runtime.GOOS != "windows" && os.Link(executable, target) == nil { return } content, err := os.ReadFile(executable) From 84ed25a2ba70dba53a672be44ee390b7242614f5 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 16:14:59 -0600 Subject: [PATCH 07/52] fix(list): match OpenTofu engine detection to execution The Engine column required the basename to be exactly tofu or tofu.exe, while execution treats any basename containing "tofu" case-insensitively as OpenTofu, so tofu-1.8 or OpenTofu.EXE were listed as terraform. Co-Authored-By: Claude Opus 5.5 --- pkg/list/instance_engine.go | 4 +++- pkg/list/instance_engine_test.go | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/pkg/list/instance_engine.go b/pkg/list/instance_engine.go index f6b69a3a5da..a6053f5859c 100644 --- a/pkg/list/instance_engine.go +++ b/pkg/list/instance_engine.go @@ -22,7 +22,9 @@ func instanceEngine(config *schema.AtmosConfiguration, kind string, component ma if command == "" { command = config.Components.Terraform.Command } - if strings.TrimSuffix(filepath.Base(command), ".exe") == "tofu" { + // Match execution's executable-name detection (pkg/component/terraform/detection.go), + // which treats any basename containing "tofu", case-insensitively, as OpenTofu. + if strings.Contains(strings.ToLower(filepath.Base(command)), "tofu") { return "opentofu" } return "terraform" diff --git a/pkg/list/instance_engine_test.go b/pkg/list/instance_engine_test.go index 3689badaae6..19e8b7889fb 100644 --- a/pkg/list/instance_engine_test.go +++ b/pkg/list/instance_engine_test.go @@ -23,6 +23,8 @@ func TestInstanceEngineSelection(t *testing.T) { }{ {"configured tofu", "terraform", nil, "opentofu"}, {"unit terraform overrides default", "terraform", map[string]any{"command": "terraform"}, "terraform"}, + {"versioned tofu binary", "terraform", map[string]any{"command": "tofu-1.8"}, "opentofu"}, + {"mixed-case OpenTofu binary", "terraform", map[string]any{"command": "OpenTofu.EXE"}, "opentofu"}, {"absolute Windows binary", "terraform", map[string]any{"command": filepath.Join(t.TempDir(), "tofu.exe")}, "opentofu"}, {"native HCL remains tofu", "terraform", map[string]any{"settings": map[string]any{"configuration": map[string]any{"provider": "terragrunt", "engine": "opentofu"}}}, "opentofu"}, {"external engine metadata", "terraform", map[string]any{"settings": map[string]any{"configuration": map[string]any{"engine": "terragrunt"}}}, "terragrunt"}, From e932f1643ebf8c8173518343a88f451bcf2e97cd Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 16:27:51 -0600 Subject: [PATCH 08/52] fix(terraform): check required var files before init and drop --var options from saved plans A fresh unit ran implicit init and its hooks before the plan phase reported a missing required var file. The requested command's required files are now checked in the provisioned directory before init and before workspace selection. Saved-plan applies also dropped only single-dash -var options; --var and --var-file reached Terraform and were rejected. Co-Authored-By: Claude Opus 5.5 --- pkg/component/execution/arguments.go | 48 ++++++++++++++++--- pkg/component/execution/arguments_test.go | 26 ++++++++++ pkg/component/terraform/execute_hooks.go | 15 ++++++ pkg/component/terraform/execute_hooks_test.go | 26 ++++++++++ pkg/component/terraform/execute_init.go | 5 ++ pkg/component/terraform/execute_pipeline.go | 5 ++ 6 files changed, 118 insertions(+), 7 deletions(-) diff --git a/pkg/component/execution/arguments.go b/pkg/component/execution/arguments.go index a02a94f8b08..15825cde6f3 100644 --- a/pkg/component/execution/arguments.go +++ b/pkg/component/execution/arguments.go @@ -13,10 +13,7 @@ import ( "github.com/cloudposse/atmos/pkg/schema" ) -const ( - varFileFlag = "-var-file=" - varFlagPrefix = "-var" -) +const varFileFlag = "-var-file=" // ArgumentsRequest describes one Terraform invocation for ApplyExtraArguments. type ArgumentsRequest struct { @@ -64,6 +61,23 @@ func ApplyExtraArguments(items []schema.ComponentExtraArguments, request *Argume return ArgumentsResult{Args: args, Environment: sortedEnvironment(environment)}, nil } +// CheckRequiredVarFiles verifies the required variable files of every block that lists the +// command, without building any arguments. It lets a caller reject a missing file before +// implicit init or workspace subprocesses, and their hooks, run for that command. +func CheckRequiredVarFiles(items []schema.ComponentExtraArguments, command, workingDirectory string) error { + defer perf.Track(nil, "execution.CheckRequiredVarFiles")() + + for i := range items { + if !slices.Contains(items[i].Commands, command) { + continue + } + if _, err := requiredVarFiles(items[i].RequiredVarFiles, workingDirectory); err != nil { + return err + } + } + return nil +} + func blockArguments(item *schema.ComponentExtraArguments, request *ArgumentsRequest) ([]string, error) { arguments := slices.Clone(item.Arguments) if request.PlanFileArgument { @@ -84,23 +98,43 @@ func blockArguments(item *schema.ComponentExtraArguments, request *ArgumentsRequ return arguments, nil } -// dropVariableArguments removes -var and -var-file options. A bare option takes the next +// dropVariableArguments removes -var and -var-file options in either dash form (Terraform's +// flag parser accepts "--var-file" as well as "-var-file"). A bare option takes the next // argument as its value ("-var", "name=value"), so that value goes with it; otherwise it // would be left behind as an unexpected positional argument. func dropVariableArguments(arguments []string) []string { var kept []string for i := 0; i < len(arguments); i++ { - if !strings.HasPrefix(arguments[i], varFlagPrefix) { + name, hasValue := variableOption(arguments[i]) + if name == "" { kept = append(kept, arguments[i]) continue } - if !strings.Contains(arguments[i], "=") { + if !hasValue { i++ } } return kept } +// variableOption reports whether argument is a -var or -var-file option, returning its name +// and whether the value is attached with "=". Other options, such as -variable-foo, are not +// variable options. +func variableOption(argument string) (name string, hasValue bool) { + option, found := strings.CutPrefix(argument, "--") + if !found { + option, found = strings.CutPrefix(argument, "-") + } + if !found { + return "", false + } + option, _, hasValue = strings.Cut(option, "=") + if option != "var" && option != "var-file" { + return "", false + } + return option, hasValue +} + func requiredVarFiles(files []string, directory string) ([]string, error) { files = keepLast(files) for _, file := range files { diff --git a/pkg/component/execution/arguments_test.go b/pkg/component/execution/arguments_test.go index 347c80612b6..b730537c844 100644 --- a/pkg/component/execution/arguments_test.go +++ b/pkg/component/execution/arguments_test.go @@ -182,3 +182,29 @@ func TestApplyExtraArgumentsSavedPlanDropsVariableValuesGivenAsSeparateArguments require.NoError(t, err) require.Equal(t, []string{"apply", "-parallelism=1", "-lock=false", "plan.tfplan"}, result.Args) } + +func TestApplyExtraArgumentsSavedPlanDropsDoubleDashVariableArguments(t *testing.T) { + item := extra("apply") + item.Arguments = []string{"--var-file=x.tfvars", "--var", "name=value", "--var-file", "y.tfvars", "-variable-like=keep", "--lock=false"} + args := []string{"apply", "plan.tfplan"} + + result, err := ApplyExtraArguments([]schema.ComponentExtraArguments{item}, &ArgumentsRequest{Command: "apply", Args: args, InsertAt: 1, PlanFileArgument: true}) + + require.NoError(t, err) + require.Equal(t, []string{"apply", "-variable-like=keep", "--lock=false", "plan.tfplan"}, result.Args) +} + +func TestCheckRequiredVarFilesMatchesOnlyTheCommand(t *testing.T) { + dir := t.TempDir() + plan := extra("plan") + plan.RequiredVarFiles = []string{"required.tfvars"} + items := []schema.ComponentExtraArguments{plan} + + err := CheckRequiredVarFiles(items, "plan", dir) + require.ErrorIs(t, err, errUtils.ErrComponentVarFileMissing) + require.ErrorContains(t, err, "required.tfvars") + require.NoError(t, CheckRequiredVarFiles(items, "apply", dir)) + + require.NoError(t, os.WriteFile(filepath.Join(dir, "required.tfvars"), nil, 0o600)) + require.NoError(t, CheckRequiredVarFiles(items, "plan", dir)) +} diff --git a/pkg/component/terraform/execute_hooks.go b/pkg/component/terraform/execute_hooks.go index 60d4e1048c9..60c82b839f0 100644 --- a/pkg/component/terraform/execute_hooks.go +++ b/pkg/component/terraform/execute_hooks.go @@ -75,6 +75,21 @@ func (e *Executor) runMainShellCommand(atmosConfig *schema.AtmosConfiguration, i }) } +// preflightRequiredVarFiles checks the requested command's required variable files in the +// provisioned module directory. A saved-plan apply is exempt: Terraform rejects variable +// options beside a plan file, so runExecutionPhase never adds them. +func preflightRequiredVarFiles(info *schema.ConfigAndStacksInfo, componentPath string) error { + config := info.ComponentExecution + if config.IsEmpty() || !shouldRunMainTerraformCommand(info) { + return nil + } + if info.SubCommand == subcommandApply && info.UseTerraformPlan { + return nil + } + command := execution.CommandName(info.SubCommand, info.AdditionalArgsAndFlags) + return execution.CheckRequiredVarFiles(config.ExtraArguments, command, componentPath) +} + // phaseAction runs the wrapped Terraform subprocess with the phase's arguments, environment, and options. type phaseAction func(info *schema.ConfigAndStacksInfo, args []string, opts []component.ExecutionOption) error diff --git a/pkg/component/terraform/execute_hooks_test.go b/pkg/component/terraform/execute_hooks_test.go index b65b90fa7ee..1aaf48c5177 100644 --- a/pkg/component/terraform/execute_hooks_test.go +++ b/pkg/component/terraform/execute_hooks_test.go @@ -459,3 +459,29 @@ func TestSavedPlanBindingKeepsExecution(t *testing.T) { require.NoError(t, decoder.Decode(&decoded)) require.Equal(t, plan.Prepared.Execution, decoded.Prepared.Execution) } + +func TestImplicitInitMissingRequiredVarFileFailsBeforeAnyProcess(t *testing.T) { + shell := &fakeShell{} + info := hookInfo("plan", &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, RequiredVarFiles: []string{"missing.tfvars"}}}, + BeforeHooks: []schema.ComponentHook{execHook("init-hook", "init"), execHook("plan-hook", "plan")}, + }) + dir := t.TempDir() + _, err := hookExecutor(shell).executeTerraformInitPhase(&schema.AtmosConfiguration{}, info, dir, "") + require.ErrorIs(t, err, errUtils.ErrComponentVarFileMissing) + require.ErrorContains(t, err, "missing.tfvars") + require.Empty(t, shell.calls, "neither init nor its hooks may run") +} + +func TestPreflightRequiredVarFiles(t *testing.T) { + missing := &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"apply", "plan"}, RequiredVarFiles: []string{"missing.tfvars"}}}} + dir := t.TempDir() + require.ErrorIs(t, preflightRequiredVarFiles(hookInfo("plan", missing), dir), errUtils.ErrComponentVarFileMissing) + require.NoError(t, preflightRequiredVarFiles(hookInfo("destroy", missing), dir), "only blocks listing the command apply") + require.NoError(t, preflightRequiredVarFiles(hookInfo("plan", nil), dir), "no provider execution") + savedPlan := hookInfo("apply", missing) + savedPlan.UseTerraformPlan = true + require.NoError(t, preflightRequiredVarFiles(savedPlan, dir), "saved plans never take variable files") + workspace := hookInfo("workspace", missing) + require.NoError(t, preflightRequiredVarFiles(workspace, dir), "workspace management runs no main command") +} diff --git a/pkg/component/terraform/execute_init.go b/pkg/component/terraform/execute_init.go index 3f949b8a75f..a5e8726b5bb 100644 --- a/pkg/component/terraform/execute_init.go +++ b/pkg/component/terraform/execute_init.go @@ -226,6 +226,11 @@ func (e *Executor) executeTerraformInitPhase(atmosConfig *schema.AtmosConfigurat if err != nil { return componentPath, err } + // Check the requested command's required variable files in the provisioned directory + // before implicit init and its hooks run, so a fresh unit fails without side effects. + if err = preflightRequiredVarFiles(info, newPath); err != nil { + return newPath, err + } if err = e.executeTerraformInitCommand(atmosConfig, info, newPath, varFile, opts...); err != nil { return newPath, err diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index 2d47fef18cb..d73f08b6e05 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -217,6 +217,11 @@ func (e *Executor) executeCommandPipeline( return err } + // A missing required variable file must fail before workspace selection runs a subprocess. + if err = preflightRequiredVarFiles(info, componentPath); err != nil { + return err + } + if err = e.runWorkspaceSetupPhase(atmosConfig, info, componentPath, opts...); err != nil { return errUtils.AsPrerequisiteError(err) } From 7cf2fa6767b0e2d84e488d09b636a182736d495e Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 16:31:28 -0600 Subject: [PATCH 09/52] fix(terragrunt): accept project-root and aliased imports, reject globs at registration terragrunt://. produced the pattern ./** which matches no unit, an aliased provider scheme such as tg:// reached the provider unchanged and was rejected, and bracket or brace characters failed later in discovery without naming the import. Co-Authored-By: Claude Opus 5.5 --- pkg/plugin/adapter/configuration/imports.go | 9 +++++--- .../adapter/configuration/imports_test.go | 22 +++++++++++++++++-- .../terragrunt/internal/provider/discovery.go | 13 ++++++++++- .../internal/provider/imports_test.go | 6 +++++ 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/pkg/plugin/adapter/configuration/imports.go b/pkg/plugin/adapter/configuration/imports.go index 8b936d0a0ba..dbc90b018d0 100644 --- a/pkg/plugin/adapter/configuration/imports.go +++ b/pkg/plugin/adapter/configuration/imports.go @@ -34,11 +34,14 @@ func (s *Session) RegisterImport(ctx context.Context, uri string) (bool, error) if err := s.ctx.Err(); err != nil { return true, err } - if !filepath.IsLocal(path) || strings.ContainsAny(path, "\\?#*") { - return true, fmt.Errorf("%w: provider import must name a project-relative file or directory", errUtils.ErrPluginInvalidConfig) + // Glob and bracket characters are rejected here, naming the import, rather than failing + // later inside provider discovery with an error that does not identify it. + if !filepath.IsLocal(path) || strings.ContainsAny(path, "\\?#*[{") { + return true, fmt.Errorf("%w: provider import %s must name a project-relative file or directory", errUtils.ErrPluginInvalidConfig, uri) } path = filepath.ToSlash(filepath.Clean(path)) - key := name + "://" + path + // The scheme may be a configuration alias; the provider only recognizes its own name. + key := request.Provider + "://" + path if s.imports[key] { return true, nil } diff --git a/pkg/plugin/adapter/configuration/imports_test.go b/pkg/plugin/adapter/configuration/imports_test.go index b24534211d7..2ae75158959 100644 --- a/pkg/plugin/adapter/configuration/imports_test.go +++ b/pkg/plugin/adapter/configuration/imports_test.go @@ -59,10 +59,11 @@ func TestImportValidationCancellationAndRevision(t *testing.T) { require.NoError(t, err) require.False(t, handled) } - for _, path := range []string{"", "../outside", "/absolute", `live\app`, "live?ref=main", "live#fragment", "live/*"} { + for _, path := range []string{"", "../outside", "/absolute", `live\app`, "live?ref=main", "live#fragment", "live/*", "live/[ab]", "live/{a,b}"} { handled, err := session.RegisterImport(t.Context(), "terragrunt://"+path) require.True(t, handled) - require.Error(t, err) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) + require.ErrorContains(t, err, "terragrunt://"+path) } _, err = session.RegisterImport(t.Context(), "terragrunt://live") require.NoError(t, err) @@ -103,3 +104,20 @@ func TestConcurrentImportRegistrationDeduplicates(t *testing.T) { require.Equal(t, []string{"terragrunt://live"}, session.requests[0].Imports) var _ schema.ComponentConfigurationImportRuntime = session } + +func TestAliasedImportSchemeUsesTheProviderName(t *testing.T) { + config := enabledConfig(t) + settings := config.Components.Terraform.ConfigurationProviders["terragrunt"] + settings.Provider = "terragrunt" + config.Components.Terraform.ConfigurationProviders = map[string]schema.ComponentConfiguration{"tg": settings} + session, err := New(t.Context(), config, "list", func(context.Context, []string) (map[string]Provider, error) { return nil, nil }) + require.NoError(t, err) + for _, uri := range []string{"tg://live", "tg://live/."} { + handled, err := session.RegisterImport(t.Context(), uri) + require.NoError(t, err) + require.True(t, handled) + } + require.Len(t, session.requests, 1) + require.Equal(t, "terragrunt", session.requests[0].Provider) + require.Equal(t, []string{"terragrunt://live"}, session.requests[0].Imports) +} diff --git a/plugins/terragrunt/internal/provider/discovery.go b/plugins/terragrunt/internal/provider/discovery.go index e0c35f7154a..9a2984609d3 100644 --- a/plugins/terragrunt/internal/provider/discovery.go +++ b/plugins/terragrunt/internal/provider/discovery.go @@ -121,7 +121,7 @@ func importPaths(request *pluginv1.DiscoverComponentsRequest) ([]string, error) return nil, err } if info.IsDir() { - path = strings.TrimSuffix(filepath.ToSlash(path), "/") + "/**" + path = directoryPattern(path) } else if info.Name() != "terragrunt.hcl" && info.Name() != "terragrunt.stack.hcl" { return nil, ErrUnsupported } @@ -132,3 +132,14 @@ func importPaths(request *pluginv1.DiscoverComponentsRequest) ([]string, error) } return paths, nil } + +// directoryPattern matches every unit below an imported directory. The project root +// ("terragrunt://." or "terragrunt://./") has no prefix: "./**" would not match the +// project-relative unit paths discovery compares against. +func directoryPattern(path string) string { + clean := filepath.ToSlash(filepath.Clean(filepath.FromSlash(path))) + if clean == "." { + return "**" + } + return clean + "/**" +} diff --git a/plugins/terragrunt/internal/provider/imports_test.go b/plugins/terragrunt/internal/provider/imports_test.go index 59f73ec7815..ede1090584c 100644 --- a/plugins/terragrunt/internal/provider/imports_test.go +++ b/plugins/terragrunt/internal/provider/imports_test.go @@ -29,6 +29,12 @@ func TestImportsSelectExplicitFilesAndDirectories(t *testing.T) { }) _, err = p.Discover(t.Context(), request) require.NoError(t, err) + for uri, want := range map[string]string{"terragrunt://.": "**", "terragrunt://./": "**", "terragrunt://live/": "live/**", "terragrunt://./live": "live/**"} { + request.Imports = []string{uri} + got, err := importPaths(request) + require.NoError(t, err, uri) + require.Equal(t, []string{want}, got, uri) + } for _, uri := range []string{"wrong://live", "terragrunt://../outside", "terragrunt://live?ref=main", "terragrunt://absent", "terragrunt://live/*", `terragrunt://live\child`} { request.Imports = []string{uri} _, err := importPaths(request) From 5a15479abea05881a53fc2fce455cea241d0647a Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 16:33:01 -0600 Subject: [PATCH 10/52] fix(plugin): keep the publisher-qualified default alias for earlier editions The product-only default alias replaced the released publisher-qualified default unconditionally. It is now an edition behavior transition, so projects pinned before 2026-10-29 keep publisher-product, as documented. Co-Authored-By: Claude Opus 5.5 --- pkg/edition/journal.yaml | 7 +++++ pkg/plugin/manage/manage.go | 4 ++- pkg/plugin/manage/pin.go | 23 +++++++++++++-- pkg/plugin/manage/pin_test.go | 34 +++++++++++++++++++--- website/docs/cli/commands/plugin/usage.mdx | 5 ++-- 5 files changed, 63 insertions(+), 10 deletions(-) diff --git a/pkg/edition/journal.yaml b/pkg/edition/journal.yaml index 2c39170a625..e564001d134 100644 --- a/pkg/edition/journal.yaml +++ b/pkg/edition/journal.yaml @@ -2,6 +2,13 @@ # Append changes here; keep old/new in the configuration field's type. # Quote dates and string values such as "true" and "false". # The CLI embeds this file; the website generates its index from the same file. +- date: "2026-10-29" + key: plugins.default_alias + kind: behavior + old: derive the publisher-qualified alias (publisher-product) when atmos plugin install has no --as + new: derive the product-only alias when atmos plugin install has no --as + description: New plugin declarations use the short product name as their alias; aliases already recorded in atmos.yaml are unchanged, and --as remains authoritative. + ref: https://github.com/cloudposse/atmos/pull/3576 - date: "2026-10-29" key: list.instances.engine_columns kind: behavior diff --git a/pkg/plugin/manage/manage.go b/pkg/plugin/manage/manage.go index 7189b816272..f28f4603ce7 100644 --- a/pkg/plugin/manage/manage.go +++ b/pkg/plugin/manage/manage.go @@ -65,7 +65,9 @@ func (m *Manager) Add(ctx context.Context, options AddOptions) error { } alias := plan.alias if alias == "" { - alias = defaultAlias(entry.Plugin) + if alias, err = m.defaultAlias(entry.Plugin); err != nil { + return nil, err + } if err := m.checkDerivedAlias(alias, options.Source, file); err != nil { return nil, err } diff --git a/pkg/plugin/manage/pin.go b/pkg/plugin/manage/pin.go index 8902387afbf..504eb6a0b31 100644 --- a/pkg/plugin/manage/pin.go +++ b/pkg/plugin/manage/pin.go @@ -9,6 +9,7 @@ import ( "github.com/Masterminds/semver/v3" errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/edition" pluginlock "github.com/cloudposse/atmos/pkg/plugin/lock" ) @@ -100,8 +101,24 @@ func invalidAlias(alias string) error { Err() } -// defaultAlias derives the short product alias used when no alias is given. -func defaultAlias(packageID string) string { +// defaultAliasBehavior is the edition transition from publisher-qualified to product-only aliases. +const defaultAliasBehavior = "plugins.default_alias" + +// defaultAlias derives the alias used when no alias is given. Projects pinned before the +// transition keep the publisher-qualified alias ("publisher-product"); later editions and +// unpinned projects use the short product name. Aliases already recorded are never rewritten. +func (m *Manager) defaultAlias(packageID string) (string, error) { + pin := "" + if m.Config != nil { + pin = m.Config.Edition + } + changed, err := edition.BehaviorChanged(pin, defaultAliasBehavior) + if err != nil { + return "", err + } + if !changed { + return strings.ReplaceAll(packageID, "/", "-"), nil + } _, product, _ := strings.Cut(packageID, "/") - return product + return product, nil } diff --git a/pkg/plugin/manage/pin_test.go b/pkg/plugin/manage/pin_test.go index 53acee214fb..a512626dcb5 100644 --- a/pkg/plugin/manage/pin_test.go +++ b/pkg/plugin/manage/pin_test.go @@ -161,11 +161,37 @@ func TestExplicitAliasMayReplaceAnotherSource(t *testing.T) { require.Equal(t, other, locked.Spec.Packages["example-fixture"].Source) } -func TestDefaultAliasUsesProductName(t *testing.T) { +func TestDefaultAliasFollowsEdition(t *testing.T) { + for _, tc := range []struct { + pin, want, not string + }{ + {"", "fixture", "example-fixture"}, + {"2026-10-29", "fixture", "example-fixture"}, + {"2026-10-28", "example-fixture", "fixture"}, + } { + t.Run("pin="+tc.pin, func(t *testing.T) { + manager, source, _ := setup(t) + manager.Config.Edition = tc.pin + require.NoError(t, manager.Add(t.Context(), manage.AddOptions{Source: source})) + locked, err := pluginlock.Load(manager.Project) + require.NoError(t, err) + require.Contains(t, locked.Spec.Packages, tc.want) + require.NotContains(t, locked.Spec.Packages, tc.not) + }) + } +} + +func TestDefaultAliasRejectsInvalidEdition(t *testing.T) { + manager, source, _ := setup(t) + manager.Config.Edition = "not-a-date" + require.Error(t, manager.Add(t.Context(), manage.AddOptions{Source: source})) +} + +func TestExplicitAliasIgnoresEdition(t *testing.T) { manager, source, _ := setup(t) - require.NoError(t, manager.Add(t.Context(), manage.AddOptions{Source: source})) + manager.Config.Edition = "2026-10-28" + require.NoError(t, manager.Add(t.Context(), manage.AddOptions{Alias: "custom", Source: source})) locked, err := pluginlock.Load(manager.Project) require.NoError(t, err) - require.Contains(t, locked.Spec.Packages, "fixture") - require.NotContains(t, locked.Spec.Packages, "example-fixture") + require.Contains(t, locked.Spec.Packages, "custom") } diff --git a/website/docs/cli/commands/plugin/usage.mdx b/website/docs/cli/commands/plugin/usage.mdx index 5db143895c8..d8a101b3c98 100644 --- a/website/docs/cli/commands/plugin/usage.mdx +++ b/website/docs/cli/commands/plugin/usage.mdx @@ -150,8 +150,9 @@ use `publisher/package` to identify an integration. For example, `cloudposse/terragrunt` identifies Cloud Posse's Atmos integration. The `plugins.packages.` key is a project-local name. Use -[`plugin install --as terragrunt`](/cli/commands/plugin/install) to select the -product alias explicitly. New declarations default to the product name. Editions +[`atmos plugin install --as terragrunt`](/cli/commands/plugin/install) to +select the alias explicitly, where `` is the plugin's OCI repository or a +configured registry selector. New declarations default to the product name. Editions before `2026-10-29` retain the publisher-qualified default, such as `cloudposse-terragrunt`. Existing declarations keep their aliases across upgrades. A collision with another source requires an explicit alias. From 8f14ce2177276bce9c28ea6a9cd6c17c33f27dcc Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 16:35:09 -0600 Subject: [PATCH 11/52] docs(terragrunt): document the coordinates of an imported unit Co-Authored-By: Claude Opus 5.5 --- website/docs/migration/terragrunt.mdx | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/website/docs/migration/terragrunt.mdx b/website/docs/migration/terragrunt.mdx index c164793daeb..e1187ebeb5c 100644 --- a/website/docs/migration/terragrunt.mdx +++ b/website/docs/migration/terragrunt.mdx @@ -916,13 +916,21 @@ atmos terraform apply --all atmos describe affected --base origin/main ``` -To pull a unit into an existing stack file instead of discovering it automatically, import it: +To add a unit to the inventory from a stack file instead of discovering it automatically, import it: ```yaml import: - terragrunt://live/dev/vpc/terragrunt.hcl ``` +The import adds the unit to the shared inventory. It does not move the unit into the importing stack: +the unit keeps the coordinates discovery gives it, its directory (`live/dev`) as the stack and its +unit name (`vpc`) as the component. Select it with those coordinates, whatever the importing file is named: + +```shell +atmos terraform plan vpc -s live/dev +``` + ### What Atmos runs for you - `dependency` and `dependencies` blocks become ordering and output edges, and dependency outputs are refreshed in the same apply. From e8338777673106ae86d6059ca076b57b4eba8b19 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 8 Nov 2026 16:38:03 -0600 Subject: [PATCH 12/52] fix(terragrunt): keep discovery and output reads working with data files and relative paths A data file named *.hcl that is not HCL failed the whole discovery inventory; it now stays a file watch. Output reads in the scratch directory resolved relative backend credential files against the scratch directory instead of the unit's. The cast fixture also masks the local plugin path wherever the checkout lives, not only under /Users or /home. Co-Authored-By: Claude Opus 5.5 --- .../fixtures/terragrunt-native/atmos.yaml | 5 +- pkg/terraform/output/configuration_state.go | 51 ++++++++++++++++++- .../output/configuration_state_test.go | 25 +++++++++ .../internal/engine/source_watches.go | 5 +- .../internal/engine/source_watches_test.go | 6 ++- 5 files changed, 87 insertions(+), 5 deletions(-) diff --git a/demo/casts/fixtures/terragrunt-native/atmos.yaml b/demo/casts/fixtures/terragrunt-native/atmos.yaml index b27d7c5861e..028940f8c4d 100644 --- a/demo/casts/fixtures/terragrunt-native/atmos.yaml +++ b/demo/casts/fixtures/terragrunt-native/atmos.yaml @@ -14,7 +14,8 @@ components: logs: level: Warning -# Keep the local-development warning, but redact the recorder's checkout path. +# Keep the local-development warning, but redact the recorder's checkout path wherever the +# checkout lives (a home directory, a CI runner workspace, or a Windows drive). settings: metrics: enabled: false @@ -23,4 +24,4 @@ settings: enabled: true replacement: '[LOCAL_PLUGIN]' patterns: - - '/(Users|home)/[^[:space:]]+/plugins/terragrunt' + - '(/|[A-Za-z]:[\\/])[^[:space:]]*[\\/]plugins[\\/]terragrunt' diff --git a/pkg/terraform/output/configuration_state.go b/pkg/terraform/output/configuration_state.go index 116e9ca763d..d5f7a2dd208 100644 --- a/pkg/terraform/output/configuration_state.go +++ b/pkg/terraform/output/configuration_state.go @@ -7,6 +7,7 @@ import ( "os" "path/filepath" "slices" + "strings" errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/auth" @@ -100,7 +101,7 @@ func configurationOutputConfig(config *ComponentConfig, state *schema.ComponentC config.Env = make(map[string]any) } config.Env["TF_DATA_DIR"] = filepath.Join(root, ".terraform") - config.BackendType, config.Backend, config.Workspace = state.BackendType, state.Backend, state.Workspace + config.BackendType, config.Backend, config.Workspace = state.BackendType, absoluteBackendFiles(state.Backend, state.WorkingDirectory), state.Workspace config.AutoGenerateBackend = true config.AutoProvisionWorkdirForOutputs = false config.Providers, config.Vars = nil, nil @@ -119,3 +120,51 @@ func configurationEnvironment(managers []any) []string { } return nil } + +// backendFileKeys name backend settings that hold file paths. Terraform resolves a relative +// path against its working directory, which for output reads is the scratch directory. +var backendFileKeys = []string{ + "path", "workspace_dir", + "shared_credentials_file", "shared_credentials_files", "shared_config_files", + "client_certificate_path", "oidc_token_file_path", +} + +// absoluteBackendFiles anchors relative backend file paths at the state working directory, +// so a scratch reader finds the same files as the unit's own execution. The input is unchanged. +func absoluteBackendFiles(backend map[string]any, directory string) map[string]any { + if directory == "" || len(backend) == 0 { + return backend + } + result := maps.Clone(backend) + for _, key := range backendFileKeys { + if value, ok := result[key]; ok { + result[key] = anchorBackendFile(value, directory) + } + } + return result +} + +// anchorBackendFile anchors one path, or each path of a list, at directory. Absolute and +// home-relative paths, and values that are not paths, are returned unchanged. +func anchorBackendFile(value any, directory string) any { + switch typed := value.(type) { + case string: + if typed != "" && !filepath.IsAbs(typed) && !strings.HasPrefix(typed, "~") { + return filepath.Join(directory, typed) + } + return typed + case []any: + paths := make([]any, len(typed)) + for i := range typed { + paths[i] = anchorBackendFile(typed[i], directory) + } + return paths + case []string: + paths := make([]any, len(typed)) + for i := range typed { + paths[i] = anchorBackendFile(typed[i], directory) + } + return paths + } + return value +} diff --git a/pkg/terraform/output/configuration_state_test.go b/pkg/terraform/output/configuration_state_test.go index 0706b0e0752..6278e56e8a1 100644 --- a/pkg/terraform/output/configuration_state_test.go +++ b/pkg/terraform/output/configuration_state_test.go @@ -154,3 +154,28 @@ func TestReadOnlyHTTPOutputDoesNotSelectWorkspace(t *testing.T) { err := ensureOutputWorkspace(t.Context(), nil, runner, &ComponentConfig{ReadOnlyState: true, BackendType: "http"}, &workspaceTarget{component: "network", stack: "live"}) require.NoError(t, err) } + +func TestOutputReaderAnchorsRelativeBackendFiles(t *testing.T) { + unit := t.TempDir() + absolute := filepath.Join(t.TempDir(), "credentials") + backend := map[string]any{ + "bucket": "state", + "shared_credentials_files": []any{"creds/aws", absolute}, + "shared_config_files": []string{"config/aws"}, + "shared_credentials_file": "legacy", + "client_certificate_path": "~/cert.pfx", + } + state := &schema.ComponentConfigurationState{BackendType: "s3", Backend: backend, WorkingDirectory: unit} + config := &ComponentConfig{} + cleanup, err := configurationOutputConfig(config, state) + require.NoError(t, err) + t.Cleanup(cleanup) + require.NotEqual(t, unit, config.ComponentPath) + require.Equal(t, []any{filepath.Join(unit, "creds", "aws"), absolute}, config.Backend["shared_credentials_files"]) + require.Equal(t, []any{filepath.Join(unit, "config", "aws")}, config.Backend["shared_config_files"]) + require.Equal(t, filepath.Join(unit, "legacy"), config.Backend["shared_credentials_file"]) + require.Equal(t, "~/cert.pfx", config.Backend["client_certificate_path"], "home-relative paths are left to Terraform") + require.Equal(t, "state", config.Backend["bucket"]) + require.Equal(t, []any{"creds/aws", absolute}, backend["shared_credentials_files"], "the provider's state is not modified") + require.Equal(t, backend, absoluteBackendFiles(backend, "")) +} diff --git a/plugins/terragrunt/internal/engine/source_watches.go b/plugins/terragrunt/internal/engine/source_watches.go index 240559780ec..f61b1744edd 100644 --- a/plugins/terragrunt/internal/engine/source_watches.go +++ b/plugins/terragrunt/internal/engine/source_watches.go @@ -28,7 +28,10 @@ func trackInputSources(fs afero.Fs, root string, result *model.Resolution) error } file, diagnostics := hclsyntax.ParseConfig(contents, path, hcl.InitialPos) if diagnostics.HasErrors() { - return fmt.Errorf("cannot track configuration sources in %s", path) + // Files also lists data files that functions read, and a data file may be named + // *.hcl without being HCL. Every configuration file already parsed during + // evaluation, so one that does not parse is data: it stays a file watch. + continue } inputs := file.Body.(*hclsyntax.Body).Attributes["inputs"] if inputs == nil { diff --git a/plugins/terragrunt/internal/engine/source_watches_test.go b/plugins/terragrunt/internal/engine/source_watches_test.go index ea73bc51039..f85478cb678 100644 --- a/plugins/terragrunt/internal/engine/source_watches_test.go +++ b/plugins/terragrunt/internal/engine/source_watches_test.go @@ -60,8 +60,12 @@ func TestInputWatchesFailuresAndIncludedInputs(t *testing.T) { result := &model.Resolution{Files: []string{path}} require.Error(t, trackInputSources(fs, root, result)) require.NoError(t, fs.MkdirAll(root, 0o700)) + // A read data file named *.hcl need not be HCL; it remains a file watch instead of + // failing the whole inventory. require.NoError(t, afero.WriteFile(fs, path, []byte("inputs = {"), 0o600)) - require.ErrorContains(t, trackInputSources(fs, root, result), "cannot track") + require.NoError(t, trackInputSources(fs, root, result)) + require.Equal(t, []string{path}, result.Files) + require.Empty(t, result.Folders) require.NoError(t, afero.WriteFile(fs, path, []byte(`locals { unrelated = "constant" }`), 0o600)) require.NoError(t, trackInputSources(fs, root, result)) require.Empty(t, result.Folders) From b8db8661c3dd81733cf656e3db0a01e8b4f15a42 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Tue, 10 Nov 2026 15:47:13 -0600 Subject: [PATCH 13/52] fix(terraform): keep plan hooks, engine checks, and plan digests consistent with execution A plan with -detailed-exitcode that found changes (exit 2) ran error hooks instead of after hooks; it now runs after hooks and keeps its exit status. Preparation rejected a provider that prepared for the engine selected by --command; the expected engine now follows the command, using the same name detection as execution (any basename containing "tofu"). Plan digests ignored every *.atmos.json file, so configuration like settings.atmos.json could change without invalidating a saved plan; only plan bindings are skipped now. Output reads anchored Consul's "path" key as a file and crashed on a local backend with no settings in a named workspace. Terragrunt input watches now follow read_terragrunt_config transitively, and imports with redundant path segments resolve to the engine's clean unit paths. Co-Authored-By: Claude Opus 5.5 --- pkg/component/terraform/configuration_plan.go | 10 ++++- .../configuration_plan_failures_test.go | 2 +- pkg/component/terraform/execute_hooks.go | 38 +++++++++++++++---- pkg/component/terraform/execute_hooks_test.go | 29 ++++++++++++++ pkg/plugin/adapter/configuration/plan.go | 4 +- pkg/plugin/adapter/configuration/plan_test.go | 4 +- .../adapter/configuration/preparation.go | 16 +++++++- .../adapter/configuration/preparation_test.go | 24 ++++++++++++ pkg/terraform/output/configuration_state.go | 19 ++++++++-- .../output/configuration_state_test.go | 20 +++++++++- .../internal/engine/source_watches.go | 18 ++++++++- .../internal/engine/source_watches_test.go | 28 ++++++++++++++ .../terragrunt/internal/provider/discovery.go | 2 + .../internal/provider/imports_test.go | 2 +- .../terragrunt/internal/provider/provider.go | 4 +- .../scripts/lifecycle_extended.star | 16 +++++++- 16 files changed, 211 insertions(+), 25 deletions(-) diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go index 00d42aba7cd..34ca457f9bd 100644 --- a/pkg/component/terraform/configuration_plan.go +++ b/pkg/component/terraform/configuration_plan.go @@ -19,6 +19,9 @@ import ( const configurationPlanSuffix = ".atmos.json" +// planBindingSuffix names the binding of a default saved plan, which Atmos writes as "*.planfile". +const planBindingSuffix = ".planfile" + configurationPlanSuffix + type configurationPlan struct { Version int Stack, Component, Identity, EngineDigest, PlanDigest, WorkingDirectory, WorkingDigest string @@ -294,9 +297,12 @@ func skipWorkdirDirectory(root, path, name string) bool { return path != root && (name == ".terraform" || name == ".git" || name == ".terragrunt-cache" || name == ".atmos") } -// ignoredWorkdirFile reports whether a file is the plan itself, its binding, or mutable state. +// ignoredWorkdirFile reports whether a file is the plan itself, a plan binding, or mutable state. +// Only actual bindings are skipped: this plan's own, or one beside a default ".planfile" plan. +// An ordinary file such as settings.atmos.json is configuration and stays in the digest. func ignoredWorkdirFile(path, planPath, name string) bool { - return path == planPath || strings.HasSuffix(name, configurationPlanSuffix) || strings.HasPrefix(name, ".atmos-plan-") || strings.HasPrefix(name, "terraform.tfstate") + return path == planPath || path == planPath+configurationPlanSuffix || strings.HasSuffix(name, planBindingSuffix) || + strings.HasPrefix(name, ".atmos-plan-") || strings.HasPrefix(name, "terraform.tfstate") } // recordWorkdirFile digests one retained file by its slash-separated path relative to the root. diff --git a/pkg/component/terraform/configuration_plan_failures_test.go b/pkg/component/terraform/configuration_plan_failures_test.go index 4bc598a8fff..d12e4dc4ddc 100644 --- a/pkg/component/terraform/configuration_plan_failures_test.go +++ b/pkg/component/terraform/configuration_plan_failures_test.go @@ -156,7 +156,7 @@ func TestConfigurationWorkdirDigestSkipsOnlyNestedMutableDirectories(t *testing. require.NoError(t, err) require.Equal(t, baseline, same, "mutable data does not alter the retained-bytes digest") - for _, retained := range []string{"modules/child/main.tf", "terragrunt.hcl", ".terraform.lock.hcl"} { + for _, retained := range []string{"modules/child/main.tf", "terragrunt.hcl", ".terraform.lock.hcl", "settings.atmos.json"} { write(retained) changed, err := configurationWorkdirDigest(root, plan) require.NoError(t, err) diff --git a/pkg/component/terraform/execute_hooks.go b/pkg/component/terraform/execute_hooks.go index 60c82b839f0..381ffa16820 100644 --- a/pkg/component/terraform/execute_hooks.go +++ b/pkg/component/terraform/execute_hooks.go @@ -122,16 +122,38 @@ func (e *Executor) runExecutionPhase(atmosConfig *schema.AtmosConfiguration, inf } runner := &hookRunner{executor: e, atmosConfig: atmosConfig, info: &phaseInfo, phase: phase, opts: hookOptions(opts)} - return execution.Run(component.ContextFromOptions(opts...), config, phase.command, runner, func() (string, error) { - if len(config.ErrorHooks) == 0 { - return "", action(&phaseInfo, applied.Args, opts) + // A detailed plan exit code of 2 reports changes, not a failure: ordinary after hooks run and + // error hooks do not. The status itself is returned unchanged once the hooks have run. + var detailedResult error + err = execution.Run(component.ContextFromOptions(opts...), config, phase.command, runner, func() (string, error) { + output, err := runPhaseAction(&phaseInfo, applied.Args, opts, len(config.ErrorHooks) > 0, action) + if isDetailedPlanChanges(phase, applied.Args, err) { + detailedResult = err + return output, nil } - // Error hooks match against the failed command's output, which the caller may also be capturing. - var output bytes.Buffer - stdout, stderr := composeRetryCaptureWriters(opts, &output) - err := action(&phaseInfo, applied.Args, append(slices.Clone(opts), component.WithStdoutCapture(stdout), component.WithStderrCapture(stderr))) - return output.String(), err + return output, err }) + if err == nil { + return detailedResult + } + return err +} + +// runPhaseAction runs the wrapped subprocess. Error hooks match against the failed command's +// output, which the caller may also be capturing, so it is captured only when they exist. +func runPhaseAction(info *schema.ConfigAndStacksInfo, args []string, opts []component.ExecutionOption, capture bool, action phaseAction) (string, error) { + if !capture { + return "", action(info, args, opts) + } + var output bytes.Buffer + stdout, stderr := composeRetryCaptureWriters(opts, &output) + err := action(info, args, append(slices.Clone(opts), component.WithStdoutCapture(stdout), component.WithStderrCapture(stderr))) + return output.String(), err +} + +// isDetailedPlanChanges reports whether err is plan's "-detailed-exitcode" status for changes present. +func isDetailedPlanChanges(phase *executionPhase, args []string, err error) bool { + return err != nil && phase.command == "plan" && slices.Contains(args, "-detailed-exitcode") && resolveExitCode(err) == detailedExitCodeChangesDetected } // hookOptions carries over only what a hook shares with the command: cancellation, the process diff --git a/pkg/component/terraform/execute_hooks_test.go b/pkg/component/terraform/execute_hooks_test.go index 1aaf48c5177..182da57cca1 100644 --- a/pkg/component/terraform/execute_hooks_test.go +++ b/pkg/component/terraform/execute_hooks_test.go @@ -236,6 +236,35 @@ func TestAfterHooksFollowRunOnErrorWhenTheCommandFails(t *testing.T) { require.Equal(t, []string{"terraform plan", "hook-always arg"}, shell.names()) } +func TestDetailedPlanChangesRunAfterHooksNotErrorHooks(t *testing.T) { + for _, test := range []struct { + name string + args []string + code int + want []string + }{ + {"changes present with -detailed-exitcode", []string{"plan", "-detailed-exitcode"}, 2, []string{"terraform plan", "hook-after arg"}}, + {"genuine failure with -detailed-exitcode", []string{"plan", "-detailed-exitcode"}, 1, []string{"terraform plan", "hook-failed arg"}}, + {"exit 2 without -detailed-exitcode is a failure", []string{"plan"}, 2, []string{"terraform plan", "hook-failed arg"}}, + } { + t.Run(test.name, func(t *testing.T) { + shell := &fakeShell{run: func(call *shellCall) error { + if call.command == "terraform" { + return errUtils.ExitCodeError{Code: test.code} + } + return nil + }} + info := hookInfo("plan", &schema.ComponentExecution{ + AfterHooks: []schema.ComponentHook{execHook("after", "plan")}, + ErrorHooks: []schema.ComponentHook{{Name: "failed", Commands: []string{"plan"}, Execute: []string{"hook-failed", "arg"}, OnErrors: []string{".*"}}}, + }) + err := hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, test.args, t.TempDir()) + require.Equal(t, test.code, resolveExitCode(err), "the plan's own exit status is preserved") + require.Equal(t, test.want, shell.names()) + }) + } +} + func TestErrorHookMatchesTheFailedCommandOutput(t *testing.T) { for _, test := range []struct { name string diff --git a/pkg/plugin/adapter/configuration/plan.go b/pkg/plugin/adapter/configuration/plan.go index 605be961696..5200dbdd422 100644 --- a/pkg/plugin/adapter/configuration/plan.go +++ b/pkg/plugin/adapter/configuration/plan.go @@ -113,8 +113,10 @@ func digestSourceDirectory(ctx context.Context, root, sourcePath string, result return err } +// excludedPlanSource skips mutable data, saved plans, and their bindings. Ordinary *.atmos.json +// configuration, such as settings.atmos.json, is a source and is digested. func excludedPlanSource(name string) bool { - return name == ".atmos" || name == ".git" || name == ".terraform" || name == ".terragrunt-cache" || name == ".terragrunt-stack" || strings.HasPrefix(name, "terraform.tfstate") || strings.HasSuffix(name, ".planfile") || strings.HasSuffix(name, ".atmos.json") + return name == ".atmos" || name == ".git" || name == ".terraform" || name == ".terragrunt-cache" || name == ".terragrunt-stack" || strings.HasPrefix(name, "terraform.tfstate") || strings.HasSuffix(name, ".planfile") || strings.HasSuffix(name, ".planfile.atmos.json") } func digestSource(ctx context.Context, root, path string, result map[string]string) error { diff --git a/pkg/plugin/adapter/configuration/plan_test.go b/pkg/plugin/adapter/configuration/plan_test.go index 9d74f0a33ea..4f2bfb82fb9 100644 --- a/pkg/plugin/adapter/configuration/plan_test.go +++ b/pkg/plugin/adapter/configuration/plan_test.go @@ -152,7 +152,7 @@ func TestPlanIdentityBindsWatchedSources(t *testing.T) { // Runtime artifacts and state never invalidate a plan, at any depth. for _, ignored := range []string{ "modules/.terraform/providers.json", "modules/.git/HEAD", "modules/terraform.tfstate", "modules/terraform.tfstate.backup", - "modules/saved.planfile", "modules/unit.atmos.json", "modules/.terragrunt-cache/x/main.tf", "modules/.terragrunt-stack/stack.hcl", + "modules/saved.planfile", "modules/saved.planfile.atmos.json", "modules/.terragrunt-cache/x/main.tf", "modules/.terragrunt-stack/stack.hcl", "modules/nested/.atmos/state", "modules/nested/.terraform/x", } { write(ignored, "noise") @@ -270,7 +270,7 @@ func TestSourceDigests(t *testing.T) { func TestExcludedPlanSource(t *testing.T) { for name, excluded := range map[string]bool{ ".atmos": true, ".git": true, ".terraform": true, ".terragrunt-cache": true, ".terragrunt-stack": true, - "terraform.tfstate": true, "terraform.tfstate.backup": true, "a.planfile": true, "b.atmos.json": true, + "terraform.tfstate": true, "terraform.tfstate.backup": true, "a.planfile": true, "a.planfile.atmos.json": true, "settings.atmos.json": false, "main.tf": false, "terragrunt.hcl": false, ".terraform.lock.hcl": false, "state.json": false, } { require.Equal(t, excluded, excludedPlanSource(name), name) diff --git a/pkg/plugin/adapter/configuration/preparation.go b/pkg/plugin/adapter/configuration/preparation.go index 871db483981..3761c45ff4c 100644 --- a/pkg/plugin/adapter/configuration/preparation.go +++ b/pkg/plugin/adapter/configuration/preparation.go @@ -34,12 +34,26 @@ func (s *Session) Prepare(ctx context.Context, info *schema.ConfigAndStacksInfo) if err != nil { return nil, err } - if result.Engine != selected.instance.Engine { + if result.Engine != expectedEngine(info, selected.instance) { return nil, errUtils.ErrPluginProtocol } return preparedConfiguration(result) } +// expectedEngine is the engine of the command this invocation runs. Discovery records the engine of +// the globally configured command, but --command or a component command may select another one, +// and the provider prepares for the binary it is given. Any other engine is a protocol violation. +func expectedEngine(info *schema.ConfigAndStacksInfo, instance *pluginv1.DiscoveredComponent) string { + if info.Command == "" { + return instance.Engine + } + // Match execution's executable-name detection (pkg/component/terraform/detection.go). + if strings.Contains(strings.ToLower(filepath.Base(info.Command)), "tofu") { + return "opentofu" + } + return "terraform" +} + // selectedComponent bundles the request, owning provider, and discovered instance for one component. type selectedComponent struct { request *pluginv1.ComponentRequest diff --git a/pkg/plugin/adapter/configuration/preparation_test.go b/pkg/plugin/adapter/configuration/preparation_test.go index b5da97f076f..e7da8bbff29 100644 --- a/pkg/plugin/adapter/configuration/preparation_test.go +++ b/pkg/plugin/adapter/configuration/preparation_test.go @@ -90,6 +90,30 @@ func TestPrepareFailsClosed(t *testing.T) { } } +func TestPrepareAcceptsTheEngineOfTheSelectedCommand(t *testing.T) { + for _, test := range []struct { + name, command, engine string + want error + }{ + {"command override selects OpenTofu", "/opt/bin/tofu", "opentofu", nil}, + {"versioned OpenTofu binary", "/opt/bin/TOFU-1.8", "opentofu", nil}, + {"command override selects Terraform", "/opt/bin/terraform", "terraform", nil}, + {"provider disagrees with the command", "/opt/bin/terraform", "opentofu", errUtils.ErrPluginProtocol}, + } { + t.Run(test.name, func(t *testing.T) { + session, provider, info := preparationSession(t) + info.Command = test.command + provider.EXPECT().Prepare(gomock.Any(), gomock.Any()).Return(&pluginv1.PrepareComponentResponse{Engine: test.engine, State: &pluginv1.ComponentStateIdentity{}}, nil) + _, err := session.Prepare(t.Context(), info) + if test.want == nil { + require.NoError(t, err) + return + } + require.ErrorIs(t, err, test.want) + }) + } +} + func TestPreparedExecutionRejectsNilEntriesInEveryList(t *testing.T) { for _, test := range []struct { name string diff --git a/pkg/terraform/output/configuration_state.go b/pkg/terraform/output/configuration_state.go index d5f7a2dd208..441cf5e0b06 100644 --- a/pkg/terraform/output/configuration_state.go +++ b/pkg/terraform/output/configuration_state.go @@ -72,6 +72,10 @@ func ConfigurationStateSections(sections map[string]any, state *schema.Component result := maps.Clone(sections) backend := maps.Clone(state.Backend) if state.BackendType == "local" && state.Workspace != "" && state.Workspace != "default" { + // A local backend may declare no settings at all; maps.Clone keeps that nil. + if backend == nil { + backend = make(map[string]any) + } root, _ := backend["workspace_dir"].(string) if root == "" { root = filepath.Join(state.WorkingDirectory, "terraform.tfstate.d") @@ -101,7 +105,7 @@ func configurationOutputConfig(config *ComponentConfig, state *schema.ComponentC config.Env = make(map[string]any) } config.Env["TF_DATA_DIR"] = filepath.Join(root, ".terraform") - config.BackendType, config.Backend, config.Workspace = state.BackendType, absoluteBackendFiles(state.Backend, state.WorkingDirectory), state.Workspace + config.BackendType, config.Backend, config.Workspace = state.BackendType, absoluteBackendFiles(state.BackendType, state.Backend, state.WorkingDirectory), state.Workspace config.AutoGenerateBackend = true config.AutoProvisionWorkdirForOutputs = false config.Providers, config.Vars = nil, nil @@ -124,19 +128,26 @@ func configurationEnvironment(managers []any) []string { // backendFileKeys name backend settings that hold file paths. Terraform resolves a relative // path against its working directory, which for output reads is the scratch directory. var backendFileKeys = []string{ - "path", "workspace_dir", "shared_credentials_file", "shared_credentials_files", "shared_config_files", "client_certificate_path", "oidc_token_file_path", } +// localBackendFileKeys hold file paths only for the local backend. Other backends use the +// same names for remote keys (Consul's "path" is a KV key), which must not be anchored. +var localBackendFileKeys = []string{"path", "workspace_dir"} + // absoluteBackendFiles anchors relative backend file paths at the state working directory, // so a scratch reader finds the same files as the unit's own execution. The input is unchanged. -func absoluteBackendFiles(backend map[string]any, directory string) map[string]any { +func absoluteBackendFiles(backendType string, backend map[string]any, directory string) map[string]any { if directory == "" || len(backend) == 0 { return backend } result := maps.Clone(backend) - for _, key := range backendFileKeys { + keys := backendFileKeys + if backendType == "local" { + keys = append(slices.Clone(localBackendFileKeys), backendFileKeys...) + } + for _, key := range keys { if value, ok := result[key]; ok { result[key] = anchorBackendFile(value, directory) } diff --git a/pkg/terraform/output/configuration_state_test.go b/pkg/terraform/output/configuration_state_test.go index 6278e56e8a1..53b0bf93f05 100644 --- a/pkg/terraform/output/configuration_state_test.go +++ b/pkg/terraform/output/configuration_state_test.go @@ -177,5 +177,23 @@ func TestOutputReaderAnchorsRelativeBackendFiles(t *testing.T) { require.Equal(t, "~/cert.pfx", config.Backend["client_certificate_path"], "home-relative paths are left to Terraform") require.Equal(t, "state", config.Backend["bucket"]) require.Equal(t, []any{"creds/aws", absolute}, backend["shared_credentials_files"], "the provider's state is not modified") - require.Equal(t, backend, absoluteBackendFiles(backend, "")) + require.Equal(t, backend, absoluteBackendFiles("s3", backend, "")) +} + +func TestOutputReaderAnchorsPathOnlyForLocalBackend(t *testing.T) { + unit := t.TempDir() + consul := map[string]any{"path": "live/vpc", "address": "consul:8500"} + require.Equal(t, "live/vpc", absoluteBackendFiles("consul", consul, unit)["path"], "a remote key is not a file") + local := map[string]any{"path": "state/terraform.tfstate", "workspace_dir": "ws"} + anchored := absoluteBackendFiles("local", local, unit) + require.Equal(t, filepath.Join(unit, "state", "terraform.tfstate"), anchored["path"]) + require.Equal(t, filepath.Join(unit, "ws"), anchored["workspace_dir"]) +} + +func TestConfigurationStateSectionsNamedWorkspaceWithoutBackendSettings(t *testing.T) { + root := t.TempDir() + state := &schema.ComponentConfigurationState{BackendType: "local", Workspace: "prod", WorkingDirectory: root} + resolved := ConfigurationStateSections(map[string]any{}, state) + require.Equal(t, filepath.Join(root, "terraform.tfstate.d", "prod", "terraform.tfstate"), resolved["backend"].(map[string]any)["path"]) + require.Nil(t, state.Backend) } diff --git a/plugins/terragrunt/internal/engine/source_watches.go b/plugins/terragrunt/internal/engine/source_watches.go index f61b1744edd..513965015d0 100644 --- a/plugins/terragrunt/internal/engine/source_watches.go +++ b/plugins/terragrunt/internal/engine/source_watches.go @@ -1,7 +1,9 @@ package engine import ( + "errors" "fmt" + "os" "path/filepath" "slices" "strings" @@ -17,13 +19,25 @@ import ( // trackInputSources inspects syntax, not values. Partial discovery intentionally // leaves inputs unevaluated; syntax watches include conditional and optional reads // without reading state, decrypting secrets, or invoking user commands. +// +// Configuration that inputs read (read_terragrunt_config) is scanned the same way, transitively, +// so a change to its own inputs' sources affects the unit. Each file is scanned once. func trackInputSources(fs afero.Fs, root string, result *model.Resolution) error { - for _, path := range slices.Clone(result.Files) { - if !strings.HasSuffix(path, ".hcl") { + evaluated := len(result.Files) + scanned := map[string]bool{} + for i := 0; i < len(result.Files); i++ { + path := result.Files[i] + if scanned[path] || !strings.HasSuffix(path, ".hcl") { continue } + scanned[path] = true contents, err := afero.ReadFile(fs, path) if err != nil { + // A file found only by this scan may be an optional read that does not exist yet; + // it stays a watch. Every evaluated file must be readable. + if i >= evaluated && errors.Is(err, os.ErrNotExist) { + continue + } return fmt.Errorf("track configuration sources %s: %w", path, err) } file, diagnostics := hclsyntax.ParseConfig(contents, path, hcl.InitialPos) diff --git a/plugins/terragrunt/internal/engine/source_watches_test.go b/plugins/terragrunt/internal/engine/source_watches_test.go index f85478cb678..3d204ff2d62 100644 --- a/plugins/terragrunt/internal/engine/source_watches_test.go +++ b/plugins/terragrunt/internal/engine/source_watches_test.go @@ -73,3 +73,31 @@ func TestInputWatchesFailuresAndIncludedInputs(t *testing.T) { require.NoError(t, trackInputSources(fs, root, result)) require.Contains(t, result.Files, filepath.Join(root, "shared-input.hcl")) } + +func TestInputWatchesFollowNestedConfigurationReads(t *testing.T) { + root := t.TempDir() + fs := afero.NewMemMapFs() + require.NoError(t, fs.MkdirAll(filepath.Join(root, "shared"), 0o700)) + unit := filepath.Join(root, "terragrunt.hcl") + write := func(path, contents string) { + t.Helper() + require.NoError(t, afero.WriteFile(fs, path, []byte(contents), 0o600)) + } + write(unit, `inputs = { common = read_terragrunt_config("shared/common.hcl") }`) + // The nested configuration reads data and, cyclically, the unit; both are followed once. + write(filepath.Join(root, "shared", "common.hcl"), `inputs = { + data = file("data.json") + again = read_terragrunt_config("../terragrunt.hcl") + deeper = read_terragrunt_config("absent.hcl") +}`) + result := &model.Resolution{Files: []string{unit}} + require.NoError(t, trackInputSources(fs, root, result)) + for _, path := range []string{ + filepath.Join(root, "shared", "common.hcl"), + filepath.Join(root, "shared", "data.json"), + filepath.Join(root, "shared", "absent.hcl"), + } { + require.Contains(t, result.Files, path, "a transitive read is watched, even before it exists") + } + require.Empty(t, result.Folders) +} diff --git a/plugins/terragrunt/internal/provider/discovery.go b/plugins/terragrunt/internal/provider/discovery.go index 9a2984609d3..a288bf2b0aa 100644 --- a/plugins/terragrunt/internal/provider/discovery.go +++ b/plugins/terragrunt/internal/provider/discovery.go @@ -116,6 +116,8 @@ func importPaths(request *pluginv1.DiscoverComponentsRequest) ([]string, error) if !found || !filepath.IsLocal(path) || strings.ContainsAny(path, "\\?#*[{") { return nil, ErrUnsupported } + // Match the engine's clean relative unit paths, whatever spelling the import used. + path = filepath.ToSlash(filepath.Clean(filepath.FromSlash(path))) info, err := os.Stat(filepath.Join(request.RootDirectory, filepath.FromSlash(path))) if err != nil { return nil, err diff --git a/plugins/terragrunt/internal/provider/imports_test.go b/plugins/terragrunt/internal/provider/imports_test.go index ede1090584c..b46cf5d1a50 100644 --- a/plugins/terragrunt/internal/provider/imports_test.go +++ b/plugins/terragrunt/internal/provider/imports_test.go @@ -29,7 +29,7 @@ func TestImportsSelectExplicitFilesAndDirectories(t *testing.T) { }) _, err = p.Discover(t.Context(), request) require.NoError(t, err) - for uri, want := range map[string]string{"terragrunt://.": "**", "terragrunt://./": "**", "terragrunt://live/": "live/**", "terragrunt://./live": "live/**"} { + for uri, want := range map[string]string{"terragrunt://.": "**", "terragrunt://./": "**", "terragrunt://live/": "live/**", "terragrunt://./live": "live/**", "terragrunt://./live/terragrunt.stack.hcl": "live/terragrunt.stack.hcl", "terragrunt://live//terragrunt.stack.hcl": "live/terragrunt.stack.hcl"} { request.Imports = []string{uri} got, err := importPaths(request) require.NoError(t, err, uri) diff --git a/plugins/terragrunt/internal/provider/provider.go b/plugins/terragrunt/internal/provider/provider.go index ea6c58bdc28..5a10d6b8560 100644 --- a/plugins/terragrunt/internal/provider/provider.go +++ b/plugins/terragrunt/internal/provider/provider.go @@ -110,7 +110,9 @@ func operation(root, name string, op *pluginv1.ComponentOperation, invocation *p } func engineName(binary string) string { - if strings.TrimSuffix(filepath.Base(binary), ".exe") == "tofu" { + // Match the host's executable-name detection, which treats any basename containing + // "tofu", case-insensitively, as OpenTofu. + if strings.Contains(strings.ToLower(filepath.Base(binary)), "tofu") { return "opentofu" } return "terraform" diff --git a/plugins/terragrunt/scripts/lifecycle_extended.star b/plugins/terragrunt/scripts/lifecycle_extended.star index 8a7eb9552f4..e73e278df84 100644 --- a/plugins/terragrunt/scripts/lifecycle_extended.star +++ b/plugins/terragrunt/scripts/lifecycle_extended.star @@ -237,6 +237,19 @@ def verify_missing_required_var_file(root): fs.write_file(path, content) +def verify_fresh_unit_missing_required_var_file(root, binary): + # A never-initialized unit whose required variable file is absent fails before implicit init: + # no init or plan hook runs, and OpenTofu never initializes the working directory. + write_unit(root, "fresh", hooked_unit(binary, "fresh")) + result = terraform(root, "plan", "fresh", failing=True) + output = result.stderr + result.stdout + fixtures.check("required.tfvars" in output, "Missing var file was not named:\n" + output) + expect_count(result.stderr, "Executing before hook", 0, "fresh unit stderr") + fixtures.check("Initializing" not in output, "Implicit init ran before the var file check:\n" + output) + data_dirs = fs.glob(root + "/.workdir/terraform/" + STACK + "-fresh*/.terraform") + fixtures.check(not data_dirs, "Implicit init initialized the fresh unit: " + str(data_dirs)) + + def verify_failing_before_hook(root, binary): write_unit(root, "gated", GATED_UNIT.replace("@TOOL@", tool_of(binary))) fixtures.write(root, "live/dev/gated/unformatted.tf", 'variable "x" {}\n') @@ -269,8 +282,9 @@ def hooks(): root = project(fixtures.workspace("atmos-terragrunt-hooks-"), "project", package, binary) verify_hook_and_argument_ordering(root, binary) verify_missing_required_var_file(root) + verify_fresh_unit_missing_required_var_file(root, binary) verify_failing_before_hook(root, binary) - ui.success("Hooks and extra_arguments: ordering, scoping, exit codes, error hooks, var files, env") + ui.success("Hooks and extra_arguments: ordering, scoping, exit codes, error hooks, var files before init, env") PLAN_UNIT = """ From 40d7bd290a457a748758ec393606d888ce99d46d Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Tue, 10 Nov 2026 15:53:27 -0600 Subject: [PATCH 14/52] fix(affected): validate watch_excludes and reuse the filtered watch index Reject a watch_excludes value that is present but not a list, wrap the path-relativization failure with ErrPluginInvalidConfig, and cache the filtered index per change set and exclusion set so provider components sharing exclusions reuse one index and its resolved view. Co-Authored-By: Claude Opus 5.5 --- internal/exec/configuration_imports_test.go | 14 ++++- .../exec/describe_affected_configuration.go | 60 ++++++++++++++++--- 2 files changed, 65 insertions(+), 9 deletions(-) diff --git a/internal/exec/configuration_imports_test.go b/internal/exec/configuration_imports_test.go index bf34bb1849e..26503d1231d 100644 --- a/internal/exec/configuration_imports_test.go +++ b/internal/exec/configuration_imports_test.go @@ -77,9 +77,21 @@ func TestConfigurationWatchExclusionsDoNotChangeSharedIndex(t *testing.T) { require.NoError(t, err) require.Equal(t, []string{filepath.Join(root, "live", "new.yaml")}, filtered.getAllFiles()) require.Len(t, index.getAllFiles(), 2) + again, err := configurationWatchIndex(config, settings, index) + require.NoError(t, err) + require.Same(t, filtered, again, "components sharing exclusions must reuse one filtered index") + other, err := configurationWatchIndex(config, map[string]any{"configuration": map[string]any{"watch_excludes": []any{"**/*.yaml"}}}, index) + require.NoError(t, err) + require.NotSame(t, filtered, other) + require.Equal(t, []string{filepath.Join(root, "live", ".terragrunt-cache", "generated.tf")}, other.getAllFiles()) unchanged, err := configurationWatchIndex(config, nil, index) require.NoError(t, err) require.Same(t, index, unchanged) + empty, err := configurationWatchIndex(config, map[string]any{"configuration": map[string]any{"watch_excludes": []any{}}}, index) + require.NoError(t, err) + require.Same(t, index, empty) + _, err = configurationWatchIndex(config, map[string]any{"configuration": map[string]any{"watch_excludes": "**/.terragrunt-cache/**"}}, index) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) for _, invalid := range []any{"[", 42} { settings["configuration"].(map[string]any)["watch_excludes"] = []any{invalid} _, err = configurationWatchIndex(config, settings, index) @@ -94,7 +106,7 @@ func TestConfigurationWatchExclusionsFailClosedOnPathsOutsideTheProject(t *testi index := newChangedFilesIndex(config, []string{absolute}, "") settings := map[string]any{"configuration": map[string]any{"watch_excludes": []any{"**/*.tf"}}} filtered, err := configurationWatchIndex(config, settings, index) - require.Error(t, err) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) require.Nil(t, filtered) } diff --git a/internal/exec/describe_affected_configuration.go b/internal/exec/describe_affected_configuration.go index 4b706ec7b98..9fb0c4cb28e 100644 --- a/internal/exec/describe_affected_configuration.go +++ b/internal/exec/describe_affected_configuration.go @@ -4,6 +4,7 @@ import ( "fmt" "path/filepath" "slices" + "strings" "sync/atomic" "github.com/bmatcuk/doublestar/v4" @@ -33,11 +34,38 @@ func rebaseConfigurationDiscovery(config *schema.AtmosConfiguration) error { // configurationWatchIndex applies provider-declared cache exclusions only to its // file watches. It never changes YAML dependencies or the shared Git change set. func configurationWatchIndex(config *schema.AtmosConfiguration, settings map[string]any, original *changedFilesIndex) (*changedFilesIndex, error) { - provider, _ := settings["configuration"].(map[string]any) - excludes, _ := provider["watch_excludes"].([]any) - if len(excludes) == 0 { + patterns, err := configurationWatchExcludes(settings) + if err != nil { + return nil, err + } + if len(patterns) == 0 { return original, nil } + key := config.BasePathAbsolute + "\x00" + strings.Join(patterns, "\x00") + if view := lastConfigurationWatchIndex.Load(); view != nil && view.source == original && view.key == key { + return view.filtered, nil + } + files, err := unexcludedWatchFiles(config.BasePathAbsolute, original.getAllFiles(), patterns) + if err != nil { + return nil, err + } + filtered := newChangedFilesIndex(config, files, config.BasePathAbsolute) + lastConfigurationWatchIndex.Store(&configurationWatchIndexView{source: original, key: key, filtered: filtered}) + return filtered, nil +} + +// configurationWatchExcludes reads and validates the provider's watch_excludes. +// An absent or empty setting yields no patterns; any other shape is rejected. +func configurationWatchExcludes(settings map[string]any) ([]string, error) { + provider, _ := settings["configuration"].(map[string]any) + raw := provider["watch_excludes"] + if raw == nil { + return nil, nil + } + excludes, ok := raw.([]any) + if !ok { + return nil, fmt.Errorf("%w: watch_excludes must be a list of patterns", errUtils.ErrPluginInvalidConfig) + } patterns := make([]string, 0, len(excludes)) for _, value := range excludes { pattern, ok := value.(string) @@ -46,11 +74,16 @@ func configurationWatchIndex(config *schema.AtmosConfiguration, settings map[str } patterns = append(patterns, pattern) } - files := make([]string, 0, len(original.getAllFiles())) - for _, path := range original.getAllFiles() { - relative, err := filepath.Rel(config.BasePathAbsolute, path) + return patterns, nil +} + +// unexcludedWatchFiles returns the files under root that match none of the patterns. +func unexcludedWatchFiles(root string, all []string, patterns []string) ([]string, error) { + files := make([]string, 0, len(all)) + for _, path := range all { + relative, err := filepath.Rel(root, path) if err != nil { - return nil, err + return nil, fmt.Errorf("%w: watch path %q is not under %q: %w", errUtils.ErrPluginInvalidConfig, path, root, err) } excluded := false for _, pattern := range patterns { @@ -61,9 +94,20 @@ func configurationWatchIndex(config *schema.AtmosConfiguration, settings map[str files = append(files, path) } } - return newChangedFilesIndex(config, files, config.BasePathAbsolute), nil + return files, nil +} + +// configurationWatchIndexView pairs a change-set index and exclusion set with its filtered copy. +type configurationWatchIndexView struct { + source, filtered *changedFilesIndex + key string } +// lastConfigurationWatchIndex holds the most recent filtered index, so provider +// components that share exclusions reuse one index (and its resolved view) +// instead of rebuilding it per component. Indexes are immutable after construction. +var lastConfigurationWatchIndex atomic.Pointer[configurationWatchIndexView] + // configurationWatchPaths resolves provider-owned watches against the project // root without putting checkout-specific absolute paths into stack comparisons. // Existing YAML dependency path semantics remain unchanged. It reports whether From c73d7f930fe06843c95754270d80ef29751925f5 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Wed, 11 Nov 2026 15:28:42 -0600 Subject: [PATCH 15/52] fix(plugin): preserve binary request in plan fingerprint --- pkg/plugin/adapter/configuration/plan.go | 4 ++-- pkg/plugin/adapter/configuration/plan_test.go | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/pkg/plugin/adapter/configuration/plan.go b/pkg/plugin/adapter/configuration/plan.go index 5200dbdd422..8ac8410c5bd 100644 --- a/pkg/plugin/adapter/configuration/plan.go +++ b/pkg/plugin/adapter/configuration/plan.go @@ -54,9 +54,9 @@ func (s *Session) PlanIdentity(ctx context.Context, info *schema.ConfigAndStacks } payload, err := json.Marshal(struct { Provider string - Request string + Request []byte Sources map[string]string - }{identity, string(raw), files}) + }{identity, raw, files}) if err != nil { return "", err } diff --git a/pkg/plugin/adapter/configuration/plan_test.go b/pkg/plugin/adapter/configuration/plan_test.go index 4f2bfb82fb9..75eeb7816e4 100644 --- a/pkg/plugin/adapter/configuration/plan_test.go +++ b/pkg/plugin/adapter/configuration/plan_test.go @@ -3,6 +3,7 @@ package configuration import ( "context" "errors" + "math" "os" "path/filepath" "testing" @@ -100,6 +101,13 @@ func TestPlanIdentityDetectsEveryBoundInputAndIgnoresInvocationOnlyInputs(t *tes } } +func TestPlanIdentityDistinguishesBinaryNumericOptions(t *testing.T) { + first, second := defaultPlanCase(), defaultPlanCase() + first.options = map[string]any{"value": math.Float64frombits(0x3ff0000000000080)} + second.options = map[string]any{"value": math.Float64frombits(0x3ff0000000000081)} + require.NotEqual(t, planIdentity(t, first), planIdentity(t, second)) +} + func TestPlanIdentityIsStableAcrossProjectRoots(t *testing.T) { // Different checkouts of identical content must accept each other's saved plans. first, info := planSession(t, defaultPlanCase()) From d0408c39f480e87bf42687220a48d85a070d12d9 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Wed, 11 Nov 2026 15:30:32 -0600 Subject: [PATCH 16/52] fix(terragrunt): initialize empty local backend configuration --- plugins/terragrunt/internal/engine/backend.go | 3 +++ plugins/terragrunt/internal/engine/backend_test.go | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/plugins/terragrunt/internal/engine/backend.go b/plugins/terragrunt/internal/engine/backend.go index 0ee6d64ebef..177fffdc4f0 100644 --- a/plugins/terragrunt/internal/engine/backend.go +++ b/plugins/terragrunt/internal/engine/backend.go @@ -21,6 +21,9 @@ func resolveBackend(cfg *config.TerragruntConfig, result *model.Resolution) erro if result.BackendType != "local" { return nil } + if result.Backend == nil { + result.Backend = make(map[string]any) + } // Local state remains relative to Terragrunt's source cache identity, // independently of the execution directory chosen by Atmos. for key, fallback := range map[string]string{"path": "terraform.tfstate", "workspace_dir": "terraform.tfstate.d"} { diff --git a/plugins/terragrunt/internal/engine/backend_test.go b/plugins/terragrunt/internal/engine/backend_test.go index 1b5656c6968..8454587cb21 100644 --- a/plugins/terragrunt/internal/engine/backend_test.go +++ b/plugins/terragrunt/internal/engine/backend_test.go @@ -32,6 +32,18 @@ func TestCloudBackendIdentityWithoutNetwork(t *testing.T) { } } +func TestLocalBackendWithoutConfigUsesDefaults(t *testing.T) { + request := nativeUnit(t, `remote_state { + backend = "local" + config = null +}`) + request.Phase = "backend" + result, err := Resolve(t.Context(), request) + require.NoError(t, err) + require.Equal(t, filepath.Join(result.WorkingDir, "terraform.tfstate"), result.Backend["path"]) + require.Equal(t, filepath.Join(result.WorkingDir, "terraform.tfstate.d"), result.Backend["workspace_dir"]) +} + func TestRemoteBackendGenerationRetainsPathAndPolicy(t *testing.T) { request := nativeUnit(t, `remote_state { backend = "s3" From eda517c1eefcea3dc487023e6ab33d922d1f0403 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Wed, 11 Nov 2026 15:33:40 -0600 Subject: [PATCH 17/52] fix(affected): reuse filtered indexes per change set --- internal/exec/configuration_imports_test.go | 7 +++++ .../describe_affected_changed_files_index.go | 4 +++ .../exec/describe_affected_configuration.go | 30 +++++++++---------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/internal/exec/configuration_imports_test.go b/internal/exec/configuration_imports_test.go index 26503d1231d..dbdc22d346d 100644 --- a/internal/exec/configuration_imports_test.go +++ b/internal/exec/configuration_imports_test.go @@ -83,6 +83,13 @@ func TestConfigurationWatchExclusionsDoNotChangeSharedIndex(t *testing.T) { other, err := configurationWatchIndex(config, map[string]any{"configuration": map[string]any{"watch_excludes": []any{"**/*.yaml"}}}, index) require.NoError(t, err) require.NotSame(t, filtered, other) + again, err = configurationWatchIndex(config, settings, index) + require.NoError(t, err) + require.Same(t, filtered, again, "alternating exclusion sets must reuse their views") + independent := newChangedFilesIndex(config, index.getAllFiles(), root) + independentView, err := configurationWatchIndex(config, settings, independent) + require.NoError(t, err) + require.NotSame(t, filtered, independentView, "separate change sets must own their views") require.Equal(t, []string{filepath.Join(root, "live", ".terragrunt-cache", "generated.tf")}, other.getAllFiles()) unchanged, err := configurationWatchIndex(config, nil, index) require.NoError(t, err) diff --git a/internal/exec/describe_affected_changed_files_index.go b/internal/exec/describe_affected_changed_files_index.go index 04358e2a82e..599fa68071c 100644 --- a/internal/exec/describe_affected_changed_files_index.go +++ b/internal/exec/describe_affected_changed_files_index.go @@ -24,6 +24,10 @@ type changedFilesIndex struct { // outside a component base path, such as native Helm values_files. allFilesSet map[string]struct{} + // watchMu protects filtered views for this change set. + watchMu sync.Mutex + watchIndexes map[string]*changedFilesIndex + mu sync.RWMutex } diff --git a/internal/exec/describe_affected_configuration.go b/internal/exec/describe_affected_configuration.go index 9fb0c4cb28e..39d6dc4e104 100644 --- a/internal/exec/describe_affected_configuration.go +++ b/internal/exec/describe_affected_configuration.go @@ -1,10 +1,10 @@ package exec import ( + "encoding/json" "fmt" "path/filepath" "slices" - "strings" "sync/atomic" "github.com/bmatcuk/doublestar/v4" @@ -41,16 +41,25 @@ func configurationWatchIndex(config *schema.AtmosConfiguration, settings map[str if len(patterns) == 0 { return original, nil } - key := config.BasePathAbsolute + "\x00" + strings.Join(patterns, "\x00") - if view := lastConfigurationWatchIndex.Load(); view != nil && view.source == original && view.key == key { - return view.filtered, nil + keyBytes, err := json.Marshal(append([]string{config.BasePathAbsolute}, patterns...)) + if err != nil { + return nil, err + } + key := string(keyBytes) + original.watchMu.Lock() + defer original.watchMu.Unlock() + if filtered := original.watchIndexes[key]; filtered != nil { + return filtered, nil } files, err := unexcludedWatchFiles(config.BasePathAbsolute, original.getAllFiles(), patterns) if err != nil { return nil, err } filtered := newChangedFilesIndex(config, files, config.BasePathAbsolute) - lastConfigurationWatchIndex.Store(&configurationWatchIndexView{source: original, key: key, filtered: filtered}) + if original.watchIndexes == nil { + original.watchIndexes = make(map[string]*changedFilesIndex) + } + original.watchIndexes[key] = filtered return filtered, nil } @@ -97,17 +106,6 @@ func unexcludedWatchFiles(root string, all []string, patterns []string) ([]strin return files, nil } -// configurationWatchIndexView pairs a change-set index and exclusion set with its filtered copy. -type configurationWatchIndexView struct { - source, filtered *changedFilesIndex - key string -} - -// lastConfigurationWatchIndex holds the most recent filtered index, so provider -// components that share exclusions reuse one index (and its resolved view) -// instead of rebuilding it per component. Indexes are immutable after construction. -var lastConfigurationWatchIndex atomic.Pointer[configurationWatchIndexView] - // configurationWatchPaths resolves provider-owned watches against the project // root without putting checkout-specific absolute paths into stack comparisons. // Existing YAML dependency path semantics remain unchanged. It reports whether From 1b1bbe80d28b7379d716bfbe0e1d9520ba394390 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Wed, 11 Nov 2026 15:37:31 -0600 Subject: [PATCH 18/52] test(terragrunt): verify installed unit identities --- plugins/terragrunt/scripts/release.star | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/terragrunt/scripts/release.star b/plugins/terragrunt/scripts/release.star index 809d79c60c2..df0597b0ead 100644 --- a/plugins/terragrunt/scripts/release.star +++ b/plugins/terragrunt/scripts/release.star @@ -54,7 +54,9 @@ def install_and_discover(work, package, release): fixtures.check("path" not in installed, "Installed package still uses a development path") result = fixtures.run(project, ["list", "instances", "--format=json"]) rows = json.decode(result.stdout) - fixtures.check(len(rows) == 4, "Installed plugin did not discover four explicit-stack units") + identities = sorted([(row["Component"], row["Stack"]) for row in rows]) + expected = [(name, "live") for name in ["dev/app", "dev/network", "prod/app", "prod/network"]] + fixtures.check(identities == expected, "Unexpected installed unit identities: " + str(identities)) fixtures.check(all([row["Type"] == "terraform" for row in rows]), "Installed units lost their component type") fixtures.check(not fs.exists(project + "/live/.terragrunt-stack"), "Installed discovery wrote generated stacks") From 08fc1c9782c5de632f7f5860cfaed62dc599a8c1 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Wed, 11 Nov 2026 15:42:48 -0600 Subject: [PATCH 19/52] fix(output): anchor default local state paths --- pkg/terraform/output/configuration_state.go | 12 +++++++++++ .../output/configuration_state_test.go | 20 ++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/pkg/terraform/output/configuration_state.go b/pkg/terraform/output/configuration_state.go index 441cf5e0b06..723e1786f5e 100644 --- a/pkg/terraform/output/configuration_state.go +++ b/pkg/terraform/output/configuration_state.go @@ -106,6 +106,18 @@ func configurationOutputConfig(config *ComponentConfig, state *schema.ComponentC } config.Env["TF_DATA_DIR"] = filepath.Join(root, ".terraform") config.BackendType, config.Backend, config.Workspace = state.BackendType, absoluteBackendFiles(state.BackendType, state.Backend, state.WorkingDirectory), state.Workspace + if state.BackendType == "local" && state.WorkingDirectory != "" { + config.Backend = maps.Clone(config.Backend) + if config.Backend == nil { + config.Backend = make(map[string]any) + } + if path, _ := config.Backend["path"].(string); path == "" { + config.Backend["path"] = filepath.Join(state.WorkingDirectory, "terraform.tfstate") + } + if dir, _ := config.Backend["workspace_dir"].(string); dir == "" { + config.Backend["workspace_dir"] = filepath.Join(state.WorkingDirectory, "terraform.tfstate.d") + } + } config.AutoGenerateBackend = true config.AutoProvisionWorkdirForOutputs = false config.Providers, config.Vars = nil, nil diff --git a/pkg/terraform/output/configuration_state_test.go b/pkg/terraform/output/configuration_state_test.go index 53b0bf93f05..ba047485de8 100644 --- a/pkg/terraform/output/configuration_state_test.go +++ b/pkg/terraform/output/configuration_state_test.go @@ -98,7 +98,7 @@ func TestPluginOutputUsesBackendOnlyScratchDirectory(t *testing.T) { config := validAtmosConfig(t) stateRoot := t.TempDir() config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { - return &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{"path": filepath.Join(stateRoot, "default.tfstate"), "workspace_dir": filepath.Join(stateRoot, "workspaces")}, Workspace: "prod", WorkingDirectory: stateRoot}, nil + return &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{"path": filepath.Join(stateRoot, "default.tfstate")}, Workspace: "prod", WorkingDirectory: stateRoot}, nil }} sections := validSections() sections["settings"] = map[string]any{"configuration": true} @@ -115,6 +115,12 @@ func TestPluginOutputUsesBackendOnlyScratchDirectory(t *testing.T) { require.NoError(t, err) require.Len(t, files, 1) require.Equal(t, "backend.tf.json", files[0].Name()) + data, err := os.ReadFile(filepath.Join(dir, "backend.tf.json")) + require.NoError(t, err) + var backend map[string]any + require.NoError(t, json.Unmarshal(data, &backend)) + local := backend["terraform"].(map[string]any)["backend"].(map[string]any)["local"].(map[string]any) + require.Equal(t, filepath.Join(stateRoot, "terraform.tfstate.d"), local["workspace_dir"]) return runner, nil })) result, err := executor.execute(t.Context(), config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true) @@ -190,6 +196,18 @@ func TestOutputReaderAnchorsPathOnlyForLocalBackend(t *testing.T) { require.Equal(t, filepath.Join(unit, "ws"), anchored["workspace_dir"]) } +func TestOutputReaderDefaultsLocalBackendPaths(t *testing.T) { + unit := t.TempDir() + state := &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{}, WorkingDirectory: unit} + config := &ComponentConfig{} + cleanup, err := configurationOutputConfig(config, state) + require.NoError(t, err) + t.Cleanup(cleanup) + require.Equal(t, filepath.Join(unit, "terraform.tfstate"), config.Backend["path"]) + require.Equal(t, filepath.Join(unit, "terraform.tfstate.d"), config.Backend["workspace_dir"]) + require.Empty(t, state.Backend) +} + func TestConfigurationStateSectionsNamedWorkspaceWithoutBackendSettings(t *testing.T) { root := t.TempDir() state := &schema.ComponentConfigurationState{BackendType: "local", Workspace: "prod", WorkingDirectory: root} From 65a62af17280361074356e20859f84dc34c87c34 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Wed, 11 Nov 2026 15:53:57 -0600 Subject: [PATCH 20/52] fix(terraform): bind downloaded modules to saved plans --- pkg/component/terraform/configuration_plan.go | 34 +++++++++++++++++-- .../terraform/configuration_plan_test.go | 22 +++++++++++- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go index 34ca457f9bd..d4e67bf98e5 100644 --- a/pkg/component/terraform/configuration_plan.go +++ b/pkg/component/terraform/configuration_plan.go @@ -274,7 +274,7 @@ func configurationWorkdirDigest(root, planPath string) (string, error) { } return nil } - if ignoredWorkdirFile(path, planPath, entry.Name()) { + if path == planPath || path == planPath+configurationPlanSuffix || insideTerraformCacheOutsideModules(root, path) || (!insideTerraformModules(root, path) && ignoredWorkdirFile(path, planPath, entry.Name())) { return nil } return recordWorkdirFile(root, path, entry, files) @@ -294,7 +294,37 @@ func configurationWorkdirDigest(root, planPath string) (string, error) { // The .atmos directory is provisioner bookkeeping (access timestamps and metadata lock files // that even a read-only lookup of the retained directory creates), never executed content. func skipWorkdirDirectory(root, path, name string) bool { - return path != root && (name == ".terraform" || name == ".git" || name == ".terragrunt-cache" || name == ".atmos") + return path != root && (name == ".git" || name == ".terragrunt-cache" || name == ".atmos" || insideTerraformCacheOutsideModules(root, path)) +} + +// Terraform's downloaded modules can contain scripts executed during apply. Other cache +// content, including providers and state, remains mutable and is excluded. +func insideTerraformCacheOutsideModules(root, path string) bool { + relative, err := filepath.Rel(root, path) + if err != nil { + return false + } + parts := strings.Split(relative, string(filepath.Separator)) + for i, part := range parts { + if part == ".terraform" && i+1 < len(parts) && parts[i+1] != "modules" { + return true + } + } + return false +} + +func insideTerraformModules(root, path string) bool { + relative, err := filepath.Rel(root, path) + if err != nil { + return false + } + parts := strings.Split(relative, string(filepath.Separator)) + for i, part := range parts { + if part == ".terraform" && i+1 < len(parts) && parts[i+1] == "modules" { + return true + } + } + return false } // ignoredWorkdirFile reports whether a file is the plan itself, a plan binding, or mutable state. diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index 459dcaf3e9a..69992502c2d 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -70,9 +70,14 @@ func TestConfigurationPlanRestoresReviewedInputs(t *testing.T) { } func TestConfigurationPlanRejectsChangedBinding(t *testing.T) { - for _, change := range []string{"plan", "engine", "provider", "workdir", "backend", "workspace", "coordinates", "missing binding", "corrupt binding", "missing plan", "runtime error", "unsupported operation", "stored plan"} { + for _, change := range []string{"plan", "engine", "provider", "workdir", "downloaded module", "backend", "workspace", "coordinates", "missing binding", "corrupt binding", "missing plan", "runtime error", "unsupported operation", "stored plan"} { t.Run(change, func(t *testing.T) { config, info, execCtx, runtime, path := planFixture(t) + module := filepath.Join(execCtx.workingDir, ".terraform", "modules", "child", "script.sh") + if change == "downloaded module" { + require.NoError(t, os.MkdirAll(filepath.Dir(module), 0o700)) + require.NoError(t, os.WriteFile(module, []byte("reviewed"), 0o600)) + } require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"plan", "-out=" + path})) info.SubCommand = "apply" info.UseTerraformPlan = true @@ -86,6 +91,8 @@ func TestConfigurationPlanRejectsChangedBinding(t *testing.T) { runtime.identity = "other provider" case "workdir": require.NoError(t, os.WriteFile(filepath.Join(execCtx.workingDir, "main.tf"), []byte("modified"), 0o600)) + case "downloaded module": + require.NoError(t, os.WriteFile(module, []byte("changed"), 0o600)) case "backend": runtime.state.Backend = map[string]any{"path": "other"} case "workspace": @@ -172,3 +179,16 @@ func TestConfigurationWorkdirDigestIgnoresMutableFiles(t *testing.T) { _, err = configurationWorkdirDigest(root, plan) require.True(t, errors.Is(err, errUtils.ErrPluginUnsupportedExtension)) } + +func TestConfigurationWorkdirDigestIncludesDownloadedModules(t *testing.T) { + root := t.TempDir() + module := filepath.Join(root, ".terraform", "modules", "child", "script.sh") + require.NoError(t, os.MkdirAll(filepath.Dir(module), 0o755)) + require.NoError(t, os.WriteFile(module, []byte("before"), 0o600)) + before, err := configurationWorkdirDigest(root, "") + require.NoError(t, err) + require.NoError(t, os.WriteFile(module, []byte("after"), 0o600)) + after, err := configurationWorkdirDigest(root, "") + require.NoError(t, err) + require.NotEqual(t, before, after) +} From 91a0a317d00d2cc7f476a5aac0e97c174d1c9473 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Wed, 11 Nov 2026 16:03:12 -0600 Subject: [PATCH 21/52] test(terraform): verify reviewed plan reaches apply --- .../terraform/configuration_plan_pipeline_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/pkg/component/terraform/configuration_plan_pipeline_test.go b/pkg/component/terraform/configuration_plan_pipeline_test.go index 7308ed66952..9c95e195506 100644 --- a/pkg/component/terraform/configuration_plan_pipeline_test.go +++ b/pkg/component/terraform/configuration_plan_pipeline_test.go @@ -4,6 +4,7 @@ import ( "context" "os" "path/filepath" + "strings" "testing" "github.com/stretchr/testify/require" @@ -108,6 +109,17 @@ func TestApplyOfReviewedPlanReusesReviewedPreparationAndWorkdir(t *testing.T) { require.Equal(t, "# reviewed module", string(retained)) require.Equal(t, planPath, apply.PlanFile) require.True(t, apply.UseTerraformPlan) + + // Run the prepared invocation through the command pipeline and inspect the + // actual subprocess arguments, rather than only the restored metadata. + argsFile := filepath.Join(project.root, "apply-args") + apply.SkipInit = true + apply.ComponentBackendType = "http" // Avoid a separate workspace subprocess. + apply.ComponentEnvList = append(apply.ComponentEnvList, "_ATMOS_TEST_ARGS_FILE="+argsFile) + require.NoError(t, testExecutor(t).executeCommandPipeline(project.config, apply, execution)) + args, err := os.ReadFile(argsFile) + require.NoError(t, err) + require.Contains(t, strings.Split(string(args), "\n"), planPath) } func TestApplyOfReviewedPlanRejectsChangedWorkdir(t *testing.T) { From 707401c7d6f24441f2be4af5c74fbfc76c14b8a4 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Wed, 11 Nov 2026 16:10:16 -0600 Subject: [PATCH 22/52] refactor(configuration): own provider import and watch policy --- internal/exec/configuration_imports.go | 35 -------- internal/exec/configuration_imports_test.go | 32 ------- .../exec/describe_affected_configuration.go | 50 +---------- internal/exec/stack_processor_utils.go | 3 +- pkg/component/configuration/imports.go | 88 +++++++++++++++++++ pkg/component/configuration/registry_test.go | 60 +++++++++++++ 6 files changed, 153 insertions(+), 115 deletions(-) delete mode 100644 internal/exec/configuration_imports.go create mode 100644 pkg/component/configuration/imports.go diff --git a/internal/exec/configuration_imports.go b/internal/exec/configuration_imports.go deleted file mode 100644 index 158183d7f6b..00000000000 --- a/internal/exec/configuration_imports.go +++ /dev/null @@ -1,35 +0,0 @@ -package exec - -import ( - "context" - "fmt" - "strings" - - errUtils "github.com/cloudposse/atmos/errors" - "github.com/cloudposse/atmos/pkg/schema" -) - -// registerConfigurationImport routes configured provider schemes before YAML or -// remote-source resolution. Providers add instances to the shared inventory; -// importing a root does not rename its stacks or merge HCL into YAML variables. -func registerConfigurationImport(config *schema.AtmosConfiguration, uri string, declaration *schema.StackImport) (bool, error) { - scheme, _, found := strings.Cut(uri, "://") - if !found || config == nil { - return false, nil - } - settings, configured := config.Components.Terraform.ConfigurationProviders[scheme] - if !configured { - return false, nil - } - if !settings.Enabled { - return true, fmt.Errorf("%w: configuration provider %s is disabled", errUtils.ErrPluginInvalidConfig, scheme) - } - if len(declaration.Context) != 0 || declaration.SkipIfMissing || declaration.TTL != "" || declaration.NestedImports != "" { - return true, fmt.Errorf("%w: provider imports do not support context, skip_if_missing, TTL, or nested_imports", errUtils.ErrPluginUnsupportedExtension) - } - imports, ok := config.ComponentConfigurations.(schema.ComponentConfigurationImportRuntime) - if !ok { - return true, fmt.Errorf("%w: provider %s has no import runtime", errUtils.ErrPluginUnsupportedExtension, scheme) - } - return imports.RegisterImport(context.Background(), uri) -} diff --git a/internal/exec/configuration_imports_test.go b/internal/exec/configuration_imports_test.go index dbdc22d346d..40ab184f4a7 100644 --- a/internal/exec/configuration_imports_test.go +++ b/internal/exec/configuration_imports_test.go @@ -2,7 +2,6 @@ package exec import ( "context" - "errors" "os" "path/filepath" "testing" @@ -25,37 +24,6 @@ func (r *importRuntime) RegisterImport(_ context.Context, uri string) (bool, err return true, r.err } -func TestConfigurationImportRouting(t *testing.T) { - config := &schema.AtmosConfiguration{} - for _, uri := range []string{"ordinary", "https://example.com/config.yaml"} { - handled, err := registerConfigurationImport(config, uri, &schema.StackImport{}) - require.NoError(t, err) - require.False(t, handled) - } - handled, err := registerConfigurationImport(nil, "terragrunt://live", &schema.StackImport{}) - require.NoError(t, err) - require.False(t, handled) - config.Components.Terraform.ConfigurationProviders = map[string]schema.ComponentConfiguration{"terragrunt": {}} - _, err = registerConfigurationImport(config, "terragrunt://live", &schema.StackImport{}) - require.ErrorContains(t, err, "disabled") - config.Components.Terraform.ConfigurationProviders["terragrunt"] = schema.ComponentConfiguration{Enabled: true} - _, err = registerConfigurationImport(config, "terragrunt://live", &schema.StackImport{}) - require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) - runtime := &importRuntime{MockComponentConfigurationRuntime: NewMockComponentConfigurationRuntime(gomock.NewController(t))} - config.ComponentConfigurations = runtime - for _, declaration := range []schema.StackImport{{Context: map[string]any{"stage": "dev"}}, {SkipIfMissing: true}, {TTL: "1h"}, {NestedImports: "local"}} { - _, err := registerConfigurationImport(config, "terragrunt://live", &declaration) - require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) - } - handled, err = registerConfigurationImport(config, "terragrunt://live", &schema.StackImport{}) - require.NoError(t, err) - require.True(t, handled) - require.Equal(t, []string{"terragrunt://live"}, runtime.imports) - runtime.err = errors.New("registration failed") - _, err = registerConfigurationImport(config, "terragrunt://live", &schema.StackImport{}) - require.ErrorIs(t, err, runtime.err) -} - func TestYAMLImportsRegisterBeforeDiscovery(t *testing.T) { root := t.TempDir() path := filepath.Join(root, "adopt.yaml") diff --git a/internal/exec/describe_affected_configuration.go b/internal/exec/describe_affected_configuration.go index 39d6dc4e104..0d947c66086 100644 --- a/internal/exec/describe_affected_configuration.go +++ b/internal/exec/describe_affected_configuration.go @@ -7,9 +7,8 @@ import ( "slices" "sync/atomic" - "github.com/bmatcuk/doublestar/v4" - errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/component/configuration" "github.com/cloudposse/atmos/pkg/schema" ) @@ -34,7 +33,7 @@ func rebaseConfigurationDiscovery(config *schema.AtmosConfiguration) error { // configurationWatchIndex applies provider-declared cache exclusions only to its // file watches. It never changes YAML dependencies or the shared Git change set. func configurationWatchIndex(config *schema.AtmosConfiguration, settings map[string]any, original *changedFilesIndex) (*changedFilesIndex, error) { - patterns, err := configurationWatchExcludes(settings) + patterns, err := configuration.WatchExcludes(settings) if err != nil { return nil, err } @@ -51,7 +50,7 @@ func configurationWatchIndex(config *schema.AtmosConfiguration, settings map[str if filtered := original.watchIndexes[key]; filtered != nil { return filtered, nil } - files, err := unexcludedWatchFiles(config.BasePathAbsolute, original.getAllFiles(), patterns) + files, err := configuration.UnexcludedWatchFiles(config.BasePathAbsolute, original.getAllFiles(), patterns) if err != nil { return nil, err } @@ -63,49 +62,6 @@ func configurationWatchIndex(config *schema.AtmosConfiguration, settings map[str return filtered, nil } -// configurationWatchExcludes reads and validates the provider's watch_excludes. -// An absent or empty setting yields no patterns; any other shape is rejected. -func configurationWatchExcludes(settings map[string]any) ([]string, error) { - provider, _ := settings["configuration"].(map[string]any) - raw := provider["watch_excludes"] - if raw == nil { - return nil, nil - } - excludes, ok := raw.([]any) - if !ok { - return nil, fmt.Errorf("%w: watch_excludes must be a list of patterns", errUtils.ErrPluginInvalidConfig) - } - patterns := make([]string, 0, len(excludes)) - for _, value := range excludes { - pattern, ok := value.(string) - if !ok || !doublestar.ValidatePattern(pattern) { - return nil, errUtils.ErrPluginInvalidConfig - } - patterns = append(patterns, pattern) - } - return patterns, nil -} - -// unexcludedWatchFiles returns the files under root that match none of the patterns. -func unexcludedWatchFiles(root string, all []string, patterns []string) ([]string, error) { - files := make([]string, 0, len(all)) - for _, path := range all { - relative, err := filepath.Rel(root, path) - if err != nil { - return nil, fmt.Errorf("%w: watch path %q is not under %q: %w", errUtils.ErrPluginInvalidConfig, path, root, err) - } - excluded := false - for _, pattern := range patterns { - matched, _ := doublestar.Match(pattern, filepath.ToSlash(relative)) - excluded = excluded || matched - } - if !excluded { - files = append(files, path) - } - } - return files, nil -} - // configurationWatchPaths resolves provider-owned watches against the project // root without putting checkout-specific absolute paths into stack comparisons. // Existing YAML dependency path semantics remain unchanged. It reports whether diff --git a/internal/exec/stack_processor_utils.go b/internal/exec/stack_processor_utils.go index 5022336b16f..a980c69ba79 100644 --- a/internal/exec/stack_processor_utils.go +++ b/internal/exec/stack_processor_utils.go @@ -19,6 +19,7 @@ import ( "gopkg.in/yaml.v3" errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/component/configuration" "github.com/cloudposse/atmos/pkg/condition" cfg "github.com/cloudposse/atmos/pkg/config" "github.com/cloudposse/atmos/pkg/function/yamlsource" @@ -1634,7 +1635,7 @@ func processYAMLConfigFileWithContextInternal( return nil, nil, impErr } imp = renderedImp - if handled, importErr := registerConfigurationImport(atmosConfig, imp, &importStruct); handled || importErr != nil { + if handled, importErr := configuration.RegisterImport(atmosConfig, imp, &importStruct); handled || importErr != nil { if importErr != nil { return nil, nil, importErr } diff --git a/pkg/component/configuration/imports.go b/pkg/component/configuration/imports.go new file mode 100644 index 00000000000..51c5327dea9 --- /dev/null +++ b/pkg/component/configuration/imports.go @@ -0,0 +1,88 @@ +package configuration + +import ( + "context" + "fmt" + "path/filepath" + "strings" + + "github.com/bmatcuk/doublestar/v4" + + errUtils "github.com/cloudposse/atmos/errors" + "github.com/cloudposse/atmos/pkg/perf" + "github.com/cloudposse/atmos/pkg/schema" +) + +// RegisterImport routes configured provider schemes before YAML or +// remote-source resolution. Providers add instances to the shared inventory; +// importing a root does not rename its stacks or merge HCL into YAML variables. +func RegisterImport(config *schema.AtmosConfiguration, uri string, declaration *schema.StackImport) (bool, error) { + defer perf.Track(nil, "configuration.RegisterImport")() + + scheme, _, found := strings.Cut(uri, "://") + if !found || config == nil { + return false, nil + } + settings, configured := config.Components.Terraform.ConfigurationProviders[scheme] + if !configured { + return false, nil + } + if !settings.Enabled { + return true, fmt.Errorf("%w: configuration provider %s is disabled", errUtils.ErrPluginInvalidConfig, scheme) + } + if len(declaration.Context) != 0 || declaration.SkipIfMissing || declaration.TTL != "" || declaration.NestedImports != "" { + return true, fmt.Errorf("%w: provider imports do not support context, skip_if_missing, TTL, or nested_imports", errUtils.ErrPluginUnsupportedExtension) + } + imports, ok := config.ComponentConfigurations.(schema.ComponentConfigurationImportRuntime) + if !ok { + return true, fmt.Errorf("%w: provider %s has no import runtime", errUtils.ErrPluginUnsupportedExtension, scheme) + } + return imports.RegisterImport(context.Background(), uri) +} + +// WatchExcludes reads and validates the provider's watch_excludes. +// An absent or empty setting yields no patterns; any other shape is rejected. +func WatchExcludes(settings map[string]any) ([]string, error) { + defer perf.Track(nil, "configuration.WatchExcludes")() + + provider, _ := settings["configuration"].(map[string]any) + raw := provider["watch_excludes"] + if raw == nil { + return nil, nil + } + excludes, ok := raw.([]any) + if !ok { + return nil, fmt.Errorf("%w: watch_excludes must be a list of patterns", errUtils.ErrPluginInvalidConfig) + } + patterns := make([]string, 0, len(excludes)) + for _, value := range excludes { + pattern, ok := value.(string) + if !ok || !doublestar.ValidatePattern(pattern) { + return nil, errUtils.ErrPluginInvalidConfig + } + patterns = append(patterns, pattern) + } + return patterns, nil +} + +// UnexcludedWatchFiles returns the files under root that match none of the patterns. +func UnexcludedWatchFiles(root string, all []string, patterns []string) ([]string, error) { + defer perf.Track(nil, "configuration.UnexcludedWatchFiles")() + + files := make([]string, 0, len(all)) + for _, path := range all { + relative, err := filepath.Rel(root, path) + if err != nil { + return nil, fmt.Errorf("%w: watch path %q is not under %q: %w", errUtils.ErrPluginInvalidConfig, path, root, err) + } + excluded := false + for _, pattern := range patterns { + matched, _ := doublestar.Match(pattern, filepath.ToSlash(relative)) + excluded = excluded || matched + } + if !excluded { + files = append(files, path) + } + } + return files, nil +} diff --git a/pkg/component/configuration/registry_test.go b/pkg/component/configuration/registry_test.go index ba0a5fb8678..e9eac530671 100644 --- a/pkg/component/configuration/registry_test.go +++ b/pkg/component/configuration/registry_test.go @@ -2,6 +2,8 @@ package configuration import ( "context" + "errors" + "path/filepath" "testing" "github.com/stretchr/testify/require" @@ -48,3 +50,61 @@ func TestFactoryRegistrationAndOptIn(t *testing.T) { factories.Unlock() require.NoError(t, Attach(t.Context(), config, "list")) } + +type importRuntime struct { + schema.ComponentConfigurationRuntime + imports []string + err error +} + +func (r *importRuntime) RegisterImport(_ context.Context, uri string) (bool, error) { + r.imports = append(r.imports, uri) + return true, r.err +} + +func TestConfigurationImportRouting(t *testing.T) { + config := &schema.AtmosConfiguration{} + for _, uri := range []string{"ordinary", "https://example.com/config.yaml"} { + handled, err := RegisterImport(config, uri, &schema.StackImport{}) + require.NoError(t, err) + require.False(t, handled) + } + handled, err := RegisterImport(nil, "terragrunt://live", &schema.StackImport{}) + require.NoError(t, err) + require.False(t, handled) + config.Components.Terraform.ConfigurationProviders = map[string]schema.ComponentConfiguration{"terragrunt": {}} + _, err = RegisterImport(config, "terragrunt://live", &schema.StackImport{}) + require.ErrorContains(t, err, "disabled") + config.Components.Terraform.ConfigurationProviders["terragrunt"] = schema.ComponentConfiguration{Enabled: true} + _, err = RegisterImport(config, "terragrunt://live", &schema.StackImport{}) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + runtime := &importRuntime{} + config.ComponentConfigurations = runtime + for _, declaration := range []schema.StackImport{{Context: map[string]any{"stage": "dev"}}, {SkipIfMissing: true}, {TTL: "1h"}, {NestedImports: "local"}} { + _, err := RegisterImport(config, "terragrunt://live", &declaration) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + } + handled, err = RegisterImport(config, "terragrunt://live", &schema.StackImport{}) + require.NoError(t, err) + require.True(t, handled) + require.Equal(t, []string{"terragrunt://live"}, runtime.imports) + runtime.err = errors.New("registration failed") + _, err = RegisterImport(config, "terragrunt://live", &schema.StackImport{}) + require.ErrorIs(t, err, runtime.err) +} + +func TestWatchExcludes(t *testing.T) { + patterns, err := WatchExcludes(map[string]any{"configuration": map[string]any{"watch_excludes": []any{"**/.cache/**"}}}) + require.NoError(t, err) + root := t.TempDir() + files := []string{filepath.Join(root, "a", ".cache", "x"), filepath.Join(root, "a", "main.tf")} + filtered, err := UnexcludedWatchFiles(root, files, patterns) + require.NoError(t, err) + require.Equal(t, files[1:], filtered) + for _, value := range []any{"bad shape", []any{"["}, []any{42}} { + _, err := WatchExcludes(map[string]any{"configuration": map[string]any{"watch_excludes": value}}) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) + } + _, err = UnexcludedWatchFiles("relative", files, patterns) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) +} From 21b69912815c6e186f829c1cb701fa13d6a45d29 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Wed, 11 Nov 2026 17:32:12 -0600 Subject: [PATCH 23/52] fix(terraform): bind effective plan output arguments --- pkg/component/terraform/configuration_plan.go | 29 +++++- .../terraform/configuration_plan_test.go | 90 +++++++++++++++++++ pkg/component/terraform/execute_command.go | 4 + pkg/component/terraform/execute_hooks.go | 12 +++ pkg/component/terraform/execute_pipeline.go | 23 +++-- 5 files changed, 149 insertions(+), 9 deletions(-) diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go index d4e67bf98e5..4655a6d7558 100644 --- a/pkg/component/terraform/configuration_plan.go +++ b/pkg/component/terraform/configuration_plan.go @@ -197,14 +197,37 @@ func newConfigurationPlan(info *schema.ConfigAndStacksInfo, execCtx *componentEx return &configurationPlan{WorkingDigest: workingDigest, Version: 1, Stack: info.Stack, Component: info.ComponentFromArg, Identity: identity, EngineDigest: engineDigest, PlanDigest: planDigest, WorkingDirectory: filepath.Clean(execCtx.workingDir), Prepared: execCtx.preparedConfiguration}, nil } +// validatePlanOutputFlags rejects ambiguous plan destinations before hooks or Terraform run. +func validatePlanOutputFlags(command string, args []string) error { + if command != "plan" { + return nil + } + count := 0 + for i := 0; i < len(args); i++ { + arg := args[i] + if arg == "-out" || arg == "--out" { + count++ + i++ + } else if strings.HasPrefix(arg, "-out=") || strings.HasPrefix(arg, "--out=") { + count++ + } + if count > 1 { + return fmt.Errorf("%w: multiple plan output flags", errUtils.ErrPlanVerificationFailed) + } + } + return nil +} + func plannedOutputPath(directory string, args []string) string { for i, arg := range args { path := "" - if arg == "-out" && i+1 < len(args) { + switch { + case (arg == "-out" || arg == "--out") && i+1 < len(args): path = args[i+1] - } - if strings.HasPrefix(arg, "-out=") { + case strings.HasPrefix(arg, "-out="): path = strings.TrimPrefix(arg, "-out=") + case strings.HasPrefix(arg, "--out="): + path = strings.TrimPrefix(arg, "--out=") } if path != "" { if filepath.IsAbs(path) { diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index 69992502c2d..55efe1d988f 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -3,12 +3,16 @@ package terraform import ( "context" "errors" + "io" "os" "path/filepath" "runtime" "testing" 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" "github.com/stretchr/testify/require" ) @@ -192,3 +196,89 @@ func TestConfigurationWorkdirDigestIncludesDownloadedModules(t *testing.T) { require.NoError(t, err) require.NotEqual(t, before, after) } + +func TestPlanOutputFlagsUseEffectiveArguments(t *testing.T) { + for _, test := range []struct { + name string + provider []string + args []string + conflict bool + output string + }{ + {"double dash", nil, []string{"plan", "--out=new.plan"}, false, "new.plan"}, + {"provider output", []string{"--out=provider.plan"}, []string{"plan"}, false, "provider.plan"}, + {"provider and default conflict", []string{"--out=new.plan"}, []string{"plan", "-out=old.plan"}, true, ""}, + {"two separated flags conflict", nil, []string{"plan", "-out", "old.plan", "--out", "new.plan"}, true, ""}, + } { + t.Run(test.name, func(t *testing.T) { + config, info, execCtx, _, _ := planFixture(t) + // A permissive CI exit mapping must not turn a preflight rejection into success. + config.CI.Enabled = true + config.Components.Terraform.CI.ExitCodes = map[int]bool{0: true, 1: true, 2: true} + shell := &fakeShell{} + executor := testExecutor(t) + executor.services.ExecuteShellCommand = shell.execute + info.ComponentExecution = &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, Arguments: test.provider}}, BeforeHooks: []schema.ComponentHook{execHook("before", "plan")}} + if test.output != "" { + require.NoError(t, os.WriteFile(filepath.Join(execCtx.componentPath, test.output), []byte("plan"), 0o600)) + } + err := executor.runMainCommandPhase(config, info, execCtx, test.args, execCtx.componentPath, false) + if test.conflict { + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + require.Empty(t, shell.calls, "neither hook nor Terraform should run") + } else { + require.NoError(t, err) + require.FileExists(t, filepath.Join(execCtx.componentPath, test.output)+configurationPlanSuffix) + require.Len(t, shell.calls, 2) + } + }) + } +} + +func TestRecoveredPlanBindsEffectiveOutput(t *testing.T) { + config, info, execCtx, _, _ := planFixture(t) + config.Components.Terraform.Init.Reconfigure = schema.TerraformInitReconfigureAuto + plan := filepath.Join(execCtx.componentPath, "recovered.plan") + require.NoError(t, os.WriteFile(plan, []byte("reviewed"), 0o600)) + info.ComponentExecution = &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, Arguments: []string{"--out=" + plan}}}} + attempts := 0 + shell := &fakeShell{run: func(call *shellCall) error { + attempts++ + if attempts == 1 { + return errors.New("Backend configuration changed") + } + return nil + }} + executor := testExecutor(t) + executor.services.ExecuteShellCommand = shell.execute + executor.executeTerraformInitForcedFn = func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, string, string, bool, bool, ...ShellCommandOption) error { + return nil + } + require.NoError(t, executor.runMainCommandPhase(config, info, execCtx, []string{"plan"}, execCtx.componentPath, false)) + require.Equal(t, 2, attempts) + require.FileExists(t, plan+configurationPlanSuffix) +} + +type planShowRunner struct{} + +//nolint:gocritic // The process.Runner interface fixes the value parameter. +func (planShowRunner) Run(_ context.Context, spec process.TaskSpec) process.Result { + _, _ = io.WriteString(spec.Streams.Stdout, `{"format_version":"1.2","planned_values":{}}`) + return process.Result{Started: true} +} + +func TestPolicyPlanBindsPublishedOutput(t *testing.T) { + config, info, execCtx, _, _ := planFixture(t) + var engine policy.Engine + require.NoError(t, engine.Register(policy.CELEngine, &policy.CEL{})) + execCtx.planPolicy = &planfile.Guard{Engine: &engine, Show: planShowRunner{}, Rules: []policy.Rule{{ID: "allow", On: policy.TerraformPlan, Engine: policy.CELEngine, Expression: "false", Effects: policy.Effects{Deny: true}}}} + shell := &fakeShell{run: func(call *shellCall) error { + path := plannedOutputPath(execCtx.componentPath, call.args) + return os.WriteFile(path, []byte("reviewed"), 0o600) + }} + executor := testExecutor(t) + executor.services.ExecuteShellCommand = shell.execute + path := filepath.Join(execCtx.componentPath, "approved.plan") + require.NoError(t, executor.runMainCommandPhase(config, info, execCtx, []string{"plan", "-out=" + path}, execCtx.componentPath, false)) + require.FileExists(t, path+configurationPlanSuffix) +} diff --git a/pkg/component/terraform/execute_command.go b/pkg/component/terraform/execute_command.go index a0e8545624f..efeb3b8cebc 100644 --- a/pkg/component/terraform/execute_command.go +++ b/pkg/component/terraform/execute_command.go @@ -103,6 +103,10 @@ func (e *Executor) executeMainTerraformCommand( //nolint:revive // argument-limi } err := e.runMainTerraformCommand(atmosConfig, info, allArgsAndFlags, componentPath, opts...) + // Invalid output selection is a preflight failure, not a Terraform exit code. + if errors.Is(err, errUtils.ErrPlanVerificationFailed) { + return err + } return e.finishMainTerraformCommand(atmosConfig, info, err, uploadStatusFlag) } diff --git a/pkg/component/terraform/execute_hooks.go b/pkg/component/terraform/execute_hooks.go index 381ffa16820..22e2b211798 100644 --- a/pkg/component/terraform/execute_hooks.go +++ b/pkg/component/terraform/execute_hooks.go @@ -43,6 +43,9 @@ type executionPhase struct { componentPath string } +// mainCommandArgsKey carries the effective main command arguments to plan binding. +type mainCommandArgsKey struct{} + // runMainShellCommand runs the requested Terraform subprocess, wrapped by the component's // provider-supplied hooks and extra arguments. They match the requested command, which is // "destroy" for `apply -destroy`. @@ -56,6 +59,9 @@ func (e *Executor) runMainShellCommand(atmosConfig *schema.AtmosConfiguration, i componentPath: componentPath, } return e.runExecutionPhase(atmosConfig, info, phase, opts, func(phaseInfo *schema.ConfigAndStacksInfo, phaseArgs []string, phaseOpts []component.ExecutionOption) error { + if captured, ok := component.ContextFromOptions(phaseOpts...).Value(mainCommandArgsKey{}).(*[]string); ok { + *captured = slices.Clone(phaseArgs) + } return ExecuteShellCommandWithRetry( atmosConfig, phaseInfo, @@ -99,6 +105,9 @@ type phaseAction func(info *schema.ConfigAndStacksInfo, args []string, opts []co func (e *Executor) runExecutionPhase(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, phase *executionPhase, opts []component.ExecutionOption, action phaseAction) error { config := info.ComponentExecution if config.IsEmpty() { + if err := validatePlanOutputFlags(phase.command, phase.args); err != nil { + return err + } return action(info, phase.args, opts) } @@ -112,6 +121,9 @@ func (e *Executor) runExecutionPhase(atmosConfig *schema.AtmosConfiguration, inf if err != nil { return err } + if err := validatePlanOutputFlags(phase.command, applied.Args); err != nil { + return err + } // Extra environment applies to this phase and to its hooks, as it does for Terragrunt. phaseInfo := *info phaseInfo.ComponentEnvList = append(slices.Clone(info.ComponentEnvList), applied.Environment...) diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index d73f08b6e05..41572f949a4 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -263,7 +263,10 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every // 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)) + var effectiveArgs []string + captureCtx := context.WithValue(component.ContextFromOptions(opts...), mainCommandArgsKey{}, &effectiveArgs) + captureArgs := component.WithProcessContext(captureCtx) + mainOpts := append(slices.Clone(opts), component.WithMetadataOutputCapture(&execMetadataStdoutBuf, &execMetadataStderrBuf), captureArgs) // Phase-level CI log grouping (Dimension "phase"): fold the main subcommand // (plan/apply/destroy/…) into its own collapsible group, separate from init @@ -272,12 +275,20 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every return e.executeGovernedTerraformCommand(atmosConfig, info, allArgsAndFlags, componentPath, uploadStatusFlag, execCtx.planPolicy, mainOpts...) }) info.ExecMetadataRawOutput = combineExecMetadataOutput(&execMetadataStdoutBuf, &execMetadataStderrBuf) - if mainErr == nil { - return recordConfigurationPlan(component.ContextFromOptions(opts...), atmosConfig, info, execCtx, allArgsAndFlags) + if mainErr != nil { + mainErr = e.recoverFromInitRequired(atmosConfig, info, execCtx, allArgsAndFlags, componentPath, uploadStatusFlag, + mainErr, &execMetadataStdoutBuf, &execMetadataStderrBuf, append(slices.Clone(opts), captureArgs)...) } - - return e.recoverFromInitRequired(atmosConfig, info, execCtx, allArgsAndFlags, componentPath, uploadStatusFlag, - mainErr, &execMetadataStdoutBuf, &execMetadataStderrBuf, opts...) + if mainErr != nil { + return mainErr + } + // Policy plans run against a private artifact that is removed after the guard + // publishes it to the original output path. The effective arguments were + // validated before execution; bind the published destination instead. + if execCtx.planPolicy != nil && info.SubCommand == "plan" && !info.DryRun { + effectiveArgs = allArgsAndFlags + } + return recordConfigurationPlan(component.ContextFromOptions(opts...), atmosConfig, info, execCtx, effectiveArgs) } // recoverFromInitRequired is smart init's plan/apply-time safety net: when the main command From 2f81e290001df0697cb6ab8fbcf383f619aa6ddc Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Thu, 12 Nov 2026 18:19:06 -0600 Subject: [PATCH 24/52] fix(terraform): reject plan output flags from TF_CLI_ARGS Terraform reads TF_CLI_ARGS and TF_CLI_ARGS_plan itself, so an output flag in either variable could select a plan file that Atmos never sees and therefore never binds. For configuration-owned plans, check the inherited, component, and provider extra-argument environment before hooks or Terraform run, and require -out to be passed as a command argument. Co-Authored-By: Claude Opus 5.5 --- pkg/component/terraform/configuration_plan.go | 52 ++++++++++++++++ .../terraform/configuration_plan_test.go | 62 +++++++++++++++++++ pkg/component/terraform/execute_pipeline.go | 3 + 3 files changed, 117 insertions(+) diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go index 4655a6d7558..a6f437755a0 100644 --- a/pkg/component/terraform/configuration_plan.go +++ b/pkg/component/terraform/configuration_plan.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "reflect" + "slices" "strings" errUtils "github.com/cloudposse/atmos/errors" @@ -19,6 +20,9 @@ import ( const configurationPlanSuffix = ".atmos.json" +// subcommandPlan names the Terraform plan command. +const subcommandPlan = "plan" + // planBindingSuffix names the binding of a default saved plan, which Atmos writes as "*.planfile". const planBindingSuffix = ".planfile" + configurationPlanSuffix @@ -218,6 +222,54 @@ func validatePlanOutputFlags(command string, args []string) error { return nil } +// validateEnvironmentPlanOutput rejects plan output flags in TF_CLI_ARGS or +// TF_CLI_ARGS_plan for a configuration-owned plan. Terraform reads these +// variables itself, so a flag there could choose a plan file that Atmos does not +// see and therefore does not bind. The check covers the inherited process +// environment, the component environment, and provider extra-argument +// environment, which together form the subprocess environment. Hooks run as +// child processes and cannot change it. +func validateEnvironmentPlanOutput(info *schema.ConfigAndStacksInfo, execCtx *componentExecContext) error { + if execCtx.preparedConfiguration == nil || info.DryRun || info.SubCommand != subcommandPlan { + return nil + } + for _, entry := range planEnvironment(info) { + name, value, _ := strings.Cut(entry, "=") + if name != tfCliArgsEnvVar && name != tfCliArgsEnvVar+"_"+subcommandPlan { + continue + } + if containsOutputFlag(parseArgs(value)) { + return fmt.Errorf("%w: %s sets a plan output flag; pass -out as a command argument instead", errUtils.ErrPlanVerificationFailed, name) + } + } + return nil +} + +// planEnvironment lists every environment entry a plan subprocess may receive. +func planEnvironment(info *schema.ConfigAndStacksInfo) []string { + entries := append(os.Environ(), info.ComponentEnvList...) + if info.ComponentExecution == nil { + return entries + } + for i := range info.ComponentExecution.ExtraArguments { + item := &info.ComponentExecution.ExtraArguments[i] + if !slices.Contains(item.Commands, subcommandPlan) { + continue + } + for name, value := range item.Environment { + entries = append(entries, name+"="+value) + } + } + return entries +} + +// containsOutputFlag reports any plan output flag, with or without a value. +func containsOutputFlag(args []string) bool { + return slices.ContainsFunc(args, func(arg string) bool { + return arg == "-out" || arg == "--out" || strings.HasPrefix(arg, "-out=") || strings.HasPrefix(arg, "--out=") + }) +} + func plannedOutputPath(directory string, args []string) string { for i, arg := range args { path := "" diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index 55efe1d988f..fc47cfec7ee 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -282,3 +282,65 @@ func TestPolicyPlanBindsPublishedOutput(t *testing.T) { require.NoError(t, executor.runMainCommandPhase(config, info, execCtx, []string{"plan", "-out=" + path}, execCtx.componentPath, false)) require.FileExists(t, path+configurationPlanSuffix) } + +// TestPlanOutputFlagsInEnvironmentAreRejected covers output flags that Terraform +// reads from TF_CLI_ARGS or TF_CLI_ARGS_plan, which Atmos cannot bind. +func TestPlanOutputFlagsInEnvironmentAreRejected(t *testing.T) { + for _, test := range []struct { + name string + inherited map[string]string + component []string + provider map[string]string + commands []string + reject bool + }{ + {name: "inherited TF_CLI_ARGS_plan", inherited: map[string]string{"TF_CLI_ARGS_plan": "-out=env.plan"}, reject: true}, + {name: "inherited TF_CLI_ARGS", inherited: map[string]string{"TF_CLI_ARGS": "-lock=false --out env.plan"}, reject: true}, + {name: "component environment", component: []string{"TF_CLI_ARGS_plan=-out env.plan"}, reject: true}, + {name: "provider environment", provider: map[string]string{"TF_CLI_ARGS_plan": "--out=env.plan"}, commands: []string{"plan"}, reject: true}, + {name: "bare flag", component: []string{"TF_CLI_ARGS=-out"}, reject: true}, + {name: "other flags", inherited: map[string]string{"TF_CLI_ARGS_plan": "-lock=false -outfile-like=x"}, component: []string{"TF_CLI_ARGS=-no-color"}}, + {name: "apply only variable", component: []string{"TF_CLI_ARGS_apply=-out=x"}}, + {name: "provider environment for apply", provider: map[string]string{"TF_CLI_ARGS_plan": "-out=env.plan"}, commands: []string{"apply"}}, + } { + t.Run(test.name, func(t *testing.T) { + t.Setenv("TF_CLI_ARGS", "") + t.Setenv("TF_CLI_ARGS_plan", "") + for name, value := range test.inherited { + t.Setenv(name, value) + } + config, info, execCtx, _, path := planFixture(t) + info.ComponentEnvList = test.component + if test.provider != nil { + info.ComponentExecution = &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: test.commands, Environment: test.provider}}, BeforeHooks: []schema.ComponentHook{execHook("before", "plan")}} + } + shell := &fakeShell{} + executor := testExecutor(t) + executor.services.ExecuteShellCommand = shell.execute + err := executor.runMainCommandPhase(config, info, execCtx, []string{"plan", "-out", filepath.Base(path)}, execCtx.componentPath, false) + if test.reject { + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + require.Empty(t, shell.calls, "neither hook nor Terraform should run") + return + } + require.NoError(t, err) + require.NotEmpty(t, shell.calls) + require.FileExists(t, path+configurationPlanSuffix) + }) + } +} + +// TestEnvironmentPlanOutputIgnoresUnownedPlans keeps ordinary components unaffected. +func TestEnvironmentPlanOutputIgnoresUnownedPlans(t *testing.T) { + t.Setenv("TF_CLI_ARGS_plan", "-out=env.plan") + _, info, execCtx, _, _ := planFixture(t) + require.ErrorIs(t, validateEnvironmentPlanOutput(info, execCtx), errUtils.ErrPlanVerificationFailed) + info.DryRun = true + require.NoError(t, validateEnvironmentPlanOutput(info, execCtx)) + info.DryRun = false + info.SubCommand = "apply" + require.NoError(t, validateEnvironmentPlanOutput(info, execCtx)) + info.SubCommand = "plan" + execCtx.preparedConfiguration = nil + require.NoError(t, validateEnvironmentPlanOutput(info, execCtx)) +} diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index 41572f949a4..aa2be2ff04f 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -262,6 +262,9 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every // 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). + if err := validateEnvironmentPlanOutput(info, execCtx); err != nil { + return err + } var execMetadataStdoutBuf, execMetadataStderrBuf bytes.Buffer var effectiveArgs []string captureCtx := context.WithValue(component.ContextFromOptions(opts...), mainCommandArgsKey{}, &effectiveArgs) From ff18c58a80066974ed18558169f950bf9a857760 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Thu, 12 Nov 2026 18:23:36 -0600 Subject: [PATCH 25/52] fix(terraform): select policy plan artifacts from effective arguments A plan policy selected its artifact before provider extra arguments were added, so a plan whose only -out came from extra_arguments failed selection even though Terraform would write a plan. A double-dash --out also failed. Add provider extra arguments before the policy guard runs. Reject conflicting output flags, and normalize --out to -out. While the guard runs, remove the added arguments from the component execution so the execution phase adds only environment and hooks. Plan binding records the published destination from the same vector. Co-Authored-By: Claude Opus 5.5 --- pkg/component/terraform/execute_hooks.go | 52 +++++++++++++++++ pkg/component/terraform/execute_hooks_test.go | 58 +++++++++++++++++++ pkg/component/terraform/execute_pipeline.go | 5 ++ 3 files changed, 115 insertions(+) diff --git a/pkg/component/terraform/execute_hooks.go b/pkg/component/terraform/execute_hooks.go index 22e2b211798..b03170af3b3 100644 --- a/pkg/component/terraform/execute_hooks.go +++ b/pkg/component/terraform/execute_hooks.go @@ -249,6 +249,58 @@ func (r *hookRunner) workingDirectory(hook *schema.ComponentHook) string { } } +// expandPolicyPlanArguments gives a plan policy the effective plan invocation. The policy +// selects its artifact from -out before Terraform runs, but provider extra arguments are +// otherwise added later, in runExecutionPhase. This adds them now and rejects conflicting +// output flags. It then removes the added arguments from the component execution until +// restore, so runExecutionPhase applies only the environment and hooks, and the arguments +// are not added twice. Double-dash output flags are written as -out, which the policy +// guard recognizes. Terraform accepts both forms. The returned arguments are also the +// published plan path that plan binding records. +func expandPolicyPlanArguments(info *schema.ConfigAndStacksInfo, execCtx *componentExecContext, args []string, componentPath string) ([]string, func(), error) { + noop := func() {} + if execCtx.planPolicy == nil || info.DryRun || info.SubCommand != subcommandPlan || len(args) == 0 { + return args, noop, nil + } + config := info.ComponentExecution + if config.IsEmpty() { + return singleDashOutputFlags(args), noop, nil + } + position := extraArgumentsPosition(info, args) + command := execution.CommandName(info.SubCommand, args[position:]) + applied, err := execution.ApplyExtraArguments(config.ExtraArguments, &execution.ArgumentsRequest{ + Command: command, Args: args, InsertAt: position, WorkingDirectory: componentPath, + }) + if err != nil { + return nil, noop, err + } + if err := validatePlanOutputFlags(command, applied.Args); err != nil { + return nil, noop, err + } + stripped := *config + stripped.ExtraArguments = slices.Clone(config.ExtraArguments) + for i := range stripped.ExtraArguments { + if slices.Contains(stripped.ExtraArguments[i].Commands, command) { + stripped.ExtraArguments[i].Arguments = nil + stripped.ExtraArguments[i].RequiredVarFiles = nil + stripped.ExtraArguments[i].OptionalVarFiles = nil + } + } + info.ComponentExecution = &stripped + return singleDashOutputFlags(applied.Args), func() { info.ComponentExecution = config }, nil +} + +// singleDashOutputFlags rewrites double-dash output flags in single-dash form, keeping their values. +func singleDashOutputFlags(args []string) []string { + result := slices.Clone(args) + for i, arg := range result { + if arg == "--out" || strings.HasPrefix(arg, "--out=") { + result[i] = arg[1:] + } + } + return result +} + // extraArgumentsPosition is the index after the Terraform command and its subcommands, so extra // arguments precede the caller's flags exactly as Terragrunt inserts them. func extraArgumentsPosition(info *schema.ConfigAndStacksInfo, args []string) int { diff --git a/pkg/component/terraform/execute_hooks_test.go b/pkg/component/terraform/execute_hooks_test.go index 182da57cca1..eb56f0a9654 100644 --- a/pkg/component/terraform/execute_hooks_test.go +++ b/pkg/component/terraform/execute_hooks_test.go @@ -15,6 +15,8 @@ import ( errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/component" + "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" ) @@ -514,3 +516,59 @@ func TestPreflightRequiredVarFiles(t *testing.T) { workspace := hookInfo("workspace", missing) require.NoError(t, preflightRequiredVarFiles(workspace, dir), "workspace management runs no main command") } + +// TestPolicyPlanUsesProviderOutputArguments verifies that a plan policy selects and binds +// the output path Terraform receives, including one supplied only by provider extra arguments. +func TestPolicyPlanUsesProviderOutputArguments(t *testing.T) { + for _, test := range []struct { + name string + provider []string + args []string + output string + conflict bool + }{ + {name: "provider only", provider: []string{"--out=provider.plan", "-lock=false"}, args: []string{"plan"}, output: "provider.plan"}, + {name: "double dash argument", args: []string{"plan", "--out", "argument.plan"}, output: "argument.plan"}, + {name: "provider and argument conflict", provider: []string{"-out=provider.plan"}, args: []string{"plan", "-out=argument.plan"}, conflict: true}, + } { + t.Run(test.name, func(t *testing.T) { + config, info, execCtx, _, _ := planFixture(t) + var engine policy.Engine + require.NoError(t, engine.Register(policy.CELEngine, &policy.CEL{})) + execCtx.planPolicy = &planfile.Guard{Engine: &engine, Show: planShowRunner{}, Rules: []policy.Rule{{ID: "allow", On: policy.TerraformPlan, Engine: policy.CELEngine, Expression: "false", Effects: policy.Effects{Deny: true}}}} + var original *schema.ComponentExecution + if test.provider != nil { + original = &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, Arguments: test.provider, Environment: map[string]string{"PROVIDER_ENV": "set"}}}} + info.ComponentExecution = original + } + shell := &fakeShell{run: func(call *shellCall) error { + return os.WriteFile(plannedOutputPath(execCtx.componentPath, call.args), []byte("reviewed"), 0o600) + }} + executor := testExecutor(t) + executor.services.ExecuteShellCommand = shell.execute + err := executor.runMainCommandPhase(config, info, execCtx, test.args, execCtx.componentPath, false) + require.Same(t, original, info.ComponentExecution, "the component execution must be restored") + if test.conflict { + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + require.Empty(t, shell.calls, "Terraform must not run") + return + } + require.NoError(t, err) + require.Len(t, shell.calls, 1) + outputs := 0 + for _, arg := range shell.calls[0].args { + if arg == "-out" || strings.HasPrefix(arg, "-out=") || strings.HasPrefix(arg, "--out") { + outputs++ + } + } + require.Equal(t, 1, outputs, "provider arguments must be added once: %v", shell.calls[0].args) + if test.provider != nil { + require.Contains(t, shell.calls[0].args, "-lock=false") + require.Contains(t, shell.calls[0].env, "PROVIDER_ENV=set") + } + published := filepath.Join(execCtx.componentPath, test.output) + require.FileExists(t, published) + require.FileExists(t, published+configurationPlanSuffix) + }) + } +} diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index aa2be2ff04f..7016f097fe5 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -265,6 +265,11 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every if err := validateEnvironmentPlanOutput(info, execCtx); err != nil { return err } + allArgsAndFlags, restore, err := expandPolicyPlanArguments(info, execCtx, allArgsAndFlags, componentPath) + if err != nil { + return err + } + defer restore() var execMetadataStdoutBuf, execMetadataStderrBuf bytes.Buffer var effectiveArgs []string captureCtx := context.WithValue(component.ContextFromOptions(opts...), mainCommandArgsKey{}, &effectiveArgs) From 427a071f76743f1db68978e7c0a6654784b07e5b Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Thu, 12 Nov 2026 18:47:17 -0600 Subject: [PATCH 26/52] fix(terraform): stream error hook output from temporary files Error hooks matched against an in-memory copy of the failed command's output and every hook's output, so a large Terraform run could hold all of its output in memory twice. Record phase and hook output in temporary files when error hooks exist, tee it to callers' writers as before, and keep every retry attempt's output. Match on_errors patterns against a streamed reader over the errors and captured output, so memory no longer grows with output size. Capture failures do not fail the command. They surface when the output is read. The temporary files are closed and removed once the lifecycle finishes. Co-Authored-By: Claude Opus 5.5 --- errors/plugin.go | 2 + pkg/component/execution/lifecycle.go | 91 +++++++++-- .../execution/lifecycle_cancellation_test.go | 4 +- pkg/component/execution/lifecycle_test.go | 83 +++++++++- pkg/component/terraform/execute_command.go | 2 +- pkg/component/terraform/execute_hooks.go | 147 ++++++++++++++++-- .../terraform/execute_hooks_process_test.go | 68 ++++++++ 7 files changed, 362 insertions(+), 35 deletions(-) diff --git a/errors/plugin.go b/errors/plugin.go index 5d0ed377307..425a8af2220 100644 --- a/errors/plugin.go +++ b/errors/plugin.go @@ -76,6 +76,8 @@ var ( ErrComponentHookFailed = errors.New("component hook failed") // ErrComponentVarFileMissing identifies a required variable file that does not exist. ErrComponentVarFileMissing = errors.New("required variable file does not exist") + // ErrComponentOutputCapture identifies command output that could not be recorded for error hook matching. + ErrComponentOutputCapture = errors.New("failed to capture command output for error hooks") ErrInvalidCredentialCodec = errors.New("invalid credential codec") ErrDuplicateCredentialCodec = errors.New("duplicate credential codec") diff --git a/pkg/component/execution/lifecycle.go b/pkg/component/execution/lifecycle.go index 64e7ca02a05..9ee7cc81a89 100644 --- a/pkg/component/execution/lifecycle.go +++ b/pkg/component/execution/lifecycle.go @@ -1,9 +1,11 @@ package execution import ( + "bufio" "context" "errors" "fmt" + "io" "regexp" "strings" @@ -13,15 +15,32 @@ import ( ) // Runner executes one hook and reports its failure and captured output. -// The output feeds error hook matching and is never printed again. +// The output feeds error hook matching and is never printed again. A nil output means +// nothing was captured. type Runner interface { - RunHook(ctx context.Context, kind HookKind, hook *schema.ComponentHook) (output string, err error) + RunHook(ctx context.Context, kind HookKind, hook *schema.ComponentHook) (output Output, err error) } +// Output is captured phase output. Error hook matching reads it as a stream, so output of +// any size is never loaded into memory as a whole. +type Output interface { + // Open returns a new reader positioned at the start of the output. A reader that + // implements io.Closer is closed after use. An error reports a failed capture. + Open() (io.Reader, error) +} + +// TextOutput is output that is already in memory. +type TextOutput string + +// Open returns a reader over the text. +// +//nolint:lintroller // Trivial accessor; no I/O. +func (t TextOutput) Open() (io.Reader, error) { return strings.NewReader(string(t)), nil } + // failure is a failed phase with the output it produced. type failure struct { err error - output string + output Output } // Run executes the command lifecycle: before hooks, the action, after hooks, then error hooks. @@ -30,7 +49,7 @@ type failure struct { // run_on_error. Error hooks run when any earlier phase failed and an on_errors expression matches // that phase's error or output. Cancellation stops every remaining hook. The action's error is // returned unchanged when nothing else failed. -func Run(ctx context.Context, config *schema.ComponentExecution, command string, runner Runner, action func() (string, error)) error { +func Run(ctx context.Context, config *schema.ComponentExecution, command string, runner Runner, action func() (Output, error)) error { defer perf.Track(nil, "execution.Run")() if config == nil || (len(config.BeforeHooks)+len(config.AfterHooks)+len(config.ErrorHooks) == 0) { @@ -83,16 +102,12 @@ func runErrorHooks(ctx context.Context, runner Runner, hooks []*schema.Component if len(hooks) == 0 || len(prior) == 0 { return nil } - var text strings.Builder - for _, item := range prior { - text.WriteString(item.err.Error() + "\n" + item.output + "\n") - } var failures []failure for _, hook := range hooks { if ctx.Err() != nil { break } - matched, err := matchesAny(hook.OnErrors, text.String()) + matched, err := matchesAny(hook.OnErrors, prior) if err != nil { failures = append(failures, failure{err: fmt.Errorf("matching on_errors of error hook %q: %w", hook.Name, err)}) continue @@ -107,19 +122,69 @@ func runErrorHooks(ctx context.Context, runner Runner, hooks []*schema.Component return failures } -func matchesAny(patterns []string, text string) (bool, error) { +// matchesAny matches each pattern against every prior failure's error and output, in order, +// as one text. The text is streamed, so memory use does not grow with the output size. +func matchesAny(patterns []string, prior []failure) (bool, error) { for _, pattern := range patterns { - matched, err := regexp.MatchString(pattern, text) + expression, err := regexp.Compile(pattern) if err != nil { return false, err } - if matched { - return true, nil + matched, err := matchFailures(expression, prior) + if err != nil || matched { + return matched, err } } return false, nil } +func matchFailures(expression *regexp.Regexp, prior []failure) (bool, error) { + readers := make([]io.Reader, 0, 3*len(prior)) + var closers []io.Closer + defer func() { + for _, closer := range closers { + _ = closer.Close() + } + }() + for _, item := range prior { + readers = append(readers, strings.NewReader(item.err.Error()+"\n")) + if item.output != nil { + reader, err := item.output.Open() + if err != nil { + return false, err + } + if closer, ok := reader.(io.Closer); ok { + closers = append(closers, closer) + } + readers = append(readers, reader) + } + readers = append(readers, strings.NewReader("\n")) + } + stream := &errorReader{reader: bufio.NewReader(io.MultiReader(readers...))} + matched := expression.MatchReader(stream) + if stream.err != nil { + return false, stream.err + } + return matched, nil +} + +// errorReader keeps the first read error, which regexp.MatchReader treats as end of input. +type errorReader struct { + reader *bufio.Reader + err error +} + +// ReadRune reads the next rune and remembers the first error other than end of input. +// +//nolint:lintroller // Called for every rune matched; tracking would dominate matching. +func (r *errorReader) ReadRune() (rune, int, error) { + value, size, err := r.reader.ReadRune() + if err != nil && !errors.Is(err, io.EOF) && r.err == nil { + r.err = err + } + return value, size, err +} + func hookError(kind HookKind, hook *schema.ComponentHook, err error) error { return fmt.Errorf("%w: %s %q: %w", errUtils.ErrComponentHookFailed, kind, hook.Name, err) } diff --git a/pkg/component/execution/lifecycle_cancellation_test.go b/pkg/component/execution/lifecycle_cancellation_test.go index 1fc72c2e98d..b974f9d2947 100644 --- a/pkg/component/execution/lifecycle_cancellation_test.go +++ b/pkg/component/execution/lifecycle_cancellation_test.go @@ -32,10 +32,10 @@ func TestRunCancellationDuringActionIsReported(t *testing.T) { AfterHooks: []schema.ComponentHook{{Name: "a", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, ErrorHooks: []schema.ComponentHook{errorHook("e")}, } - err := Run(ctx, config, "plan", r, func() (string, error) { + err := Run(ctx, config, "plan", r, func() (Output, error) { r.calls = append(r.calls, "action") cancel() - return "", test.actionErr + return nil, test.actionErr }) require.ErrorIs(t, err, context.Canceled) require.Equal(t, test.wantFailure, errors.Is(err, failure), "the action's own failure must stay visible") diff --git a/pkg/component/execution/lifecycle_test.go b/pkg/component/execution/lifecycle_test.go index e6e10660041..1ba53313a52 100644 --- a/pkg/component/execution/lifecycle_test.go +++ b/pkg/component/execution/lifecycle_test.go @@ -3,6 +3,8 @@ package execution import ( "context" "errors" + "io" + "slices" "strings" "testing" @@ -23,21 +25,21 @@ type recorder struct { onCall func(name string) } -func (r *recorder) RunHook(_ context.Context, kind HookKind, hook *schema.ComponentHook) (string, error) { +func (r *recorder) RunHook(_ context.Context, kind HookKind, hook *schema.ComponentHook) (Output, error) { name := string(kind) + ":" + hook.Name r.calls = append(r.calls, name) if r.onCall != nil { r.onCall(name) } - return r.output[hook.Name], r.failures[hook.Name] + return TextOutput(r.output[hook.Name]), r.failures[hook.Name] } -func (r *recorder) action() (string, error) { +func (r *recorder) action() (Output, error) { r.calls = append(r.calls, "action") if r.onCall != nil { r.onCall("action") } - return r.output["action"], r.failures["action"] + return TextOutput(r.output["action"]), r.failures["action"] } func hook(name string, commands ...string) schema.ComponentHook { @@ -260,3 +262,76 @@ func TestValidate(t *testing.T) { }) } } + +// streamOutput is captured output that records how it was read. +type streamOutput struct { + text string + openErr error + readErr error + closed int +} + +func (o *streamOutput) Open() (io.Reader, error) { + if o.openErr != nil { + return nil, o.openErr + } + var reader io.Reader = strings.NewReader(o.text) + if o.readErr != nil { + reader = io.MultiReader(reader, &failingReader{err: o.readErr}) + } + return &closingReader{Reader: reader, output: o}, nil +} + +type closingReader struct { + io.Reader + output *streamOutput +} + +func (r *closingReader) Close() error { + r.output.closed++ + return nil +} + +type failingReader struct{ err error } + +func (r *failingReader) Read([]byte) (int, error) { return 0, r.err } + +// TestErrorHooksStreamCapturedOutput verifies error hook matching reads captured output as a +// stream, closes every reader, keeps matching across failure boundaries, and reports capture +// failures instead of silently skipping the hook. +func TestErrorHooksStreamCapturedOutput(t *testing.T) { + failed := errors.New("exit status 1") + readFailure := errors.New("capture read failed") + large := strings.Repeat("x", 4<<20) + "\nError: AccessDenied\n" + for _, test := range []struct { + name string + output *streamOutput + pattern string + hookRan bool + wantErr error + }{ + {name: "match at end of large output", output: &streamOutput{text: large}, pattern: "AccessDenied", hookRan: true}, + {name: "match spans error and output", output: &streamOutput{text: "Error: Throttled"}, pattern: "(?s)exit status 1.*Throttled", hookRan: true}, + {name: "anchored pattern on the whole text", output: &streamOutput{text: "tail"}, pattern: "^exit status 1\ntail\n$", hookRan: true}, + {name: "no match", output: &streamOutput{text: large}, pattern: "Throttled"}, + {name: "capture cannot be opened", output: &streamOutput{openErr: readFailure}, pattern: ".*", wantErr: readFailure}, + {name: "capture read fails", output: &streamOutput{text: "partial", readErr: readFailure}, pattern: "never", wantErr: readFailure}, + } { + t.Run(test.name, func(t *testing.T) { + r := &recorder{} + config := &schema.ComponentExecution{ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"plan"}, OnErrors: []string{test.pattern}, Execute: []string{"tool"}}}} + err := Run(t.Context(), config, "plan", r, func() (Output, error) { + r.calls = append(r.calls, "action") + return test.output, failed + }) + require.ErrorIs(t, err, failed, "the action failure stays visible") + if test.wantErr != nil { + require.ErrorIs(t, err, test.wantErr) + } + require.Equal(t, test.hookRan, slices.Contains(r.calls, "error_hook:e"), r.calls) + if test.output.openErr == nil { + require.Equal(t, 1, test.output.closed, "every opened reader is closed") + } + }) + } +} diff --git a/pkg/component/terraform/execute_command.go b/pkg/component/terraform/execute_command.go index efeb3b8cebc..c580c6df4e7 100644 --- a/pkg/component/terraform/execute_command.go +++ b/pkg/component/terraform/execute_command.go @@ -388,7 +388,7 @@ func ExecuteShellCommandWithRetry( // 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) { +func composeRetryCaptureWriters(baseOpts []component.ExecutionOption, buf io.Writer) (stdout, stderr io.Writer) { var probe component.ExecutionOptions for _, opt := range baseOpts { opt(&probe) diff --git a/pkg/component/terraform/execute_hooks.go b/pkg/component/terraform/execute_hooks.go index b03170af3b3..1cde7cb0681 100644 --- a/pkg/component/terraform/execute_hooks.go +++ b/pkg/component/terraform/execute_hooks.go @@ -1,14 +1,18 @@ package terraform import ( - "bytes" "context" + "errors" "fmt" "io" + "io/fs" + "os" "path/filepath" "slices" "strings" + "sync" + errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/component" "github.com/cloudposse/atmos/pkg/component/execution" log "github.com/cloudposse/atmos/pkg/logger" @@ -133,12 +137,17 @@ func (e *Executor) runExecutionPhase(atmosConfig *schema.AtmosConfiguration, inf return action(&phaseInfo, applied.Args, opts) } - runner := &hookRunner{executor: e, atmosConfig: atmosConfig, info: &phaseInfo, phase: phase, opts: hookOptions(opts)} + var capture *phaseCapture + if len(config.ErrorHooks) > 0 { + capture = &phaseCapture{} + defer capture.cleanup() + } + runner := &hookRunner{executor: e, atmosConfig: atmosConfig, info: &phaseInfo, phase: phase, opts: hookOptions(opts), capture: capture} // A detailed plan exit code of 2 reports changes, not a failure: ordinary after hooks run and // error hooks do not. The status itself is returned unchanged once the hooks have run. var detailedResult error - err = execution.Run(component.ContextFromOptions(opts...), config, phase.command, runner, func() (string, error) { - output, err := runPhaseAction(&phaseInfo, applied.Args, opts, len(config.ErrorHooks) > 0, action) + err = execution.Run(component.ContextFromOptions(opts...), config, phase.command, runner, func() (execution.Output, error) { + output, err := runPhaseAction(&phaseInfo, applied.Args, opts, capture, action) if isDetailedPlanChanges(phase, applied.Args, err) { detailedResult = err return output, nil @@ -152,15 +161,16 @@ func (e *Executor) runExecutionPhase(atmosConfig *schema.AtmosConfiguration, inf } // runPhaseAction runs the wrapped subprocess. Error hooks match against the failed command's -// output, which the caller may also be capturing, so it is captured only when they exist. -func runPhaseAction(info *schema.ConfigAndStacksInfo, args []string, opts []component.ExecutionOption, capture bool, action phaseAction) (string, error) { - if !capture { - return "", action(info, args, opts) +// output, which the caller may also be capturing, so it is captured only when they exist. The +// capture goes to a temporary file, never to memory, and keeps every retry attempt's output. +func runPhaseAction(info *schema.ConfigAndStacksInfo, args []string, opts []component.ExecutionOption, capture *phaseCapture, action phaseAction) (execution.Output, error) { + if capture == nil { + return nil, action(info, args, opts) } - var output bytes.Buffer - stdout, stderr := composeRetryCaptureWriters(opts, &output) + file := capture.create() + stdout, stderr := composeRetryCaptureWriters(opts, file) err := action(info, args, append(slices.Clone(opts), component.WithStdoutCapture(stdout), component.WithStderrCapture(stderr))) - return output.String(), err + return file.finish(), err } // isDetailedPlanChanges reports whether err is plan's "-detailed-exitcode" status for changes present. @@ -198,11 +208,13 @@ type hookRunner struct { info *schema.ConfigAndStacksInfo phase *executionPhase opts []component.ExecutionOption + // capture records hook output for error hook matching; nil means no error hooks exist. + capture *phaseCapture } // RunHook runs one hook and returns its captured output for error hook matching. // Hook stdout goes to the UI channel (stderr), never to the data channel, unless suppressed. -func (r *hookRunner) RunHook(_ context.Context, kind execution.HookKind, hook *schema.ComponentHook) (string, error) { +func (r *hookRunner) RunHook(_ context.Context, kind execution.HookKind, hook *schema.ComponentHook) (execution.Output, error) { defer perf.Track(r.atmosConfig, "terraform.hookRunner.RunHook")() ui.Info(fmt.Sprintf("Executing %s: %s", strings.ReplaceAll(string(kind), "_", " "), hook.Name)) @@ -219,8 +231,12 @@ func (r *hookRunner) RunHook(_ context.Context, kind execution.HookKind, hook *s stdout = io.Discard } - var output bytes.Buffer - captured := &syncWriter{w: &output} + captured := io.Discard + var file *captureFile + if r.capture != nil { + file = r.capture.create() + captured = file + } opts := append( slices.Clone(r.opts), component.WithStdoutOverride(stdout), @@ -234,7 +250,10 @@ func (r *hookRunner) RunHook(_ context.Context, kind execution.HookKind, hook *s hookEnvHookName+"="+hook.Name, ) err := r.executor.services.ExecuteShellCommand(*r.atmosConfig, hook.Execute[0], hook.Execute[1:], r.workingDirectory(hook), env, false, "", opts...) - return output.String(), err + if file == nil { + return nil, err + } + return file.finish(), err } // workingDirectory defaults to the provisioned module directory; relative paths resolve there. @@ -310,3 +329,101 @@ func extraArgumentsPosition(info *schema.ConfigAndStacksInfo, args []string) int } return position } + +// phaseCapture owns the temporary files that record phase and hook output for error hook +// matching. Cleanup removes every file once the lifecycle, including matching, has finished. +type phaseCapture struct { + mu sync.Mutex + files []*captureFile +} + +// create starts a new capture file. If the file cannot be created, the command still runs and +// the capture reports the failure when error hooks read it. +func (c *phaseCapture) create() *captureFile { + file, err := os.CreateTemp("", "atmos-phase-output-*") + capture := &captureFile{file: file} + if err != nil { + capture.err = captureError(err) + return capture + } + capture.path = file.Name() + c.mu.Lock() + c.files = append(c.files, capture) + c.mu.Unlock() + return capture +} + +// cleanup closes and removes every capture file. +func (c *phaseCapture) cleanup() { + c.mu.Lock() + defer c.mu.Unlock() + for _, file := range c.files { + file.close() + if err := os.Remove(file.path); err != nil && !errors.Is(err, fs.ErrNotExist) { + log.Debug("Failed to remove captured command output", "path", file.path, "error", err) + } + } + c.files = nil +} + +// captureFile records output in a temporary file. A write failure is kept and reported when +// the output is read. The command keeps running, so a full disk cannot fail it. +type captureFile struct { + mu sync.Mutex + file *os.File + path string + err error +} + +// Write appends to the capture and always reports success to the writer chain. +// +//nolint:lintroller // Called for every output chunk; tracking would dominate the copy. +func (c *captureFile) Write(p []byte) (int, error) { + c.mu.Lock() + defer c.mu.Unlock() + if c.err == nil && c.file != nil { + if _, err := c.file.Write(p); err != nil { + c.err = captureError(err) + } + } + return len(p), nil +} + +// finish closes the file for writing and returns it as lifecycle output. +func (c *captureFile) finish() execution.Output { + c.close() + return c +} + +func (c *captureFile) close() { + c.mu.Lock() + defer c.mu.Unlock() + if c.file == nil { + return + } + if err := c.file.Close(); err != nil && c.err == nil { + c.err = captureError(err) + } + c.file = nil +} + +// Open returns a reader over the captured output, or the capture failure. +func (c *captureFile) Open() (io.Reader, error) { + defer perf.Track(nil, "terraform.captureFile.Open")() + + c.mu.Lock() + defer c.mu.Unlock() + if c.err != nil { + return nil, c.err + } + file, err := os.Open(c.path) + if err != nil { + return nil, captureError(err) + } + return file, nil +} + +// captureError marks a failure to record or read captured command output. +func captureError(err error) error { + return fmt.Errorf("%w: %w", errUtils.ErrComponentOutputCapture, err) +} diff --git a/pkg/component/terraform/execute_hooks_process_test.go b/pkg/component/terraform/execute_hooks_process_test.go index 70a4706230c..365b9de75d3 100644 --- a/pkg/component/terraform/execute_hooks_process_test.go +++ b/pkg/component/terraform/execute_hooks_process_test.go @@ -324,3 +324,71 @@ func TestPipelineCancellationStopsAHookAndEverythingAfterIt(t *testing.T) { names := timeline(readHookLog(t, pipeline.log)) require.Equal(t, "slow", names[len(names)-1], "nothing may start after the canceled hook: %v", names) } + +// TestErrorHookCaptureUsesTemporaryFiles verifies error hooks match command and hook output +// recorded in temporary files rather than memory, and that every file is removed afterwards. +func TestErrorHookCaptureUsesTemporaryFiles(t *testing.T) { + temp := t.TempDir() + for _, name := range []string{"TMPDIR", "TMP", "TEMP"} { + t.Setenv(name, temp) + } + capturedDuringCommand := 0 + shell := &fakeShell{run: func(call *shellCall) error { + switch call.command { + case "terraform": + _, _ = call.options.StderrCapture.Write([]byte("Error: Throttled")) + entries, err := os.ReadDir(temp) + require.NoError(t, err) + capturedDuringCommand = len(entries) + return errUtils.ExitCodeError{Code: 1} + case "hook-after": + _, _ = call.options.StdoutCapture.Write([]byte("HookNeedle")) + return errUtils.ExitCodeError{Code: 2} + } + return nil + }} + after := execHook("after", "apply") + after.RunOnError = true + info := hookInfo("apply", &schema.ComponentExecution{ + AfterHooks: []schema.ComponentHook{after}, + ErrorHooks: []schema.ComponentHook{ + {Name: "throttle", Commands: []string{"apply"}, Execute: []string{"hook-throttle"}, OnErrors: []string{"Throttled"}}, + {Name: "needle", Commands: []string{"apply"}, Execute: []string{"hook-needle"}, OnErrors: []string{"HookNeedle"}}, + {Name: "absent", Commands: []string{"apply"}, Execute: []string{"hook-absent"}, OnErrors: []string{"NotInAnyOutput"}}, + }, + }) + err := hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"apply"}, t.TempDir()) + require.Error(t, err) + require.Equal(t, []string{"terraform apply", "hook-after arg", "hook-throttle", "hook-needle"}, shell.names()) + require.Equal(t, 1, capturedDuringCommand, "the command output is written to one temporary file") + entries, err := os.ReadDir(temp) + require.NoError(t, err) + require.Empty(t, entries, "every capture file is removed") +} + +// TestCaptureFileReportsFailures verifies capture failures surface when error hooks read the +// output, without failing the writer chain of the command being recorded. +func TestCaptureFileReportsFailures(t *testing.T) { + missing := filepath.Join(t.TempDir(), "missing") + for _, name := range []string{"TMPDIR", "TMP", "TEMP"} { + t.Setenv(name, missing) + } + capture := &phaseCapture{} + file := capture.create() + written, err := file.Write([]byte("output")) + require.NoError(t, err, "a capture failure never fails the command") + require.Equal(t, 6, written) + _, err = file.finish().Open() + require.ErrorIs(t, err, errUtils.ErrComponentOutputCapture) + capture.cleanup() + + t.Setenv("TMPDIR", t.TempDir()) + t.Setenv("TMP", os.Getenv("TMPDIR")) + t.Setenv("TEMP", os.Getenv("TMPDIR")) + file = capture.create() + _, _ = file.Write([]byte("kept")) + require.NoError(t, os.Remove(file.path)) + _, err = file.finish().Open() + require.ErrorIs(t, err, errUtils.ErrComponentOutputCapture, "a removed capture reports a read failure") + capture.cleanup() +} From cfdb81812c1b854ed11430a1604ee0b259a4d6d3 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Thu, 12 Nov 2026 22:08:12 -0600 Subject: [PATCH 27/52] fix: address L5 configuration review findings --- internal/exec/configuration_imports_test.go | 25 ++- internal/exec/stack_processor_utils.go | 7 + pkg/component/terraform/configuration.go | 61 ++++--- .../configuration_plan_pipeline_test.go | 63 +++++++ pkg/component/terraform/execute_hooks.go | 155 ++++++++++++++++-- .../terraform/execute_hooks_process_test.go | 78 +++++++++ pkg/component/terraform/execute_hooks_test.go | 96 +++++++++++ pkg/component/terraform/execute_init.go | 9 +- pkg/component/terraform/execute_pipeline.go | 60 ++++++- pkg/component/workdir_path.go | 17 +- pkg/component/workdir_path_test.go | 16 ++ pkg/list/column/required_sections.go | 9 + pkg/list/instance_engine_test.go | 32 ++++ pkg/plugin/adapter/configuration/execution.go | 26 ++- pkg/plugin/adapter/configuration/plan.go | 26 +++ pkg/plugin/adapter/configuration/plan_test.go | 24 +++ .../preparation_execution_test.go | 33 ++++ .../adapter/configuration/preparation_test.go | 25 ++- .../configuration/toolchain_boundary_test.go | 3 +- pkg/plugin/proto/atmos/plugin/v1/plugin.pb.go | 137 +++++++++------- pkg/schema/component_execution.go | 13 ++ pkg/terraform/output/config.go | 57 +++++-- pkg/terraform/output/configuration_state.go | 93 +++++++++++ .../output/configuration_state_test.go | 138 +++++++++++++++- pkg/terraform/output/executor.go | 24 +-- .../output/executor_toolchain_test.go | 2 +- .../terragrunt/internal/engine/execution.go | 62 +++++++ .../internal/engine/execution_test.go | 88 ++++++++++ plugins/terragrunt/internal/engine/resolve.go | 30 +++- plugins/terragrunt/internal/model/model.go | 3 + .../internal/provider/preparation.go | 1 + .../internal/provider/provider_test.go | 28 ++++ proto/atmos/plugin/v1/plugin.proto | 6 + sdk/proto/atmos/plugin/v1/plugin.pb.go | 137 +++++++++------- 34 files changed, 1364 insertions(+), 220 deletions(-) diff --git a/internal/exec/configuration_imports_test.go b/internal/exec/configuration_imports_test.go index 40ab184f4a7..66314c9d5ed 100644 --- a/internal/exec/configuration_imports_test.go +++ b/internal/exec/configuration_imports_test.go @@ -31,9 +31,32 @@ func TestYAMLImportsRegisterBeforeDiscovery(t *testing.T) { runtime := &importRuntime{MockComponentConfigurationRuntime: NewMockComponentConfigurationRuntime(gomock.NewController(t))} config := &schema.AtmosConfiguration{BasePathAbsolute: root, ComponentConfigurations: runtime} config.Components.Terraform.ConfigurationProviders = map[string]schema.ComponentConfiguration{"terragrunt": {Enabled: true}} - _, err := ProcessYAMLConfigFile(config, root, path, map[string]map[string]any{}, nil, false, true, false, false, nil, nil, nil, nil, "") + importsConfig := map[string]map[string]any{} + _, err := ProcessYAMLConfigFile(config, root, path, importsConfig, nil, false, true, false, false, nil, nil, nil, nil, "") require.NoError(t, err) require.Equal(t, []string{"terragrunt://live"}, runtime.imports) + require.Equal(t, map[string]map[string]any{"terragrunt://live": {}}, importsConfig, + "a provider import must appear in the stack import report without contributing YAML") +} + +func TestStackImportReportListsProviderImports(t *testing.T) { + root := t.TempDir() + stacks := filepath.Join(root, "stacks") + require.NoError(t, os.MkdirAll(filepath.Join(stacks, "catalog"), 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(stacks, "catalog", "base.yaml"), []byte("import:\n - terragrunt://live/demo\nvars:\n team: core\n"), 0o600)) + path := filepath.Join(stacks, "dev.yaml") + require.NoError(t, os.WriteFile(path, []byte("import:\n - terragrunt://live/demo\n - catalog/base\nvars:\n stage: dev\n"), 0o600)) + runtime := &importRuntime{MockComponentConfigurationRuntime: NewMockComponentConfigurationRuntime(gomock.NewController(t))} + config := &schema.AtmosConfiguration{BasePathAbsolute: root, ComponentConfigurations: runtime} + config.Components.Terraform.ConfigurationProviders = map[string]schema.ComponentConfiguration{"terragrunt": {Enabled: true}} + + importsConfig := map[string]map[string]any{} + _, err := ProcessYAMLConfigFile(config, stacks, path, importsConfig, nil, false, true, false, false, nil, nil, nil, nil, "") + require.NoError(t, err) + require.Contains(t, importsConfig, "terragrunt://live/demo", "a provider import repeated by a nested manifest is reported once") + require.Empty(t, importsConfig["terragrunt://live/demo"]) + require.Contains(t, importsConfig, "catalog/base", "ordinary imports are still reported") + require.Len(t, importsConfig, 2) } func TestConfigurationWatchExclusionsDoNotChangeSharedIndex(t *testing.T) { diff --git a/internal/exec/stack_processor_utils.go b/internal/exec/stack_processor_utils.go index a980c69ba79..b44c44bd4a1 100644 --- a/internal/exec/stack_processor_utils.go +++ b/internal/exec/stack_processor_utils.go @@ -1639,6 +1639,13 @@ func processYAMLConfigFileWithContextInternal( if importErr != nil { return nil, nil, importErr } + // Report the provider import with the stack's other imports, keyed by its URI as + // remote imports are. It contributes no YAML, so its recorded configuration is empty. + importsConfigLock.Lock() + if _, recorded := importsConfig[imp]; !recorded { + importsConfig[imp] = map[string]any{} + } + importsConfigLock.Unlock() continue } diff --git a/pkg/component/terraform/configuration.go b/pkg/component/terraform/configuration.go index a04b8c944c3..0174c7bc483 100644 --- a/pkg/component/terraform/configuration.go +++ b/pkg/component/terraform/configuration.go @@ -34,31 +34,15 @@ func prepareConfiguration(ctx context.Context, config *schema.AtmosConfiguration if info.DryRun { return nil, nil } - // Plugins do not inherit PATH. Resolve the selected host executable before - // Terragrunt evaluates version constraints or dependency outputs. - binary, err := exec.LookPath(info.Command) - if err != nil { - return nil, fmt.Errorf("%w: Terraform executable: %w", errUtils.ErrPluginInvalidConfig, err) - } - binary, err = filepath.Abs(binary) - if err != nil { - return nil, fmt.Errorf("%w: %w", errUtils.ErrPluginInvalidConfig, err) - } - info.Command = binary - var prepared *schema.PreparedComponentConfiguration - if usesConfigurationPlan(info) { - if err = useRetainedWorkdir(config, info); err != nil { - return nil, err - } - prepared, err = restoreConfigurationPlan(ctx, config, info) - } else { - prepared, err = config.ComponentConfigurations.Prepare(ctx, info) + if err := resolveHostExecutable(info); err != nil { + return nil, err } + prepared, execution, err := prepareOrRestore(ctx, config, info) if err != nil || prepared == nil { return prepared, err } info.ComponentVarsSection = prepared.Inputs - info.ComponentExecution = prepared.Execution + info.ComponentExecution = execution info.ConfigurationSensitiveInputKeys = maps.Clone(prepared.SensitiveInputKeys) info.ComponentBackendType, info.ComponentBackendSection = prepared.BackendType, prepared.Backend info.TerraformWorkspace = prepared.Workspace @@ -81,6 +65,43 @@ func prepareConfiguration(ctx context.Context, config *schema.AtmosConfiguration return prepared, nil } +// resolveHostExecutable makes info.Command the absolute path of the selected executable. +func resolveHostExecutable(info *schema.ConfigAndStacksInfo) error { + // Plugins do not inherit PATH. Resolve the selected host executable before + // Terragrunt evaluates version constraints or dependency outputs. + binary, err := exec.LookPath(info.Command) + if err != nil { + return fmt.Errorf("%w: Terraform executable: %w", errUtils.ErrPluginInvalidConfig, err) + } + binary, err = filepath.Abs(binary) + if err != nil { + return fmt.Errorf("%w: %w", errUtils.ErrPluginInvalidConfig, err) + } + info.Command = binary + return nil +} + +// prepareOrRestore prepares the component, or restores the reviewed preparation of a saved +// plan. It also returns the hooks and extra arguments the invocation runs: a saved plan was +// prepared for plan, so it runs the apply-time metadata recorded with it. +func prepareOrRestore(ctx context.Context, config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) (*schema.PreparedComponentConfiguration, *schema.ComponentExecution, error) { + if !usesConfigurationPlan(info) { + prepared, err := config.ComponentConfigurations.Prepare(ctx, info) + if err != nil || prepared == nil { + return prepared, nil, err + } + return prepared, prepared.Execution, nil + } + if err := useRetainedWorkdir(config, info); err != nil { + return nil, nil, err + } + prepared, err := restoreConfigurationPlan(ctx, config, info) + if err != nil || prepared == nil { + return prepared, nil, err + } + return prepared, prepared.Execution.ForSavedPlanApply(), nil +} + // isLocalModuleSource reports whether the evaluated module source is a path on this machine. func isLocalModuleSource(uri string) bool { return strings.HasPrefix(uri, ".") || filepath.IsAbs(uri) diff --git a/pkg/component/terraform/configuration_plan_pipeline_test.go b/pkg/component/terraform/configuration_plan_pipeline_test.go index 9c95e195506..71289720b0e 100644 --- a/pkg/component/terraform/configuration_plan_pipeline_test.go +++ b/pkg/component/terraform/configuration_plan_pipeline_test.go @@ -122,6 +122,69 @@ func TestApplyOfReviewedPlanReusesReviewedPreparationAndWorkdir(t *testing.T) { require.Contains(t, strings.Split(string(args), "\n"), planPath) } +// Terragrunt evaluates the configuration again for apply, so a hook enabled only for apply is +// absent from the plan-time preparation. Applying the reviewed plan runs the apply-time hooks +// recorded with it, and not the plan-time ones. +func TestApplyOfReviewedPlanRunsApplyTimeHooks(t *testing.T) { + exe, err := os.Executable() + require.NoError(t, err) + hook := func(name string) schema.ComponentHook { + return schema.ComponentHook{Name: name, Commands: []string{"apply"}, Execute: []string{exe, name}} + } + project := newReviewedPlanProject(t) + reviewed := project.prepared() + reviewed.Execution = &schema.ComponentExecution{ + AfterHooks: []schema.ComponentHook{hook("plan-time")}, + Apply: &schema.ComponentExecution{AfterHooks: []schema.ComponentHook{hook("notify"), hook("always")}}, + } + info := project.info(t, "plan") + project.provider.EXPECT().Prepare(gomock.Any(), info).Return(reviewed, nil) + planned, err := testExecutor(t).prepareComponentExecution(t.Context(), provisioner.OutputWriters{}, project.config, info, false) + require.NoError(t, err) + planPath := filepath.Join(planned.workingDir, "reviewed.planfile") + require.NoError(t, os.WriteFile(planPath, []byte("reviewed plan"), 0o600)) + require.NoError(t, recordConfigurationPlan(t.Context(), project.config, info, planned, []string{"-out=" + planPath})) + + apply := project.reviewedApply(t, planPath) + execution, err := testExecutor(t).prepareComponentExecution(t.Context(), provisioner.OutputWriters{}, project.config, apply, false) + require.NoError(t, err) + require.Equal(t, &schema.ComponentExecution{AfterHooks: []schema.ComponentHook{hook("notify"), hook("always")}}, apply.ComponentExecution) + + // Terraform and the hooks are all the test binary, which records every invocation. + log := filepath.Join(t.TempDir(), "hooks.jsonl") + apply.SkipInit = true + apply.ComponentBackendType = "http" // Avoid a separate workspace subprocess. + apply.ComponentEnvList = append(apply.ComponentEnvList, testEnvHookChild+"=1", testEnvHookLog+"="+log) + require.NoError(t, testExecutor(t).executeCommandPipeline(project.config, apply, execution)) + var ran []string + for _, record := range readHookLog(t, log) { + ran = append(ran, record.Arguments[0]) + } + require.Equal(t, []string{"apply", "notify", "always"}, ran) +} + +// Plan runs with the provider's module source, so its directory follows source target rules, +// including an explicit working_directory. A new apply process whose discovered section carries +// no source must locate the same retained directory without provisioning anything. +func TestApplyOfReviewedPlanFindsRetainedWorkdirWithoutDiscoveredSource(t *testing.T) { + project := newReviewedPlanProject(t) + plan := project.info(t, "plan") + delete(plan.ComponentSection, "source") + project.provider.EXPECT().Prepare(gomock.Any(), plan).Return(project.prepared(), nil) + planned, err := testExecutor(t).prepareComponentExecution(t.Context(), provisioner.OutputWriters{}, project.config, plan, false) + require.NoError(t, err) + require.Equal(t, project.workdir, planned.workingDir) + planPath := filepath.Join(planned.workingDir, "reviewed.planfile") + require.NoError(t, os.WriteFile(planPath, []byte("reviewed plan"), 0o600)) + require.NoError(t, recordConfigurationPlan(t.Context(), project.config, plan, planned, []string{"-out=" + planPath})) + + apply := project.reviewedApply(t, planPath) + delete(apply.ComponentSection, "source") + execution, err := testExecutor(t).prepareComponentExecution(t.Context(), provisioner.OutputWriters{}, project.config, apply, false) + require.NoError(t, err) + require.Equal(t, project.workdir, execution.componentPath) +} + func TestApplyOfReviewedPlanRejectsChangedWorkdir(t *testing.T) { project := newReviewedPlanProject(t) planPath := project.review(t) diff --git a/pkg/component/terraform/execute_hooks.go b/pkg/component/terraform/execute_hooks.go index 1cde7cb0681..0c452c795cd 100644 --- a/pkg/component/terraform/execute_hooks.go +++ b/pkg/component/terraform/execute_hooks.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "io/fs" + "maps" "os" "path/filepath" "slices" @@ -19,6 +20,7 @@ import ( "github.com/cloudposse/atmos/pkg/perf" "github.com/cloudposse/atmos/pkg/process" "github.com/cloudposse/atmos/pkg/schema" + "github.com/cloudposse/atmos/pkg/terraform/autoinit" "github.com/cloudposse/atmos/pkg/ui" ) @@ -50,6 +52,25 @@ type executionPhase struct { // mainCommandArgsKey carries the effective main command arguments to plan binding. type mainCommandArgsKey struct{} +// mainCommandRecoveryKey carries smart init's recovery into the main command's hook lifecycle. +type mainCommandRecoveryKey struct{} + +// mainCommandRecovery runs smart init's recovery for a failed main command. Its retry re-runs only +// the Terraform subprocess, inside the lifecycle that already ran the before hooks. +type mainCommandRecovery struct { + recover func(mainErr error, retry func() error) error +} + +// lifecycleOwnsRecovery reports whether the main command runs inside a hook lifecycle that +// recovers from a missing init itself. Dry runs never run hooks, so they recover outside it. +func lifecycleOwnsRecovery(info *schema.ConfigAndStacksInfo) bool { + config := info.ComponentExecution + if info.DryRun || config == nil { + return false + } + return len(config.BeforeHooks)+len(config.AfterHooks)+len(config.ErrorHooks) > 0 +} + // runMainShellCommand runs the requested Terraform subprocess, wrapped by the component's // provider-supplied hooks and extra arguments. They match the requested command, which is // "destroy" for `apply -destroy`. @@ -63,25 +84,36 @@ func (e *Executor) runMainShellCommand(atmosConfig *schema.AtmosConfiguration, i componentPath: componentPath, } return e.runExecutionPhase(atmosConfig, info, phase, opts, func(phaseInfo *schema.ConfigAndStacksInfo, phaseArgs []string, phaseOpts []component.ExecutionOption) error { - if captured, ok := component.ContextFromOptions(phaseOpts...).Value(mainCommandArgsKey{}).(*[]string); ok { + ctx := component.ContextFromOptions(phaseOpts...) + if captured, ok := ctx.Value(mainCommandArgsKey{}).(*[]string); ok { *captured = slices.Clone(phaseArgs) } - return ExecuteShellCommandWithRetry( - atmosConfig, - phaseInfo, - info.SubCommand, - func(o ...component.ExecutionOption) error { - return e.executeStreamingOrShell(atmosConfig, phaseInfo, &streamingExecRequest{ - componentPath: componentPath, - args: phaseArgs, - gatePhase: info.SubCommand, - subCommand: info.SubCommand, - redirectStdErr: info.RedirectStdErr, - shellOpts: o, - }) - }, - phaseOpts..., - ) + run := func() error { + return ExecuteShellCommandWithRetry( + atmosConfig, + phaseInfo, + info.SubCommand, + func(o ...component.ExecutionOption) error { + return e.executeStreamingOrShell(atmosConfig, phaseInfo, &streamingExecRequest{ + componentPath: componentPath, + args: phaseArgs, + gatePhase: info.SubCommand, + subCommand: info.SubCommand, + redirectStdErr: info.RedirectStdErr, + shellOpts: o, + }) + }, + phaseOpts..., + ) + } + err := run() + if err == nil || !lifecycleOwnsRecovery(info) { + return err + } + if recovery, ok := ctx.Value(mainCommandRecoveryKey{}).(*mainCommandRecovery); ok { + return recovery.recover(err, run) + } + return err }) } @@ -427,3 +459,92 @@ func (c *captureFile) Open() (io.Reader, error) { func captureError(err error) error { return fmt.Errorf("%w: %w", errUtils.ErrComponentOutputCapture, err) } + +// initFileFlags are the init options whose value can name a file Terraform reads during init. +var initFileFlags = []string{"-backend-config", "-var-file"} + +// withProviderInitSettings adds the init settings that provider extra arguments contribute to smart +// init's inputs: the effective init arguments and environment, and the contents of files those +// arguments name. Without them, an edited init-only backend file would leave a stale initialization +// trusted. Values only contribute to the fingerprint digest; they are never recorded or logged. +// Settings that cannot be evaluated return nil inputs, so no recorded marker is trusted; the init +// phase itself then reports the problem. +func withProviderInitSettings(in *autoinit.Inputs, info *schema.ConfigAndStacksInfo, componentPath string) *autoinit.Inputs { + config := info.ComponentExecution + if in == nil || config.IsEmpty() { + return in + } + applied, err := execution.ApplyExtraArguments(config.ExtraArguments, &execution.ArgumentsRequest{ + Command: subcommandInit, Args: []string{subcommandInit}, InsertAt: 1, WorkingDirectory: componentPath, + }) + if err != nil { + return nil + } + in.EnvLookup = overlayEnvironment(applied.Environment, in.EnvLookup) + extra := maps.Clone(in.Extra) + if extra == nil { + extra = map[string]string{} + } + arguments := applied.Args[1:] + extra["provider-init-arguments"] = strings.Join(arguments, "\x00") + extra["provider-init-environment"] = strings.Join(applied.Environment, "\x00") + for _, path := range initFileArguments(arguments) { + if !filepath.IsAbs(path) { + path = filepath.Join(componentPath, path) + } + // A missing or unreadable file is recorded as such, so creating it changes the digest. + digest, err := fileDigest(path) + if err != nil { + digest = "unreadable" + } + extra["provider-init-file:"+path] = digest + } + in.Extra = extra + return in +} + +// overlayEnvironment looks up KEY=value entries first and then base. A nil base reports a key +// absent, so the caller falls back to the process environment. +func overlayEnvironment(entries []string, base func(string) (string, bool)) func(string) (string, bool) { + environment := map[string]string{} + for _, entry := range entries { + if key, value, ok := strings.Cut(entry, "="); ok { + environment[key] = value + } + } + return func(key string) (string, bool) { + if value, ok := environment[key]; ok { + return value, true + } + if base == nil { + return "", false + } + return base(key) + } +} + +// initFileArguments returns the file paths named by -backend-config and -var-file options, in +// both the "-flag=value" and "-flag value" forms. A -backend-config value holding "=" sets one +// setting instead of naming a file; it is already part of the arguments. +func initFileArguments(arguments []string) []string { + var paths []string + for i := 0; i < len(arguments); i++ { + name, value, inline := strings.Cut(strings.TrimPrefix(arguments[i], "-"), "=") + name = "-" + name + if !slices.Contains(initFileFlags, name) { + continue + } + if !inline { + if i+1 >= len(arguments) { + break + } + i++ + value = arguments[i] + } + if name == "-backend-config" && strings.Contains(value, "=") { + continue + } + paths = append(paths, value) + } + return paths +} diff --git a/pkg/component/terraform/execute_hooks_process_test.go b/pkg/component/terraform/execute_hooks_process_test.go index 365b9de75d3..0432bcac624 100644 --- a/pkg/component/terraform/execute_hooks_process_test.go +++ b/pkg/component/terraform/execute_hooks_process_test.go @@ -25,6 +25,7 @@ import ( "github.com/cloudposse/atmos/pkg/component" "github.com/cloudposse/atmos/pkg/process" "github.com/cloudposse/atmos/pkg/schema" + autoinit "github.com/cloudposse/atmos/pkg/terraform/autoinit" ) const ( @@ -392,3 +393,80 @@ func TestCaptureFileReportsFailures(t *testing.T) { require.ErrorIs(t, err, errUtils.ErrComponentOutputCapture, "a removed capture reports a read failure") capture.cleanup() } + +// Smart init must see the init settings a provider adds through extra arguments. Otherwise an +// edited init-only backend file would leave the previous initialization trusted. +func TestSmartInitFingerprintIncludesProviderInitSettings(t *testing.T) { + dir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(dir, "main.tf"), []byte(`terraform { + backend "local" {} +} +`), 0o600)) + backendFile := filepath.Join(dir, "backend.hcl") + varFile := filepath.Join(t.TempDir(), "init.tfvars") + require.NoError(t, os.WriteFile(backendFile, []byte(`path = "a.tfstate"`), 0o600)) + require.NoError(t, os.WriteFile(varFile, []byte(`a = 1`), 0o600)) + atmosConfig := &schema.AtmosConfiguration{} + info := &schema.ConfigAndStacksInfo{Command: "terraform", ComponentEnvList: []string{"BASE=1"}, ComponentExecution: &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{ + {Commands: []string{"init"}, Arguments: []string{"-backend-config=backend.hcl", "-backend-config", "bucket=one", "-var-file", varFile}}, + {Commands: []string{"plan"}, Arguments: []string{"-backend-config=ignored-by-init"}, Environment: map[string]string{"PLAN_ONLY": "1"}}, + }, + }} + fingerprint := func() autoinit.Fingerprint { + t.Helper() + in := newAutoInitInputs(atmosConfig, info, dir, "") + require.NotNil(t, in) + result, err := autoinit.Compute(in) + require.NoError(t, err) + return result + } + base := fingerprint() + require.Equal(t, base, fingerprint(), "unchanged settings keep the fingerprint") + + // Settings for other commands do not affect init. + info.ComponentExecution.ExtraArguments[1].Arguments = []string{"-lock=false"} + info.ComponentExecution.ExtraArguments[1].Environment = nil + require.Equal(t, base.Hash, fingerprint().Hash) + + for name, change := range map[string]func(){ + "backend file contents": func() { require.NoError(t, os.WriteFile(backendFile, []byte(`path = "b.tfstate"`), 0o600)) }, + "backend file removed": func() { require.NoError(t, os.Remove(backendFile)) }, + "var file contents": func() { require.NoError(t, os.WriteFile(varFile, []byte(`a = 2`), 0o600)) }, + "backend value": func() { info.ComponentExecution.ExtraArguments[0].Arguments[2] = "bucket=two" }, + "init environment": func() { + info.ComponentExecution.ExtraArguments[0].Environment = map[string]string{"TF_CLI_ARGS_init": "-upgrade"} + }, + } { + previous := fingerprint().Hash + change() + require.NotEqual(t, previous, fingerprint().Hash, name) + } +} + +// The data directory an init-only TF_DATA_DIR selects is where smart init looks for its marker. +func TestSmartInitUsesProviderInitDataDirectory(t *testing.T) { + dir := t.TempDir() + data := filepath.Join(t.TempDir(), "data") + info := &schema.ConfigAndStacksInfo{Command: "terraform", ComponentExecution: &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"init"}, Environment: map[string]string{"TF_DATA_DIR": data}}}, + }} + in := newAutoInitInputs(&schema.AtmosConfiguration{}, info, dir, "") + value, ok := in.EnvLookup("TF_DATA_DIR") + require.True(t, ok) + require.Equal(t, data, value) + require.Equal(t, data, autoinit.DataDir(dir, in.EnvLookup)) + + // Without provider execution, the inputs are unchanged. + info.ComponentExecution = nil + plain := newAutoInitInputs(&schema.AtmosConfiguration{}, info, dir, "") + require.Empty(t, plain.Extra) +} + +// Settings smart init cannot evaluate never let a recorded marker be trusted. +func TestSmartInitDistrustsUnresolvableProviderInitSettings(t *testing.T) { + info := &schema.ConfigAndStacksInfo{Command: "terraform", ComponentExecution: &schema.ComponentExecution{ + ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"init"}, RequiredVarFiles: []string{"missing.tfvars"}}}, + }} + require.Nil(t, newAutoInitInputs(&schema.AtmosConfiguration{}, info, t.TempDir(), "")) +} diff --git a/pkg/component/terraform/execute_hooks_test.go b/pkg/component/terraform/execute_hooks_test.go index eb56f0a9654..1358cacdc63 100644 --- a/pkg/component/terraform/execute_hooks_test.go +++ b/pkg/component/terraform/execute_hooks_test.go @@ -572,3 +572,99 @@ func TestPolicyPlanUsesProviderOutputArguments(t *testing.T) { }) } } + +// TestInitRecoveryRetriesInsideOneHookLifecycle verifies that smart init's recovery retry +// stays inside the main command's lifecycle: before and after hooks run once around both +// attempts, and the forced init runs between them. +func TestInitRecoveryRetriesInsideOneHookLifecycle(t *testing.T) { + attempts := 0 + shell := &fakeShell{run: func(call *shellCall) error { + if call.command != "terraform" { + return nil + } + attempts++ + if attempts == 1 { + _, _ = io.WriteString(call.options.ExecMetadataStderrCapture, "Error: Backend configuration changed") + return errUtils.ExitCodeError{Code: 1} + } + _, _ = io.WriteString(call.options.ExecMetadataStdoutCapture, "Apply complete!") + return nil + }} + executor := testExecutor(t) + executor.services.ExecuteShellCommand = shell.execute + var timeline []string + executor.executeTerraformInitForcedFn = func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _, _ string, reconfigure, _ bool, _ ...ShellCommandOption) error { + require.True(t, reconfigure) + timeline = append(timeline, "forced init") + return nil + } + config := &schema.AtmosConfiguration{} + config.Components.Terraform.Init.Reconfigure = schema.TerraformInitReconfigureAuto + info := hookInfo("apply", &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{execHook("before", "apply")}, + AfterHooks: []schema.ComponentHook{execHook("after", "apply")}, + }) + execCtx := &componentExecContext{componentPath: t.TempDir()} + + err := executor.runMainCommandPhase(config, info, execCtx, []string{"apply"}, execCtx.componentPath, false) + require.NoError(t, err) + require.Equal(t, []string{"hook-before arg", "terraform apply", "terraform apply", "hook-after arg"}, shell.names(), + "hooks must wrap the user command once, not each recovery attempt") + require.Equal(t, []string{"forced init"}, timeline) + require.Equal(t, "Apply complete!", info.ExecMetadataRawOutput, "exec metadata must reflect only the retry") +} + +// TestInitRecoveryWithoutHooksStillRetriesOnce verifies that a component without provider +// hooks keeps the existing recovery path and retries the main command exactly once. +func TestInitRecoveryWithoutHooksStillRetriesOnce(t *testing.T) { + attempts := 0 + shell := &fakeShell{run: func(call *shellCall) error { + attempts++ + if attempts == 1 { + _, _ = io.WriteString(call.options.ExecMetadataStderrCapture, "Error: Backend configuration changed") + return errUtils.ExitCodeError{Code: 1} + } + return nil + }} + executor := testExecutor(t) + executor.services.ExecuteShellCommand = shell.execute + forced := 0 + executor.executeTerraformInitForcedFn = func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, string, string, bool, bool, ...ShellCommandOption) error { + forced++ + return nil + } + config := &schema.AtmosConfiguration{} + config.Components.Terraform.Init.Reconfigure = schema.TerraformInitReconfigureAuto + info := hookInfo("apply", nil) + execCtx := &componentExecContext{componentPath: t.TempDir()} + + require.NoError(t, executor.runMainCommandPhase(config, info, execCtx, []string{"apply"}, execCtx.componentPath, false)) + require.Equal(t, []string{"terraform apply", "terraform apply"}, shell.names()) + require.Equal(t, 1, forced) +} + +// TestInitRecoveryInsideLifecycleRunsOnlyWhenDiagnosed verifies that a hooked command failing +// for another reason is not retried and still reaches its after hooks once. +func TestInitRecoveryInsideLifecycleRunsOnlyWhenDiagnosed(t *testing.T) { + shell := &fakeShell{run: func(call *shellCall) error { + if call.command == "terraform" { + _, _ = io.WriteString(call.options.ExecMetadataStderrCapture, "Error: unrelated validation failure") + return errUtils.ExitCodeError{Code: 1} + } + return nil + }} + executor := testExecutor(t) + executor.services.ExecuteShellCommand = shell.execute + executor.executeTerraformInitForcedFn = func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, string, string, bool, bool, ...ShellCommandOption) error { + t.Fatal("forced init must not run without an init-required diagnosis") + return nil + } + always := execHook("after", "apply") + always.RunOnError = true + info := hookInfo("apply", &schema.ComponentExecution{AfterHooks: []schema.ComponentHook{always}}) + execCtx := &componentExecContext{componentPath: t.TempDir()} + + err := executor.runMainCommandPhase(&schema.AtmosConfiguration{}, info, execCtx, []string{"apply"}, execCtx.componentPath, false) + require.Equal(t, 1, resolveExitCode(err)) + require.Equal(t, []string{"terraform apply", "hook-after arg"}, shell.names()) +} diff --git a/pkg/component/terraform/execute_init.go b/pkg/component/terraform/execute_init.go index 4d879dbf5c5..ffa891d22bd 100644 --- a/pkg/component/terraform/execute_init.go +++ b/pkg/component/terraform/execute_init.go @@ -89,10 +89,11 @@ func buildInitArgs(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAn } // newAutoInitInputs builds this invocation's fingerprint inputs -- the thin internal/exec-local -// wrapper around autoinit.InputsFromInfo shared by buildInitSubcommandArgs, recordAutoInit, and -// executeTerraformInitForced. +// wrapper around autoinit.InputsFromInfo shared by buildInitSubcommandArgs, recordAutoInit, +// executeTerraformInitCommand, and executeTerraformInitForced. It adds the init settings that +// provider extra arguments contribute, which the init phase applies. func newAutoInitInputs(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentPath, varFile string) *autoinit.Inputs { - return autoinit.InputsFromInfo(atmosConfig, info, componentPath, varFile) + return withProviderInitSettings(autoinit.InputsFromInfo(atmosConfig, info, componentPath, varFile), info, componentPath) } // decideAutoInit resolves atmos.yaml's init policy plus force into an autoinit.Request and @@ -324,7 +325,7 @@ func prerequisiteOutputOptions(info *schema.ConfigAndStacksInfo, opts []componen 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) + in := newAutoInitInputs(atmosConfig, info, componentPath, varFile) decision := autoinit.Decide(autoinit.RequestFromInfo(atmosConfig, info, in, false)) if !decision.RunInit { diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index a8eb1655d2a..b133bc0c6f2 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -280,6 +280,13 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every var effectiveArgs []string captureCtx := context.WithValue(component.ContextFromOptions(opts...), mainCommandArgsKey{}, &effectiveArgs) captureArgs := component.WithProcessContext(captureCtx) + // A component with provider-supplied hooks recovers inside its main-command lifecycle, so + // the retry runs between the same before and after hooks instead of repeating them. + recovery := &mainCommandRecovery{recover: func(mainErr error, retry func() error) error { + return e.recoverWithinLifecycle(atmosConfig, info, execCtx, componentPath, mainErr, + &execMetadataStdoutBuf, &execMetadataStderrBuf, retry, append(slices.Clone(opts), captureArgs)...) + }} + captureArgs = component.WithProcessContext(context.WithValue(captureCtx, mainCommandRecoveryKey{}, recovery)) mainOpts := append(slices.Clone(opts), component.WithMetadataOutputCapture(&execMetadataStdoutBuf, &execMetadataStderrBuf), captureArgs) // Phase-level CI log grouping (Dimension "phase"): fold the main subcommand @@ -289,7 +296,7 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every return e.executeGovernedTerraformCommand(atmosConfig, info, allArgsAndFlags, componentPath, uploadStatusFlag, execCtx.planPolicy, mainOpts...) }) info.ExecMetadataRawOutput = combineExecMetadataOutput(&execMetadataStdoutBuf, &execMetadataStderrBuf) - if mainErr != nil { + if mainErr != nil && !lifecycleOwnsRecovery(info) { mainErr = e.recoverFromInitRequired(atmosConfig, info, execCtx, allArgsAndFlags, componentPath, uploadStatusFlag, mainErr, &execMetadataStdoutBuf, &execMetadataStderrBuf, append(slices.Clone(opts), captureArgs)...) } @@ -327,17 +334,36 @@ func (e *Executor) recoverFromInitRequired( //nolint:revive // argument-limit: e stdoutBuf, stderrBuf *bytes.Buffer, opts ...component.ExecutionOption, ) error { + retry, err := e.runInitRecovery(atmosConfig, info, execCtx, componentPath, mainErr, stdoutBuf, stderrBuf, opts...) + if !retry { + return err + } + return e.retryMainCommand(atmosConfig, info, allArgsAndFlags, componentPath, uploadStatusFlag, execCtx.planPolicy, opts...) +} + +// runInitRecovery classifies a failed main command and, when smart init's recovery policy +// allows it, runs the forced init the diagnostic asked for. It reports whether the main command +// should now be retried; otherwise it returns the error to propagate. +func (e *Executor) runInitRecovery( //nolint:revive // argument-limit: every parameter is load-bearing context from the recovering caller. + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + execCtx *componentExecContext, + componentPath string, + mainErr error, + stdoutBuf, stderrBuf *bytes.Buffer, + opts ...component.ExecutionOption, +) (bool, error) { if info.SubCommand == subcommandInit { - return mainErr + return false, mainErr } combined := stdoutBuf.String() + "\n" + stderrBuf.String() + "\n" + mainErr.Error() rec, policyErr := classifyInitRecovery(atmosConfig, info, combined) if policyErr != nil { - return errors.Join(mainErr, policyErr) + return false, errors.Join(mainErr, policyErr) } if !rec.Run { - return mainErr + return false, mainErr } ui.Warning(fmt.Sprintf("Terraform requires initialization; running init and retrying '%s'", info.SubCommand)) @@ -346,10 +372,32 @@ func (e *Executor) recoverFromInitRequired( //nolint:revive // argument-limit: e return e.executeTerraformInitForcedFn(atmosConfig, info, componentPath, execCtx.varFile, rec.WithReconfigure, rec.WithUpgrade, opts...) }) if initErr != nil { - return errors.Join(mainErr, initErr) + return false, errors.Join(mainErr, initErr) } + return true, nil +} - return e.retryMainCommand(atmosConfig, info, allArgsAndFlags, componentPath, uploadStatusFlag, execCtx.planPolicy, opts...) +// recoverWithinLifecycle is recoverFromInitRequired for a main command wrapped by provider +// hooks. It runs inside that command's lifecycle and retries only the Terraform subprocess, so +// before and after hooks run once for the user command. The exec-metadata capture is reset so +// it reflects only the retry's own output, as retryMainCommand does. +func (e *Executor) recoverWithinLifecycle( //nolint:revive // argument-limit: every parameter is load-bearing context from runMainCommandPhase. + atmosConfig *schema.AtmosConfiguration, + info *schema.ConfigAndStacksInfo, + execCtx *componentExecContext, + componentPath string, + mainErr error, + stdoutBuf, stderrBuf *bytes.Buffer, + retry func() error, + opts ...component.ExecutionOption, +) error { + again, err := e.runInitRecovery(atmosConfig, info, execCtx, componentPath, mainErr, stdoutBuf, stderrBuf, opts...) + if !again { + return err + } + stdoutBuf.Reset() + stderrBuf.Reset() + return retry() } // retryMainCommand re-runs the main subcommand once after a forced init recovery, resetting the diff --git a/pkg/component/workdir_path.go b/pkg/component/workdir_path.go index 037f7883af2..2164d6acf56 100644 --- a/pkg/component/workdir_path.go +++ b/pkg/component/workdir_path.go @@ -222,7 +222,7 @@ func ProvisionAndResolveComponentPath( ) (string, bool, error) { defer perf.Track(atmosConfig, "component.ProvisionAndResolveComponentPath")() if info.DryRun { - return resolveDryRunComponentPath(atmosConfig, info, componentType, fallbackComponentPath) + return resolveDryRunComponentPath(atmosConfig, info, componentType, fallbackComponentPath, provSource.HasSource(info.ComponentSection)) } // Provision an isolated workdir for LOCAL Terraform components (no JIT source) up front — before @@ -308,11 +308,13 @@ func DryRunDefersMissingPath(info *schema.ConfigAndStacksInfo) bool { // resolveDryRunComponentPath validates placement without downloading source or copying a workdir. // The existence result describes the current filesystem, not a simulated successful provision. -func resolveDryRunComponentPath(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentType, fallback string) (string, bool, error) { +// A sourced component's directory follows source target rules, which honor an explicit +// working_directory for an enabled workdir. +func resolveDryRunComponentPath(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentType, fallback string, sourced bool) (string, bool, error) { var err error isWorkdir := provWorkdir.IsWorkdirEnabled(info.ComponentSection) && - (componentType == cfg.TerraformComponentType || provSource.HasSource(info.ComponentSection)) - if provSource.HasSource(info.ComponentSection) { + (componentType == cfg.TerraformComponentType || sourced) + if sourced { fallback, err = dryRunSourceTarget(atmosConfig, info, componentType) } else if componentType == cfg.TerraformComponentType && isWorkdir { fallback, err = provWorkdir.BuildPath(atmosConfig.BasePath, componentType, info.FinalComponent, info.Stack, info.ComponentSection) @@ -336,11 +338,14 @@ func resolveDryRunComponentPath(atmosConfig *schema.AtmosConfiguration, info *sc // ResolveRetainedWorkdir records, without provisioning anything, the directory an earlier run // populated and returns it. Reviewed saved plans use it to find their retained workdir in a new -// process, where only provisioning would otherwise set provWorkdir.WorkdirPathKey. +// process, where only provisioning would otherwise set provWorkdir.WorkdirPathKey. The plan ran +// with the provider's module source, so an enabled workdir follows source target rules, including +// an explicit working_directory, even when the discovered section carries no source. func ResolveRetainedWorkdir(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, componentType, fallbackComponentPath string) (string, error) { defer perf.Track(atmosConfig, "component.ResolveRetainedWorkdir")() - path, _, err := resolveDryRunComponentPath(atmosConfig, info, componentType, fallbackComponentPath) + sourced := provSource.HasSource(info.ComponentSection) || provWorkdir.IsWorkdirEnabled(info.ComponentSection) + path, _, err := resolveDryRunComponentPath(atmosConfig, info, componentType, fallbackComponentPath, sourced) return path, err } diff --git a/pkg/component/workdir_path_test.go b/pkg/component/workdir_path_test.go index c39f0acc3a0..69a1a1902a6 100644 --- a/pkg/component/workdir_path_test.go +++ b/pkg/component/workdir_path_test.go @@ -622,6 +622,22 @@ func TestResolveRetainedWorkdirRecordsPathWithoutProvisioning(t *testing.T) { require.NoError(t, err) assert.Empty(t, entries, "locating a retained workdir must not create or download anything") + // A reviewed plan ran with the provider's module source. Without a discovered source, an + // enabled workdir still follows source target rules, including an explicit working_directory. + chosen := filepath.Join(base, "chosen") + unsourced := &schema.ConfigAndStacksInfo{FinalComponent: "vpc", Stack: "dev", ComponentSection: map[string]any{ + "component": "vpc", "atmos_component": "vpc", "atmos_stack": "dev", + "provision": map[string]any{"workdir": map[string]any{"enabled": true}}, + "metadata": map[string]any{"working_directory": chosen}, + }} + path, err = ResolveRetainedWorkdir(config, unsourced, cfg.TerraformComponentType, fallback) + require.NoError(t, err) + assert.Equal(t, chosen, path) + assert.Equal(t, chosen, unsourced.ComponentSection[provWorkdir.WorkdirPathKey]) + entries, err = os.ReadDir(base) + require.NoError(t, err) + assert.Empty(t, entries, "locating a retained workdir must not create or download anything") + // Without a workdir the component keeps its own directory and nothing is recorded. plain := &schema.ConfigAndStacksInfo{FinalComponent: "vpc", Stack: "dev", ComponentSection: map[string]any{}} path, err = ResolveRetainedWorkdir(config, plain, cfg.TerraformComponentType, base) diff --git a/pkg/list/column/required_sections.go b/pkg/list/column/required_sections.go index 356c5c31b09..c9be79cb5b6 100644 --- a/pkg/list/column/required_sections.go +++ b/pkg/list/column/required_sections.go @@ -37,6 +37,14 @@ var derivedFields = map[string]struct{}{ "component_base": {}, "inherits": {}, "type": {}, "tags": {}, "labels": {}, "stack_count": {}, } +// derivedFieldInputs lists the component fields a derived field is computed from. RequiredPaths +// evaluates exactly these fields, so a templated input never decides the value raw, without +// requiring (and fully evaluating) the sections that contain them. The engine comes from +// settings.configuration.engine or the component command. +var derivedFieldInputs = map[string][][]string{ + "engine": {{"settings", "configuration", "engine"}, {"command"}}, +} + // RequiredSections statically determines which top-level Atmos component sections (vars, // settings, metadata, env, backend) the given column templates actually reference, by walking // each column's parsed Go-template AST for field references (see pkg/template.ExtractFieldRefs) @@ -110,6 +118,7 @@ func RequiredPaths(columns []Config) [][]string { if _, ok := sectionBackedFields[ref.Path[0]]; ok { paths = append(paths, ref.Path) } + paths = append(paths, derivedFieldInputs[ref.Path[0]]...) } } } diff --git a/pkg/list/instance_engine_test.go b/pkg/list/instance_engine_test.go index 19e8b7889fb..0bbd8436693 100644 --- a/pkg/list/instance_engine_test.go +++ b/pkg/list/instance_engine_test.go @@ -8,6 +8,7 @@ import ( "go.uber.org/mock/gomock" e "github.com/cloudposse/atmos/internal/exec" + "github.com/cloudposse/atmos/pkg/deferred" "github.com/cloudposse/atmos/pkg/list/column" "github.com/cloudposse/atmos/pkg/list/extract" "github.com/cloudposse/atmos/pkg/schema" @@ -104,3 +105,34 @@ func TestInstanceEngineReachesRenderedColumns(t *testing.T) { {"yaml", "live", "terraform", "terraform"}, }, rows) } + +// TestEngineColumnEvaluatesItsInputs verifies that selecting the Engine column evaluates the +// fields the engine is derived from, so a templated command decides it only after rendering, +// while the narrowed evaluation still leaves unrelated fields alone. +func TestEngineColumnEvaluatesItsInputs(t *testing.T) { + columns := []column.Config{{Name: "Engine", Value: "{{ .engine }}"}} + sections, ok := column.RequiredSections(columns) + require.True(t, ok) + require.Empty(t, sections, "the inputs are narrowed to fields, not whole sections") + require.ElementsMatch(t, [][]string{{"settings", "configuration", "engine"}, {"command"}}, column.RequiredPaths(columns)) + + config := &schema.AtmosConfiguration{} + paths := resolveInstancesEvaluationPaths(config, columns, &InstancesCommandOptions{}) + require.Contains(t, paths, []string{"command"}) + require.Contains(t, paths, []string{"settings", "configuration", "engine"}) + require.NotContains(t, paths, []string{"settings"}, "settings are not evaluated wholesale for the engine") + + component := map[string]any{ + "command": "{{ .settings.tool }}", + "settings": map[string]any{"tool": "tofu", "configuration": map[string]any{"engine": ""}}, + "vars": map[string]any{"secret": "!terraform.output other live password"}, + } + expanded := deferred.ExpandEvaluationPaths(component, paths) + selected, _ := deferred.SplitEvaluationFields(component, expanded) + require.Contains(t, selected, "command", "the templated command must be evaluated before the engine is derived") + require.Contains(t, expanded, []string{"settings", "tool"}, "the command's own template inputs are evaluated too") + require.NotContains(t, selected, "vars", "unrelated sections stay unevaluated") + + // Other derived columns still read nothing. + require.Empty(t, column.RequiredPaths([]column.Config{{Name: "Component", Value: "{{ .component }}"}})) +} diff --git a/pkg/plugin/adapter/configuration/execution.go b/pkg/plugin/adapter/configuration/execution.go index b9550447c15..a2a0d6d9e52 100644 --- a/pkg/plugin/adapter/configuration/execution.go +++ b/pkg/plugin/adapter/configuration/execution.go @@ -1,6 +1,7 @@ package configuration import ( + "fmt" "maps" "slices" @@ -16,6 +17,28 @@ func preparedExecution(source *pluginv1.ComponentExecutionConfiguration) (*schem if source == nil { return nil, nil } + result, err := executionMetadata(source) + if err != nil { + return nil, err + } + // The apply-time evaluation is kept even when empty: it means apply adds nothing, + // which differs from an absent evaluation, where the plan-time metadata applies. + if apply := source.GetApply(); apply != nil { + if apply.GetApply() != nil { + return nil, fmt.Errorf("%w: apply-time execution metadata cannot nest another", errUtils.ErrPluginProtocol) + } + if result.Apply, err = executionMetadata(apply); err != nil { + return nil, err + } + } + if result.IsEmpty() && result.Apply == nil { + return nil, nil + } + return result, nil +} + +// executionMetadata converts one evaluation of hooks and extra arguments and validates it. +func executionMetadata(source *pluginv1.ComponentExecutionConfiguration) (*schema.ComponentExecution, error) { result := &schema.ComponentExecution{} for _, item := range source.GetExtraArguments() { if item == nil { @@ -42,9 +65,6 @@ func preparedExecution(source *pluginv1.ComponentExecutionConfiguration) (*schem if err := execution.Validate(result); err != nil { return nil, err } - if result.IsEmpty() { - return nil, nil - } return result, nil } diff --git a/pkg/plugin/adapter/configuration/plan.go b/pkg/plugin/adapter/configuration/plan.go index 8ac8410c5bd..d29b4d7ac21 100644 --- a/pkg/plugin/adapter/configuration/plan.go +++ b/pkg/plugin/adapter/configuration/plan.go @@ -104,6 +104,9 @@ func digestSourceDirectory(ctx context.Context, root, sourcePath string, result if excludedPlanSource(entry.Name()) { return nil } + if err := rejectDirectorySymlink(root, path, entry); err != nil { + return err + } return digestSource(ctx, root, path, result) }) if os.IsNotExist(err) { @@ -113,6 +116,29 @@ func digestSourceDirectory(ctx context.Context, root, sourcePath string, result return err } +// rejectDirectorySymlink fails clearly on a symlink to a directory inside a watched directory. +// Atmos does not follow directory symlinks when it copies or binds sources, so the saved plan +// cannot fingerprint what the link leads to. File symlinks and dangling links are left to +// digestSource, which digests or records them. +func rejectDirectorySymlink(root, path string, entry fs.DirEntry) error { + if entry.Type()&fs.ModeSymlink == 0 { + return nil + } + info, err := os.Stat(path) + if err != nil || !info.IsDir() { + return nil //nolint:nilerr // Dangling links and file links are handled by digestSource. + } + shown, relErr := filepath.Rel(root, path) + if relErr != nil { + shown = path + } + shown = filepath.ToSlash(shown) + return errUtils.Build(fmt.Errorf("%w: watched source %s is a symlink to a directory", errUtils.ErrPluginInvalidConfig, shown)). + WithContext("source", shown). + WithHint("Atmos does not follow directory symlinks in watched sources. Replace the link with a copy of the directory, or watch its target directly"). + Err() +} + // excludedPlanSource skips mutable data, saved plans, and their bindings. Ordinary *.atmos.json // configuration, such as settings.atmos.json, is a source and is digested. func excludedPlanSource(name string) bool { diff --git a/pkg/plugin/adapter/configuration/plan_test.go b/pkg/plugin/adapter/configuration/plan_test.go index 75eeb7816e4..d9135c7f416 100644 --- a/pkg/plugin/adapter/configuration/plan_test.go +++ b/pkg/plugin/adapter/configuration/plan_test.go @@ -246,6 +246,30 @@ func TestSourceDigests(t *testing.T) { require.NoError(t, err) require.NotEqual(t, before["link.hcl"], after["link.hcl"], "edits behind a symlink must change the digest") }) + t.Run("directory symlink inside a watched directory is rejected, not followed", func(t *testing.T) { + root := t.TempDir() + require.NoError(t, os.MkdirAll(filepath.Join(root, "shared"), 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(root, "shared", "inputs.hcl"), []byte("x"), 0o600)) + require.NoError(t, os.Mkdir(filepath.Join(root, "unit"), 0o700)) + require.NoError(t, os.WriteFile(filepath.Join(root, "unit", "main.tf"), []byte("m"), 0o600)) + if err := os.Symlink(filepath.Join(root, "shared"), filepath.Join(root, "unit", "shared")); err != nil { + t.Skipf("symlinks unavailable: %v", err) + } + _, err := sourceDigests(t.Context(), root, []schema.ComponentConfigurationSource{{Path: "unit", Directory: true}}) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) + require.Contains(t, err.Error(), "unit/shared") + }) + t.Run("file symlinks inside a watched directory are still digested", func(t *testing.T) { + root := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(root, "shared.hcl"), []byte("one"), 0o600)) + require.NoError(t, os.Mkdir(filepath.Join(root, "unit"), 0o700)) + if err := os.Symlink(filepath.Join(root, "shared.hcl"), filepath.Join(root, "unit", "link.hcl")); err != nil { + t.Skipf("symlinks unavailable: %v", err) + } + got, err := sourceDigests(t.Context(), root, []schema.ComponentConfigurationSource{{Path: "unit", Directory: true}}) + require.NoError(t, err) + require.Contains(t, got, "unit/link.hcl") + }) t.Run("missing sources are recorded as absent", func(t *testing.T) { got, err := sourceDigests(t.Context(), t.TempDir(), []schema.ComponentConfigurationSource{{Path: "missing.hcl"}, {Path: "nodir", Directory: true}}) require.NoError(t, err) diff --git a/pkg/plugin/adapter/configuration/preparation_execution_test.go b/pkg/plugin/adapter/configuration/preparation_execution_test.go index cde83005fda..3da72d46fcb 100644 --- a/pkg/plugin/adapter/configuration/preparation_execution_test.go +++ b/pkg/plugin/adapter/configuration/preparation_execution_test.go @@ -92,6 +92,8 @@ func TestPreparedConfigurationRejectsUnrunnableHooks(t *testing.T) { {"no command", &pluginv1.ComponentExecutionConfiguration{BeforeHooks: []*pluginv1.ComponentHook{{Name: "h", Commands: []string{"plan"}}}}}, {"bad pattern", &pluginv1.ComponentExecutionConfiguration{ErrorHooks: []*pluginv1.ComponentHook{{Name: "h", Commands: []string{"plan"}, Execute: []string{"t"}, OnErrors: []string{"("}}}}}, {"nil hook", &pluginv1.ComponentExecutionConfiguration{AfterHooks: []*pluginv1.ComponentHook{nil}}}, + {"unrunnable apply hook", &pluginv1.ComponentExecutionConfiguration{Apply: &pluginv1.ComponentExecutionConfiguration{AfterHooks: []*pluginv1.ComponentHook{{Name: "h", Commands: []string{"apply"}}}}}}, + {"nested apply", &pluginv1.ComponentExecutionConfiguration{Apply: &pluginv1.ComponentExecutionConfiguration{Apply: &pluginv1.ComponentExecutionConfiguration{}}}}, } { t.Run(test.name, func(t *testing.T) { _, err := preparedConfiguration(executionResponse(t, test.execution)) @@ -99,3 +101,34 @@ func TestPreparedConfigurationRejectsUnrunnableHooks(t *testing.T) { }) } } + +// A plan preparation carries the apply-time evaluation separately. An empty one is kept, +// because it means apply runs nothing rather than falling back to the plan-time hooks. +func TestPreparedConfigurationCarriesApplyTimeExecution(t *testing.T) { + got, err := preparedConfiguration(roundTrip(t, executionResponse(t, &pluginv1.ComponentExecutionConfiguration{ + BeforeHooks: []*pluginv1.ComponentHook{{Name: "plan_only", Commands: []string{"plan"}, Execute: []string{"tool", "plan"}}}, + Apply: &pluginv1.ComponentExecutionConfiguration{ + AfterHooks: []*pluginv1.ComponentHook{{Name: "notify", Commands: []string{"apply"}, Execute: []string{"tool", "notify"}}}, + }, + }))) + require.NoError(t, err) + require.Equal(t, []schema.ComponentHook{{Name: "plan_only", Commands: []string{"plan"}, Execute: []string{"tool", "plan"}}}, got.Execution.BeforeHooks) + require.Equal(t, &schema.ComponentExecution{ + AfterHooks: []schema.ComponentHook{{Name: "notify", Commands: []string{"apply"}, Execute: []string{"tool", "notify"}}}, + }, got.Execution.Apply) + require.Same(t, got.Execution.Apply, got.Execution.ForSavedPlanApply()) + + // Apply adds nothing, while plan adds nothing either: the empty evaluation still wins. + got, err = preparedConfiguration(roundTrip(t, executionResponse(t, &pluginv1.ComponentExecutionConfiguration{Apply: &pluginv1.ComponentExecutionConfiguration{}}))) + require.NoError(t, err) + require.NotNil(t, got.Execution) + require.True(t, got.Execution.IsEmpty()) + require.Equal(t, &schema.ComponentExecution{}, got.Execution.ForSavedPlanApply()) + + // Without an apply-time evaluation, a saved plan applies the plan-time metadata. + got, err = preparedConfiguration(roundTrip(t, executionResponse(t, &pluginv1.ComponentExecutionConfiguration{ + AfterHooks: []*pluginv1.ComponentHook{{Name: "always", Commands: []string{"apply"}, Execute: []string{"tool"}}}, + }))) + require.NoError(t, err) + require.Same(t, got.Execution, got.Execution.ForSavedPlanApply()) +} diff --git a/pkg/plugin/adapter/configuration/preparation_test.go b/pkg/plugin/adapter/configuration/preparation_test.go index e7da8bbff29..0c56a5a5466 100644 --- a/pkg/plugin/adapter/configuration/preparation_test.go +++ b/pkg/plugin/adapter/configuration/preparation_test.go @@ -19,7 +19,8 @@ func preparationSession(t *testing.T) (*Session, *MockProvider, *schema.ConfigAn t.Helper() provider := NewMockProvider(gomock.NewController(t)) provider.EXPECT().ComponentType().Return("terraform").AnyTimes() - provider.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{unit(t, "app")}}, nil).MaxTimes(1) + // The session discovers lazily, and Prepare rediscovers to revalidate dependencies. + provider.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{unit(t, "app")}}, nil).AnyTimes() session, err := New(t.Context(), enabledConfig(t), "apply", func(context.Context, []string) (map[string]Provider, error) { return map[string]Provider{"terragrunt": provider}, nil }) @@ -64,6 +65,28 @@ func TestPrepareTranslatesGeneratedArtifactsAndWatchedSources(t *testing.T) { require.Equal(t, "-var=a=1", sent.Operation.TerraformArguments[0], "the request owns a copy of the arguments") } +func TestPrepareRejectsDependenciesChangedSinceDiscovery(t *testing.T) { + provider := NewMockProvider(gomock.NewController(t)) + provider.EXPECT().ComponentType().Return("terraform").AnyTimes() + network, app := unit(t, "network"), unit(t, "app") + moved := unit(t, "app") + moved.Dependencies = []*pluginv1.ComponentDependency{{Target: network.Address, Kind: pluginv1.ComponentDependency_OUTPUT}} + gomock.InOrder( + provider.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{network, app}}, nil), + provider.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{network, moved}}, nil), + ) + session, err := New(t.Context(), enabledConfig(t), "apply", func(context.Context, []string) (map[string]Provider, error) { + return map[string]Provider{"terragrunt": provider}, nil + }) + require.NoError(t, err) + info := &schema.ConfigAndStacksInfo{Stack: "live", ComponentFromArg: "app", SubCommand: "apply", ComponentSettingsSection: map[string]any{"configuration": true}} + + prepared, err := session.Prepare(t.Context(), info) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) + require.ErrorContains(t, err, "preparation changed discovered dependency paths") + require.Nil(t, prepared, "Prepare must not run when dependencies changed") +} + func TestPrepareFailsClosed(t *testing.T) { failure := errors.New("provider crashed") for _, test := range []struct { diff --git a/pkg/plugin/adapter/configuration/toolchain_boundary_test.go b/pkg/plugin/adapter/configuration/toolchain_boundary_test.go index 45450f4d70e..69b60a9cd7d 100644 --- a/pkg/plugin/adapter/configuration/toolchain_boundary_test.go +++ b/pkg/plugin/adapter/configuration/toolchain_boundary_test.go @@ -24,7 +24,8 @@ func preparedRequest(t *testing.T, info *schema.ConfigAndStacksInfo) *pluginv1.C provider := NewMockProvider(gomock.NewController(t)) provider.EXPECT().ComponentType().Return("terraform").AnyTimes() component := unit(t, "app") - provider.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{component}}, nil) + // The session discovers once; each Prepare rediscovers to revalidate dependencies. + provider.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{component}}, nil).MinTimes(1) inputs, err := adapter.EncodeObject(map[string]any{}) require.NoError(t, err) backend, err := adapter.EncodeObject(map[string]any{"path": "state.tfstate"}) diff --git a/pkg/plugin/proto/atmos/plugin/v1/plugin.pb.go b/pkg/plugin/proto/atmos/plugin/v1/plugin.pb.go index 5228242498d..3a6e7fbce94 100644 --- a/pkg/plugin/proto/atmos/plugin/v1/plugin.pb.go +++ b/pkg/plugin/proto/atmos/plugin/v1/plugin.pb.go @@ -2821,8 +2821,14 @@ type ComponentExecutionConfiguration struct { BeforeHooks []*ComponentHook `protobuf:"bytes,2,rep,name=before_hooks,json=beforeHooks,proto3" json:"before_hooks,omitempty"` AfterHooks []*ComponentHook `protobuf:"bytes,3,rep,name=after_hooks,json=afterHooks,proto3" json:"after_hooks,omitempty"` ErrorHooks []*ComponentHook `protobuf:"bytes,4,rep,name=error_hooks,json=errorHooks,proto3" json:"error_hooks,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache + // Metadata evaluated as the apply command would see it. Providers set it when + // preparing a plan whose execution depends on the command, so a saved plan + // applied later runs the apply-time hooks recorded with the reviewed plan. + // Unset means the plan-time metadata applies unchanged; an empty message + // means apply adds nothing. + Apply *ComponentExecutionConfiguration `protobuf:"bytes,5,opt,name=apply,proto3" json:"apply,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache } func (x *ComponentExecutionConfiguration) Reset() { @@ -2883,6 +2889,13 @@ func (x *ComponentExecutionConfiguration) GetErrorHooks() []*ComponentHook { return nil } +func (x *ComponentExecutionConfiguration) GetApply() *ComponentExecutionConfiguration { + if x != nil { + return x.Apply + } + return nil +} + type ComponentExtraArguments struct { state protoimpl.MessageState `protogen:"open.v1"` Commands []string `protobuf:"bytes,1,rep,name=commands,proto3" json:"commands,omitempty"` @@ -3475,14 +3488,15 @@ const file_atmos_plugin_v1_plugin_proto_rawDesc = "" + "\x04path\x18\x01 \x01(\tR\x04path\x12\x18\n" + "\acontent\x18\x02 \x01(\fR\acontent\x12\x1b\n" + "\tif_exists\x18\x03 \x01(\tR\bifExists\x12\x1c\n" + - "\tsensitive\x18\x04 \x01(\bR\tsensitive\"\xb9\x02\n" + + "\tsensitive\x18\x04 \x01(\bR\tsensitive\"\x81\x03\n" + "\x1fComponentExecutionConfiguration\x12Q\n" + "\x0fextra_arguments\x18\x01 \x03(\v2(.atmos.plugin.v1.ComponentExtraArgumentsR\x0eextraArguments\x12A\n" + "\fbefore_hooks\x18\x02 \x03(\v2\x1e.atmos.plugin.v1.ComponentHookR\vbeforeHooks\x12?\n" + "\vafter_hooks\x18\x03 \x03(\v2\x1e.atmos.plugin.v1.ComponentHookR\n" + "afterHooks\x12?\n" + "\verror_hooks\x18\x04 \x03(\v2\x1e.atmos.plugin.v1.ComponentHookR\n" + - "errorHooks\"\xcc\x02\n" + + "errorHooks\x12F\n" + + "\x05apply\x18\x05 \x01(\v20.atmos.plugin.v1.ComponentExecutionConfigurationR\x05apply\"\xcc\x02\n" + "\x17ComponentExtraArguments\x12\x1a\n" + "\bcommands\x18\x01 \x03(\tR\bcommands\x12\x1c\n" + "\targuments\x18\x02 \x03(\tR\targuments\x12,\n" + @@ -3687,63 +3701,64 @@ var file_atmos_plugin_v1_plugin_proto_depIdxs = []int32{ 44, // 59: atmos.plugin.v1.ComponentExecutionConfiguration.before_hooks:type_name -> atmos.plugin.v1.ComponentHook 44, // 60: atmos.plugin.v1.ComponentExecutionConfiguration.after_hooks:type_name -> atmos.plugin.v1.ComponentHook 44, // 61: atmos.plugin.v1.ComponentExecutionConfiguration.error_hooks:type_name -> atmos.plugin.v1.ComponentHook - 54, // 62: atmos.plugin.v1.ComponentExtraArguments.environment:type_name -> atmos.plugin.v1.ComponentExtraArguments.EnvironmentEntry - 28, // 63: atmos.plugin.v1.PrepareComponentResponse.inputs:type_name -> atmos.plugin.v1.ComponentValue - 40, // 64: atmos.plugin.v1.PrepareComponentResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity - 41, // 65: atmos.plugin.v1.PrepareComponentResponse.files:type_name -> atmos.plugin.v1.ComponentGeneratedFile - 33, // 66: atmos.plugin.v1.PrepareComponentResponse.dependencies:type_name -> atmos.plugin.v1.ComponentDependency - 32, // 67: atmos.plugin.v1.PrepareComponentResponse.sources:type_name -> atmos.plugin.v1.ComponentSource - 14, // 68: atmos.plugin.v1.PrepareComponentResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic - 42, // 69: atmos.plugin.v1.PrepareComponentResponse.execution:type_name -> atmos.plugin.v1.ComponentExecutionConfiguration - 40, // 70: atmos.plugin.v1.ResolveComponentStateResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity - 32, // 71: atmos.plugin.v1.ResolveComponentStateResponse.sources:type_name -> atmos.plugin.v1.ComponentSource - 14, // 72: atmos.plugin.v1.ResolveComponentStateResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic - 28, // 73: atmos.plugin.v1.ComponentOperation.OptionsEntry.value:type_name -> atmos.plugin.v1.ComponentValue - 6, // 74: atmos.plugin.v1.Info.Describe:input_type -> atmos.plugin.v1.DescribeRequest - 8, // 75: atmos.plugin.v1.Info.Configure:input_type -> atmos.plugin.v1.ConfigureRequest - 9, // 76: atmos.plugin.v1.AuthProvider.Validate:input_type -> atmos.plugin.v1.ProviderRequest - 9, // 77: atmos.plugin.v1.AuthProvider.Authenticate:input_type -> atmos.plugin.v1.ProviderRequest - 17, // 78: atmos.plugin.v1.AuthProvider.Refresh:input_type -> atmos.plugin.v1.RefreshRequest - 9, // 79: atmos.plugin.v1.AuthProvider.Logout:input_type -> atmos.plugin.v1.ProviderRequest - 9, // 80: atmos.plugin.v1.AuthProvider.Environment:input_type -> atmos.plugin.v1.ProviderRequest - 9, // 81: atmos.plugin.v1.AuthProvider.Paths:input_type -> atmos.plugin.v1.ProviderRequest - 20, // 82: atmos.plugin.v1.AuthProvider.PrepareEnvironment:input_type -> atmos.plugin.v1.PrepareEnvironmentRequest - 10, // 83: atmos.plugin.v1.AuthIdentity.Validate:input_type -> atmos.plugin.v1.IdentityRequest - 23, // 84: atmos.plugin.v1.AuthIdentity.Authenticate:input_type -> atmos.plugin.v1.IdentityAuthenticateRequest - 10, // 85: atmos.plugin.v1.AuthIdentity.Logout:input_type -> atmos.plugin.v1.IdentityRequest - 10, // 86: atmos.plugin.v1.AuthIdentity.Environment:input_type -> atmos.plugin.v1.IdentityRequest - 10, // 87: atmos.plugin.v1.AuthIdentity.Paths:input_type -> atmos.plugin.v1.IdentityRequest - 24, // 88: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:input_type -> atmos.plugin.v1.IdentityPrepareRequest - 25, // 89: atmos.plugin.v1.Commands.Run:input_type -> atmos.plugin.v1.CommandRequest - 37, // 90: atmos.plugin.v1.ComponentDiscovery.Discover:input_type -> atmos.plugin.v1.DiscoverComponentsRequest - 39, // 91: atmos.plugin.v1.ComponentValidation.Validate:input_type -> atmos.plugin.v1.ComponentRequest - 39, // 92: atmos.plugin.v1.ComponentPreparation.Prepare:input_type -> atmos.plugin.v1.ComponentRequest - 39, // 93: atmos.plugin.v1.ComponentState.Resolve:input_type -> atmos.plugin.v1.ComponentRequest - 7, // 94: atmos.plugin.v1.Info.Describe:output_type -> atmos.plugin.v1.DescribeResponse - 57, // 95: atmos.plugin.v1.Info.Configure:output_type -> google.protobuf.Empty - 15, // 96: atmos.plugin.v1.AuthProvider.Validate:output_type -> atmos.plugin.v1.ValidateResponse - 13, // 97: atmos.plugin.v1.AuthProvider.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent - 18, // 98: atmos.plugin.v1.AuthProvider.Refresh:output_type -> atmos.plugin.v1.RefreshResponse - 57, // 99: atmos.plugin.v1.AuthProvider.Logout:output_type -> google.protobuf.Empty - 19, // 100: atmos.plugin.v1.AuthProvider.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse - 22, // 101: atmos.plugin.v1.AuthProvider.Paths:output_type -> atmos.plugin.v1.PathsResponse - 19, // 102: atmos.plugin.v1.AuthProvider.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse - 15, // 103: atmos.plugin.v1.AuthIdentity.Validate:output_type -> atmos.plugin.v1.ValidateResponse - 13, // 104: atmos.plugin.v1.AuthIdentity.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent - 57, // 105: atmos.plugin.v1.AuthIdentity.Logout:output_type -> google.protobuf.Empty - 19, // 106: atmos.plugin.v1.AuthIdentity.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse - 22, // 107: atmos.plugin.v1.AuthIdentity.Paths:output_type -> atmos.plugin.v1.PathsResponse - 19, // 108: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse - 27, // 109: atmos.plugin.v1.Commands.Run:output_type -> atmos.plugin.v1.CommandEvent - 38, // 110: atmos.plugin.v1.ComponentDiscovery.Discover:output_type -> atmos.plugin.v1.DiscoverComponentsResponse - 15, // 111: atmos.plugin.v1.ComponentValidation.Validate:output_type -> atmos.plugin.v1.ValidateResponse - 45, // 112: atmos.plugin.v1.ComponentPreparation.Prepare:output_type -> atmos.plugin.v1.PrepareComponentResponse - 46, // 113: atmos.plugin.v1.ComponentState.Resolve:output_type -> atmos.plugin.v1.ResolveComponentStateResponse - 94, // [94:114] is the sub-list for method output_type - 74, // [74:94] is the sub-list for method input_type - 74, // [74:74] is the sub-list for extension type_name - 74, // [74:74] is the sub-list for extension extendee - 0, // [0:74] is the sub-list for field type_name + 42, // 62: atmos.plugin.v1.ComponentExecutionConfiguration.apply:type_name -> atmos.plugin.v1.ComponentExecutionConfiguration + 54, // 63: atmos.plugin.v1.ComponentExtraArguments.environment:type_name -> atmos.plugin.v1.ComponentExtraArguments.EnvironmentEntry + 28, // 64: atmos.plugin.v1.PrepareComponentResponse.inputs:type_name -> atmos.plugin.v1.ComponentValue + 40, // 65: atmos.plugin.v1.PrepareComponentResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity + 41, // 66: atmos.plugin.v1.PrepareComponentResponse.files:type_name -> atmos.plugin.v1.ComponentGeneratedFile + 33, // 67: atmos.plugin.v1.PrepareComponentResponse.dependencies:type_name -> atmos.plugin.v1.ComponentDependency + 32, // 68: atmos.plugin.v1.PrepareComponentResponse.sources:type_name -> atmos.plugin.v1.ComponentSource + 14, // 69: atmos.plugin.v1.PrepareComponentResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic + 42, // 70: atmos.plugin.v1.PrepareComponentResponse.execution:type_name -> atmos.plugin.v1.ComponentExecutionConfiguration + 40, // 71: atmos.plugin.v1.ResolveComponentStateResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity + 32, // 72: atmos.plugin.v1.ResolveComponentStateResponse.sources:type_name -> atmos.plugin.v1.ComponentSource + 14, // 73: atmos.plugin.v1.ResolveComponentStateResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic + 28, // 74: atmos.plugin.v1.ComponentOperation.OptionsEntry.value:type_name -> atmos.plugin.v1.ComponentValue + 6, // 75: atmos.plugin.v1.Info.Describe:input_type -> atmos.plugin.v1.DescribeRequest + 8, // 76: atmos.plugin.v1.Info.Configure:input_type -> atmos.plugin.v1.ConfigureRequest + 9, // 77: atmos.plugin.v1.AuthProvider.Validate:input_type -> atmos.plugin.v1.ProviderRequest + 9, // 78: atmos.plugin.v1.AuthProvider.Authenticate:input_type -> atmos.plugin.v1.ProviderRequest + 17, // 79: atmos.plugin.v1.AuthProvider.Refresh:input_type -> atmos.plugin.v1.RefreshRequest + 9, // 80: atmos.plugin.v1.AuthProvider.Logout:input_type -> atmos.plugin.v1.ProviderRequest + 9, // 81: atmos.plugin.v1.AuthProvider.Environment:input_type -> atmos.plugin.v1.ProviderRequest + 9, // 82: atmos.plugin.v1.AuthProvider.Paths:input_type -> atmos.plugin.v1.ProviderRequest + 20, // 83: atmos.plugin.v1.AuthProvider.PrepareEnvironment:input_type -> atmos.plugin.v1.PrepareEnvironmentRequest + 10, // 84: atmos.plugin.v1.AuthIdentity.Validate:input_type -> atmos.plugin.v1.IdentityRequest + 23, // 85: atmos.plugin.v1.AuthIdentity.Authenticate:input_type -> atmos.plugin.v1.IdentityAuthenticateRequest + 10, // 86: atmos.plugin.v1.AuthIdentity.Logout:input_type -> atmos.plugin.v1.IdentityRequest + 10, // 87: atmos.plugin.v1.AuthIdentity.Environment:input_type -> atmos.plugin.v1.IdentityRequest + 10, // 88: atmos.plugin.v1.AuthIdentity.Paths:input_type -> atmos.plugin.v1.IdentityRequest + 24, // 89: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:input_type -> atmos.plugin.v1.IdentityPrepareRequest + 25, // 90: atmos.plugin.v1.Commands.Run:input_type -> atmos.plugin.v1.CommandRequest + 37, // 91: atmos.plugin.v1.ComponentDiscovery.Discover:input_type -> atmos.plugin.v1.DiscoverComponentsRequest + 39, // 92: atmos.plugin.v1.ComponentValidation.Validate:input_type -> atmos.plugin.v1.ComponentRequest + 39, // 93: atmos.plugin.v1.ComponentPreparation.Prepare:input_type -> atmos.plugin.v1.ComponentRequest + 39, // 94: atmos.plugin.v1.ComponentState.Resolve:input_type -> atmos.plugin.v1.ComponentRequest + 7, // 95: atmos.plugin.v1.Info.Describe:output_type -> atmos.plugin.v1.DescribeResponse + 57, // 96: atmos.plugin.v1.Info.Configure:output_type -> google.protobuf.Empty + 15, // 97: atmos.plugin.v1.AuthProvider.Validate:output_type -> atmos.plugin.v1.ValidateResponse + 13, // 98: atmos.plugin.v1.AuthProvider.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent + 18, // 99: atmos.plugin.v1.AuthProvider.Refresh:output_type -> atmos.plugin.v1.RefreshResponse + 57, // 100: atmos.plugin.v1.AuthProvider.Logout:output_type -> google.protobuf.Empty + 19, // 101: atmos.plugin.v1.AuthProvider.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse + 22, // 102: atmos.plugin.v1.AuthProvider.Paths:output_type -> atmos.plugin.v1.PathsResponse + 19, // 103: atmos.plugin.v1.AuthProvider.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse + 15, // 104: atmos.plugin.v1.AuthIdentity.Validate:output_type -> atmos.plugin.v1.ValidateResponse + 13, // 105: atmos.plugin.v1.AuthIdentity.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent + 57, // 106: atmos.plugin.v1.AuthIdentity.Logout:output_type -> google.protobuf.Empty + 19, // 107: atmos.plugin.v1.AuthIdentity.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse + 22, // 108: atmos.plugin.v1.AuthIdentity.Paths:output_type -> atmos.plugin.v1.PathsResponse + 19, // 109: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse + 27, // 110: atmos.plugin.v1.Commands.Run:output_type -> atmos.plugin.v1.CommandEvent + 38, // 111: atmos.plugin.v1.ComponentDiscovery.Discover:output_type -> atmos.plugin.v1.DiscoverComponentsResponse + 15, // 112: atmos.plugin.v1.ComponentValidation.Validate:output_type -> atmos.plugin.v1.ValidateResponse + 45, // 113: atmos.plugin.v1.ComponentPreparation.Prepare:output_type -> atmos.plugin.v1.PrepareComponentResponse + 46, // 114: atmos.plugin.v1.ComponentState.Resolve:output_type -> atmos.plugin.v1.ResolveComponentStateResponse + 95, // [95:115] is the sub-list for method output_type + 75, // [75:95] is the sub-list for method input_type + 75, // [75:75] is the sub-list for extension type_name + 75, // [75:75] is the sub-list for extension extendee + 0, // [0:75] is the sub-list for field type_name } func init() { file_atmos_plugin_v1_plugin_proto_init() } diff --git a/pkg/schema/component_execution.go b/pkg/schema/component_execution.go index a61c9b42899..65716eed1ee 100644 --- a/pkg/schema/component_execution.go +++ b/pkg/schema/component_execution.go @@ -8,6 +8,10 @@ type ComponentExecution struct { BeforeHooks []ComponentHook AfterHooks []ComponentHook ErrorHooks []ComponentHook + // Apply is the metadata the provider evaluated for the apply command while preparing a + // plan. A saved plan applies it instead of the plan-time metadata, as Terragrunt would + // evaluate the configuration again for apply. Nil means the plan-time metadata applies. + Apply *ComponentExecution } // ComponentExtraArguments adds arguments, variable files, and environment to the listed commands. @@ -33,6 +37,15 @@ type ComponentHook struct { OnErrors []string } +// ForSavedPlanApply returns the metadata to run when applying a saved plan prepared with e. +// It is the apply-time evaluation when the provider recorded one, and e otherwise. +func (e *ComponentExecution) ForSavedPlanApply() *ComponentExecution { + if e == nil || e.Apply == nil { + return e + } + return e.Apply +} + // IsEmpty reports whether the execution adds nothing to a command. func (e *ComponentExecution) IsEmpty() bool { return e == nil || len(e.ExtraArguments)+len(e.BeforeHooks)+len(e.AfterHooks)+len(e.ErrorHooks) == 0 diff --git a/pkg/terraform/output/config.go b/pkg/terraform/output/config.go index 5902a82dd05..d51e48ee858 100644 --- a/pkg/terraform/output/config.go +++ b/pkg/terraform/output/config.go @@ -107,6 +107,22 @@ func isComponentEnabled(varsSection map[string]any) bool { // extractRequiredFields extracts required fields from sections into config. func extractRequiredFields(atmosConfig *schema.AtmosConfiguration, sections map[string]any, component, stack string, config *ComponentConfig) error { + if err := extractCommandFields(sections, component, stack, config); err != nil { + return err + } + + // Extract component_path (required). + componentPath, err := extractComponentPath(atmosConfig, sections, component, stack) + if err != nil { + return err + } + config.ComponentPath = componentPath + + return nil +} + +// extractCommandFields extracts the executable and workspace every output read requires. +func extractCommandFields(sections map[string]any, component, stack string, config *ComponentConfig) error { // Extract executable (required). executable, ok := sections[cfg.CommandSectionName].(string) if !ok { @@ -125,13 +141,6 @@ func extractRequiredFields(atmosConfig *schema.AtmosConfiguration, sections map[ } config.Workspace = workspace - // Extract component_path (required). - componentPath, err := extractComponentPath(atmosConfig, sections, component, stack) - if err != nil { - return err - } - config.ComponentPath = componentPath - return nil } @@ -251,15 +260,7 @@ func extractOptionalFields(sections map[string]any, config *ComponentConfig) { func ExtractComponentConfig(atmosConfig *schema.AtmosConfiguration, sections map[string]any, component, stack string) (*ComponentConfig, error) { defer perf.Track(atmosConfig, "output.ExtractComponentConfig")() - config := &ComponentConfig{ - AutoGenerateBackend: atmosConfig.Components.Terraform.AutoGenerateBackendFile, - InitMode: atmosConfig.Components.Terraform.EffectiveInitMode(), - InitReconfigure: atmosConfig.Components.Terraform.EffectiveInitReconfigure(), - InitUpgrade: atmosConfig.Components.Terraform.EffectiveInitUpgrade(), - AutoProvisionWorkdirForOutputs: atmosConfig.Components.Terraform.AutoProvisionWorkdirForOutputs, - PassVars: atmosConfig.Components.Terraform.Init.PassVars, - } - + config := newComponentConfig(atmosConfig) if err := extractRequiredFields(atmosConfig, sections, component, stack, config); err != nil { return nil, err } @@ -269,6 +270,30 @@ func ExtractComponentConfig(atmosConfig *schema.AtmosConfiguration, sections map return config, nil } +// extractConfigurationStateConfig extracts what a plugin-owned state read needs. That read runs +// in a scratch or the unit's own initialized directory, never in the component's execution +// workdir, so the workdir path is not resolved and its configuration cannot fail the read. +func extractConfigurationStateConfig(atmosConfig *schema.AtmosConfiguration, sections map[string]any, component, stack string) (*ComponentConfig, error) { + config := newComponentConfig(atmosConfig) + if err := extractCommandFields(sections, component, stack, config); err != nil { + return nil, err + } + extractOptionalFields(sections, config) + return config, nil +} + +// newComponentConfig applies the project-wide output defaults. +func newComponentConfig(atmosConfig *schema.AtmosConfiguration) *ComponentConfig { + return &ComponentConfig{ + AutoGenerateBackend: atmosConfig.Components.Terraform.AutoGenerateBackendFile, + InitMode: atmosConfig.Components.Terraform.EffectiveInitMode(), + InitReconfigure: atmosConfig.Components.Terraform.EffectiveInitReconfigure(), + InitUpgrade: atmosConfig.Components.Terraform.EffectiveInitUpgrade(), + AutoProvisionWorkdirForOutputs: atmosConfig.Components.Terraform.AutoProvisionWorkdirForOutputs, + PassVars: atmosConfig.Components.Terraform.Init.PassVars, + } +} + // ValidateBackendConfig validates that backend configuration is complete for backend generation. func ValidateBackendConfig(config *ComponentConfig, component, stack string) error { defer perf.Track(nil, "output.ValidateBackendConfig")() diff --git a/pkg/terraform/output/configuration_state.go b/pkg/terraform/output/configuration_state.go index 688de480650..6114a545120 100644 --- a/pkg/terraform/output/configuration_state.go +++ b/pkg/terraform/output/configuration_state.go @@ -6,6 +6,7 @@ import ( "maps" "os" "path/filepath" + "reflect" "slices" "strings" @@ -90,9 +91,44 @@ func ConfigurationStateSections(sections map[string]any, state *schema.Component delete(result, "provision") delete(result, "remote_state_backend") delete(result, "remote_state_backend_type") + if remoteType, remote := explicitRemoteStateBackend(sections, state.BackendType, backend); remoteType != "" { + result["remote_state_backend_type"], result["remote_state_backend"] = remoteType, remote + } return result } +// explicitRemoteStateBackend keeps the remote-state read settings the stack configured, applied to +// the plugin's backend. Atmos stores remote_state_backend as the component's backend with those +// settings layered on top. For a plugin-owned component that backend is a discovery placeholder, +// so only what differs from it was configured explicitly. A static remote state is kept as is. +// The plugin's backend stays the fallback for everything the stack did not override. +func explicitRemoteStateBackend(sections map[string]any, pluginType string, pluginBackend map[string]any) (string, map[string]any) { + remoteType, _ := sections["remote_state_backend_type"].(string) + remote, _ := sections["remote_state_backend"].(map[string]any) + if remoteType == "static" { + return remoteType, remote + } + placeholderType, _ := sections["backend_type"].(string) + overrides := remoteStateOverrides(remote, remoteType, placeholderType, sections) + explicitType := remoteType != "" && remoteType != placeholderType + if !explicitType && len(overrides) == 0 { + return "", nil + } + if !explicitType { + remoteType = pluginType + } + if remoteType != pluginType { + // A different backend type is a separate read location; it is not layered on the plugin's. + return remoteType, overrides + } + merged := maps.Clone(pluginBackend) + if merged == nil { + merged = make(map[string]any, len(overrides)) + } + maps.Copy(merged, overrides) + return remoteType, merged +} + // configurationOutputConfig uses a disposable backend-only directory. Output reads must not // regenerate a unit, fetch its module, invoke hooks, or overwrite its existing execution directory. func configurationOutputConfig(config *ComponentConfig, state *schema.ComponentConfigurationState) (func(), error) { @@ -127,6 +163,63 @@ func configurationOutputConfig(config *ComponentConfig, state *schema.ComponentC return func() { _ = os.RemoveAll(root) }, nil } +// configurationInitializedOutputConfig reads outputs from the unit's own working directory when +// the caller skips init. Nothing is generated or initialized there: the directory is used exactly +// as the unit's last execution left it, so a directory that was never initialized fails clearly. +func configurationInitializedOutputConfig(config *ComponentConfig, state *schema.ComponentConfigurationState, component, stack string) error { + if state.WorkingDirectory == "" { + return errUtils.Build(errUtils.ErrTerraformOutputFailed). + WithExplanationf("Component '%s' in stack '%s' reported no working directory to read outputs from without init.", component, stack). + WithHint("Run the command without --skip-init so Atmos can initialize a temporary state reader"). + Err() + } + dataDir := ".terraform" + if configured, ok := config.Env["TF_DATA_DIR"].(string); ok && configured != "" { + dataDir = configured + } + if !filepath.IsAbs(dataDir) { + dataDir = filepath.Join(state.WorkingDirectory, dataDir) + } + if info, err := os.Stat(dataDir); err != nil || !info.IsDir() { + return errUtils.Build(errUtils.ErrTerraformOutputFailed). + WithExplanationf("Component '%s' in stack '%s' has not been initialized in %s.", component, stack, state.WorkingDirectory). + WithHint("Run the command without --skip-init so Atmos can initialize a temporary state reader"). + Err() + } + config.ComponentPath = state.WorkingDirectory + config.ReadOnlyState = true + config.Workspace = state.Workspace + config.AutoGenerateBackend = false + config.AutoProvisionWorkdirForOutputs = false + config.Providers, config.Vars = nil, nil + config.PassVars = false + return nil +} + +// remoteStateOverrides returns the remote-state settings that differ from the placeholder backend. +// A remote state of another type shares nothing with the placeholder, so all of it is explicit. +func remoteStateOverrides(remote map[string]any, remoteType, placeholderType string, sections map[string]any) map[string]any { + placeholder, _ := sections["backend"].(map[string]any) + overrides := make(map[string]any) + for key, value := range remote { + existing, ok := placeholder[key] + if remoteType != placeholderType || !ok || !reflect.DeepEqual(existing, value) { + overrides[key] = value + } + } + return overrides +} + +// configurationStateReader points config at the directory a plugin-owned state read runs in and +// returns its cleanup. Skipping init reads the unit's already-initialized directory as it is; +// otherwise a disposable backend-only scratch directory is initialized. +func configurationStateReader(config *ComponentConfig, state *schema.ComponentConfigurationState, opts *OutputOptions, component, stack string) (func(), error) { + if opts != nil && opts.SkipInit { + return func() {}, configurationInitializedOutputConfig(config, state, component, stack) + } + return configurationOutputConfig(config, state) +} + // configurationEnvironment retains an explicitly empty sanitized environment. func configurationEnvironment(managers []any) []string { for _, candidate := range managers { diff --git a/pkg/terraform/output/configuration_state_test.go b/pkg/terraform/output/configuration_state_test.go index ba047485de8..06227917cf7 100644 --- a/pkg/terraform/output/configuration_state_test.go +++ b/pkg/terraform/output/configuration_state_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "maps" "os" "path/filepath" "testing" @@ -123,7 +124,7 @@ func TestPluginOutputUsesBackendOnlyScratchDirectory(t *testing.T) { require.Equal(t, filepath.Join(stateRoot, "terraform.tfstate.d"), local["workspace_dir"]) return runner, nil })) - result, err := executor.execute(t.Context(), config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true) + result, err := executor.execute(t.Context(), config, "network", "live", sections, nil, &OutputOptions{}, true) if fail { require.ErrorContains(t, err, "workspace prod does not exist") } else { @@ -215,3 +216,138 @@ func TestConfigurationStateSectionsNamedWorkspaceWithoutBackendSettings(t *testi require.Equal(t, filepath.Join(root, "terraform.tfstate.d", "prod", "terraform.tfstate"), resolved["backend"].(map[string]any)["path"]) require.Nil(t, state.Backend) } + +// TestPluginOutputSkipInitReadsTheInitializedUnit verifies that skipping init is honored for a +// plugin-owned component: outputs are read from the unit's initialized directory as it is, with no +// init, no workspace selection, and no generated files. +func TestPluginOutputSkipInitReadsTheInitializedUnit(t *testing.T) { + ctrl := gomock.NewController(t) + runner := NewMockTerraformRunner(ctrl) + runner.EXPECT().SetEnv(gomock.Any()).Return(nil) + runner.EXPECT().Output(gomock.Any()).Return(map[string]tfexec.OutputMeta{"result": {Type: json.RawMessage(`"string"`), Value: json.RawMessage(`"applied"`)}}, nil) + config := validAtmosConfig(t) + unit := t.TempDir() + require.NoError(t, os.Mkdir(filepath.Join(unit, ".terraform"), 0o700)) + config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return &schema.ComponentConfigurationState{BackendType: "local", Workspace: "prod", WorkingDirectory: unit}, nil + }} + sections := validSections() + sections["settings"] = map[string]any{"configuration": true} + sections["providers"] = map[string]any{"aws": map[string]any{"region": "us-east-2"}} + binary, err := os.Executable() + require.NoError(t, err) + sections["command"] = binary + executor := NewExecutor(nil, WithRunnerFactory(func(dir, _ string) (TerraformRunner, error) { + require.Equal(t, unit, dir, "skipping init must read the unit's own initialized directory") + return runner, nil + })) + + result, err := executor.execute(t.Context(), config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true) + require.NoError(t, err) + require.Equal(t, "applied", result["result"]) + files, err := os.ReadDir(unit) + require.NoError(t, err) + require.Len(t, files, 1, "no backend, provider, or marker file may be written into the unit") + require.Equal(t, ".terraform", files[0].Name()) +} + +// TestPluginOutputSkipInitRejectsAnUninitializedUnit verifies that skipping init never falls back +// to initializing anything: a unit that was never initialized fails with a clear error. +func TestPluginOutputSkipInitRejectsAnUninitializedUnit(t *testing.T) { + for name, state := range map[string]*schema.ComponentConfigurationState{ + "no working directory": {BackendType: "local", Workspace: "prod"}, + "not initialized": {BackendType: "local", Workspace: "prod", WorkingDirectory: t.TempDir()}, + } { + t.Run(name, func(t *testing.T) { + config := validAtmosConfig(t) + config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return state, nil + }} + sections := validSections() + sections["settings"] = map[string]any{"configuration": true} + executor := NewExecutor(nil, WithRunnerFactory(func(string, string) (TerraformRunner, error) { + t.Fatal("no Terraform process may start") + return nil, nil + })) + _, err := executor.execute(t.Context(), config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true) + require.ErrorIs(t, err, errUtils.ErrTerraformOutputFailed) + }) + } +} + +// TestPluginOutputDoesNotResolveTheExecutionWorkdir verifies that a plugin-owned state read +// succeeds even when the component's execution workdir cannot be resolved. +func TestPluginOutputDoesNotResolveTheExecutionWorkdir(t *testing.T) { + ctrl := gomock.NewController(t) + runner := NewMockTerraformRunner(ctrl) + runner.EXPECT().SetEnv(gomock.Any()).Return(nil) + runner.EXPECT().Output(gomock.Any()).Return(map[string]tfexec.OutputMeta{"result": {Type: json.RawMessage(`"string"`), Value: json.RawMessage(`"applied"`)}}, nil) + config := validAtmosConfig(t) + unit := t.TempDir() + require.NoError(t, os.Mkdir(filepath.Join(unit, ".terraform"), 0o700)) + config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return &schema.ComponentConfigurationState{BackendType: "local", Workspace: "prod", WorkingDirectory: unit}, nil + }} + sections := validSections() + sections["settings"] = map[string]any{"configuration": true} + // No base component: an ordinary component could not resolve its execution path at all. + delete(sections, "component") + _, err := ExtractComponentConfig(config, sections, "network", "live") + require.ErrorIs(t, err, errUtils.ErrMissingComponentPath, "precondition: the execution path is unresolvable") + executor := NewExecutor(nil, WithRunnerFactory(func(string, string) (TerraformRunner, error) { return runner, nil })) + + result, err := executor.execute(t.Context(), config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true) + require.NoError(t, err) + require.Equal(t, "applied", result["result"]) +} + +// TestConfigurationStateSectionsKeepsExplicitRemoteStateOverrides verifies that remote-state read +// settings the stack configured survive for a plugin-owned component, layered on the plugin's +// backend, while the discovery placeholder backend never leaks into the read. +func TestConfigurationStateSectionsKeepsExplicitRemoteStateOverrides(t *testing.T) { + state := &schema.ComponentConfigurationState{BackendType: "s3", Backend: map[string]any{"bucket": "unit-state", "key": "live/network.tfstate", "region": "us-east-2"}, Workspace: "default"} + placeholder := map[string]any{"bucket": "placeholder", "region": "us-east-2"} + for _, test := range []struct { + name string + remoteType string + remote map[string]any + wantType string + want map[string]any + }{ + {name: "no override derives nothing", remoteType: "s3", remote: maps.Clone(placeholder)}, + { + name: "read role override is layered on the plugin backend", remoteType: "s3", + remote: map[string]any{"bucket": "placeholder", "region": "us-east-2", "role_arn": "arn:aws:iam::1:role/read"}, + wantType: "s3", want: map[string]any{"bucket": "unit-state", "key": "live/network.tfstate", "region": "us-east-2", "role_arn": "arn:aws:iam::1:role/read"}, + }, + { + name: "explicit separate location wins over the plugin backend", remoteType: "s3", + remote: map[string]any{"bucket": "mirror", "region": "us-east-2"}, + wantType: "s3", want: map[string]any{"bucket": "mirror", "key": "live/network.tfstate", "region": "us-east-2"}, + }, + { + name: "a different backend type is a separate read location", remoteType: "gcs", + remote: map[string]any{"bucket": "gcs-mirror", "prefix": "network"}, + wantType: "gcs", want: map[string]any{"bucket": "gcs-mirror", "prefix": "network"}, + }, + { + name: "static remote state is kept", remoteType: "static", + remote: map[string]any{"vpc_id": "vpc-1"}, + wantType: "static", want: map[string]any{"vpc_id": "vpc-1"}, + }, + } { + t.Run(test.name, func(t *testing.T) { + sections := map[string]any{"backend_type": "s3", "backend": maps.Clone(placeholder), "remote_state_backend_type": test.remoteType, "remote_state_backend": test.remote} + resolved := ConfigurationStateSections(sections, state) + require.Equal(t, state.Backend, resolved["backend"], "the plugin backend stays the primary location") + if test.wantType == "" { + require.NotContains(t, resolved, "remote_state_backend") + require.NotContains(t, resolved, "remote_state_backend_type") + return + } + require.Equal(t, test.wantType, resolved["remote_state_backend_type"]) + require.Equal(t, test.want, resolved["remote_state_backend"]) + require.Equal(t, map[string]any{"bucket": "unit-state", "key": "live/network.tfstate", "region": "us-east-2"}, state.Backend, "the plugin state must not be modified") + }) + } +} diff --git a/pkg/terraform/output/executor.go b/pkg/terraform/output/executor.go index b674848d24b..4f739a30fa4 100644 --- a/pkg/terraform/output/executor.go +++ b/pkg/terraform/output/executor.go @@ -485,30 +485,30 @@ func (e *Executor) execute( // ExtractComponentConfig uses utils.GetComponentPath internally to ensure // proper path resolution that works correctly with --chdir and when running // from non-project-root directories. - config, err := ExtractComponentConfig(atmosConfig, sections, component, stack) + // Plugin-owned state is located first: its reads never run in the component's execution + // workdir, so that workdir is neither resolved nor required. + state, err := ResolveConfigurationState(ctx, atmosConfig, &ConfigurationStateRequest{Sections: sections, Component: component, Stack: stack, AuthContext: authContext, AuthManagers: authManagers}) if err != nil { return nil, err } - - state, err := ResolveConfigurationState(ctx, atmosConfig, &ConfigurationStateRequest{Sections: sections, Component: component, Stack: stack, AuthContext: authContext, AuthManagers: authManagers}) + var config *ComponentConfig + if state != nil { + config, err = extractConfigurationStateConfig(atmosConfig, sections, component, stack) + } else { + config, err = ExtractComponentConfig(atmosConfig, sections, component, stack) + } if err != nil { return nil, err } if state != nil { config.BaseEnvironment = configurationEnvironment(authManagers) - cleanup, err := configurationOutputConfig(config, state) + cleanup, err := configurationStateReader(config, state, opts, component, stack) if err != nil { return nil, err } defer cleanup() - // The scratch directory always needs init, even after an earlier apply. - copy := OutputOptions{} - if opts != nil { - copy = *opts - } - copy.SkipInit = false - opts = © - processYamlFunctions = true + // Only the scratch reader regenerates its backend; the initialized unit is read as it is. + processYamlFunctions = opts == nil || !opts.SkipInit } // Step 2.5: Auto-provision JIT workdir if needed before toolchain resolution and init. diff --git a/pkg/terraform/output/executor_toolchain_test.go b/pkg/terraform/output/executor_toolchain_test.go index 09aacc58380..372bc06264b 100644 --- a/pkg/terraform/output/executor_toolchain_test.go +++ b/pkg/terraform/output/executor_toolchain_test.go @@ -206,7 +206,7 @@ func TestPluginOwnedOutputReadUsesIdentityEnvironmentAndToolchain(t *testing.T) sections[cfg.CommandSectionName] = "tofu" sections["dependencies"] = map[string]any{"tools": map[string]any{"tofu": "1.0.0", "fixture-tool": "1.0.0"}} - outputs, err := executor.execute(t.Context(), setup.config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true, manager) + outputs, err := executor.execute(t.Context(), setup.config, "network", "live", sections, nil, &OutputOptions{}, true, manager) require.NoError(t, err) require.Equal(t, "value", outputs["id"]) diff --git a/plugins/terragrunt/internal/engine/execution.go b/plugins/terragrunt/internal/engine/execution.go index 85cad98b24a..9dd59e3ac64 100644 --- a/plugins/terragrunt/internal/engine/execution.go +++ b/plugins/terragrunt/internal/engine/execution.go @@ -1,13 +1,16 @@ package engine import ( + "bytes" "errors" "fmt" "maps" + "reflect" "regexp" "slices" "github.com/gruntwork-io/terragrunt/pkg/config" + "github.com/spf13/afero" "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" ) @@ -55,6 +58,65 @@ func executionFromConfig(terraform *config.TerraformConfig) (*model.Execution, e return result, nil } +// commandFunctions are the Terragrunt functions whose value depends on the Terraform command. +var commandFunctions = [][]byte{[]byte("get_terraform_command"), []byte("get_terraform_cli_args")} + +// applyTimeExecution records how the apply command sees hooks and extra arguments when a +// plan is prepared. A saved plan is applied later from that preparation, while Terragrunt +// evaluates the configuration again for apply; without this, a hook enabled only for apply +// (for example `if = get_terraform_command() == "apply"`) would silently never run. +// Configuration that cannot observe the command evaluates identically, so it is not parsed twice. +func applyTimeExecution(request model.Request, path string, planned *model.Execution, filesystem afero.Fs, read []string, + parse func(*config.ParsingContext) (*config.TerragruntConfig, error), +) (*model.Execution, error) { + if request.Command != "plan" || !observesCommand(filesystem, append(slices.Clone(read), path)) { + return planned, nil + } + cfg, err := parse(parsingContext(path, request.Tofu, "apply", nil)) + if err != nil { + return nil, err + } + applied, err := executionFromConfig(cfg.Terraform) + if err != nil { + return nil, err + } + if applied == nil { + applied = &model.Execution{} + } + if planned == nil { + planned = &model.Execution{} + } + if reflect.DeepEqual(*applied, *planned) { + return nilIfEmpty(planned), nil + } + planned.Apply = applied + return planned, nil +} + +// observesCommand reports whether any configuration file the unit read can call a function +// that depends on the command. An unreadable file is assumed to, so apply is evaluated. +func observesCommand(filesystem afero.Fs, paths []string) bool { + for _, path := range paths { + contents, err := afero.ReadFile(filesystem, path) + if err != nil { + return true + } + for _, name := range commandFunctions { + if bytes.Contains(contents, name) { + return true + } + } + } + return false +} + +func nilIfEmpty(execution *model.Execution) *model.Execution { + if execution.Apply == nil && len(execution.ExtraArguments)+len(execution.BeforeHooks)+len(execution.AfterHooks)+len(execution.ErrorHooks) == 0 { + return nil + } + return execution +} + func extraArguments(source *config.TerraformExtraArguments) model.ExtraArguments { result := model.ExtraArguments{Commands: slices.Clone(source.Commands)} if source.Arguments != nil { diff --git a/plugins/terragrunt/internal/engine/execution_test.go b/plugins/terragrunt/internal/engine/execution_test.go index cee787d1d4e..88e36980858 100644 --- a/plugins/terragrunt/internal/engine/execution_test.go +++ b/plugins/terragrunt/internal/engine/execution_test.go @@ -6,6 +6,7 @@ import ( "path/filepath" "testing" + "github.com/spf13/afero" "github.com/stretchr/testify/require" "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" @@ -198,3 +199,90 @@ func TestValidateHookRequiresRunnableExecuteList(t *testing.T) { } require.NoError(t, validateHook("before_hook", &model.Hook{Name: "h", Commands: []string{"plan"}, Execute: []string{"tool"}})) } + +// A saved plan is applied from its plan-time preparation, but Terragrunt evaluates the +// configuration again for apply. Hooks that depend on the command are recorded for apply. +func TestPlanPreparationRecordsApplyTimeExecution(t *testing.T) { + body := ` + before_hook "plan_only" { + commands = ["plan", "apply"] + execute = ["tool", "plan"] + if = get_terraform_command() == "plan" + } + after_hook "notify" { + commands = ["apply"] + execute = ["tool", "notify"] + if = get_terraform_command() == "apply" + } + after_hook "always" { + commands = ["apply"] + execute = ["tool", "always"] + } +` + planned, err := Resolve(t.Context(), executionUnit(t, body)) + require.NoError(t, err) + require.Equal(t, []string{"plan_only"}, hookNames(planned.Execution.BeforeHooks)) + require.Equal(t, []string{"always"}, hookNames(planned.Execution.AfterHooks)) + require.NotNil(t, planned.Execution.Apply) + require.Empty(t, planned.Execution.Apply.BeforeHooks) + require.Equal(t, []string{"notify", "always"}, hookNames(planned.Execution.Apply.AfterHooks)) + require.Nil(t, planned.Execution.Apply.Apply) + + apply := executionUnit(t, body) + apply.Command = "apply" + applied, err := Resolve(t.Context(), apply) + require.NoError(t, err) + require.Nil(t, applied.Execution.Apply, "apply already evaluates as apply") + require.Equal(t, planned.Execution.Apply.AfterHooks, applied.Execution.AfterHooks) +} + +func TestPlanPreparationRecordsThatApplyRunsNothing(t *testing.T) { + result, err := Resolve(t.Context(), executionUnit(t, ` + before_hook "plan_only" { + commands = ["plan", "apply"] + execute = ["tool", "plan"] + if = get_terraform_command() == "plan" + } +`)) + require.NoError(t, err) + require.Len(t, result.Execution.BeforeHooks, 1) + require.NotNil(t, result.Execution.Apply, "an empty apply-time evaluation must not fall back to plan hooks") + require.Empty(t, result.Execution.Apply.BeforeHooks) +} + +func TestPlanPreparationOmitsApplyWhenCommandIsNotObserved(t *testing.T) { + for name, body := range map[string]string{ + "no command function": ` + after_hook "always" { + commands = ["apply"] + execute = ["tool", "always"] + } +`, + "same result for apply": ` + after_hook "always" { + commands = ["apply"] + execute = ["tool", "always"] + if = length(get_terraform_command()) > 0 + } +`, + } { + t.Run(name, func(t *testing.T) { + result, err := Resolve(t.Context(), executionUnit(t, body)) + require.NoError(t, err) + require.Equal(t, []string{"always"}, hookNames(result.Execution.AfterHooks)) + require.Nil(t, result.Execution.Apply) + }) + } +} + +func TestObservesCommandAssumesUnreadableFilesDo(t *testing.T) { + require.True(t, observesCommand(afero.NewMemMapFs(), []string{"missing.hcl"})) +} + +func hookNames(hooks []model.Hook) []string { + var names []string + for i := range hooks { + names = append(names, hooks[i].Name) + } + return names +} diff --git a/plugins/terragrunt/internal/engine/resolve.go b/plugins/terragrunt/internal/engine/resolve.go index 75c91b8d2b5..a9e693bb664 100644 --- a/plugins/terragrunt/internal/engine/resolve.go +++ b/plugins/terragrunt/internal/engine/resolve.go @@ -74,15 +74,7 @@ func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (* if command == "" || command == "discover" || command == "generate" { command = "plan" } - pctx := config.NewParsingContext().WithFileReadTracking() - pctx.WorkingDir, pctx.RootWorkingDir = filepath.Dir(path), filepath.Dir(path) - pctx.TerragruntConfigPath, pctx.OriginalTerragruntConfigPath = path, path - pctx.TFPath, pctx.TerraformCommand = request.Tofu, command - pctx.OriginalTerraformCommand = command - pctx.TerraformCliArgs.SetCommand(command).AppendArgument(request.Arguments...) - pctx.DownloadDir = filepath.Join(filepath.Dir(path), ".terragrunt-cache") - pctx.AutoInit = true - pctx.MaxFoldersToCheck = options.DefaultMaxFoldersToCheck + pctx := parsingContext(path, request.Tofu, command, request.Arguments) if filepath.Base(path) == "terragrunt.stack.hcl" { if generate { if err := atmosbridge.Generate(ctx, logger, environment, path, request.Tofu); err != nil { @@ -215,6 +207,12 @@ func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (* if execution, err = executionFromConfig(cfg.Terraform); err != nil { return nil, err } + reparse := func(apply *config.ParsingContext) (*config.TerragruntConfig, error) { + return config.ParseConfigFile(ctx, logger, environment, apply, path, nil) + } + if execution, err = applyTimeExecution(request, path, execution, environment.FS, pctx.FilesRead.Paths(), reparse); err != nil { + return nil, err + } } if cfg.PreventDestroy != nil && *cfg.PreventDestroy && atmosbridge.IsDestroyOperation(request.Command, request.Arguments) { return nil, fmt.Errorf("unit is protected by prevent_destroy") @@ -254,3 +252,17 @@ func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (* } return result, nil } + +// parsingContext configures Terragrunt evaluation of the unit at path as the named command. +func parsingContext(path, tofu, command string, arguments []string) *config.ParsingContext { + pctx := config.NewParsingContext().WithFileReadTracking() + pctx.WorkingDir, pctx.RootWorkingDir = filepath.Dir(path), filepath.Dir(path) + pctx.TerragruntConfigPath, pctx.OriginalTerragruntConfigPath = path, path + pctx.TFPath, pctx.TerraformCommand = tofu, command + pctx.OriginalTerraformCommand = command + pctx.TerraformCliArgs.SetCommand(command).AppendArgument(arguments...) + pctx.DownloadDir = filepath.Join(filepath.Dir(path), ".terragrunt-cache") + pctx.AutoInit = true + pctx.MaxFoldersToCheck = options.DefaultMaxFoldersToCheck + return pctx +} diff --git a/plugins/terragrunt/internal/model/model.go b/plugins/terragrunt/internal/model/model.go index 5f50d34f5aa..2ecbbddbda1 100644 --- a/plugins/terragrunt/internal/model/model.go +++ b/plugins/terragrunt/internal/model/model.go @@ -57,6 +57,9 @@ type Execution struct { BeforeHooks []Hook `json:"before_hooks,omitempty"` AfterHooks []Hook `json:"after_hooks,omitempty"` ErrorHooks []Hook `json:"error_hooks,omitempty"` + // Apply is the metadata evaluated for the apply command while preparing a plan whose + // configuration depends on the command. Nil means the plan-time metadata applies. + Apply *Execution `json:"apply,omitempty"` } // ExtraArguments adds arguments, variable files, and environment to the listed commands. diff --git a/plugins/terragrunt/internal/provider/preparation.go b/plugins/terragrunt/internal/provider/preparation.go index e75b1b6f092..1e8e6d3be22 100644 --- a/plugins/terragrunt/internal/provider/preparation.go +++ b/plugins/terragrunt/internal/provider/preparation.go @@ -172,6 +172,7 @@ func executionResponse(execution *model.Execution) *pluginv1.ComponentExecutionC BeforeHooks: hooksResponse(execution.BeforeHooks), AfterHooks: hooksResponse(execution.AfterHooks), ErrorHooks: hooksResponse(execution.ErrorHooks), + Apply: executionResponse(execution.Apply), } for i := range execution.ExtraArguments { extra := &execution.ExtraArguments[i] diff --git a/plugins/terragrunt/internal/provider/provider_test.go b/plugins/terragrunt/internal/provider/provider_test.go index 53a8625dc34..f3155a9426f 100644 --- a/plugins/terragrunt/internal/provider/provider_test.go +++ b/plugins/terragrunt/internal/provider/provider_test.go @@ -158,6 +158,34 @@ func TestPrepareReturnsExecutionInDeclarationOrder(t *testing.T) { require.Equal(t, []string{"x", "y"}, got.ErrorHooks[0].OnErrors) } +// The apply-time evaluation crosses the boundary distinctly, including an empty one, which +// means apply runs nothing rather than the plan-time metadata. +func TestPrepareReturnsApplyTimeExecution(t *testing.T) { + request := componentRequest(t) + execution := &model.Execution{ + BeforeHooks: []model.Hook{{Name: "plan_only", Commands: []string{"plan"}, Execute: []string{"tool", "plan"}}}, + Apply: &model.Execution{AfterHooks: []model.Hook{{Name: "notify", Commands: []string{"apply"}, Execute: []string{"tool", "notify"}}}}, + } + p := New(func(context.Context, model.Request) (*model.Resolution, error) { + return &model.Resolution{Execution: execution, Source: request.RootDirectory, WorkingDir: request.RootDirectory, BackendType: "local", Backend: map[string]any{}}, nil + }) + result, err := p.Prepare(t.Context(), request) + require.NoError(t, err) + got := result.GetExecution() + require.Equal(t, "plan_only", got.GetBeforeHooks()[0].GetName()) + require.Empty(t, got.GetAfterHooks()) + require.Len(t, got.GetApply().GetAfterHooks(), 1) + require.Equal(t, "notify", got.GetApply().GetAfterHooks()[0].GetName()) + require.Equal(t, []string{"tool", "notify"}, got.GetApply().GetAfterHooks()[0].GetExecute()) + require.Nil(t, got.GetApply().GetApply()) + + execution.Apply = &model.Execution{} + result, err = p.Prepare(t.Context(), request) + require.NoError(t, err) + require.NotNil(t, result.GetExecution().GetApply(), "an empty apply-time evaluation must be preserved") + require.Empty(t, result.GetExecution().GetApply().GetBeforeHooks()) +} + func TestPrepareOmitsExecutionWithoutHooks(t *testing.T) { request := componentRequest(t) p := New(func(context.Context, model.Request) (*model.Resolution, error) { diff --git a/proto/atmos/plugin/v1/plugin.proto b/proto/atmos/plugin/v1/plugin.proto index 3d64dbf1075..be156d6f195 100644 --- a/proto/atmos/plugin/v1/plugin.proto +++ b/proto/atmos/plugin/v1/plugin.proto @@ -260,6 +260,12 @@ message ComponentExecutionConfiguration { repeated ComponentHook before_hooks = 2; repeated ComponentHook after_hooks = 3; repeated ComponentHook error_hooks = 4; + // Metadata evaluated as the apply command would see it. Providers set it when + // preparing a plan whose execution depends on the command, so a saved plan + // applied later runs the apply-time hooks recorded with the reviewed plan. + // Unset means the plan-time metadata applies unchanged; an empty message + // means apply adds nothing. + ComponentExecutionConfiguration apply = 5; } message ComponentExtraArguments { repeated string commands = 1; diff --git a/sdk/proto/atmos/plugin/v1/plugin.pb.go b/sdk/proto/atmos/plugin/v1/plugin.pb.go index 5228242498d..3a6e7fbce94 100644 --- a/sdk/proto/atmos/plugin/v1/plugin.pb.go +++ b/sdk/proto/atmos/plugin/v1/plugin.pb.go @@ -2821,8 +2821,14 @@ type ComponentExecutionConfiguration struct { BeforeHooks []*ComponentHook `protobuf:"bytes,2,rep,name=before_hooks,json=beforeHooks,proto3" json:"before_hooks,omitempty"` AfterHooks []*ComponentHook `protobuf:"bytes,3,rep,name=after_hooks,json=afterHooks,proto3" json:"after_hooks,omitempty"` ErrorHooks []*ComponentHook `protobuf:"bytes,4,rep,name=error_hooks,json=errorHooks,proto3" json:"error_hooks,omitempty"` - unknownFields protoimpl.UnknownFields - sizeCache protoimpl.SizeCache + // Metadata evaluated as the apply command would see it. Providers set it when + // preparing a plan whose execution depends on the command, so a saved plan + // applied later runs the apply-time hooks recorded with the reviewed plan. + // Unset means the plan-time metadata applies unchanged; an empty message + // means apply adds nothing. + Apply *ComponentExecutionConfiguration `protobuf:"bytes,5,opt,name=apply,proto3" json:"apply,omitempty"` + unknownFields protoimpl.UnknownFields + sizeCache protoimpl.SizeCache } func (x *ComponentExecutionConfiguration) Reset() { @@ -2883,6 +2889,13 @@ func (x *ComponentExecutionConfiguration) GetErrorHooks() []*ComponentHook { return nil } +func (x *ComponentExecutionConfiguration) GetApply() *ComponentExecutionConfiguration { + if x != nil { + return x.Apply + } + return nil +} + type ComponentExtraArguments struct { state protoimpl.MessageState `protogen:"open.v1"` Commands []string `protobuf:"bytes,1,rep,name=commands,proto3" json:"commands,omitempty"` @@ -3475,14 +3488,15 @@ const file_atmos_plugin_v1_plugin_proto_rawDesc = "" + "\x04path\x18\x01 \x01(\tR\x04path\x12\x18\n" + "\acontent\x18\x02 \x01(\fR\acontent\x12\x1b\n" + "\tif_exists\x18\x03 \x01(\tR\bifExists\x12\x1c\n" + - "\tsensitive\x18\x04 \x01(\bR\tsensitive\"\xb9\x02\n" + + "\tsensitive\x18\x04 \x01(\bR\tsensitive\"\x81\x03\n" + "\x1fComponentExecutionConfiguration\x12Q\n" + "\x0fextra_arguments\x18\x01 \x03(\v2(.atmos.plugin.v1.ComponentExtraArgumentsR\x0eextraArguments\x12A\n" + "\fbefore_hooks\x18\x02 \x03(\v2\x1e.atmos.plugin.v1.ComponentHookR\vbeforeHooks\x12?\n" + "\vafter_hooks\x18\x03 \x03(\v2\x1e.atmos.plugin.v1.ComponentHookR\n" + "afterHooks\x12?\n" + "\verror_hooks\x18\x04 \x03(\v2\x1e.atmos.plugin.v1.ComponentHookR\n" + - "errorHooks\"\xcc\x02\n" + + "errorHooks\x12F\n" + + "\x05apply\x18\x05 \x01(\v20.atmos.plugin.v1.ComponentExecutionConfigurationR\x05apply\"\xcc\x02\n" + "\x17ComponentExtraArguments\x12\x1a\n" + "\bcommands\x18\x01 \x03(\tR\bcommands\x12\x1c\n" + "\targuments\x18\x02 \x03(\tR\targuments\x12,\n" + @@ -3687,63 +3701,64 @@ var file_atmos_plugin_v1_plugin_proto_depIdxs = []int32{ 44, // 59: atmos.plugin.v1.ComponentExecutionConfiguration.before_hooks:type_name -> atmos.plugin.v1.ComponentHook 44, // 60: atmos.plugin.v1.ComponentExecutionConfiguration.after_hooks:type_name -> atmos.plugin.v1.ComponentHook 44, // 61: atmos.plugin.v1.ComponentExecutionConfiguration.error_hooks:type_name -> atmos.plugin.v1.ComponentHook - 54, // 62: atmos.plugin.v1.ComponentExtraArguments.environment:type_name -> atmos.plugin.v1.ComponentExtraArguments.EnvironmentEntry - 28, // 63: atmos.plugin.v1.PrepareComponentResponse.inputs:type_name -> atmos.plugin.v1.ComponentValue - 40, // 64: atmos.plugin.v1.PrepareComponentResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity - 41, // 65: atmos.plugin.v1.PrepareComponentResponse.files:type_name -> atmos.plugin.v1.ComponentGeneratedFile - 33, // 66: atmos.plugin.v1.PrepareComponentResponse.dependencies:type_name -> atmos.plugin.v1.ComponentDependency - 32, // 67: atmos.plugin.v1.PrepareComponentResponse.sources:type_name -> atmos.plugin.v1.ComponentSource - 14, // 68: atmos.plugin.v1.PrepareComponentResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic - 42, // 69: atmos.plugin.v1.PrepareComponentResponse.execution:type_name -> atmos.plugin.v1.ComponentExecutionConfiguration - 40, // 70: atmos.plugin.v1.ResolveComponentStateResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity - 32, // 71: atmos.plugin.v1.ResolveComponentStateResponse.sources:type_name -> atmos.plugin.v1.ComponentSource - 14, // 72: atmos.plugin.v1.ResolveComponentStateResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic - 28, // 73: atmos.plugin.v1.ComponentOperation.OptionsEntry.value:type_name -> atmos.plugin.v1.ComponentValue - 6, // 74: atmos.plugin.v1.Info.Describe:input_type -> atmos.plugin.v1.DescribeRequest - 8, // 75: atmos.plugin.v1.Info.Configure:input_type -> atmos.plugin.v1.ConfigureRequest - 9, // 76: atmos.plugin.v1.AuthProvider.Validate:input_type -> atmos.plugin.v1.ProviderRequest - 9, // 77: atmos.plugin.v1.AuthProvider.Authenticate:input_type -> atmos.plugin.v1.ProviderRequest - 17, // 78: atmos.plugin.v1.AuthProvider.Refresh:input_type -> atmos.plugin.v1.RefreshRequest - 9, // 79: atmos.plugin.v1.AuthProvider.Logout:input_type -> atmos.plugin.v1.ProviderRequest - 9, // 80: atmos.plugin.v1.AuthProvider.Environment:input_type -> atmos.plugin.v1.ProviderRequest - 9, // 81: atmos.plugin.v1.AuthProvider.Paths:input_type -> atmos.plugin.v1.ProviderRequest - 20, // 82: atmos.plugin.v1.AuthProvider.PrepareEnvironment:input_type -> atmos.plugin.v1.PrepareEnvironmentRequest - 10, // 83: atmos.plugin.v1.AuthIdentity.Validate:input_type -> atmos.plugin.v1.IdentityRequest - 23, // 84: atmos.plugin.v1.AuthIdentity.Authenticate:input_type -> atmos.plugin.v1.IdentityAuthenticateRequest - 10, // 85: atmos.plugin.v1.AuthIdentity.Logout:input_type -> atmos.plugin.v1.IdentityRequest - 10, // 86: atmos.plugin.v1.AuthIdentity.Environment:input_type -> atmos.plugin.v1.IdentityRequest - 10, // 87: atmos.plugin.v1.AuthIdentity.Paths:input_type -> atmos.plugin.v1.IdentityRequest - 24, // 88: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:input_type -> atmos.plugin.v1.IdentityPrepareRequest - 25, // 89: atmos.plugin.v1.Commands.Run:input_type -> atmos.plugin.v1.CommandRequest - 37, // 90: atmos.plugin.v1.ComponentDiscovery.Discover:input_type -> atmos.plugin.v1.DiscoverComponentsRequest - 39, // 91: atmos.plugin.v1.ComponentValidation.Validate:input_type -> atmos.plugin.v1.ComponentRequest - 39, // 92: atmos.plugin.v1.ComponentPreparation.Prepare:input_type -> atmos.plugin.v1.ComponentRequest - 39, // 93: atmos.plugin.v1.ComponentState.Resolve:input_type -> atmos.plugin.v1.ComponentRequest - 7, // 94: atmos.plugin.v1.Info.Describe:output_type -> atmos.plugin.v1.DescribeResponse - 57, // 95: atmos.plugin.v1.Info.Configure:output_type -> google.protobuf.Empty - 15, // 96: atmos.plugin.v1.AuthProvider.Validate:output_type -> atmos.plugin.v1.ValidateResponse - 13, // 97: atmos.plugin.v1.AuthProvider.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent - 18, // 98: atmos.plugin.v1.AuthProvider.Refresh:output_type -> atmos.plugin.v1.RefreshResponse - 57, // 99: atmos.plugin.v1.AuthProvider.Logout:output_type -> google.protobuf.Empty - 19, // 100: atmos.plugin.v1.AuthProvider.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse - 22, // 101: atmos.plugin.v1.AuthProvider.Paths:output_type -> atmos.plugin.v1.PathsResponse - 19, // 102: atmos.plugin.v1.AuthProvider.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse - 15, // 103: atmos.plugin.v1.AuthIdentity.Validate:output_type -> atmos.plugin.v1.ValidateResponse - 13, // 104: atmos.plugin.v1.AuthIdentity.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent - 57, // 105: atmos.plugin.v1.AuthIdentity.Logout:output_type -> google.protobuf.Empty - 19, // 106: atmos.plugin.v1.AuthIdentity.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse - 22, // 107: atmos.plugin.v1.AuthIdentity.Paths:output_type -> atmos.plugin.v1.PathsResponse - 19, // 108: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse - 27, // 109: atmos.plugin.v1.Commands.Run:output_type -> atmos.plugin.v1.CommandEvent - 38, // 110: atmos.plugin.v1.ComponentDiscovery.Discover:output_type -> atmos.plugin.v1.DiscoverComponentsResponse - 15, // 111: atmos.plugin.v1.ComponentValidation.Validate:output_type -> atmos.plugin.v1.ValidateResponse - 45, // 112: atmos.plugin.v1.ComponentPreparation.Prepare:output_type -> atmos.plugin.v1.PrepareComponentResponse - 46, // 113: atmos.plugin.v1.ComponentState.Resolve:output_type -> atmos.plugin.v1.ResolveComponentStateResponse - 94, // [94:114] is the sub-list for method output_type - 74, // [74:94] is the sub-list for method input_type - 74, // [74:74] is the sub-list for extension type_name - 74, // [74:74] is the sub-list for extension extendee - 0, // [0:74] is the sub-list for field type_name + 42, // 62: atmos.plugin.v1.ComponentExecutionConfiguration.apply:type_name -> atmos.plugin.v1.ComponentExecutionConfiguration + 54, // 63: atmos.plugin.v1.ComponentExtraArguments.environment:type_name -> atmos.plugin.v1.ComponentExtraArguments.EnvironmentEntry + 28, // 64: atmos.plugin.v1.PrepareComponentResponse.inputs:type_name -> atmos.plugin.v1.ComponentValue + 40, // 65: atmos.plugin.v1.PrepareComponentResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity + 41, // 66: atmos.plugin.v1.PrepareComponentResponse.files:type_name -> atmos.plugin.v1.ComponentGeneratedFile + 33, // 67: atmos.plugin.v1.PrepareComponentResponse.dependencies:type_name -> atmos.plugin.v1.ComponentDependency + 32, // 68: atmos.plugin.v1.PrepareComponentResponse.sources:type_name -> atmos.plugin.v1.ComponentSource + 14, // 69: atmos.plugin.v1.PrepareComponentResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic + 42, // 70: atmos.plugin.v1.PrepareComponentResponse.execution:type_name -> atmos.plugin.v1.ComponentExecutionConfiguration + 40, // 71: atmos.plugin.v1.ResolveComponentStateResponse.state:type_name -> atmos.plugin.v1.ComponentStateIdentity + 32, // 72: atmos.plugin.v1.ResolveComponentStateResponse.sources:type_name -> atmos.plugin.v1.ComponentSource + 14, // 73: atmos.plugin.v1.ResolveComponentStateResponse.diagnostics:type_name -> atmos.plugin.v1.Diagnostic + 28, // 74: atmos.plugin.v1.ComponentOperation.OptionsEntry.value:type_name -> atmos.plugin.v1.ComponentValue + 6, // 75: atmos.plugin.v1.Info.Describe:input_type -> atmos.plugin.v1.DescribeRequest + 8, // 76: atmos.plugin.v1.Info.Configure:input_type -> atmos.plugin.v1.ConfigureRequest + 9, // 77: atmos.plugin.v1.AuthProvider.Validate:input_type -> atmos.plugin.v1.ProviderRequest + 9, // 78: atmos.plugin.v1.AuthProvider.Authenticate:input_type -> atmos.plugin.v1.ProviderRequest + 17, // 79: atmos.plugin.v1.AuthProvider.Refresh:input_type -> atmos.plugin.v1.RefreshRequest + 9, // 80: atmos.plugin.v1.AuthProvider.Logout:input_type -> atmos.plugin.v1.ProviderRequest + 9, // 81: atmos.plugin.v1.AuthProvider.Environment:input_type -> atmos.plugin.v1.ProviderRequest + 9, // 82: atmos.plugin.v1.AuthProvider.Paths:input_type -> atmos.plugin.v1.ProviderRequest + 20, // 83: atmos.plugin.v1.AuthProvider.PrepareEnvironment:input_type -> atmos.plugin.v1.PrepareEnvironmentRequest + 10, // 84: atmos.plugin.v1.AuthIdentity.Validate:input_type -> atmos.plugin.v1.IdentityRequest + 23, // 85: atmos.plugin.v1.AuthIdentity.Authenticate:input_type -> atmos.plugin.v1.IdentityAuthenticateRequest + 10, // 86: atmos.plugin.v1.AuthIdentity.Logout:input_type -> atmos.plugin.v1.IdentityRequest + 10, // 87: atmos.plugin.v1.AuthIdentity.Environment:input_type -> atmos.plugin.v1.IdentityRequest + 10, // 88: atmos.plugin.v1.AuthIdentity.Paths:input_type -> atmos.plugin.v1.IdentityRequest + 24, // 89: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:input_type -> atmos.plugin.v1.IdentityPrepareRequest + 25, // 90: atmos.plugin.v1.Commands.Run:input_type -> atmos.plugin.v1.CommandRequest + 37, // 91: atmos.plugin.v1.ComponentDiscovery.Discover:input_type -> atmos.plugin.v1.DiscoverComponentsRequest + 39, // 92: atmos.plugin.v1.ComponentValidation.Validate:input_type -> atmos.plugin.v1.ComponentRequest + 39, // 93: atmos.plugin.v1.ComponentPreparation.Prepare:input_type -> atmos.plugin.v1.ComponentRequest + 39, // 94: atmos.plugin.v1.ComponentState.Resolve:input_type -> atmos.plugin.v1.ComponentRequest + 7, // 95: atmos.plugin.v1.Info.Describe:output_type -> atmos.plugin.v1.DescribeResponse + 57, // 96: atmos.plugin.v1.Info.Configure:output_type -> google.protobuf.Empty + 15, // 97: atmos.plugin.v1.AuthProvider.Validate:output_type -> atmos.plugin.v1.ValidateResponse + 13, // 98: atmos.plugin.v1.AuthProvider.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent + 18, // 99: atmos.plugin.v1.AuthProvider.Refresh:output_type -> atmos.plugin.v1.RefreshResponse + 57, // 100: atmos.plugin.v1.AuthProvider.Logout:output_type -> google.protobuf.Empty + 19, // 101: atmos.plugin.v1.AuthProvider.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse + 22, // 102: atmos.plugin.v1.AuthProvider.Paths:output_type -> atmos.plugin.v1.PathsResponse + 19, // 103: atmos.plugin.v1.AuthProvider.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse + 15, // 104: atmos.plugin.v1.AuthIdentity.Validate:output_type -> atmos.plugin.v1.ValidateResponse + 13, // 105: atmos.plugin.v1.AuthIdentity.Authenticate:output_type -> atmos.plugin.v1.AuthenticateEvent + 57, // 106: atmos.plugin.v1.AuthIdentity.Logout:output_type -> google.protobuf.Empty + 19, // 107: atmos.plugin.v1.AuthIdentity.Environment:output_type -> atmos.plugin.v1.EnvironmentResponse + 22, // 108: atmos.plugin.v1.AuthIdentity.Paths:output_type -> atmos.plugin.v1.PathsResponse + 19, // 109: atmos.plugin.v1.AuthIdentity.PrepareEnvironment:output_type -> atmos.plugin.v1.EnvironmentResponse + 27, // 110: atmos.plugin.v1.Commands.Run:output_type -> atmos.plugin.v1.CommandEvent + 38, // 111: atmos.plugin.v1.ComponentDiscovery.Discover:output_type -> atmos.plugin.v1.DiscoverComponentsResponse + 15, // 112: atmos.plugin.v1.ComponentValidation.Validate:output_type -> atmos.plugin.v1.ValidateResponse + 45, // 113: atmos.plugin.v1.ComponentPreparation.Prepare:output_type -> atmos.plugin.v1.PrepareComponentResponse + 46, // 114: atmos.plugin.v1.ComponentState.Resolve:output_type -> atmos.plugin.v1.ResolveComponentStateResponse + 95, // [95:115] is the sub-list for method output_type + 75, // [75:95] is the sub-list for method input_type + 75, // [75:75] is the sub-list for extension type_name + 75, // [75:75] is the sub-list for extension extendee + 0, // [0:75] is the sub-list for field type_name } func init() { file_atmos_plugin_v1_plugin_proto_init() } From 114daa77073514a8d9bf325695f1aad293744ae4 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 13 Nov 2026 06:29:46 -0600 Subject: [PATCH 28/52] fix(terragrunt): label incomplete dry-run previews --- pkg/component/terraform/execute_pipeline.go | 3 +++ website/docs/migration/terragrunt.mdx | 1 + 2 files changed, 4 insertions(+) diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index b133bc0c6f2..b577e736f3a 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -198,6 +198,9 @@ func (e *Executor) executeCommandPipeline( opts ...component.ExecutionOption, ) error { componentPath := execCtx.componentPath + if info.DryRun && schema.ConfigurationOwned(info.ComponentSettingsSection) { + ui.Warning("Dry-run preview may omit Terragrunt extra arguments, required var files, and environment variables because Terragrunt configuration is not evaluated") + } if shouldRunTerraformInit(atmosConfig, info) { // Phase-level CI log grouping (Dimension "phase"): fold the `init` phase diff --git a/website/docs/migration/terragrunt.mdx b/website/docs/migration/terragrunt.mdx index e1187ebeb5c..9a6ec552bd1 100644 --- a/website/docs/migration/terragrunt.mdx +++ b/website/docs/migration/terragrunt.mdx @@ -935,6 +935,7 @@ atmos terraform plan vpc -s live/dev - `dependency` and `dependencies` blocks become ordering and output edges, and dependency outputs are refreshed in the same apply. - `extra_arguments` and `before_hook`, `after_hook`, and `error_hook` blocks run with the same environment, identity, and toolchain as the Terraform command. A hook that lists `plan` does not run for `apply`. The implicit `init` matches hooks for `init`, not for the requested command. +- Dry runs do not evaluate Terragrunt configuration. Their command previews may omit dynamically computed extra arguments, required var files, and environment variables. Real runs include these values. - Generated files and backend configuration keep their Terragrunt identity, so existing state stays addressable. - `atmos terraform plan` followed by `atmos terraform apply --from-plan` applies exactly the reviewed plan. Atmos rejects the apply if the plan file, the unit's configuration, or the working directory changed after the plan. The original working directory must still exist. - Remote Git and HTTPS module sources are fetched by Atmos. Local module sources are re-copied on every run so edits take effect. From 9568d8e94aa0be893b41ef2cc25c369f5ba0beba Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 13 Nov 2026 07:09:14 -0600 Subject: [PATCH 29/52] fix(terragrunt): address L5 saved-plan and execution review findings - Record an explicit -detailed-exitcode plan before returning status 2. - Check plan output flags against the effective subprocess environment (sanitized base, global env, component env, extra arguments). - Resolve TF_DATA_DIR for skip-init state reads with SetupEnvironment's precedence. - Keep the action from starting after cancellation, including during the last before hook and with no hooks. - Include downloaded-module subdirectories (.git, .atmos, .terragrunt-cache) in the saved-plan workdir digest. - Watch configuration read only during the apply-time evaluation as saved-plan sources. - Wrap source-fingerprint and saved-plan I/O failures with static sentinels (ErrReadFile, ErrPlanVerificationFailed). - Return static sentinels for the Terragrunt remote_state and prevent_destroy rejections. - Close the output capture before removing it in the capture test, so it works on Windows. - Use the generated configuration-runtime mock in the hooks test. Co-Authored-By: Claude Opus 5.5 --- pkg/component/execution/lifecycle.go | 10 +-- pkg/component/execution/lifecycle_test.go | 24 +++++++ pkg/component/terraform/configuration_plan.go | 67 +++++++++++++------ .../configuration_plan_failures_test.go | 2 +- .../terraform/configuration_plan_test.go | 65 +++++++++++++++--- .../terraform/execute_hooks_process_test.go | 4 +- pkg/component/terraform/execute_hooks_test.go | 20 ++---- pkg/component/terraform/execute_pipeline.go | 16 ++++- pkg/plugin/adapter/configuration/plan.go | 16 +++-- pkg/plugin/adapter/configuration/plan_test.go | 3 +- pkg/terraform/output/configuration_state.go | 16 +++-- .../output/configuration_state_test.go | 33 +++++++++ plugins/terragrunt/internal/engine/backend.go | 7 +- .../internal/engine/backend_test.go | 2 + .../terragrunt/internal/engine/execution.go | 17 +++-- .../internal/engine/execution_test.go | 21 ++++++ .../internal/engine/generation_test.go | 3 +- plugins/terragrunt/internal/engine/resolve.go | 13 ++-- 18 files changed, 264 insertions(+), 75 deletions(-) diff --git a/pkg/component/execution/lifecycle.go b/pkg/component/execution/lifecycle.go index 9ee7cc81a89..d5a34ab68e8 100644 --- a/pkg/component/execution/lifecycle.go +++ b/pkg/component/execution/lifecycle.go @@ -52,16 +52,18 @@ type failure struct { func Run(ctx context.Context, config *schema.ComponentExecution, command string, runner Runner, action func() (Output, error)) error { defer perf.Track(nil, "execution.Run")() - if config == nil || (len(config.BeforeHooks)+len(config.AfterHooks)+len(config.ErrorHooks) == 0) { - _, err := action() + // An already-canceled context never starts the action. + if err := ctx.Err(); err != nil { return err } - if err := ctx.Err(); err != nil { + if config == nil || (len(config.BeforeHooks)+len(config.AfterHooks)+len(config.ErrorHooks) == 0) { + _, err := action() return err } failures := runHooks(ctx, runner, KindBefore, hooksFor(config.BeforeHooks, command), nil) - if len(failures) == 0 { + // Cancellation during the last before hook must also keep the action from starting. + if len(failures) == 0 && ctx.Err() == nil { if output, err := action(); err != nil { failures = append(failures, failure{err: err, output: output}) } diff --git a/pkg/component/execution/lifecycle_test.go b/pkg/component/execution/lifecycle_test.go index 1ba53313a52..7f778e5fb83 100644 --- a/pkg/component/execution/lifecycle_test.go +++ b/pkg/component/execution/lifecycle_test.go @@ -235,6 +235,30 @@ func TestRunDoesNotStartWhenAlreadyCanceled(t *testing.T) { require.Empty(t, r.calls) } +func TestRunDoesNotStartActionWhenLastBeforeHookIsCanceled(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + r := &recorder{onCall: func(name string) { + if name == "before_hook:last" { + cancel() + } + }} + config := &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{hook("first", "plan"), hook("last", "plan")}} + err := Run(ctx, config, "plan", r, r.action) + require.ErrorIs(t, err, context.Canceled) + require.Equal(t, []string{"before_hook:first", "before_hook:last"}, r.calls) +} + +func TestRunWithoutHooksDoesNotStartWhenAlreadyCanceled(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + cancel() + for _, config := range []*schema.ComponentExecution{nil, {}} { + r := &recorder{} + err := Run(ctx, config, "plan", r, r.action) + require.ErrorIs(t, err, context.Canceled) + require.Empty(t, r.calls) + } +} + func TestRunErrorMessageNamesTheHook(t *testing.T) { r := &recorder{failures: map[string]error{"lint": errors.New("exit status 3")}} err := runLifecycle(t, &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{hook("lint", "plan")}}, "plan", r) diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go index ca8f2cdba32..e33a67a2016 100644 --- a/pkg/component/terraform/configuration_plan.go +++ b/pkg/component/terraform/configuration_plan.go @@ -15,6 +15,7 @@ import ( "strings" errUtils "github.com/cloudposse/atmos/errors" + envpkg "github.com/cloudposse/atmos/pkg/env" "github.com/cloudposse/atmos/pkg/schema" ) @@ -225,29 +226,41 @@ func validatePlanOutputFlags(command string, args []string) error { // validateEnvironmentPlanOutput rejects plan output flags in TF_CLI_ARGS or // TF_CLI_ARGS_plan for a configuration-owned plan. Terraform reads these // variables itself, so a flag there could choose a plan file that Atmos does not -// see and therefore does not bind. The check covers the inherited process +// see and therefore does not bind. The check inspects the effective value of each +// variable in the subprocess environment: the sanitized process environment (or +// the inherited one when auth did not sanitize it), the global atmos.yaml // environment, the component environment, and provider extra-argument -// environment, which together form the subprocess environment. Hooks run as -// child processes and cannot change it. -func validateEnvironmentPlanOutput(info *schema.ConfigAndStacksInfo, execCtx *componentExecContext) error { +// environment, with later layers overriding earlier ones. Hooks run as child +// processes and cannot change it. +func validateEnvironmentPlanOutput(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, execCtx *componentExecContext) error { if execCtx.preparedConfiguration == nil || info.DryRun || info.SubCommand != subcommandPlan { return nil } - for _, entry := range planEnvironment(info) { - name, value, _ := strings.Cut(entry, "=") - if name != tfCliArgsEnvVar && name != tfCliArgsEnvVar+"_"+subcommandPlan { - continue + effective := make(map[string]string) + for _, entry := range planEnvironment(atmosConfig, info) { + if name, value, ok := strings.Cut(entry, "="); ok { + effective[name] = value } - if containsOutputFlag(parseArgs(value)) { + } + for _, name := range []string{tfCliArgsEnvVar, tfCliArgsEnvVar + "_" + subcommandPlan} { + if containsOutputFlag(parseArgs(effective[name])) { return fmt.Errorf("%w: %s sets a plan output flag; pass -out as a command argument instead", errUtils.ErrPlanVerificationFailed, name) } } return nil } -// planEnvironment lists every environment entry a plan subprocess may receive. -func planEnvironment(info *schema.ConfigAndStacksInfo) []string { - entries := append(os.Environ(), info.ComponentEnvList...) +// planEnvironment lists the environment layers a plan subprocess receives, in precedence order. +func planEnvironment(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) []string { + base := info.SanitizedEnv + if base == nil { + base = os.Environ() + } + entries := slices.Clone(base) + if atmosConfig != nil { + entries = envpkg.MergeGlobalEnv(entries, atmosConfig.Env) + } + entries = append(entries, info.ComponentEnvList...) if info.ComponentExecution == nil { return entries } @@ -294,12 +307,12 @@ func plannedOutputPath(directory string, args []string) string { func fileDigest(path string) (string, error) { file, err := os.Open(path) if err != nil { - return "", err + return "", planIOError(err) } defer file.Close() h := sha256.New() if _, err := io.Copy(h, file); err != nil { - return "", err + return "", planIOError(err) } return hex.EncodeToString(h.Sum(nil)), nil } @@ -307,17 +320,25 @@ func fileDigest(path string) (string, error) { func writePlanBinding(path string, data []byte) error { file, err := os.CreateTemp(filepath.Dir(path), ".atmos-plan-*") if err != nil { - return err + return planIOError(err) } defer os.Remove(file.Name()) if _, err := file.Write(data); err != nil { file.Close() - return err + return planIOError(err) } if err := file.Close(); err != nil { - return err + return planIOError(err) } - return os.Rename(file.Name(), path) + if err := os.Rename(file.Name(), path); err != nil { + return planIOError(err) + } + return nil +} + +// planIOError wraps a saved-plan filesystem failure while keeping the underlying cause. +func planIOError(err error) error { + return fmt.Errorf("%w: %w", errUtils.ErrPlanVerificationFailed, err) } // verifyConfigurationPlanState rejects a changed backend without reading dependency outputs. @@ -377,8 +398,16 @@ func configurationWorkdirDigest(root, planPath string) (string, error) { // skipWorkdirDirectory reports whether a nested directory holds only mutable or cached data. // The .atmos directory is provisioner bookkeeping (access timestamps and metadata lock files // that even a read-only lookup of the retained directory creates), never executed content. +// Name-based exclusions apply only outside downloaded modules: a module's own subdirectories +// are content Terraform may execute during apply, whatever they are called. func skipWorkdirDirectory(root, path, name string) bool { - return path != root && (name == ".git" || name == ".terragrunt-cache" || name == ".atmos" || insideTerraformCacheOutsideModules(root, path)) + if path == root { + return false + } + if insideTerraformCacheOutsideModules(root, path) { + return true + } + return !insideTerraformModules(root, path) && (name == ".git" || name == ".terragrunt-cache" || name == ".atmos") } // Terraform's downloaded modules can contain scripts executed during apply. Other cache diff --git a/pkg/component/terraform/configuration_plan_failures_test.go b/pkg/component/terraform/configuration_plan_failures_test.go index d12e4dc4ddc..516589b25a8 100644 --- a/pkg/component/terraform/configuration_plan_failures_test.go +++ b/pkg/component/terraform/configuration_plan_failures_test.go @@ -156,7 +156,7 @@ func TestConfigurationWorkdirDigestSkipsOnlyNestedMutableDirectories(t *testing. require.NoError(t, err) require.Equal(t, baseline, same, "mutable data does not alter the retained-bytes digest") - for _, retained := range []string{"modules/child/main.tf", "terragrunt.hcl", ".terraform.lock.hcl", "settings.atmos.json"} { + for _, retained := range []string{"modules/child/main.tf", "terragrunt.hcl", ".terraform.lock.hcl", "settings.atmos.json", ".terraform/modules/vpc/.atmos/hook.sh", ".terraform/modules/vpc/.terragrunt-cache/main.tf", ".terraform/modules/vpc/.git/hooks/post-checkout"} { write(retained) changed, err := configurationWorkdirDigest(root, plan) require.NoError(t, err) diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index fc47cfec7ee..930d012ee78 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -152,9 +152,14 @@ func TestConfigurationPlanPathsAndFileErrors(t *testing.T) { require.Equal(t, path, configurationPlanPath(config, info)) info.PlanFile = path require.Equal(t, path, configurationPlanPath(config, info)) - require.Error(t, writePlanBinding(filepath.Join(t.TempDir(), "absent", "binding"), nil)) - _, err := fileDigest(t.TempDir()) - require.Error(t, err) + err := writePlanBinding(filepath.Join(t.TempDir(), "absent", "binding"), nil) + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + require.ErrorIs(t, err, os.ErrNotExist) + _, err = fileDigest(t.TempDir()) + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + _, err = fileDigest(filepath.Join(t.TempDir(), "absent")) + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + require.ErrorIs(t, err, os.ErrNotExist) _, err = configurationWorkdirDigest(filepath.Join(t.TempDir(), "absent"), path) require.Error(t, err) require.True(t, usesConfigurationPlan(info)) @@ -259,6 +264,41 @@ func TestRecoveredPlanBindsEffectiveOutput(t *testing.T) { require.FileExists(t, plan+configurationPlanSuffix) } +// TestDetailedExitCodePlanRecordsReviewedPlan binds a plan that reports changes with status 2, +// and leaves an ordinary failure unbound. +func TestDetailedExitCodePlanRecordsReviewedPlan(t *testing.T) { + for _, test := range []struct { + name string + args []string + code int + record bool + }{ + {name: "explicit detailed exit code with changes", args: []string{"plan", "-detailed-exitcode", "-out", "changes.plan"}, code: 2, record: true}, + {name: "failure with detailed exit code", args: []string{"plan", "-detailed-exitcode", "-out", "changes.plan"}, code: 1}, + {name: "status 2 without detailed exit code", args: []string{"plan", "-out", "changes.plan"}, code: 2}, + } { + t.Run(test.name, func(t *testing.T) { + config, info, execCtx, _, _ := planFixture(t) + shell := &fakeShell{run: func(call *shellCall) error { + if err := os.WriteFile(plannedOutputPath(execCtx.componentPath, call.args), []byte("changes"), 0o600); err != nil { + return err + } + return errUtils.ExitCodeError{Code: test.code} + }} + executor := testExecutor(t) + executor.services.ExecuteShellCommand = shell.execute + err := executor.runMainCommandPhase(config, info, execCtx, test.args, execCtx.componentPath, false) + require.Equal(t, test.code, resolveExitCode(err), "the plan status is returned unchanged") + binding := filepath.Join(execCtx.componentPath, "changes.plan") + configurationPlanSuffix + if test.record { + require.FileExists(t, binding) + return + } + require.NoFileExists(t, binding) + }) + } +} + type planShowRunner struct{} //nolint:gocritic // The process.Runner interface fixes the value parameter. @@ -291,6 +331,8 @@ func TestPlanOutputFlagsInEnvironmentAreRejected(t *testing.T) { inherited map[string]string component []string provider map[string]string + global map[string]string + sanitized []string commands []string reject bool }{ @@ -302,6 +344,11 @@ func TestPlanOutputFlagsInEnvironmentAreRejected(t *testing.T) { {name: "other flags", inherited: map[string]string{"TF_CLI_ARGS_plan": "-lock=false -outfile-like=x"}, component: []string{"TF_CLI_ARGS=-no-color"}}, {name: "apply only variable", component: []string{"TF_CLI_ARGS_apply=-out=x"}}, {name: "provider environment for apply", provider: map[string]string{"TF_CLI_ARGS_plan": "-out=env.plan"}, commands: []string{"apply"}}, + {name: "global environment", global: map[string]string{"TF_CLI_ARGS_plan": "-out=env.plan"}, reject: true}, + {name: "sanitized environment omits inherited flag", inherited: map[string]string{"TF_CLI_ARGS_plan": "-out=env.plan"}, sanitized: []string{"PATH=/bin"}}, + {name: "sanitized environment carries flag", sanitized: []string{"TF_CLI_ARGS=-out=env.plan"}, reject: true}, + {name: "component overrides inherited flag", inherited: map[string]string{"TF_CLI_ARGS_plan": "-out=env.plan"}, component: []string{"TF_CLI_ARGS_plan=-lock=false"}}, + {name: "component overrides global flag", global: map[string]string{"TF_CLI_ARGS": "-out=env.plan"}, component: []string{"TF_CLI_ARGS="}}, } { t.Run(test.name, func(t *testing.T) { t.Setenv("TF_CLI_ARGS", "") @@ -310,6 +357,8 @@ func TestPlanOutputFlagsInEnvironmentAreRejected(t *testing.T) { t.Setenv(name, value) } config, info, execCtx, _, path := planFixture(t) + config.Env = test.global + info.SanitizedEnv = test.sanitized info.ComponentEnvList = test.component if test.provider != nil { info.ComponentExecution = &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: test.commands, Environment: test.provider}}, BeforeHooks: []schema.ComponentHook{execHook("before", "plan")}} @@ -333,14 +382,14 @@ func TestPlanOutputFlagsInEnvironmentAreRejected(t *testing.T) { // TestEnvironmentPlanOutputIgnoresUnownedPlans keeps ordinary components unaffected. func TestEnvironmentPlanOutputIgnoresUnownedPlans(t *testing.T) { t.Setenv("TF_CLI_ARGS_plan", "-out=env.plan") - _, info, execCtx, _, _ := planFixture(t) - require.ErrorIs(t, validateEnvironmentPlanOutput(info, execCtx), errUtils.ErrPlanVerificationFailed) + config, info, execCtx, _, _ := planFixture(t) + require.ErrorIs(t, validateEnvironmentPlanOutput(config, info, execCtx), errUtils.ErrPlanVerificationFailed) info.DryRun = true - require.NoError(t, validateEnvironmentPlanOutput(info, execCtx)) + require.NoError(t, validateEnvironmentPlanOutput(config, info, execCtx)) info.DryRun = false info.SubCommand = "apply" - require.NoError(t, validateEnvironmentPlanOutput(info, execCtx)) + require.NoError(t, validateEnvironmentPlanOutput(config, info, execCtx)) info.SubCommand = "plan" execCtx.preparedConfiguration = nil - require.NoError(t, validateEnvironmentPlanOutput(info, execCtx)) + require.NoError(t, validateEnvironmentPlanOutput(config, info, execCtx)) } diff --git a/pkg/component/terraform/execute_hooks_process_test.go b/pkg/component/terraform/execute_hooks_process_test.go index 0432bcac624..d1c7ae1f83e 100644 --- a/pkg/component/terraform/execute_hooks_process_test.go +++ b/pkg/component/terraform/execute_hooks_process_test.go @@ -388,8 +388,10 @@ func TestCaptureFileReportsFailures(t *testing.T) { t.Setenv("TEMP", os.Getenv("TMPDIR")) file = capture.create() _, _ = file.Write([]byte("kept")) + // Close the capture before removing it: Windows cannot remove a file that is still open. + output := file.finish() require.NoError(t, os.Remove(file.path)) - _, err = file.finish().Open() + _, err = output.Open() require.ErrorIs(t, err, errUtils.ErrComponentOutputCapture, "a removed capture reports a read failure") capture.cleanup() } diff --git a/pkg/component/terraform/execute_hooks_test.go b/pkg/component/terraform/execute_hooks_test.go index 1358cacdc63..7140513eb22 100644 --- a/pkg/component/terraform/execute_hooks_test.go +++ b/pkg/component/terraform/execute_hooks_test.go @@ -12,6 +12,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/component" @@ -442,26 +443,15 @@ func TestUnknownHookFailureIsNotMistakenForSuccess(t *testing.T) { require.Equal(t, []string{"hook-b"}, shell.names()) } -// fakeConfigurationRuntime is a provider session that returns a fixed preparation. -type fakeConfigurationRuntime struct { - prepared *schema.PreparedComponentConfiguration -} - -func (fakeConfigurationRuntime) Discover(context.Context) (map[string]map[string]any, error) { - return nil, nil -} - -func (f fakeConfigurationRuntime) Prepare(context.Context, *schema.ConfigAndStacksInfo) (*schema.PreparedComponentConfiguration, error) { - return f.prepared, nil -} - func TestPrepareConfigurationHandsExecutionToThePipeline(t *testing.T) { executable, err := os.Executable() require.NoError(t, err) execution := &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{execHook("b", "plan")}} - config := &schema.AtmosConfiguration{ComponentConfigurations: fakeConfigurationRuntime{prepared: &schema.PreparedComponentConfiguration{ + runtime := NewMockComponentConfigurationRuntime(gomock.NewController(t)) + runtime.EXPECT().Prepare(gomock.Any(), gomock.Any()).Return(&schema.PreparedComponentConfiguration{ Source: "./module", BackendType: "local", Backend: map[string]any{}, Workspace: "default", Execution: execution, - }}} + }, nil) + config := &schema.AtmosConfiguration{ComponentConfigurations: runtime} info := &schema.ConfigAndStacksInfo{ SubCommand: "plan", Command: executable, ComponentSection: map[string]any{}, ComponentSettingsSection: map[string]any{"configuration": true}, diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index b577e736f3a..1d533f3725c 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -271,7 +271,7 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every // 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). - if err := validateEnvironmentPlanOutput(info, execCtx); err != nil { + if err := validateEnvironmentPlanOutput(atmosConfig, info, execCtx); err != nil { return err } allArgsAndFlags, restore, err := expandPolicyPlanArguments(info, execCtx, allArgsAndFlags, componentPath) @@ -303,7 +303,9 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every mainErr = e.recoverFromInitRequired(atmosConfig, info, execCtx, allArgsAndFlags, componentPath, uploadStatusFlag, mainErr, &execMetadataStdoutBuf, &execMetadataStderrBuf, append(slices.Clone(opts), captureArgs)...) } - if mainErr != nil { + // An explicit -detailed-exitcode plan reports changes with status 2 after writing the + // plan, so the reviewed plan is recorded before that status is returned. + if mainErr != nil && !isExplicitDetailedPlanChanges(info, allArgsAndFlags, mainErr) { return mainErr } // Policy plans run against a private artifact that is removed after the guard @@ -312,7 +314,15 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every if execCtx.planPolicy != nil && info.SubCommand == "plan" && !info.DryRun { effectiveArgs = allArgsAndFlags } - return recordConfigurationPlan(component.ContextFromOptions(opts...), atmosConfig, info, execCtx, effectiveArgs) + if err := recordConfigurationPlan(component.ContextFromOptions(opts...), atmosConfig, info, execCtx, effectiveArgs); err != nil { + return err + } + return mainErr +} + +// isExplicitDetailedPlanChanges reports plan's "changes present" status from -detailed-exitcode. +func isExplicitDetailedPlanChanges(info *schema.ConfigAndStacksInfo, args []string, err error) bool { + return info.SubCommand == subcommandPlan && slices.Contains(args, detailedExitCodeFlag) && resolveExitCode(err) == detailedExitCodeChangesDetected } // recoverFromInitRequired is smart init's plan/apply-time safety net: when the main command diff --git a/pkg/plugin/adapter/configuration/plan.go b/pkg/plugin/adapter/configuration/plan.go index d29b4d7ac21..3f2022e3832 100644 --- a/pkg/plugin/adapter/configuration/plan.go +++ b/pkg/plugin/adapter/configuration/plan.go @@ -5,6 +5,7 @@ import ( "crypto/sha256" "encoding/hex" "encoding/json" + "errors" "fmt" "io/fs" "os" @@ -67,7 +68,7 @@ func (s *Session) PlanIdentity(ctx context.Context, info *schema.ConfigAndStacks func sourceDigests(ctx context.Context, root string, sources []schema.ComponentConfigurationSource) (map[string]string, error) { root, err := filepath.EvalSymlinks(root) if err != nil { - return nil, err + return nil, sourceReadError(err) } result := map[string]string{} for _, source := range sources { @@ -93,7 +94,7 @@ func digestSourceDirectory(ctx context.Context, root, sourcePath string, result top := filepath.Join(root, filepath.FromSlash(sourcePath)) err := filepath.WalkDir(top, func(path string, entry fs.DirEntry, err error) error { if err != nil { - return err + return sourceReadError(err) } if entry.IsDir() { if path != top && excludedPlanSource(entry.Name()) { @@ -109,13 +110,18 @@ func digestSourceDirectory(ctx context.Context, root, sourcePath string, result } return digestSource(ctx, root, path, result) }) - if os.IsNotExist(err) { + if errors.Is(err, fs.ErrNotExist) { result[sourcePath] = "absent" return nil } return err } +// sourceReadError wraps a filesystem failure met while fingerprinting watched sources. +func sourceReadError(err error) error { + return fmt.Errorf("%w: fingerprint watched source: %w", errUtils.ErrReadFile, err) +} + // rejectDirectorySymlink fails clearly on a symlink to a directory inside a watched directory. // Atmos does not follow directory symlinks when it copies or binds sources, so the saved plan // cannot fingerprint what the link leads to. File symlinks and dangling links are left to @@ -159,7 +165,7 @@ func digestSource(ctx context.Context, root, path string, result map[string]stri return nil } if err != nil { - return err + return sourceReadError(err) } relative, err := filepath.Rel(root, real) if err != nil || !filepath.IsLocal(relative) { @@ -167,7 +173,7 @@ func digestSource(ctx context.Context, root, path string, result map[string]stri } data, err := os.ReadFile(real) if err != nil { - return err + return sourceReadError(err) } sum := sha256.Sum256(data) relative, err = filepath.Rel(root, path) diff --git a/pkg/plugin/adapter/configuration/plan_test.go b/pkg/plugin/adapter/configuration/plan_test.go index d9135c7f416..7c360622ca1 100644 --- a/pkg/plugin/adapter/configuration/plan_test.go +++ b/pkg/plugin/adapter/configuration/plan_test.go @@ -277,13 +277,14 @@ func TestSourceDigests(t *testing.T) { }) t.Run("missing project root fails", func(t *testing.T) { _, err := sourceDigests(t.Context(), filepath.Join(t.TempDir(), "gone"), nil) + require.ErrorIs(t, err, errUtils.ErrReadFile) require.ErrorIs(t, err, os.ErrNotExist) }) t.Run("directory watched as a file fails", func(t *testing.T) { root := t.TempDir() require.NoError(t, os.Mkdir(filepath.Join(root, "modules"), 0o700)) _, err := sourceDigests(t.Context(), root, []schema.ComponentConfigurationSource{{Path: "modules"}}) - require.Error(t, err) + require.ErrorIs(t, err, errUtils.ErrReadFile) }) t.Run("canceled context stops digesting", func(t *testing.T) { root := t.TempDir() diff --git a/pkg/terraform/output/configuration_state.go b/pkg/terraform/output/configuration_state.go index 6114a545120..f27b40b5b79 100644 --- a/pkg/terraform/output/configuration_state.go +++ b/pkg/terraform/output/configuration_state.go @@ -173,10 +173,7 @@ func configurationInitializedOutputConfig(config *ComponentConfig, state *schema WithHint("Run the command without --skip-init so Atmos can initialize a temporary state reader"). Err() } - dataDir := ".terraform" - if configured, ok := config.Env["TF_DATA_DIR"].(string); ok && configured != "" { - dataDir = configured - } + dataDir := configurationDataDir(config) if !filepath.IsAbs(dataDir) { dataDir = filepath.Join(state.WorkingDirectory, dataDir) } @@ -196,6 +193,17 @@ func configurationInitializedOutputConfig(config *ComponentConfig, state *schema return nil } +// configurationDataDir resolves TF_DATA_DIR from the environment the output subprocess receives, +// with SetupEnvironment's precedence: the authoritative base environment (or the inherited one +// when there is none), then the component env section. Terraform's default applies otherwise. +func configurationDataDir(config *ComponentConfig) string { + environment, err := (&defaultEnvironmentSetup{}).SetupEnvironment(&ComponentConfig{Env: config.Env, BaseEnvironment: config.BaseEnvironment}, nil) + if err == nil && environment["TF_DATA_DIR"] != "" { + return environment["TF_DATA_DIR"] + } + return ".terraform" +} + // remoteStateOverrides returns the remote-state settings that differ from the placeholder backend. // A remote state of another type shares nothing with the placeholder, so all of it is explicit. func remoteStateOverrides(remote map[string]any, remoteType, placeholderType string, sections map[string]any) map[string]any { diff --git a/pkg/terraform/output/configuration_state_test.go b/pkg/terraform/output/configuration_state_test.go index 06227917cf7..219946d053d 100644 --- a/pkg/terraform/output/configuration_state_test.go +++ b/pkg/terraform/output/configuration_state_test.go @@ -275,6 +275,39 @@ func TestPluginOutputSkipInitRejectsAnUninitializedUnit(t *testing.T) { } } +// TestConfigurationInitializedOutputConfigUsesEffectiveDataDir verifies that the initialized +// directory is located through the effective TF_DATA_DIR, with SetupEnvironment's precedence. +func TestConfigurationInitializedOutputConfigUsesEffectiveDataDir(t *testing.T) { + for _, test := range []struct { + name string + inherited string + base []string + env map[string]any + dataDir string + ok bool + }{ + {name: "base environment", base: []string{"TF_DATA_DIR=base-data"}, dataDir: "base-data", ok: true}, + {name: "inherited environment without base", inherited: "inherited-data", dataDir: "inherited-data", ok: true}, + {name: "base environment omits inherited value", inherited: "inherited-data", base: []string{"PATH=/bin"}, dataDir: "inherited-data"}, + {name: "component env overrides base", base: []string{"TF_DATA_DIR=base-data"}, env: map[string]any{"TF_DATA_DIR": "component-data"}, dataDir: "component-data", ok: true}, + {name: "default data directory", base: []string{}, dataDir: ".terraform", ok: true}, + } { + t.Run(test.name, func(t *testing.T) { + t.Setenv("TF_DATA_DIR", test.inherited) + unit := t.TempDir() + require.NoError(t, os.Mkdir(filepath.Join(unit, test.dataDir), 0o700)) + config := &ComponentConfig{BaseEnvironment: test.base, Env: test.env} + err := configurationInitializedOutputConfig(config, &schema.ComponentConfigurationState{Workspace: "prod", WorkingDirectory: unit}, "network", "live") + if !test.ok { + require.ErrorIs(t, err, errUtils.ErrTerraformOutputFailed) + return + } + require.NoError(t, err) + require.Equal(t, unit, config.ComponentPath) + }) + } +} + // TestPluginOutputDoesNotResolveTheExecutionWorkdir verifies that a plugin-owned state read // succeeds even when the component's execution workdir cannot be resolved. func TestPluginOutputDoesNotResolveTheExecutionWorkdir(t *testing.T) { diff --git a/plugins/terragrunt/internal/engine/backend.go b/plugins/terragrunt/internal/engine/backend.go index 177fffdc4f0..446fed8b3fb 100644 --- a/plugins/terragrunt/internal/engine/backend.go +++ b/plugins/terragrunt/internal/engine/backend.go @@ -1,7 +1,7 @@ package engine import ( - "fmt" + "errors" "github.com/gruntwork-io/terragrunt/pkg/atmosbridge" "github.com/gruntwork-io/terragrunt/pkg/config" @@ -9,12 +9,15 @@ import ( "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" ) +// ErrUnsupportedRemoteState identifies remote_state settings that the native adapter cannot reproduce. +var ErrUnsupportedRemoteState = errors.New("native adaptation of remote_state disable_init or encryption is not implemented") + func resolveBackend(cfg *config.TerragruntConfig, result *model.Resolution) error { if cfg.RemoteState == nil { return nil } if cfg.RemoteState.DisableInit || len(cfg.RemoteState.Encryption) > 0 { - return fmt.Errorf("native adaptation of remote_state disable_init or encryption is not implemented") + return ErrUnsupportedRemoteState } result.BackendType = cfg.RemoteState.BackendName result.Backend = atmosbridge.BackendConfig(cfg.RemoteState.Config) diff --git a/plugins/terragrunt/internal/engine/backend_test.go b/plugins/terragrunt/internal/engine/backend_test.go index 8454587cb21..e4fab0e4708 100644 --- a/plugins/terragrunt/internal/engine/backend_test.go +++ b/plugins/terragrunt/internal/engine/backend_test.go @@ -1,6 +1,7 @@ package engine import ( + "errors" "path/filepath" "testing" @@ -114,6 +115,7 @@ generate "duplicate" { t.Run(test.name, func(t *testing.T) { result, err := Resolve(t.Context(), nativeUnit(t, test.hcl)) require.ErrorContains(t, err, test.want) + require.Equal(t, test.name == "disable_init" || test.name == "encryption", errors.Is(err, ErrUnsupportedRemoteState)) require.Nil(t, result) }) } diff --git a/plugins/terragrunt/internal/engine/execution.go b/plugins/terragrunt/internal/engine/execution.go index 9dd59e3ac64..db81dcef94b 100644 --- a/plugins/terragrunt/internal/engine/execution.go +++ b/plugins/terragrunt/internal/engine/execution.go @@ -68,17 +68,20 @@ var commandFunctions = [][]byte{[]byte("get_terraform_command"), []byte("get_ter // Configuration that cannot observe the command evaluates identically, so it is not parsed twice. func applyTimeExecution(request model.Request, path string, planned *model.Execution, filesystem afero.Fs, read []string, parse func(*config.ParsingContext) (*config.TerragruntConfig, error), -) (*model.Execution, error) { +) (*model.Execution, []string, error) { if request.Command != "plan" || !observesCommand(filesystem, append(slices.Clone(read), path)) { - return planned, nil + return planned, nil, nil } - cfg, err := parse(parsingContext(path, request.Tofu, "apply", nil)) + apply := parsingContext(path, request.Tofu, "apply", nil) + cfg, err := parse(apply) if err != nil { - return nil, err + return nil, nil, err } + // Configuration read only when evaluating for apply also shapes the reviewed plan. + files := apply.FilesRead.Paths() applied, err := executionFromConfig(cfg.Terraform) if err != nil { - return nil, err + return nil, nil, err } if applied == nil { applied = &model.Execution{} @@ -87,10 +90,10 @@ func applyTimeExecution(request model.Request, path string, planned *model.Execu planned = &model.Execution{} } if reflect.DeepEqual(*applied, *planned) { - return nilIfEmpty(planned), nil + return nilIfEmpty(planned), files, nil } planned.Apply = applied - return planned, nil + return planned, files, nil } // observesCommand reports whether any configuration file the unit read can call a function diff --git a/plugins/terragrunt/internal/engine/execution_test.go b/plugins/terragrunt/internal/engine/execution_test.go index 88e36980858..638e385b17a 100644 --- a/plugins/terragrunt/internal/engine/execution_test.go +++ b/plugins/terragrunt/internal/engine/execution_test.go @@ -236,6 +236,27 @@ func TestPlanPreparationRecordsApplyTimeExecution(t *testing.T) { require.Equal(t, planned.Execution.Apply.AfterHooks, applied.Execution.AfterHooks) } +// Configuration that only the apply-time evaluation reads also shapes the reviewed plan, so the +// plan's watched sources include it. +func TestPlanPreparationWatchesApplyTimeReads(t *testing.T) { + request := nativeUnit(t, `locals { + hooks = read_terragrunt_config(get_terraform_command() == "apply" ? "apply-hooks.hcl" : "plan-hooks.hcl") +}`) + root := filepath.Dir(request.Path) + for _, name := range []string{"apply-hooks.hcl", "plan-hooks.hcl"} { + require.NoError(t, os.WriteFile(filepath.Join(root, name), []byte("locals {}\n"), 0o600)) + } + result, err := Resolve(t.Context(), request) + require.NoError(t, err) + var names []string + for _, file := range result.Files { + names = append(names, filepath.Base(file)) + } + require.Contains(t, names, "plan-hooks.hcl") + require.Contains(t, names, "apply-hooks.hcl", "apply-time reads must be watched by the saved plan") + require.Contains(t, names, "terragrunt.hcl") +} + func TestPlanPreparationRecordsThatApplyRunsNothing(t *testing.T) { result, err := Resolve(t.Context(), executionUnit(t, ` before_hook "plan_only" { diff --git a/plugins/terragrunt/internal/engine/generation_test.go b/plugins/terragrunt/internal/engine/generation_test.go index b5f4d0cc89a..f3fcbdc2ce6 100644 --- a/plugins/terragrunt/internal/engine/generation_test.go +++ b/plugins/terragrunt/internal/engine/generation_test.go @@ -96,6 +96,7 @@ func TestPreventDestroyUsesCommandAndBooleanArguments(t *testing.T) { request.Command, request.Arguments = test.command, test.args _, err := Resolve(t.Context(), request) if test.blocked { + require.ErrorIs(t, err, ErrPreventDestroy) require.ErrorContains(t, err, "protected by prevent_destroy") } else { require.NoError(t, err) @@ -109,7 +110,7 @@ func TestPreventDestroyRejectsBeforeInputEffects(t *testing.T) { inputs = { value = run_cmd("must-not-execute") }`) request.Command = "destroy" _, err := Resolve(t.Context(), request) - require.ErrorContains(t, err, "protected by prevent_destroy") + require.ErrorIs(t, err, ErrPreventDestroy) require.NotContains(t, err.Error(), "must-not-execute") } diff --git a/plugins/terragrunt/internal/engine/resolve.go b/plugins/terragrunt/internal/engine/resolve.go index a9e693bb664..d097f8dd886 100644 --- a/plugins/terragrunt/internal/engine/resolve.go +++ b/plugins/terragrunt/internal/engine/resolve.go @@ -3,6 +3,7 @@ package engine import ( "context" + "errors" "fmt" "io" "os" @@ -24,6 +25,9 @@ func Resolve(ctx context.Context, request model.Request) (*model.Resolution, err return resolve(ctx, request, nil) } +// ErrPreventDestroy rejects a destroy operation on a unit that sets prevent_destroy. +var ErrPreventDestroy = errors.New("unit is protected by prevent_destroy") + func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (*model.Resolution, error) { if request.Phase == "inventory" { return discoverProject(ctx, request) @@ -177,7 +181,7 @@ func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (* return nil, err } if guard.PreventDestroy != nil && *guard.PreventDestroy { - return nil, fmt.Errorf("unit is protected by prevent_destroy") + return nil, ErrPreventDestroy } } var cfg *config.TerragruntConfig @@ -203,6 +207,7 @@ func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (* // Hooks and extra arguments are returned as data for the host to run; evaluation never // executes them. Backend identity is read from a partial parse that does not decode them. var execution *model.Execution + var applyFiles []string if request.Phase != "backend" { if execution, err = executionFromConfig(cfg.Terraform); err != nil { return nil, err @@ -210,17 +215,17 @@ func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (* reparse := func(apply *config.ParsingContext) (*config.TerragruntConfig, error) { return config.ParseConfigFile(ctx, logger, environment, apply, path, nil) } - if execution, err = applyTimeExecution(request, path, execution, environment.FS, pctx.FilesRead.Paths(), reparse); err != nil { + if execution, applyFiles, err = applyTimeExecution(request, path, execution, environment.FS, pctx.FilesRead.Paths(), reparse); err != nil { return nil, err } } if cfg.PreventDestroy != nil && *cfg.PreventDestroy && atmosbridge.IsDestroyOperation(request.Command, request.Arguments) { - return nil, fmt.Errorf("unit is protected by prevent_destroy") + return nil, ErrPreventDestroy } if cfg.Errors != nil || cfg.Exclude != nil || len(cfg.FeatureFlags) > 0 || cfg.IamRole != "" || cfg.IamWebIdentityToken != "" { return nil, fmt.Errorf("native adaptation of errors, exclude, feature flags, or IAM settings is not implemented") } - result := &model.Resolution{Execution: execution, Path: path, Inputs: cfg.Inputs, Source: *cfg.Terraform.Source, Generated: map[string]string{}, Files: append(pctx.FilesRead.Paths(), path)} + result := &model.Resolution{Execution: execution, Path: path, Inputs: cfg.Inputs, Source: *cfg.Terraform.Source, Generated: map[string]string{}, Files: append(append(pctx.FilesRead.Paths(), applyFiles...), path)} if plainLocalSource(result.Source) { result.Source = localPath(filepath.Dir(path), result.Source) if info, err := os.Stat(result.Source); err != nil || !info.IsDir() { From 8921b1bd39803b26e095bd01ebfd635cd4df0560 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 13 Nov 2026 07:42:46 -0600 Subject: [PATCH 30/52] test(execution): drive lifecycle tests through a generated Runner mock Replace the recorder's handwritten Runner implementation with a mockgen MockRunner (go:generate in lifecycle.go). The recorder keeps its call timeline, output, failure, and cancellation behavior and feeds the mock through DoAndReturn. Fold the cancellation tests into lifecycle_test.go unchanged so the generated file does not grow the PR file count. Co-Authored-By: Claude Opus 5.5 --- pkg/component/execution/lifecycle.go | 2 + .../execution/lifecycle_cancellation_test.go | 89 --------------- pkg/component/execution/lifecycle_test.go | 104 ++++++++++++++++-- pkg/component/execution/mock_runner_test.go | 57 ++++++++++ 4 files changed, 155 insertions(+), 97 deletions(-) delete mode 100644 pkg/component/execution/lifecycle_cancellation_test.go create mode 100644 pkg/component/execution/mock_runner_test.go diff --git a/pkg/component/execution/lifecycle.go b/pkg/component/execution/lifecycle.go index d5a34ab68e8..1ffe85979b7 100644 --- a/pkg/component/execution/lifecycle.go +++ b/pkg/component/execution/lifecycle.go @@ -14,6 +14,8 @@ import ( "github.com/cloudposse/atmos/pkg/schema" ) +//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -destination=mock_runner_test.go -package=execution . Runner + // Runner executes one hook and reports its failure and captured output. // The output feeds error hook matching and is never printed again. A nil output means // nothing was captured. diff --git a/pkg/component/execution/lifecycle_cancellation_test.go b/pkg/component/execution/lifecycle_cancellation_test.go deleted file mode 100644 index b974f9d2947..00000000000 --- a/pkg/component/execution/lifecycle_cancellation_test.go +++ /dev/null @@ -1,89 +0,0 @@ -package execution - -import ( - "context" - "errors" - "testing" - - "github.com/stretchr/testify/require" - - "github.com/cloudposse/atmos/pkg/schema" -) - -func errorHook(name string) schema.ComponentHook { - return schema.ComponentHook{Name: name, Commands: []string{"plan"}, OnErrors: []string{".*"}, Execute: []string{"tool", name}} -} - -func TestRunCancellationDuringActionIsReported(t *testing.T) { - failure := errors.New("exit status 1") - for _, test := range []struct { - name string - actionErr error - wantFailure bool - }{ - {"successful action", nil, false}, - {"failed action", failure, true}, - } { - t.Run(test.name, func(t *testing.T) { - ctx, cancel := context.WithCancel(t.Context()) - r := &recorder{} - config := &schema.ComponentExecution{ - BeforeHooks: []schema.ComponentHook{hook("b", "plan")}, - AfterHooks: []schema.ComponentHook{{Name: "a", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, - ErrorHooks: []schema.ComponentHook{errorHook("e")}, - } - err := Run(ctx, config, "plan", r, func() (Output, error) { - r.calls = append(r.calls, "action") - cancel() - return nil, test.actionErr - }) - require.ErrorIs(t, err, context.Canceled) - require.Equal(t, test.wantFailure, errors.Is(err, failure), "the action's own failure must stay visible") - require.Equal(t, []string{"before_hook:b", "action"}, r.calls, "no later hook may run after cancellation") - }) - } -} - -func TestRunCancellationDuringAfterHookSkipsErrorHooks(t *testing.T) { - ctx, cancel := context.WithCancel(t.Context()) - failure := errors.New("exit status 2") - r := &recorder{failures: map[string]error{"action": failure}, onCall: func(name string) { - if name == "after_hook:a" { - cancel() - } - }} - config := &schema.ComponentExecution{ - AfterHooks: []schema.ComponentHook{{Name: "a", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, - ErrorHooks: []schema.ComponentHook{errorHook("e")}, - } - err := Run(ctx, config, "plan", r, r.action) - require.ErrorIs(t, err, context.Canceled) - require.ErrorIs(t, err, failure) - require.Equal(t, []string{"action", "after_hook:a"}, r.calls) -} - -func TestRunCancellationBetweenErrorHooksStopsRemainingHooks(t *testing.T) { - ctx, cancel := context.WithCancel(t.Context()) - failure := errors.New("exit status 3") - r := &recorder{failures: map[string]error{"action": failure}, onCall: func(name string) { - if name == "error_hook:e1" { - cancel() - } - }} - config := &schema.ComponentExecution{ErrorHooks: []schema.ComponentHook{errorHook("e1"), errorHook("e2")}} - err := Run(ctx, config, "plan", r, r.action) - require.ErrorIs(t, err, context.Canceled) - require.ErrorIs(t, err, failure) - require.Equal(t, []string{"action", "error_hook:e1"}, r.calls, "the second error hook must not start once canceled") -} - -func TestJoinFailuresKeepsLoneErrorIntact(t *testing.T) { - lone := errors.New("only") - require.Same(t, lone, joinFailures([]failure{{err: lone}}, nil)) - require.Same(t, lone, joinFailures([]failure{{err: lone}}, lone), "an already-recorded cause is not duplicated") - require.NoError(t, joinFailures(nil, nil)) - other := errors.New("other") - joined := joinFailures([]failure{{err: lone}}, other) - require.ErrorIs(t, joined, lone) - require.ErrorIs(t, joined, other) -} diff --git a/pkg/component/execution/lifecycle_test.go b/pkg/component/execution/lifecycle_test.go index 7f778e5fb83..52379687b0b 100644 --- a/pkg/component/execution/lifecycle_test.go +++ b/pkg/component/execution/lifecycle_test.go @@ -9,12 +9,14 @@ import ( "testing" "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/schema" ) -// recorder implements Runner and records every hook and the action in call order. +// recorder records every hook and the action in call order. Tests drive it through the +// generated MockRunner returned by runner. type recorder struct { calls []string // failures maps a hook name (or "action") to the error it returns. @@ -25,7 +27,15 @@ type recorder struct { onCall func(name string) } -func (r *recorder) RunHook(_ context.Context, kind HookKind, hook *schema.ComponentHook) (Output, error) { +// runner returns a generated Runner mock whose RunHook calls feed the recorder. +func (r *recorder) runner(t *testing.T) Runner { + t.Helper() + mock := NewMockRunner(gomock.NewController(t)) + mock.EXPECT().RunHook(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(r.runHook).AnyTimes() + return mock +} + +func (r *recorder) runHook(_ context.Context, kind HookKind, hook *schema.ComponentHook) (Output, error) { name := string(kind) + ":" + hook.Name r.calls = append(r.calls, name) if r.onCall != nil { @@ -48,7 +58,7 @@ func hook(name string, commands ...string) schema.ComponentHook { func runLifecycle(t *testing.T, config *schema.ComponentExecution, command string, r *recorder) error { t.Helper() - return Run(t.Context(), config, command, r, r.action) + return Run(t.Context(), config, command, r.runner(t), r.action) } func TestRunOrdersBeforeActionAfter(t *testing.T) { @@ -221,7 +231,7 @@ func TestRunStopsWhenCanceled(t *testing.T) { AfterHooks: []schema.ComponentHook{{Name: "a", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"plan"}, OnErrors: []string{".*"}, Execute: []string{"tool"}}}, } - err := Run(ctx, config, "plan", r, r.action) + err := Run(ctx, config, "plan", r.runner(t), r.action) require.ErrorIs(t, err, context.Canceled) require.Equal(t, []string{"before_hook:b1"}, r.calls) } @@ -230,7 +240,7 @@ func TestRunDoesNotStartWhenAlreadyCanceled(t *testing.T) { ctx, cancel := context.WithCancel(t.Context()) cancel() r := &recorder{} - err := Run(ctx, &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{hook("b", "plan")}}, "plan", r, r.action) + err := Run(ctx, &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{hook("b", "plan")}}, "plan", r.runner(t), r.action) require.ErrorIs(t, err, context.Canceled) require.Empty(t, r.calls) } @@ -243,7 +253,7 @@ func TestRunDoesNotStartActionWhenLastBeforeHookIsCanceled(t *testing.T) { } }} config := &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{hook("first", "plan"), hook("last", "plan")}} - err := Run(ctx, config, "plan", r, r.action) + err := Run(ctx, config, "plan", r.runner(t), r.action) require.ErrorIs(t, err, context.Canceled) require.Equal(t, []string{"before_hook:first", "before_hook:last"}, r.calls) } @@ -253,7 +263,7 @@ func TestRunWithoutHooksDoesNotStartWhenAlreadyCanceled(t *testing.T) { cancel() for _, config := range []*schema.ComponentExecution{nil, {}} { r := &recorder{} - err := Run(ctx, config, "plan", r, r.action) + err := Run(ctx, config, "plan", r.runner(t), r.action) require.ErrorIs(t, err, context.Canceled) require.Empty(t, r.calls) } @@ -344,7 +354,7 @@ func TestErrorHooksStreamCapturedOutput(t *testing.T) { t.Run(test.name, func(t *testing.T) { r := &recorder{} config := &schema.ComponentExecution{ErrorHooks: []schema.ComponentHook{{Name: "e", Commands: []string{"plan"}, OnErrors: []string{test.pattern}, Execute: []string{"tool"}}}} - err := Run(t.Context(), config, "plan", r, func() (Output, error) { + err := Run(t.Context(), config, "plan", r.runner(t), func() (Output, error) { r.calls = append(r.calls, "action") return test.output, failed }) @@ -359,3 +369,81 @@ func TestErrorHooksStreamCapturedOutput(t *testing.T) { }) } } + +func errorHook(name string) schema.ComponentHook { + return schema.ComponentHook{Name: name, Commands: []string{"plan"}, OnErrors: []string{".*"}, Execute: []string{"tool", name}} +} + +func TestRunCancellationDuringActionIsReported(t *testing.T) { + failure := errors.New("exit status 1") + for _, test := range []struct { + name string + actionErr error + wantFailure bool + }{ + {"successful action", nil, false}, + {"failed action", failure, true}, + } { + t.Run(test.name, func(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + r := &recorder{} + config := &schema.ComponentExecution{ + BeforeHooks: []schema.ComponentHook{hook("b", "plan")}, + AfterHooks: []schema.ComponentHook{{Name: "a", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, + ErrorHooks: []schema.ComponentHook{errorHook("e")}, + } + err := Run(ctx, config, "plan", r.runner(t), func() (Output, error) { + r.calls = append(r.calls, "action") + cancel() + return nil, test.actionErr + }) + require.ErrorIs(t, err, context.Canceled) + require.Equal(t, test.wantFailure, errors.Is(err, failure), "the action's own failure must stay visible") + require.Equal(t, []string{"before_hook:b", "action"}, r.calls, "no later hook may run after cancellation") + }) + } +} + +func TestRunCancellationDuringAfterHookSkipsErrorHooks(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + failure := errors.New("exit status 2") + r := &recorder{failures: map[string]error{"action": failure}, onCall: func(name string) { + if name == "after_hook:a" { + cancel() + } + }} + config := &schema.ComponentExecution{ + AfterHooks: []schema.ComponentHook{{Name: "a", Commands: []string{"plan"}, Execute: []string{"tool"}, RunOnError: true}}, + ErrorHooks: []schema.ComponentHook{errorHook("e")}, + } + err := Run(ctx, config, "plan", r.runner(t), r.action) + require.ErrorIs(t, err, context.Canceled) + require.ErrorIs(t, err, failure) + require.Equal(t, []string{"action", "after_hook:a"}, r.calls) +} + +func TestRunCancellationBetweenErrorHooksStopsRemainingHooks(t *testing.T) { + ctx, cancel := context.WithCancel(t.Context()) + failure := errors.New("exit status 3") + r := &recorder{failures: map[string]error{"action": failure}, onCall: func(name string) { + if name == "error_hook:e1" { + cancel() + } + }} + config := &schema.ComponentExecution{ErrorHooks: []schema.ComponentHook{errorHook("e1"), errorHook("e2")}} + err := Run(ctx, config, "plan", r.runner(t), r.action) + require.ErrorIs(t, err, context.Canceled) + require.ErrorIs(t, err, failure) + require.Equal(t, []string{"action", "error_hook:e1"}, r.calls, "the second error hook must not start once canceled") +} + +func TestJoinFailuresKeepsLoneErrorIntact(t *testing.T) { + lone := errors.New("only") + require.Same(t, lone, joinFailures([]failure{{err: lone}}, nil)) + require.Same(t, lone, joinFailures([]failure{{err: lone}}, lone), "an already-recorded cause is not duplicated") + require.NoError(t, joinFailures(nil, nil)) + other := errors.New("other") + joined := joinFailures([]failure{{err: lone}}, other) + require.ErrorIs(t, joined, lone) + require.ErrorIs(t, joined, other) +} diff --git a/pkg/component/execution/mock_runner_test.go b/pkg/component/execution/mock_runner_test.go new file mode 100644 index 00000000000..0e51cf554e0 --- /dev/null +++ b/pkg/component/execution/mock_runner_test.go @@ -0,0 +1,57 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/cloudposse/atmos/pkg/component/execution (interfaces: Runner) +// +// Generated by this command: +// +// mockgen -destination=mock_runner_test.go -package=execution . Runner +// + +// Package execution is a generated GoMock package. +package execution + +import ( + context "context" + reflect "reflect" + + schema "github.com/cloudposse/atmos/pkg/schema" + gomock "go.uber.org/mock/gomock" +) + +// MockRunner is a mock of Runner interface. +type MockRunner struct { + ctrl *gomock.Controller + recorder *MockRunnerMockRecorder + isgomock struct{} +} + +// MockRunnerMockRecorder is the mock recorder for MockRunner. +type MockRunnerMockRecorder struct { + mock *MockRunner +} + +// NewMockRunner creates a new mock instance. +func NewMockRunner(ctrl *gomock.Controller) *MockRunner { + mock := &MockRunner{ctrl: ctrl} + mock.recorder = &MockRunnerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockRunner) EXPECT() *MockRunnerMockRecorder { + return m.recorder +} + +// RunHook mocks base method. +func (m *MockRunner) RunHook(ctx context.Context, kind HookKind, hook *schema.ComponentHook) (Output, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "RunHook", ctx, kind, hook) + ret0, _ := ret[0].(Output) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// RunHook indicates an expected call of RunHook. +func (mr *MockRunnerMockRecorder) RunHook(ctx, kind, hook any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "RunHook", reflect.TypeOf((*MockRunner)(nil).RunHook), ctx, kind, hook) +} From d061c170fa0ea37d0f124514bd8b93754fe1c341 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 13 Nov 2026 07:51:33 -0600 Subject: [PATCH 31/52] test(terraform): use generated mocks for the reviewed-plan runtime Generate MockReviewedPlanRuntime (prepare, plan identity, and state), MockPlanIdentityRuntime, and MockPlanShowRunner with mockgen source mode (go:generate in configuration_plan_test.go). These replace the handwritten planRuntimeFixture methods, reviewedPlanRuntime, identityOnlyRuntime, stateFailureRuntime, and planShowRunner. planRuntimeFixture keeps only the shared response data, and the mock reads it at call time. Fold state_configuration_test.go into configuration_test.go unchanged so the generated file does not grow the PR file count. Co-Authored-By: Claude Opus 5.5 --- .../configuration_plan_failures_test.go | 34 ++- .../configuration_plan_pipeline_test.go | 24 +- .../terraform/configuration_plan_test.go | 76 +++++-- pkg/component/terraform/configuration_test.go | 32 +++ pkg/component/terraform/execute_hooks_test.go | 2 +- .../terraform/mock_configuration_test.go | 2 +- .../terraform/mock_plan_runtime_test.go | 210 ++++++++++++++++++ .../terraform/state_configuration_test.go | 41 ---- 8 files changed, 326 insertions(+), 95 deletions(-) create mode 100644 pkg/component/terraform/mock_plan_runtime_test.go delete mode 100644 pkg/component/terraform/state_configuration_test.go diff --git a/pkg/component/terraform/configuration_plan_failures_test.go b/pkg/component/terraform/configuration_plan_failures_test.go index 516589b25a8..1de907b496e 100644 --- a/pkg/component/terraform/configuration_plan_failures_test.go +++ b/pkg/component/terraform/configuration_plan_failures_test.go @@ -1,7 +1,6 @@ package terraform import ( - "context" "errors" "os" "path/filepath" @@ -9,28 +8,27 @@ import ( "testing" "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/schema" ) -// identityOnlyRuntime can bind plans but cannot resolve backend state. -type identityOnlyRuntime struct { - schema.ComponentConfigurationRuntime -} - -func (identityOnlyRuntime) PlanIdentity(context.Context, *schema.ConfigAndStacksInfo, []schema.ComponentConfigurationSource) (string, error) { - return "provider-source-options", nil -} - -// stateFailureRuntime accepts the plan identity but fails to resolve the backend. -type stateFailureRuntime struct { - *planRuntimeFixture - stateErr error +// identityOnlyRuntime returns a generated runtime that can bind plans but cannot resolve backend state. +func identityOnlyRuntime(t *testing.T) *MockPlanIdentityRuntime { + t.Helper() + runtime := NewMockPlanIdentityRuntime(gomock.NewController(t)) + runtime.EXPECT().PlanIdentity(gomock.Any(), gomock.Any(), gomock.Any()).Return("provider-source-options", nil).AnyTimes() + return runtime } -func (r stateFailureRuntime) ResolveState(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { - return nil, r.stateErr +// stateFailureRuntime returns a generated runtime that accepts the plan identity but fails to resolve the backend. +func stateFailureRuntime(t *testing.T, fixture *planRuntimeFixture, stateErr error) *MockReviewedPlanRuntime { + t.Helper() + runtime := NewMockReviewedPlanRuntime(gomock.NewController(t)) + runtime.EXPECT().PlanIdentity(gomock.Any(), gomock.Any(), gomock.Any()).Return(fixture.identity, fixture.err).AnyTimes() + runtime.EXPECT().ResolveState(gomock.Any(), gomock.Any()).Return(nil, stateErr).AnyTimes() + return runtime } // recordedPlan records a plan companion and prepares the invocation for a direct apply. @@ -54,7 +52,7 @@ func TestRestoreConfigurationPlanRequiresPlanBindingRuntime(t *testing.T) { func TestRestoreConfigurationPlanRequiresStateRuntime(t *testing.T) { config, info, _, _, _ := recordedPlan(t) - config.ComponentConfigurations = identityOnlyRuntime{} + config.ComponentConfigurations = identityOnlyRuntime(t) prepared, err := restoreConfigurationPlan(t.Context(), config, info) require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension, "a provider that cannot prove the backend is unchanged must not apply a saved plan") require.Nil(t, prepared) @@ -64,7 +62,7 @@ func TestRestoreConfigurationPlanBackendResolution(t *testing.T) { failure := errors.New("backend unreachable") t.Run("resolution error is propagated", func(t *testing.T) { config, info, _, runtime, _ := recordedPlan(t) - config.ComponentConfigurations = stateFailureRuntime{planRuntimeFixture: runtime, stateErr: failure} + config.ComponentConfigurations = stateFailureRuntime(t, runtime, failure) _, err := restoreConfigurationPlan(t.Context(), config, info) require.ErrorIs(t, err, failure) require.NotErrorIs(t, err, errUtils.ErrPlanVerificationFailed, "an unreachable backend is not a verification mismatch") diff --git a/pkg/component/terraform/configuration_plan_pipeline_test.go b/pkg/component/terraform/configuration_plan_pipeline_test.go index 71289720b0e..569972ed940 100644 --- a/pkg/component/terraform/configuration_plan_pipeline_test.go +++ b/pkg/component/terraform/configuration_plan_pipeline_test.go @@ -1,7 +1,6 @@ package terraform import ( - "context" "os" "path/filepath" "strings" @@ -15,25 +14,11 @@ import ( "github.com/cloudposse/atmos/pkg/schema" ) -// reviewedPlanRuntime is a configuration provider that can prepare, bind plans, and resolve state. -type reviewedPlanRuntime struct { - *MockComponentConfigurationRuntime - state *schema.ComponentConfigurationState -} - -func (reviewedPlanRuntime) PlanIdentity(context.Context, *schema.ConfigAndStacksInfo, []schema.ComponentConfigurationSource) (string, error) { - return "provider-source-options", nil -} - -func (r reviewedPlanRuntime) ResolveState(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { - return r.state, nil -} - // reviewedPlanProject is an isolated workdir-enabled component whose module can change after review. type reviewedPlanProject struct { root, module, workdir, state string config *schema.AtmosConfiguration - provider *MockComponentConfigurationRuntime + provider *MockReviewedPlanRuntime } func newReviewedPlanProject(t *testing.T) *reviewedPlanProject { @@ -44,9 +29,12 @@ func newReviewedPlanProject(t *testing.T) *reviewedPlanProject { require.NoError(t, os.WriteFile(filepath.Join(project.module, "main.tf"), []byte("# reviewed module"), 0o644)) project.config = &schema.AtmosConfiguration{BasePath: root, BasePathAbsolute: root, TerraformDirAbsolutePath: filepath.Join(root, "components")} project.config.Components.Terraform.BasePath = "components" - project.provider = NewMockComponentConfigurationRuntime(gomock.NewController(t)) + // The provider is a generated runtime that can prepare, bind plans, and resolve state. + project.provider = NewMockReviewedPlanRuntime(gomock.NewController(t)) state := &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{"path": project.state}, Workspace: "default"} - project.config.ComponentConfigurations = reviewedPlanRuntime{MockComponentConfigurationRuntime: project.provider, state: state} + project.provider.EXPECT().PlanIdentity(gomock.Any(), gomock.Any(), gomock.Any()).Return("provider-source-options", nil).AnyTimes() + project.provider.EXPECT().ResolveState(gomock.Any(), gomock.Any()).Return(state, nil).AnyTimes() + project.config.ComponentConfigurations = project.provider return project } diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index 930d012ee78..1df73acc2c8 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -15,21 +15,60 @@ import ( "github.com/cloudposse/atmos/pkg/process" "github.com/cloudposse/atmos/pkg/schema" "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" ) -type planRuntimeFixture struct { +//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=mock_plan_runtime_test.go -package=$GOPACKAGE -mock_names=reviewedPlanRuntime=MockReviewedPlanRuntime,planIdentityRuntime=MockPlanIdentityRuntime,planShowRunner=MockPlanShowRunner + +// reviewedPlanRuntime is a configuration runtime that prepares, binds saved plans, and resolves state. +type reviewedPlanRuntime interface { + schema.ComponentConfigurationRuntime + schema.ComponentConfigurationPlanRuntime + schema.ComponentConfigurationStateRuntime +} + +// planIdentityRuntime can bind plans but cannot resolve backend state. +type planIdentityRuntime interface { schema.ComponentConfigurationRuntime + schema.ComponentConfigurationPlanRuntime +} + +// planShowRunner runs the plan show command for plan policy evaluation. +type planShowRunner interface { + process.Runner +} + +// The generated mocks implement the contracts that the code under test asserts. +var ( + _ reviewedPlanRuntime = (*MockReviewedPlanRuntime)(nil) + _ planIdentityRuntime = (*MockPlanIdentityRuntime)(nil) + _ planShowRunner = (*MockPlanShowRunner)(nil) +) + +// planRuntimeFixture holds the responses that the generated plan runtime mock returns. +// Tests change its fields to simulate a different provider identity, backend, or failure. +type planRuntimeFixture struct { identity string state *schema.ComponentConfigurationState err error } -func (p *planRuntimeFixture) PlanIdentity(context.Context, *schema.ConfigAndStacksInfo, []schema.ComponentConfigurationSource) (string, error) { - return p.identity, p.err -} - -func (p *planRuntimeFixture) ResolveState(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { - return p.state, p.err +// mock returns a generated plan runtime that answers PlanIdentity and ResolveState from the +// fixture at call time. Discover and Prepare have no expectations, so gomock rejects them. +func (p *planRuntimeFixture) mock(t *testing.T) *MockReviewedPlanRuntime { + t.Helper() + runtime := NewMockReviewedPlanRuntime(gomock.NewController(t)) + runtime.EXPECT().PlanIdentity(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( + func(context.Context, *schema.ConfigAndStacksInfo, []schema.ComponentConfigurationSource) (string, error) { + return p.identity, p.err + }, + ).AnyTimes() + runtime.EXPECT().ResolveState(gomock.Any(), gomock.Any()).DoAndReturn( + func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return p.state, p.err + }, + ).AnyTimes() + return runtime } func planFixture(t *testing.T) (*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, *componentExecContext, *planRuntimeFixture, string) { @@ -47,7 +86,7 @@ func planFixture(t *testing.T) (*schema.AtmosConfiguration, *schema.ConfigAndSta require.NoError(t, os.WriteFile(path, []byte("reviewed plan"), 0o600)) prepared := &schema.PreparedComponentConfiguration{Engine: "terraform", Source: root, Inputs: map[string]any{"value": "reviewed"}, BackendType: "local", Backend: map[string]any{"path": filepath.Join(root, "state")}, Workspace: "default"} runtime := &planRuntimeFixture{identity: "provider-source-options", state: &schema.ComponentConfigurationState{BackendType: prepared.BackendType, Backend: prepared.Backend, Workspace: prepared.Workspace}} - config.ComponentConfigurations = runtime + config.ComponentConfigurations = runtime.mock(t) execCtx := &componentExecContext{componentPath: working, workingDir: working, preparedConfiguration: prepared} return config, info, execCtx, runtime, path } @@ -136,7 +175,7 @@ func TestConfigurationPlanRecordingGuards(t *testing.T) { info.SubCommand = "plan" config.ComponentConfigurations = nil require.ErrorIs(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"-out", path}), errUtils.ErrPluginUnsupportedExtension) - config.ComponentConfigurations = runtime + config.ComponentConfigurations = runtime.mock(t) runtime.err = context.Canceled require.ErrorIs(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"-out", path}), context.Canceled) runtime.err = nil @@ -299,19 +338,24 @@ func TestDetailedExitCodePlanRecordsReviewedPlan(t *testing.T) { } } -type planShowRunner struct{} - -//nolint:gocritic // The process.Runner interface fixes the value parameter. -func (planShowRunner) Run(_ context.Context, spec process.TaskSpec) process.Result { - _, _ = io.WriteString(spec.Streams.Stdout, `{"format_version":"1.2","planned_values":{}}`) - return process.Result{Started: true} +// newPlanShowRunner returns a generated runner whose plan show prints an empty JSON plan. +func newPlanShowRunner(t *testing.T) process.Runner { + t.Helper() + runner := NewMockPlanShowRunner(gomock.NewController(t)) + runner.EXPECT().Run(gomock.Any(), gomock.Any()).DoAndReturn( + func(_ context.Context, spec process.TaskSpec) process.Result { + _, _ = io.WriteString(spec.Streams.Stdout, `{"format_version":"1.2","planned_values":{}}`) + return process.Result{Started: true} + }, + ).AnyTimes() + return runner } func TestPolicyPlanBindsPublishedOutput(t *testing.T) { config, info, execCtx, _, _ := planFixture(t) var engine policy.Engine require.NoError(t, engine.Register(policy.CELEngine, &policy.CEL{})) - execCtx.planPolicy = &planfile.Guard{Engine: &engine, Show: planShowRunner{}, Rules: []policy.Rule{{ID: "allow", On: policy.TerraformPlan, Engine: policy.CELEngine, Expression: "false", Effects: policy.Effects{Deny: true}}}} + execCtx.planPolicy = &planfile.Guard{Engine: &engine, Show: newPlanShowRunner(t), Rules: []policy.Rule{{ID: "allow", On: policy.TerraformPlan, Engine: policy.CELEngine, Expression: "false", Effects: policy.Effects{Deny: true}}}} shell := &fakeShell{run: func(call *shellCall) error { path := plannedOutputPath(execCtx.componentPath, call.args) return os.WriteFile(path, []byte("reviewed"), 0o600) diff --git a/pkg/component/terraform/configuration_test.go b/pkg/component/terraform/configuration_test.go index d6715ed9b4f..437f3ec3f5d 100644 --- a/pkg/component/terraform/configuration_test.go +++ b/pkg/component/terraform/configuration_test.go @@ -1,14 +1,18 @@ package terraform import ( + "context" + "errors" "os" "path/filepath" "testing" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" errUtils "github.com/cloudposse/atmos/errors" + componentpkg "github.com/cloudposse/atmos/pkg/component" atmosio "github.com/cloudposse/atmos/pkg/io" "github.com/cloudposse/atmos/pkg/schema" ) @@ -188,3 +192,31 @@ func TestPreparedConfigurationSensitiveInputsNeverHitDisk(t *testing.T) { require.ElementsMatch(t, []string{`TF_VAR_credentials={"secret-input-key":""}`, `TF_VAR_empty={}`, `TF_VAR_null=null`}, env) } } + +// failingStateRuntime owns the component but cannot identify its backend. +type failingStateRuntime struct { + schema.ComponentConfigurationRuntime + err error +} + +func (r failingStateRuntime) ResolveState(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return nil, r.err +} + +func TestGetTerraformStateStopsWhenProviderCannotResolveBackend(t *testing.T) { + failure := errors.New("provider could not locate state") + executor := testExecutor(t) + executor.services.ExecuteDescribeComponent = func(params *componentpkg.ExecuteDescribeComponentParams) (map[string]any, error) { + assert.Equal(t, "network", params.Component) + assert.Equal(t, "live", params.Stack) + return map[string]any{ + "settings": map[string]any{"configuration": true}, + "backend": map[string]any{"path": "must-not-be-read"}, + }, nil + } + config := &schema.AtmosConfiguration{ComponentConfigurations: failingStateRuntime{err: failure}} + + result, err := executor.GetTerraformState(config, "!terraform.state", "live", "network", "vpc_id", true, nil, nil) + require.ErrorIs(t, err, failure, "the provider's failure is returned as is, not retried against a guessed backend") + require.Nil(t, result) +} diff --git a/pkg/component/terraform/execute_hooks_test.go b/pkg/component/terraform/execute_hooks_test.go index 7140513eb22..3e43dfbc18f 100644 --- a/pkg/component/terraform/execute_hooks_test.go +++ b/pkg/component/terraform/execute_hooks_test.go @@ -525,7 +525,7 @@ func TestPolicyPlanUsesProviderOutputArguments(t *testing.T) { config, info, execCtx, _, _ := planFixture(t) var engine policy.Engine require.NoError(t, engine.Register(policy.CELEngine, &policy.CEL{})) - execCtx.planPolicy = &planfile.Guard{Engine: &engine, Show: planShowRunner{}, Rules: []policy.Rule{{ID: "allow", On: policy.TerraformPlan, Engine: policy.CELEngine, Expression: "false", Effects: policy.Effects{Deny: true}}}} + execCtx.planPolicy = &planfile.Guard{Engine: &engine, Show: newPlanShowRunner(t), Rules: []policy.Rule{{ID: "allow", On: policy.TerraformPlan, Engine: policy.CELEngine, Expression: "false", Effects: policy.Effects{Deny: true}}}} var original *schema.ComponentExecution if test.provider != nil { original = &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, Arguments: test.provider, Environment: map[string]string{"PROVIDER_ENV": "set"}}}} diff --git a/pkg/component/terraform/mock_configuration_test.go b/pkg/component/terraform/mock_configuration_test.go index 61d0a255dd6..0e9e2de844f 100644 --- a/pkg/component/terraform/mock_configuration_test.go +++ b/pkg/component/terraform/mock_configuration_test.go @@ -3,7 +3,7 @@ // // Generated by this command: // -// mockgen -destination=pkg/component/terraform/mock_configuration_test.go -package=terraform github.com/cloudposse/atmos/pkg/schema ComponentConfigurationRuntime +// mockgen -destination=mock_configuration_test.go -package=terraform github.com/cloudposse/atmos/pkg/schema ComponentConfigurationRuntime // // Package terraform is a generated GoMock package. diff --git a/pkg/component/terraform/mock_plan_runtime_test.go b/pkg/component/terraform/mock_plan_runtime_test.go new file mode 100644 index 00000000000..9de5eb3b5b4 --- /dev/null +++ b/pkg/component/terraform/mock_plan_runtime_test.go @@ -0,0 +1,210 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: configuration_plan_test.go +// +// Generated by this command: +// +// mockgen -source=configuration_plan_test.go -destination=mock_plan_runtime_test.go -package=terraform -mock_names=reviewedPlanRuntime=MockReviewedPlanRuntime,planIdentityRuntime=MockPlanIdentityRuntime,planShowRunner=MockPlanShowRunner +// + +// Package terraform is a generated GoMock package. +package terraform + +import ( + context "context" + reflect "reflect" + + process "github.com/cloudposse/atmos/pkg/process" + schema "github.com/cloudposse/atmos/pkg/schema" + gomock "go.uber.org/mock/gomock" +) + +// MockReviewedPlanRuntime is a mock of reviewedPlanRuntime interface. +type MockReviewedPlanRuntime struct { + ctrl *gomock.Controller + recorder *MockReviewedPlanRuntimeMockRecorder + isgomock struct{} +} + +// MockReviewedPlanRuntimeMockRecorder is the mock recorder for MockReviewedPlanRuntime. +type MockReviewedPlanRuntimeMockRecorder struct { + mock *MockReviewedPlanRuntime +} + +// NewMockReviewedPlanRuntime creates a new mock instance. +func NewMockReviewedPlanRuntime(ctrl *gomock.Controller) *MockReviewedPlanRuntime { + mock := &MockReviewedPlanRuntime{ctrl: ctrl} + mock.recorder = &MockReviewedPlanRuntimeMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockReviewedPlanRuntime) EXPECT() *MockReviewedPlanRuntimeMockRecorder { + return m.recorder +} + +// Discover mocks base method. +func (m *MockReviewedPlanRuntime) Discover(arg0 context.Context) (map[string]map[string]any, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Discover", arg0) + ret0, _ := ret[0].(map[string]map[string]any) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Discover indicates an expected call of Discover. +func (mr *MockReviewedPlanRuntimeMockRecorder) Discover(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Discover", reflect.TypeOf((*MockReviewedPlanRuntime)(nil).Discover), arg0) +} + +// PlanIdentity mocks base method. +func (m *MockReviewedPlanRuntime) PlanIdentity(arg0 context.Context, arg1 *schema.ConfigAndStacksInfo, arg2 []schema.ComponentConfigurationSource) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PlanIdentity", arg0, arg1, arg2) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// PlanIdentity indicates an expected call of PlanIdentity. +func (mr *MockReviewedPlanRuntimeMockRecorder) PlanIdentity(arg0, arg1, arg2 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PlanIdentity", reflect.TypeOf((*MockReviewedPlanRuntime)(nil).PlanIdentity), arg0, arg1, arg2) +} + +// Prepare mocks base method. +func (m *MockReviewedPlanRuntime) Prepare(arg0 context.Context, arg1 *schema.ConfigAndStacksInfo) (*schema.PreparedComponentConfiguration, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Prepare", arg0, arg1) + ret0, _ := ret[0].(*schema.PreparedComponentConfiguration) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Prepare indicates an expected call of Prepare. +func (mr *MockReviewedPlanRuntimeMockRecorder) Prepare(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Prepare", reflect.TypeOf((*MockReviewedPlanRuntime)(nil).Prepare), arg0, arg1) +} + +// ResolveState mocks base method. +func (m *MockReviewedPlanRuntime) ResolveState(arg0 context.Context, arg1 *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ResolveState", arg0, arg1) + ret0, _ := ret[0].(*schema.ComponentConfigurationState) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ResolveState indicates an expected call of ResolveState. +func (mr *MockReviewedPlanRuntimeMockRecorder) ResolveState(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ResolveState", reflect.TypeOf((*MockReviewedPlanRuntime)(nil).ResolveState), arg0, arg1) +} + +// MockPlanIdentityRuntime is a mock of planIdentityRuntime interface. +type MockPlanIdentityRuntime struct { + ctrl *gomock.Controller + recorder *MockPlanIdentityRuntimeMockRecorder + isgomock struct{} +} + +// MockPlanIdentityRuntimeMockRecorder is the mock recorder for MockPlanIdentityRuntime. +type MockPlanIdentityRuntimeMockRecorder struct { + mock *MockPlanIdentityRuntime +} + +// NewMockPlanIdentityRuntime creates a new mock instance. +func NewMockPlanIdentityRuntime(ctrl *gomock.Controller) *MockPlanIdentityRuntime { + mock := &MockPlanIdentityRuntime{ctrl: ctrl} + mock.recorder = &MockPlanIdentityRuntimeMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockPlanIdentityRuntime) EXPECT() *MockPlanIdentityRuntimeMockRecorder { + return m.recorder +} + +// Discover mocks base method. +func (m *MockPlanIdentityRuntime) Discover(arg0 context.Context) (map[string]map[string]any, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Discover", arg0) + ret0, _ := ret[0].(map[string]map[string]any) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Discover indicates an expected call of Discover. +func (mr *MockPlanIdentityRuntimeMockRecorder) Discover(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Discover", reflect.TypeOf((*MockPlanIdentityRuntime)(nil).Discover), arg0) +} + +// PlanIdentity mocks base method. +func (m *MockPlanIdentityRuntime) PlanIdentity(arg0 context.Context, arg1 *schema.ConfigAndStacksInfo, arg2 []schema.ComponentConfigurationSource) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "PlanIdentity", arg0, arg1, arg2) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// PlanIdentity indicates an expected call of PlanIdentity. +func (mr *MockPlanIdentityRuntimeMockRecorder) PlanIdentity(arg0, arg1, arg2 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "PlanIdentity", reflect.TypeOf((*MockPlanIdentityRuntime)(nil).PlanIdentity), arg0, arg1, arg2) +} + +// Prepare mocks base method. +func (m *MockPlanIdentityRuntime) Prepare(arg0 context.Context, arg1 *schema.ConfigAndStacksInfo) (*schema.PreparedComponentConfiguration, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Prepare", arg0, arg1) + ret0, _ := ret[0].(*schema.PreparedComponentConfiguration) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Prepare indicates an expected call of Prepare. +func (mr *MockPlanIdentityRuntimeMockRecorder) Prepare(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Prepare", reflect.TypeOf((*MockPlanIdentityRuntime)(nil).Prepare), arg0, arg1) +} + +// MockPlanShowRunner is a mock of planShowRunner interface. +type MockPlanShowRunner struct { + ctrl *gomock.Controller + recorder *MockPlanShowRunnerMockRecorder + isgomock struct{} +} + +// MockPlanShowRunnerMockRecorder is the mock recorder for MockPlanShowRunner. +type MockPlanShowRunnerMockRecorder struct { + mock *MockPlanShowRunner +} + +// NewMockPlanShowRunner creates a new mock instance. +func NewMockPlanShowRunner(ctrl *gomock.Controller) *MockPlanShowRunner { + mock := &MockPlanShowRunner{ctrl: ctrl} + mock.recorder = &MockPlanShowRunnerMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockPlanShowRunner) EXPECT() *MockPlanShowRunnerMockRecorder { + return m.recorder +} + +// Run mocks base method. +func (m *MockPlanShowRunner) Run(ctx context.Context, spec process.TaskSpec) process.Result { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Run", ctx, spec) + ret0, _ := ret[0].(process.Result) + return ret0 +} + +// Run indicates an expected call of Run. +func (mr *MockPlanShowRunnerMockRecorder) Run(ctx, spec any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Run", reflect.TypeOf((*MockPlanShowRunner)(nil).Run), ctx, spec) +} diff --git a/pkg/component/terraform/state_configuration_test.go b/pkg/component/terraform/state_configuration_test.go deleted file mode 100644 index 797ae1e0026..00000000000 --- a/pkg/component/terraform/state_configuration_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package terraform - -import ( - "context" - "errors" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - componentpkg "github.com/cloudposse/atmos/pkg/component" - "github.com/cloudposse/atmos/pkg/schema" -) - -// failingStateRuntime owns the component but cannot identify its backend. -type failingStateRuntime struct { - schema.ComponentConfigurationRuntime - err error -} - -func (r failingStateRuntime) ResolveState(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { - return nil, r.err -} - -func TestGetTerraformStateStopsWhenProviderCannotResolveBackend(t *testing.T) { - failure := errors.New("provider could not locate state") - executor := testExecutor(t) - executor.services.ExecuteDescribeComponent = func(params *componentpkg.ExecuteDescribeComponentParams) (map[string]any, error) { - assert.Equal(t, "network", params.Component) - assert.Equal(t, "live", params.Stack) - return map[string]any{ - "settings": map[string]any{"configuration": true}, - "backend": map[string]any{"path": "must-not-be-read"}, - }, nil - } - config := &schema.AtmosConfiguration{ComponentConfigurations: failingStateRuntime{err: failure}} - - result, err := executor.GetTerraformState(config, "!terraform.state", "live", "network", "vpc_id", true, nil, nil) - require.ErrorIs(t, err, failure, "the provider's failure is returned as is, not retried against a guessed backend") - require.Nil(t, result) -} From 4f695f6e8e3cd4dfd051c3136730640a4225201f Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 13 Nov 2026 07:52:13 -0600 Subject: [PATCH 32/52] test(output): resolve configuration state through a generated runtime mock Generate MockConfigurationStateRuntime with mockgen source mode (go:generate in configuration_state_test.go) and replace the handwritten stateRuntimeFixture. Each test now sets an explicit ResolveState expectation, and gomock rejects unexpected Discover and Prepare calls. Fold configuration_state_protocol_test.go into configuration_state_test.go unchanged so the generated file does not grow the PR file count. Co-Authored-By: Claude Opus 5.5 --- .../configuration_state_protocol_test.go | 71 ------------ .../output/configuration_state_test.go | 106 ++++++++++++++---- .../output/executor_toolchain_test.go | 4 +- .../output/mock_configuration_state_test.go | 87 ++++++++++++++ 4 files changed, 173 insertions(+), 95 deletions(-) delete mode 100644 pkg/terraform/output/configuration_state_protocol_test.go create mode 100644 pkg/terraform/output/mock_configuration_state_test.go diff --git a/pkg/terraform/output/configuration_state_protocol_test.go b/pkg/terraform/output/configuration_state_protocol_test.go deleted file mode 100644 index 83d71b67982..00000000000 --- a/pkg/terraform/output/configuration_state_protocol_test.go +++ /dev/null @@ -1,71 +0,0 @@ -package output - -import ( - "context" - "os" - "path/filepath" - "testing" - - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" - - errUtils "github.com/cloudposse/atmos/errors" - "github.com/cloudposse/atmos/pkg/schema" -) - -func TestResolveConfigurationStateRejectsProviderWithoutStateOrError(t *testing.T) { - config := validAtmosConfig(t) - sections := validSections() - sections["settings"] = map[string]any{"configuration": true} - config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { - return nil, nil - }} - state, err := ResolveConfigurationState(t.Context(), config, &ConfigurationStateRequest{Sections: sections, Component: "network", Stack: "live"}) - require.ErrorIs(t, err, errUtils.ErrPluginProtocol, "a provider that owns a component must always identify its state") - require.Nil(t, state) -} - -func TestSetupEnvironmentFiltersProhibitedVariablesFromBaseEnvironment(t *testing.T) { - setup := &defaultEnvironmentSetup{} - result, err := setup.SetupEnvironment(&ComponentConfig{BaseEnvironment: []string{ - "KEEP=value", "WITH_EQUALS=a=b", "malformed-entry-without-separator", - "TF_INPUT=0", "TF_WORKSPACE=other", "TF_LOG=trace", - "TF_VAR_region=us-east-1", "TF_CLI_ARGS_plan=-lock=false", "TF_CLI_ARGS=-no-color", - }}, nil) - require.NoError(t, err) - assert.Equal(t, map[string]string{"KEEP": "value", "WITH_EQUALS": "a=b"}, result, "only terraform-exec owned variables are removed") -} - -func TestSetupEnvironmentBaseEnvironmentReplacesAmbientEnvironment(t *testing.T) { - t.Setenv("AMBIENT_ONLY", "must-not-leak") - result, err := (&defaultEnvironmentSetup{}).SetupEnvironment(&ComponentConfig{BaseEnvironment: []string{}}, nil) - require.NoError(t, err) - assert.NotContains(t, result, "AMBIENT_ONLY", "an explicitly empty sanitized base is authoritative") - assert.Empty(t, result) -} - -func TestPluginOutputFailsBeforeRunnerWhenScratchDirectoryCannotBeCreated(t *testing.T) { - config := validAtmosConfig(t) - stateRoot := t.TempDir() - config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { - return &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{"path": filepath.Join(stateRoot, "default.tfstate")}, Workspace: "default", WorkingDirectory: stateRoot}, nil - }} - sections := validSections() - sections["settings"] = map[string]any{"configuration": true} - missing := filepath.Join(t.TempDir(), "does-not-exist") - t.Setenv("TMPDIR", missing) - t.Setenv("TMP", missing) - t.Setenv("TEMP", missing) - runnerCreated := false - executor := NewExecutor(nil, WithRunnerFactory(func(string, string) (TerraformRunner, error) { - runnerCreated = true - return nil, nil - })) - result, err := executor.execute(t.Context(), config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true) - require.Error(t, err) - require.Nil(t, result) - require.False(t, runnerCreated, "no Terraform runner may start without its isolated backend directory") - entries, readErr := os.ReadDir(stateRoot) - require.NoError(t, readErr) - require.Empty(t, entries, "the plugin's state directory is never used as scratch space") -} diff --git a/pkg/terraform/output/configuration_state_test.go b/pkg/terraform/output/configuration_state_test.go index 219946d053d..f46e6d641dd 100644 --- a/pkg/terraform/output/configuration_state_test.go +++ b/pkg/terraform/output/configuration_state_test.go @@ -13,24 +13,29 @@ import ( "github.com/cloudposse/atmos/pkg/auth/types" "github.com/cloudposse/atmos/pkg/schema" "github.com/hashicorp/terraform-exec/tfexec" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/mock/gomock" ) -type stateRuntimeFixture struct { - resolve func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) -} +//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=mock_configuration_state_test.go -package=$GOPACKAGE -mock_names=configurationStateRuntime=MockConfigurationStateRuntime -func (*stateRuntimeFixture) Discover(context.Context) (map[string]map[string]any, error) { - panic("unexpected discovery") +// configurationStateRuntime is a configuration runtime that can resolve component state. +type configurationStateRuntime interface { + schema.ComponentConfigurationRuntime + schema.ComponentConfigurationStateRuntime } -func (*stateRuntimeFixture) Prepare(context.Context, *schema.ConfigAndStacksInfo) (*schema.PreparedComponentConfiguration, error) { - panic("state reads must not prepare") -} +// The generated mock implements the contract that the code under test asserts. +var _ configurationStateRuntime = (*MockConfigurationStateRuntime)(nil) -func (s *stateRuntimeFixture) ResolveState(ctx context.Context, info *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { - return s.resolve(ctx, info) +// stateRuntime returns a generated state runtime that expects ResolveState once and answers +// through resolve. Discover and Prepare have no expectations, so gomock rejects them. +func stateRuntime(t *testing.T, resolve func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error)) *MockConfigurationStateRuntime { + t.Helper() + runtime := NewMockConfigurationStateRuntime(gomock.NewController(t)) + runtime.EXPECT().ResolveState(gomock.Any(), gomock.Any()).DoAndReturn(resolve) + return runtime } func TestResolveConfigurationStateUsesTargetEnvironment(t *testing.T) { @@ -42,12 +47,12 @@ func TestResolveConfigurationStateUsesTargetEnvironment(t *testing.T) { sections["settings"] = map[string]any{"configuration": true} sections["env"] = map[string]any{"EXPLICIT": "component"} config := validAtmosConfig(t) - config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(_ context.Context, info *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + config.ComponentConfigurations = stateRuntime(t, func(_ context.Context, info *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { require.Equal(t, []string{"EXPLICIT=component"}, info.SanitizedEnv) require.Equal(t, "output", info.SubCommand) require.Equal(t, "network", info.ComponentFromArg) return &schema.ComponentConfigurationState{BackendType: "local", Workspace: "default"}, nil - }} + }) state, err := ResolveConfigurationState(t.Context(), config, &ConfigurationStateRequest{Sections: sections, Component: "network", Stack: "live", AuthManagers: []any{manager}}) require.NoError(t, err) require.Equal(t, "local", state.BackendType) @@ -98,9 +103,9 @@ func TestPluginOutputUsesBackendOnlyScratchDirectory(t *testing.T) { } config := validAtmosConfig(t) stateRoot := t.TempDir() - config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + config.ComponentConfigurations = stateRuntime(t, func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { return &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{"path": filepath.Join(stateRoot, "default.tfstate")}, Workspace: "prod", WorkingDirectory: stateRoot}, nil - }} + }) sections := validSections() sections["settings"] = map[string]any{"configuration": true} sections["source"] = map[string]any{"uri": "https://must-not-fetch.invalid/module"} @@ -143,9 +148,9 @@ func TestConfigurationStateFailureAndScratchCleanup(t *testing.T) { config := validAtmosConfig(t) sections := validSections() sections["settings"] = map[string]any{"configuration": true} - config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + config.ComponentConfigurations = stateRuntime(t, func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { return nil, context.Canceled - }} + }) _, err := NewExecutor(nil).execute(t.Context(), config, "network", "live", sections, nil, nil, true) require.ErrorIs(t, err, context.Canceled) badTemp := filepath.Join(t.TempDir(), "does-not-exist") @@ -228,9 +233,9 @@ func TestPluginOutputSkipInitReadsTheInitializedUnit(t *testing.T) { config := validAtmosConfig(t) unit := t.TempDir() require.NoError(t, os.Mkdir(filepath.Join(unit, ".terraform"), 0o700)) - config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + config.ComponentConfigurations = stateRuntime(t, func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { return &schema.ComponentConfigurationState{BackendType: "local", Workspace: "prod", WorkingDirectory: unit}, nil - }} + }) sections := validSections() sections["settings"] = map[string]any{"configuration": true} sections["providers"] = map[string]any{"aws": map[string]any{"region": "us-east-2"}} @@ -260,9 +265,9 @@ func TestPluginOutputSkipInitRejectsAnUninitializedUnit(t *testing.T) { } { t.Run(name, func(t *testing.T) { config := validAtmosConfig(t) - config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + config.ComponentConfigurations = stateRuntime(t, func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { return state, nil - }} + }) sections := validSections() sections["settings"] = map[string]any{"configuration": true} executor := NewExecutor(nil, WithRunnerFactory(func(string, string) (TerraformRunner, error) { @@ -318,9 +323,9 @@ func TestPluginOutputDoesNotResolveTheExecutionWorkdir(t *testing.T) { config := validAtmosConfig(t) unit := t.TempDir() require.NoError(t, os.Mkdir(filepath.Join(unit, ".terraform"), 0o700)) - config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + config.ComponentConfigurations = stateRuntime(t, func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { return &schema.ComponentConfigurationState{BackendType: "local", Workspace: "prod", WorkingDirectory: unit}, nil - }} + }) sections := validSections() sections["settings"] = map[string]any{"configuration": true} // No base component: an ordinary component could not resolve its execution path at all. @@ -384,3 +389,60 @@ func TestConfigurationStateSectionsKeepsExplicitRemoteStateOverrides(t *testing. }) } } + +func TestResolveConfigurationStateRejectsProviderWithoutStateOrError(t *testing.T) { + config := validAtmosConfig(t) + sections := validSections() + sections["settings"] = map[string]any{"configuration": true} + config.ComponentConfigurations = stateRuntime(t, func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return nil, nil + }) + state, err := ResolveConfigurationState(t.Context(), config, &ConfigurationStateRequest{Sections: sections, Component: "network", Stack: "live"}) + require.ErrorIs(t, err, errUtils.ErrPluginProtocol, "a provider that owns a component must always identify its state") + require.Nil(t, state) +} + +func TestSetupEnvironmentFiltersProhibitedVariablesFromBaseEnvironment(t *testing.T) { + setup := &defaultEnvironmentSetup{} + result, err := setup.SetupEnvironment(&ComponentConfig{BaseEnvironment: []string{ + "KEEP=value", "WITH_EQUALS=a=b", "malformed-entry-without-separator", + "TF_INPUT=0", "TF_WORKSPACE=other", "TF_LOG=trace", + "TF_VAR_region=us-east-1", "TF_CLI_ARGS_plan=-lock=false", "TF_CLI_ARGS=-no-color", + }}, nil) + require.NoError(t, err) + assert.Equal(t, map[string]string{"KEEP": "value", "WITH_EQUALS": "a=b"}, result, "only terraform-exec owned variables are removed") +} + +func TestSetupEnvironmentBaseEnvironmentReplacesAmbientEnvironment(t *testing.T) { + t.Setenv("AMBIENT_ONLY", "must-not-leak") + result, err := (&defaultEnvironmentSetup{}).SetupEnvironment(&ComponentConfig{BaseEnvironment: []string{}}, nil) + require.NoError(t, err) + assert.NotContains(t, result, "AMBIENT_ONLY", "an explicitly empty sanitized base is authoritative") + assert.Empty(t, result) +} + +func TestPluginOutputFailsBeforeRunnerWhenScratchDirectoryCannotBeCreated(t *testing.T) { + config := validAtmosConfig(t) + stateRoot := t.TempDir() + config.ComponentConfigurations = stateRuntime(t, func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{"path": filepath.Join(stateRoot, "default.tfstate")}, Workspace: "default", WorkingDirectory: stateRoot}, nil + }) + sections := validSections() + sections["settings"] = map[string]any{"configuration": true} + missing := filepath.Join(t.TempDir(), "does-not-exist") + t.Setenv("TMPDIR", missing) + t.Setenv("TMP", missing) + t.Setenv("TEMP", missing) + runnerCreated := false + executor := NewExecutor(nil, WithRunnerFactory(func(string, string) (TerraformRunner, error) { + runnerCreated = true + return nil, nil + })) + result, err := executor.execute(t.Context(), config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true) + require.Error(t, err) + require.Nil(t, result) + require.False(t, runnerCreated, "no Terraform runner may start without its isolated backend directory") + entries, readErr := os.ReadDir(stateRoot) + require.NoError(t, readErr) + require.Empty(t, entries, "the plugin's state directory is never used as scratch space") +} diff --git a/pkg/terraform/output/executor_toolchain_test.go b/pkg/terraform/output/executor_toolchain_test.go index 372bc06264b..4af6dec6a23 100644 --- a/pkg/terraform/output/executor_toolchain_test.go +++ b/pkg/terraform/output/executor_toolchain_test.go @@ -198,9 +198,9 @@ func TestPluginOwnedOutputReadUsesIdentityEnvironmentAndToolchain(t *testing.T) return runner, nil })) stateRoot := t.TempDir() - setup.config.ComponentConfigurations = &stateRuntimeFixture{resolve: func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + setup.config.ComponentConfigurations = stateRuntime(t, func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { return &schema.ComponentConfigurationState{BackendType: "local", Backend: map[string]any{"path": filepath.Join(stateRoot, "default.tfstate")}, Workspace: "prod", WorkingDirectory: stateRoot}, nil - }} + }) sections := validSections() sections["settings"] = map[string]any{"configuration": true} sections[cfg.CommandSectionName] = "tofu" diff --git a/pkg/terraform/output/mock_configuration_state_test.go b/pkg/terraform/output/mock_configuration_state_test.go new file mode 100644 index 00000000000..234e4cc026c --- /dev/null +++ b/pkg/terraform/output/mock_configuration_state_test.go @@ -0,0 +1,87 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: configuration_state_test.go +// +// Generated by this command: +// +// mockgen -source=configuration_state_test.go -destination=mock_configuration_state_test.go -package=output -mock_names=configurationStateRuntime=MockConfigurationStateRuntime +// + +// Package output is a generated GoMock package. +package output + +import ( + context "context" + reflect "reflect" + + schema "github.com/cloudposse/atmos/pkg/schema" + gomock "go.uber.org/mock/gomock" +) + +// MockConfigurationStateRuntime is a mock of configurationStateRuntime interface. +type MockConfigurationStateRuntime struct { + ctrl *gomock.Controller + recorder *MockConfigurationStateRuntimeMockRecorder + isgomock struct{} +} + +// MockConfigurationStateRuntimeMockRecorder is the mock recorder for MockConfigurationStateRuntime. +type MockConfigurationStateRuntimeMockRecorder struct { + mock *MockConfigurationStateRuntime +} + +// NewMockConfigurationStateRuntime creates a new mock instance. +func NewMockConfigurationStateRuntime(ctrl *gomock.Controller) *MockConfigurationStateRuntime { + mock := &MockConfigurationStateRuntime{ctrl: ctrl} + mock.recorder = &MockConfigurationStateRuntimeMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockConfigurationStateRuntime) EXPECT() *MockConfigurationStateRuntimeMockRecorder { + return m.recorder +} + +// Discover mocks base method. +func (m *MockConfigurationStateRuntime) Discover(arg0 context.Context) (map[string]map[string]any, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Discover", arg0) + ret0, _ := ret[0].(map[string]map[string]any) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Discover indicates an expected call of Discover. +func (mr *MockConfigurationStateRuntimeMockRecorder) Discover(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Discover", reflect.TypeOf((*MockConfigurationStateRuntime)(nil).Discover), arg0) +} + +// Prepare mocks base method. +func (m *MockConfigurationStateRuntime) Prepare(arg0 context.Context, arg1 *schema.ConfigAndStacksInfo) (*schema.PreparedComponentConfiguration, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Prepare", arg0, arg1) + ret0, _ := ret[0].(*schema.PreparedComponentConfiguration) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Prepare indicates an expected call of Prepare. +func (mr *MockConfigurationStateRuntimeMockRecorder) Prepare(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Prepare", reflect.TypeOf((*MockConfigurationStateRuntime)(nil).Prepare), arg0, arg1) +} + +// ResolveState mocks base method. +func (m *MockConfigurationStateRuntime) ResolveState(arg0 context.Context, arg1 *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ResolveState", arg0, arg1) + ret0, _ := ret[0].(*schema.ComponentConfigurationState) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ResolveState indicates an expected call of ResolveState. +func (mr *MockConfigurationStateRuntimeMockRecorder) ResolveState(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ResolveState", reflect.TypeOf((*MockConfigurationStateRuntime)(nil).ResolveState), arg0, arg1) +} From 78de52d1f0bba1cfe4503557b105a5d30de00989 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 13 Nov 2026 07:52:51 -0600 Subject: [PATCH 33/52] test(terraform): restore the unchanged configuration runtime mock header A package-wide go generate run rewrote only the generator-command comment in mock_configuration_test.go. Restore the original header. The file belongs to the base layer and this layer does not change it. Co-Authored-By: Claude Opus 5.5 --- pkg/component/terraform/mock_configuration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/component/terraform/mock_configuration_test.go b/pkg/component/terraform/mock_configuration_test.go index 0e9e2de844f..61d0a255dd6 100644 --- a/pkg/component/terraform/mock_configuration_test.go +++ b/pkg/component/terraform/mock_configuration_test.go @@ -3,7 +3,7 @@ // // Generated by this command: // -// mockgen -destination=mock_configuration_test.go -package=terraform github.com/cloudposse/atmos/pkg/schema ComponentConfigurationRuntime +// mockgen -destination=pkg/component/terraform/mock_configuration_test.go -package=terraform github.com/cloudposse/atmos/pkg/schema ComponentConfigurationRuntime // // Package terraform is a generated GoMock package. From f3b275dea11def128511473eaf6878bf510568a6 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 13 Nov 2026 07:54:23 -0600 Subject: [PATCH 34/52] test(configuration): generate the provider mock with its identity capability Define a test-only identifiedProvider contract (Provider plus the optional Identity capability) and generate MockIdentifiedProvider with mockgen source mode (go:generate in plan_test.go). This replaces the handwritten identifiedProvider wrapper. Tests configure Identity through gomock expectations. Fold state_test.go into preparation_test.go unchanged so the generated file does not grow the PR file count. Co-Authored-By: Claude Opus 5.5 --- .../mock_identified_provider_test.go | 101 ++++++++++++++++++ pkg/plugin/adapter/configuration/plan_test.go | 21 ++-- .../adapter/configuration/preparation_test.go | 60 +++++++++++ .../adapter/configuration/state_test.go | 73 ------------- 4 files changed, 174 insertions(+), 81 deletions(-) create mode 100644 pkg/plugin/adapter/configuration/mock_identified_provider_test.go delete mode 100644 pkg/plugin/adapter/configuration/state_test.go diff --git a/pkg/plugin/adapter/configuration/mock_identified_provider_test.go b/pkg/plugin/adapter/configuration/mock_identified_provider_test.go new file mode 100644 index 00000000000..860ee04b591 --- /dev/null +++ b/pkg/plugin/adapter/configuration/mock_identified_provider_test.go @@ -0,0 +1,101 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: plan_test.go +// +// Generated by this command: +// +// mockgen -source=plan_test.go -destination=mock_identified_provider_test.go -package=configuration -mock_names=identifiedProvider=MockIdentifiedProvider +// + +// Package configuration is a generated GoMock package. +package configuration + +import ( + context "context" + reflect "reflect" + + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" + gomock "go.uber.org/mock/gomock" +) + +// MockIdentifiedProvider is a mock of identifiedProvider interface. +type MockIdentifiedProvider struct { + ctrl *gomock.Controller + recorder *MockIdentifiedProviderMockRecorder + isgomock struct{} +} + +// MockIdentifiedProviderMockRecorder is the mock recorder for MockIdentifiedProvider. +type MockIdentifiedProviderMockRecorder struct { + mock *MockIdentifiedProvider +} + +// NewMockIdentifiedProvider creates a new mock instance. +func NewMockIdentifiedProvider(ctrl *gomock.Controller) *MockIdentifiedProvider { + mock := &MockIdentifiedProvider{ctrl: ctrl} + mock.recorder = &MockIdentifiedProviderMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockIdentifiedProvider) EXPECT() *MockIdentifiedProviderMockRecorder { + return m.recorder +} + +// ComponentType mocks base method. +func (m *MockIdentifiedProvider) ComponentType() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ComponentType") + ret0, _ := ret[0].(string) + return ret0 +} + +// ComponentType indicates an expected call of ComponentType. +func (mr *MockIdentifiedProviderMockRecorder) ComponentType() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ComponentType", reflect.TypeOf((*MockIdentifiedProvider)(nil).ComponentType)) +} + +// Discover mocks base method. +func (m *MockIdentifiedProvider) Discover(arg0 context.Context, arg1 *pluginv1.DiscoverComponentsRequest) (*pluginv1.DiscoverComponentsResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Discover", arg0, arg1) + ret0, _ := ret[0].(*pluginv1.DiscoverComponentsResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Discover indicates an expected call of Discover. +func (mr *MockIdentifiedProviderMockRecorder) Discover(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Discover", reflect.TypeOf((*MockIdentifiedProvider)(nil).Discover), arg0, arg1) +} + +// Identity mocks base method. +func (m *MockIdentifiedProvider) Identity(arg0 context.Context) (string, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Identity", arg0) + ret0, _ := ret[0].(string) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Identity indicates an expected call of Identity. +func (mr *MockIdentifiedProviderMockRecorder) Identity(arg0 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Identity", reflect.TypeOf((*MockIdentifiedProvider)(nil).Identity), arg0) +} + +// Prepare mocks base method. +func (m *MockIdentifiedProvider) Prepare(arg0 context.Context, arg1 *pluginv1.ComponentRequest) (*pluginv1.PrepareComponentResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Prepare", arg0, arg1) + ret0, _ := ret[0].(*pluginv1.PrepareComponentResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Prepare indicates an expected call of Prepare. +func (mr *MockIdentifiedProviderMockRecorder) Prepare(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Prepare", reflect.TypeOf((*MockIdentifiedProvider)(nil).Prepare), arg0, arg1) +} diff --git a/pkg/plugin/adapter/configuration/plan_test.go b/pkg/plugin/adapter/configuration/plan_test.go index 7c360622ca1..da5eaa1e89c 100644 --- a/pkg/plugin/adapter/configuration/plan_test.go +++ b/pkg/plugin/adapter/configuration/plan_test.go @@ -17,13 +17,16 @@ import ( "github.com/cloudposse/atmos/pkg/schema" ) -// identifiedProvider adds the optional Identity capability to the generated provider mock. -type identifiedProvider struct { +//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=mock_identified_provider_test.go -package=$GOPACKAGE -mock_names=identifiedProvider=MockIdentifiedProvider + +// identifiedProvider is a provider with the optional Identity capability that binds saved plans. +type identifiedProvider interface { Provider - identity func(context.Context) (string, error) + identityProvider } -func (p *identifiedProvider) Identity(ctx context.Context) (string, error) { return p.identity(ctx) } +// The generated mock implements the contract that PlanIdentity asserts. +var _ identifiedProvider = (*MockIdentifiedProvider)(nil) // planCase describes every input that a saved-plan binding is expected to cover. type planCase struct { @@ -47,16 +50,16 @@ func planSession(t *testing.T, c *planCase) (*Session, *schema.ConfigAndStacksIn settings := config.Components.Terraform.ConfigurationProviders["terragrunt"] settings.Options = c.options config.Components.Terraform.ConfigurationProviders["terragrunt"] = settings - mock := NewMockProvider(gomock.NewController(t)) + mock := NewMockIdentifiedProvider(gomock.NewController(t)) mock.EXPECT().ComponentType().Return("terraform").AnyTimes() + mock.EXPECT().Identity(gomock.Any()).Return(c.identity, nil).AnyTimes() component := unit(t, c.component) configuration, err := adapter.EncodeObject(map[string]any{"source": c.source}) require.NoError(t, err) component.Configuration = configuration mock.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{component}}, nil) - provider := &identifiedProvider{Provider: mock, identity: func(context.Context) (string, error) { return c.identity, nil }} session, err := New(t.Context(), config, "plan", func(context.Context, []string) (map[string]Provider, error) { - return map[string]Provider{"terragrunt": provider}, nil + return map[string]Provider{"terragrunt": mock}, nil }) require.NoError(t, err) info := &schema.ConfigAndStacksInfo{Stack: "live", ComponentFromArg: c.component, SubCommand: c.subCommand, AdditionalArgsAndFlags: c.args, ComponentEnvList: c.env} @@ -201,7 +204,9 @@ func TestPlanIdentityRejectsUnboundableRequests(t *testing.T) { _, err := session.Discover(t.Context()) require.NoError(t, err) failure := errors.New("executable unreadable") - session.registry["terragrunt"] = &identifiedProvider{Provider: NewMockProvider(gomock.NewController(t)), identity: func(context.Context) (string, error) { return "", failure }} + provider := NewMockIdentifiedProvider(gomock.NewController(t)) + provider.EXPECT().Identity(gomock.Any()).Return("", failure) + session.registry["terragrunt"] = provider _, err = session.PlanIdentity(t.Context(), info, nil) require.ErrorIs(t, err, failure) }) diff --git a/pkg/plugin/adapter/configuration/preparation_test.go b/pkg/plugin/adapter/configuration/preparation_test.go index 0c56a5a5466..56a102e185e 100644 --- a/pkg/plugin/adapter/configuration/preparation_test.go +++ b/pkg/plugin/adapter/configuration/preparation_test.go @@ -3,6 +3,7 @@ package configuration import ( "context" "errors" + "path/filepath" "testing" "github.com/stretchr/testify/require" @@ -154,3 +155,62 @@ func TestPreparedExecutionRejectsNilEntriesInEveryList(t *testing.T) { }) } } + +type stateProviderFixture struct { + Provider + resolve func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) +} + +func (p *stateProviderFixture) ResolveState(ctx context.Context, req *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + return p.resolve(ctx, req) +} + +func TestSessionResolvesStateWithoutPreparation(t *testing.T) { + config := enabledConfig(t) + mock := NewMockProvider(gomock.NewController(t)) + mock.EXPECT().ComponentType().Return("terraform") + mock.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{unit(t, "network")}}, nil) + backend, err := adapter.EncodeObject(map[string]any{"path": "state.tfstate"}) + require.NoError(t, err) + provider := &stateProviderFixture{Provider: mock, resolve: func(_ context.Context, req *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + require.Nil(t, req.Effects) + require.Equal(t, "output", req.Operation.Command) + require.Equal(t, "selected", req.Invocation.Environment["IDENTITY"]) + require.Equal(t, "stage", req.Invocation.Environment["TF_WORKSPACE"]) + return &pluginv1.ResolveComponentStateResponse{State: &pluginv1.ComponentStateIdentity{BackendType: "local", Backend: backend, Workspace: "stage", WorkingDirectory: config.BasePathAbsolute}}, nil + }} + session, err := New(t.Context(), config, "describe", func(context.Context, []string) (map[string]Provider, error) { + return map[string]Provider{"terragrunt": provider}, nil + }) + require.NoError(t, err) + info := &schema.ConfigAndStacksInfo{Stack: "live", ComponentFromArg: "network", SubCommand: "output", TerraformWorkspace: "stage", ComponentSettingsSection: map[string]any{"configuration": true}, SanitizedEnv: []string{"IDENTITY=selected"}} + state, err := session.ResolveState(t.Context(), info) + require.NoError(t, err) + require.Equal(t, filepath.Join(config.BasePathAbsolute, "state.tfstate"), state.Backend["path"]) + require.Equal(t, "stage", state.Workspace) + provider.resolve = func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + return nil, context.Canceled + } + _, err = session.ResolveState(t.Context(), info) + require.ErrorIs(t, err, context.Canceled) + provider.resolve = func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + return &pluginv1.ResolveComponentStateResponse{}, nil + } + _, err = session.ResolveState(t.Context(), info) + require.ErrorIs(t, err, errUtils.ErrPluginProtocol) + provider.resolve = func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + return &pluginv1.ResolveComponentStateResponse{State: &pluginv1.ComponentStateIdentity{Backend: &pluginv1.ComponentValue{}}}, nil + } + _, err = session.ResolveState(t.Context(), info) + require.ErrorIs(t, err, errUtils.ErrPluginProtocol) + session.registry["terragrunt"] = mock + _, err = session.ResolveState(t.Context(), info) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + info.ComponentFromArg = "absent" + _, err = session.ResolveState(t.Context(), info) + require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) + info.ComponentSettingsSection = nil + state, err = session.ResolveState(t.Context(), info) + require.NoError(t, err) + require.Nil(t, state) +} diff --git a/pkg/plugin/adapter/configuration/state_test.go b/pkg/plugin/adapter/configuration/state_test.go deleted file mode 100644 index 1c648719a9e..00000000000 --- a/pkg/plugin/adapter/configuration/state_test.go +++ /dev/null @@ -1,73 +0,0 @@ -package configuration - -import ( - "context" - "path/filepath" - "testing" - - errUtils "github.com/cloudposse/atmos/errors" - adapter "github.com/cloudposse/atmos/pkg/plugin/adapter/component" - pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" - "github.com/cloudposse/atmos/pkg/schema" - "github.com/stretchr/testify/require" - "go.uber.org/mock/gomock" -) - -type stateProviderFixture struct { - Provider - resolve func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) -} - -func (p *stateProviderFixture) ResolveState(ctx context.Context, req *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { - return p.resolve(ctx, req) -} - -func TestSessionResolvesStateWithoutPreparation(t *testing.T) { - config := enabledConfig(t) - mock := NewMockProvider(gomock.NewController(t)) - mock.EXPECT().ComponentType().Return("terraform") - mock.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{unit(t, "network")}}, nil) - backend, err := adapter.EncodeObject(map[string]any{"path": "state.tfstate"}) - require.NoError(t, err) - provider := &stateProviderFixture{Provider: mock, resolve: func(_ context.Context, req *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { - require.Nil(t, req.Effects) - require.Equal(t, "output", req.Operation.Command) - require.Equal(t, "selected", req.Invocation.Environment["IDENTITY"]) - require.Equal(t, "stage", req.Invocation.Environment["TF_WORKSPACE"]) - return &pluginv1.ResolveComponentStateResponse{State: &pluginv1.ComponentStateIdentity{BackendType: "local", Backend: backend, Workspace: "stage", WorkingDirectory: config.BasePathAbsolute}}, nil - }} - session, err := New(t.Context(), config, "describe", func(context.Context, []string) (map[string]Provider, error) { - return map[string]Provider{"terragrunt": provider}, nil - }) - require.NoError(t, err) - info := &schema.ConfigAndStacksInfo{Stack: "live", ComponentFromArg: "network", SubCommand: "output", TerraformWorkspace: "stage", ComponentSettingsSection: map[string]any{"configuration": true}, SanitizedEnv: []string{"IDENTITY=selected"}} - state, err := session.ResolveState(t.Context(), info) - require.NoError(t, err) - require.Equal(t, filepath.Join(config.BasePathAbsolute, "state.tfstate"), state.Backend["path"]) - require.Equal(t, "stage", state.Workspace) - provider.resolve = func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { - return nil, context.Canceled - } - _, err = session.ResolveState(t.Context(), info) - require.ErrorIs(t, err, context.Canceled) - provider.resolve = func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { - return &pluginv1.ResolveComponentStateResponse{}, nil - } - _, err = session.ResolveState(t.Context(), info) - require.ErrorIs(t, err, errUtils.ErrPluginProtocol) - provider.resolve = func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { - return &pluginv1.ResolveComponentStateResponse{State: &pluginv1.ComponentStateIdentity{Backend: &pluginv1.ComponentValue{}}}, nil - } - _, err = session.ResolveState(t.Context(), info) - require.ErrorIs(t, err, errUtils.ErrPluginProtocol) - session.registry["terragrunt"] = mock - _, err = session.ResolveState(t.Context(), info) - require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) - info.ComponentFromArg = "absent" - _, err = session.ResolveState(t.Context(), info) - require.ErrorIs(t, err, errUtils.ErrPluginInvalidConfig) - info.ComponentSettingsSection = nil - state, err = session.ResolveState(t.Context(), info) - require.NoError(t, err) - require.Nil(t, state) -} From 9fb30e2c838533885004b5bd40f2ded43f45e72f Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 13 Nov 2026 11:54:03 -0600 Subject: [PATCH 35/52] docs(plugin): clarify L5 component service status --- docs/prd/plugins/extensions/component-engines.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/prd/plugins/extensions/component-engines.md b/docs/prd/plugins/extensions/component-engines.md index e664a53f588..7ec9726dbfa 100644 --- a/docs/prd/plugins/extensions/component-engines.md +++ b/docs/prd/plugins/extensions/component-engines.md @@ -2,6 +2,8 @@ **Status:** Implementation in progress. The protocol, SDK, native runtime clients, static component descriptors, explicit package loader, and configuration-service adapters exist. CLI discovery, stack adaptation, and native Terraform preparation are wired on this branch. End-to-end validation is limited to local Terragrunt fixtures; the full release gates remain. +This L5 status supersedes the earlier, L4-scoped "planned" descriptions in the [platform interfaces](../platform/interfaces.md#component), [protocol](../platform/protocol.md#component-configuration-services), and [SDK README](../../../../sdk/README.md#component-configuration-contracts-in-development). On this branch, `plugin.Services` accepts the four optional component handlers, `component.Encode` and `component.Decode` are available, and component exports can be validated and packed. The host has matching clients, explicit package loading, and private provider registries. The CLI and native Terraform path use these services for discovery, stack adaptation, and preparation. External execution engines, general host process and filesystem grants, typed plugin flags, and the remaining integration and release gates below are still pending. + **Decision:** Separate component type, configuration provider, and execution engine. Negotiate optional capabilities independently. A configuration provider must not need an executor. See the [Terragrunt requirements](../../terragrunt-support.md) for the first consumer and [all extension types](README.md) for the platform scope. From 22fc3a6a2e494975e4408297ddec6b0339888ba0 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 13 Nov 2026 11:56:05 -0600 Subject: [PATCH 36/52] ci(plugin): authenticate official package checks to GHCR --- .github/workflows/plugin-packages.yaml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/plugin-packages.yaml b/.github/workflows/plugin-packages.yaml index 13c5a1dffa2..300e705480a 100644 --- a/.github/workflows/plugin-packages.yaml +++ b/.github/workflows/plugin-packages.yaml @@ -3,6 +3,7 @@ on: workflow_call: permissions: contents: read + packages: read env: ATMOS_CI: 'true' CGO_ENABLED: '0' @@ -47,6 +48,8 @@ jobs: GOCACHE: ${{ runner.temp }}/plugin-go-build GOMODCACHE: ${{ runner.temp }}/plugin-go-mod RELEASE_BASE: ${{ steps.base.outputs.ref }} + GITHUB_TOKEN: ${{ github.token }} + ATMOS_GITHUB_USERNAME: ${{ github.actor }} run: atmos plugin release --inventory plugins/releases.yaml --check ${RELEASE_BASE:+--base "$RELEASE_BASE"} release: From dcff90855cecff618ec77240498fa0ef96b2a3b1 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 13 Nov 2026 11:59:43 -0600 Subject: [PATCH 37/52] test(terraform): fold PATH regression into toolchain suite --- .../configuration_toolchain_path_test.go | 32 ------------------- .../terraform/configuration_toolchain_test.go | 22 +++++++++++++ 2 files changed, 22 insertions(+), 32 deletions(-) delete mode 100644 pkg/component/terraform/configuration_toolchain_path_test.go diff --git a/pkg/component/terraform/configuration_toolchain_path_test.go b/pkg/component/terraform/configuration_toolchain_path_test.go deleted file mode 100644 index 5a4b499b5e1..00000000000 --- a/pkg/component/terraform/configuration_toolchain_path_test.go +++ /dev/null @@ -1,32 +0,0 @@ -package terraform - -import ( - "path/filepath" - "strings" - "testing" - - "github.com/stretchr/testify/require" - - "github.com/cloudposse/atmos/pkg/schema" -) - -// When the process PATH already starts with the toolchain directories (for example an Atmos run -// nested inside a toolchain shell), there is nothing left to prepend: the selected PATH is used as is. -func TestToolchainPathEnvWithToolchainDirectoriesAlreadyInAmbientPath(t *testing.T) { - harness := newToolchainHarness(t, "1.0.0") - info := harness.info(t, &toolchainScenario{identity: "development", tofuVersion: "1.0.0", toolVersion: "1.0.0"}, filepath.Join(t.TempDir(), "probe.jsonl")) - tenv, err := resolveAndInstallToolchainDeps(harness.config, info) - require.NoError(t, err) - full := tenv.PATH() - require.NotEmpty(t, full) - t.Setenv("PATH", full) - - selected := filepath.Join(t.TempDir(), "selected") - got := toolchainPathEnv(&schema.ConfigAndStacksInfo{ComponentEnvList: []string{"PATH=" + selected}}, tenv) - require.Equal(t, []string{"PATH=" + selected}, got) - - // An empty selected PATH is the same case with nothing to extend: only the toolchain prefix remains, which is empty. - got = toolchainPathEnv(&schema.ConfigAndStacksInfo{ComponentEnvList: []string{"PATH="}}, tenv) - require.Equal(t, []string{"PATH="}, got) - require.False(t, strings.Contains(got[0], full), "the ambient toolchain directories must not be re-added") -} diff --git a/pkg/component/terraform/configuration_toolchain_test.go b/pkg/component/terraform/configuration_toolchain_test.go index d3bac51e7b3..cce174445b6 100644 --- a/pkg/component/terraform/configuration_toolchain_test.go +++ b/pkg/component/terraform/configuration_toolchain_test.go @@ -76,6 +76,28 @@ func readToolchainProbes(t *testing.T, path string) []toolchainProbe { return probes } +// When the process PATH already starts with the toolchain directories (for example an Atmos run +// nested inside a toolchain shell), there is nothing left to prepend: the selected PATH is used as is. +func TestToolchainPathEnvWithToolchainDirectoriesAlreadyInAmbientPath(t *testing.T) { + harness := newToolchainHarness(t, "1.0.0") + info := harness.info(t, &toolchainScenario{identity: "development", tofuVersion: "1.0.0", toolVersion: "1.0.0"}, filepath.Join(t.TempDir(), "probe.jsonl")) + + tenv, err := resolveAndInstallToolchainDeps(harness.config, info) + require.NoError(t, err) + full := tenv.PATH() + require.NotEmpty(t, full) + t.Setenv("PATH", full) + + selected := filepath.Join(t.TempDir(), "selected") + got := toolchainPathEnv(&schema.ConfigAndStacksInfo{ComponentEnvList: []string{"PATH=" + selected}}, tenv) + require.Equal(t, []string{"PATH=" + selected}, got) + + // An empty selected PATH is the same case with nothing to extend: only the toolchain prefix remains, which is empty. + got = toolchainPathEnv(&schema.ConfigAndStacksInfo{ComponentEnvList: []string{"PATH="}}, tenv) + require.Equal(t, []string{"PATH="}, got) + require.False(t, strings.Contains(got[0], full), "the ambient toolchain directories must not be re-added") +} + // installToolchainFixture places a hard link (or copy) of the test binary where the real // toolchain installer would have installed owner/repo at version, so no network is used. func installToolchainFixture(t *testing.T, installPath, owner, repo, version, name string) string { From a7f639ce66eb9e8aa7ceb8611811bea9b35007be Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Fri, 13 Nov 2026 22:12:41 -0600 Subject: [PATCH 38/52] fix(terragrunt): preserve effective plan and preparation inputs --- pkg/component/terraform/configuration_plan.go | 44 +++++++- .../terraform/configuration_plan_test.go | 19 ++++ pkg/component/terraform/configuration_test.go | 15 +-- .../terraform/configuration_toolchain_test.go | 82 +++++++------- .../terraform/execute_environment.go | 16 +-- pkg/component/terraform/execute_pipeline.go | 2 +- .../mock_configuration_provider_test.go | 101 ++++++++++++++++++ .../adapter/configuration/preparation_test.go | 27 ++--- .../terragrunt/internal/engine/execution.go | 19 ++++ .../internal/engine/execution_test.go | 53 +++++++++ .../terragrunt/internal/engine/policies.go | 14 ++- .../terragrunt/internal/provider/provider.go | 3 + .../internal/provider/provider_test.go | 6 ++ 13 files changed, 310 insertions(+), 91 deletions(-) create mode 100644 pkg/component/terraform/mock_configuration_provider_test.go diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go index e33a67a2016..fd6311c9286 100644 --- a/pkg/component/terraform/configuration_plan.go +++ b/pkg/component/terraform/configuration_plan.go @@ -1,6 +1,7 @@ package terraform import ( + "bytes" "context" "crypto/sha256" "encoding/hex" @@ -8,6 +9,7 @@ import ( "fmt" "io" "io/fs" + "math/big" "os" "path/filepath" "reflect" @@ -351,7 +353,7 @@ func verifyConfigurationPlanState(ctx context.Context, config *schema.AtmosConfi if err != nil { return err } - if state == nil || state.BackendType != plan.Prepared.BackendType || state.Workspace != plan.Prepared.Workspace || !reflect.DeepEqual(state.Backend, plan.Prepared.Backend) { + if state == nil || state.BackendType != plan.Prepared.BackendType || state.Workspace != plan.Prepared.Workspace || !equalBackendConfiguration(state.Backend, plan.Prepared.Backend) { return fmt.Errorf("%w: reviewed backend identity changed", errUtils.ErrPlanVerificationFailed) } return nil @@ -464,3 +466,43 @@ func recordWorkdirFile(root, path string, entry fs.DirEntry, files map[string]st files[filepath.ToSlash(relative)] = digest return nil } + +// equalBackendConfiguration compares JSON backend values without losing numeric precision. +func equalBackendConfiguration(left, right map[string]any) bool { + a, errA := normalizedBackend(left) + b, errB := normalizedBackend(right) + return errA == nil && errB == nil && reflect.DeepEqual(a, b) +} + +func normalizedBackend(value map[string]any) (any, error) { + data, err := json.Marshal(value) + if err != nil { + return nil, err + } + decoder := json.NewDecoder(bytes.NewReader(data)) + decoder.UseNumber() + var decoded any + if err := decoder.Decode(&decoded); err != nil { + return nil, err + } + return normalizeBackendNumbers(decoded), nil +} + +func normalizeBackendNumbers(value any) any { + switch typed := value.(type) { + case json.Number: + number, ok := new(big.Rat).SetString(string(typed)) + if ok { + return number + } + case map[string]any: + for key, child := range typed { + typed[key] = normalizeBackendNumbers(child) + } + case []any: + for index, child := range typed { + typed[index] = normalizeBackendNumbers(child) + } + } + return value +} diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index 1df73acc2c8..0462e85a592 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -2,6 +2,7 @@ package terraform import ( "context" + "encoding/json" "errors" "io" "os" @@ -308,16 +309,21 @@ func TestRecoveredPlanBindsEffectiveOutput(t *testing.T) { func TestDetailedExitCodePlanRecordsReviewedPlan(t *testing.T) { for _, test := range []struct { name string + extra bool args []string code int record bool }{ {name: "explicit detailed exit code with changes", args: []string{"plan", "-detailed-exitcode", "-out", "changes.plan"}, code: 2, record: true}, + {name: "provider detailed exit code", extra: true, args: []string{"plan", "-out", "changes.plan"}, code: 2, record: true}, {name: "failure with detailed exit code", args: []string{"plan", "-detailed-exitcode", "-out", "changes.plan"}, code: 1}, {name: "status 2 without detailed exit code", args: []string{"plan", "-out", "changes.plan"}, code: 2}, } { t.Run(test.name, func(t *testing.T) { config, info, execCtx, _, _ := planFixture(t) + if test.extra { + info.ComponentExecution = &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, Arguments: []string{"-detailed-exitcode"}}}} + } shell := &fakeShell{run: func(call *shellCall) error { if err := os.WriteFile(plannedOutputPath(execCtx.componentPath, call.args), []byte("changes"), 0o600); err != nil { return err @@ -437,3 +443,16 @@ func TestEnvironmentPlanOutputIgnoresUnownedPlans(t *testing.T) { execCtx.preparedConfiguration = nil require.NoError(t, validateEnvironmentPlanOutput(config, info, execCtx)) } + +func TestConfigurationPlanRoundTripNumericBackend(t *testing.T) { + config, info, execCtx, fixture, path := planFixture(t) + execCtx.preparedConfiguration.Backend["port"] = 1234 + execCtx.preparedConfiguration.Backend["timeout"] = json.Number("1.0") + require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"plan", "-out", filepath.Base(path)})) + fixture.state.Backend["port"] = float64(1234) + fixture.state.Backend["timeout"] = 1 + info.SubCommand, info.UseTerraformPlan, info.PlanFile = "apply", true, path + _, err := restoreConfigurationPlan(t.Context(), config, info) + require.NoError(t, err) + require.False(t, equalBackendConfiguration(map[string]any{"value": json.Number("9007199254740993")}, map[string]any{"value": json.Number("9007199254740992")})) +} diff --git a/pkg/component/terraform/configuration_test.go b/pkg/component/terraform/configuration_test.go index 437f3ec3f5d..5791743c6c1 100644 --- a/pkg/component/terraform/configuration_test.go +++ b/pkg/component/terraform/configuration_test.go @@ -1,7 +1,6 @@ package terraform import ( - "context" "errors" "os" "path/filepath" @@ -193,16 +192,6 @@ func TestPreparedConfigurationSensitiveInputsNeverHitDisk(t *testing.T) { } } -// failingStateRuntime owns the component but cannot identify its backend. -type failingStateRuntime struct { - schema.ComponentConfigurationRuntime - err error -} - -func (r failingStateRuntime) ResolveState(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { - return nil, r.err -} - func TestGetTerraformStateStopsWhenProviderCannotResolveBackend(t *testing.T) { failure := errors.New("provider could not locate state") executor := testExecutor(t) @@ -214,7 +203,9 @@ func TestGetTerraformStateStopsWhenProviderCannotResolveBackend(t *testing.T) { "backend": map[string]any{"path": "must-not-be-read"}, }, nil } - config := &schema.AtmosConfiguration{ComponentConfigurations: failingStateRuntime{err: failure}} + runtime := NewMockReviewedPlanRuntime(gomock.NewController(t)) + runtime.EXPECT().ResolveState(gomock.Any(), gomock.Any()).Return(nil, failure) + config := &schema.AtmosConfiguration{ComponentConfigurations: runtime} result, err := executor.GetTerraformState(config, "!terraform.state", "live", "network", "vpc_id", true, nil, nil) require.ErrorIs(t, err, failure, "the provider's failure is returned as is, not retried against a guessed backend") diff --git a/pkg/component/terraform/configuration_toolchain_test.go b/pkg/component/terraform/configuration_toolchain_test.go index cce174445b6..c7e40ffb952 100644 --- a/pkg/component/terraform/configuration_toolchain_test.go +++ b/pkg/component/terraform/configuration_toolchain_test.go @@ -12,6 +12,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" errUtils "github.com/cloudposse/atmos/errors" "github.com/cloudposse/atmos/pkg/dependencies" @@ -77,7 +78,7 @@ func readToolchainProbes(t *testing.T, path string) []toolchainProbe { } // When the process PATH already starts with the toolchain directories (for example an Atmos run -// nested inside a toolchain shell), there is nothing left to prepend: the selected PATH is used as is. +// nested inside a toolchain shell), replacing PATH still preserves the toolchain directories. func TestToolchainPathEnvWithToolchainDirectoriesAlreadyInAmbientPath(t *testing.T) { harness := newToolchainHarness(t, "1.0.0") info := harness.info(t, &toolchainScenario{identity: "development", tofuVersion: "1.0.0", toolVersion: "1.0.0"}, filepath.Join(t.TempDir(), "probe.jsonl")) @@ -90,12 +91,12 @@ func TestToolchainPathEnvWithToolchainDirectoriesAlreadyInAmbientPath(t *testing selected := filepath.Join(t.TempDir(), "selected") got := toolchainPathEnv(&schema.ConfigAndStacksInfo{ComponentEnvList: []string{"PATH=" + selected}}, tenv) - require.Equal(t, []string{"PATH=" + selected}, got) + require.Equal(t, []string{"PATH=" + tenv.PrependToPath(selected)}, got) - // An empty selected PATH is the same case with nothing to extend: only the toolchain prefix remains, which is empty. + // An empty selected PATH retains only the toolchain directories. got = toolchainPathEnv(&schema.ConfigAndStacksInfo{ComponentEnvList: []string{"PATH="}}, tenv) - require.Equal(t, []string{"PATH="}, got) - require.False(t, strings.Contains(got[0], full), "the ambient toolchain directories must not be re-added") + require.Equal(t, []string{"PATH=" + tenv.PrependToPath("")}, got) + require.NotEmpty(t, tenv.ToolchainDirs()) } // installToolchainFixture places a hard link (or copy) of the test binary where the real @@ -141,51 +142,41 @@ func pathContains(path string) (string, bool) { return "", false } -// recordingConfigurationProvider is a native configuration provider that records what the -// host sent over the plugin boundary. It stands in only for the out-of-process plugin. +// recordingConfigurationProvider stores observations from generated mock callbacks. type recordingConfigurationProvider struct { mu sync.Mutex events *[]string requests []*pluginv1.ComponentRequest } -func (p *recordingConfigurationProvider) ComponentType() string { return "terraform" } - -func (p *recordingConfigurationProvider) Discover(context.Context, *pluginv1.DiscoverComponentsRequest) (*pluginv1.DiscoverComponentsResponse, error) { - configuration, err := adapter.EncodeObject(map[string]any{"source": "./module", "vars": map[string]any{}}) - if err != nil { - return nil, err - } - return &pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{{ - Address: &pluginv1.ComponentAddress{Stack: "live", Component: "app", ComponentType: "terraform"}, - Provider: "terragrunt", Engine: "opentofu", Source: "live/app/terragrunt.hcl", Configuration: configuration, - }}}, nil -} - -func (p *recordingConfigurationProvider) Prepare(_ context.Context, request *pluginv1.ComponentRequest) (*pluginv1.PrepareComponentResponse, error) { - p.mu.Lock() - defer p.mu.Unlock() - *p.events = append(*p.events, "evaluate") - p.requests = append(p.requests, request) - inputs, err := adapter.EncodeObject(map[string]any{}) - if err != nil { - return nil, err - } - backend, err := adapter.EncodeObject(map[string]any{"path": "terraform.tfstate"}) - if err != nil { - return nil, err - } - return &pluginv1.PrepareComponentResponse{Engine: "opentofu", ModuleSource: "./module", Inputs: inputs, State: &pluginv1.ComponentStateIdentity{BackendType: "local", Backend: backend, Workspace: "default"}}, nil -} - -// ResolveState reports the same local state Prepare returns; the single discovered unit has -// no output dependencies, so preparation never needs it. -func (p *recordingConfigurationProvider) ResolveState(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { +func newRecordingConfigurationProvider(t *testing.T, events *[]string) (*MockConfigurationProvider, *recordingConfigurationProvider) { + t.Helper() + p := &recordingConfigurationProvider{events: events} + provider := NewMockConfigurationProvider(gomock.NewController(t)) + provider.EXPECT().ComponentType().Return("terraform").AnyTimes() + provider.EXPECT().Discover(gomock.Any(), gomock.Any()).DoAndReturn(func(context.Context, *pluginv1.DiscoverComponentsRequest) (*pluginv1.DiscoverComponentsResponse, error) { + configuration, err := adapter.EncodeObject(map[string]any{"source": "./module", "vars": map[string]any{}}) + if err != nil { + return nil, err + } + return &pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{{ + Address: &pluginv1.ComponentAddress{Stack: "live", Component: "app", ComponentType: "terraform"}, + Provider: "terragrunt", Engine: "opentofu", Source: "live/app/terragrunt.hcl", Configuration: configuration, + }}}, nil + }).AnyTimes() backend, err := adapter.EncodeObject(map[string]any{"path": "terraform.tfstate"}) - if err != nil { - return nil, err - } - return &pluginv1.ResolveComponentStateResponse{State: &pluginv1.ComponentStateIdentity{BackendType: "local", Backend: backend, Workspace: "default"}}, nil + require.NoError(t, err) + inputs, err := adapter.EncodeObject(map[string]any{}) + require.NoError(t, err) + provider.EXPECT().Prepare(gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, request *pluginv1.ComponentRequest) (*pluginv1.PrepareComponentResponse, error) { + p.mu.Lock() + defer p.mu.Unlock() + *p.events = append(*p.events, "evaluate") + p.requests = append(p.requests, request) + return &pluginv1.PrepareComponentResponse{Engine: "opentofu", ModuleSource: "./module", Inputs: inputs, State: &pluginv1.ComponentStateIdentity{BackendType: "local", Backend: backend, Workspace: "default"}}, nil + }).AnyTimes() + provider.EXPECT().ResolveState(gomock.Any(), gomock.Any()).Return(&pluginv1.ResolveComponentStateResponse{State: &pluginv1.ComponentStateIdentity{BackendType: "local", Backend: backend, Workspace: "default"}}, nil).AnyTimes() + return provider, p } // toolchainScenario is one component execution: an identity and the tool versions it pins. @@ -240,9 +231,10 @@ func newToolchainHarness(t *testing.T, versions ...string) *toolchainHarness { harness.tools["fixture-tool@"+version] = installToolchainFixture(t, config.Toolchain.InstallPath, "acme", "fixture-tool", version, "fixture-tool") } - harness.provider = &recordingConfigurationProvider{events: &harness.events} + provider, recorder := newRecordingConfigurationProvider(t, &harness.events) + harness.provider = recorder session, err := configadapter.New(t.Context(), config, "plan", func(context.Context, []string) (map[string]configadapter.Provider, error) { - return map[string]configadapter.Provider{"terragrunt": harness.provider}, nil + return map[string]configadapter.Provider{"terragrunt": provider}, nil }) require.NoError(t, err) config.ComponentConfigurations = session diff --git a/pkg/component/terraform/execute_environment.go b/pkg/component/terraform/execute_environment.go index be777691281..e13027ce2db 100644 --- a/pkg/component/terraform/execute_environment.go +++ b/pkg/component/terraform/execute_environment.go @@ -126,27 +126,17 @@ func assembleComponentEnvVars(atmosConfig *schema.AtmosConfiguration, info *sche // discarded by it, so the toolchain directories are prepended to that PATH instead. Without a // component-specific PATH this is exactly the toolchain PATH. func toolchainPathEnv(info *schema.ConfigAndStacksInfo, tenv *dependencies.ToolchainEnvironment) []string { - full := tenv.PATH() - if full == "" { + if tenv.PATH() == "" { return tenv.EnvVars() } selected, ok := lastEnvListValue(info.ComponentEnvList, envPath) if !ok { selected, ok = lastEnvListValue(info.SanitizedEnv, envPath) } - ambient := os.Getenv(envPath) //nolint:forbidigo // PATH is the process search path, not an Atmos configuration variable. - if !ok || selected == ambient || !strings.HasSuffix(full, ambient) { + if !ok { return tenv.EnvVars() } - // The toolchain PATH is ""; swap the ambient tail. - prefix := strings.TrimSuffix(strings.TrimSuffix(full, ambient), string(os.PathListSeparator)) - if selected == "" { - return []string{envPath + envAssign + prefix} - } - if prefix == "" { - return []string{envPath + envAssign + selected} - } - return []string{envPath + envAssign + prefix + string(os.PathListSeparator) + selected} + return []string{envPath + envAssign + tenv.PrependToPath(selected)} } // lastEnvListValue returns the value of the last KEY=value entry for key; later entries win. diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index 1d533f3725c..7193ddf145b 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -305,7 +305,7 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every } // An explicit -detailed-exitcode plan reports changes with status 2 after writing the // plan, so the reviewed plan is recorded before that status is returned. - if mainErr != nil && !isExplicitDetailedPlanChanges(info, allArgsAndFlags, mainErr) { + if mainErr != nil && !isExplicitDetailedPlanChanges(info, effectiveArgs, mainErr) { return mainErr } // Policy plans run against a private artifact that is removed after the guard diff --git a/pkg/component/terraform/mock_configuration_provider_test.go b/pkg/component/terraform/mock_configuration_provider_test.go new file mode 100644 index 00000000000..d214c882de3 --- /dev/null +++ b/pkg/component/terraform/mock_configuration_provider_test.go @@ -0,0 +1,101 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: github.com/cloudposse/atmos/pkg/plugin/adapter/configuration (interfaces: Provider) +// +// Generated by this command: +// +// mockgen -destination=pkg/component/terraform/mock_configuration_provider_test.go -package=terraform -mock_names=Provider=MockConfigurationProvider github.com/cloudposse/atmos/pkg/plugin/adapter/configuration Provider +// + +// Package terraform is a generated GoMock package. +package terraform + +import ( + context "context" + reflect "reflect" + + pluginv1 "github.com/cloudposse/atmos/pkg/plugin/proto/atmos/plugin/v1" + gomock "go.uber.org/mock/gomock" +) + +// MockConfigurationProvider is a mock of Provider interface. +type MockConfigurationProvider struct { + ctrl *gomock.Controller + recorder *MockConfigurationProviderMockRecorder + isgomock struct{} +} + +// MockConfigurationProviderMockRecorder is the mock recorder for MockConfigurationProvider. +type MockConfigurationProviderMockRecorder struct { + mock *MockConfigurationProvider +} + +// NewMockConfigurationProvider creates a new mock instance. +func NewMockConfigurationProvider(ctrl *gomock.Controller) *MockConfigurationProvider { + mock := &MockConfigurationProvider{ctrl: ctrl} + mock.recorder = &MockConfigurationProviderMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockConfigurationProvider) EXPECT() *MockConfigurationProviderMockRecorder { + return m.recorder +} + +// ComponentType mocks base method. +func (m *MockConfigurationProvider) ComponentType() string { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ComponentType") + ret0, _ := ret[0].(string) + return ret0 +} + +// ComponentType indicates an expected call of ComponentType. +func (mr *MockConfigurationProviderMockRecorder) ComponentType() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ComponentType", reflect.TypeOf((*MockConfigurationProvider)(nil).ComponentType)) +} + +// Discover mocks base method. +func (m *MockConfigurationProvider) Discover(arg0 context.Context, arg1 *pluginv1.DiscoverComponentsRequest) (*pluginv1.DiscoverComponentsResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Discover", arg0, arg1) + ret0, _ := ret[0].(*pluginv1.DiscoverComponentsResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Discover indicates an expected call of Discover. +func (mr *MockConfigurationProviderMockRecorder) Discover(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Discover", reflect.TypeOf((*MockConfigurationProvider)(nil).Discover), arg0, arg1) +} + +// Prepare mocks base method. +func (m *MockConfigurationProvider) Prepare(arg0 context.Context, arg1 *pluginv1.ComponentRequest) (*pluginv1.PrepareComponentResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Prepare", arg0, arg1) + ret0, _ := ret[0].(*pluginv1.PrepareComponentResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// Prepare indicates an expected call of Prepare. +func (mr *MockConfigurationProviderMockRecorder) Prepare(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Prepare", reflect.TypeOf((*MockConfigurationProvider)(nil).Prepare), arg0, arg1) +} + +// ResolveState mocks base method. +func (m *MockConfigurationProvider) ResolveState(arg0 context.Context, arg1 *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "ResolveState", arg0, arg1) + ret0, _ := ret[0].(*pluginv1.ResolveComponentStateResponse) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// ResolveState indicates an expected call of ResolveState. +func (mr *MockConfigurationProviderMockRecorder) ResolveState(arg0, arg1 any) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ResolveState", reflect.TypeOf((*MockConfigurationProvider)(nil).ResolveState), arg0, arg1) +} diff --git a/pkg/plugin/adapter/configuration/preparation_test.go b/pkg/plugin/adapter/configuration/preparation_test.go index 1cc3ce64c8d..b7a699bfa26 100644 --- a/pkg/plugin/adapter/configuration/preparation_test.go +++ b/pkg/plugin/adapter/configuration/preparation_test.go @@ -156,15 +156,6 @@ func TestPreparedExecutionRejectsNilEntriesInEveryList(t *testing.T) { } } -type stateProviderFixture struct { - Provider - resolve func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) -} - -func (p *stateProviderFixture) ResolveState(ctx context.Context, req *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { - return p.resolve(ctx, req) -} - func TestSessionResolvesStateWithoutPreparation(t *testing.T) { config := enabledConfig(t) mock := NewMockProvider(gomock.NewController(t)) @@ -172,15 +163,15 @@ func TestSessionResolvesStateWithoutPreparation(t *testing.T) { mock.EXPECT().Discover(gomock.Any(), gomock.Any()).Return(&pluginv1.DiscoverComponentsResponse{Components: []*pluginv1.DiscoveredComponent{unit(t, "network")}}, nil) backend, err := adapter.EncodeObject(map[string]any{"path": "state.tfstate"}) require.NoError(t, err) - provider := &stateProviderFixture{Provider: mock, resolve: func(_ context.Context, req *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + mock.EXPECT().ResolveState(gomock.Any(), gomock.Any()).DoAndReturn(func(_ context.Context, req *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { require.Nil(t, req.Effects) require.Equal(t, "output", req.Operation.Command) require.Equal(t, "selected", req.Invocation.Environment["IDENTITY"]) require.Equal(t, "stage", req.Invocation.Environment["TF_WORKSPACE"]) return &pluginv1.ResolveComponentStateResponse{State: &pluginv1.ComponentStateIdentity{BackendType: "local", Backend: backend, Workspace: "stage", WorkingDirectory: config.BasePathAbsolute}}, nil - }} + }) session, err := New(t.Context(), config, "describe", func(context.Context, []string) (map[string]Provider, error) { - return map[string]Provider{"terragrunt": provider}, nil + return map[string]Provider{"terragrunt": mock}, nil }) require.NoError(t, err) info := &schema.ConfigAndStacksInfo{Stack: "live", ComponentFromArg: "network", SubCommand: "output", TerraformWorkspace: "stage", ComponentSettingsSection: map[string]any{"configuration": true}, SanitizedEnv: []string{"IDENTITY=selected"}} @@ -188,19 +179,19 @@ func TestSessionResolvesStateWithoutPreparation(t *testing.T) { require.NoError(t, err) require.Equal(t, filepath.Join(config.BasePathAbsolute, "state.tfstate"), state.Backend["path"]) require.Equal(t, "stage", state.Workspace) - provider.resolve = func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + mock.EXPECT().ResolveState(gomock.Any(), gomock.Any()).DoAndReturn(func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { return nil, context.Canceled - } + }) _, err = session.ResolveState(t.Context(), info) require.ErrorIs(t, err, context.Canceled) - provider.resolve = func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + mock.EXPECT().ResolveState(gomock.Any(), gomock.Any()).DoAndReturn(func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { return &pluginv1.ResolveComponentStateResponse{}, nil - } + }) _, err = session.ResolveState(t.Context(), info) require.ErrorIs(t, err, errUtils.ErrPluginProtocol) - provider.resolve = func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { + mock.EXPECT().ResolveState(gomock.Any(), gomock.Any()).DoAndReturn(func(context.Context, *pluginv1.ComponentRequest) (*pluginv1.ResolveComponentStateResponse, error) { return &pluginv1.ResolveComponentStateResponse{State: &pluginv1.ComponentStateIdentity{Backend: &pluginv1.ComponentValue{}}}, nil - } + }) _, err = session.ResolveState(t.Context(), info) require.ErrorIs(t, err, errUtils.ErrPluginProtocol) info.ComponentFromArg = "absent" diff --git a/plugins/terragrunt/internal/engine/execution.go b/plugins/terragrunt/internal/engine/execution.go index db81dcef94b..afd1716d7fc 100644 --- a/plugins/terragrunt/internal/engine/execution.go +++ b/plugins/terragrunt/internal/engine/execution.go @@ -72,13 +72,21 @@ func applyTimeExecution(request model.Request, path string, planned *model.Execu if request.Command != "plan" || !observesCommand(filesystem, append(slices.Clone(read), path)) { return planned, nil, nil } + if err := rejectArgumentDependentExecution(filesystem, append(slices.Clone(read), path)); err != nil { + return nil, nil, err + } apply := parsingContext(path, request.Tofu, "apply", nil) + // Dependency mock eligibility belongs to the plan being prepared; hook expressions see apply. + apply.OriginalTerraformCommand = request.Command cfg, err := parse(apply) if err != nil { return nil, nil, err } // Configuration read only when evaluating for apply also shapes the reviewed plan. files := apply.FilesRead.Paths() + if err := rejectArgumentDependentExecution(filesystem, files); err != nil { + return nil, nil, err + } applied, err := executionFromConfig(cfg.Terraform) if err != nil { return nil, nil, err @@ -201,3 +209,14 @@ func validateHook(kind string, hook *model.Hook) error { } return nil } + +// rejectArgumentDependentExecution fails closed until saved apply arguments can be bound. +func rejectArgumentDependentExecution(filesystem afero.Fs, paths []string) error { + for _, path := range paths { + content, err := afero.ReadFile(filesystem, path) + if err != nil || bytes.Contains(content, []byte("get_terraform_cli_args")) { + return fmt.Errorf("%w: argument-dependent saved-plan execution cannot be bound before apply", ErrUnsupportedExecution) + } + } + return nil +} diff --git a/plugins/terragrunt/internal/engine/execution_test.go b/plugins/terragrunt/internal/engine/execution_test.go index 638e385b17a..8c988c1bb2a 100644 --- a/plugins/terragrunt/internal/engine/execution_test.go +++ b/plugins/terragrunt/internal/engine/execution_test.go @@ -307,3 +307,56 @@ func hookNames(hooks []model.Hook) []string { } return names } + +func TestPlanRejectsArgumentDependentSavedHooks(t *testing.T) { + request := executionUnit(t, `before_hook "args" { + commands = ["apply"] + execute = ["echo", join(",", get_terraform_cli_args())] +}`) + _, err := Resolve(t.Context(), request) + require.ErrorIs(t, err, ErrUnsupportedExecution) +} + +func TestPreventDestroyChecksExpandedArguments(t *testing.T) { + request := executionUnit(t, `extra_arguments "destroy" { + commands = ["plan"] + arguments = ["-destroy"] +}`) + file, err := os.OpenFile(request.Path, os.O_APPEND|os.O_WRONLY, 0o600) + require.NoError(t, err) + _, err = file.WriteString("\nprevent_destroy = true\n") + require.NoError(t, err) + require.NoError(t, file.Close()) + _, err = Resolve(t.Context(), request) + require.ErrorIs(t, err, ErrPreventDestroy) + request.Arguments = []string{"-destroy=false"} + _, err = Resolve(t.Context(), request) + require.NoError(t, err) +} + +func TestPlanApplyMetadataReusesPlanApprovedMocks(t *testing.T) { + request := executionUnit(t, `before_hook "command" { + commands = ["plan", "apply"] + execute = ["echo", get_terraform_command()] +}`) + dependency := filepath.Join(request.BasePath, "dependency") + require.NoError(t, os.MkdirAll(dependency, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(dependency, "terragrunt.hcl"), []byte("inputs = {}"), 0o644)) + file, err := os.OpenFile(request.Path, os.O_APPEND|os.O_WRONLY, 0o600) + require.NoError(t, err) + _, err = file.WriteString(` +dependency "upstream" { + config_path = "./dependency" + skip_outputs = true + mock_outputs = { value = "planned" } + mock_outputs_allowed_terraform_commands = ["plan"] +} +inputs = { value = dependency.upstream.outputs.value } +`) + require.NoError(t, err) + require.NoError(t, file.Close()) + result, err := Resolve(t.Context(), request) + require.NoError(t, err) + require.Equal(t, "planned", result.Inputs["value"]) + require.NotNil(t, result.Execution.Apply) +} diff --git a/plugins/terragrunt/internal/engine/policies.go b/plugins/terragrunt/internal/engine/policies.go index 3989c7204e3..6360dfb4d12 100644 --- a/plugins/terragrunt/internal/engine/policies.go +++ b/plugins/terragrunt/internal/engine/policies.go @@ -6,6 +6,7 @@ import ( "fmt" "os/exec" "path/filepath" + "slices" "strings" "github.com/gruntwork-io/terragrunt/pkg/atmosbridge" @@ -87,7 +88,7 @@ func checkUnitPolicies(ctx context.Context, request model.Request, cfg *config.T return fmt.Errorf("native adaptation of terraform_binary is not implemented") } // Hooks and extra arguments are adapted as execution data by unitExecution. - if cfg.PreventDestroy != nil && *cfg.PreventDestroy && atmosbridge.IsDestroyOperation(request.Command, request.Arguments) { + if cfg.PreventDestroy != nil && *cfg.PreventDestroy && atmosbridge.IsDestroyOperation(request.Command, effectivePolicyArguments(request, cfg)) { return ErrPreventDestroy } if cfg.Errors != nil || cfg.Exclude != nil || len(cfg.FeatureFlags) > 0 || cfg.IamRole != "" || cfg.IamWebIdentityToken != "" { @@ -107,3 +108,14 @@ func validateRemoteSource(source string) error { } return fmt.Errorf("native adapter does not support this module source transport") } + +// effectivePolicyArguments follows host ordering: extra arguments precede user arguments. +func effectivePolicyArguments(request model.Request, cfg *config.TerragruntConfig) []string { + var args []string + for _, extra := range cfg.Terraform.ExtraArgs { + if slices.Contains(extra.Commands, request.Command) && extra.Arguments != nil { + args = append(args, (*extra.Arguments)...) + } + } + return append(args, request.Arguments...) +} diff --git a/plugins/terragrunt/internal/provider/provider.go b/plugins/terragrunt/internal/provider/provider.go index 853fc29f726..994fcc78251 100644 --- a/plugins/terragrunt/internal/provider/provider.go +++ b/plugins/terragrunt/internal/provider/provider.go @@ -169,6 +169,9 @@ func rpcError(err error) error { if errors.Is(err, engine.ErrUnsupportedExecution) { return status.Error(codes.Unimplemented, err.Error()) } + if errors.Is(err, engine.ErrPreventDestroy) { + return status.Error(codes.FailedPrecondition, engine.ErrPreventDestroy.Error()) + } result := status.New(codes.InvalidArgument, "Terragrunt evaluation failed") var diagnostic *engine.DiscoveryError if errors.As(err, &diagnostic) { diff --git a/plugins/terragrunt/internal/provider/provider_test.go b/plugins/terragrunt/internal/provider/provider_test.go index f3155a9426f..8b93566624d 100644 --- a/plugins/terragrunt/internal/provider/provider_test.go +++ b/plugins/terragrunt/internal/provider/provider_test.go @@ -269,3 +269,9 @@ func TestDiscoveryDiagnosticsRedactPayloads(t *testing.T) { }) } } + +func TestRPCErrorPreservesDestroyProtection(t *testing.T) { + err := rpcError(fmt.Errorf("private evaluator context: %w", engine.ErrPreventDestroy)) + require.Equal(t, codes.FailedPrecondition, status.Code(err)) + require.Equal(t, engine.ErrPreventDestroy.Error(), status.Convert(err).Message()) +} From 47decc5ab36324b8df946e4f715b0796e05dc1a6 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sat, 14 Nov 2026 14:00:56 -0600 Subject: [PATCH 39/52] fix(terragrunt): evaluate apply-time execution with the request's download directory The apply-time evaluation used the default .terragrunt-cache and ignored TG_DOWNLOAD_DIR, so an apply-only hook using get_working_dir() recorded a different working directory than the plan-time evaluation. Build it with requestParsingContext instead. Co-Authored-By: Claude Sonnet 5.5 --- .../terragrunt/internal/engine/execution.go | 6 +++++- .../internal/engine/execution_test.go | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/plugins/terragrunt/internal/engine/execution.go b/plugins/terragrunt/internal/engine/execution.go index afd1716d7fc..e2c3eeb95cf 100644 --- a/plugins/terragrunt/internal/engine/execution.go +++ b/plugins/terragrunt/internal/engine/execution.go @@ -75,7 +75,11 @@ func applyTimeExecution(request model.Request, path string, planned *model.Execu if err := rejectArgumentDependentExecution(filesystem, append(slices.Clone(read), path)); err != nil { return nil, nil, err } - apply := parsingContext(path, request.Tofu, "apply", nil) + // Apply is evaluated with the request's own download directory, so the working directory + // matches the plan-time evaluation. + applyRequest := request + applyRequest.Command, applyRequest.Arguments = "apply", nil + apply := requestParsingContext(applyRequest, path) // Dependency mock eligibility belongs to the plan being prepared; hook expressions see apply. apply.OriginalTerraformCommand = request.Command cfg, err := parse(apply) diff --git a/plugins/terragrunt/internal/engine/execution_test.go b/plugins/terragrunt/internal/engine/execution_test.go index 8c988c1bb2a..57b4949041c 100644 --- a/plugins/terragrunt/internal/engine/execution_test.go +++ b/plugins/terragrunt/internal/engine/execution_test.go @@ -236,6 +236,25 @@ func TestPlanPreparationRecordsApplyTimeExecution(t *testing.T) { require.Equal(t, planned.Execution.Apply.AfterHooks, applied.Execution.AfterHooks) } +// The apply-time evaluation uses the same download directory as the plan-time evaluation, so a +// hook that reports the working directory sees the TG_DOWNLOAD_DIR override in both. +func TestPlanPreparationApplyTimeUsesDownloadDirectory(t *testing.T) { + request := executionUnit(t, ` + after_hook "where" { + commands = ["apply"] + execute = ["tool", get_working_dir()] + if = get_terraform_command() == "apply" + } +`) + request.Environment["TG_DOWNLOAD_DIR"] = "environment-cache" + result, err := Resolve(t.Context(), request) + require.NoError(t, err) + require.NotNil(t, result.Execution.Apply) + require.Len(t, result.Execution.Apply.AfterHooks, 1) + require.Contains(t, result.Execution.Apply.AfterHooks[0].Execute[1], "environment-cache") + require.NotContains(t, result.Execution.Apply.AfterHooks[0].Execute[1], ".terragrunt-cache") +} + // Configuration that only the apply-time evaluation reads also shapes the reviewed plan, so the // plan's watched sources include it. func TestPlanPreparationWatchesApplyTimeReads(t *testing.T) { From d85769c6f56bcce1905d0c770e60db8a08982123 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sat, 14 Nov 2026 14:01:30 -0600 Subject: [PATCH 40/52] fix(terraform): harden plan preflight and toolchain PATH selection - A failed hook is never plan's detailed-exitcode "changes present" status, even when the hook itself exits 2, so no reviewed-plan companion is recorded for it. - The TF_CLI_ARGS and TF_CLI_ARGS_plan output-flag preflight matches variable names case-insensitively on Windows only, with the last matching entry effective. - toolchainPathEnv compares the PATH name case-insensitively on Windows so a component or identity Path selection is extended, not discarded. Co-Authored-By: Claude Sonnet 5.5 --- pkg/component/terraform/configuration_plan.go | 17 +++++---- .../terraform/configuration_plan_test.go | 38 +++++++++++++++++++ .../terraform/configuration_toolchain_test.go | 20 ++++++++++ .../terraform/execute_environment.go | 25 ++++++++++-- pkg/component/terraform/execute_pipeline.go | 4 ++ 5 files changed, 93 insertions(+), 11 deletions(-) diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go index fd6311c9286..b3fb25f8a1c 100644 --- a/pkg/component/terraform/configuration_plan.go +++ b/pkg/component/terraform/configuration_plan.go @@ -13,6 +13,7 @@ import ( "os" "path/filepath" "reflect" + "runtime" "slices" "strings" @@ -238,14 +239,16 @@ func validateEnvironmentPlanOutput(atmosConfig *schema.AtmosConfiguration, info if execCtx.preparedConfiguration == nil || info.DryRun || info.SubCommand != subcommandPlan { return nil } - effective := make(map[string]string) - for _, entry := range planEnvironment(atmosConfig, info) { - if name, value, ok := strings.Cut(entry, "="); ok { - effective[name] = value - } - } + return rejectEnvironmentPlanOutput(planEnvironment(atmosConfig, info), runtime.GOOS) +} + +// rejectEnvironmentPlanOutput rejects a plan output flag in the effective TF_CLI_ARGS or +// TF_CLI_ARGS_plan of an environment listed in precedence order. Variable names are +// case-insensitive on Windows only, where the last matching entry is the effective value. +func rejectEnvironmentPlanOutput(entries []string, goos string) error { for _, name := range []string{tfCliArgsEnvVar, tfCliArgsEnvVar + "_" + subcommandPlan} { - if containsOutputFlag(parseArgs(effective[name])) { + value, _ := lastEnvListValue(entries, name, goos) + if containsOutputFlag(parseArgs(value)) { return fmt.Errorf("%w: %s sets a plan output flag; pass -out as a command argument instead", errUtils.ErrPlanVerificationFailed, name) } } diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index 0462e85a592..51b1d12754f 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "errors" + "fmt" "io" "os" "path/filepath" @@ -313,11 +314,13 @@ func TestDetailedExitCodePlanRecordsReviewedPlan(t *testing.T) { args []string code int record bool + hook bool }{ {name: "explicit detailed exit code with changes", args: []string{"plan", "-detailed-exitcode", "-out", "changes.plan"}, code: 2, record: true}, {name: "provider detailed exit code", extra: true, args: []string{"plan", "-out", "changes.plan"}, code: 2, record: true}, {name: "failure with detailed exit code", args: []string{"plan", "-detailed-exitcode", "-out", "changes.plan"}, code: 1}, {name: "status 2 without detailed exit code", args: []string{"plan", "-out", "changes.plan"}, code: 2}, + {name: "after hook failure with status 2", args: []string{"plan", "-detailed-exitcode", "-out", "changes.plan"}, code: 2, hook: true}, } { t.Run(test.name, func(t *testing.T) { config, info, execCtx, _, _ := planFixture(t) @@ -328,12 +331,19 @@ func TestDetailedExitCodePlanRecordsReviewedPlan(t *testing.T) { if err := os.WriteFile(plannedOutputPath(execCtx.componentPath, call.args), []byte("changes"), 0o600); err != nil { return err } + if test.hook { + // A hook that exits 2 after Terraform wrote the plan is a failure, not "changes present". + return fmt.Errorf("%w: after %q: %w", errUtils.ErrComponentHookFailed, "notify", errUtils.ExitCodeError{Code: test.code}) + } return errUtils.ExitCodeError{Code: test.code} }} executor := testExecutor(t) executor.services.ExecuteShellCommand = shell.execute err := executor.runMainCommandPhase(config, info, execCtx, test.args, execCtx.componentPath, false) require.Equal(t, test.code, resolveExitCode(err), "the plan status is returned unchanged") + if test.hook { + require.ErrorIs(t, err, errUtils.ErrComponentHookFailed) + } binding := filepath.Join(execCtx.componentPath, "changes.plan") + configurationPlanSuffix if test.record { require.FileExists(t, binding) @@ -429,6 +439,34 @@ func TestPlanOutputFlagsInEnvironmentAreRejected(t *testing.T) { } } +// TestRejectEnvironmentPlanOutputNameCase matches the variable names case-insensitively on Windows only. +func TestRejectEnvironmentPlanOutputNameCase(t *testing.T) { + for _, test := range []struct { + name string + goos string + entries []string + reject bool + }{ + {name: "windows mixed-case plan variable", goos: "windows", entries: []string{"tf_cli_args_plan=-out=env.plan"}, reject: true}, + {name: "windows mixed-case shared variable", goos: "windows", entries: []string{"Tf_Cli_Args=--out env.plan"}, reject: true}, + {name: "windows later case variant clears the flag", goos: "windows", entries: []string{"TF_CLI_ARGS_plan=-out=env.plan", "tf_cli_args_PLAN=-lock=false"}}, + {name: "windows later case variant sets the flag", goos: "windows", entries: []string{"TF_CLI_ARGS_plan=-lock=false", "tf_cli_args_plan=-out=env.plan"}, reject: true}, + {name: "windows earlier case variant is overridden", goos: "windows", entries: []string{"tf_cli_args=-out=env.plan", "TF_CLI_ARGS="}}, + {name: "linux keeps names exact", goos: "linux", entries: []string{"tf_cli_args_plan=-out=env.plan", "Tf_Cli_Args=-out"}}, + {name: "linux exact name is rejected", goos: "linux", entries: []string{"TF_CLI_ARGS_plan=-out=env.plan"}, reject: true}, + {name: "linux later exact name overrides", goos: "linux", entries: []string{"TF_CLI_ARGS_plan=-out=env.plan", "TF_CLI_ARGS_plan=-lock=false"}}, + } { + t.Run(test.name, func(t *testing.T) { + err := rejectEnvironmentPlanOutput(test.entries, test.goos) + if test.reject { + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + return + } + require.NoError(t, err) + }) + } +} + // TestEnvironmentPlanOutputIgnoresUnownedPlans keeps ordinary components unaffected. func TestEnvironmentPlanOutputIgnoresUnownedPlans(t *testing.T) { t.Setenv("TF_CLI_ARGS_plan", "-out=env.plan") diff --git a/pkg/component/terraform/configuration_toolchain_test.go b/pkg/component/terraform/configuration_toolchain_test.go index c7e40ffb952..cd7b04be323 100644 --- a/pkg/component/terraform/configuration_toolchain_test.go +++ b/pkg/component/terraform/configuration_toolchain_test.go @@ -520,6 +520,26 @@ func TestToolchainPathEnvSelectsThePathToExtend(t *testing.T) { }) } + // Windows names the variable Path or PATH interchangeably; other platforms keep them distinct. + for _, test := range []struct { + name string + goos string + component []string + sanitized []string + want string + }{ + {name: "windows component Path is extended", goos: "windows", component: []string{"Path=/component"}, want: join(dirs, "/component")}, + {name: "windows identity Path is extended", goos: "windows", sanitized: []string{"Path=/identity"}, want: join(dirs, "/identity")}, + {name: "windows last case variant wins", goos: "windows", component: []string{"PATH=/first", "Path=/last"}, want: join(dirs, "/last")}, + {name: "windows component Path beats identity PATH", goos: "windows", component: []string{"Path=/component"}, sanitized: []string{"PATH=/identity"}, want: join(dirs, "/component")}, + {name: "linux Path is not PATH", goos: "linux", component: []string{"Path=/component"}, want: join(dirs, ambient)}, + } { + t.Run(test.name, func(t *testing.T) { + got := toolchainPathEnvForGOOS(&schema.ConfigAndStacksInfo{ComponentEnvList: test.component, SanitizedEnv: test.sanitized}, tenv, test.goos) + require.Equal(t, []string{"PATH=" + test.want}, got) + }) + } + t.Run("without toolchain directories nothing is overridden", func(t *testing.T) { empty := &dependencies.ToolchainEnvironment{} require.Nil(t, toolchainPathEnv(&schema.ConfigAndStacksInfo{ComponentEnvList: []string{"PATH=/component"}}, empty)) diff --git a/pkg/component/terraform/execute_environment.go b/pkg/component/terraform/execute_environment.go index e13027ce2db..fda4b181827 100644 --- a/pkg/component/terraform/execute_environment.go +++ b/pkg/component/terraform/execute_environment.go @@ -5,6 +5,7 @@ import ( fmt "fmt" os "os" filepath "path/filepath" + runtime "runtime" slices "slices" strings "strings" @@ -126,12 +127,18 @@ func assembleComponentEnvVars(atmosConfig *schema.AtmosConfiguration, info *sche // discarded by it, so the toolchain directories are prepended to that PATH instead. Without a // component-specific PATH this is exactly the toolchain PATH. func toolchainPathEnv(info *schema.ConfigAndStacksInfo, tenv *dependencies.ToolchainEnvironment) []string { + return toolchainPathEnvForGOOS(info, tenv, runtime.GOOS) +} + +// toolchainPathEnvForGOOS is toolchainPathEnv parameterized by GOOS so the Windows +// case-insensitive PATH name (Path, PATH) can be unit-tested from any host platform. +func toolchainPathEnvForGOOS(info *schema.ConfigAndStacksInfo, tenv *dependencies.ToolchainEnvironment, goos string) []string { if tenv.PATH() == "" { return tenv.EnvVars() } - selected, ok := lastEnvListValue(info.ComponentEnvList, envPath) + selected, ok := lastEnvListValue(info.ComponentEnvList, envPath, goos) if !ok { - selected, ok = lastEnvListValue(info.SanitizedEnv, envPath) + selected, ok = lastEnvListValue(info.SanitizedEnv, envPath, goos) } if !ok { return tenv.EnvVars() @@ -139,11 +146,21 @@ func toolchainPathEnv(info *schema.ConfigAndStacksInfo, tenv *dependencies.Toolc return []string{envPath + envAssign + tenv.PrependToPath(selected)} } +// envNamesEqual reports whether two environment variable names are the same variable on goos. +// Windows names are case-insensitive; every other platform compares them exactly. +func envNamesEqual(a, b, goos string) bool { + if goos == "windows" { + return strings.EqualFold(a, b) + } + return a == b +} + // lastEnvListValue returns the value of the last KEY=value entry for key; later entries win. -func lastEnvListValue(list []string, key string) (string, bool) { +// Names compare case-insensitively on Windows only. +func lastEnvListValue(list []string, key, goos string) (string, bool) { value, found := "", false for _, entry := range list { - if k, v, ok := strings.Cut(entry, envAssign); ok && k == key { + if k, v, ok := strings.Cut(entry, envAssign); ok && envNamesEqual(k, key, goos) { value, found = v, true } } diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index 7193ddf145b..0907fac71d6 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -321,7 +321,11 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every } // isExplicitDetailedPlanChanges reports plan's "changes present" status from -detailed-exitcode. +// A failed hook is never that status, even when the hook itself exited with 2. func isExplicitDetailedPlanChanges(info *schema.ConfigAndStacksInfo, args []string, err error) bool { + if errors.Is(err, errUtils.ErrComponentHookFailed) { + return false + } return info.SubCommand == subcommandPlan && slices.Contains(args, detailedExitCodeFlag) && resolveExitCode(err) == detailedExitCodeChangesDetected } From f738b5cf26647b84459b1693f139c4abfa9abb9e Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sat, 14 Nov 2026 22:00:39 -0600 Subject: [PATCH 41/52] fix(terraform): tokenize TF_CLI_ARGS with the engine's rules when guarding plan output The plan output guard split TF_CLI_ARGS values on spaces only, so a tab or newline before -out hid the flag from the check while the engine still honored it. Split on all whitespace outside quotes and reject values the engine cannot parse. Co-Authored-By: Claude Sonnet 5.5 --- pkg/component/terraform/configuration_plan.go | 17 ++++++++++++++++- .../terraform/configuration_plan_test.go | 5 +++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go index b3fb25f8a1c..13874acb975 100644 --- a/pkg/component/terraform/configuration_plan.go +++ b/pkg/component/terraform/configuration_plan.go @@ -17,6 +17,8 @@ import ( "slices" "strings" + "github.com/google/shlex" + errUtils "github.com/cloudposse/atmos/errors" envpkg "github.com/cloudposse/atmos/pkg/env" "github.com/cloudposse/atmos/pkg/schema" @@ -248,13 +250,26 @@ func validateEnvironmentPlanOutput(atmosConfig *schema.AtmosConfiguration, info func rejectEnvironmentPlanOutput(entries []string, goos string) error { for _, name := range []string{tfCliArgsEnvVar, tfCliArgsEnvVar + "_" + subcommandPlan} { value, _ := lastEnvListValue(entries, name, goos) - if containsOutputFlag(parseArgs(value)) { + args, err := tokenizeEnvironmentArgs(value) + if err != nil { + return fmt.Errorf("%w: %s cannot be parsed: %w", errUtils.ErrPlanVerificationFailed, name, err) + } + if containsOutputFlag(args) { return fmt.Errorf("%w: %s sets a plan output flag; pass -out as a command argument instead", errUtils.ErrPlanVerificationFailed, name) } } return nil } +// tokenizeEnvironmentArgs splits a TF_CLI_ARGS value the way the engine does: +// spaces, tabs, and newlines separate tokens outside quotes, and a malformed +// value (for example an unterminated quote) is an error because the engine +// refuses to run with it. The shared parseArgs only splits on spaces, so a tab +// before -out would hide the flag from the check. +func tokenizeEnvironmentArgs(value string) ([]string, error) { + return shlex.Split(value) +} + // planEnvironment lists the environment layers a plan subprocess receives, in precedence order. func planEnvironment(atmosConfig *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo) []string { base := info.SanitizedEnv diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index 51b1d12754f..6237e5864f2 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -455,6 +455,11 @@ func TestRejectEnvironmentPlanOutputNameCase(t *testing.T) { {name: "linux keeps names exact", goos: "linux", entries: []string{"tf_cli_args_plan=-out=env.plan", "Tf_Cli_Args=-out"}}, {name: "linux exact name is rejected", goos: "linux", entries: []string{"TF_CLI_ARGS_plan=-out=env.plan"}, reject: true}, {name: "linux later exact name overrides", goos: "linux", entries: []string{"TF_CLI_ARGS_plan=-out=env.plan", "TF_CLI_ARGS_plan=-lock=false"}}, + {name: "tab separates tokens", goos: "linux", entries: []string{"TF_CLI_ARGS_plan=-lock=false\t-out=hidden.plan"}, reject: true}, + {name: "newline separates tokens", goos: "linux", entries: []string{"TF_CLI_ARGS=-lock=false\n--out hidden.plan"}, reject: true}, + {name: "quoted flag", goos: "linux", entries: []string{"TF_CLI_ARGS_plan=-lock=false \"-out=hidden.plan\""}, reject: true}, + {name: "quoted tab stays inside one token", goos: "linux", entries: []string{"TF_CLI_ARGS_plan=-var 'a\t-out=x'"}}, + {name: "unterminated quote fails to parse", goos: "linux", entries: []string{"TF_CLI_ARGS_plan=-var \"a -lock=false"}, reject: true}, } { t.Run(test.name, func(t *testing.T) { err := rejectEnvironmentPlanOutput(test.entries, test.goos) From ebe0057fc55235784d0727d2549a16316c293b75 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sat, 14 Nov 2026 22:03:21 -0600 Subject: [PATCH 42/52] refactor(terragrunt): split unit resolution out of resolve Extract resolveUnit and buildUnitResolution so resolve is a flat sequence of named steps under the cyclomatic limit. Behavior is unchanged: the execution is still set before the backend resolves and every early return is preserved. Co-Authored-By: Claude Sonnet 5.5 --- plugins/terragrunt/internal/engine/resolve.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/plugins/terragrunt/internal/engine/resolve.go b/plugins/terragrunt/internal/engine/resolve.go index b05c070a594..641118f9681 100644 --- a/plugins/terragrunt/internal/engine/resolve.go +++ b/plugins/terragrunt/internal/engine/resolve.go @@ -57,6 +57,13 @@ func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (* if discovering(request) { return resolveUnitInventory(ctx, logger, environment, pctx, request, path) } + return resolveUnit(ctx, logger, environment, pctx, request, path) +} + +// resolveUnit evaluates one unit: it guards destroy, parses the unit, applies +// policies, and assembles the resolution with its execution data, backend, overlay, +// and generated files. +func resolveUnit(ctx context.Context, logger log.Logger, environment *atmosbridge.Venv, pctx *config.ParsingContext, request model.Request, path string) (*model.Resolution, error) { if err := checkDestroyGuard(ctx, logger, environment, pctx, request, path); err != nil { return nil, err } @@ -67,6 +74,12 @@ func resolve(ctx context.Context, request model.Request, filesystem afero.Fs) (* if err := checkUnitPolicies(ctx, request, cfg); err != nil { return nil, err } + return buildUnitResolution(ctx, logger, environment, pctx, request, cfg, path) +} + +// buildUnitResolution assembles the resolution of a parsed unit. The execution is +// set before the backend resolves so the backend step sees the final result. +func buildUnitResolution(ctx context.Context, logger log.Logger, environment *atmosbridge.Venv, pctx *config.ParsingContext, request model.Request, cfg *config.TerragruntConfig, path string) (*model.Resolution, error) { execution, applyFiles, err := unitExecution(ctx, logger, environment, pctx, request, cfg, path) if err != nil { return nil, err From 4bc6f03026796ab3d9caef66246c234d2c93bf97 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sat, 14 Nov 2026 22:10:09 -0600 Subject: [PATCH 43/52] fix(terraform): bind the saved plan to the configuration Terraform read recordConfigurationPlan recomputed the provider identity and working directory digest after the main command returned, which includes any after hook. A hook that rewrote the module after Terraform read it made the binding record the rewritten bytes, so apply could not detect the change. Snapshot both around the Terraform subprocess, after the before hooks and before the after hooks, bind the observed snapshot, and reject the plan when the configuration changed while Terraform ran. The private plan directory of a plan policy is excluded from the digest because it exists only during the run. Co-Authored-By: Claude Sonnet 5.5 --- pkg/component/terraform/configuration_plan.go | 33 +++--- .../terraform/configuration_plan_test.go | 67 ++++++++++++ pkg/component/terraform/execute_hooks.go | 3 + pkg/component/terraform/execute_pipeline.go | 100 ++++++++++++++++++ 4 files changed, 186 insertions(+), 17 deletions(-) diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go index 13874acb975..b691f8b5bf9 100644 --- a/pkg/component/terraform/configuration_plan.go +++ b/pkg/component/terraform/configuration_plan.go @@ -157,21 +157,18 @@ func verifyConfigurationPlanWorkdir(plan *configurationPlan, path string) error return nil } -// recordConfigurationPlan stores a private companion beside the completed plan. +// recordConfigurationPlan stores a private companion beside the completed plan. It binds the +// configuration Terraform read, which the plan snapshot captured before any after hook ran. func recordConfigurationPlan(ctx context.Context, config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, execCtx *componentExecContext, args []string) error { path := recordedPlanPath(info, execCtx, args) if path == "" { return nil } - runtime, err := planRuntime(config) + snapshot, err := execCtx.reviewedSnapshot(ctx, config, info, path) if err != nil { return err } - identity, err := runtime.PlanIdentity(ctx, info, execCtx.preparedConfiguration.Sources) - if err != nil { - return err - } - plan, err := newConfigurationPlan(info, execCtx, identity, path) + plan, err := newConfigurationPlan(info, execCtx, snapshot, path) if err != nil { return err } @@ -184,14 +181,19 @@ func recordConfigurationPlan(ctx context.Context, config *schema.AtmosConfigurat // recordedPlanPath returns the plan file to bind, or empty when this invocation produces none. func recordedPlanPath(info *schema.ConfigAndStacksInfo, execCtx *componentExecContext, args []string) string { - if execCtx.preparedConfiguration == nil || info.DryRun || info.SubCommand != "plan" { + if !bindsPlan(info, execCtx) { return "" } return plannedOutputPath(execCtx.componentPath, args) } -// newConfigurationPlan binds the identity to the digests of the plan, engine, and working directory. -func newConfigurationPlan(info *schema.ConfigAndStacksInfo, execCtx *componentExecContext, identity, path string) (*configurationPlan, error) { +// bindsPlan reports whether this invocation produces a plan that is bound to its configuration. +func bindsPlan(info *schema.ConfigAndStacksInfo, execCtx *componentExecContext) bool { + return execCtx.preparedConfiguration != nil && !info.DryRun && info.SubCommand == subcommandPlan +} + +// newConfigurationPlan binds the snapshot to the digests of the plan and engine. +func newConfigurationPlan(info *schema.ConfigAndStacksInfo, execCtx *componentExecContext, snapshot *planSnapshot, path string) (*configurationPlan, error) { planDigest, err := fileDigest(path) if err != nil { return nil, err @@ -200,11 +202,7 @@ func newConfigurationPlan(info *schema.ConfigAndStacksInfo, execCtx *componentEx if err != nil { return nil, err } - workingDigest, err := configurationWorkdirDigest(execCtx.workingDir, path) - if err != nil { - return nil, err - } - return &configurationPlan{WorkingDigest: workingDigest, Version: 1, Stack: info.Stack, Component: info.ComponentFromArg, Identity: identity, EngineDigest: engineDigest, PlanDigest: planDigest, WorkingDirectory: filepath.Clean(execCtx.workingDir), Prepared: execCtx.preparedConfiguration}, nil + return &configurationPlan{WorkingDigest: snapshot.workingDigest, Version: 1, Stack: info.Stack, Component: info.ComponentFromArg, Identity: snapshot.identity, EngineDigest: engineDigest, PlanDigest: planDigest, WorkingDirectory: filepath.Clean(execCtx.workingDir), Prepared: execCtx.preparedConfiguration}, nil } // validatePlanOutputFlags rejects ambiguous plan destinations before hooks or Terraform run. @@ -417,7 +415,8 @@ func configurationWorkdirDigest(root, planPath string) (string, error) { // skipWorkdirDirectory reports whether a nested directory holds only mutable or cached data. // The .atmos directory is provisioner bookkeeping (access timestamps and metadata lock files -// that even a read-only lookup of the retained directory creates), never executed content. +// that even a read-only lookup of the retained directory creates), never executed content. The +// private plan directory of a plan policy exists only while Terraform runs. // Name-based exclusions apply only outside downloaded modules: a module's own subdirectories // are content Terraform may execute during apply, whatever they are called. func skipWorkdirDirectory(root, path, name string) bool { @@ -427,7 +426,7 @@ func skipWorkdirDirectory(root, path, name string) bool { if insideTerraformCacheOutsideModules(root, path) { return true } - return !insideTerraformModules(root, path) && (name == ".git" || name == ".terragrunt-cache" || name == ".atmos") + return !insideTerraformModules(root, path) && (name == ".git" || name == ".terragrunt-cache" || name == ".atmos" || strings.HasPrefix(name, ".atmos-policy-plan-")) } // Terraform's downloaded modules can contain scripts executed during apply. Other cache diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index 6237e5864f2..7ce6501b63a 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -367,6 +367,73 @@ func newPlanShowRunner(t *testing.T) process.Runner { return runner } +// TestPlanBindingIgnoresChangesAfterTerraformRan keeps the binding on the configuration Terraform +// read. An after hook runs once Terraform has written the plan, so a hook that rewrites the +// module must leave the binding on the original bytes and make the later apply fail. +func TestPlanBindingIgnoresChangesAfterTerraformRan(t *testing.T) { + config, info, execCtx, _, path := planFixture(t) + info.ComponentExecution = &schema.ComponentExecution{AfterHooks: []schema.ComponentHook{execHook("after", "plan")}} + shell := &fakeShell{run: func(call *shellCall) error { + if call.command != info.Command { + return os.WriteFile(filepath.Join(execCtx.workingDir, "main.tf"), []byte("rewritten by the hook"), 0o600) + } + return os.WriteFile(plannedOutputPath(execCtx.componentPath, call.args), []byte("reviewed plan"), 0o600) + }} + executor := testExecutor(t) + executor.services.ExecuteShellCommand = shell.execute + require.NoError(t, executor.runMainCommandPhase(config, info, execCtx, []string{"plan", "-out=" + path}, execCtx.componentPath, false)) + require.Len(t, shell.calls, 2, "Terraform and the after hook both ran") + require.FileExists(t, path+configurationPlanSuffix) + + info.SubCommand = "apply" + info.UseTerraformPlan = true + info.PlanFile = path + _, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) +} + +// TestPlanBindingRejectsConfigurationChangedWhileTerraformRan covers a change made during the +// Terraform process itself, which leaves no way to tell which bytes the plan came from. +func TestPlanBindingRejectsConfigurationChangedWhileTerraformRan(t *testing.T) { + for _, change := range []string{"module", "provider identity"} { + t.Run(change, func(t *testing.T) { + config, info, execCtx, runtime, path := planFixture(t) + shell := &fakeShell{run: func(call *shellCall) error { + switch change { + case "module": + if err := os.WriteFile(filepath.Join(execCtx.workingDir, "main.tf"), []byte("changed during plan"), 0o600); err != nil { + return err + } + case "provider identity": + runtime.identity = "changed during plan" + } + return os.WriteFile(plannedOutputPath(execCtx.componentPath, call.args), []byte("reviewed plan"), 0o600) + }} + executor := testExecutor(t) + executor.services.ExecuteShellCommand = shell.execute + err := executor.runMainCommandPhase(config, info, execCtx, []string{"plan", "-out=" + path}, execCtx.componentPath, false) + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + require.NoFileExists(t, path+configurationPlanSuffix) + }) + } +} + +// TestPlanBindingAllowsFilesTerraformWrites keeps the plan and state out of the comparison, +// so an ordinary run that writes them is not rejected. +func TestPlanBindingAllowsFilesTerraformWrites(t *testing.T) { + config, info, execCtx, _, path := planFixture(t) + shell := &fakeShell{run: func(call *shellCall) error { + if err := os.WriteFile(filepath.Join(execCtx.workingDir, "terraform.tfstate"), []byte("state"), 0o600); err != nil { + return err + } + return os.WriteFile(plannedOutputPath(execCtx.componentPath, call.args), []byte("new plan bytes"), 0o600) + }} + executor := testExecutor(t) + executor.services.ExecuteShellCommand = shell.execute + require.NoError(t, executor.runMainCommandPhase(config, info, execCtx, []string{"plan", "-out=" + path}, execCtx.componentPath, false)) + require.FileExists(t, path+configurationPlanSuffix) +} + func TestPolicyPlanBindsPublishedOutput(t *testing.T) { config, info, execCtx, _, _ := planFixture(t) var engine policy.Engine diff --git a/pkg/component/terraform/execute_hooks.go b/pkg/component/terraform/execute_hooks.go index 0c452c795cd..3547172ff9b 100644 --- a/pkg/component/terraform/execute_hooks.go +++ b/pkg/component/terraform/execute_hooks.go @@ -106,6 +106,9 @@ func (e *Executor) runMainShellCommand(atmosConfig *schema.AtmosConfiguration, i phaseOpts..., ) } + if snapshots, ok := ctx.Value(planSnapshotKey{}).(*planSnapshotter); ok { + run = snapshots.around(phaseArgs, run) + } err := run() if err == nil || !lifecycleOwnsRecovery(info) { return err diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index 0907fac71d6..d79bc9bd536 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -34,6 +34,8 @@ type componentExecContext struct { workingDir string tenv *dependencies.ToolchainEnvironment planPolicy *planfile.Guard + // planSnapshots observes the configuration around the main command for plan binding. + planSnapshots *planSnapshotter } // prepareComponentExecution consolidates all per-component setup into one call: @@ -282,6 +284,7 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every var execMetadataStdoutBuf, execMetadataStderrBuf bytes.Buffer var effectiveArgs []string captureCtx := context.WithValue(component.ContextFromOptions(opts...), mainCommandArgsKey{}, &effectiveArgs) + captureCtx = execCtx.observePlanSnapshots(captureCtx, atmosConfig, info, allArgsAndFlags) captureArgs := component.WithProcessContext(captureCtx) // A component with provider-supplied hooks recovers inside its main-command lifecycle, so // the retry runs between the same before and after hooks instead of repeating them. @@ -320,6 +323,103 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every return mainErr } +// planSnapshot is the configuration a saved plan is bound to: the provider identity and the +// digest of the retained working directory, excluding the plan and the state Terraform writes. +type planSnapshot struct { + path, identity, workingDigest string +} + +// planSnapshotKey carries the plan snapshotter to the main command's subprocess. +type planSnapshotKey struct{} + +// planSnapshotter observes the configuration around the Terraform subprocess, after the before +// hooks and before the after hooks. A hook that rewrites the module after Terraform read it then +// cannot move the binding onto the rewritten bytes. +type planSnapshotter struct { + ctx context.Context + config *schema.AtmosConfiguration + info *schema.ConfigAndStacksInfo + execCtx *componentExecContext + pathFrom func(phaseArgs []string) string + result *planSnapshot + err error +} + +// takePlanSnapshot fingerprints the configuration with the inputs the binding record uses. +func takePlanSnapshot(ctx context.Context, config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, execCtx *componentExecContext, path string) (*planSnapshot, error) { + runtime, err := planRuntime(config) + if err != nil { + return nil, err + } + identity, err := runtime.PlanIdentity(ctx, info, execCtx.preparedConfiguration.Sources) + if err != nil { + return nil, err + } + digest, err := configurationWorkdirDigest(execCtx.workingDir, path) + if err != nil { + return nil, err + } + return &planSnapshot{path: path, identity: identity, workingDigest: digest}, nil +} + +// around wraps one run of the Terraform subprocess. The configuration must be the same before and +// after it; the unchanged snapshot is kept for the binding, and a changed one rejects the plan. +func (s *planSnapshotter) around(phaseArgs []string, run func() error) func() error { + return func() error { + s.result, s.err = nil, nil + path := s.pathFrom(phaseArgs) + if path == "" { + return run() + } + before, err := takePlanSnapshot(s.ctx, s.config, s.info, s.execCtx, path) + if err != nil { + return err + } + runErr := run() + after, err := takePlanSnapshot(s.ctx, s.config, s.info, s.execCtx, path) + switch { + case err != nil: + s.err = err + case *after != *before: + s.err = fmt.Errorf("%w: configuration changed while Terraform ran", errUtils.ErrPlanVerificationFailed) + default: + s.result = after + } + return runErr + } +} + +// reviewedSnapshot returns the configuration Terraform read, or takes a fresh snapshot when no +// observed run recorded one for this plan. +func (c *componentExecContext) reviewedSnapshot(ctx context.Context, config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, path string) (*planSnapshot, error) { + if s := c.planSnapshots; s != nil { + if s.err != nil { + return nil, s.err + } + if s.result != nil && s.result.path == path { + return s.result, nil + } + } + return takePlanSnapshot(ctx, config, info, c, path) +} + +// observePlanSnapshots returns ctx carrying a snapshotter for a plan this invocation binds. Policy +// plans publish the artifact at the original output path, so that path is the one excluded from the +// digest. The returned context is unchanged when no plan is bound. +func (c *componentExecContext) observePlanSnapshots(ctx context.Context, config *schema.AtmosConfiguration, info *schema.ConfigAndStacksInfo, policyArgs []string) context.Context { + c.planSnapshots = nil + if !bindsPlan(info, c) { + return ctx + } + c.planSnapshots = &planSnapshotter{ctx: ctx, config: config, info: info, execCtx: c, pathFrom: func(phaseArgs []string) string { + if c.planPolicy != nil { + phaseArgs = policyArgs + } + return plannedOutputPath(c.componentPath, phaseArgs) + }} + return context.WithValue(ctx, planSnapshotKey{}, c.planSnapshots) +} + // isExplicitDetailedPlanChanges reports plan's "changes present" status from -detailed-exitcode. // A failed hook is never that status, even when the hook itself exited with 2. func isExplicitDetailedPlanChanges(info *schema.ConfigAndStacksInfo, args []string, err error) bool { From 70f57c9b2aaa6051662f02e0827e4e36a8ea751e Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Sun, 15 Nov 2026 22:45:47 -0600 Subject: [PATCH 44/52] fix(terraform): keep hook failures, init file flags and plan bytes honest - Apply the CI exit-code mapping only to Terraform's own status, so a failed hook that exits with 2 is no longer remapped to success. - Recognize double-dash -backend-config and -var-file forms when fingerprinting init files. - Fingerprint the plan file when the Terraform subprocess returns and reject the binding when an after hook rewrote it, except for policy plans, which the gate publishes itself. Co-Authored-By: Claude Sonnet 5.5 --- pkg/component/terraform/configuration_plan.go | 3 ++ .../configuration_plan_pipeline_test.go | 45 +++++++++++++++++++ .../terraform/configuration_plan_test.go | 26 +++++++++++ pkg/component/terraform/execute_command.go | 6 +++ pkg/component/terraform/execute_hooks.go | 4 +- .../terraform/execute_hooks_process_test.go | 32 +++++++++++++ pkg/component/terraform/execute_hooks_test.go | 21 +++++++++ pkg/component/terraform/execute_pipeline.go | 10 +++++ 8 files changed, 145 insertions(+), 2 deletions(-) diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go index b691f8b5bf9..e7fcabbbf59 100644 --- a/pkg/component/terraform/configuration_plan.go +++ b/pkg/component/terraform/configuration_plan.go @@ -198,6 +198,9 @@ func newConfigurationPlan(info *schema.ConfigAndStacksInfo, execCtx *componentEx if err != nil { return nil, err } + if snapshot.planDigest != "" && snapshot.planDigest != planDigest { + return nil, fmt.Errorf("%w: plan file changed after Terraform wrote it", errUtils.ErrPlanVerificationFailed) + } engineDigest, err := fileDigest(info.Command) if err != nil { return nil, err diff --git a/pkg/component/terraform/configuration_plan_pipeline_test.go b/pkg/component/terraform/configuration_plan_pipeline_test.go index 569972ed940..082b4c1b3e2 100644 --- a/pkg/component/terraform/configuration_plan_pipeline_test.go +++ b/pkg/component/terraform/configuration_plan_pipeline_test.go @@ -235,3 +235,48 @@ func TestPrepareInitExecutionKeepsReviewedPlanWorkspace(t *testing.T) { }) } } + +// TestPlanSnapshotterFingerprintsThePlanBeforeAfterHooksRun verifies that the plan Terraform wrote is +// fingerprinted when the subprocess returns, so an after hook that rewrites it cannot be bound. +func TestPlanSnapshotterFingerprintsThePlanBeforeAfterHooksRun(t *testing.T) { + for _, test := range []struct { + name string + afterHook func(t *testing.T, planPath string) + wantRecord bool + }{ + {name: "plan unchanged by after hooks", afterHook: func(*testing.T, string) {}, wantRecord: true}, + {name: "plan rewritten by an after hook", afterHook: func(t *testing.T, planPath string) { + require.NoError(t, os.WriteFile(planPath, []byte("rewritten by an after hook"), 0o600)) + }}, + } { + t.Run(test.name, func(t *testing.T) { + project := newReviewedPlanProject(t) + info := project.info(t, "plan") + project.provider.EXPECT().Prepare(gomock.Any(), info).Return(project.prepared(), nil) + execution, err := testExecutor(t).prepareComponentExecution(t.Context(), provisioner.OutputWriters{}, project.config, info, false) + require.NoError(t, err) + planPath := filepath.Join(execution.workingDir, "reviewed.planfile") + snapshotter := &planSnapshotter{ctx: t.Context(), config: project.config, info: info, execCtx: execution, pathFrom: func([]string) string { return planPath }} + execution.planSnapshots = snapshotter + + // Terraform writes the plan. + terraform := snapshotter.around(nil, func() error { return os.WriteFile(planPath, []byte("reviewed plan"), 0o600) }) + require.NoError(t, terraform()) + written, err := fileDigest(planPath) + require.NoError(t, err) + require.NotNil(t, snapshotter.result) + require.Equal(t, written, snapshotter.result.planDigest, "the digest is taken when the subprocess returns") + + test.afterHook(t, planPath) + err = recordConfigurationPlan(t.Context(), project.config, info, execution, []string{"-out=" + planPath}) + + if test.wantRecord { + require.NoError(t, err) + require.FileExists(t, planPath+configurationPlanSuffix) + return + } + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + require.NoFileExists(t, planPath+configurationPlanSuffix, "a rewritten plan is never bound") + }) + } +} diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index 7ce6501b63a..028a7f40374 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -566,3 +566,29 @@ func TestConfigurationPlanRoundTripNumericBackend(t *testing.T) { require.NoError(t, err) require.False(t, equalBackendConfiguration(map[string]any{"value": json.Number("9007199254740993")}, map[string]any{"value": json.Number("9007199254740992")})) } + +func TestNewConfigurationPlanRejectsAPlanRewrittenAfterTerraformWroteIt(t *testing.T) { + binary, err := os.Executable() + require.NoError(t, err) + dir := t.TempDir() + path := filepath.Join(dir, "reviewed.planfile") + require.NoError(t, os.WriteFile(path, []byte("reviewed plan"), 0o600)) + captured, err := fileDigest(path) + require.NoError(t, err) + info := &schema.ConfigAndStacksInfo{Command: binary, Stack: "dev", ComponentFromArg: "app"} + execCtx := &componentExecContext{workingDir: dir} + snapshot := &planSnapshot{path: path, identity: "identity", workingDigest: "working", planDigest: captured} + + plan, err := newConfigurationPlan(info, execCtx, snapshot, path) + require.NoError(t, err) + require.Equal(t, captured, plan.PlanDigest, "an unchanged plan keeps the existing binding flow") + + require.NoError(t, os.WriteFile(path, []byte("rewritten by an after hook"), 0o600)) + _, err = newConfigurationPlan(info, execCtx, snapshot, path) + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + + snapshot.planDigest = "" + plan, err = newConfigurationPlan(info, execCtx, snapshot, path) + require.NoError(t, err, "a snapshot without an observed digest binds the plan as it is") + require.NotEqual(t, captured, plan.PlanDigest) +} diff --git a/pkg/component/terraform/execute_command.go b/pkg/component/terraform/execute_command.go index c580c6df4e7..100dd105f11 100644 --- a/pkg/component/terraform/execute_command.go +++ b/pkg/component/terraform/execute_command.go @@ -202,6 +202,12 @@ func (e *Executor) finishMainTerraformCommand( } } + // A failed hook is never remapped: its exit status is not a plan result, so a hook that exits with 2 + // must not be read as "changes detected" and then mapped to success. + if errors.Is(err, errUtils.ErrComponentHookFailed) { + return err + } + // 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 { diff --git a/pkg/component/terraform/execute_hooks.go b/pkg/component/terraform/execute_hooks.go index 3547172ff9b..279fd4dbedb 100644 --- a/pkg/component/terraform/execute_hooks.go +++ b/pkg/component/terraform/execute_hooks.go @@ -527,12 +527,12 @@ func overlayEnvironment(entries []string, base func(string) (string, bool)) func } // initFileArguments returns the file paths named by -backend-config and -var-file options, in -// both the "-flag=value" and "-flag value" forms. A -backend-config value holding "=" sets one +// the "-flag=value" and "-flag value" forms, each with a single or double dash. A -backend-config value holding "=" sets one // setting instead of naming a file; it is already part of the arguments. func initFileArguments(arguments []string) []string { var paths []string for i := 0; i < len(arguments); i++ { - name, value, inline := strings.Cut(strings.TrimPrefix(arguments[i], "-"), "=") + name, value, inline := strings.Cut(strings.TrimLeft(arguments[i], "-"), "=") name = "-" + name if !slices.Contains(initFileFlags, name) { continue diff --git a/pkg/component/terraform/execute_hooks_process_test.go b/pkg/component/terraform/execute_hooks_process_test.go index d1c7ae1f83e..d8a95d31dc0 100644 --- a/pkg/component/terraform/execute_hooks_process_test.go +++ b/pkg/component/terraform/execute_hooks_process_test.go @@ -472,3 +472,35 @@ func TestSmartInitDistrustsUnresolvableProviderInitSettings(t *testing.T) { }} require.Nil(t, newAutoInitInputs(&schema.AtmosConfiguration{}, info, t.TempDir(), "")) } + +// TestFinishMainTerraformCommandKeepsHookFailuresUnderPermissiveCIMapping verifies that the CI exit-code mapping +// only remaps Terraform's own status: a hook that fails with status 2 is still a failure. +func TestFinishMainTerraformCommandKeepsHookFailuresUnderPermissiveCIMapping(t *testing.T) { + pipeline := newProcessPipeline(t, &schema.ComponentExecution{ + AfterHooks: []schema.ComponentHook{toolHook("plan-after", []string{"plan"}, "exit=2")}, + }) + hookErr := pipeline.run(t) + require.ErrorIs(t, hookErr, errUtils.ErrComponentHookFailed) + require.Equal(t, 2, resolveExitCode(hookErr), "the hook's own status is 2") + + permissive := func(_ *schema.AtmosConfiguration, code int) int { + if code == 0 || code == 2 { + return 0 + } + return code + } + executor := NewExecutor(Services{MapCIExitCode: permissive}) + config := &schema.AtmosConfiguration{} + + // The same status from Terraform itself is a plan result, so the mapping still applies. + require.NoError(t, executor.finishMainTerraformCommand(config, pipeline.info, errUtils.ExitCodeError{Code: 2}, false)) + + err := executor.finishMainTerraformCommand(config, pipeline.info, hookErr, false) + require.ErrorIs(t, err, errUtils.ErrComponentHookFailed) + require.Equal(t, 2, resolveExitCode(err)) + + // Neither does the neutralization of a -detailed-exitcode that exec-metadata added hide it. + pipeline.info.ExecMetadataDetailedExitCodeAdded = true + err = executor.finishMainTerraformCommand(config, pipeline.info, hookErr, false) + require.ErrorIs(t, err, errUtils.ErrComponentHookFailed) +} diff --git a/pkg/component/terraform/execute_hooks_test.go b/pkg/component/terraform/execute_hooks_test.go index 3e43dfbc18f..8a0a7473058 100644 --- a/pkg/component/terraform/execute_hooks_test.go +++ b/pkg/component/terraform/execute_hooks_test.go @@ -658,3 +658,24 @@ func TestInitRecoveryInsideLifecycleRunsOnlyWhenDiagnosed(t *testing.T) { require.Equal(t, 1, resolveExitCode(err)) require.Equal(t, []string{"terraform apply", "hook-after arg"}, shell.names()) } + +func TestInitFileArgumentsAcceptSingleAndDoubleDashForms(t *testing.T) { + for _, test := range []struct { + name string + arguments []string + want []string + }{ + {name: "single dash inline", arguments: []string{"-backend-config=backend.hcl"}, want: []string{"backend.hcl"}}, + {name: "double dash inline", arguments: []string{"--backend-config=backend.hcl"}, want: []string{"backend.hcl"}}, + {name: "single dash separate", arguments: []string{"-var-file", "vars.tfvars"}, want: []string{"vars.tfvars"}}, + {name: "double dash separate", arguments: []string{"--var-file", "vars.tfvars"}, want: []string{"vars.tfvars"}}, + {name: "both forms mixed", arguments: []string{"--backend-config=a.hcl", "-backend-config", "b.hcl", "--var-file=c.tfvars"}, want: []string{"a.hcl", "b.hcl", "c.tfvars"}}, + {name: "inline setting is not a file", arguments: []string{"--backend-config=bucket=state", "-backend-config", "key=value"}}, + {name: "unrelated flags", arguments: []string{"--reconfigure", "-input=false", "--upgrade"}}, + {name: "flag at the end without a value", arguments: []string{"--backend-config"}}, + } { + t.Run(test.name, func(t *testing.T) { + require.Equal(t, test.want, initFileArguments(test.arguments)) + }) + } +} diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index d79bc9bd536..3bf5fafc8c6 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -327,6 +327,9 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every // digest of the retained working directory, excluding the plan and the state Terraform writes. type planSnapshot struct { path, identity, workingDigest string + // planDigest is the digest of the plan file Terraform wrote, taken before any after hook runs. It is + // empty when no digest was observed, and it is not part of the before-and-after comparison. + planDigest string } // planSnapshotKey carries the plan snapshotter to the main command's subprocess. @@ -383,6 +386,13 @@ func (s *planSnapshotter) around(phaseArgs []string, run func() error) func() er case *after != *before: s.err = fmt.Errorf("%w: configuration changed while Terraform ran", errUtils.ErrPlanVerificationFailed) default: + // Terraform writes the plan itself unless a policy gate publishes it later, so the plan is + // fingerprinted now and an after hook cannot move the binding onto rewritten bytes. + if s.execCtx.planPolicy == nil { + if digest, digestErr := fileDigest(path); digestErr == nil { + after.planDigest = digest + } + } s.result = after } return runErr From 83b34186581daf8114598c3acba2fcf3a151f9e1 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 16 Nov 2026 07:42:12 -0600 Subject: [PATCH 45/52] fix(terraform): bind the engine that ran Terraform, not the one present after hooks Capture the Terraform executable digest in the plan snapshot taken around the subprocess and compare it before and after the run. The binding stores that digest and rejects an engine that changed before the binding was written. Co-Authored-By: Claude Sonnet 5.5 --- pkg/component/terraform/configuration_plan.go | 7 +++ .../configuration_plan_pipeline_test.go | 51 +++++++++++++++++++ pkg/component/terraform/execute_pipeline.go | 7 ++- 3 files changed, 64 insertions(+), 1 deletion(-) diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go index e7fcabbbf59..2c6cf882bf6 100644 --- a/pkg/component/terraform/configuration_plan.go +++ b/pkg/component/terraform/configuration_plan.go @@ -205,6 +205,13 @@ func newConfigurationPlan(info *schema.ConfigAndStacksInfo, execCtx *componentEx if err != nil { return nil, err } + // The engine that produced the plan is the one observed around the subprocess, not the one present now. + if snapshot.engineDigest != "" { + if snapshot.engineDigest != engineDigest { + return nil, fmt.Errorf("%w: engine changed after Terraform ran", errUtils.ErrPlanVerificationFailed) + } + engineDigest = snapshot.engineDigest + } return &configurationPlan{WorkingDigest: snapshot.workingDigest, Version: 1, Stack: info.Stack, Component: info.ComponentFromArg, Identity: snapshot.identity, EngineDigest: engineDigest, PlanDigest: planDigest, WorkingDirectory: filepath.Clean(execCtx.workingDir), Prepared: execCtx.preparedConfiguration}, nil } diff --git a/pkg/component/terraform/configuration_plan_pipeline_test.go b/pkg/component/terraform/configuration_plan_pipeline_test.go index 082b4c1b3e2..eaacf3a2fc9 100644 --- a/pkg/component/terraform/configuration_plan_pipeline_test.go +++ b/pkg/component/terraform/configuration_plan_pipeline_test.go @@ -280,3 +280,54 @@ func TestPlanSnapshotterFingerprintsThePlanBeforeAfterHooksRun(t *testing.T) { }) } } + +// TestPlanSnapshotterBindsTheEngineThatRanTerraform verifies that the engine digest is captured around +// the subprocess, so an engine replaced before the binding is written is rejected. +func TestPlanSnapshotterBindsTheEngineThatRanTerraform(t *testing.T) { + for _, test := range []struct { + name string + replace bool + }{ + {name: "engine unchanged binds the digest of the engine that ran"}, + {name: "engine replaced after Terraform finished is rejected", replace: true}, + } { + t.Run(test.name, func(t *testing.T) { + project := newReviewedPlanProject(t) + info := project.info(t, "plan") + original, err := os.ReadFile(info.Command) + require.NoError(t, err) + engine := filepath.Join(t.TempDir(), "engine") + require.NoError(t, os.WriteFile(engine, original, 0o700)) + info.Command = engine + ran, err := fileDigest(engine) + require.NoError(t, err) + + project.provider.EXPECT().Prepare(gomock.Any(), info).Return(project.prepared(), nil) + execution, err := testExecutor(t).prepareComponentExecution(t.Context(), provisioner.OutputWriters{}, project.config, info, false) + require.NoError(t, err) + planPath := filepath.Join(execution.workingDir, "reviewed.planfile") + snapshotter := &planSnapshotter{ctx: t.Context(), config: project.config, info: info, execCtx: execution, pathFrom: func([]string) string { return planPath }} + execution.planSnapshots = snapshotter + + terraform := snapshotter.around(nil, func() error { return os.WriteFile(planPath, []byte("reviewed plan"), 0o600) }) + require.NoError(t, terraform()) + require.NotNil(t, snapshotter.result) + require.Equal(t, ran, snapshotter.result.engineDigest, "the engine digest is taken around the subprocess") + + if test.replace { + require.NoError(t, os.WriteFile(engine, append(original, []byte("replaced by an after hook")...), 0o700)) + } + err = recordConfigurationPlan(t.Context(), project.config, info, execution, []string{"-out=" + planPath}) + + if test.replace { + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + require.NoFileExists(t, planPath+configurationPlanSuffix, "a replaced engine is never bound") + return + } + require.NoError(t, err) + data, err := os.ReadFile(planPath + configurationPlanSuffix) + require.NoError(t, err) + require.Contains(t, string(data), ran) + }) + } +} diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index 3bf5fafc8c6..2182b20cc4a 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -327,6 +327,10 @@ func (e *Executor) runMainCommandPhase( //nolint:revive // argument-limit: every // digest of the retained working directory, excluding the plan and the state Terraform writes. type planSnapshot struct { path, identity, workingDigest string + // engineDigest is the digest of the Terraform executable observed around the subprocess, so a hook + // that replaces the binary afterwards cannot move the binding onto the replacement. It is empty when + // the executable could not be read, and the binding then falls back to its own observation. + engineDigest string // planDigest is the digest of the plan file Terraform wrote, taken before any after hook runs. It is // empty when no digest was observed, and it is not part of the before-and-after comparison. planDigest string @@ -362,7 +366,8 @@ func takePlanSnapshot(ctx context.Context, config *schema.AtmosConfiguration, in if err != nil { return nil, err } - return &planSnapshot{path: path, identity: identity, workingDigest: digest}, nil + engineDigest, _ := fileDigest(info.Command) + return &planSnapshot{path: path, identity: identity, workingDigest: digest, engineDigest: engineDigest}, nil } // around wraps one run of the Terraform subprocess. The configuration must be the same before and From 84b919436569dcb1b194cbdbe2bb6b5f0fdb4705 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 16 Nov 2026 09:35:55 -0600 Subject: [PATCH 46/52] test(terraform): give the engine copy an executable extension on Windows The engine-binding test copied the test command to a file named engine with no extension. Windows runs only files that have one, so the executable lookup failed there. The copy now keeps the original command's extension. Co-Authored-By: Claude Sonnet 5.5 --- pkg/component/terraform/configuration_plan_pipeline_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/component/terraform/configuration_plan_pipeline_test.go b/pkg/component/terraform/configuration_plan_pipeline_test.go index eaacf3a2fc9..0c18a3e7dfb 100644 --- a/pkg/component/terraform/configuration_plan_pipeline_test.go +++ b/pkg/component/terraform/configuration_plan_pipeline_test.go @@ -296,7 +296,8 @@ func TestPlanSnapshotterBindsTheEngineThatRanTerraform(t *testing.T) { info := project.info(t, "plan") original, err := os.ReadFile(info.Command) require.NoError(t, err) - engine := filepath.Join(t.TempDir(), "engine") + // The copy keeps the original's extension, because Windows runs only files that have one. + engine := filepath.Join(t.TempDir(), "engine"+filepath.Ext(info.Command)) require.NoError(t, os.WriteFile(engine, original, 0o700)) info.Command = engine ran, err := fileDigest(engine) From a81adae6d0472a4c623f52e0987b53b37da0f2f6 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 16 Nov 2026 14:13:43 -0600 Subject: [PATCH 47/52] fix(terraform): honor explicit -detailed-exitcode values in plan status checks Recognize enabled explicit forms such as -detailed-exitcode=true, evaluate occurrences in order so the last value wins, and treat unparsable values as not enabled. The plan predicate and the hook predicate share one helper. Co-Authored-By: Claude Sonnet 5.5 --- pkg/component/terraform/execute_hooks.go | 2 +- pkg/component/terraform/execute_hooks_test.go | 37 +++++++++++++++++++ pkg/component/terraform/execute_pipeline.go | 23 +++++++++++- 3 files changed, 60 insertions(+), 2 deletions(-) diff --git a/pkg/component/terraform/execute_hooks.go b/pkg/component/terraform/execute_hooks.go index 279fd4dbedb..5e2a25c80f6 100644 --- a/pkg/component/terraform/execute_hooks.go +++ b/pkg/component/terraform/execute_hooks.go @@ -210,7 +210,7 @@ func runPhaseAction(info *schema.ConfigAndStacksInfo, args []string, opts []comp // isDetailedPlanChanges reports whether err is plan's "-detailed-exitcode" status for changes present. func isDetailedPlanChanges(phase *executionPhase, args []string, err error) bool { - return err != nil && phase.command == "plan" && slices.Contains(args, "-detailed-exitcode") && resolveExitCode(err) == detailedExitCodeChangesDetected + return err != nil && phase.command == "plan" && detailedExitCodeEnabled(args) && resolveExitCode(err) == detailedExitCodeChangesDetected } // hookOptions carries over only what a hook shares with the command: cancellation, the process diff --git a/pkg/component/terraform/execute_hooks_test.go b/pkg/component/terraform/execute_hooks_test.go index 8a0a7473058..fb235a624a6 100644 --- a/pkg/component/terraform/execute_hooks_test.go +++ b/pkg/component/terraform/execute_hooks_test.go @@ -679,3 +679,40 @@ func TestInitFileArgumentsAcceptSingleAndDoubleDashForms(t *testing.T) { }) } } + +// TestDetailedExitCodeEnabled covers the bare flag, explicit boolean forms, and last-wins ordering. +func TestDetailedExitCodeEnabled(t *testing.T) { + tests := []struct { + name string + args []string + want bool + }{ + {name: "absent", args: []string{"-out=plan"}, want: false}, + {name: "bare", args: []string{"-detailed-exitcode"}, want: true}, + {name: "double dash bare", args: []string{"--detailed-exitcode"}, want: true}, + {name: "explicit true", args: []string{"-detailed-exitcode=true"}, want: true}, + {name: "explicit 1", args: []string{"-detailed-exitcode=1"}, want: true}, + {name: "explicit false", args: []string{"-detailed-exitcode=false"}, want: false}, + {name: "explicit 0 double dash", args: []string{"--detailed-exitcode=0"}, want: false}, + {name: "unparsable", args: []string{"-detailed-exitcode=maybe"}, want: false}, + {name: "empty value", args: []string{"-detailed-exitcode="}, want: false}, + {name: "last wins disabled", args: []string{"-detailed-exitcode", "-detailed-exitcode=false"}, want: false}, + {name: "last wins enabled", args: []string{"-detailed-exitcode=false", "-detailed-exitcode=true"}, want: true}, + {name: "unparsable after enabled", args: []string{"-detailed-exitcode", "-detailed-exitcode=x"}, want: false}, + {name: "similar flag", args: []string{"-detailed-exitcode-extra=true"}, want: false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + require.Equal(t, tt.want, detailedExitCodeEnabled(tt.args)) + }) + } +} + +// TestIsExplicitDetailedPlanChangesHonorsExplicitValue checks the plan predicate end to end. +func TestIsExplicitDetailedPlanChangesHonorsExplicitValue(t *testing.T) { + info := &schema.ConfigAndStacksInfo{SubCommand: subcommandPlan} + err := errUtils.ExitCodeError{Code: detailedExitCodeChangesDetected} + require.True(t, isExplicitDetailedPlanChanges(info, []string{"-detailed-exitcode=true"}, err)) + require.False(t, isExplicitDetailedPlanChanges(info, []string{"-detailed-exitcode=false"}, err)) + require.False(t, isExplicitDetailedPlanChanges(info, []string{"-detailed-exitcode", "-detailed-exitcode=false"}, err)) +} diff --git a/pkg/component/terraform/execute_pipeline.go b/pkg/component/terraform/execute_pipeline.go index 2182b20cc4a..f378756072e 100644 --- a/pkg/component/terraform/execute_pipeline.go +++ b/pkg/component/terraform/execute_pipeline.go @@ -7,6 +7,7 @@ import ( "fmt" "path/filepath" "slices" + "strconv" "strings" errUtils "github.com/cloudposse/atmos/errors" @@ -441,7 +442,27 @@ func isExplicitDetailedPlanChanges(info *schema.ConfigAndStacksInfo, args []stri if errors.Is(err, errUtils.ErrComponentHookFailed) { return false } - return info.SubCommand == subcommandPlan && slices.Contains(args, detailedExitCodeFlag) && resolveExitCode(err) == detailedExitCodeChangesDetected + return info.SubCommand == subcommandPlan && detailedExitCodeEnabled(args) && resolveExitCode(err) == detailedExitCodeChangesDetected +} + +// detailedExitCodeEnabled reports whether args enable -detailed-exitcode. It accepts the bare flag +// and the explicit boolean forms (-detailed-exitcode=true, --detailed-exitcode=false), and the last +// occurrence wins, matching Go flag semantics. An unparsable value counts as not enabled. +func detailedExitCodeEnabled(args []string) bool { + enabled := false + for _, arg := range args { + name, value, hasValue := strings.Cut(arg, "=") + if name != detailedExitCodeFlag && name != "-"+detailedExitCodeFlag { + continue + } + if !hasValue { + enabled = true + continue + } + parsed, err := strconv.ParseBool(value) + enabled = err == nil && parsed + } + return enabled } // recoverFromInitRequired is smart init's plan/apply-time safety net: when the main command From f8eba994d496d0b39127e956ee1acac1e265292e Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 16 Nov 2026 14:15:14 -0600 Subject: [PATCH 48/52] fix(terraform): resolve workdir digest paths before excluding the plan file configurationWorkdirDigest now resolves the root and any non-empty plan path to absolute paths before walking, so a relative root no longer fails to exclude an absolute plan path. Resolution errors are returned; an empty plan path is unchanged. Co-Authored-By: Claude Sonnet 5.5 --- pkg/component/terraform/configuration_plan.go | 13 +++++++++- .../terraform/configuration_plan_test.go | 26 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/pkg/component/terraform/configuration_plan.go b/pkg/component/terraform/configuration_plan.go index 2c6cf882bf6..ea9fc439bb8 100644 --- a/pkg/component/terraform/configuration_plan.go +++ b/pkg/component/terraform/configuration_plan.go @@ -395,9 +395,20 @@ func excludedWorkdirFile(root, path, planPath, name string) bool { } // configurationWorkdirDigest binds retained module/configuration bytes, excluding mutable execution data. +// Root and a non-empty planPath are resolved to absolute paths first, so a relative root never +// fails to exclude the plan file; an empty planPath stays empty and excludes nothing. func configurationWorkdirDigest(root, planPath string) (string, error) { + root, err := filepath.Abs(root) + if err != nil { + return "", planIOError(err) + } + if planPath != "" { + if planPath, err = filepath.Abs(planPath); err != nil { + return "", planIOError(err) + } + } files := make(map[string]string) - err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error { + err = filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error { if err != nil { return err } diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index 028a7f40374..fb758b59327 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -592,3 +592,29 @@ func TestNewConfigurationPlanRejectsAPlanRewrittenAfterTerraformWroteIt(t *testi require.NoError(t, err, "a snapshot without an observed digest binds the plan as it is") require.NotEqual(t, captured, plan.PlanDigest) } + +func TestConfigurationWorkdirDigestResolvesRelativePaths(t *testing.T) { + base := t.TempDir() + root := filepath.Join(base, "work") + require.NoError(t, os.Mkdir(root, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(root, "main.tf"), []byte("source"), 0o600)) + t.Chdir(base) + absolute, err := configurationWorkdirDigest(root, filepath.Join(root, "reviewed.plan")) + require.NoError(t, err) + require.NoError(t, os.WriteFile(filepath.Join(root, "reviewed.plan"), []byte("plan"), 0o600)) + for name, plan := range map[string]string{ + "relative root absolute plan": filepath.Join(root, "reviewed.plan"), + "relative root relative plan": filepath.Join("work", "reviewed.plan"), + } { + t.Run(name, func(t *testing.T) { + digest, err := configurationWorkdirDigest("work", plan) + require.NoError(t, err) + require.Equal(t, absolute, digest, "the plan file must be excluded") + }) + } + t.Run("a different file is not excluded", func(t *testing.T) { + digest, err := configurationWorkdirDigest("work", filepath.Join("work", "other.plan")) + require.NoError(t, err) + require.NotEqual(t, absolute, digest) + }) +} From c129827216d80b70bc77db1dab85197185a9a366 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Mon, 16 Nov 2026 14:16:18 -0600 Subject: [PATCH 49/52] fix(terragrunt): decide prevent_destroy from syntax when the guard parse cannot run commands The destroy guard parses with command execution denied, so a unit whose locals call run_cmd failed the guard parse and was blocked even when unprotected, and a protected one reported an execution error instead of the protection verdict. When the guard parse fails, decide from the unit's own syntax without evaluating any function: a literal prevent_destroy decides, and a unit without one and without include blocks is unprotected. Anything the syntax cannot settle keeps the original error, so the guard still fails closed. Co-Authored-By: Claude Sonnet 5.5 --- .../internal/engine/generation_test.go | 42 +++++++++++++++++++ plugins/terragrunt/internal/engine/resolve.go | 38 ++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/plugins/terragrunt/internal/engine/generation_test.go b/plugins/terragrunt/internal/engine/generation_test.go index f3fcbdc2ce6..83284f155cb 100644 --- a/plugins/terragrunt/internal/engine/generation_test.go +++ b/plugins/terragrunt/internal/engine/generation_test.go @@ -122,3 +122,45 @@ func TestPreventDestroyGuardFailsClosedWhenConfigCannotBeParsed(t *testing.T) { require.Nil(t, result) require.NotContains(t, err.Error(), "protected by prevent_destroy", "the failure is a parse error, not a protection verdict") } + +func TestPreventDestroyGuardWithCommandLocals(t *testing.T) { + for _, test := range []struct { + name string + extra string + blocked bool + }{ + {"unprotected unit with run_cmd local", "locals { value = run_cmd(\"must-not-execute\") }\ninputs = { value = local.value }", false}, + {"protected unit with run_cmd local", "prevent_destroy = true\nlocals { value = run_cmd(\"must-not-execute\") }\ninputs = { value = local.value }", true}, + } { + t.Run(test.name, func(t *testing.T) { + request := nativeUnit(t, test.extra) + request.Command = "destroy" + _, err := Resolve(t.Context(), request) + if test.blocked { + require.ErrorIs(t, err, ErrPreventDestroy) + require.NotContains(t, err.Error(), "must-not-execute") + return + } + require.NotErrorIs(t, err, ErrPreventDestroy) + if err != nil { + require.NotContains(t, err.Error(), "read-only discovery cannot execute") + } + }) + } +} + +func TestPreventDestroyGuardStaysClosedWhenSyntaxCannotDecide(t *testing.T) { + for name, extra := range map[string]string{ + "expression depends on a command local": "locals { flag = run_cmd(\"must-not-execute\") }\nprevent_destroy = local.flag == \"x\"", + "include may set the flag": "locals { value = run_cmd(\"must-not-execute\") }\ninclude \"root\" { path = \"parent.hcl\" }", + } { + t.Run(name, func(t *testing.T) { + request := nativeUnit(t, extra) + request.Command = "destroy" + result, err := Resolve(t.Context(), request) + require.Error(t, err) + require.Nil(t, result) + require.NotErrorIs(t, err, ErrPreventDestroy) + }) + } +} diff --git a/plugins/terragrunt/internal/engine/resolve.go b/plugins/terragrunt/internal/engine/resolve.go index 43e74aa84b7..3ab64b4a139 100644 --- a/plugins/terragrunt/internal/engine/resolve.go +++ b/plugins/terragrunt/internal/engine/resolve.go @@ -16,6 +16,7 @@ import ( "github.com/gruntwork-io/terragrunt/pkg/log/format" "github.com/gruntwork-io/terragrunt/pkg/options" "github.com/hashicorp/hcl/v2" + "github.com/hashicorp/hcl/v2/hclsyntax" "github.com/hashicorp/hcl/v2/hclwrite" "github.com/spf13/afero" "github.com/zclconf/go-cty/cty" @@ -214,7 +215,7 @@ func checkDestroyGuard(ctx context.Context, logger log.Logger, environment *atmo guard, err := config.PartialParseConfigFile(ctx, logger, atmosbridge.Discovery(environment, false), pctx.WithDecodeList(config.TerragruntFlags), path, nil) if err != nil { - return err + return staticDestroyGuard(environment.FS, path, err) } if guard.PreventDestroy != nil && *guard.PreventDestroy { return ErrPreventDestroy @@ -222,6 +223,41 @@ func checkDestroyGuard(ctx context.Context, logger log.Logger, environment *atmo return nil } +// staticDestroyGuard decides prevent_destroy from the unit's own syntax when the evaluated guard +// parse failed, for example because a local calls run_cmd and guard parsing never executes +// commands. It evaluates no function and reads no other file. A literal prevent_destroy decides the +// verdict, and a unit that sets none without an include block is unprotected, so the destroy +// continues to the ordinary parse. Anything the syntax cannot settle (an expression, an include, an +// unreadable or invalid file) keeps the original error, so the guard still fails closed. +func staticDestroyGuard(fs afero.Fs, path string, parseErr error) error { + data, err := afero.ReadFile(fs, path) + if err != nil { + return parseErr + } + file, diagnostics := hclsyntax.ParseConfig(data, path, hcl.InitialPos) + body, ok := file.Body.(*hclsyntax.Body) + if diagnostics.HasErrors() || !ok { + return parseErr + } + for _, block := range body.Blocks { + if block.Type == "include" { + return parseErr + } + } + attribute, found := body.Attributes["prevent_destroy"] + if !found { + return nil + } + value, diagnostics := attribute.Expr.Value(nil) + if diagnostics.HasErrors() || value.IsNull() || !value.IsKnown() || value.Type() != cty.Bool { + return parseErr + } + if value.True() { + return ErrPreventDestroy + } + return nil +} + // unitExecution returns hooks and extra arguments as data for the host to run; // evaluation never executes them. Backend identity is read from a partial parse // that does not decode them. From a170c89204a5a693d3cc8474377d66d93cfe3b0c Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Tue, 17 Nov 2026 06:39:36 -0600 Subject: [PATCH 50/52] fix(terragrunt): enforce destroy protection for environment arguments --- plugins/terragrunt/go.mod | 2 +- .../engine/destroy_environment_test.go | 75 +++++++++++++++++++ .../terragrunt/internal/engine/policies.go | 61 ++++++++++++--- plugins/terragrunt/internal/engine/resolve.go | 23 ++++-- 4 files changed, 144 insertions(+), 17 deletions(-) create mode 100644 plugins/terragrunt/internal/engine/destroy_environment_test.go diff --git a/plugins/terragrunt/go.mod b/plugins/terragrunt/go.mod index 5ad34e800fb..e6c2d8092fb 100644 --- a/plugins/terragrunt/go.mod +++ b/plugins/terragrunt/go.mod @@ -10,6 +10,7 @@ require ( github.com/gruntwork-io/terragrunt/pkg/atmosbridge v0.0.0-00010101000000-000000000000 github.com/hashicorp/go-version v1.9.0 github.com/hashicorp/hcl/v2 v2.25.0 + github.com/mattn/go-shellwords v1.0.16 github.com/spf13/afero v1.15.0 github.com/stretchr/testify v1.12.1 github.com/zclconf/go-cty v1.19.0 @@ -155,7 +156,6 @@ require ( github.com/mattn/go-colorable v0.1.15 // indirect github.com/mattn/go-isatty v0.0.24 // indirect github.com/mattn/go-runewidth v0.0.31 // indirect - github.com/mattn/go-shellwords v1.0.16 // indirect github.com/mattn/go-zglob v0.0.8 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/mitchellh/go-testing-interface v1.14.1 // indirect diff --git a/plugins/terragrunt/internal/engine/destroy_environment_test.go b/plugins/terragrunt/internal/engine/destroy_environment_test.go new file mode 100644 index 00000000000..cab40908fce --- /dev/null +++ b/plugins/terragrunt/internal/engine/destroy_environment_test.go @@ -0,0 +1,75 @@ +package engine + +import ( + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestPreventDestroyEnvironment(t *testing.T) { + for _, test := range []struct { + name string + env map[string]string + args []string + extra string + blocked bool + }{ + {name: "command environment", env: map[string]string{"TF_CLI_ARGS_apply": "-destroy"}, blocked: true}, + {name: "global environment", env: map[string]string{"TF_CLI_ARGS": "-destroy"}, blocked: true}, + {name: "quoted and whitespace", env: map[string]string{"TF_CLI_ARGS_apply": "-lock=false\t\"-destroy=true\"\n"}, blocked: true}, + {name: "explicit false wins", env: map[string]string{"TF_CLI_ARGS_apply": "-destroy"}, args: []string{"-destroy=false"}}, + {name: "global inserted after command", env: map[string]string{"TF_CLI_ARGS_apply": "-destroy=false", "TF_CLI_ARGS": "-destroy"}, blocked: true}, + {name: "global false wins", env: map[string]string{"TF_CLI_ARGS_apply": "-destroy", "TF_CLI_ARGS": "-destroy=false"}}, + {name: "other command ignored", env: map[string]string{"TF_CLI_ARGS_plan": "-destroy"}}, + {name: "extra environment", extra: `extra_arguments "env" { + commands = ["apply"] + env_vars = { TF_CLI_ARGS_apply = "-destroy" } + }`, blocked: true}, + {name: "extra clears inherited", env: map[string]string{"TF_CLI_ARGS_apply": "-destroy"}, extra: `extra_arguments "env" { + commands = ["apply"] + env_vars = { TF_CLI_ARGS_apply = "" } + }`}, + {name: "extra arguments win", env: map[string]string{"TF_CLI_ARGS_apply": "-destroy"}, extra: `extra_arguments "args" { + commands = ["apply"] + arguments = ["-destroy=false"] + }`}, + {name: "last extra environment wins", extra: `extra_arguments "first" { + commands = ["apply"] + env_vars = { TF_CLI_ARGS_apply = "-destroy=false" } + } + extra_arguments "last" { + commands = ["apply"] + env_vars = { TF_CLI_ARGS_apply = "-destroy" } + }`, blocked: true}, + } { + t.Run(test.name, func(t *testing.T) { + request := executionUnit(t, test.extra) + data, err := os.ReadFile(request.Path) + require.NoError(t, err) + data = append(data, []byte("\nprevent_destroy = true\n")...) + if test.blocked { + data = append(data, []byte("inputs = { value = run_cmd(\"must-not-execute\") }\n")...) + } + require.NoError(t, os.WriteFile(request.Path, data, 0o600)) + request.Command, request.Arguments = "apply", test.args + if test.env != nil { + request.Environment = test.env + } + _, err = Resolve(t.Context(), request) + if test.blocked { + require.ErrorIs(t, err, ErrPreventDestroy) + } else { + require.NoError(t, err) + } + }) + } +} + +func TestPreventDestroyMalformedEnvironmentFailsClosed(t *testing.T) { + request := nativeUnit(t, "prevent_destroy = true") + request.Command = "apply" + request.Environment["TF_CLI_ARGS_apply"] = "\"-destroy" + _, err := Resolve(t.Context(), request) + require.ErrorContains(t, err, "invalid TF_CLI_ARGS_apply") +} diff --git a/plugins/terragrunt/internal/engine/policies.go b/plugins/terragrunt/internal/engine/policies.go index 7039890f8de..b672cffde62 100644 --- a/plugins/terragrunt/internal/engine/policies.go +++ b/plugins/terragrunt/internal/engine/policies.go @@ -5,6 +5,7 @@ import ( "encoding/json" "errors" "fmt" + "maps" "os/exec" "path/filepath" "slices" @@ -13,6 +14,7 @@ import ( "github.com/gruntwork-io/terragrunt/pkg/atmosbridge" "github.com/gruntwork-io/terragrunt/pkg/config" "github.com/hashicorp/go-version" + "github.com/mattn/go-shellwords" "github.com/cloudposse/atmos/plugins/terragrunt/internal/model" ) @@ -80,12 +82,12 @@ func checkVersions(ctx context.Context, request model.Request, terraform, terrag // checkUnitPolicies rejects Terragrunt semantics that the native adapter cannot // represent faithfully. Backend reads skip the version probe, which may execute // the selected binary. -func checkUnitPolicies(ctx context.Context, request model.Request, cfg *config.TerragruntConfig) error { +func checkUnitPolicies(ctx context.Context, request *model.Request, cfg *config.TerragruntConfig) error { if cfg.Terraform == nil || cfg.Terraform.Source == nil { return fmt.Errorf("native adapter requires terraform.source") } if request.Phase != "backend" { - if err := checkVersions(ctx, request, cfg.TerraformVersionConstraint, cfg.TerragruntVersionConstraint); err != nil { + if err := checkVersions(ctx, *request, cfg.TerraformVersionConstraint, cfg.TerragruntVersionConstraint); err != nil { return err } } @@ -93,12 +95,27 @@ func checkUnitPolicies(ctx context.Context, request model.Request, cfg *config.T return fmt.Errorf("native adaptation of terraform_binary is not implemented") } // Hooks and extra arguments are adapted as execution data by unitExecution. - if cfg.PreventDestroy != nil && *cfg.PreventDestroy && atmosbridge.IsDestroyOperation(request.Command, effectivePolicyArguments(request, cfg)) { - return ErrPreventDestroy + if err := checkPreventDestroy(request, cfg); err != nil { + return err } return checkUnsupportedSettings(cfg) } +// checkPreventDestroy evaluates the same effective arguments as the engine. +func checkPreventDestroy(request *model.Request, cfg *config.TerragruntConfig) error { + if request.Phase == "backend" || cfg.PreventDestroy == nil || !*cfg.PreventDestroy { + return nil + } + args, err := effectivePolicyArguments(request, cfg) + if err != nil { + return err + } + if atmosbridge.IsDestroyOperation(request.Command, args) { + return ErrPreventDestroy + } + return nil +} + // checkUnsupportedSettings rejects error handling, exclusion, feature flag and // IAM settings, which have no native equivalent. func checkUnsupportedSettings(cfg *config.TerragruntConfig) error { @@ -120,13 +137,37 @@ func validateRemoteSource(source string) error { return fmt.Errorf("native adapter does not support this module source transport") } -// effectivePolicyArguments follows host ordering: extra arguments precede user arguments. -func effectivePolicyArguments(request model.Request, cfg *config.TerragruntConfig) []string { +// effectivePolicyArguments matches engine insertion order: command environment, +// global environment, extra arguments, then explicit arguments. Matching extra +// argument blocks override environment values in declaration order. +func effectivePolicyArguments(request *model.Request, cfg *config.TerragruntConfig) ([]string, error) { + environment := map[string]string{} + maps.Copy(environment, request.Environment) var args []string - for _, extra := range cfg.Terraform.ExtraArgs { - if slices.Contains(extra.Commands, request.Command) && extra.Arguments != nil { - args = append(args, (*extra.Arguments)...) + if cfg.Terraform != nil { + for _, extra := range cfg.Terraform.ExtraArgs { + if !slices.Contains(extra.Commands, request.Command) { + continue + } + if extra.Arguments != nil { + args = append(args, (*extra.Arguments)...) + } + if extra.EnvVars != nil { + maps.Copy(environment, *extra.EnvVars) + } + } + } + var injected []string + suffix := strings.NewReplacer("-", "_", " ", "_").Replace(request.Command) + for _, name := range []string{"TF_CLI_ARGS_" + suffix, "TF_CLI_ARGS"} { + parser := shellwords.NewParser() + parser.ParseEnv, parser.ParseBacktick = false, false + tokens, err := parser.Parse(environment[name]) + if err != nil { + return nil, fmt.Errorf("invalid %s arguments: %w", name, err) } + injected = append(injected, tokens...) } - return append(args, request.Arguments...) + args = append(injected, args...) + return append(args, request.Arguments...), nil } diff --git a/plugins/terragrunt/internal/engine/resolve.go b/plugins/terragrunt/internal/engine/resolve.go index 3ab64b4a139..0db9cdbee32 100644 --- a/plugins/terragrunt/internal/engine/resolve.go +++ b/plugins/terragrunt/internal/engine/resolve.go @@ -74,7 +74,7 @@ func resolveUnit(ctx context.Context, logger log.Logger, environment *atmosbridg if err != nil { return nil, err } - if err := checkUnitPolicies(ctx, request, cfg); err != nil { + if err := checkUnitPolicies(ctx, &request, cfg); err != nil { return nil, err } return buildUnitResolution(ctx, logger, environment, pctx, request, cfg, path) @@ -209,17 +209,28 @@ func parsingContext(path, tofu, command string, arguments []string) *config.Pars // checkDestroyGuard rejects destroy before a full parse can evaluate HCL functions // for a unit that sets prevent_destroy. Backend reads never destroy. func checkDestroyGuard(ctx context.Context, logger log.Logger, environment *atmosbridge.Venv, pctx *config.ParsingContext, request model.Request, path string) error { - if request.Phase == "backend" || !atmosbridge.IsDestroyOperation(request.Command, request.Arguments) { + if request.Phase == "backend" || (request.Command != "plan" && request.Command != "apply" && request.Command != "destroy") { return nil } guard, err := config.PartialParseConfigFile(ctx, logger, atmosbridge.Discovery(environment, false), - pctx.WithDecodeList(config.TerragruntFlags), path, nil) + pctx.WithDecodeList(config.TerragruntFlags, config.TerraformExtraArgs), path, nil) if err != nil { - return staticDestroyGuard(environment.FS, path, err) + return failedDestroyGuard(&request, environment.FS, path, err) } - if guard.PreventDestroy != nil && *guard.PreventDestroy { - return ErrPreventDestroy + return checkPreventDestroy(&request, guard) +} + +// failedDestroyGuard preserves syntax-only decisions for known destroy requests. +func failedDestroyGuard(request *model.Request, fs afero.Fs, path string, parseErr error) error { + args, err := effectivePolicyArguments(request, &config.TerragruntConfig{}) + if err != nil { + return err + } + if atmosbridge.IsDestroyOperation(request.Command, args) { + return staticDestroyGuard(fs, path, parseErr) } + // A full parse may evaluate locals unavailable to the read-only guard. + // Its policy check still inspects every expanded argument and environment. return nil } From 833fff13358a9c9bdb205bf4085114888ac5da33 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Tue, 17 Nov 2026 06:52:52 -0600 Subject: [PATCH 51/52] refactor(terragrunt): simplify provider discovery and preparation assembly --- .../internal/engine/resolve_test.go | 2 +- .../terragrunt/internal/provider/discovery.go | 65 ++++++++++++------- .../internal/provider/fixtures_test.go | 12 ++-- .../internal/provider/preparation.go | 28 +++++--- .../internal/provider/provider_test.go | 2 +- 5 files changed, 69 insertions(+), 40 deletions(-) diff --git a/plugins/terragrunt/internal/engine/resolve_test.go b/plugins/terragrunt/internal/engine/resolve_test.go index 3e93a610e2b..ea101eb5707 100644 --- a/plugins/terragrunt/internal/engine/resolve_test.go +++ b/plugins/terragrunt/internal/engine/resolve_test.go @@ -41,7 +41,7 @@ inputs = { large = 9007199254740993, nested = { nullable = null, enabled = false `) result, err := Resolve(t.Context(), request) require.NoError(t, err) - require.Equal(t, filepath.Join(result.WorkingDir, "relative/state.tfstate"), result.Backend["path"]) + require.Equal(t, filepath.Join(result.WorkingDir, "relative", "state.tfstate"), result.Backend["path"]) require.Equal(t, "# {{ keep }} ${literal}\n", result.Generated["literal.tf"]) require.Equal(t, json.Number("9007199254740993"), result.Inputs["large"]) require.NoFileExists(t, filepath.Join(filepath.Dir(request.Path), "literal.tf")) diff --git a/plugins/terragrunt/internal/provider/discovery.go b/plugins/terragrunt/internal/provider/discovery.go index a288bf2b0aa..9ed5b3c1938 100644 --- a/plugins/terragrunt/internal/provider/discovery.go +++ b/plugins/terragrunt/internal/provider/discovery.go @@ -30,12 +30,14 @@ func (p *Provider) Discover(ctx context.Context, request *pluginv1.DiscoverCompo return nil, rpcError(err) } addresses := map[string]*pluginv1.ComponentAddress{} - for _, unit := range result.Inventory { + for i := range result.Inventory { + unit := &result.Inventory[i] addresses[unit.Config.Path] = &pluginv1.ComponentAddress{Stack: unit.Stack, Component: unit.Name, ComponentType: "terraform"} } response := &pluginv1.DiscoverComponentsResponse{} - for _, unit := range result.Inventory { - discovered, err := discoveredUnit(req, unit, addresses) + for i := range result.Inventory { + unit := &result.Inventory[i] + discovered, err := discoveredUnit(&req, unit, addresses) if err != nil { return nil, rpcError(err) } @@ -44,25 +46,14 @@ func (p *Provider) Discover(ctx context.Context, request *pluginv1.DiscoverCompo return response, nil } -func discoveredUnit(req model.Request, unit model.DiscoveredUnit, addresses map[string]*pluginv1.ComponentAddress) (*pluginv1.DiscoveredComponent, error) { +func discoveredUnit(req *model.Request, unit *model.DiscoveredUnit, addresses map[string]*pluginv1.ComponentAddress) (*pluginv1.DiscoveredComponent, error) { source, err := relative(req.BasePath, unit.Config.Path) if err != nil { return nil, err } - module := unit.Config.Source - if len(unit.Config.Folders) > 0 && (module == "" || filepath.IsAbs(module)) { - path := module - if path == "" { - path = unit.Config.Folders[0] - } - local, err := relative(req.BasePath, path) - if err != nil { - return nil, err - } - module = "./" + local - } - if module == "" { - return nil, ErrUnsupported + module, err := discoveredModule(req.BasePath, &unit.Config) + if err != nil { + return nil, err } generation := "" if unit.GenerationRoot != "" { @@ -93,18 +84,48 @@ func discoveredUnit(req model.Request, unit model.DiscoveredUnit, addresses map[ return nil, err } instance := &pluginv1.DiscoveredComponent{Address: addresses[unit.Config.Path], Provider: "terragrunt", Engine: engineName(req.Tofu), Source: source, Configuration: config, Sources: watched} - for _, path := range unit.Config.Dependencies { + instance.Dependencies, err = discoveredDependencies(&unit.Config, addresses) + if err != nil { + return nil, err + } + return instance, nil +} + +// discoveredModule resolves local module identities relative to the project. +func discoveredModule(root string, cfg *model.Resolution) (string, error) { + module := cfg.Source + if len(cfg.Folders) > 0 && (module == "" || filepath.IsAbs(module)) { + path := module + if path == "" { + path = cfg.Folders[0] + } + local, err := relative(root, path) + if err != nil { + return "", err + } + module = "./" + local + } + if module == "" { + return "", ErrUnsupported + } + return module, nil +} + +// discoveredDependencies binds evaluated dependency paths to discovered addresses. +func discoveredDependencies(cfg *model.Resolution, addresses map[string]*pluginv1.ComponentAddress) ([]*pluginv1.ComponentDependency, error) { + var dependencies []*pluginv1.ComponentDependency + for _, path := range cfg.Dependencies { target := addresses[path] if target == nil { return nil, ErrUnsupported } kind := pluginv1.ComponentDependency_ORDER - if slices.Contains(unit.Config.OutputDependencies, path) { + if slices.Contains(cfg.OutputDependencies, path) { kind = pluginv1.ComponentDependency_OUTPUT } - instance.Dependencies = append(instance.Dependencies, &pluginv1.ComponentDependency{Target: target, Kind: kind}) + dependencies = append(dependencies, &pluginv1.ComponentDependency{Target: target, Kind: kind}) } - return instance, nil + return dependencies, nil } // importPaths adds explicit files or directory roots to the same scoped inventory. diff --git a/plugins/terragrunt/internal/provider/fixtures_test.go b/plugins/terragrunt/internal/provider/fixtures_test.go index a9d8708a92c..c3cec2d24d2 100644 --- a/plugins/terragrunt/internal/provider/fixtures_test.go +++ b/plugins/terragrunt/internal/provider/fixtures_test.go @@ -16,7 +16,7 @@ import ( func fixture(t *testing.T, name string) string { t.Helper() - source, err := filepath.Abs(filepath.Join("../../fixtures", name)) + source, err := filepath.Abs(filepath.Join("..", "..", "fixtures", name)) require.NoError(t, err) root := t.TempDir() require.NoError(t, os.CopyFS(root, os.DirFS(source))) @@ -43,7 +43,7 @@ func TestDiscoveryFixtures(t *testing.T) { request := &pluginv1.DiscoverComponentsRequest{RootDirectory: root, Provider: "terragrunt", ProviderConfiguration: opts, IncludePaths: []string{test.scope}, Operation: &pluginv1.ComponentOperation{Mode: "native", Command: "list"}, Invocation: &pluginv1.Invocation{Environment: map[string]string{"ATMOS_TERRAFORM_BINARY": "must-not-execute"}}} response, err := p.Discover(t.Context(), request) require.NoError(t, err) - expected := map[string]map[string]pluginv1.ComponentDependency_Kind{} + var expected map[string]map[string]pluginv1.ComponentDependency_Kind if test.name == "advanced" { expected = map[string]map[string]pluginv1.ComponentDependency_Kind{ "app": {"network": pluginv1.ComponentDependency_ORDER, "database": pluginv1.ComponentDependency_OUTPUT, "cache": pluginv1.ComponentDependency_OUTPUT}, @@ -98,17 +98,17 @@ func TestExplicitStackStateAndPreparation(t *testing.T) { request := &pluginv1.ComponentRequest{RootDirectory: root, Provider: "terragrunt", ProviderConfiguration: opts, Address: network.Address, Source: network.Source, Configuration: network.Configuration, Operation: &pluginv1.ComponentOperation{Mode: "native", Command: "plan"}, Invocation: &pluginv1.Invocation{Environment: map[string]string{"ATMOS_TERRAFORM_BINARY": "must-not-execute"}}} state, err := provider.Resolve(t.Context(), request) require.NoError(t, err) - require.NoDirExists(t, filepath.Join(root, "live/.terragrunt-stack")) + require.NoDirExists(t, filepath.Join(root, "live", ".terragrunt-stack")) request.Effects = &pluginv1.ComponentEffects{Commands: true, Network: true, Secrets: true, Writes: true, StateReads: true} prepared, err := provider.Prepare(t.Context(), request) require.NoError(t, err) - require.DirExists(t, filepath.Join(root, "live/.terragrunt-stack")) + require.DirExists(t, filepath.Join(root, "live", ".terragrunt-stack")) require.Equal(t, state.State.WorkingDirectory, prepared.State.WorkingDirectory) var before, after map[string]any require.NoError(t, decode(state.State.Backend, &before)) require.NoError(t, decode(prepared.State.Backend, &after)) require.Equal(t, before, after) - require.Equal(t, filepath.Join(root, "state/dev/network.tfstate"), after["path"]) + require.Equal(t, filepath.Join(root, "state", "dev", "network.tfstate"), after["path"]) require.True(t, prepared.BackendGenerated) require.Len(t, prepared.Files, 3) require.Equal(t, "backend.tf", prepared.Files[0].Path) @@ -164,7 +164,7 @@ func TestRemoteModuleSourceIsNotAFileWatch(t *testing.T) { remote := "git::https://example.com/modules.git//app?ref=v1.0.0" unit := model.DiscoveredUnit{Config: model.Resolution{Path: path, Source: remote, Files: []string{path}}} addresses := map[string]*pluginv1.ComponentAddress{path: {Stack: "live", Component: "app", ComponentType: "terraform"}} - instance, err := discoveredUnit(model.Request{BasePath: root}, unit, addresses) + instance, err := discoveredUnit(&model.Request{BasePath: root}, &unit, addresses) require.NoError(t, err) var configuration map[string]any require.NoError(t, decode(instance.Configuration, &configuration)) diff --git a/plugins/terragrunt/internal/provider/preparation.go b/plugins/terragrunt/internal/provider/preparation.go index a889ab3f3ed..9fde8107554 100644 --- a/plugins/terragrunt/internal/provider/preparation.go +++ b/plugins/terragrunt/internal/provider/preparation.go @@ -59,7 +59,7 @@ func (p *Provider) Prepare(ctx context.Context, request *pluginv1.ComponentReque } // All provider processes generating the same stack must retain the lock // until the subsequent unit parse has finished. - unlock, err := p.generateLocked(ctx, req) + unlock, err := p.generateLocked(ctx, &req) if err != nil { return nil, rpcError(err) } @@ -68,7 +68,7 @@ func (p *Provider) Prepare(ctx context.Context, request *pluginv1.ComponentReque if err != nil { return nil, rpcError(err) } - response, err := preparedResponse(req, result) + response, err := preparedResponse(&req, result) if err != nil { return nil, rpcError(err) } @@ -81,7 +81,7 @@ func preparationEffects(effects *pluginv1.ComponentEffects) bool { // generateLocked generates the owning stack under a cross-process lock and // returns the function that releases it. Units outside a stack need no lock. -func (p *Provider) generateLocked(ctx context.Context, req model.Request) (func(), error) { +func (p *Provider) generateLocked(ctx context.Context, req *model.Request) (func(), error) { if req.GenerationRoot == "" { return func() {}, nil } @@ -89,7 +89,7 @@ func (p *Provider) generateLocked(ctx context.Context, req model.Request) (func( if err != nil { return nil, err } - generation := req + generation := *req generation.Path, generation.Phase, generation.Command = req.GenerationRoot, "discover", "generate" if _, err := p.evaluate(ctx, generation); err != nil { _ = lock.Unlock() @@ -100,7 +100,7 @@ func (p *Provider) generateLocked(ctx context.Context, req model.Request) (func( // generationLock keys the lock by the physical generation root, so processes // that spell the project root differently still serialize. -func generationLock(ctx context.Context, req model.Request) (*flock.Flock, error) { +func generationLock(ctx context.Context, req *model.Request) (*flock.Flock, error) { root, err := filepath.EvalSymlinks(req.BasePath) if err != nil { return nil, err @@ -125,7 +125,7 @@ func generationLock(ctx context.Context, req model.Request) (*flock.Flock, error return lock, nil } -func preparedResponse(req model.Request, result *model.Resolution) (*pluginv1.PrepareComponentResponse, error) { +func preparedResponse(req *model.Request, result *model.Resolution) (*pluginv1.PrepareComponentResponse, error) { values := result.Inputs if values == nil { values = map[string]any{} @@ -143,6 +143,14 @@ func preparedResponse(req model.Request, result *model.Resolution) (*pluginv1.Pr return nil, err } response := &pluginv1.PrepareComponentResponse{Engine: engineName(req.Tofu), ModuleSource: result.Source, WorkingDirectory: result.WorkingDir, Inputs: inputs, State: state, Sources: watched, BackendGenerated: result.BackendGenerated, Execution: executionResponse(result.Execution)} + response.Files = preparedFiles(result) + + return response, nil +} + +// preparedFiles merges generated files over the overlay and preserves stable ordering. +func preparedFiles(result *model.Resolution) []*pluginv1.ComponentGeneratedFile { + var filesOut []*pluginv1.ComponentGeneratedFile files := maps.Clone(result.Overlay) if files == nil { files = map[string][]byte{} @@ -160,12 +168,12 @@ func preparedResponse(req model.Request, result *model.Resolution) (*pluginv1.Pr if _, generated := result.Generated[name]; generated { mode = 0 } - response.Files = append(response.Files, &pluginv1.ComponentGeneratedFile{Path: name, Content: files[name], IfExists: policy, Mode: mode}) + filesOut = append(filesOut, &pluginv1.ComponentGeneratedFile{Path: name, Content: files[name], IfExists: policy, Mode: mode}) } - return response, nil + return filesOut } -func stateIdentity(req model.Request, result *model.Resolution) (*pluginv1.ComponentStateIdentity, error) { +func stateIdentity(req *model.Request, result *model.Resolution) (*pluginv1.ComponentStateIdentity, error) { backend := result.Backend if backend == nil { backend = map[string]any{} @@ -196,7 +204,7 @@ func (p *Provider) Resolve(ctx context.Context, request *pluginv1.ComponentReque if err != nil { return nil, rpcError(err) } - state, err := stateIdentity(req, result) + state, err := stateIdentity(&req, result) if err != nil { return nil, rpcError(err) } diff --git a/plugins/terragrunt/internal/provider/provider_test.go b/plugins/terragrunt/internal/provider/provider_test.go index 122dd4e8f9e..91e6ed82105 100644 --- a/plugins/terragrunt/internal/provider/provider_test.go +++ b/plugins/terragrunt/internal/provider/provider_test.go @@ -40,7 +40,7 @@ func TestPreparationAndStateUseSeparatePhases(t *testing.T) { phases = append(phases, req.Phase) if req.Phase == "discover" { require.Equal(t, "generate", req.Command) - require.Equal(t, filepath.Join(request.RootDirectory, "live/terragrunt.stack.hcl"), req.Path) + require.Equal(t, filepath.Join(request.RootDirectory, "live", "terragrunt.stack.hcl"), req.Path) } return &model.Resolution{Source: filepath.Join(request.RootDirectory, "module"), WorkingDir: filepath.Join(request.RootDirectory, "cache"), Inputs: map[string]any{"payload": "fresh"}, BackendType: "local", Backend: map[string]any{"path": "relative.tfstate"}, Generated: map[string]string{"z.tf": "{{ literal }}", "a.tf": "${expression}"}, Files: []string{filepath.Join(request.RootDirectory, "root.hcl")}}, nil }) From 0e80d241206079a9f679a508470cf017d7ef92b9 Mon Sep 17 00:00:00 2001 From: Erik Osterman Date: Tue, 17 Nov 2026 11:31:50 -0600 Subject: [PATCH 52/52] fix(terraform): finish plugin output init policy and shell mocks Reuse initialized plugin output readers when init.mode is never, including nil output options, without changing ordinary component initialization. Generate the hook shell mock and retain every existing assertion while consolidating reviewed-plan tests to preserve the branch file budget. --- .../configuration_plan_failures_test.go | 181 ------------------ .../configuration_plan_pipeline_test.go | 168 ++++++++++++++++ .../terraform/configuration_plan_test.go | 44 ++--- .../terraform/execute_hooks_process_test.go | 4 +- pkg/component/terraform/execute_hooks_test.go | 130 +++++++------ pkg/component/terraform/mock_shell_test.go | 61 ++++++ pkg/terraform/output/configuration_state.go | 8 +- .../output/configuration_state_test.go | 114 ++++++----- pkg/terraform/output/executor.go | 9 +- 9 files changed, 405 insertions(+), 314 deletions(-) delete mode 100644 pkg/component/terraform/configuration_plan_failures_test.go create mode 100644 pkg/component/terraform/mock_shell_test.go diff --git a/pkg/component/terraform/configuration_plan_failures_test.go b/pkg/component/terraform/configuration_plan_failures_test.go deleted file mode 100644 index 1de907b496e..00000000000 --- a/pkg/component/terraform/configuration_plan_failures_test.go +++ /dev/null @@ -1,181 +0,0 @@ -package terraform - -import ( - "errors" - "os" - "path/filepath" - "runtime" - "testing" - - "github.com/stretchr/testify/require" - "go.uber.org/mock/gomock" - - errUtils "github.com/cloudposse/atmos/errors" - "github.com/cloudposse/atmos/pkg/schema" -) - -// identityOnlyRuntime returns a generated runtime that can bind plans but cannot resolve backend state. -func identityOnlyRuntime(t *testing.T) *MockPlanIdentityRuntime { - t.Helper() - runtime := NewMockPlanIdentityRuntime(gomock.NewController(t)) - runtime.EXPECT().PlanIdentity(gomock.Any(), gomock.Any(), gomock.Any()).Return("provider-source-options", nil).AnyTimes() - return runtime -} - -// stateFailureRuntime returns a generated runtime that accepts the plan identity but fails to resolve the backend. -func stateFailureRuntime(t *testing.T, fixture *planRuntimeFixture, stateErr error) *MockReviewedPlanRuntime { - t.Helper() - runtime := NewMockReviewedPlanRuntime(gomock.NewController(t)) - runtime.EXPECT().PlanIdentity(gomock.Any(), gomock.Any(), gomock.Any()).Return(fixture.identity, fixture.err).AnyTimes() - runtime.EXPECT().ResolveState(gomock.Any(), gomock.Any()).Return(nil, stateErr).AnyTimes() - return runtime -} - -// recordedPlan records a plan companion and prepares the invocation for a direct apply. -func recordedPlan(t *testing.T) (*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, *componentExecContext, *planRuntimeFixture, string) { - t.Helper() - config, info, execCtx, runtime, path := planFixture(t) - require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"plan", "-out=" + path})) - info.SubCommand = "apply" - info.UseTerraformPlan = true - info.PlanFile = path - return config, info, execCtx, runtime, path -} - -func TestRestoreConfigurationPlanRequiresPlanBindingRuntime(t *testing.T) { - config, info, _, _, _ := recordedPlan(t) - config.ComponentConfigurations = nil - prepared, err := restoreConfigurationPlan(t.Context(), config, info) - require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) - require.Nil(t, prepared) -} - -func TestRestoreConfigurationPlanRequiresStateRuntime(t *testing.T) { - config, info, _, _, _ := recordedPlan(t) - config.ComponentConfigurations = identityOnlyRuntime(t) - prepared, err := restoreConfigurationPlan(t.Context(), config, info) - require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension, "a provider that cannot prove the backend is unchanged must not apply a saved plan") - require.Nil(t, prepared) -} - -func TestRestoreConfigurationPlanBackendResolution(t *testing.T) { - failure := errors.New("backend unreachable") - t.Run("resolution error is propagated", func(t *testing.T) { - config, info, _, runtime, _ := recordedPlan(t) - config.ComponentConfigurations = stateFailureRuntime(t, runtime, failure) - _, err := restoreConfigurationPlan(t.Context(), config, info) - require.ErrorIs(t, err, failure) - require.NotErrorIs(t, err, errUtils.ErrPlanVerificationFailed, "an unreachable backend is not a verification mismatch") - }) - t.Run("missing state is a verification failure", func(t *testing.T) { - config, info, _, runtime, _ := recordedPlan(t) - runtime.state = nil - _, err := restoreConfigurationPlan(t.Context(), config, info) - require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) - }) - t.Run("backend type change is a verification failure", func(t *testing.T) { - config, info, _, runtime, _ := recordedPlan(t) - runtime.state.BackendType = "s3" - _, err := restoreConfigurationPlan(t.Context(), config, info) - require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) - }) -} - -func TestRestoreConfigurationPlanReportsUnreadableArtifacts(t *testing.T) { - t.Run("missing engine binary", func(t *testing.T) { - config, info, _, _, _ := recordedPlan(t) - require.NoError(t, os.Remove(info.Command)) - prepared, err := restoreConfigurationPlan(t.Context(), config, info) - require.ErrorIs(t, err, os.ErrNotExist) - require.Nil(t, prepared) - }) - t.Run("symlink added to the reviewed workdir", func(t *testing.T) { - config, info, execCtx, _, _ := recordedPlan(t) - if err := os.Symlink(filepath.Join(execCtx.workingDir, "main.tf"), filepath.Join(execCtx.workingDir, "alias.tf")); err != nil { - t.Skipf("symlinks unavailable: %v", err) - } - prepared, err := restoreConfigurationPlan(t.Context(), config, info) - require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) - require.Nil(t, prepared) - }) - t.Run("unchanged inputs are accepted", func(t *testing.T) { - config, info, execCtx, _, path := recordedPlan(t) - prepared, err := restoreConfigurationPlan(t.Context(), config, info) - require.NoError(t, err) - require.Equal(t, execCtx.preparedConfiguration.Backend, prepared.Backend) - require.Equal(t, path, info.PlanFile) - }) -} - -func TestRecordConfigurationPlanFailsWhenWorkdirCannotBeDigested(t *testing.T) { - config, info, execCtx, _, path := planFixture(t) - execCtx.workingDir = filepath.Join(t.TempDir(), "absent") - err := recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"plan", "-out=" + path}) - require.ErrorIs(t, err, os.ErrNotExist) - require.NoFileExists(t, path+configurationPlanSuffix, "no binding may be written for an unverifiable workdir") -} - -func TestRecordConfigurationPlanReplacesBindingAtomically(t *testing.T) { - config, info, execCtx, runtime, path := planFixture(t) - args := []string{"plan", "-out=" + path} - require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, args)) - first, err := os.ReadFile(path + configurationPlanSuffix) - require.NoError(t, err) - runtime.identity = "changed provider" - require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, args)) - second, err := os.ReadFile(path + configurationPlanSuffix) - require.NoError(t, err) - require.NotEqual(t, first, second, "re-planning must replace the earlier binding") - require.Contains(t, string(second), "changed provider") - entries, err := os.ReadDir(filepath.Dir(path)) - require.NoError(t, err) - for _, entry := range entries { - require.NotContains(t, entry.Name(), ".atmos-plan-", "temporary binding files are cleaned up") - } -} - -func TestConfigurationWorkdirDigestSkipsOnlyNestedMutableDirectories(t *testing.T) { - root := t.TempDir() - plan := filepath.Join(root, "reviewed.plan") - write := func(name string) { - t.Helper() - path := filepath.Join(root, filepath.FromSlash(name)) - require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700)) - require.NoError(t, os.WriteFile(path, []byte(name), 0o600)) - } - write("main.tf") - baseline, err := configurationWorkdirDigest(root, plan) - require.NoError(t, err) - - for _, ignored := range []string{".git/HEAD", ".terragrunt-cache/x/main.tf", ".terraform/modules.json", "sub/.terraform/x", ".atmos/metadata.json", ".atmos/metadata.json.lock", "sub/.atmos/source-files.json", ".atmos-plan-temp", "terraform.tfstate.backup", "other.planfile.atmos.json"} { - write(ignored) - } - same, err := configurationWorkdirDigest(root, plan) - require.NoError(t, err) - require.Equal(t, baseline, same, "mutable data does not alter the retained-bytes digest") - - for _, retained := range []string{"modules/child/main.tf", "terragrunt.hcl", ".terraform.lock.hcl", "settings.atmos.json", ".terraform/modules/vpc/.atmos/hook.sh", ".terraform/modules/vpc/.terragrunt-cache/main.tf", ".terraform/modules/vpc/.git/hooks/post-checkout"} { - write(retained) - changed, err := configurationWorkdirDigest(root, plan) - require.NoError(t, err) - require.NotEqual(t, baseline, changed, retained) - baseline = changed - } -} - -func TestConfigurationWorkdirDigestFailsForUnreadableFile(t *testing.T) { - if runtime.GOOS == "windows" { - t.Skip("file permissions are not enforced on Windows") - } - root := t.TempDir() - locked := filepath.Join(root, "locked.tf") - require.NoError(t, os.WriteFile(locked, []byte("secret"), 0o600)) - require.NoError(t, os.Chmod(locked, 0)) - t.Cleanup(func() { _ = os.Chmod(locked, 0o600) }) - if file, err := os.Open(locked); err == nil { - _ = file.Close() - t.Skip("permissions are not enforced for this user") - } - _, err := configurationWorkdirDigest(root, filepath.Join(root, "reviewed.plan")) - require.ErrorIs(t, err, os.ErrPermission) -} diff --git a/pkg/component/terraform/configuration_plan_pipeline_test.go b/pkg/component/terraform/configuration_plan_pipeline_test.go index 0c18a3e7dfb..5fcd3deb08e 100644 --- a/pkg/component/terraform/configuration_plan_pipeline_test.go +++ b/pkg/component/terraform/configuration_plan_pipeline_test.go @@ -1,8 +1,10 @@ package terraform import ( + "errors" "os" "path/filepath" + "runtime" "strings" "testing" @@ -332,3 +334,169 @@ func TestPlanSnapshotterBindsTheEngineThatRanTerraform(t *testing.T) { }) } } + +// identityOnlyRuntime returns a generated runtime that can bind plans but cannot resolve backend state. +func identityOnlyRuntime(t *testing.T) *MockPlanIdentityRuntime { + t.Helper() + runtime := NewMockPlanIdentityRuntime(gomock.NewController(t)) + runtime.EXPECT().PlanIdentity(gomock.Any(), gomock.Any(), gomock.Any()).Return("provider-source-options", nil).AnyTimes() + return runtime +} + +// stateFailureRuntime returns a generated runtime that accepts the plan identity but fails to resolve the backend. +func stateFailureRuntime(t *testing.T, fixture *planRuntimeFixture, stateErr error) *MockReviewedPlanRuntime { + t.Helper() + runtime := NewMockReviewedPlanRuntime(gomock.NewController(t)) + runtime.EXPECT().PlanIdentity(gomock.Any(), gomock.Any(), gomock.Any()).Return(fixture.identity, fixture.err).AnyTimes() + runtime.EXPECT().ResolveState(gomock.Any(), gomock.Any()).Return(nil, stateErr).AnyTimes() + return runtime +} + +// recordedPlan records a plan companion and prepares the invocation for a direct apply. +func recordedPlan(t *testing.T) (*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, *componentExecContext, *planRuntimeFixture, string) { + t.Helper() + config, info, execCtx, runtime, path := planFixture(t) + require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"plan", "-out=" + path})) + info.SubCommand = "apply" + info.UseTerraformPlan = true + info.PlanFile = path + return config, info, execCtx, runtime, path +} + +func TestRestoreConfigurationPlanRequiresPlanBindingRuntime(t *testing.T) { + config, info, _, _, _ := recordedPlan(t) + config.ComponentConfigurations = nil + prepared, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + require.Nil(t, prepared) +} + +func TestRestoreConfigurationPlanRequiresStateRuntime(t *testing.T) { + config, info, _, _, _ := recordedPlan(t) + config.ComponentConfigurations = identityOnlyRuntime(t) + prepared, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension, "a provider that cannot prove the backend is unchanged must not apply a saved plan") + require.Nil(t, prepared) +} + +func TestRestoreConfigurationPlanBackendResolution(t *testing.T) { + failure := errors.New("backend unreachable") + t.Run("resolution error is propagated", func(t *testing.T) { + config, info, _, runtime, _ := recordedPlan(t) + config.ComponentConfigurations = stateFailureRuntime(t, runtime, failure) + _, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, failure) + require.NotErrorIs(t, err, errUtils.ErrPlanVerificationFailed, "an unreachable backend is not a verification mismatch") + }) + t.Run("missing state is a verification failure", func(t *testing.T) { + config, info, _, runtime, _ := recordedPlan(t) + runtime.state = nil + _, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + }) + t.Run("backend type change is a verification failure", func(t *testing.T) { + config, info, _, runtime, _ := recordedPlan(t) + runtime.state.BackendType = "s3" + _, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) + }) +} + +func TestRestoreConfigurationPlanReportsUnreadableArtifacts(t *testing.T) { + t.Run("missing engine binary", func(t *testing.T) { + config, info, _, _, _ := recordedPlan(t) + require.NoError(t, os.Remove(info.Command)) + prepared, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, os.ErrNotExist) + require.Nil(t, prepared) + }) + t.Run("symlink added to the reviewed workdir", func(t *testing.T) { + config, info, execCtx, _, _ := recordedPlan(t) + if err := os.Symlink(filepath.Join(execCtx.workingDir, "main.tf"), filepath.Join(execCtx.workingDir, "alias.tf")); err != nil { + t.Skipf("symlinks unavailable: %v", err) + } + prepared, err := restoreConfigurationPlan(t.Context(), config, info) + require.ErrorIs(t, err, errUtils.ErrPluginUnsupportedExtension) + require.Nil(t, prepared) + }) + t.Run("unchanged inputs are accepted", func(t *testing.T) { + config, info, execCtx, _, path := recordedPlan(t) + prepared, err := restoreConfigurationPlan(t.Context(), config, info) + require.NoError(t, err) + require.Equal(t, execCtx.preparedConfiguration.Backend, prepared.Backend) + require.Equal(t, path, info.PlanFile) + }) +} + +func TestRecordConfigurationPlanFailsWhenWorkdirCannotBeDigested(t *testing.T) { + config, info, execCtx, _, path := planFixture(t) + execCtx.workingDir = filepath.Join(t.TempDir(), "absent") + err := recordConfigurationPlan(t.Context(), config, info, execCtx, []string{"plan", "-out=" + path}) + require.ErrorIs(t, err, os.ErrNotExist) + require.NoFileExists(t, path+configurationPlanSuffix, "no binding may be written for an unverifiable workdir") +} + +func TestRecordConfigurationPlanReplacesBindingAtomically(t *testing.T) { + config, info, execCtx, runtime, path := planFixture(t) + args := []string{"plan", "-out=" + path} + require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, args)) + first, err := os.ReadFile(path + configurationPlanSuffix) + require.NoError(t, err) + runtime.identity = "changed provider" + require.NoError(t, recordConfigurationPlan(t.Context(), config, info, execCtx, args)) + second, err := os.ReadFile(path + configurationPlanSuffix) + require.NoError(t, err) + require.NotEqual(t, first, second, "re-planning must replace the earlier binding") + require.Contains(t, string(second), "changed provider") + entries, err := os.ReadDir(filepath.Dir(path)) + require.NoError(t, err) + for _, entry := range entries { + require.NotContains(t, entry.Name(), ".atmos-plan-", "temporary binding files are cleaned up") + } +} + +func TestConfigurationWorkdirDigestSkipsOnlyNestedMutableDirectories(t *testing.T) { + root := t.TempDir() + plan := filepath.Join(root, "reviewed.plan") + write := func(name string) { + t.Helper() + path := filepath.Join(root, filepath.FromSlash(name)) + require.NoError(t, os.MkdirAll(filepath.Dir(path), 0o700)) + require.NoError(t, os.WriteFile(path, []byte(name), 0o600)) + } + write("main.tf") + baseline, err := configurationWorkdirDigest(root, plan) + require.NoError(t, err) + + for _, ignored := range []string{".git/HEAD", ".terragrunt-cache/x/main.tf", ".terraform/modules.json", "sub/.terraform/x", ".atmos/metadata.json", ".atmos/metadata.json.lock", "sub/.atmos/source-files.json", ".atmos-plan-temp", "terraform.tfstate.backup", "other.planfile.atmos.json"} { + write(ignored) + } + same, err := configurationWorkdirDigest(root, plan) + require.NoError(t, err) + require.Equal(t, baseline, same, "mutable data does not alter the retained-bytes digest") + + for _, retained := range []string{"modules/child/main.tf", "terragrunt.hcl", ".terraform.lock.hcl", "settings.atmos.json", ".terraform/modules/vpc/.atmos/hook.sh", ".terraform/modules/vpc/.terragrunt-cache/main.tf", ".terraform/modules/vpc/.git/hooks/post-checkout"} { + write(retained) + changed, err := configurationWorkdirDigest(root, plan) + require.NoError(t, err) + require.NotEqual(t, baseline, changed, retained) + baseline = changed + } +} + +func TestConfigurationWorkdirDigestFailsForUnreadableFile(t *testing.T) { + if runtime.GOOS == "windows" { + t.Skip("file permissions are not enforced on Windows") + } + root := t.TempDir() + locked := filepath.Join(root, "locked.tf") + require.NoError(t, os.WriteFile(locked, []byte("secret"), 0o600)) + require.NoError(t, os.Chmod(locked, 0)) + t.Cleanup(func() { _ = os.Chmod(locked, 0o600) }) + if file, err := os.Open(locked); err == nil { + _ = file.Close() + t.Skip("permissions are not enforced for this user") + } + _, err := configurationWorkdirDigest(root, filepath.Join(root, "reviewed.plan")) + require.ErrorIs(t, err, os.ErrPermission) +} diff --git a/pkg/component/terraform/configuration_plan_test.go b/pkg/component/terraform/configuration_plan_test.go index fb758b59327..3417c5cef8d 100644 --- a/pkg/component/terraform/configuration_plan_test.go +++ b/pkg/component/terraform/configuration_plan_test.go @@ -261,9 +261,9 @@ func TestPlanOutputFlagsUseEffectiveArguments(t *testing.T) { // A permissive CI exit mapping must not turn a preflight rejection into success. config.CI.Enabled = true config.Components.Terraform.CI.ExitCodes = map[int]bool{0: true, 1: true, 2: true} - shell := &fakeShell{} + shell := newShellFixture(t, nil) executor := testExecutor(t) - executor.services.ExecuteShellCommand = shell.execute + executor.services.ExecuteShellCommand = shell.mock.Execute info.ComponentExecution = &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, Arguments: test.provider}}, BeforeHooks: []schema.ComponentHook{execHook("before", "plan")}} if test.output != "" { require.NoError(t, os.WriteFile(filepath.Join(execCtx.componentPath, test.output), []byte("plan"), 0o600)) @@ -288,15 +288,15 @@ func TestRecoveredPlanBindsEffectiveOutput(t *testing.T) { require.NoError(t, os.WriteFile(plan, []byte("reviewed"), 0o600)) info.ComponentExecution = &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, Arguments: []string{"--out=" + plan}}}} attempts := 0 - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { attempts++ if attempts == 1 { return errors.New("Backend configuration changed") } return nil - }} + }) executor := testExecutor(t) - executor.services.ExecuteShellCommand = shell.execute + executor.services.ExecuteShellCommand = shell.mock.Execute executor.executeTerraformInitForcedFn = func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, string, string, bool, bool, ...ShellCommandOption) error { return nil } @@ -327,7 +327,7 @@ func TestDetailedExitCodePlanRecordsReviewedPlan(t *testing.T) { if test.extra { info.ComponentExecution = &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, Arguments: []string{"-detailed-exitcode"}}}} } - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if err := os.WriteFile(plannedOutputPath(execCtx.componentPath, call.args), []byte("changes"), 0o600); err != nil { return err } @@ -336,9 +336,9 @@ func TestDetailedExitCodePlanRecordsReviewedPlan(t *testing.T) { return fmt.Errorf("%w: after %q: %w", errUtils.ErrComponentHookFailed, "notify", errUtils.ExitCodeError{Code: test.code}) } return errUtils.ExitCodeError{Code: test.code} - }} + }) executor := testExecutor(t) - executor.services.ExecuteShellCommand = shell.execute + executor.services.ExecuteShellCommand = shell.mock.Execute err := executor.runMainCommandPhase(config, info, execCtx, test.args, execCtx.componentPath, false) require.Equal(t, test.code, resolveExitCode(err), "the plan status is returned unchanged") if test.hook { @@ -373,14 +373,14 @@ func newPlanShowRunner(t *testing.T) process.Runner { func TestPlanBindingIgnoresChangesAfterTerraformRan(t *testing.T) { config, info, execCtx, _, path := planFixture(t) info.ComponentExecution = &schema.ComponentExecution{AfterHooks: []schema.ComponentHook{execHook("after", "plan")}} - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if call.command != info.Command { return os.WriteFile(filepath.Join(execCtx.workingDir, "main.tf"), []byte("rewritten by the hook"), 0o600) } return os.WriteFile(plannedOutputPath(execCtx.componentPath, call.args), []byte("reviewed plan"), 0o600) - }} + }) executor := testExecutor(t) - executor.services.ExecuteShellCommand = shell.execute + executor.services.ExecuteShellCommand = shell.mock.Execute require.NoError(t, executor.runMainCommandPhase(config, info, execCtx, []string{"plan", "-out=" + path}, execCtx.componentPath, false)) require.Len(t, shell.calls, 2, "Terraform and the after hook both ran") require.FileExists(t, path+configurationPlanSuffix) @@ -398,7 +398,7 @@ func TestPlanBindingRejectsConfigurationChangedWhileTerraformRan(t *testing.T) { for _, change := range []string{"module", "provider identity"} { t.Run(change, func(t *testing.T) { config, info, execCtx, runtime, path := planFixture(t) - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { switch change { case "module": if err := os.WriteFile(filepath.Join(execCtx.workingDir, "main.tf"), []byte("changed during plan"), 0o600); err != nil { @@ -408,9 +408,9 @@ func TestPlanBindingRejectsConfigurationChangedWhileTerraformRan(t *testing.T) { runtime.identity = "changed during plan" } return os.WriteFile(plannedOutputPath(execCtx.componentPath, call.args), []byte("reviewed plan"), 0o600) - }} + }) executor := testExecutor(t) - executor.services.ExecuteShellCommand = shell.execute + executor.services.ExecuteShellCommand = shell.mock.Execute err := executor.runMainCommandPhase(config, info, execCtx, []string{"plan", "-out=" + path}, execCtx.componentPath, false) require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) require.NoFileExists(t, path+configurationPlanSuffix) @@ -422,14 +422,14 @@ func TestPlanBindingRejectsConfigurationChangedWhileTerraformRan(t *testing.T) { // so an ordinary run that writes them is not rejected. func TestPlanBindingAllowsFilesTerraformWrites(t *testing.T) { config, info, execCtx, _, path := planFixture(t) - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if err := os.WriteFile(filepath.Join(execCtx.workingDir, "terraform.tfstate"), []byte("state"), 0o600); err != nil { return err } return os.WriteFile(plannedOutputPath(execCtx.componentPath, call.args), []byte("new plan bytes"), 0o600) - }} + }) executor := testExecutor(t) - executor.services.ExecuteShellCommand = shell.execute + executor.services.ExecuteShellCommand = shell.mock.Execute require.NoError(t, executor.runMainCommandPhase(config, info, execCtx, []string{"plan", "-out=" + path}, execCtx.componentPath, false)) require.FileExists(t, path+configurationPlanSuffix) } @@ -439,12 +439,12 @@ func TestPolicyPlanBindsPublishedOutput(t *testing.T) { var engine policy.Engine require.NoError(t, engine.Register(policy.CELEngine, &policy.CEL{})) execCtx.planPolicy = &planfile.Guard{Engine: &engine, Show: newPlanShowRunner(t), Rules: []policy.Rule{{ID: "allow", On: policy.TerraformPlan, Engine: policy.CELEngine, Expression: "false", Effects: policy.Effects{Deny: true}}}} - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { path := plannedOutputPath(execCtx.componentPath, call.args) return os.WriteFile(path, []byte("reviewed"), 0o600) - }} + }) executor := testExecutor(t) - executor.services.ExecuteShellCommand = shell.execute + executor.services.ExecuteShellCommand = shell.mock.Execute path := filepath.Join(execCtx.componentPath, "approved.plan") require.NoError(t, executor.runMainCommandPhase(config, info, execCtx, []string{"plan", "-out=" + path}, execCtx.componentPath, false)) require.FileExists(t, path+configurationPlanSuffix) @@ -490,9 +490,9 @@ func TestPlanOutputFlagsInEnvironmentAreRejected(t *testing.T) { if test.provider != nil { info.ComponentExecution = &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: test.commands, Environment: test.provider}}, BeforeHooks: []schema.ComponentHook{execHook("before", "plan")}} } - shell := &fakeShell{} + shell := newShellFixture(t, nil) executor := testExecutor(t) - executor.services.ExecuteShellCommand = shell.execute + executor.services.ExecuteShellCommand = shell.mock.Execute err := executor.runMainCommandPhase(config, info, execCtx, []string{"plan", "-out", filepath.Base(path)}, execCtx.componentPath, false) if test.reject { require.ErrorIs(t, err, errUtils.ErrPlanVerificationFailed) diff --git a/pkg/component/terraform/execute_hooks_process_test.go b/pkg/component/terraform/execute_hooks_process_test.go index d8a95d31dc0..4b86f85ad75 100644 --- a/pkg/component/terraform/execute_hooks_process_test.go +++ b/pkg/component/terraform/execute_hooks_process_test.go @@ -334,7 +334,7 @@ func TestErrorHookCaptureUsesTemporaryFiles(t *testing.T) { t.Setenv(name, temp) } capturedDuringCommand := 0 - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { switch call.command { case "terraform": _, _ = call.options.StderrCapture.Write([]byte("Error: Throttled")) @@ -347,7 +347,7 @@ func TestErrorHookCaptureUsesTemporaryFiles(t *testing.T) { return errUtils.ExitCodeError{Code: 2} } return nil - }} + }) after := execHook("after", "apply") after.RunOnError = true info := hookInfo("apply", &schema.ComponentExecution{ diff --git a/pkg/component/terraform/execute_hooks_test.go b/pkg/component/terraform/execute_hooks_test.go index fb235a624a6..2c12232aadf 100644 --- a/pkg/component/terraform/execute_hooks_test.go +++ b/pkg/component/terraform/execute_hooks_test.go @@ -34,31 +34,45 @@ type shellCall struct { type hookContextKey struct{} -// fakeShell records every process the executor starts and lets a test script the outcome. -type fakeShell struct { +//go:generate go run go.uber.org/mock/mockgen@v0.6.0 -source=$GOFILE -destination=mock_shell_test.go -package=$GOPACKAGE -mock_names=shellCommandExecutor=MockShellCommandExecutor + +// shellCommandExecutor is the test contract for Services.ExecuteShellCommand. +type shellCommandExecutor interface { + Execute(schema.AtmosConfiguration, string, []string, string, []string, bool, string, ...component.ExecutionOption) error +} + +var _ shellCommandExecutor = (*MockShellCommandExecutor)(nil) + +// shellFixture captures calls made through the generated process mock. +type shellFixture struct { + mock *MockShellCommandExecutor mu sync.Mutex calls []shellCall - // run decides the result and may write to the option writers like a real process would. - run func(call *shellCall) error } -//nolint:gocritic // The signature is fixed by Services.ExecuteShellCommand. -func (f *fakeShell) execute(_ schema.AtmosConfiguration, command string, args []string, dir string, env []string, dryRun bool, _ string, opts ...component.ExecutionOption) error { - call := shellCall{command: command, args: append([]string(nil), args...), dir: dir, env: append([]string(nil), env...), dryRun: dryRun} - for _, opt := range opts { - opt(&call.options) - } - f.mu.Lock() - f.calls = append(f.calls, call) - f.mu.Unlock() - if f.run == nil { - return nil - } - return f.run(&call) +func newShellFixture(t *testing.T, run func(*shellCall) error) *shellFixture { + t.Helper() + fixture := &shellFixture{mock: NewMockShellCommandExecutor(gomock.NewController(t))} + fixture.mock.EXPECT().Execute(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn( + func(_ schema.AtmosConfiguration, command string, args []string, dir string, env []string, dryRun bool, _ string, opts ...component.ExecutionOption) error { + call := shellCall{command: command, args: append([]string(nil), args...), dir: dir, env: append([]string(nil), env...), dryRun: dryRun} + for _, opt := range opts { + opt(&call.options) + } + fixture.mu.Lock() + fixture.calls = append(fixture.calls, call) + fixture.mu.Unlock() + if run == nil { + return nil + } + return run(&call) + }, + ).AnyTimes() + return fixture } // names lists " " per call, which is enough to read the timeline. -func (f *fakeShell) names() []string { +func (f *shellFixture) names() []string { var names []string for i := range f.calls { call := &f.calls[i] @@ -71,8 +85,8 @@ func (f *fakeShell) names() []string { return names } -func hookExecutor(shell *fakeShell) *Executor { - return NewExecutor(Services{OperationServices: nil, ExecuteShellCommand: shell.execute}) +func hookExecutor(shell *shellFixture) *Executor { + return NewExecutor(Services{OperationServices: nil, ExecuteShellCommand: shell.mock.Execute}) } func hookInfo(subCommand string, execution *schema.ComponentExecution) *schema.ConfigAndStacksInfo { @@ -107,7 +121,7 @@ func TestExtraArgumentsPosition(t *testing.T) { } func TestRunExecutionPhaseWithoutExecutionIsUnchanged(t *testing.T) { - shell := &fakeShell{} + shell := newShellFixture(t, nil) e := hookExecutor(shell) info := hookInfo("plan", nil) called := 0 @@ -126,7 +140,7 @@ func TestRunExecutionPhaseWithoutExecutionIsUnchanged(t *testing.T) { func TestMainCommandAppliesExtraArgumentsAndEnvironment(t *testing.T) { dir := t.TempDir() require.NoError(t, os.WriteFile(filepath.Join(dir, "required.tfvars"), nil, 0o600)) - shell := &fakeShell{} + shell := newShellFixture(t, nil) e := hookExecutor(shell) info := hookInfo("plan", &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{ {Commands: []string{"plan"}, Arguments: []string{"-lock-timeout=1m"}, RequiredVarFiles: []string{"required.tfvars"}, Environment: map[string]string{"TF_LOG": "INFO"}}, @@ -143,7 +157,7 @@ func TestMainCommandAppliesExtraArgumentsAndEnvironment(t *testing.T) { } func TestMainCommandMissingRequiredVarFileFailsBeforeAnyProcess(t *testing.T) { - shell := &fakeShell{} + shell := newShellFixture(t, nil) info := hookInfo("plan", &schema.ComponentExecution{ ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, RequiredVarFiles: []string{"missing.tfvars"}}}, BeforeHooks: []schema.ComponentHook{execHook("before", "plan")}, @@ -156,7 +170,7 @@ func TestMainCommandMissingRequiredVarFileFailsBeforeAnyProcess(t *testing.T) { func TestHooksRunWithTheInvocationEnvironmentAndDirectory(t *testing.T) { dir := t.TempDir() absolute := t.TempDir() - shell := &fakeShell{} + shell := newShellFixture(t, nil) e := hookExecutor(shell) ctx := context.WithValue(t.Context(), hookContextKey{}, "marker") info := hookInfo("plan", &schema.ComponentExecution{ @@ -187,13 +201,13 @@ func TestHooksRunWithTheInvocationEnvironmentAndDirectory(t *testing.T) { func TestHookOutputGoesToTheUIStreamNotTheDataStream(t *testing.T) { var data, ui strings.Builder streams := process.Streams{Stdout: &data, Stderr: &ui} - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if call.command == "hook-loud" || call.command == "hook-quiet" { _, _ = io.WriteString(call.options.StdoutOverride, call.command+" says hi\n") _, _ = io.WriteString(call.options.StdoutCapture, call.command+" says hi\n") } return nil - }} + }) info := hookInfo("plan", &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{ {Name: "loud", Commands: []string{"plan"}, Execute: []string{"hook-loud"}}, {Name: "quiet", Commands: []string{"plan"}, Execute: []string{"hook-quiet"}, SuppressStdout: true}, }}) @@ -208,12 +222,12 @@ func TestHookOutputGoesToTheUIStreamNotTheDataStream(t *testing.T) { } func TestFailingBeforeHookAbortsTheCommandAndSurfacesItsExitCode(t *testing.T) { - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if call.command == "hook-b1" { return errUtils.ExitCodeError{Code: 23} } return nil - }} + }) info := hookInfo("apply", &schema.ComponentExecution{ BeforeHooks: []schema.ComponentHook{execHook("b1", "apply"), execHook("b2", "apply")}, AfterHooks: []schema.ComponentHook{execHook("a1", "apply")}, @@ -225,12 +239,12 @@ func TestFailingBeforeHookAbortsTheCommandAndSurfacesItsExitCode(t *testing.T) { } func TestAfterHooksFollowRunOnErrorWhenTheCommandFails(t *testing.T) { - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if call.command == "terraform" { return errUtils.ExitCodeError{Code: 1} } return nil - }} + }) always := execHook("always", "plan") always.RunOnError = true info := hookInfo("plan", &schema.ComponentExecution{AfterHooks: []schema.ComponentHook{execHook("skipped", "plan"), always}}) @@ -251,12 +265,12 @@ func TestDetailedPlanChangesRunAfterHooksNotErrorHooks(t *testing.T) { {"exit 2 without -detailed-exitcode is a failure", []string{"plan"}, 2, []string{"terraform plan", "hook-failed arg"}}, } { t.Run(test.name, func(t *testing.T) { - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if call.command == "terraform" { return errUtils.ExitCodeError{Code: test.code} } return nil - }} + }) info := hookInfo("plan", &schema.ComponentExecution{ AfterHooks: []schema.ComponentHook{execHook("after", "plan")}, ErrorHooks: []schema.ComponentHook{{Name: "failed", Commands: []string{"plan"}, Execute: []string{"hook-failed", "arg"}, OnErrors: []string{".*"}}}, @@ -279,13 +293,13 @@ func TestErrorHookMatchesTheFailedCommandOutput(t *testing.T) { {"non matching output does not", []string{"AccessDenied"}, "Error: other", []string{"terraform apply"}}, } { t.Run(test.name, func(t *testing.T) { - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if call.command != "terraform" { return nil } _, _ = io.WriteString(call.options.StderrCapture, test.stderr) return errUtils.ExitCodeError{Code: 1} - }} + }) info := hookInfo("apply", &schema.ComponentExecution{ErrorHooks: []schema.ComponentHook{ {Name: "recover", Commands: []string{"apply"}, Execute: []string{"hook-recover", "arg"}, OnErrors: test.patterns}, }}) @@ -299,7 +313,7 @@ func TestErrorHookMatchesTheFailedCommandOutput(t *testing.T) { } func TestDryRunAppliesArgumentsButNeverRunsHooks(t *testing.T) { - shell := &fakeShell{} + shell := newShellFixture(t, nil) info := hookInfo("plan", &schema.ComponentExecution{ ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, Arguments: []string{"-lock=false"}}}, BeforeHooks: []schema.ComponentHook{execHook("b", "plan")}, @@ -315,13 +329,13 @@ func TestDryRunAppliesArgumentsButNeverRunsHooks(t *testing.T) { func TestCancellationStopsRemainingHooksAndTheCommand(t *testing.T) { ctx, cancel := context.WithCancel(t.Context()) - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if call.command == "hook-b1" { cancel() return context.Canceled } return nil - }} + }) always := execHook("always", "plan") always.RunOnError = true info := hookInfo("plan", &schema.ComponentExecution{ @@ -334,7 +348,7 @@ func TestCancellationStopsRemainingHooksAndTheCommand(t *testing.T) { } func TestDestroyCommandNameMatchesApplyDestroy(t *testing.T) { - shell := &fakeShell{} + shell := newShellFixture(t, nil) info := hookInfo("apply", &schema.ComponentExecution{ BeforeHooks: []schema.ComponentHook{execHook("for-apply", "apply"), execHook("for-destroy", "destroy")}, ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"destroy"}, Arguments: []string{"-parallelism=1"}}}, @@ -345,7 +359,7 @@ func TestDestroyCommandNameMatchesApplyDestroy(t *testing.T) { } func TestSavedPlanApplyDropsVariableArguments(t *testing.T) { - shell := &fakeShell{} + shell := newShellFixture(t, nil) info := hookInfo("apply", &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{ {Commands: []string{"apply"}, Arguments: []string{"-var=a=b", "-parallelism=1"}, RequiredVarFiles: []string{"never-needed.tfvars"}}, }}) @@ -357,7 +371,7 @@ func TestSavedPlanApplyDropsVariableArguments(t *testing.T) { // Hooks and extra arguments are matched per phase. The implicit init is "init" even when the // user asked for plan, so plan hooks must not fire for it, and init hooks must not fire for plan. func TestImplicitInitMatchesInitNotTheRequestedCommand(t *testing.T) { - shell := &fakeShell{} + shell := newShellFixture(t, nil) e := hookExecutor(shell) execution := &schema.ComponentExecution{ ExtraArguments: []schema.ComponentExtraArguments{ @@ -390,12 +404,12 @@ func TestImplicitInitMatchesInitNotTheRequestedCommand(t *testing.T) { } func TestHooksListedOnlyForOtherCommandsNeverFire(t *testing.T) { - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if call.command == "terraform" { return errUtils.ExitCodeError{Code: 1} } return nil - }} + }) e := hookExecutor(shell) always := execHook("always", "apply") always.RunOnError = true @@ -415,7 +429,7 @@ func TestHooksListedOnlyForOtherCommandsNeverFire(t *testing.T) { } func TestWorkspaceSelectionNeverRunsHooksOrExtraArguments(t *testing.T) { - shell := &fakeShell{run: func(*shellCall) error { return nil }} + shell := newShellFixture(t, func(*shellCall) error { return nil }) e := hookExecutor(shell) info := hookInfo("plan", &schema.ComponentExecution{ ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan", "init", "workspace"}, Arguments: []string{"-never"}, Environment: map[string]string{"NEVER": "1"}}}, @@ -430,12 +444,12 @@ func TestWorkspaceSelectionNeverRunsHooksOrExtraArguments(t *testing.T) { } func TestUnknownHookFailureIsNotMistakenForSuccess(t *testing.T) { - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if call.command == "hook-b" { return errors.New("executable file not found") } return nil - }} + }) info := hookInfo("plan", &schema.ComponentExecution{BeforeHooks: []schema.ComponentHook{{Name: "b", Commands: []string{"plan"}, Execute: []string{"hook-b"}}}}) err := hookExecutor(shell).runMainTerraformCommand(&schema.AtmosConfiguration{}, info, []string{"plan"}, t.TempDir()) require.ErrorIs(t, err, errUtils.ErrComponentHookFailed) @@ -482,7 +496,7 @@ func TestSavedPlanBindingKeepsExecution(t *testing.T) { } func TestImplicitInitMissingRequiredVarFileFailsBeforeAnyProcess(t *testing.T) { - shell := &fakeShell{} + shell := newShellFixture(t, nil) info := hookInfo("plan", &schema.ComponentExecution{ ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, RequiredVarFiles: []string{"missing.tfvars"}}}, BeforeHooks: []schema.ComponentHook{execHook("init-hook", "init"), execHook("plan-hook", "plan")}, @@ -531,11 +545,11 @@ func TestPolicyPlanUsesProviderOutputArguments(t *testing.T) { original = &schema.ComponentExecution{ExtraArguments: []schema.ComponentExtraArguments{{Commands: []string{"plan"}, Arguments: test.provider, Environment: map[string]string{"PROVIDER_ENV": "set"}}}} info.ComponentExecution = original } - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { return os.WriteFile(plannedOutputPath(execCtx.componentPath, call.args), []byte("reviewed"), 0o600) - }} + }) executor := testExecutor(t) - executor.services.ExecuteShellCommand = shell.execute + executor.services.ExecuteShellCommand = shell.mock.Execute err := executor.runMainCommandPhase(config, info, execCtx, test.args, execCtx.componentPath, false) require.Same(t, original, info.ComponentExecution, "the component execution must be restored") if test.conflict { @@ -568,7 +582,7 @@ func TestPolicyPlanUsesProviderOutputArguments(t *testing.T) { // attempts, and the forced init runs between them. func TestInitRecoveryRetriesInsideOneHookLifecycle(t *testing.T) { attempts := 0 - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if call.command != "terraform" { return nil } @@ -579,9 +593,9 @@ func TestInitRecoveryRetriesInsideOneHookLifecycle(t *testing.T) { } _, _ = io.WriteString(call.options.ExecMetadataStdoutCapture, "Apply complete!") return nil - }} + }) executor := testExecutor(t) - executor.services.ExecuteShellCommand = shell.execute + executor.services.ExecuteShellCommand = shell.mock.Execute var timeline []string executor.executeTerraformInitForcedFn = func(_ *schema.AtmosConfiguration, _ *schema.ConfigAndStacksInfo, _, _ string, reconfigure, _ bool, _ ...ShellCommandOption) error { require.True(t, reconfigure) @@ -608,16 +622,16 @@ func TestInitRecoveryRetriesInsideOneHookLifecycle(t *testing.T) { // hooks keeps the existing recovery path and retries the main command exactly once. func TestInitRecoveryWithoutHooksStillRetriesOnce(t *testing.T) { attempts := 0 - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { attempts++ if attempts == 1 { _, _ = io.WriteString(call.options.ExecMetadataStderrCapture, "Error: Backend configuration changed") return errUtils.ExitCodeError{Code: 1} } return nil - }} + }) executor := testExecutor(t) - executor.services.ExecuteShellCommand = shell.execute + executor.services.ExecuteShellCommand = shell.mock.Execute forced := 0 executor.executeTerraformInitForcedFn = func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, string, string, bool, bool, ...ShellCommandOption) error { forced++ @@ -636,15 +650,15 @@ func TestInitRecoveryWithoutHooksStillRetriesOnce(t *testing.T) { // TestInitRecoveryInsideLifecycleRunsOnlyWhenDiagnosed verifies that a hooked command failing // for another reason is not retried and still reaches its after hooks once. func TestInitRecoveryInsideLifecycleRunsOnlyWhenDiagnosed(t *testing.T) { - shell := &fakeShell{run: func(call *shellCall) error { + shell := newShellFixture(t, func(call *shellCall) error { if call.command == "terraform" { _, _ = io.WriteString(call.options.ExecMetadataStderrCapture, "Error: unrelated validation failure") return errUtils.ExitCodeError{Code: 1} } return nil - }} + }) executor := testExecutor(t) - executor.services.ExecuteShellCommand = shell.execute + executor.services.ExecuteShellCommand = shell.mock.Execute executor.executeTerraformInitForcedFn = func(*schema.AtmosConfiguration, *schema.ConfigAndStacksInfo, string, string, bool, bool, ...ShellCommandOption) error { t.Fatal("forced init must not run without an init-required diagnosis") return nil diff --git a/pkg/component/terraform/mock_shell_test.go b/pkg/component/terraform/mock_shell_test.go new file mode 100644 index 00000000000..c89a2c514ad --- /dev/null +++ b/pkg/component/terraform/mock_shell_test.go @@ -0,0 +1,61 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: execute_hooks_test.go +// +// Generated by this command: +// +// mockgen -source=execute_hooks_test.go -destination=mock_shell_test.go -package=terraform -mock_names=shellCommandExecutor=MockShellCommandExecutor +// + +// Package terraform is a generated GoMock package. +package terraform + +import ( + reflect "reflect" + + component "github.com/cloudposse/atmos/pkg/component" + schema "github.com/cloudposse/atmos/pkg/schema" + gomock "go.uber.org/mock/gomock" +) + +// MockShellCommandExecutor is a mock of shellCommandExecutor interface. +type MockShellCommandExecutor struct { + ctrl *gomock.Controller + recorder *MockShellCommandExecutorMockRecorder + isgomock struct{} +} + +// MockShellCommandExecutorMockRecorder is the mock recorder for MockShellCommandExecutor. +type MockShellCommandExecutorMockRecorder struct { + mock *MockShellCommandExecutor +} + +// NewMockShellCommandExecutor creates a new mock instance. +func NewMockShellCommandExecutor(ctrl *gomock.Controller) *MockShellCommandExecutor { + mock := &MockShellCommandExecutor{ctrl: ctrl} + mock.recorder = &MockShellCommandExecutorMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockShellCommandExecutor) EXPECT() *MockShellCommandExecutorMockRecorder { + return m.recorder +} + +// Execute mocks base method. +func (m *MockShellCommandExecutor) Execute(arg0 schema.AtmosConfiguration, arg1 string, arg2 []string, arg3 string, arg4 []string, arg5 bool, arg6 string, arg7 ...component.ExecutionOption) error { + m.ctrl.T.Helper() + varargs := []any{arg0, arg1, arg2, arg3, arg4, arg5, arg6} + for _, a := range arg7 { + varargs = append(varargs, a) + } + ret := m.ctrl.Call(m, "Execute", varargs...) + ret0, _ := ret[0].(error) + return ret0 +} + +// Execute indicates an expected call of Execute. +func (mr *MockShellCommandExecutorMockRecorder) Execute(arg0, arg1, arg2, arg3, arg4, arg5, arg6 any, arg7 ...any) *gomock.Call { + mr.mock.ctrl.T.Helper() + varargs := append([]any{arg0, arg1, arg2, arg3, arg4, arg5, arg6}, arg7...) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Execute", reflect.TypeOf((*MockShellCommandExecutor)(nil).Execute), varargs...) +} diff --git a/pkg/terraform/output/configuration_state.go b/pkg/terraform/output/configuration_state.go index f27b40b5b79..40873b8bcab 100644 --- a/pkg/terraform/output/configuration_state.go +++ b/pkg/terraform/output/configuration_state.go @@ -170,7 +170,7 @@ func configurationInitializedOutputConfig(config *ComponentConfig, state *schema if state.WorkingDirectory == "" { return errUtils.Build(errUtils.ErrTerraformOutputFailed). WithExplanationf("Component '%s' in stack '%s' reported no working directory to read outputs from without init.", component, stack). - WithHint("Run the command without --skip-init so Atmos can initialize a temporary state reader"). + WithHint("Allow initialization with init.mode: auto and without --skip-init so Atmos can initialize a temporary state reader"). Err() } dataDir := configurationDataDir(config) @@ -180,7 +180,7 @@ func configurationInitializedOutputConfig(config *ComponentConfig, state *schema if info, err := os.Stat(dataDir); err != nil || !info.IsDir() { return errUtils.Build(errUtils.ErrTerraformOutputFailed). WithExplanationf("Component '%s' in stack '%s' has not been initialized in %s.", component, stack, state.WorkingDirectory). - WithHint("Run the command without --skip-init so Atmos can initialize a temporary state reader"). + WithHint("Allow initialization with init.mode: auto and without --skip-init so Atmos can initialize a temporary state reader"). Err() } config.ComponentPath = state.WorkingDirectory @@ -221,8 +221,8 @@ func remoteStateOverrides(remote map[string]any, remoteType, placeholderType str // configurationStateReader points config at the directory a plugin-owned state read runs in and // returns its cleanup. Skipping init reads the unit's already-initialized directory as it is; // otherwise a disposable backend-only scratch directory is initialized. -func configurationStateReader(config *ComponentConfig, state *schema.ComponentConfigurationState, opts *OutputOptions, component, stack string) (func(), error) { - if opts != nil && opts.SkipInit { +func configurationStateReader(config *ComponentConfig, state *schema.ComponentConfigurationState, skipInit bool, component, stack string) (func(), error) { + if skipInit { return func() {}, configurationInitializedOutputConfig(config, state, component, stack) } return configurationOutputConfig(config, state) diff --git a/pkg/terraform/output/configuration_state_test.go b/pkg/terraform/output/configuration_state_test.go index f46e6d641dd..8ed6c6f4424 100644 --- a/pkg/terraform/output/configuration_state_test.go +++ b/pkg/terraform/output/configuration_state_test.go @@ -222,60 +222,86 @@ func TestConfigurationStateSectionsNamedWorkspaceWithoutBackendSettings(t *testi require.Nil(t, state.Backend) } -// TestPluginOutputSkipInitReadsTheInitializedUnit verifies that skipping init is honored for a +// TestPluginOutputSkipInitReadsTheInitializedUnit verifies that skip-init and init.mode: never honor a // plugin-owned component: outputs are read from the unit's initialized directory as it is, with no // init, no workspace selection, and no generated files. func TestPluginOutputSkipInitReadsTheInitializedUnit(t *testing.T) { - ctrl := gomock.NewController(t) - runner := NewMockTerraformRunner(ctrl) - runner.EXPECT().SetEnv(gomock.Any()).Return(nil) - runner.EXPECT().Output(gomock.Any()).Return(map[string]tfexec.OutputMeta{"result": {Type: json.RawMessage(`"string"`), Value: json.RawMessage(`"applied"`)}}, nil) - config := validAtmosConfig(t) - unit := t.TempDir() - require.NoError(t, os.Mkdir(filepath.Join(unit, ".terraform"), 0o700)) - config.ComponentConfigurations = stateRuntime(t, func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { - return &schema.ComponentConfigurationState{BackendType: "local", Workspace: "prod", WorkingDirectory: unit}, nil - }) - sections := validSections() - sections["settings"] = map[string]any{"configuration": true} - sections["providers"] = map[string]any{"aws": map[string]any{"region": "us-east-2"}} - binary, err := os.Executable() - require.NoError(t, err) - sections["command"] = binary - executor := NewExecutor(nil, WithRunnerFactory(func(dir, _ string) (TerraformRunner, error) { - require.Equal(t, unit, dir, "skipping init must read the unit's own initialized directory") - return runner, nil - })) - - result, err := executor.execute(t.Context(), config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true) - require.NoError(t, err) - require.Equal(t, "applied", result["result"]) - files, err := os.ReadDir(unit) - require.NoError(t, err) - require.Len(t, files, 1, "no backend, provider, or marker file may be written into the unit") - require.Equal(t, ".terraform", files[0].Name()) -} - -// TestPluginOutputSkipInitRejectsAnUninitializedUnit verifies that skipping init never falls back -// to initializing anything: a unit that was never initialized fails with a clear error. -func TestPluginOutputSkipInitRejectsAnUninitializedUnit(t *testing.T) { - for name, state := range map[string]*schema.ComponentConfigurationState{ - "no working directory": {BackendType: "local", Workspace: "prod"}, - "not initialized": {BackendType: "local", Workspace: "prod", WorkingDirectory: t.TempDir()}, + for _, policy := range []struct { + name string + mode schema.TerraformInitMode + opts *OutputOptions + }{ + {name: "explicit skip", opts: &OutputOptions{SkipInit: true}}, + {name: "mode never without options", mode: schema.TerraformInitModeNever}, + {name: "mode never with options", mode: schema.TerraformInitModeNever, opts: &OutputOptions{}}, } { - t.Run(name, func(t *testing.T) { + t.Run(policy.name, func(t *testing.T) { + ctrl := gomock.NewController(t) + runner := NewMockTerraformRunner(ctrl) + runner.EXPECT().SetEnv(gomock.Any()).Return(nil) + runner.EXPECT().Output(gomock.Any()).Return(map[string]tfexec.OutputMeta{"result": {Type: json.RawMessage(`"string"`), Value: json.RawMessage(`"applied"`)}}, nil) config := validAtmosConfig(t) + config.Components.Terraform.Init.Mode = policy.mode + unit := t.TempDir() + require.NoError(t, os.Mkdir(filepath.Join(unit, ".terraform"), 0o700)) config.ComponentConfigurations = stateRuntime(t, func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { - return state, nil + return &schema.ComponentConfigurationState{BackendType: "local", Workspace: "prod", WorkingDirectory: unit}, nil }) sections := validSections() sections["settings"] = map[string]any{"configuration": true} - executor := NewExecutor(nil, WithRunnerFactory(func(string, string) (TerraformRunner, error) { - t.Fatal("no Terraform process may start") - return nil, nil + sections["providers"] = map[string]any{"aws": map[string]any{"region": "us-east-2"}} + binary, err := os.Executable() + require.NoError(t, err) + sections["command"] = binary + executor := NewExecutor(nil, WithRunnerFactory(func(dir, _ string) (TerraformRunner, error) { + require.Equal(t, unit, dir, "skipping init must read the unit's own initialized directory") + return runner, nil })) - _, err := executor.execute(t.Context(), config, "network", "live", sections, nil, &OutputOptions{SkipInit: true}, true) - require.ErrorIs(t, err, errUtils.ErrTerraformOutputFailed) + + result, err := executor.execute(t.Context(), config, "network", "live", sections, nil, policy.opts, true) + require.NoError(t, err) + require.Equal(t, "applied", result["result"]) + files, err := os.ReadDir(unit) + require.NoError(t, err) + require.Len(t, files, 1, "no backend, provider, or marker file may be written into the unit") + require.Equal(t, ".terraform", files[0].Name()) + }) + } +} + +// TestPluginOutputSkipInitRejectsAnUninitializedUnit verifies that disabling init never falls back +// to initializing anything: a unit that was never initialized fails with a clear error. +func TestPluginOutputSkipInitRejectsAnUninitializedUnit(t *testing.T) { + for _, policy := range []struct { + name string + mode schema.TerraformInitMode + opts *OutputOptions + }{ + {name: "explicit skip", opts: &OutputOptions{SkipInit: true}}, + {name: "mode never without options", mode: schema.TerraformInitModeNever}, + {name: "mode never with options", mode: schema.TerraformInitModeNever, opts: &OutputOptions{}}, + } { + t.Run(policy.name, func(t *testing.T) { + for name, state := range map[string]*schema.ComponentConfigurationState{ + "no working directory": {BackendType: "local", Workspace: "prod"}, + "not initialized": {BackendType: "local", Workspace: "prod", WorkingDirectory: t.TempDir()}, + } { + t.Run(name, func(t *testing.T) { + config := validAtmosConfig(t) + config.Components.Terraform.Init.Mode = policy.mode + config.ComponentConfigurations = stateRuntime(t, func(context.Context, *schema.ConfigAndStacksInfo) (*schema.ComponentConfigurationState, error) { + return state, nil + }) + sections := validSections() + sections["settings"] = map[string]any{"configuration": true} + executor := NewExecutor(nil, WithRunnerFactory(func(string, string) (TerraformRunner, error) { + t.Fatal("no Terraform process may start") + return nil, nil + })) + _, err := executor.execute(t.Context(), config, "network", "live", sections, nil, policy.opts, true) + require.ErrorIs(t, err, errUtils.ErrTerraformOutputFailed) + }) + } }) } } diff --git a/pkg/terraform/output/executor.go b/pkg/terraform/output/executor.go index 4f739a30fa4..94ea630d632 100644 --- a/pkg/terraform/output/executor.go +++ b/pkg/terraform/output/executor.go @@ -500,15 +500,19 @@ func (e *Executor) execute( if err != nil { return nil, err } + skipInit := opts != nil && opts.SkipInit if state != nil { + // A plugin state reader must reuse initialized state when init is disabled. + // Ordinary components retain their existing mode and workspace behavior. + skipInit = skipInit || config.InitMode == schema.TerraformInitModeNever config.BaseEnvironment = configurationEnvironment(authManagers) - cleanup, err := configurationStateReader(config, state, opts, component, stack) + cleanup, err := configurationStateReader(config, state, skipInit, component, stack) if err != nil { return nil, err } defer cleanup() // Only the scratch reader regenerates its backend; the initialized unit is read as it is. - processYamlFunctions = opts == nil || !opts.SkipInit + processYamlFunctions = !skipInit } // Step 2.5: Auto-provision JIT workdir if needed before toolchain resolution and init. @@ -600,7 +604,6 @@ func (e *Executor) execute( // SkipInit is set: the component was just applied and .terraform/ state is // already correct — re-initializing would require auth credentials that // may not be available in PostRunE context. - skipInit := opts != nil && opts.SkipInit if err := e.ensureInitialized(ctx, atmosConfig, runner, config, component, stack, stderrCapture, pluginCache, environMap, skipInit); err != nil { return nil, err }