|
| 1 | +package schema |
| 2 | + |
| 3 | +import ( |
| 4 | + "bufio" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "log" |
| 10 | + "net/http" |
| 11 | + "strings" |
| 12 | +) |
| 13 | + |
| 14 | +// ProviderChecksums holds the SHA256 checksums for the Databricks Terraform |
| 15 | +// provider archive for supported Linux architectures. |
| 16 | +type ProviderChecksums struct { |
| 17 | + LinuxAmd64 string |
| 18 | + LinuxArm64 string |
| 19 | +} |
| 20 | + |
| 21 | +// FetchProviderChecksums downloads the SHA256SUMS file from the GitHub release |
| 22 | +// for the given provider version and extracts checksums for the linux_amd64 and |
| 23 | +// linux_arm64 archives. It also downloads both zips to verify that the parsed |
| 24 | +// checksums are correct. |
| 25 | +// https://github.com/databricks/terraform-provider-databricks/releases |
| 26 | +func FetchProviderChecksums(version string) (*ProviderChecksums, error) { |
| 27 | + url := fmt.Sprintf( |
| 28 | + "https://github.com/databricks/terraform-provider-databricks/releases/download/v%s/terraform-provider-databricks_%s_SHA256SUMS", |
| 29 | + version, version, |
| 30 | + ) |
| 31 | + |
| 32 | + resp, err := http.Get(url) |
| 33 | + if err != nil { |
| 34 | + return nil, fmt.Errorf("downloading SHA256SUMS for provider v%s: %w", version, err) |
| 35 | + } |
| 36 | + defer resp.Body.Close() |
| 37 | + |
| 38 | + if resp.StatusCode != http.StatusOK { |
| 39 | + return nil, fmt.Errorf("downloading SHA256SUMS for provider v%s: HTTP %s", version, resp.Status) |
| 40 | + } |
| 41 | + |
| 42 | + checksums := &ProviderChecksums{} |
| 43 | + amd64Suffix := fmt.Sprintf("terraform-provider-databricks_%s_linux_amd64.zip", version) |
| 44 | + arm64Suffix := fmt.Sprintf("terraform-provider-databricks_%s_linux_arm64.zip", version) |
| 45 | + |
| 46 | + scanner := bufio.NewScanner(resp.Body) |
| 47 | + for scanner.Scan() { |
| 48 | + line := scanner.Text() |
| 49 | + parts := strings.Fields(line) |
| 50 | + if len(parts) != 2 { |
| 51 | + continue |
| 52 | + } |
| 53 | + switch parts[1] { |
| 54 | + case amd64Suffix: |
| 55 | + checksums.LinuxAmd64 = parts[0] |
| 56 | + case arm64Suffix: |
| 57 | + checksums.LinuxArm64 = parts[0] |
| 58 | + } |
| 59 | + } |
| 60 | + if err := scanner.Err(); err != nil { |
| 61 | + return nil, fmt.Errorf("reading SHA256SUMS for provider v%s: %w", version, err) |
| 62 | + } |
| 63 | + |
| 64 | + if checksums.LinuxAmd64 == "" { |
| 65 | + return nil, fmt.Errorf("checksum not found for %s in SHA256SUMS", amd64Suffix) |
| 66 | + } |
| 67 | + if checksums.LinuxArm64 == "" { |
| 68 | + return nil, fmt.Errorf("checksum not found for %s in SHA256SUMS", arm64Suffix) |
| 69 | + } |
| 70 | + |
| 71 | + // Sanity check: download both zips and verify the checksums match. |
| 72 | + err = verifyProviderChecksum(version, "linux_amd64", checksums.LinuxAmd64) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + err = verifyProviderChecksum(version, "linux_arm64", checksums.LinuxArm64) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + |
| 81 | + return checksums, nil |
| 82 | +} |
| 83 | + |
| 84 | +// verifyProviderChecksum downloads the provider zip for the given platform and |
| 85 | +// verifies it matches the expected SHA256 checksum. |
| 86 | +func verifyProviderChecksum(version, platform, expectedChecksum string) error { |
| 87 | + url := fmt.Sprintf( |
| 88 | + "https://github.com/databricks/terraform-provider-databricks/releases/download/v%s/terraform-provider-databricks_%s_%s.zip", |
| 89 | + version, version, platform, |
| 90 | + ) |
| 91 | + |
| 92 | + log.Printf("verifying checksum for %s provider archive", platform) |
| 93 | + resp, err := http.Get(url) |
| 94 | + if err != nil { |
| 95 | + return fmt.Errorf("downloading provider archive for checksum verification: %w", err) |
| 96 | + } |
| 97 | + defer resp.Body.Close() |
| 98 | + |
| 99 | + if resp.StatusCode != http.StatusOK { |
| 100 | + return fmt.Errorf("downloading provider archive for checksum verification: HTTP %s", resp.Status) |
| 101 | + } |
| 102 | + |
| 103 | + hash := sha256.New() |
| 104 | + if _, err := io.Copy(hash, resp.Body); err != nil { |
| 105 | + return fmt.Errorf("computing checksum for provider archive: %w", err) |
| 106 | + } |
| 107 | + |
| 108 | + actualChecksum := hex.EncodeToString(hash.Sum(nil)) |
| 109 | + if actualChecksum != expectedChecksum { |
| 110 | + return fmt.Errorf("checksum mismatch for %s provider archive: expected %s, got %s", platform, expectedChecksum, actualChecksum) |
| 111 | + } |
| 112 | + |
| 113 | + log.Printf("checksum verified for %s provider archive", platform) |
| 114 | + return nil |
| 115 | +} |
0 commit comments