Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalize the package hash to hex. #512

Merged
merged 1 commit into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions internal/sbom/spdx.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ package sbom

import (
"bytes"
"encoding/base64"
"encoding/hex"
"fmt"
"strings"
"text/template"
"time"
Expand Down Expand Up @@ -48,6 +51,16 @@ type tmplInfo struct {
// TODO: use k8s.io/release/pkg/bom
var tmpl = template.Must(template.New("").Funcs(template.FuncMap{
"dots": func(s string) string { return strings.ReplaceAll(s, "/", ".") },
"h1toSHA256": func(s string) (string, error) {
if !strings.HasPrefix(s, "h1:") {
return "", fmt.Errorf("malformed sum prefix: %q", s)
}
b, err := base64.StdEncoding.DecodeString(s[3:])
if err != nil {
return "", fmt.Errorf("malformed sum: %q: %w", s, err)
}
return hex.EncodeToString(b), nil
},
}).Parse(`SPDXVersion: SPDX-2.2
DataLicense: CC0-1.0
SPDXID: SPDXRef-DOCUMENT
Expand All @@ -71,10 +84,10 @@ PackageLicenseComments: NOASSERTION
PackageComment: NOASSERTION

Relationship: SPDXRef-DOCUMENT DESCRIBES SPDXRef-Package-{{ .BuildInfo.Main.Path | dots }}
{{ range .Deps }}
Relationship: SPDXRef-Package-{{ $.Main.Path | dots }} DEPENDS_ON SPDXRef-Package-{{ .Path | dots }}-{{ .Version }}{{ end }}

{{ range .Deps }}
Relationship: SPDXRef-Package-{{ $.Main.Path | dots }} DEPENDS_ON SPDXRef-Package-{{ .Path | dots }}-{{ .Version }}

##### Package representing {{ .Path }}

PackageName: {{ .Path }}
Expand All @@ -83,8 +96,8 @@ PackageVersion: {{ .Version }}
PackageSupplier: Organization: {{ .Path }}
PackageDownloadLocation: https://proxy.golang.org/{{ .Path }}/@v/{{ .Version }}.zip
FilesAnalyzed: false
PackageChecksum: SHA256: {{ .Sum }}
PackageLicenseConcluded: NOASSERTION
{{ if .Sum }}PackageChecksum: SHA256: {{ .Sum | h1toSHA256 }}
{{ end }}PackageLicenseConcluded: NOASSERTION
PackageLicenseDeclared: NOASSERTION
PackageCopyrightText: NOASSERTION
PackageLicenseComments: NOASSERTION
Expand Down
4 changes: 2 additions & 2 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,10 @@ func gobuildOptions(bo *options.BuildOptions) ([]build.Option, error) {
switch bo.SBOM {
case "none":
opts = append(opts, build.WithDisabledSBOM())
case "spdx":
opts = append(opts, build.WithSPDX(version()))
case "go.version-m":
opts = append(opts, build.WithGoVersionSBOM())
default: // "spdx"
opts = append(opts, build.WithSPDX(version()))
}
opts = append(opts, build.WithTrimpath(bo.Trimpath))
for _, lf := range bo.Labels {
Expand Down