From d9b0d2d3a7c3281f8b5c308bc576c0b2e2279f10 Mon Sep 17 00:00:00 2001 From: milldr Date: Fri, 27 Dec 2024 10:06:12 -0500 Subject: [PATCH 1/3] added warning message when TF_CLI var-file specified --- internal/exec/shell_utils.go | 20 +++++++++++++------- internal/exec/terraform.go | 13 +++++++++++++ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/internal/exec/shell_utils.go b/internal/exec/shell_utils.go index fbf14a6f9..c3969f119 100644 --- a/internal/exec/shell_utils.go +++ b/internal/exec/shell_utils.go @@ -185,13 +185,19 @@ func execTerraformShellCommand( } }() - // Set the Terraform environment variables to reference the var file - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_plan=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_apply=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_refresh=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_import=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_destroy=-var-file=%s", varFile)) - componentEnvList = append(componentEnvList, fmt.Sprintf("TF_CLI_ARGS_console=-var-file=%s", varFile)) + // Define the Terraform commands that may use var-file configuration + tfCommands := []string{"plan", "apply", "refresh", "import", "destroy", "console"} + + // Check for existing var-file arguments in TF_CLI environment variables + for _, cmd := range tfCommands { + envVar := fmt.Sprintf("TF_CLI_ARGS_%s", cmd) + existing := os.Getenv(envVar) + if existing != "" && strings.Contains(existing, "-var-file=") { + u.LogWarning(atmosConfig, "Found var-file in environment! This may be overwritten by Atmos") + } + // Set the Terraform environment variable to reference the var file + componentEnvList = append(componentEnvList, fmt.Sprintf("%s=-var-file=%s", envVar, varFile)) + } // Set environment variables to indicate the details of the Atmos shell configuration componentEnvList = append(componentEnvList, fmt.Sprintf("ATMOS_STACK=%s", stack)) diff --git a/internal/exec/terraform.go b/internal/exec/terraform.go index fcf65bdb3..1910471c3 100644 --- a/internal/exec/terraform.go +++ b/internal/exec/terraform.go @@ -245,10 +245,23 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error { info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("TF_APPEND_USER_AGENT=%s", appendUserAgent)) } + // Check for existing var-file arguments in TF_CLI environment variables + tfCommands := []string{"plan", "apply", "refresh", "import", "destroy", "console"} + for _, cmd := range tfCommands { + envVar := fmt.Sprintf("TF_CLI_ARGS_%s", cmd) + existing := os.Getenv(envVar) + if existing != "" && strings.Contains(existing, "-var-file=") { + u.LogWarning(atmosConfig, "Found var-file in environment! This may be overwritten by Atmos") + } + } + // Print ENV vars if they are found in the component's stack config if len(info.ComponentEnvList) > 0 { u.LogDebug(atmosConfig, "\nUsing ENV vars:") for _, v := range info.ComponentEnvList { + if strings.Contains(v, "-var-file=") { + u.LogWarning(atmosConfig, "Found var-file in component environment! This may be overwritten by Atmos") + } u.LogDebug(atmosConfig, v) } } From 0cbee45b4a6bf663518e56f6c668fe567776b403 Mon Sep 17 00:00:00 2001 From: milldr Date: Thu, 2 Jan 2025 16:22:26 -0500 Subject: [PATCH 2/3] Refactor handling of Terraform environment variables --- internal/exec/shell_utils.go | 12 ++++++++---- internal/exec/terraform.go | 21 ++++++++------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/internal/exec/shell_utils.go b/internal/exec/shell_utils.go index c3969f119..19e133a90 100644 --- a/internal/exec/shell_utils.go +++ b/internal/exec/shell_utils.go @@ -192,11 +192,15 @@ func execTerraformShellCommand( for _, cmd := range tfCommands { envVar := fmt.Sprintf("TF_CLI_ARGS_%s", cmd) existing := os.Getenv(envVar) - if existing != "" && strings.Contains(existing, "-var-file=") { - u.LogWarning(atmosConfig, "Found var-file in environment! This may be overwritten by Atmos") + if existing != "" { + // Remove any surrounding quotes from existing value + existing = strings.Trim(existing, "\"") + // Create new value by combining existing and new var-file argument + newValue := fmt.Sprintf("%s -var-file=%s", existing, varFile) + componentEnvList = append(componentEnvList, fmt.Sprintf("%s=%s", envVar, newValue)) + } else { + componentEnvList = append(componentEnvList, fmt.Sprintf("%s=-var-file=%s", envVar, varFile)) } - // Set the Terraform environment variable to reference the var file - componentEnvList = append(componentEnvList, fmt.Sprintf("%s=-var-file=%s", envVar, varFile)) } // Set environment variables to indicate the details of the Atmos shell configuration diff --git a/internal/exec/terraform.go b/internal/exec/terraform.go index 1910471c3..ef973bc6f 100644 --- a/internal/exec/terraform.go +++ b/internal/exec/terraform.go @@ -231,6 +231,14 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error { } } + // Check for any Terraform environment variables that might conflict with Atmos + for _, envVar := range os.Environ() { + if strings.HasPrefix(envVar, "TF_") { + varName := strings.Split(envVar, "=")[0] + u.LogWarning(atmosConfig, fmt.Sprintf("Found Terraform environment variable '%s' which may conflict with Atmos", varName)) + } + } + // Set `TF_IN_AUTOMATION` ENV var to `true` to suppress verbose instructions after terraform commands // https://developer.hashicorp.com/terraform/cli/config/environment-variables#tf_in_automation info.ComponentEnvList = append(info.ComponentEnvList, "TF_IN_AUTOMATION=true") @@ -245,23 +253,10 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error { info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("TF_APPEND_USER_AGENT=%s", appendUserAgent)) } - // Check for existing var-file arguments in TF_CLI environment variables - tfCommands := []string{"plan", "apply", "refresh", "import", "destroy", "console"} - for _, cmd := range tfCommands { - envVar := fmt.Sprintf("TF_CLI_ARGS_%s", cmd) - existing := os.Getenv(envVar) - if existing != "" && strings.Contains(existing, "-var-file=") { - u.LogWarning(atmosConfig, "Found var-file in environment! This may be overwritten by Atmos") - } - } - // Print ENV vars if they are found in the component's stack config if len(info.ComponentEnvList) > 0 { u.LogDebug(atmosConfig, "\nUsing ENV vars:") for _, v := range info.ComponentEnvList { - if strings.Contains(v, "-var-file=") { - u.LogWarning(atmosConfig, "Found var-file in component environment! This may be overwritten by Atmos") - } u.LogDebug(atmosConfig, v) } } From 971be0709bd4363a4c4c2880ed2796dcd012da2a Mon Sep 17 00:00:00 2001 From: milldr Date: Thu, 2 Jan 2025 16:42:58 -0500 Subject: [PATCH 3/3] Update handling of Terraform environment variables --- internal/exec/shell_utils.go | 5 +++-- internal/exec/terraform.go | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/exec/shell_utils.go b/internal/exec/shell_utils.go index 19e133a90..58d4b093b 100644 --- a/internal/exec/shell_utils.go +++ b/internal/exec/shell_utils.go @@ -193,10 +193,11 @@ func execTerraformShellCommand( envVar := fmt.Sprintf("TF_CLI_ARGS_%s", cmd) existing := os.Getenv(envVar) if existing != "" { + u.LogWarning(atmosConfig, fmt.Sprintf("Found Terraform environment variable '%s' which may conflict with or be overwritten by Atmos", envVar)) // Remove any surrounding quotes from existing value existing = strings.Trim(existing, "\"") // Create new value by combining existing and new var-file argument - newValue := fmt.Sprintf("%s -var-file=%s", existing, varFile) + newValue := fmt.Sprintf("\"%s -var-file=%s\"", existing, varFile) componentEnvList = append(componentEnvList, fmt.Sprintf("%s=%s", envVar, newValue)) } else { componentEnvList = append(componentEnvList, fmt.Sprintf("%s=-var-file=%s", envVar, varFile)) @@ -245,7 +246,7 @@ func execTerraformShellCommand( pa := os.ProcAttr{ Files: []*os.File{os.Stdin, os.Stdout, os.Stderr}, Dir: componentPath, - Env: append(os.Environ(), componentEnvList...), + Env: append(componentEnvList, os.Environ()...), // Give priority to Atmos defined env vars } // Start a new shell diff --git a/internal/exec/terraform.go b/internal/exec/terraform.go index ef973bc6f..768d0346e 100644 --- a/internal/exec/terraform.go +++ b/internal/exec/terraform.go @@ -235,7 +235,7 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error { for _, envVar := range os.Environ() { if strings.HasPrefix(envVar, "TF_") { varName := strings.Split(envVar, "=")[0] - u.LogWarning(atmosConfig, fmt.Sprintf("Found Terraform environment variable '%s' which may conflict with Atmos", varName)) + u.LogWarning(atmosConfig, fmt.Sprintf("Found Terraform environment variable '%s' which may conflict with or be overwritten by Atmos", varName)) } }