-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathspdx.go
More file actions
207 lines (188 loc) · 6.23 KB
/
Copy pathspdx.go
File metadata and controls
207 lines (188 loc) · 6.23 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package sbom
import (
"encoding/json"
"strings"
)
type spdxDoc struct {
SPDXVersion string `json:"spdxVersion"`
SPDXID string `json:"SPDXID"`
Name string `json:"name"`
DataLicense string `json:"dataLicense"`
DocumentNamespace string `json:"documentNamespace"`
CreationInfo *spdxCreationInfo `json:"creationInfo"`
Packages []spdxPackage `json:"packages"`
Relationships []spdxRelationship `json:"relationships,omitempty"`
ExtractedLicensingInfos []spdxExtractedLicenseInfo `json:"hasExtractedLicensingInfos,omitempty"`
}
type spdxEnvelope struct {
SBOM json.RawMessage `json:"sbom"`
Predicate json.RawMessage `json:"predicate"`
PredicateType string `json:"predicateType"`
}
type spdxExtractedLicenseInfo struct {
LicenseID string `json:"licenseId"`
ExtractedText string `json:"extractedText"`
Name string `json:"name,omitempty"`
}
type spdxCreationInfo struct {
Created string `json:"created"`
Creators []string `json:"creators"`
LicenseListVersion string `json:"licenseListVersion"`
}
type spdxPackage struct {
SPDXID string `json:"SPDXID"`
Name string `json:"name"`
VersionInfo string `json:"versionInfo,omitempty"`
DownloadLocation string `json:"downloadLocation"`
Homepage string `json:"homepage,omitempty"`
PackageFileName string `json:"packageFileName,omitempty"`
LicenseConcluded string `json:"licenseConcluded,omitempty"`
LicenseDeclared string `json:"licenseDeclared,omitempty"`
CopyrightText string `json:"copyrightText,omitempty"`
Description string `json:"description,omitempty"`
Supplier string `json:"supplier,omitempty"`
Originator string `json:"originator,omitempty"`
PrimaryPackagePurpose string `json:"primaryPackagePurpose,omitempty"`
Checksums []spdxChecksum `json:"checksums,omitempty"`
ExternalRefs []spdxExtRef `json:"externalRefs,omitempty"`
}
type spdxChecksum struct {
Algorithm string `json:"algorithm"`
Value string `json:"checksumValue"`
}
type spdxExtRef struct {
Category string `json:"referenceCategory"`
Type string `json:"referenceType"`
Locator string `json:"referenceLocator"`
}
type spdxRelationship struct {
SPDXElementID string `json:"spdxElementId"`
RelationshipType string `json:"relationshipType"`
RelatedSPDXElement string `json:"relatedSpdxElement"`
}
const maxEnvelopeDepth = 3
func parseSPDX(data []byte, envelope bool) (*SBOM, error) {
var err error
data, err = unwrapSPDXEnvelope(data, envelope)
if err != nil {
return nil, err
}
var doc spdxDoc
if err := json.Unmarshal(data, &doc); err != nil {
return nil, wrapErr("spdx json", err)
}
if doc.SPDXVersion == "" && doc.SPDXID == "" {
return nil, ErrUnrecognized
}
s := newSizedSBOM(TypeSPDX, len(doc.Packages), len(doc.Relationships))
s.SpecVersion = doc.SPDXVersion
s.Document = Document{
Name: doc.Name,
ID: doc.SPDXID,
Type: TypeSPDX,
SpecVersion: doc.SPDXVersion,
DataLicense: doc.DataLicense,
Namespace: doc.DocumentNamespace,
}
applySPDXCreationInfo(s, doc.CreationInfo)
var elements map[string]string
if len(doc.Relationships) > 0 {
elements = make(map[string]string, len(doc.Packages)+1)
elements[doc.SPDXID] = doc.Name
}
for i := range doc.Packages {
sp := &doc.Packages[i]
p := Package{
ID: sp.SPDXID,
Name: sp.Name,
Version: sp.VersionInfo,
Type: normalizePackageType(sp.PrimaryPackagePurpose),
Description: sp.Description,
Homepage: sp.Homepage,
DownloadLocation: sp.DownloadLocation,
Filename: sp.PackageFileName,
LicenseConcluded: sp.LicenseConcluded,
LicenseDeclared: sp.LicenseDeclared,
Copyright: sp.CopyrightText,
}
if sp.Supplier != "" {
p.SupplierType, p.Supplier = splitColon(sp.Supplier)
}
if sp.Originator != "" {
p.OriginatorType, p.Originator = splitColon(sp.Originator)
}
if len(sp.Checksums) > 0 {
p.Checksums = make([]Checksum, len(sp.Checksums))
for i := range sp.Checksums {
p.Checksums[i] = Checksum(sp.Checksums[i])
}
}
if len(sp.ExternalRefs) > 0 {
p.ExternalRefs = make([]ExternalRef, len(sp.ExternalRefs))
for i := range sp.ExternalRefs {
p.ExternalRefs[i] = ExternalRef(sp.ExternalRefs[i])
}
}
if elements != nil {
elements[sp.SPDXID] = sp.Name
}
s.addPackage(p)
}
for i := range doc.Relationships {
r := &doc.Relationships[i]
s.Relationships = append(s.Relationships, Relationship{
SourceID: r.SPDXElementID,
Source: elements[r.SPDXElementID],
TargetID: r.RelatedSPDXElement,
Target: elements[r.RelatedSPDXElement],
Type: r.RelationshipType,
})
}
return s, nil
}
func applySPDXCreationInfo(s *SBOM, creationInfo *spdxCreationInfo) {
if creationInfo == nil {
return
}
s.Document.Created = creationInfo.Created
for _, c := range creationInfo.Creators {
typ, name := splitColon(c)
if typ == SupplierOrganization {
s.Document.Supplier = name
} else {
if s.Document.Creators == nil {
s.Document.Creators = make([]Creator, 0, len(creationInfo.Creators))
}
s.Document.Creators = append(s.Document.Creators, Creator{Type: typ, Name: name})
}
}
}
func unwrapSPDXEnvelope(data []byte, envelope bool) ([]byte, error) {
for range maxEnvelopeDepth - 1 {
if !envelope {
break
}
var outer spdxEnvelope
if err := json.Unmarshal(data, &outer); err != nil {
return nil, wrapErr("spdx json", err)
}
if len(outer.SBOM) > 0 {
data = outer.SBOM
envelope = detect(data).spdxEnvelope
continue
}
if strings.Contains(outer.PredicateType, "spdx") && len(outer.Predicate) > 0 {
data = outer.Predicate
envelope = detect(data).spdxEnvelope
continue
}
break
}
return data, nil
}
func splitColon(s string) (typ, name string) {
if i := strings.Index(s, ": "); i >= 0 {
return s[:i], s[i+2:]
}
return "", s
}