Skip to content

Commit

Permalink
fix: download conftest binary for correct arch (#4089)
Browse files Browse the repository at this point in the history
* download conftest binary for correct arch

Signed-off-by: Prajith P <prajithpalakkuda@gmail.com>

* remove default constant

Signed-off-by: Prajith P <prajithpalakkuda@gmail.com>

---------

Signed-off-by: Prajith P <prajithpalakkuda@gmail.com>
  • Loading branch information
Prajithp authored Dec 27, 2023
1 parent 6452a3c commit b7a06fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
27 changes: 23 additions & 4 deletions server/core/runtime/policy/conftest_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 ""
}
}
8 changes: 2 additions & 6 deletions server/core/runtime/policy/conftest_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"path/filepath"
"runtime"
"testing"

"github.com/hashicorp/go-version"
Expand All @@ -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)

Expand Down

0 comments on commit b7a06fa

Please sign in to comment.