-
Notifications
You must be signed in to change notification settings - Fork 26
/
sbom_format.go
108 lines (95 loc) · 2.7 KB
/
sbom_format.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package sbom
import (
"fmt"
"mime"
"github.com/anchore/syft/syft"
"github.com/anchore/syft/syft/sbom"
"github.com/paketo-buildpacks/packit/v2/sbom/internal/formats/cyclonedx13"
"github.com/paketo-buildpacks/packit/v2/sbom/internal/formats/spdx22"
"github.com/paketo-buildpacks/packit/v2/sbom/internal/formats/syft2"
"github.com/paketo-buildpacks/packit/v2/sbom/internal/formats/syft301"
)
// TODO: refactor the version lookup part
var syftFormats map[string]sbom.FormatID = map[string]sbom.FormatID{
"default": syft301.ID,
"3.0.1": syft301.ID,
"2.0.2": syft2.ID,
}
var cyclonedxFormats map[string]sbom.FormatID = map[string]sbom.FormatID{
"default": cyclonedx13.ID,
"1.4": syft.CycloneDxJSONFormatID,
"1.3": cyclonedx13.ID,
}
var spdxFormats map[string]sbom.FormatID = map[string]sbom.FormatID{
"default": spdx22.ID,
"2.2": spdx22.ID,
}
var additionalFormats []sbomFormat
func init() {
additionalFormats = []sbomFormat{
newSBOMFormat(cyclonedx13.Format()),
newSBOMFormat(syft2.Format()),
newSBOMFormat(syft301.Format()),
newSBOMFormat(spdx22.Format()),
}
}
// An experimental type added to support more SBOM formats
// It extends the Syft sbom.Format interface
type sbomFormat struct {
sbom.Format
}
func newSBOMFormat(format sbom.Format) sbomFormat {
return sbomFormat{
Format: format,
}
}
func (f sbomFormat) Extension() string {
switch f.ID() {
case syft.CycloneDxJSONFormatID, cyclonedx13.ID:
return "cdx.json"
case syft.SPDXJSONFormatID, spdx22.ID:
return "spdx.json"
case syft.JSONFormatID, syft2.ID, syft301.ID:
return "syft.json"
default:
return ""
}
}
func sbomFormatByMediaType(mediaType string) (sbomFormat, error) {
baseType, params, err := mime.ParseMediaType(mediaType)
if err != nil {
return sbomFormat{}, fmt.Errorf("failed to parse SBOM media type: %w", err)
}
// TODO: semver version parsing?
version, ok := params["version"]
if !ok {
version = "default"
}
var selected sbom.FormatID
switch baseType {
case CycloneDXFormat:
selected = cyclonedxFormats[version]
case SPDXFormat:
selected = spdxFormats[version]
case SyftFormat:
selected = syftFormats[version]
default:
return sbomFormat{}, fmt.Errorf("unsupported SBOM format: '%s'", mediaType)
}
if selected == sbom.FormatID("") {
return sbomFormat{}, fmt.Errorf("version '%s' is not supported for SBOM format '%s'", version, baseType)
}
return sbomFormatByID(selected)
}
func sbomFormatByID(id sbom.FormatID) (sbomFormat, error) {
for _, f := range additionalFormats {
if f.ID() == id {
return f, nil
}
}
format := syft.FormatByID(id)
if format == nil {
return sbomFormat{}, fmt.Errorf("'%s' is not a valid SBOM format identifier", id)
}
return newSBOMFormat(format), nil
}