@@ -129,28 +129,33 @@ func loadDocumentLicenses(bom *schema.BOM, policyConfig *schema.LicensePolicyCon
129
129
return
130
130
}
131
131
}
132
-
133
132
return
134
133
}
135
134
135
+ // Note: An actual error SHOULD ONLY be returned by the custom validation code.
136
+ func warnNoLicenseFound (bom * schema.BOM , location int ) {
137
+ message := fmt .Sprintf ("%s (%s)" ,
138
+ MSG_LICENSES_NOT_FOUND , // "licenses not found"
139
+ schema .GetLicenseChoiceLocationName (location ))
140
+ sbomError := NewInvalidSBOMError (bom , message , nil , nil )
141
+ getLogger ().Warning (sbomError )
142
+ }
143
+
144
+ func warnInvalidResourceLicense (resourceType string , bomRef string , name string , version string ) {
145
+ getLogger ().Warningf ("%s. resourceType: `%s`: bomRef: `%s`, name:`%s`, version: `%s`" ,
146
+ MSG_LICENSE_NOT_FOUND ,
147
+ resourceType , bomRef , name , version )
148
+ }
149
+
136
150
// Hash the license found in the (root).metadata.licenses[] array
137
151
func hashMetadataLicenses (bom * schema.BOM , policyConfig * schema.LicensePolicyConfig , location int , whereFilters []common.WhereFilter , licenseFlags utils.LicenseCommandFlags ) (err error ) {
138
152
getLogger ().Enter ()
139
153
defer getLogger ().Exit (err )
140
154
141
155
pLicenses := bom .GetCdxMetadataLicenses ()
156
+ // Issue a warning that the SBOM does not declare at least one, top-level component license.
142
157
if pLicenses == nil {
143
- sbomError := NewInvalidSBOMError (
144
- bom ,
145
- fmt .Sprintf ("%s (%s)" ,
146
- MSG_LICENSES_NOT_FOUND ,
147
- schema .GetLicenseChoiceLocationName (location )),
148
- nil , nil )
149
- // Issue a warning as an SBOM without at least one, top-level license
150
- // (in the metadata license summary) SHOULD be noted.
151
- // Note: An actual error SHOULD ONLY be returned by
152
- // the custom validation code.
153
- getLogger ().Warning (sbomError )
158
+ warnNoLicenseFound (bom , location )
154
159
return
155
160
}
156
161
@@ -168,7 +173,6 @@ func hashMetadataLicenses(bom *schema.BOM, policyConfig *schema.LicensePolicyCon
168
173
return
169
174
}
170
175
}
171
-
172
176
return
173
177
}
174
178
@@ -179,22 +183,10 @@ func hashMetadataComponentLicenses(bom *schema.BOM, policyConfig *schema.License
179
183
180
184
component := bom .GetCdxMetadataComponent ()
181
185
if component == nil {
182
- sbomError := NewInvalidSBOMError (
183
- bom ,
184
- fmt .Sprintf ("%s (%s)" ,
185
- MSG_LICENSES_NOT_FOUND ,
186
- schema .GetLicenseChoiceLocationName (location )),
187
- nil , nil )
188
- // Issue a warning as an SBOM without at least one
189
- // top-level component license declared SHOULD be noted.
190
- // Note: An actual error SHOULD ONLY be returned by
191
- // the custom validation code.
192
- getLogger ().Warning (sbomError )
186
+ warnNoLicenseFound (bom , location )
193
187
return
194
188
}
195
-
196
189
_ , err = hashComponentLicense (bom , policyConfig , * component , location , whereFilters , licenseFlags )
197
-
198
190
return
199
191
}
200
192
@@ -240,38 +232,23 @@ func hashComponentLicense(bom *schema.BOM, policyConfig *schema.LicensePolicyCon
240
232
getLogger ().Debugf ("licenseChoice: %s" , getLogger ().FormatStruct (licenseChoice ))
241
233
getLogger ().Tracef ("hashing license for component=`%s`" , cdxComponent .Name )
242
234
243
- licenseInfo .LicenseChoice = licenseChoice
244
- licenseInfo .Component = cdxComponent
245
- licenseInfo .BOMLocationValue = location
246
- licenseInfo .ResourceName = cdxComponent .Name
247
- if cdxComponent .BOMRef != nil {
248
- licenseInfo .BOMRef = * cdxComponent .BOMRef
249
- }
235
+ licenseInfo = * schema .NewLicenseInfoFromComponent (cdxComponent , licenseChoice , location )
250
236
err = hashLicenseInfoByLicenseType (bom , policyConfig , licenseInfo , whereFilters , licenseFlags )
251
237
252
238
if err != nil {
253
239
// Show intent to not check for error returns as there no intent to recover
254
- _ = getLogger ().Errorf ("Unable to hash empty license: %v" , licenseInfo )
240
+ _ = getLogger ().Errorf ("%s. license: %+v" , MSG_LICENSE_HASH_ERROR , licenseInfo )
255
241
return
256
242
}
257
243
}
258
244
} else {
259
245
// Account for component with no license with an "UNDEFINED" entry
260
- // hash any component w/o a license using special key name
261
- licenseInfo .Component = cdxComponent
262
- licenseInfo .BOMLocationValue = location
263
- licenseInfo .ResourceName = cdxComponent .Name
264
- if cdxComponent .BOMRef != nil {
265
- licenseInfo .BOMRef = * cdxComponent .BOMRef
266
- }
246
+ licenseInfo = * schema .NewLicenseInfoFromComponent (cdxComponent , schema.CDXLicenseChoice {}, location )
267
247
_ , err = bom .HashmapLicenseInfo (policyConfig , LICENSE_NO_ASSERTION , licenseInfo , whereFilters , licenseFlags )
268
248
269
- getLogger ().Warningf ("%s: %s (name:`%s`, version: `%s`, package-url: `%s`)" ,
270
- "No license found for component. bomRef" ,
271
- licenseInfo .BOMRef ,
272
- licenseInfo .ResourceName ,
273
- cdxComponent .Version ,
274
- cdxComponent .Purl )
249
+ // Issue a warning that the component had no license; use "safe" BOMRef string value
250
+ // TODO: flag component for stats. purposes
251
+ warnInvalidResourceLicense (schema .RESOURCE_TYPE_COMPONENT , licenseInfo .BOMRef .String (), cdxComponent .Name , cdxComponent .Version )
275
252
// No actual licenses to process
276
253
return
277
254
}
@@ -284,7 +261,6 @@ func hashComponentLicense(bom *schema.BOM, policyConfig *schema.LicensePolicyCon
284
261
return
285
262
}
286
263
}
287
-
288
264
return
289
265
}
290
266
@@ -301,35 +277,23 @@ func hashServiceLicense(bom *schema.BOM, policyConfig *schema.LicensePolicyConfi
301
277
for _ , licenseChoice := range * pLicenses {
302
278
getLogger ().Debugf ("licenseChoice: %s" , getLogger ().FormatStruct (licenseChoice ))
303
279
getLogger ().Tracef ("Hashing license for service=`%s`" , cdxService .Name )
304
- licenseInfo .LicenseChoice = licenseChoice
305
- licenseInfo .Service = cdxService
306
- licenseInfo .ResourceName = cdxService .Name
307
- if cdxService .BOMRef != nil {
308
- licenseInfo .BOMRef = * cdxService .BOMRef
309
- }
310
- licenseInfo .BOMLocationValue = location
280
+ licenseInfo = * schema .NewLicenseInfoFromService (cdxService , licenseChoice , location )
311
281
err = hashLicenseInfoByLicenseType (bom , policyConfig , licenseInfo , whereFilters , licenseFlags )
312
-
313
282
if err != nil {
283
+ // Show intent to not check for error returns as there no intent to recover
284
+ _ = getLogger ().Errorf ("%s. license: %+v" , MSG_LICENSE_HASH_ERROR , licenseInfo )
314
285
return
315
286
}
316
287
}
317
288
} else {
318
289
// Account for service with no license with an "UNDEFINED" entry
319
290
// hash any service w/o a license using special key name
320
- licenseInfo .Service = cdxService
321
- licenseInfo .BOMLocationValue = location
322
- licenseInfo .ResourceName = cdxService .Name
323
- if cdxService .BOMRef != nil {
324
- licenseInfo .BOMRef = * cdxService .BOMRef
325
- }
291
+ licenseInfo = * schema .NewLicenseInfoFromService (cdxService , schema.CDXLicenseChoice {}, location )
326
292
_ , err = bom .HashmapLicenseInfo (policyConfig , LICENSE_NO_ASSERTION , licenseInfo , whereFilters , licenseFlags )
327
293
328
- getLogger ().Warningf ("%s: %s (name: `%s`, version: `%s`)" ,
329
- "No license found for service. bomRef" ,
330
- cdxService .BOMRef ,
331
- cdxService .Name ,
332
- cdxService .Version )
294
+ // Issue a warning that the service had no license; use "safe" BOMRef string value
295
+ // TODO: flag service for stats. purposes
296
+ warnInvalidResourceLicense (schema .RESOURCE_TYPE_SERVICE , licenseInfo .BOMRef .String (), cdxService .Name , cdxService .Version )
333
297
334
298
// No actual licenses to process
335
299
return
@@ -340,8 +304,8 @@ func hashServiceLicense(bom *schema.BOM, policyConfig *schema.LicensePolicyConfi
340
304
if pServices != nil && len (* pServices ) > 0 {
341
305
err = hashServicesLicenses (bom , policyConfig , * pServices , location , whereFilters , licenseFlags )
342
306
if err != nil {
343
- // Show intent to not check for error returns as there is no recovery
344
- _ = getLogger ().Errorf ("Unable to hash empty license: %v" , licenseInfo )
307
+ // Show intent to not check for error returns as there no intent to recover
308
+ _ = getLogger ().Errorf ("%s. license: %+v" , MSG_LICENSE_HASH_ERROR , licenseInfo )
345
309
return
346
310
}
347
311
}
0 commit comments