-
-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathcomponent.py
More file actions
278 lines (223 loc) · 7.65 KB
/
component.py
File metadata and controls
278 lines (223 loc) · 7.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# encoding: utf-8
# This file is part of CycloneDX Python Lib
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) OWASP Foundation. All Rights Reserved.
from enum import Enum
from os.path import exists
from packageurl import PackageURL
from typing import List
from . import HashAlgorithm, HashType, sha1sum
from .vulnerability import Vulnerability
class ComponentType(Enum):
"""
Enum object that defines the permissible 'types' for a Component according to the CycloneDX schema.
.. note::
See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.3/#type_classification
"""
APPLICATION = 'application'
CONTAINER = 'container'
DEVICE = 'device'
FILE = 'file'
FIRMWARE = 'firmware'
FRAMEWORK = 'framework'
LIBRARY = 'library'
OPERATING_SYSTEM = 'operating-system'
class Component:
"""
This is our internal representation of a Component within a Bom.
.. note::
See the CycloneDX Schema definition: https://cyclonedx.org/docs/1.3/#type_component
"""
_type: ComponentType
_package_url_type: str
_name: str
_version: str
_qualifiers: str
_author: str = None
_description: str = None
_license: str = None
_hashes: List[HashType] = []
_vulnerabilites: List[Vulnerability] = []
@staticmethod
def for_file(absolute_file_path: str, path_for_bom: str = None):
"""
Helper method to create a Component that represents the provided local file as a Component.
Args:
absolute_file_path:
Absolute path to the file you wish to represent
path_for_bom:
Optionally, if supplied this is the path that will be used to identify the file in the BOM
Returns:
`Component` representing the supplied file
"""
if not exists(absolute_file_path):
raise FileExistsError('Supplied file path \'{}\' does not exist'.format(absolute_file_path))
sha1_hash: str = sha1sum(filename=absolute_file_path)
return Component(
name=path_for_bom if path_for_bom else absolute_file_path,
version='0.0.0-{}'.format(sha1_hash[0:12]),
hashes=[
HashType(algorithm=HashAlgorithm.SHA_1, hash_value=sha1_hash)
],
component_type=ComponentType.FILE,
package_url_type='generic'
)
def __init__(self, name: str, version: str, qualifiers: str = None, hashes: List[HashType] = [],
component_type: ComponentType = ComponentType.LIBRARY, package_url_type: str = 'pypi'):
self._name = name
self._version = version
self._type = component_type
self._qualifiers = qualifiers
self._hashes = hashes
self._vulnerabilites = []
self._package_url_type = package_url_type
def add_hash(self, hash: HashType):
"""
Adds a hash that pins/identifies this Component.
Args:
hash:
`HashType` instance
"""
self._hashes.append(hash)
def add_vulnerability(self, vulnerability: Vulnerability):
"""
Add a Vulnerability to this Component.
Args:
vulnerability:
`cyclonedx.model.vulnerability.Vulnerability` instance to add to this Component.
Returns:
None
"""
self._vulnerabilites.append(vulnerability)
def get_author(self) -> str:
"""
Get the author of this Component.
Returns:
Declared author of this Component as `str`.
"""
return self._author
def get_description(self) -> str:
"""
Get the description of this Component.
Returns:
Declared description of this Component as `str`.
"""
return self._description
def get_hashes(self) -> List[HashType]:
"""
List of cryptographic hashes that identify this Component.
Returns:
`List` of `HashType` objects where there are any hashes, else an empty `List`.
"""
return self._hashes
def get_license(self) -> str:
"""
Get the license of this Component.
Returns:
Declared license of this Component as `str`.
"""
return self._license
def get_name(self) -> str:
"""
Get the name of this Component.
Returns:
Declared name of this Component as `str`.
"""
return self._name
def get_purl(self) -> str:
"""
Get the PURL for this Component.
Returns:
PackageURL that reflects this Component as `str`.
"""
base_purl = 'pkg:{}/{}@{}'.format(self._package_url_type, self._name, self._version)
if self._qualifiers:
base_purl = '{}?{}'.format(base_purl, self._qualifiers)
return base_purl
def get_type(self) -> ComponentType:
"""
Get the type of this Component.
Returns:
Declared type of this Component as `ComponentType`.
"""
return self._type
def get_version(self) -> str:
"""
Get the version of this Component.
Returns:
Declared version of this Component as `str`.
"""
return self._version
def get_vulnerabilities(self) -> List[Vulnerability]:
"""
Get all the Vulnerabilities for this Component.
Returns:
List of `Vulnerability` objects assigned to this Component.
"""
return self._vulnerabilites
def has_vulnerabilities(self) -> bool:
"""
Does this Component have any vulnerabilities?
Returns:
`True` if this Component has 1 or more vulnerabilities, `False` otherwise.
"""
return len(self._vulnerabilites) != 0
def set_author(self, author: str):
"""
Set the author of this Component.
Args:
author:
`str` to set the author to
Returns:
None
"""
self._author = author
def set_description(self, description: str):
"""
Set the description of this Component.
Args:
description:
`str` to set the description to
Returns:
None
"""
self._description = description
def set_license(self, license_str: str):
"""
Set the license for this Component.
Args:
license_str:
`str` to set the license to
Returns:
None
"""
self._license = license_str
def to_package_url(self) -> PackageURL:
"""
Return a PackageURL representation of this Component.
Returns:
`packageurl.PackageURL` instance which represents this Component.
"""""
return PackageURL(
type=self._package_url_type,
name=self._name,
version=self._version,
qualifiers=self._qualifiers
)
def __eq__(self, other):
return other.get_purl() == self.get_purl()
def __repr__(self):
return '<Component {}={}>'.format(self._name, self._version)