Skip to content

Commit cf17652

Browse files
Move provider checksum verification from unit test to codegen sanity check
Instead of a separate test that downloads large archives on every test run, verify the checksum inline during codegen: FetchProviderChecksums now downloads the linux_amd64 zip and verifies it matches the parsed SHA256SUMS entry. This runs once during `go run .` (provider version bump) rather than on every `make test`. Co-authored-by: Isaac
1 parent cbbc9a3 commit cf17652

2 files changed

Lines changed: 45 additions & 15 deletions

File tree

bundle/deploy/terraform/pkg_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,6 @@ func TestTerraformArchiveChecksums(t *testing.T) {
5858
downloadAndChecksum(t, armUrl, tv.ChecksumLinuxArm64)
5959
}
6060

61-
func TestTerraformProviderArchiveChecksums(t *testing.T) {
62-
if testing.Short() {
63-
t.Skip("skipping slow test in short mode")
64-
}
65-
66-
metadata, err := NewTerraformMetadata(t.Context())
67-
require.NoError(t, err)
68-
69-
amdUrl := fmt.Sprintf("https://github.com/databricks/terraform-provider-databricks/releases/download/v%s/terraform-provider-databricks_%s_linux_amd64.zip", metadata.ProviderVersion, metadata.ProviderVersion)
70-
armUrl := fmt.Sprintf("https://github.com/databricks/terraform-provider-databricks/releases/download/v%s/terraform-provider-databricks_%s_linux_arm64.zip", metadata.ProviderVersion, metadata.ProviderVersion)
71-
72-
downloadAndChecksum(t, amdUrl, metadata.ProviderChecksum.LinuxAmd64)
73-
downloadAndChecksum(t, armUrl, metadata.ProviderChecksum.LinuxArm64)
74-
}
7561

7662
func TestGetTerraformVersionDefault(t *testing.T) {
7763
// Verify that the default version is used

bundle/internal/tf/codegen/schema/checksum.go

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ package schema
22

33
import (
44
"bufio"
5+
"crypto/sha256"
6+
"encoding/hex"
57
"fmt"
8+
"io"
9+
"log"
610
"net/http"
711
"strings"
812
)
@@ -16,7 +20,8 @@ type ProviderChecksums struct {
1620

1721
// FetchProviderChecksums downloads the SHA256SUMS file from the GitHub release
1822
// for the given provider version and extracts checksums for the linux_amd64 and
19-
// linux_arm64 archives.
23+
// linux_arm64 archives. It also downloads the linux_amd64 zip to verify that
24+
// the parsed checksum is correct.
2025
// https://github.com/databricks/terraform-provider-databricks/releases
2126
func FetchProviderChecksums(version string) (*ProviderChecksums, error) {
2227
url := fmt.Sprintf(
@@ -63,5 +68,44 @@ func FetchProviderChecksums(version string) (*ProviderChecksums, error) {
6368
return nil, fmt.Errorf("checksum not found for %s in SHA256SUMS", arm64Suffix)
6469
}
6570

71+
// Sanity check: download the linux_amd64 zip and verify the checksum matches.
72+
err = verifyProviderChecksum(version, "linux_amd64", checksums.LinuxAmd64)
73+
if err != nil {
74+
return nil, err
75+
}
76+
6677
return checksums, nil
6778
}
79+
80+
// verifyProviderChecksum downloads the provider zip for the given platform and
81+
// verifies it matches the expected SHA256 checksum.
82+
func verifyProviderChecksum(version, platform, expectedChecksum string) error {
83+
url := fmt.Sprintf(
84+
"https://github.com/databricks/terraform-provider-databricks/releases/download/v%s/terraform-provider-databricks_%s_%s.zip",
85+
version, version, platform,
86+
)
87+
88+
log.Printf("verifying checksum for %s provider archive", platform)
89+
resp, err := http.Get(url)
90+
if err != nil {
91+
return fmt.Errorf("downloading provider archive for checksum verification: %w", err)
92+
}
93+
defer resp.Body.Close()
94+
95+
if resp.StatusCode != http.StatusOK {
96+
return fmt.Errorf("downloading provider archive for checksum verification: HTTP %s", resp.Status)
97+
}
98+
99+
hash := sha256.New()
100+
if _, err := io.Copy(hash, resp.Body); err != nil {
101+
return fmt.Errorf("computing checksum for provider archive: %w", err)
102+
}
103+
104+
actualChecksum := hex.EncodeToString(hash.Sum(nil))
105+
if actualChecksum != expectedChecksum {
106+
return fmt.Errorf("checksum mismatch for %s provider archive: expected %s, got %s", platform, expectedChecksum, actualChecksum)
107+
}
108+
109+
log.Printf("checksum verified for %s provider archive", platform)
110+
return nil
111+
}

0 commit comments

Comments
 (0)