Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ require (
github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.7 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-version v1.8.0 // indirect
github.com/hashicorp/go-version v1.8.0
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
Expand Down
26 changes: 26 additions & 0 deletions pkg/shell/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ import (
"path/filepath"
"regexp"
"strings"
"sync"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-exec/tfexec"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/gocty"
Expand Down Expand Up @@ -74,12 +76,36 @@ func tfExecPath() (string, error) {
return path, nil
}

const minTfVersion = "1.12.2"

var tfVersionOnce sync.Once

func checkTfVersion() {
tfVersionOnce.Do(func() {
v, err := TfVersion()
if err != nil {
return // Ignore error, maybe terraform version --json is not supported or something else
}

tfVer, err := version.NewVersion(v)
if err != nil {
return
}

minVer, _ := version.NewVersion(minTfVersion)
if tfVer.LessThan(minVer) {
logging.Warn("Terraform version %s is older than the recommended minimum version %s. Please consider upgrading.", v, minTfVersion)
}
Comment on lines +95 to +98
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Ignoring the error from version.NewVersion when parsing the hardcoded minTfVersion constant could lead to a panic. If minTfVersion were ever set to an invalid version string, version.NewVersion would return a nil version and an error. The subsequent call to tfVer.LessThan(minVer) would then cause a nil pointer dereference. It's safer to handle this potential error by panicking with a descriptive message, which would catch such a configuration bug during development.

                minVer, err := version.NewVersion(minTfVersion)
                if err != nil {
                        // This is a developer error with a constant, so panicking is appropriate
                        // to catch this during development.
                        panic(fmt.Sprintf("BUG: hardcoded minimum terraform version %q is invalid: %v", minTfVersion, err))
                }
                if tfVer.LessThan(minVer) {
                        logging.Warn("Terraform version %s is older than the recommended minimum version %s. Please consider upgrading.", v, minTfVersion)
                }

})
}

// ConfigureTerraform returns a Terraform object used to execute commands
func ConfigureTerraform(workingDir string) (*tfexec.Terraform, error) {
path, err := tfExecPath()
if err != nil {
return nil, err
}
checkTfVersion()
return tfexec.NewTerraform(workingDir, path)
}

Expand Down
Loading