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

Fix error when metadata is incomplete #38

Closed
Closed
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
262 changes: 119 additions & 143 deletions alma_sbom.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,101 +239,142 @@ def add_package_source_info(immudb_metadata: Dict, component: Dict):
)


def get_info_about_package(
immudb_hash: str,
def add_package_build_info(
immudb_metadata: Dict,
component: Dict,
albs_url: str,
immudb_wrapper: ImmudbWrapper,
):
result = {}
immudb_info_about_package = _extract_immudb_info_about_package(
immudb_hash=immudb_hash,
immudb_wrapper=immudb_wrapper,
)
source_rpm, package_nevra = _get_specific_info_about_package(
immudb_info_about_package=immudb_info_about_package,
)
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,
},
component['properties'].extend(
[
{
'name': 'almalinux:package:buildhost',
'value': immudb_metadata['build_host'],
},
{
'name': 'almalinux:package:timestamp',
'value': immudb_info_about_package['timestamp'],
'value': immudb_metadata['build_host']
if 'build_host' in immudb_metadata
else None,
},
{
'name': 'almalinux:albs:build:targetArch',
'value': immudb_metadata['build_arch'],
},
{
'name': 'almalinux:albs:build:packageType',
'value': 'rpm',
},
{
'name': 'almalinux:sbom:immudbHash',
'value': immudb_hash,
'value': immudb_metadata['build_arch']
if 'build_arch' in immudb_metadata
else None,
},
{
'name': 'almalinux:albs:build:ID',
'value': immudb_metadata['build_id'],
'value': immudb_metadata['build_id']
if 'build_id' in immudb_metadata
else None,
},
{
'name': 'almalinux:albs:build:URL',
'value': f'{albs_url}/build/{immudb_metadata["build_id"]}',
'value': f'{albs_url}/build/{immudb_metadata["build_id"]}'
if 'build_id' in immudb_metadata
else None,
},
{
'name': 'almalinux:albs:build:author',
'value': immudb_metadata['built_by'],
'value': immudb_metadata['built_by']
if 'built_by' in immudb_metadata
else None,
},
],
}
]
)


def add_package_info(
immudb_hash: str,
immudb_info_about_package: Dict,
component: Dict,
albs_url: str,
):
source_rpm, package_nevra = _get_specific_info_about_package(
immudb_info_about_package=immudb_info_about_package,
)
immudb_metadata = immudb_info_about_package['Metadata']

component['name'] = package_nevra.name
component['version'] = (
f'{package_nevra.epoch if package_nevra.epoch else ""}'
f'{":" if package_nevra.epoch else ""}'
f'{package_nevra.version}-{package_nevra.release}'
)
component['hashes'] = [
{
'alg': 'SHA-256',
'content': immudb_hash,
}
]
component['cpe'] = _generate_cpe(package_nevra=package_nevra)
component['purl'] = _generate_purl(
package_nevra=package_nevra,
source_rpm=source_rpm,
)
component['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:timestamp',
'value': immudb_info_about_package['timestamp'],
},
{
'name': 'almalinux:albs:build:packageType',
'value': 'rpm',
},
{
'name': 'almalinux:sbom:immudbHash',
'value': immudb_hash,
},
]

add_package_build_info(
immudb_metadata=immudb_metadata,
component=component,
albs_url=albs_url,
)
add_package_source_info(
immudb_metadata=immudb_metadata,
component=component,
)


def get_info_about_package(
immudb_hash: str,
albs_url: str,
immudb_wrapper: ImmudbWrapper,
):
result = {}
immudb_info_about_package = _extract_immudb_info_about_package(
immudb_hash=immudb_hash,
immudb_wrapper=immudb_wrapper,
)
immudb_metadata = immudb_info_about_package['Metadata']
result['version'] = 1
if 'unsigned_hash' in immudb_metadata:
result['version'] += 1
result['metadata'] = {}
result['metadata']['component'] = {}
add_package_info(
immudb_hash=immudb_hash,
immudb_info_about_package=immudb_info_about_package,
component=result['metadata']['component'],
albs_url=albs_url,
)
return result

Expand Down Expand Up @@ -384,77 +425,12 @@ def get_info_about_build(
immudb_wrapper=immudb_wrapper,
)
immudb_metadata = result_of_execution['Metadata']
source_rpm, package_nevra = _get_specific_info_about_package(
component = {}
add_package_info(
immudb_hash=immudb_hash,
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,
albs_url=albs_url,
)
components.append(component)
result['components'] = components
Expand Down