Skip to content

Commit c5a92d9

Browse files
committed
Clean the env vars of most TF_* flags
Such variables would allow a crafty user to directly manipulate terraform. We do not want users doing that. Terraform is NOT an API. It is a hidden completely internal implementation detail. So clear those env vars.
1 parent 5252bff commit c5a92d9

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

cmd/openshift-install/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"flag"
5+
"fmt"
56
"io/ioutil"
67
"os"
78
"path/filepath"
@@ -24,6 +25,9 @@ var (
2425
)
2526

2627
func main() {
28+
// Do this extremely early before we could have multiple threads
29+
cleanEnvironment()
30+
2731
// This attempts to configure klog (used by vendored Kubernetes code) not
2832
// to log anything.
2933
var fs flag.FlagSet
@@ -43,6 +47,29 @@ func main() {
4347
installerMain()
4448
}
4549

50+
// Drop all terraform environment variables that the user may have passed
51+
// so they can't use terraform directly.
52+
func cleanEnvironment() {
53+
environ := os.Environ()
54+
for _, env := range environ {
55+
splits := strings.Split(env, "=")
56+
key := splits[0]
57+
if strings.HasPrefix(key, "TF_") {
58+
switch key {
59+
case "TF_LOG":
60+
//Do Nothing
61+
default:
62+
// Using format because log hasn't been set up yet.
63+
// and I don't want to do this after log is set up because
64+
// then we might have multiple threads and messing with ENV
65+
// is basically impossible then.
66+
fmt.Printf("Ignoring unsupported environment variable: %s", key)
67+
os.Unsetenv(key)
68+
}
69+
}
70+
}
71+
}
72+
4673
func installerMain() {
4774
rootCmd := newRootCmd()
4875

0 commit comments

Comments
 (0)