Skip to content

Commit cf13c68

Browse files
committed
doc: added documentation
Signed-off-by: Paul Horton <[email protected]>
1 parent ac70eee commit cf13c68

File tree

4 files changed

+185
-13
lines changed

4 files changed

+185
-13
lines changed

cyclonedx/model/component.py

Lines changed: 104 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@
2828

2929
class ComponentType(Enum):
3030
"""
31-
Enum object that defines the permissible 'types' for a Component according to the CycloneDX
32-
schemas.
31+
Enum object that defines the permissible 'types' for a Component according to the CycloneDX schema.
32+
33+
.. note::
34+
See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.3/#type_classification
3335
"""
3436
APPLICATION = 'application'
3537
CONTAINER = 'container'
@@ -43,7 +45,10 @@ class ComponentType(Enum):
4345

4446
class Component:
4547
"""
46-
An object that mirrors the Component type in the CycloneDX schema.
48+
This is our internal representation of a Component within a Bom.
49+
50+
.. note::
51+
See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.3/#type_component
4752
"""
4853
_type: ComponentType
4954
_name: str
@@ -65,52 +70,147 @@ def __init__(self, name: str, version: str, qualifiers: str = None,
6570
self._vulnerabilites = []
6671

6772
def add_vulnerability(self, vulnerability: Vulnerability):
73+
"""
74+
Add a Vulnerability to this Component.
75+
76+
Args:
77+
vulnerability:
78+
`cyclonedx.model.vulnerability.Vulnerability` instance to add to this Component.
79+
80+
Returns:
81+
None
82+
"""
6883
self._vulnerabilites.append(vulnerability)
6984

7085
def get_author(self) -> str:
86+
"""
87+
Get the author of this Component.
88+
89+
Returns:
90+
Declared author of this Component as `str`.
91+
"""
7192
return self._author
7293

7394
def get_description(self) -> str:
95+
"""
96+
Get the description of this Component.
97+
98+
Returns:
99+
Declared description of this Component as `str`.
100+
"""
74101
return self._description
75102

76103
def get_license(self) -> str:
104+
"""
105+
Get the license of this Component.
106+
107+
Returns:
108+
Declared license of this Component as `str`.
109+
"""
77110
return self._license
78111

79112
def get_name(self) -> str:
113+
"""
114+
Get the name of this Component.
115+
116+
Returns:
117+
Declared name of this Component as `str`.
118+
"""
80119
return self._name
81120

82121
def get_purl(self) -> str:
122+
"""
123+
Get the PURL for this Component.
124+
125+
Returns:
126+
PackageURL that reflects this Component as `str`.
127+
"""
83128
base_purl = 'pkg:{}/{}@{}'.format(PURL_TYPE_PREFIX, self._name, self._version)
84129
if self._qualifiers:
85130
base_purl = '{}?{}'.format(base_purl, self._qualifiers)
86131
return base_purl
87132

88133
def get_type(self) -> ComponentType:
134+
"""
135+
Get the type of this Component.
136+
137+
Returns:
138+
Declared type of this Component as `ComponentType`.
139+
"""
89140
return self._type
90141

91142
def get_version(self) -> str:
143+
"""
144+
Get the version of this Component.
145+
146+
Returns:
147+
Declared version of this Component as `str`.
148+
"""
92149
return self._version
93150

94151
def get_vulnerabilities(self) -> List[Vulnerability]:
152+
"""
153+
Get all the Vulnerabilities for this Component.
154+
155+
Returns:
156+
List of `Vulnerability` objects assigned to this Component.
157+
"""
95158
return self._vulnerabilites
96159

97160
def has_vulnerabilities(self) -> bool:
161+
"""
162+
Does this Component have any vulnerabilities?
163+
164+
Returns:
165+
`True` if this Component has 1 or more vulnerabilities, `False` otherwise.
166+
"""
98167
return len(self._vulnerabilites) != 0
99168

100169
def set_author(self, author: str):
170+
"""
171+
Set the author of this Component.
172+
173+
Args:
174+
author:
175+
`str` to set the author to
176+
177+
Returns:
178+
None
179+
"""
101180
self._author = author
102181

103182
def set_description(self, description: str):
183+
"""
184+
Set the description of this Component.
185+
186+
Args:
187+
description:
188+
`str` to set the description to
189+
190+
Returns:
191+
None
192+
"""
104193
self._description = description
105194

106195
def set_license(self, license_str: str):
196+
"""
197+
Set the license for this Component.
198+
199+
Args:
200+
license_str:
201+
`str` to set the license to
202+
203+
Returns:
204+
None
205+
"""
107206
self._license = license_str
108207

109208
def to_package_url(self) -> PackageURL:
110209
"""
111210
Return a PackageURL representation of this Component.
112211
113-
:return: PackageURL
212+
Returns:
213+
`packageurl.PackageURL` instance which represents this Component.
114214
"""""
115215
return PackageURL(
116216
type=PURL_TYPE_PREFIX,

cyclonedx/model/vulnerability.py

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,19 @@
2424

2525
"""
2626
This set of classes represents the data that is possible under the CycloneDX extension
27-
schema for Vulnerabilties (version 1.0).
27+
schema for Vulnerabilities (version 1.0).
2828
29-
See: https://github.com/CycloneDX/specification/blob/master/schema/ext/vulnerability-1.0.xsd
29+
.. note::
30+
See the CycloneDX Schema extension definition https://cyclonedx.org/ext/vulnerability/.
3031
"""
3132

3233

3334
class VulnerabilitySourceType(Enum):
3435
"""
35-
Represents <xs:simpleType name="scoreSourceType">
36+
Enum object that defines the permissible source types for a Vulnerability.
37+
38+
.. note::
39+
See `scoreSourceType` in https://github.com/CycloneDX/specification/blob/master/schema/ext/vulnerability-1.0.xsd
3640
"""
3741
CVSS_V2 = 'CVSSv2'
3842
CVSS_V3 = 'CVSSv3'
@@ -42,6 +46,17 @@ class VulnerabilitySourceType(Enum):
4246

4347
@staticmethod
4448
def get_from_vector(vector: str):
49+
"""
50+
Attempt to derive the correct SourceType from an attack vector.
51+
52+
For example, often attack vector strings are prefixed with the scheme in question - such
53+
that __CVSS:3.0/AV:L/AC:L/PR:N/UI:R/S:C/C:L/I:N/A:N__ would be the vector
54+
__AV:L/AC:L/PR:N/UI:R/S:C/C:L/I:N/A:N__ under the __CVSS 3__ scheme.
55+
56+
Returns:
57+
Always returns an instance of `VulnerabilitySourceType`. `VulnerabilitySourceType.OTHER` is returned if the
58+
scheme is not obvious or known to us.
59+
"""
4560
if vector.startswith('CVSS:3.'):
4661
return VulnerabilitySourceType.CVSS_V3
4762
elif vector.startswith('CVSS:2.'):
@@ -53,12 +68,13 @@ def get_from_vector(vector: str):
5368

5469
def get_localised_vector(self, vector: str) -> str:
5570
"""
56-
This method will remove any Source Scheme type from the supplied vector.
71+
This method will remove any Source Scheme type from the supplied vector, returning just the vector.
5772
58-
For example if VulnerabilitySourceType.OWASP
73+
.. Note::
74+
Currently supports CVSS 3.x, CVSS 2.x and OWASP schemes.
5975
60-
:param vector:
61-
:return:
76+
Returns:
77+
The vector without any scheme prefix as a `str`.
6278
"""
6379
if self == VulnerabilitySourceType.CVSS_V3 and vector.startswith('CVSS:3.'):
6480
return re.sub('^CVSS:3\\.\\d/?', '', vector)
@@ -74,7 +90,10 @@ def get_localised_vector(self, vector: str) -> str:
7490

7591
class VulnerabilitySeverity(Enum):
7692
"""
77-
Represents <xs:simpleType name="severityType">
93+
Enum object that defines the permissible severities for a Vulnerability.
94+
95+
.. note::
96+
See `severityType` in https://github.com/CycloneDX/specification/blob/master/schema/ext/vulnerability-1.0.xsd
7897
"""
7998
NONE = 'None'
8099
LOW = 'Low'
@@ -85,6 +104,15 @@ class VulnerabilitySeverity(Enum):
85104

86105
@staticmethod
87106
def get_from_cvss_scores(scores: tuple = None):
107+
"""
108+
Derives the Severity of a Vulnerability from it's declared CVSS scores.
109+
110+
Args:
111+
scores: A `tuple` of CVSS scores. CVSS scoring system allows for up to three separate scores.
112+
113+
Returns:
114+
Always returns an instance of `VulnerabilitySeverity`.
115+
"""
88116
if type(scores) is float:
89117
scores = (scores,)
90118

@@ -107,7 +135,10 @@ def get_from_cvss_scores(scores: tuple = None):
107135

108136
class VulnerabilityRating:
109137
"""
110-
Represents <xs:complexType name="scoreType">
138+
Class that models the `scoreType` complex element in the Vulnerability extension schema.
139+
140+
.. note::
141+
See `scoreType` in https://github.com/CycloneDX/specification/blob/master/schema/ext/vulnerability-1.0.xsd
111142
"""
112143
_score_base: float
113144
_score_impact: float
@@ -130,18 +161,48 @@ def __init__(self, score_base: float = None, score_impact: float = None, score_e
130161
self._vector = vector
131162

132163
def get_base_score(self) -> float:
164+
"""
165+
Get the base score of this VulnerabilityRating.
166+
167+
Returns:
168+
Declared base score of this VulnerabilityRating as `float`.
169+
"""
133170
return self._score_base
134171

135172
def get_impact_score(self) -> float:
173+
"""
174+
Get the impact score of this VulnerabilityRating.
175+
176+
Returns:
177+
Declared impact score of this VulnerabilityRating as `float`.
178+
"""
136179
return self._score_impact
137180

138181
def get_exploitability_score(self) -> float:
182+
"""
183+
Get the exploitability score of this VulnerabilityRating.
184+
185+
Returns:
186+
Declared exploitability score of this VulnerabilityRating as `float`.
187+
"""
139188
return self._score_exploitability
140189

141190
def get_severity(self) -> Union[VulnerabilitySeverity, None]:
191+
"""
192+
Get the severity score of this VulnerabilityRating.
193+
194+
Returns:
195+
Declared severity of this VulnerabilityRating as `VulnerabilitySeverity` or `None`.
196+
"""
142197
return self._severity
143198

144199
def get_method(self) -> Union[VulnerabilitySourceType, None]:
200+
"""
201+
Get the source method of this VulnerabilitySourceType.
202+
203+
Returns:
204+
Declared source method of this VulnerabilitySourceType as `VulnerabilitySourceType` or `None`.
205+
"""
145206
return self._method
146207

147208
def get_vector(self) -> Union[str, None]:

cyclonedx/output/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
#
1515
# SPDX-License-Identifier: Apache-2.0
1616

17+
"""
18+
Set of classes and methods for outputting our libraries internal Bom model to CycloneDX documents in varying formats
19+
and according to different versions of the CycloneDX schema standard.
20+
"""
21+
1722
import importlib
1823
import os
1924
from abc import ABC, abstractmethod

cyclonedx/parser/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
#
1515
# SPDX-License-Identifier: Apache-2.0
1616

17+
"""
18+
Set of classes and methods which allow for quick creation of a Bom instance from your environment or Python project.
19+
20+
Use a Parser instead of programmatically creating a Bom as a developer.
21+
"""
22+
1723
from abc import ABC
1824
from typing import List
1925

0 commit comments

Comments
 (0)