Skip to content

Commit 29577d8

Browse files
authored
Warn on specific TF Environment Variables (#1206)
* warn on specific TF vars * warn on specific TF vars * remove duplicated warnings
1 parent a62050b commit 29577d8

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

internal/exec/shell_utils.go

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"mvdan.cc/sh/v3/interp"
1818
"mvdan.cc/sh/v3/syntax"
1919

20+
log "github.com/charmbracelet/log"
2021
"github.com/cloudposse/atmos/pkg/schema"
2122
u "github.com/cloudposse/atmos/pkg/utils"
2223
)
@@ -214,7 +215,7 @@ func execTerraformShellCommand(
214215
}
215216
val, err := strconv.Atoi(atmosShellLvl)
216217
if err != nil {
217-
u.LogWarning(fmt.Sprintf("Failed to parse ATMOS_SHLVL: %v", err))
218+
log.Warn("Failed to parse ATMOS_SHLVL", "error", err)
218219
return
219220
}
220221
// Prevent negative values
@@ -223,7 +224,7 @@ func execTerraformShellCommand(
223224
newVal = 0
224225
}
225226
if err := os.Setenv("ATMOS_SHLVL", fmt.Sprintf("%d", newVal)); err != nil {
226-
u.LogWarning(fmt.Sprintf("Failed to update ATMOS_SHLVL: %v", err))
227+
log.Warn("Failed to update ATMOS_SHLVL", "error", err)
227228
}
228229
}()
229230

@@ -342,16 +343,6 @@ func execTerraformShellCommand(
342343
func mergeEnvVars(atmosConfig schema.AtmosConfiguration, componentEnvList []string) []string {
343344
envMap := make(map[string]string)
344345

345-
// Parse system environment variables
346-
for _, env := range os.Environ() {
347-
if parts := strings.SplitN(env, "=", 2); len(parts) == 2 {
348-
if strings.HasPrefix(parts[0], "TF_") {
349-
u.LogWarning(fmt.Sprintf("detected '%s' set in the environment; this may interfere with Atmos's control of Terraform.", parts[0]))
350-
}
351-
envMap[parts[0]] = parts[1]
352-
}
353-
}
354-
355346
// Merge with new, Atmos defined environment variables
356347
for _, env := range componentEnvList {
357348
if parts := strings.SplitN(env, "=", 2); len(parts) == 2 {

internal/exec/terraform.go

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"strings"
99

10+
log "github.com/charmbracelet/log"
1011
"github.com/pkg/errors"
1112
"github.com/spf13/cobra"
1213

@@ -222,13 +223,40 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
222223
return err
223224
}
224225

225-
// Check for any Terraform environment variables that might conflict with Atmos
226+
// Check for specific Terraform environment variables that might conflict with Atmos
227+
warnOnExactVars := []string{
228+
"TF_CLI_ARGS",
229+
"TF_WORKSPACE",
230+
}
231+
232+
warnOnPrefixVars := []string{
233+
"TF_VAR_",
234+
"TF_CLI_ARGS_",
235+
}
236+
237+
var problematicVars []string
238+
226239
for _, envVar := range os.Environ() {
227-
if strings.HasPrefix(envVar, "TF_") {
228-
varName := strings.SplitN(envVar, "=", 2)[0]
229-
u.LogWarning(fmt.Sprintf("detected '%s' set in the environment; this may interfere with Atmos's control of Terraform.", varName))
240+
if parts := strings.SplitN(envVar, "=", 2); len(parts) == 2 {
241+
// Check for exact matches
242+
if u.SliceContainsString(warnOnExactVars, parts[0]) {
243+
problematicVars = append(problematicVars, parts[0])
244+
}
245+
// Check for prefix matches
246+
for _, prefix := range warnOnPrefixVars {
247+
if strings.HasPrefix(parts[0], prefix) {
248+
problematicVars = append(problematicVars, parts[0])
249+
break
250+
}
251+
}
230252
}
231253
}
254+
255+
if len(problematicVars) > 0 {
256+
log.Warn("detected environment variables that may interfere with Atmos's control of Terraform",
257+
"variables", problematicVars)
258+
}
259+
232260
info.ComponentEnvList = append(info.ComponentEnvList, fmt.Sprintf("ATMOS_CLI_CONFIG_PATH=%s", atmosConfig.CliConfigPath))
233261
basePath, err := filepath.Abs(atmosConfig.BasePath)
234262
if err != nil {

0 commit comments

Comments
 (0)