|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha256" |
| 5 | + "encoding/hex" |
| 6 | + "encoding/json" |
| 7 | + "flag" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "google.golang.org/protobuf/encoding/protojson" |
| 15 | + |
| 16 | + pb "github.com/ConductorOne/github-workflows/pb/artifacts/v1" |
| 17 | +) |
| 18 | + |
| 19 | +const ( |
| 20 | + // AttestationTypeInTotoV1 is the in-toto Statement v1 envelope type |
| 21 | + AttestationTypeInTotoV1 = "https://in-toto.io/Statement/v1" |
| 22 | + // PredicateTypeSLSAProvenanceV1 is the SLSA v1 provenance predicate type |
| 23 | + PredicateTypeSLSAProvenanceV1 = "https://slsa.dev/provenance/v1" |
| 24 | + // PredicateTypeSPDX is the SPDX SBOM predicate type |
| 25 | + PredicateTypeSPDX = "https://spdx.dev/Document" |
| 26 | +) |
| 27 | + |
| 28 | +func main() { |
| 29 | + var ( |
| 30 | + distDir string |
| 31 | + cdnBaseURL string |
| 32 | + s3Dir string |
| 33 | + ) |
| 34 | + flag.StringVar(&distDir, "dist-dir", "", "Path to the dist directory containing Windows artifacts") |
| 35 | + flag.StringVar(&cdnBaseURL, "cdn-base-url", "", "CDN base URL for artifact links") |
| 36 | + flag.StringVar(&s3Dir, "s3-directory", "", "S3 directory path for artifacts") |
| 37 | + flag.Parse() |
| 38 | + |
| 39 | + if distDir == "" || cdnBaseURL == "" || s3Dir == "" { |
| 40 | + fmt.Fprintf(os.Stderr, "generate-windows-manifest: error: all flags are required\n") |
| 41 | + fmt.Fprintf(os.Stderr, "Usage: generate-windows-manifest -dist-dir <path> -cdn-base-url <url> -s3-directory <dir>\n") |
| 42 | + os.Exit(1) |
| 43 | + } |
| 44 | + |
| 45 | + baseURL := fmt.Sprintf("%s/%s", cdnBaseURL, s3Dir) |
| 46 | + assets := make(map[string]*pb.Asset) |
| 47 | + |
| 48 | + // Find and process zip files |
| 49 | + zipFiles, err := filepath.Glob(filepath.Join(distDir, "*.zip")) |
| 50 | + if err != nil { |
| 51 | + fmt.Fprintf(os.Stderr, "generate-windows-manifest: error finding zip files: %v\n", err) |
| 52 | + os.Exit(1) |
| 53 | + } |
| 54 | + |
| 55 | + for _, zipPath := range zipFiles { |
| 56 | + filename := filepath.Base(zipPath) |
| 57 | + if strings.Contains(filename, "checksums") { |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + asset, err := buildAsset(zipPath, filename, "application/zip", baseURL, distDir) |
| 62 | + if err != nil { |
| 63 | + fmt.Fprintf(os.Stderr, "generate-windows-manifest: error processing %s: %v\n", filename, err) |
| 64 | + os.Exit(1) |
| 65 | + } |
| 66 | + |
| 67 | + // Windows zip uses key "windows-amd64" |
| 68 | + assets["windows-amd64"] = asset |
| 69 | + fmt.Fprintf(os.Stderr, "✅ Added zip asset: windows-amd64 -> %s\n", filename) |
| 70 | + } |
| 71 | + |
| 72 | + // Find and process MSI files (flattened to dist root by workflow) |
| 73 | + msiFiles, err := filepath.Glob(filepath.Join(distDir, "*.msi")) |
| 74 | + if err != nil { |
| 75 | + fmt.Fprintf(os.Stderr, "generate-windows-manifest: error finding MSI files: %v\n", err) |
| 76 | + os.Exit(1) |
| 77 | + } |
| 78 | + |
| 79 | + for _, msiPath := range msiFiles { |
| 80 | + filename := filepath.Base(msiPath) |
| 81 | + |
| 82 | + asset, err := buildAsset(msiPath, filename, "application/x-msi", baseURL, distDir) |
| 83 | + if err != nil { |
| 84 | + fmt.Fprintf(os.Stderr, "generate-windows-manifest: error processing %s: %v\n", filename, err) |
| 85 | + os.Exit(1) |
| 86 | + } |
| 87 | + |
| 88 | + // MSI uses key "windows-amd64-msi" |
| 89 | + // MSI has cosign signatures and attestations; Azure Trusted Signing (Windows code signing) planned for Stage 2 |
| 90 | + assets["windows-amd64-msi"] = asset |
| 91 | + fmt.Fprintf(os.Stderr, "✅ Added MSI asset: windows-amd64-msi -> %s\n", filename) |
| 92 | + } |
| 93 | + |
| 94 | + // Marshal assets map to JSON |
| 95 | + // We need to output a map[string]Asset JSON, not a full manifest |
| 96 | + output := make(map[string]json.RawMessage) |
| 97 | + marshalOpts := protojson.MarshalOptions{ |
| 98 | + EmitUnpopulated: true, |
| 99 | + } |
| 100 | + |
| 101 | + for key, asset := range assets { |
| 102 | + jsonBytes, err := marshalOpts.Marshal(asset) |
| 103 | + if err != nil { |
| 104 | + fmt.Fprintf(os.Stderr, "generate-windows-manifest: error marshaling asset %s: %v\n", key, err) |
| 105 | + os.Exit(1) |
| 106 | + } |
| 107 | + output[key] = jsonBytes |
| 108 | + } |
| 109 | + |
| 110 | + // Output JSON to stdout |
| 111 | + outputBytes, err := json.Marshal(output) |
| 112 | + if err != nil { |
| 113 | + fmt.Fprintf(os.Stderr, "generate-windows-manifest: error marshaling output: %v\n", err) |
| 114 | + os.Exit(1) |
| 115 | + } |
| 116 | + |
| 117 | + fmt.Println(string(outputBytes)) |
| 118 | + fmt.Fprintf(os.Stderr, "✅ Generated Windows manifest with %d assets\n", len(assets)) |
| 119 | +} |
| 120 | + |
| 121 | +func buildAsset(filePath, filename, mediaType, baseURL, distDir string) (*pb.Asset, error) { |
| 122 | + // Calculate SHA256 |
| 123 | + hash, err := sha256File(filePath) |
| 124 | + if err != nil { |
| 125 | + return nil, fmt.Errorf("calculating hash: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + // Get file size |
| 129 | + info, err := os.Stat(filePath) |
| 130 | + if err != nil { |
| 131 | + return nil, fmt.Errorf("getting file info: %w", err) |
| 132 | + } |
| 133 | + |
| 134 | + sizeBytes := info.Size() |
| 135 | + href := fmt.Sprintf("%s/%s", baseURL, filename) |
| 136 | + |
| 137 | + // Check for signature and certificate files (all in dist root after flatten step) |
| 138 | + var signatureHref, certificateHref *string |
| 139 | + sigPath := filepath.Join(distDir, filename+".sig") |
| 140 | + if _, err := os.Stat(sigPath); err == nil { |
| 141 | + s := fmt.Sprintf("%s/%s.sig", baseURL, filename) |
| 142 | + signatureHref = &s |
| 143 | + } |
| 144 | + certPath := filepath.Join(distDir, filename+".cert") |
| 145 | + if _, err := os.Stat(certPath); err == nil { |
| 146 | + c := fmt.Sprintf("%s/%s.cert", baseURL, filename) |
| 147 | + certificateHref = &c |
| 148 | + } |
| 149 | + |
| 150 | + // Build attestations array |
| 151 | + var attestations []*pb.AttestationDescriptor |
| 152 | + |
| 153 | + // Check for provenance attestation |
| 154 | + provenancePath := filepath.Join(distDir, filename+".provenance.sigstore.json") |
| 155 | + if _, err := os.Stat(provenancePath); err == nil { |
| 156 | + attestationType := AttestationTypeInTotoV1 |
| 157 | + predicateType := PredicateTypeSLSAProvenanceV1 |
| 158 | + bundleHref := fmt.Sprintf("%s/%s.provenance.sigstore.json", baseURL, filename) |
| 159 | + attestations = append(attestations, pb.AttestationDescriptor_builder{ |
| 160 | + AttestationType: &attestationType, |
| 161 | + PredicateType: &predicateType, |
| 162 | + BundleHref: &bundleHref, |
| 163 | + }.Build()) |
| 164 | + } |
| 165 | + |
| 166 | + // Check for SBOM attestation |
| 167 | + sbomPath := filepath.Join(distDir, filename+".sbom.sigstore.json") |
| 168 | + if _, err := os.Stat(sbomPath); err == nil { |
| 169 | + attestationType := AttestationTypeInTotoV1 |
| 170 | + predicateType := PredicateTypeSPDX |
| 171 | + bundleHref := fmt.Sprintf("%s/%s.sbom.sigstore.json", baseURL, filename) |
| 172 | + attestations = append(attestations, pb.AttestationDescriptor_builder{ |
| 173 | + AttestationType: &attestationType, |
| 174 | + PredicateType: &predicateType, |
| 175 | + BundleHref: &bundleHref, |
| 176 | + }.Build()) |
| 177 | + } |
| 178 | + |
| 179 | + return pb.Asset_builder{ |
| 180 | + Filename: &filename, |
| 181 | + MediaType: &mediaType, |
| 182 | + SizeBytes: &sizeBytes, |
| 183 | + Sha256: &hash, |
| 184 | + Href: &href, |
| 185 | + SignatureHref: signatureHref, |
| 186 | + CertificateHref: certificateHref, |
| 187 | + Attestations: attestations, |
| 188 | + }.Build(), nil |
| 189 | +} |
| 190 | + |
| 191 | +func sha256File(path string) (string, error) { |
| 192 | + f, err := os.Open(path) |
| 193 | + if err != nil { |
| 194 | + return "", err |
| 195 | + } |
| 196 | + defer f.Close() |
| 197 | + |
| 198 | + h := sha256.New() |
| 199 | + if _, err := io.Copy(h, f); err != nil { |
| 200 | + return "", err |
| 201 | + } |
| 202 | + |
| 203 | + return hex.EncodeToString(h.Sum(nil)), nil |
| 204 | +} |
0 commit comments