Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 19 additions & 1 deletion cyclonedx/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
from enum import Enum
from functools import reduce
from json import loads as json_loads
from typing import Any, Dict, FrozenSet, Generator, Iterable, List, Optional, Tuple, Type
from typing import Any, Dict, FrozenSet, Generator, Iterable, List, Optional, Tuple, Type, Union
from uuid import UUID
from warnings import warn
from xml.etree.ElementTree import Element as XmlElement # nosec B405

Expand Down Expand Up @@ -767,6 +768,23 @@ def deserialize(cls, o: Any) -> 'XsUri':
f'XsUri string supplied does not parse: {o!r}'
) from err

@classmethod
def make_bom_link(cls, serialnumber: Union[UUID, str], version: int = 1, bom_ref: Optional[str] = None) -> 'XsUri':
"""
Generate a BOM-Link URI.

Args:
serialnumber (Union[UUID, str]): Unique identifier for the BOM, either as a UUID or a string.
version (int, optional): Version number of the BOM-Link. Defaults to 1.
bom_ref (Optional[str], optional): Reference to a specific component in the BOM. Defaults to None.

Returns:
XsUri: Instance of XsUri with the generated BOM-Link URI.
"""
bom_ref_part = f'#{bom_ref}' if bom_ref else ''
uri = f'urn:cdx:{serialnumber}/{version}{bom_ref_part}'
return cls(uri)


@serializable.serializable_class
class ExternalReference:
Expand Down
17 changes: 16 additions & 1 deletion cyclonedx/model/bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
SchemaVersion1Dot6,
)
from ..serialization import LicenseRepositoryHelper, UrnUuidHelper
from . import ExternalReference, Property
from . import ExternalReference, Property, XsUri
from .bom_ref import BomRef
from .component import Component
from .contact import OrganizationalContact, OrganizationalEntity
Expand Down Expand Up @@ -665,6 +665,21 @@ def register_dependency(self, target: Dependable, depends_on: Optional[Iterable[
def urn(self) -> str:
return f'urn:cdx:{self.serial_number}/{self.version}'

def get_bom_link(self, bom_ref: Union[str, BomRef]) -> XsUri:
"""
Generate a BOM-Link URI.

Args:
bom_ref: The unique identifier of the component, service, or vulnerability within the BOM.

Returns:
XsUri: Instance of XsUri with the generated BOM-Link URI.

.. note:
See the CycloneDX Schema for BOM-Link: https://cyclonedx.org/capabilities/bomlink
"""
return XsUri(f'{self.urn}#{bom_ref}')

def validate(self) -> bool:
"""
Perform data-model level validations to make sure we have some known data integrity prior to attempting output
Expand Down