Skip to content

Commit

Permalink
Update PR as per feedback
Browse files Browse the repository at this point in the history
- update copyright statements
- use `go` for Golang modules package format
- rafactor how PURLs are generated
- remove CycloneDX template
  • Loading branch information
coderpatros committed Jul 31, 2021
1 parent 225dbb1 commit ce1c04d
Show file tree
Hide file tree
Showing 8 changed files with 12 additions and 72 deletions.
2 changes: 1 addition & 1 deletion tern/analyze/default/command_lib/base.yml
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ npm:
delimiter: "LICF"
# golang----------------------------------------------------------------------
go:
pkg_format: 'go.mod'
pkg_format: 'go'
os_guess:
- 'None'
path:
Expand Down
2 changes: 1 addition & 1 deletion tern/formats/cyclonedx/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2019 VMware, Inc. All Rights Reserved.
# Copyright (c) 2021 Patrick Dwyer. All Rights Reserved.
# SPDX-License-Identifier: BSD-2-Clause
24 changes: 0 additions & 24 deletions tern/formats/cyclonedx/cyclonedx.py

This file was deleted.

40 changes: 3 additions & 37 deletions tern/formats/cyclonedx/cyclonedx_common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 VMware, Inc. All Rights Reserved.
# Copyright (c) 2021 Patrick Dwyer. All Rights Reserved.
# SPDX-License-Identifier: BSD-2-Clause

"""
Expand Down Expand Up @@ -33,38 +33,13 @@
}


pkg_format_purl_type_mapping = {
'deb': 'deb',
'rpm': 'rpm',
'apk': 'apk',
'pip': 'pip',
'gem': 'gem',
'npm': 'npm',
'go.mod': 'go',
}


purl_types_with_namespaces = [
'deb',
'rpm',
'apk',
]


# map Tern OS guesses to package URL namespace
os_guess_purl_namespace_mapping = {
'debian': 'debian',
'ubuntu': 'ubuntu',
'alpine linux': 'alpine',
'centos': 'centos',
'fedora': 'fedora',
'opensuse': 'opensuse',
'rhel': 'rhel',
# Arch Linux
# Photon
}


def get_serial_number():
''' Return a randomly generated CycloneDX BOM serial number '''
return 'urn:uuid:' + str(uuid.uuid4())
Expand All @@ -86,23 +61,14 @@ def get_property(name, value):
return {'name': name, 'value': value}


def get_purl_type(pkg_format):
return pkg_format_purl_type_mapping.get(pkg_format.lower())


def get_purl_namespace(os_guess, pkg_format):
if pkg_format in purl_types_with_namespaces:
for os in os_guess_purl_namespace_mapping:
if os_guess.lower().startswith(os):
return os_guess_purl_namespace_mapping.get(os)
return os_guess.partition(' ')[0].lower()
return None


def get_os_guess(image_obj):
for layer in image_obj.layers:
if layer.os_guess:
return layer.os_guess
return None
return image_obj.layers[0].os_guess or None


def get_license_from_name(name):
Expand Down
2 changes: 1 addition & 1 deletion tern/formats/cyclonedx/cyclonedxjson/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 VMware, Inc. All Rights Reserved.
# Copyright (c) 2021 Patrick Dwyer. All Rights Reserved.
# SPDX-License-Identifier: BSD-2-Clause
8 changes: 3 additions & 5 deletions tern/formats/cyclonedx/cyclonedxjson/generator.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 VMware, Inc. All Rights Reserved.
# Copyright (c) 2021 Patrick Dwyer. All Rights Reserved.
# SPDX-License-Identifier: BSD-2-Clause

'''
Expand All @@ -12,7 +12,6 @@

from tern.utils import constants
from tern.formats import generator
from tern.formats.cyclonedx.cyclonedx import CycloneDX
from tern.formats.cyclonedx import cyclonedx_common
from tern.formats.cyclonedx.cyclonedxjson import image_helpers as mhelpers
from tern.formats.cyclonedx.cyclonedxjson import package_helpers as phelpers
Expand All @@ -22,7 +21,7 @@
logger = logging.getLogger(constants.logger_name)


def get_document_dict(image_obj_list, template): # pylint: disable=[unused-argument]
def get_document_dict(image_obj_list):
''' Return document info as a dictionary '''
docu_dict = {
'bomFormat': 'CycloneDX',
Expand Down Expand Up @@ -58,7 +57,6 @@ def generate(self, image_obj_list, print_inclusive=False):
in report.py. '''
logger.debug('Generating CycloneDX JSON document...')

template = CycloneDX()
report = get_document_dict(image_obj_list, template)
report = get_document_dict(image_obj_list)

return json.dumps(report, indent=2)
2 changes: 1 addition & 1 deletion tern/formats/cyclonedx/cyclonedxjson/image_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 VMware, Inc. All Rights Reserved.
# Copyright (c) 2021 Patrick Dwyer. All Rights Reserved.
# SPDX-License-Identifier: BSD-2-Clause

'''
Expand Down
4 changes: 2 additions & 2 deletions tern/formats/cyclonedx/cyclonedxjson/package_helpers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2021 VMware, Inc. All Rights Reserved.
# Copyright (c) 2021 Patrick Dwyer. All Rights Reserved.
# SPDX-License-Identifier: BSD-2-Clause

'''
Expand All @@ -20,7 +20,7 @@ def get_package_dict(os_guess, package):
'type': 'application',
}

purl_type = cyclonedx_common.get_purl_type(package.pkg_format)
purl_type = package.pkg_format
purl_namespace = cyclonedx_common.get_purl_namespace(os_guess, package.pkg_format)
if purl_type:
purl = PackageURL(purl_type, purl_namespace, package.name, package.version)
Expand Down

0 comments on commit ce1c04d

Please sign in to comment.