From 8323114e327bf7a7a12c996a8882539a75cd1bbc Mon Sep 17 00:00:00 2001 From: Prajith Date: Wed, 27 Dec 2023 21:19:27 +0530 Subject: [PATCH] fix: download conftest binary for correct arch (#4089) * download conftest binary for correct arch Signed-off-by: Prajith P * remove default constant Signed-off-by: Prajith P --------- Signed-off-by: Prajith P --- server/core/runtime/policy/conftest_client.go | 27 ++++++++++++++++--- .../runtime/policy/conftest_client_test.go | 8 ++---- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/server/core/runtime/policy/conftest_client.go b/server/core/runtime/policy/conftest_client.go index 9b0f1fa2f8..5218f883d2 100644 --- a/server/core/runtime/policy/conftest_client.go +++ b/server/core/runtime/policy/conftest_client.go @@ -20,15 +20,12 @@ import ( "github.com/runatlantis/atlantis/server/events/command" "github.com/runatlantis/atlantis/server/events/models" "github.com/runatlantis/atlantis/server/logging" - "golang.org/x/text/cases" - "golang.org/x/text/language" ) const ( DefaultConftestVersionEnvKey = "DEFAULT_CONFTEST_VERSION" conftestBinaryName = "conftest" conftestDownloadURLPrefix = "https://github.com/open-policy-agent/conftest/releases/download/v" - conftestArch = "x86_64" ) type Arg struct { @@ -113,8 +110,13 @@ type ConfTestVersionDownloader struct { func (c ConfTestVersionDownloader) downloadConfTestVersion(v *version.Version, destPath string) (runtime_models.FilePath, error) { versionURLPrefix := fmt.Sprintf("%s%s", conftestDownloadURLPrefix, v.Original()) + conftestPlatform := getPlatform() + if conftestPlatform == "" { + return runtime_models.LocalFilePath(""), fmt.Errorf("don't know where to find conftest for %s on %s", runtime.GOOS, runtime.GOARCH) + } + // download binary in addition to checksum file - binURL := fmt.Sprintf("%s/conftest_%s_%s_%s.tar.gz", versionURLPrefix, v.Original(), cases.Title(language.English).String(runtime.GOOS), conftestArch) + binURL := fmt.Sprintf("%s/conftest_%s_%s.tar.gz", versionURLPrefix, v.Original(), conftestPlatform) checksumURL := fmt.Sprintf("%s/checksums.txt", versionURLPrefix) // underlying implementation uses go-getter so the URL is formatted as such. @@ -313,3 +315,20 @@ func hasFailures(output string) bool { } return false } + +func getPlatform() string { + platform := runtime.GOOS + "_" + runtime.GOARCH + + switch platform { + case "linux_amd64": + return "Linux_x86_64" + case "linux_arm64": + return "Linux_arm64" + case "darwin_amd64": + return "Darwin_x86_64" + case "darwin_arm64": + return "Darwin_arm64" + default: + return "" + } +} diff --git a/server/core/runtime/policy/conftest_client_test.go b/server/core/runtime/policy/conftest_client_test.go index 143b6e8dbc..d8c5f5b9dc 100644 --- a/server/core/runtime/policy/conftest_client_test.go +++ b/server/core/runtime/policy/conftest_client_test.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "path/filepath" - "runtime" "testing" "github.com/hashicorp/go-version" @@ -17,17 +16,14 @@ import ( "github.com/runatlantis/atlantis/server/events/command" "github.com/runatlantis/atlantis/server/logging" . "github.com/runatlantis/atlantis/testing" - - "golang.org/x/text/cases" - "golang.org/x/text/language" ) func TestConfTestVersionDownloader(t *testing.T) { version, _ := version.NewVersion("0.25.0") destPath := "some/path" - - fullURL := fmt.Sprintf("https://github.com/open-policy-agent/conftest/releases/download/v0.25.0/conftest_0.25.0_%s_x86_64.tar.gz?checksum=file:https://github.com/open-policy-agent/conftest/releases/download/v0.25.0/checksums.txt", cases.Title(language.English).String(runtime.GOOS)) + platform := getPlatform() + fullURL := fmt.Sprintf("https://github.com/open-policy-agent/conftest/releases/download/v0.25.0/conftest_0.25.0_%s.tar.gz?checksum=file:https://github.com/open-policy-agent/conftest/releases/download/v0.25.0/checksums.txt", platform) RegisterMockTestingT(t)