diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..73536140 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +* text=auto +* text eol=lf diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 00000000..706072d7 --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,30 @@ +# This workflow will build a golang project +# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go + +name: Go + +on: + push: + branches: [ '*' ] + +jobs: + + integration_tests: + strategy: + matrix: + os: [ubuntu-latest, windows-latest] + go_version: ['1.22'] + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go_version }} + + - name: Build + run: go build -v ./... + + - name: Test + run: go test -v ./... diff --git a/CHANGELOG.md b/CHANGELOG.md index 5128ce00..aa7003fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased +### Added +- added verification of the checksums for downloaded terraform files. + ## [0.13.1201] - 2021-11-28 ### Bug fixes - No matter what users pass to --bin or -b, the local binary is called terraform. User can resume old behavior where -b is custom diff --git a/README.md b/README.md index 0ce94a1f..1639f3fe 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ The `tfswitch` command line tool lets you switch between different versions of [terraform](https://www.terraform.io/). -If you do not have a particular version of terraform installed, `tfswitch` will download the version you desire. +If you do not have a particular version of terraform installed, `tfswitch` will download and verify the version you desire. The installation is minimal and easy. Once installed, simply select the version you require from the dropdown and start using terraform. diff --git a/go.mod b/go.mod index 804ba6fd..18bc0d89 100644 --- a/go.mod +++ b/go.mod @@ -10,6 +10,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 github.com/pborman/getopt v1.1.0 github.com/spf13/viper v1.18.2 + golang.org/x/crypto v0.21.0 golang.org/x/sys v0.18.0 ) diff --git a/go.sum b/go.sum index 28dba11a..c6f9d6c9 100644 --- a/go.sum +++ b/go.sum @@ -25,20 +25,22 @@ github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3a github.com/golang/protobuf v1.1.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/hashicorp/errwrap v0.0.0-20180715044906-d6c0cd880357/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= github.com/hashicorp/go-multierror v0.0.0-20180717150148-3d5d8f294aa0/go.mod h1:JMRHfdO9jKNzS/+BTlxCjKNQHg/jZAft8U7LloJvN7I= github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek= github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA= +github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f/go.mod h1:oZtUIOe8dh44I2q6ScRibXws4Ajl+d+nod3AaR9vL5w= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/hcl/v2 v2.0.0 h1:efQznTz+ydmQXq3BOnRa3AXzvCeTq1P4dKj/z5GLlY8= github.com/hashicorp/hcl/v2 v2.0.0/go.mod h1:oVVDG71tEinNGYCxinCYadcmKU9bglqW9pV3txagJ90= github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80 h1:PFfGModn55JA0oBsvFghhj0v93me+Ctr3uHC/UmFAls= github.com/hashicorp/hcl2 v0.0.0-20191002203319-fb75b3253c80/go.mod h1:Cxv+IJLuBiEhQ7pBYGEuORa0nr4U994pE8mYLuFd7v0= -github.com/hashicorp/terraform-config-inspect v0.0.0-20231204233900-a34142ec2a72 h1:nZ5gGjbe5o7XUu1d7j+Y5Ztcxlp+yaumTKH9i0D3wlg= -github.com/hashicorp/terraform-config-inspect v0.0.0-20231204233900-a34142ec2a72/go.mod h1:l8HcFPm9cQh6Q0KSWoYPiePqMvRFenybP1CH2MjKdlg= +github.com/hashicorp/terraform-config-inspect v0.0.0-20211115214459-90acf1ca460f h1:R8UIC07Ha9jZYkdcJ51l4ownCB8xYwfJtrgZSMvqjWI= +github.com/hashicorp/terraform-config-inspect v0.0.0-20211115214459-90acf1ca460f/go.mod h1:Z0Nnk4+3Cy89smEbrq+sl1bxc9198gIP4I7wcQF6Kqs= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= @@ -87,6 +89,7 @@ github.com/spf13/afero v1.11.0/go.mod h1:GH9Y3pIexgf1MTIWtNGyogA5MwRIDXGUr+hbWNo github.com/spf13/cast v1.6.0 h1:GEiTHELF+vaR5dhz3VqZfFSzZjYbgeKDpBxQVS4GYJ0= github.com/spf13/cast v1.6.0/go.mod h1:ancEpBxwJDODSW/UG4rDrAqiKolqNNh2DX3mk86cAdo= github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= @@ -112,6 +115,8 @@ go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI= go.uber.org/multierr v1.9.0/go.mod h1:X2jQV1h+kxSjClGpnseKVIxpmcjrj7MNnI0bnlfKTVQ= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g= golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k= golang.org/x/net v0.0.0-20180811021610-c39426892332/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= diff --git a/lib/checksum.go b/lib/checksum.go new file mode 100644 index 00000000..b86b9612 --- /dev/null +++ b/lib/checksum.go @@ -0,0 +1,73 @@ +package lib + +import ( + "bufio" + "crypto/sha256" + "encoding/hex" + "fmt" + "golang.org/x/crypto/openpgp" + "io" + "log" + "os" + "path/filepath" + "strings" +) + +// getChecksumFromFile Extract the checksum from the signature file +func getChecksumFromHashFile(signatureFilePath string, terraformFileName string) (string, error) { + readFile, err := os.Open(signatureFilePath) + if err != nil { + fmt.Println("[Error]: Could not open ", signatureFilePath) + return "", err + } + defer readFile.Close() + + scanner := bufio.NewScanner(readFile) + scanner.Split(bufio.ScanLines) + for scanner.Scan() { + split := strings.Split(scanner.Text(), " ") + if split[1] == terraformFileName { + return split[0], nil + } + } + return "", nil +} + +// checkChecksumMatches This will calculate and compare the check sum of the downloaded zip file. +func checkChecksumMatches(hashFile string, targetFile *os.File) bool { + _, fileName := filepath.Split(targetFile.Name()) + expectedChecksum, err := getChecksumFromHashFile(hashFile, fileName) + if err != nil { + fmt.Println("[Error]: could not get expected checksum from file: " + err.Error()) + return false + } + hash := sha256.New() + if _, err := io.Copy(hash, targetFile); err != nil { + fmt.Println("[Error]: Calculating Checksum failed: " + err.Error()) + return false + } + checksum := hex.EncodeToString(hash.Sum(nil)) + if expectedChecksum != checksum { + fmt.Println("[Error]: Checksum mismatch. Expected: ", expectedChecksum, " got ", checksum) + return false + } + return true +} + +// checkSignatureOfChecksums THis will verify the signature of the file containing the hash sums +func checkSignatureOfChecksums(keyRingReader *os.File, hashFile *os.File, signatureFile *os.File) bool { + log.Println("Verifying signature of checksum file...") + keyring, err := openpgp.ReadArmoredKeyRing(keyRingReader) + if err != nil { + log.Fatal("[Error]: Read armored key ring: " + err.Error()) + return false + } + + _, err = openpgp.CheckDetachedSignature(keyring, hashFile, signatureFile) + if err != nil { + log.Fatal("[Error]: Checking detached signature: " + err.Error()) + return false + } + log.Println("Verification successful.") + return true +} diff --git a/lib/checksum_test.go b/lib/checksum_test.go new file mode 100644 index 00000000..cb68df06 --- /dev/null +++ b/lib/checksum_test.go @@ -0,0 +1,29 @@ +package lib + +import ( + "os" + "testing" +) + +func Test_getChecksumFromHashFile(t *testing.T) { + expected := "3ff056b5e8259003f67fd0f0ed7229499cfb0b41f3ff55cc184088589994f7a5" + got, err := getChecksumFromHashFile("../test-data/terraform_1.7.5_SHA256SUMS", "terraform_1.7.5_linux_amd64.zip") + if err != nil { + t.Errorf("getChecksumFromHashFile() error = %v", err) + return + } + if got != expected { + t.Errorf("getChecksumFromHashFile() got = %v, expected %v", got, expected) + } +} + +func Test_checkChecksumMatches(t *testing.T) { + targetFile, err := os.Open("../test-data/checksum-check-file") + if err != nil { + t.Errorf("[Error]: Could not open testfile for signature verification.") + } + + if got := checkChecksumMatches("../test-data/terraform_1.7.5_SHA256SUMS", targetFile); got != true { + t.Errorf("checkChecksumMatches() = %v, want %v", got, true) + } +} diff --git a/lib/download.go b/lib/download.go index 348373f1..0a3b918b 100644 --- a/lib/download.go +++ b/lib/download.go @@ -1,23 +1,99 @@ package lib import ( + "errors" "fmt" "io" + "log" "net/http" "os" "path/filepath" "strings" ) -// DownloadFromURL : Downloads the binary from the source url -func DownloadFromURL(installLocation string, url string) (string, error) { +const ( + pubKeyId = "72D7468F" + pubKeyPrefix = "hashicorp_" + pubKeySuffix = ".asc" + pubKeyUri = "https://www.hashicorp.com/.well-known/pgp-key.txt" +) + +// DownloadFromURL : Downloads the terraform binary and its hash from the source url +func DownloadFromURL(installLocation string, mirrorURL string, tfversion string, versionPrefix string, goos string, goarch string) (string, error) { + pubKeyFilename := filepath.Join(installLocation, "/", pubKeyPrefix+pubKeyId+pubKeySuffix) + zipUrl := mirrorURL + tfversion + "/" + versionPrefix + tfversion + "_" + goos + "_" + goarch + ".zip" + hashUrl := mirrorURL + tfversion + "/" + versionPrefix + tfversion + "_SHA256SUMS" + hashSignatureUrl := mirrorURL + tfversion + "/" + versionPrefix + tfversion + "_SHA256SUMS." + pubKeyId + ".sig" + + err := downloadPublicKey(installLocation, pubKeyFilename) + if err != nil { + log.Fatal("[Error]: Could not download public key file") + return "", err + } + + log.Println("Downloading ", zipUrl) + zipFilePath, err := downloadFromURL(installLocation, zipUrl) + if err != nil { + log.Fatal("[Error]: Could not download zip file") + return "", err + } + + log.Println("Downloading ", hashUrl) + hashFilePath, err := downloadFromURL(installLocation, hashUrl) + if err != nil { + log.Fatal("[Error]: Could not download hash file") + return "", err + } + + log.Println("Downloading ", hashSignatureUrl) + hashSigFilePath, err := downloadFromURL(installLocation, hashSignatureUrl) + if err != nil { + log.Fatal("[Error]: Could not download hash signature file") + return "", err + } + + publicKeyFile, err := os.Open(pubKeyFilename) + if err != nil { + log.Fatal("[Error]: Could not open the public key") + return "", err + } + + signatureFile, err := os.Open(hashSigFilePath) + if err != nil { + log.Fatal("[Error]: Could not open the public key") + return "", err + } + + targetFile, err := os.Open(zipFilePath) + if err != nil { + log.Fatal("[Error]: Could not open the terraform binary for signature verification.") + return "", err + } + + hashFile, err := os.Open(hashFilePath) + if err != nil { + log.Fatal("[Error]: Could not open the terraform binary for signature verification.") + return "", err + } + verified := checkSignatureOfChecksums(publicKeyFile, hashFile, signatureFile) + if !verified { + return "", errors.New("signature of checksum files could not be verified") + } + match := checkChecksumMatches(hashFilePath, targetFile) + if !match { + return "", errors.New("checksums did not match") + } + return zipFilePath, err +} + +func downloadFromURL(installLocation string, url string) (string, error) { tokens := strings.Split(url, "/") fileName := tokens[len(tokens)-1] - fmt.Printf("Downloading to: %s\n", installLocation) + log.Printf("Downloading to: %s\n", filepath.Join(installLocation, "/", fileName)) response, err := http.Get(url) if err != nil { - fmt.Println("[Error] : Error while downloading", url, "-", err) + log.Fatal("[Error] : Error while downloading", url, "-", err) return "", err } defer response.Body.Close() @@ -25,23 +101,42 @@ func DownloadFromURL(installLocation string, url string) (string, error) { if response.StatusCode != 200 { //Sometimes hashicorp terraform file names are not consistent //For example 0.12.0-alpha4 naming convention in the release repo is not consistent - return "", fmt.Errorf("[Error] : Unable to download from %s", url) + log.Fatalf("[Error] : Unable to download from %s", url) } zipFile := filepath.Join(installLocation, fileName) output, err := os.Create(zipFile) if err != nil { - fmt.Println("[Error] : Error while creating", zipFile, "-", err) + log.Fatal("[Error] : Error while creating", zipFile, "-", err) return "", err } defer output.Close() n, err := io.Copy(output, response.Body) if err != nil { - fmt.Println("[Error] : Error while downloading", url, "-", err) + log.Fatal("[Error] : Error while writing file", url, "-", err) return "", err } - fmt.Println(n, "bytes downloaded") + log.Println(n, "bytes downloaded") return zipFile, nil } + +func downloadPublicKey(installLocation string, targetFileName string) error { + fmt.Println("Looking up public key file at ", targetFileName) + publicKeyFileExists := FileExists(targetFileName) + if !publicKeyFileExists { + // Public key does not exist. Let's grab it from hashicorp + pubKeyFile, errDl := downloadFromURL(installLocation, pubKeyUri) + if errDl != nil { + log.Fatal("[Error]: Error while fetching the public key file from ", pubKeyUri) + return errDl + } + errRename := os.Rename(pubKeyFile, targetFileName) + if errRename != nil { + log.Fatal("[Error]: Error while renaming the public key file from ", pubKeyFile, " to ", targetFileName) + return errRename + } + } + return nil +} diff --git a/lib/download_test.go b/lib/download_test.go index 4f133eb2..2d0fa039 100644 --- a/lib/download_test.go +++ b/lib/download_test.go @@ -22,9 +22,10 @@ func TestDownloadFromURL_FileNameMatch(t *testing.T) { installPath := fmt.Sprintf(tempDir + string(os.PathSeparator) + ".terraform.versions_test") macOS := "_darwin_amd64.zip" - home, err := homedir.Dir() - if err != nil { - log.Fatalf("Could not detect home directory.") + // get current user + home, errCurr := homedir.Dir() + if errCurr != nil { + log.Fatal(errCurr) } fmt.Printf("Current user homedir: %v \n", home) diff --git a/lib/install.go b/lib/install.go index 1960339e..c78c7664 100644 --- a/lib/install.go +++ b/lib/install.go @@ -126,8 +126,7 @@ func Install(tfversion string, binPath string, mirrorURL string) { /* if selected version already exist, */ /* proceed to download it from the hashicorp release page */ - url := mirrorURL + tfversion + "/" + versionPrefix + tfversion + "_" + goos + "_" + goarch + ".zip" - zipFile, errDownload := DownloadFromURL(installLocation, url) + zipFile, errDownload := DownloadFromURL(installLocation, mirrorURL, tfversion, versionPrefix, goos, goarch) /* If unable to download file from url, exit(1) immediately */ if errDownload != nil { diff --git a/lib/utils.go b/lib/utils.go new file mode 100644 index 00000000..87212ec0 --- /dev/null +++ b/lib/utils.go @@ -0,0 +1,12 @@ +package lib + +import "os" + +// fileExists checks if a file exists and is not a directory before we try using it to prevent further errors. +func FileExists(filename string) bool { + info, err := os.Stat(filename) + if os.IsNotExist(err) { + return false + } + return !info.IsDir() +} diff --git a/main.go b/main.go index cf31d027..67e3c39c 100644 --- a/main.go +++ b/main.go @@ -19,7 +19,6 @@ package main import ( "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -96,10 +95,10 @@ func main() { * If you provide a custom binary path with the -b option, this will override the bin value in the toml file * If you provide a version on the command line, this will override the version value in the toml file */ - case fileExists(TOMLConfigFile) || fileExists(HomeTOMLConfigFile): + case lib.FileExists(TOMLConfigFile) || lib.FileExists(HomeTOMLConfigFile): version := "" binPath := *custBinPath - if fileExists(TOMLConfigFile) { //read from toml from current directory + if lib.FileExists(TOMLConfigFile) { //read from toml from current directory version, binPath = getParamsTOML(binPath, *chDirPath) } else { // else read from toml from home directory version, binPath = getParamsTOML(binPath, homedir) @@ -126,12 +125,12 @@ func main() { case len(args) == 1: installVersion(args[0], &binPath, mirrorURL) /* provide an tfswitchrc file (IN ADDITION TO A TOML FILE) */ - case fileExists(RCFile) && len(args) == 0: + case lib.FileExists(RCFile) && len(args) == 0: readingFileMsg(rcFilename) tfversion := retrieveFileContents(RCFile) installVersion(tfversion, &binPath, mirrorURL) /* if .terraform-version file found (IN ADDITION TO A TOML FILE) */ - case fileExists(TFVersionFile) && len(args) == 0: + case lib.FileExists(TFVersionFile) && len(args) == 0: readingFileMsg(tfvFilename) tfversion := retrieveFileContents(TFVersionFile) installVersion(tfversion, &binPath, mirrorURL) @@ -144,7 +143,7 @@ func main() { fmt.Printf("Terraform version environment variable: %s\n", tfversion) installVersion(tfversion, &binPath, mirrorURL) /* if terragrunt.hcl file found (IN ADDITION TO A TOML FILE) */ - case fileExists(TGHACLFile) && checkVersionDefinedHCL(&TGHACLFile) && len(args) == 0: + case lib.FileExists(TGHACLFile) && checkVersionDefinedHCL(&TGHACLFile) && len(args) == 0: installTGHclFile(&TGHACLFile, &binPath, mirrorURL) // if no arg is provided - but toml file is provided case version != "": @@ -191,13 +190,13 @@ func main() { installVersion(args[0], custBinPath, mirrorURL) /* provide an tfswitchrc file */ - case fileExists(RCFile) && len(args) == 0: + case lib.FileExists(RCFile) && len(args) == 0: readingFileMsg(rcFilename) tfversion := retrieveFileContents(RCFile) installVersion(tfversion, custBinPath, mirrorURL) /* if .terraform-version file found */ - case fileExists(TFVersionFile) && len(args) == 0: + case lib.FileExists(TFVersionFile) && len(args) == 0: readingFileMsg(tfvFilename) tfversion := retrieveFileContents(TFVersionFile) installVersion(tfversion, custBinPath, mirrorURL) @@ -207,7 +206,7 @@ func main() { installTFProvidedModule(*chDirPath, custBinPath, mirrorURL) /* if terragrunt.hcl file found */ - case fileExists(TGHACLFile) && checkVersionDefinedHCL(&TGHACLFile) && len(args) == 0: + case lib.FileExists(TGHACLFile) && checkVersionDefinedHCL(&TGHACLFile) && len(args) == 0: installTGHclFile(&TGHACLFile, custBinPath, mirrorURL) /* if Terraform Version environment variable is set */ @@ -315,7 +314,7 @@ func installVersion(arg string, custBinPath *string, mirrorURL *string) { // retrive file content of regular file func retrieveFileContents(file string) string { - fileContents, err := ioutil.ReadFile(file) + fileContents, err := os.ReadFile(file) if err != nil { fmt.Printf("Failed to read %s file. Follow the README.md instructions for setup. https://github.com/warrensbox/terraform-switcher/blob/master/README.md\n", tfvFilename) fmt.Printf("Error: %s\n", err) @@ -330,15 +329,6 @@ func readingFileMsg(filename string) { fmt.Printf("Reading file %s \n", filename) } -// fileExists checks if a file exists and is not a directory before we try using it to prevent further errors. -func fileExists(filename string) bool { - info, err := os.Stat(filename) - if os.IsNotExist(err) { - return false - } - return !info.IsDir() -} - // fileExists checks if a file exists and is not a directory before we // try using it to prevent further errors. func checkTFModuleFileExist(dir string) bool { diff --git a/test-data/checksum-check-file b/test-data/checksum-check-file new file mode 100644 index 00000000..35853f1a --- /dev/null +++ b/test-data/checksum-check-file @@ -0,0 +1,7 @@ +Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. + +Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. + +Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. + +Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. \ No newline at end of file diff --git a/test-data/terraform_1.7.5_SHA256SUMS b/test-data/terraform_1.7.5_SHA256SUMS new file mode 100644 index 00000000..1c6c594e --- /dev/null +++ b/test-data/terraform_1.7.5_SHA256SUMS @@ -0,0 +1,15 @@ +0eaf64e28f82e2defd06f7a6f3187d8cea03d5d9fcd2af54f549a6c32d6833f7 terraform_1.7.5_darwin_amd64.zip +99c4d4feafb0183af2f7fbe07beeea6f83e5f5a29ae29fee3168b6810e37ff98 terraform_1.7.5_darwin_arm64.zip +3885fd4ce29788e040cfb350db88c6c1f3e34491c3f89f7a36ff8476f6c03959 terraform_1.7.5_freebsd_386.zip +3846d408255336460f40d7f5eeb7e987936f0359c9f45d1eb659a09f3d8893ab terraform_1.7.5_freebsd_amd64.zip +b02aa14ecb4052482bf4e64fca7de7a743fce691511f96b4cd58610350886c9e terraform_1.7.5_freebsd_arm.zip +e188ce5b45c9d10fa9a5a118438add2eab056d16587f774ecab93e3ca39e1bca terraform_1.7.5_linux_386.zip +3ff056b5e8259003f67fd0f0ed7229499cfb0b41f3ff55cc184088589994f7a5 terraform_1.7.5_linux_amd64.zip +4e74db9394d5cdf0f91cf8fecd290216edf6cf06273eb8f55e35f26eac4a936a terraform_1.7.5_linux_arm.zip +08631c385667dd28f03b3a3f77cb980393af4a2fcfc2236c148a678ad9150c8c terraform_1.7.5_linux_arm64.zip +c0416b6b9fe0155bb3377e39a3f584b9f7b9a11a1236b9ea8cf7c074a804a513 terraform_1.7.5_openbsd_386.zip +3bd02023764365b7ae7ee3e597a6258e896049e3fd115896bc01113384864cba terraform_1.7.5_openbsd_amd64.zip +385af229bd76a058c221b9c0be56f02a7d0fa2535620040c9c895df00e0f09ee terraform_1.7.5_solaris_amd64.zip +2639c9444c6091fd5ad76f112040d592e99931489582ada4d485c12a64a79052 terraform_1.7.5_windows_386.zip +9b7be6ae159191ec1f4b5b9d27529ae5243e41020fb545c0041235bec8d92269 terraform_1.7.5_windows_amd64.zip +5df31bd6da3ed91eb3fb8e5a7616fd2f59edb488a110f6d43621ca2136037d9a checksum-check-file