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

Next Feature Release #145

Merged
merged 21 commits into from
Jan 27, 2022
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
158 changes: 158 additions & 0 deletions cyclonedx/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,164 @@ def sha1sum(filename: str) -> str:
h.update(byte_block)
return h.hexdigest()

class DataFlow(Enum):
"""
This is out internal representation of the dataFlowType simple type within the CycloneDX standard.

.. note::
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/xml/#type_dataFlowType
"""
INBOUND = "inbound"
OUTBOUND = "outbound"
BI_DIRECTIONAL = "bi-directional"
UNKNOWN = "unknown"

class Data:
"""
This is our internal representation of the `dataClassificationType` complex type within the CycloneDX standard.

.. note::
See the CycloneDX Schema for dataClassificationType: https://cyclonedx.org/docs/1.4/xml/#type_dataClassificationType
"""

def __init__(self, flow: DataFlow, classification: str) -> None:
if not flow and not classification:
raise NoPropertiesProvidedException(
'One of `flow` or `classification` must be supplied - neither supplied'
)

self.flow = flow
self.classification = classification

@property
def flow(self) -> DataFlow:
"""
Specifies the data flow for the service.

Returns:
`DataFlow`
"""
return self._flow

@flow.setter
def flow(self, flow: DataFlow) -> None:
self._flow = flow

@property
def classification(self) -> str:
"""
Specifies the classification of the data for the service.

Returns:
`str`
"""
return self._content_type

@classification.setter
def classification(self, classification: str) -> None:
self._classification = classification

class SignatureAlgorithm(Enum):
"""
This is out internal representation of the algorithm simple type within the CycloneDX standard.

.. note::
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/json/#tab-pane_signature_oneOf_i2_algorithm_oneOf_i0
"""
RS256 = "RS256"
RS384 = "RS384"
RS512 = "RS512"
PS256 = "PS256"
PS384 = "PS384"
PS512 = "PS512"
ES256 = "ES256"
ES384 = "ES384"
ES512 = "ES512"
ED25519 = "Ed25519"
ED448 = "Ed448"
HS256 = "HS256"
HS384 = "HS384"
HS512 = "HS512"

class SignaturePublicKeyKty(Enum):
"""
This is our internal representation of the kty simple type within the CycloneDX standard.

.. note::
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/json/#signature_oneOf_i2_publicKey_kty
"""
EC = "EC"
OKP = "OKP"
RSA = "RSA"

class SignaturePublicKeyCrv(Enum):
"""
This is our internal representation of the crv simple type within the CycloneDX standard.

.. note::
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/json/#signature_oneOf_i2_publicKey_allOf_i1_then_crv
"""
ED25519 = "Ed25519"
Ed448 = "Ed448"

class SignaturePublicKey:
"""
This is our internal representation of the public key complex type within the CycloneDX standard.

.. note::
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/json/#signature_oneOf_i2_publicKey
JSON only
"""
def __init__(self, kty: SignaturePublicKeyKty = None, crv: Optional[SignaturePublicKeyCrv] = None,
madpah marked this conversation as resolved.
Show resolved Hide resolved
x: Optional[str] = None, y: Optional[str] = None,
n: Optional[str] = None, e: Optional[str] = None) -> None:
if not kty:
raise NoPropertiesProvidedException(
'`kty` must be supplied'
)
if kty == SignaturePublicKeyKty.EC and not crv and not x and not y:
raise NoPropertiesProvidedException(
'if `kty` equals EC, `crv`, `x` and `y` must be supplied'
)
if kty == SignaturePublicKeyKty.OKP and not crv and not x:
raise NoPropertiesProvidedException(
'if `kty` equals OKP, `crv`, and `x` must be supplied'
)
if kty == SignaturePublicKeyKty.RSA and not n and not e:
raise NoPropertiesProvidedException(
'if `kty` equals RSA, `n`, and `e` must be supplied'
)
self.kty = kty
self.crv = crv
self.x = x
self.y = y
self.n = n
self.e = e
self.value = value
madpah marked this conversation as resolved.
Show resolved Hide resolved

class Signature:
"""
This is out internal representation of the signature complex type within the CycloneDX standard.

.. note::
See the CycloneDX Schema: https://cyclonedx.org/docs/1.4/json/#signature
JSON only
"""

def __init__(self, algorithm: SignatureAlgorithm, value: str, key_id: Optional[str],
public_key: Optional[SignaturePublicKey] = None,
certificate_path: Optional[List[str]] = None,
excludes: Optional[List[str]] = None) -> None:
if not algorithm and not value:
raise NoPropertiesProvidedException(
'One of `algorithm` or `value` must be supplied - neither supplied'
)
self.algorithm = algorithm
self.key_id = key_id
self.public_key = public_key
self.certificate_path = certificate_path
self.excludes = excludes
self.value = value

class Encoding(Enum):
"""
Expand Down
65 changes: 65 additions & 0 deletions cyclonedx/model/bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from . import ThisTool, Tool
from .component import Component
from ..parser import BaseParser
from .service import Service


class BomMetaData:
Expand Down Expand Up @@ -143,6 +144,7 @@ def __init__(self) -> None:
self.uuid = uuid4()
self.metadata = BomMetaData()
self._components: List[Component] = []
self._services: List[Service] = []

@property
def uuid(self) -> UUID:
Expand Down Expand Up @@ -265,6 +267,69 @@ def has_component(self, component: Component) -> bool:
"""
return component in self._components

@property
def services(self) -> List[Service]:
"""
Get all the Services currently in this Bom.

Returns:
List of all Services in this Bom.
"""
return self._services

@services.setter
def services(self, services: List[Service]) -> None:
self._services = services

def add_service(self, service: Service) -> None:
"""
Add a Service to this Bom instance.

Args:
service:
`cyclonedx.model.service.Service` instance to add to this Bom.

Returns:
None
"""
if not self.has_component(service=service):
madpah marked this conversation as resolved.
Show resolved Hide resolved
self._services.append(service)

def add_services(self, services: List[Service]) -> None:
"""
Add multiple Services at once to this Bom instance.

Args:
services:
List of `cyclonedx.model.service.Service` instances to add to this Bom.

Returns:
None
"""
self.services = self._services + services

def has_service(self, service: Service) -> bool:
"""
Check whether this Bom contains the provided Service.

Args:
component:
The instance of `cyclonedx.model.service.Service` to check if this Bom contains.

Returns:
`bool` - `True` if the supplied Service is part of this Bom, `False` otherwise.
"""
return service in self._services

def service_count(self) -> int:
"""
Returns the current count of Services within this Bom.

Returns:
The number of Services in this Bom as `int`.
"""
return len(self._services)

def has_vulnerabilities(self) -> bool:
"""
Check whether this Bom has any declared vulnerabilities.
Expand Down
2 changes: 1 addition & 1 deletion cyclonedx/model/issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class IssueClassification(Enum):
This is out internal representation of the enum `issueClassification`.

.. note::
See the CycloneDX Schema definition: hhttps://cyclonedx.org/docs/1.4/xml/#type_issueClassification
See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.4/xml/#type_issueClassification
"""
DEFECT = 'defect'
ENHANCEMENT = 'enhancement'
Expand Down
Loading