-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
alma_sbom.py
executable file
·728 lines (670 loc) · 21.8 KB
/
alma_sbom.py
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
#!/usr/bin/env python3
# -*- mode:python; coding:utf-8; -*-
import argparse
import dataclasses
import os
import sys
from logging import basicConfig, getLogger, DEBUG, INFO, WARNING
from collections import defaultdict
from typing import Dict, List, Literal, Optional, Tuple
import requests
from immudb_wrapper import ImmudbWrapper
from libsbom import cyclonedx as alma_cyclonedx
from libsbom import spdx as alma_spdx
ALBS_URL = 'https://build.almalinux.org'
IS_SIGNED = 3
_logger = getLogger('alma-sbom')
@dataclasses.dataclass
class PackageNevra:
name: str = None
epoch: str = None
version: str = None
release: str = None
arch: str = None
def __repr__(self):
if self.epoch is not None:
return (
f'{self.epoch}:{self.name}-'
f'{self.version}-{self.release}.{self.arch}'
)
return f'{self.name}-{self.version}-' f'{self.release}.{self.arch}'
@dataclasses.dataclass
class FileFormat:
sbom_record_type: Literal[
'cyclonedx',
'spdx',
] = 'cyclonedx'
file_format: Literal[
'json',
'tagvalue',
'xml',
'yaml',
] = 'json'
def __repr__(self):
return f'"{self.sbom_record_type}-{self.file_format}"'
class FileFormatType:
supported_file_formats = defaultdict(
list,
**{
'cyclonedx': [
'json',
'xml',
],
'spdx': [
'json',
'tagvalue',
'xml',
'yaml',
],
},
)
def __call__(self, sbom_type_file_format: str) -> FileFormat:
sbom_record_type, file_format = sbom_type_file_format.split('-')
if sbom_record_type not in self.supported_file_formats:
_logger.error('The utility doesn\'t support that SBOM type yet')
sys.exit(1)
if file_format not in self.supported_file_formats[sbom_record_type]:
_logger.error('The utility doesn\'t support that file format yet')
sys.exit(1)
return FileFormat(
sbom_record_type=sbom_record_type,
file_format=file_format,
)
@classmethod
def choices(cls) -> List[FileFormat]:
return [
FileFormat(sbom_record_type, file_format)
for sbom_record_type in cls.supported_file_formats
for file_format in cls.supported_file_formats[sbom_record_type]
]
def split_name_of_package_by_nevra(package_name: str) -> PackageNevra:
package_nevra = PackageNevra()
split_by_dot = package_name.replace('.rpm', '')[::-1].split('.', 1)
package_nevra.arch = split_by_dot[0][::-1]
split_by_hyphen = split_by_dot[1].split('-', 2)
package_nevra.release = split_by_hyphen[0][::-1]
package_nevra.version = split_by_hyphen[1][::-1]
package_nevra.name = split_by_hyphen[2][::-1]
return package_nevra
def generate_sbom_version(json_data: Dict) -> int:
sbom_version = 1
if json_data['sign_tasks'] and 'status' in json_data['sign_tasks'][-1]:
sbom_version += json_data['sign_tasks'][-1]['status'] == IS_SIGNED
return sbom_version
def _extract_immudb_info_about_package(
immudb_wrapper: ImmudbWrapper,
immudb_hash: str = None,
rpm_package: str = None,
) -> Dict:
if immudb_hash != None :
response = immudb_wrapper.authenticate(immudb_hash)
elif rpm_package != None :
if not os.path.exists(rpm_package):
_logger.error(f'File {rpm_package} Not Found')
sys.exit(1)
response = immudb_wrapper.authenticate_file(rpm_package)
result = response.get('value', {})
result['timestamp'] = response.get('timestamp')
return result
def _get_specific_info_about_package(
immudb_info_about_package: Dict,
) -> Tuple[Optional[str], PackageNevra]:
immudb_metadata = immudb_info_about_package['Metadata']
# We have `sbom_api_ver` in git records and `sbom_api`
# in RPM package records. The latter parameter is the bug,
# but we should handle it anyway
# since a lot of packages already have it.
api_ver = immudb_metadata.get('sbom_api_ver')
if not api_ver:
api_ver = immudb_metadata.get('sbom_api')
if not api_ver:
raise ValueError(
'Immudb metadata is malformed, API version cannot be detected'
)
if api_ver == '0.1':
package_name = immudb_info_about_package['Name']
package_nevra = split_name_of_package_by_nevra(package_name)
source_rpm = None
else:
package_nevra = PackageNevra(
name=immudb_metadata['name'],
epoch=immudb_metadata['epoch'],
version=immudb_metadata['version'],
release=immudb_metadata['release'],
arch=immudb_metadata['arch'],
)
source_rpm = immudb_metadata['sourcerpm']
return source_rpm, package_nevra
def _generate_cpe(package_nevra: PackageNevra) -> str:
# https://github.com/AlmaLinux/build-system-rfes/commit/e4e6e655ecd09796e539fc6bc4665a55b047e49d
cpe_version = '2.3'
cpe_epoch_part = f'{package_nevra.epoch if package_nevra.epoch else ""}'
cpe_epoch_part += '\\:' if cpe_epoch_part else ""
cpe = (
f'cpe:{cpe_version}:a:almalinux:'
f'{package_nevra.name}:{cpe_epoch_part}'
f'{package_nevra.version}-{package_nevra.release}:*:*:*:*:*:*:*'
)
return cpe
def _generate_purl(package_nevra: PackageNevra, source_rpm: str):
# https://github.com/AlmaLinux/build-system-rfes/commit/a132ececa1d7901fe42348022ce954d475578920
if package_nevra.epoch:
purl_epoch_part = f'&epoch={package_nevra.epoch}'
else:
purl_epoch_part = ''
if source_rpm:
purl_upstream_part = f'&upstream={source_rpm}'
else:
purl_upstream_part = ''
purl = (
f'pkg:rpm/almalinux/{package_nevra.name}@{package_nevra.version}-'
f'{package_nevra.release}?arch={package_nevra.arch}'
f'{purl_epoch_part}{purl_upstream_part}'
)
return purl
def add_package_source_info(immudb_metadata: Dict, component: Dict):
if immudb_metadata['source_type'] == 'git':
component['properties'].extend(
[
{
'name': 'almalinux:albs:build:source:gitURL',
'value': immudb_metadata['git_url'],
},
{
'name': 'almalinux:albs:build:source:type',
'value': 'git',
},
{
'name': 'almalinux:albs:build:source:gitCommit',
'value': immudb_metadata['git_commit'],
},
{
'name': 'almalinux:albs:build:source:gitRef',
'value': immudb_metadata['git_ref'],
},
{
'name': 'almalinux:albs:build:source:gitCommitImmudbHash',
'value': immudb_metadata['alma_commit_sbom_hash']
if 'alma_commit_sbom_hash' in immudb_metadata
else None,
},
]
)
elif immudb_metadata['source_type'] == 'srpm':
component['properties'].extend(
[
{
'name': 'almalinux:albs:build:source:srpmURL',
'value': immudb_metadata['srpm_url'],
},
{
'name': 'almalinux:albs:build:source:type',
'value': 'srpm',
},
{
'name': 'almalinux:albs:build:source:srpmChecksum',
'value': immudb_metadata['srpm_sha256'],
},
{
'name': 'almalinux:albs:build:source:srpmNEVRA',
'value': immudb_metadata['srpm_nevra'],
},
]
)
def get_info_about_package(
albs_url: str,
immudb_wrapper: ImmudbWrapper,
immudb_hash: str = None,
rpm_package: str = None,
):
result = {}
immudb_info_about_package = _extract_immudb_info_about_package(
immudb_wrapper=immudb_wrapper,
immudb_hash=immudb_hash,
rpm_package=rpm_package,
)
source_rpm, package_nevra = _get_specific_info_about_package(
immudb_info_about_package=immudb_info_about_package,
)
immudb_hash = immudb_hash or immudb_info_about_package['Hash']
immudb_metadata = immudb_info_about_package['Metadata']
result['version'] = 1
if 'unsigned_hash' in immudb_metadata:
result['version'] += 1
result['metadata'] = {}
result['metadata']['component'] = {
'name': package_nevra.name,
'version': (
f'{package_nevra.epoch if package_nevra.epoch else ""}'
f'{":" if package_nevra.epoch else ""}'
f'{package_nevra.version}-{package_nevra.release}'
),
'hashes': [
{
'alg': 'SHA-256',
'content': immudb_hash,
}
],
'cpe': _generate_cpe(package_nevra=package_nevra),
'purl': _generate_purl(
package_nevra=package_nevra,
source_rpm=source_rpm,
),
'properties': [
{
'name': 'almalinux:package:epoch',
'value': package_nevra.epoch,
},
{
'name': 'almalinux:package:version',
'value': package_nevra.version,
},
{
'name': 'almalinux:package:release',
'value': package_nevra.release,
},
{
'name': 'almalinux:package:arch',
'value': package_nevra.arch,
},
{
'name': 'almalinux:package:sourcerpm',
'value': source_rpm,
},
{
'name': 'almalinux:package:buildhost',
'value': immudb_metadata['build_host'],
},
{
'name': 'almalinux:package:timestamp',
'value': immudb_info_about_package['timestamp'],
},
{
'name': 'almalinux:albs:build:targetArch',
'value': immudb_metadata['build_arch'],
},
{
'name': 'almalinux:albs:build:packageType',
'value': 'rpm',
},
{
'name': 'almalinux:sbom:immudbHash',
'value': immudb_hash,
},
{
'name': 'almalinux:albs:build:ID',
'value': immudb_metadata['build_id'],
},
{
'name': 'almalinux:albs:build:URL',
'value': f'{albs_url}/build/{immudb_metadata["build_id"]}',
},
{
'name': 'almalinux:albs:build:author',
'value': immudb_metadata['built_by'],
},
],
}
add_package_source_info(
immudb_metadata=immudb_metadata,
component=result['metadata']['component'],
)
return result
def get_info_about_build(
build_id: int,
albs_url: str,
immudb_wrapper: ImmudbWrapper,
):
result = {}
albs_builds_endpoint = f'{albs_url}/api/v1/builds'
response = requests.get(
url=f'{albs_builds_endpoint}/{build_id}',
)
response.raise_for_status()
json_data = response.json()
result['version'] = generate_sbom_version(json_data)
owner = json_data['owner']
build_url = f'{albs_url}/build/{build_id}'
build_metadata = {
'name': f'build-{build_id}',
'author': f"{owner['username']} <{owner['email']}>",
'properties': [
{
'name': 'almalinux:albs:build:ID',
'value': build_id,
},
{
'name': 'almalinux:albs:build:URL',
'value': build_url,
},
{
'name': 'almalinux:albs:build:timestamp',
'value': json_data['created_at'],
},
],
}
result['metadata'] = {}
result['metadata']['component'] = build_metadata
components = []
for task in json_data['tasks']:
for artifact in task['artifacts']:
if artifact['type'] != 'rpm':
continue
immudb_hash = artifact['cas_hash']
result_of_execution = _extract_immudb_info_about_package(
immudb_wrapper=immudb_wrapper,
immudb_hash=immudb_hash,
)
immudb_metadata = result_of_execution['Metadata']
source_rpm, package_nevra = _get_specific_info_about_package(
immudb_info_about_package=result_of_execution,
)
component = {
'name': package_nevra.name,
'version': package_nevra.version,
'cpe': _generate_cpe(package_nevra=package_nevra),
'purl': _generate_purl(
package_nevra=package_nevra,
source_rpm=source_rpm,
),
'hashes': [
{
'alg': 'SHA-256',
'content': immudb_hash,
}
],
'properties': [
{
'name': 'almalinux:package:epoch',
'value': package_nevra.epoch,
},
{
'name': 'almalinux:package:version',
'value': package_nevra.version,
},
{
'name': 'almalinux:package:release',
'value': package_nevra.release,
},
{
'name': 'almalinux:package:arch',
'value': package_nevra.arch,
},
{
'name': 'almalinux:package:sourcerpm',
'value': source_rpm,
},
{
'name': 'almalinux:package:buildhost',
'value': immudb_metadata['build_host'],
},
{
'name': 'almalinux:albs:build:targetArch',
'value': immudb_metadata['build_arch'],
},
{
'name': 'almalinux:albs:build:packageType',
'value': 'rpm',
},
{
'name': 'almalinux:sbom:immudbHash',
'value': result_of_execution['Hash'],
},
{
'name': 'almalinux:albs:build:ID',
'value': build_id,
},
{
'name': 'almalinux:albs:build:URL',
'value': build_url,
},
{
'name': 'almalinux:albs:build:author',
'value': immudb_metadata['built_by'],
},
],
}
add_package_source_info(
immudb_metadata=immudb_metadata,
component=component,
)
components.append(component)
result['components'] = components
return result
def _proc_opt_creators(
persons_name: list,
persons_email: list,
orgs_name: list,
orgs_email: list,
):
creators_person = {
'name': persons_name,
'email': persons_email,
}
creators_org = {
'name': orgs_name,
'email': orgs_email,
}
opt_creators = {
'creators_person': creators_person,
'creators_org': creators_org,
}
return opt_creators
def create_parser():
parser = argparse.ArgumentParser()
parser.add_argument(
'--output-file',
type=str,
help=(
'Full path to an output file with SBOM. Output will be '
'to stdout if the parameter is absent or emtpy'
),
required=False,
default=None,
)
parser.add_argument(
'--file-format',
default=FileFormat(),
const=FileFormat(),
nargs='?',
choices=FileFormatType.choices(),
type=FileFormatType(),
help='Generate SBOM in one of format mode (default: %(default)s)',
)
object_id_group = parser.add_mutually_exclusive_group(required=True)
object_id_group.add_argument(
'--build-id',
type=int,
help='UID of a build from AlmaLinux Build System',
)
object_id_group.add_argument(
'--rpm-package-hash',
type=str,
help='SHA256 hash of an RPM package',
)
object_id_group.add_argument(
'--rpm-package',
type=str,
help='path to an RPM package',
)
parser.add_argument(
'--albs-url',
type=str,
help='Override ALBS url',
)
parser.add_argument(
'--immudb-username',
type=str,
help=(
'Provide your immudb username if not set as '
'an environmental variable'
),
required=False,
)
parser.add_argument(
'--immudb-password',
type=str,
help=(
'Provide your immudb password if not set as '
'an environmental variable'
),
required=False,
)
parser.add_argument(
'--immudb-database',
type=str,
help=(
'Provide your immudb database if not set as '
'an environmental variable'
),
required=False,
)
parser.add_argument(
'--immudb-address',
type=str,
help=(
'Provide your immudb address if not set as '
'an environmental variable'
),
required=False,
)
parser.add_argument(
'--immudb-public-key-file',
type=str,
help=(
'Provide your immudb public key file if not set as '
'an environmental variable'
),
required=False,
)
parser.add_argument(
'--verbose',
help=(
'Print verbose output'
),
required=False,
default=WARNING,
action='store_const', dest='loglevel', const=INFO,
)
parser.add_argument(
'--debug',
help=(
'Print debug log'
),
required=False,
action='store_const', dest='loglevel', const=DEBUG,
)
parser.add_argument(
'--creator-name-person',
type=str,
action='append',
help=(
'The person(s) who create SBOM'
),
required=False,
default=[],
)
parser.add_argument(
'--creator-email-person',
type=str,
action='append',
help=(
'The email address of SBOM creator. '
'This option is only required if --creator-name-personal is provided. '
'The combination of name and email address depends on the order specified. '
'If an extra email address is specified, it will be ignored'
),
required=False,
default=[],
)
parser.add_argument(
'--creator-name-org',
type=str,
action='append',
help=(
'The organization(s) who create SBOM'
),
required=False,
default=[],
)
parser.add_argument(
'--creator-email-org',
type=str,
action='append',
help=(
'The email address of SBOM creator. '
'This option is only required if --creator-name-org is provided. '
'The combination of name and email address depends on the order specified. '
'If an extra email address is specified, it will be ignored.'
),
required=False,
default=[],
)
return parser
def cli_main():
formatters = {
'cyclonedx': alma_cyclonedx.SBOM,
'spdx': alma_spdx.SBOM,
}
fmt = '%(asctime)s %(name)s: [%(levelname)s] %(message)s'
datefmt = '%b %d %H:%M:%S'
basicConfig(format=fmt, datefmt=datefmt)
args = create_parser().parse_args()
_logger.setLevel(args.loglevel)
immudb_wrapper = ImmudbWrapper(
username=(
args.immudb_username
or os.getenv('IMMUDB_USERNAME')
or ImmudbWrapper.read_only_username()
),
password=(
args.immudb_password
or os.getenv('IMMUDB_PASSWORD')
or ImmudbWrapper.read_only_password()
),
database=(
args.immudb_database
or os.getenv('IMMUDB_DATABASE')
or ImmudbWrapper.almalinux_database_name()
),
immudb_address=(
args.immudb_address
or os.getenv('IMMUDB_ADDRESS')
or ImmudbWrapper.almalinux_database_address()
),
public_key_file=(
args.immudb_public_key_file or os.getenv('IMMUDB_PUBLIC_KEY_FILE')
),
)
albs_url = args.albs_url or ALBS_URL
if args.build_id:
sbom = get_info_about_build(
args.build_id,
albs_url=albs_url,
immudb_wrapper=immudb_wrapper,
)
sbom_object_type = 'build'
else:
sbom = get_info_about_package(
albs_url=albs_url,
immudb_wrapper=immudb_wrapper,
immudb_hash=args.rpm_package_hash,
rpm_package=args.rpm_package,
)
sbom_object_type = 'package'
opt_creators = _proc_opt_creators(
args.creator_name_person,
args.creator_email_person,
args.creator_name_org,
args.creator_email_org
)
sbom_formatter = formatters[args.file_format.sbom_record_type](
data=sbom,
sbom_object_type=sbom_object_type,
output_format=args.file_format.file_format,
output_file=args.output_file,
opt_creators=opt_creators,
)
sbom_formatter.run()
if __name__ == '__main__':
cli_main()