Skip to content

Commit 50ed891

Browse files
authored
Merge pull request #121 from fosslight/ossitem
Refactoring OSS Item classes
2 parents 597238b + 0ad96ce commit 50ed891

File tree

5 files changed

+129
-174
lines changed

5 files changed

+129
-174
lines changed

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ py-tlsh
88
pytz
99
XlsxWriter
1010
PyYAML
11-
fosslight_util~=1.4.47
11+
fosslight_util>=2.0.0
1212
dependency-check

src/fosslight_binary/_binary.py

Lines changed: 61 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,10 @@
22
# -*- coding: utf-8 -*-
33
# Copyright (c) 2020 LG Electronics Inc.
44
# SPDX-License-Identifier: Apache-2.0
5-
import hashlib
6-
import tlsh
7-
from io import open
5+
from fosslight_util.oss_item import FileItem
86

9-
_EXCLUDE_TRUE_VALUE = "Exclude"
10-
_TLSH_CHECKSUM_NULL = "0"
11-
12-
13-
class OssItem:
14-
name = ""
15-
version = ""
16-
license = ""
17-
dl_url = ""
18-
comment = ""
19-
exclude = False
20-
21-
def __init__(self, name, version, license, dl_url=""):
22-
self.name = name
23-
self.version = version
24-
self.license = license
25-
self.dl_url = dl_url
26-
self.exclude = False
27-
self.comment = ""
28-
29-
def set_comment(self, value):
30-
if self.comment:
31-
self.comment = f"{self.comment} / {value}"
32-
else:
33-
self.comment = value
34-
35-
def set_exclude(self, value):
36-
self.exclude = value
37-
38-
def get_comment(self):
39-
return self.comment
7+
EXCLUDE_TRUE_VALUE = "Exclude"
8+
TLSH_CHECKSUM_NULL = "0"
409

4110

4211
class VulnerabilityItem:
@@ -50,119 +19,84 @@ def __init__(self, file_path, id, url):
5019
self.nvd_url = url
5120

5221

53-
class BinaryItem:
54-
bin_name = ""
55-
binary_name_without_path = ""
56-
binary_strip_root = "" # Value of binary name column
57-
tlsh = _TLSH_CHECKSUM_NULL
58-
checksum = _TLSH_CHECKSUM_NULL
59-
oss_items = []
60-
vulnerability_items = []
61-
exclude = False
62-
comment = ""
63-
found_in_owasp = False
64-
22+
class BinaryItem(FileItem):
6523
def __init__(self, value):
24+
super().__init__("")
6625
self.exclude = False
67-
self.binary_strip_root = ""
68-
self.checksum = _TLSH_CHECKSUM_NULL
69-
self.tlsh = _TLSH_CHECKSUM_NULL
70-
self.oss_items = []
26+
self.source_name_or_path = ""
27+
self.checksum = TLSH_CHECKSUM_NULL
28+
self.tlsh = TLSH_CHECKSUM_NULL
7129
self.vulnerability_items = []
7230
self.binary_name_without_path = ""
73-
self.set_bin_name(value)
31+
self.bin_name_with_path = value
32+
self.found_in_owasp = False
33+
self.is_binary = True
7434

7535
def __del__(self):
7636
pass
7737

7838
def set_oss_items(self, new_oss_list, exclude=False, exclude_msg=""):
7939
if exclude:
8040
for oss in new_oss_list:
81-
oss.set_exclude(True)
82-
oss.set_comment(exclude_msg)
41+
oss.exclude = True
42+
oss.comment = exclude_msg
8343
# Append New input OSS
8444
self.oss_items.extend(new_oss_list)
8545

86-
def get_oss_items(self):
87-
return self.oss_items
88-
89-
def set_vulnerability_items(self, vul_list):
90-
if vul_list is not None:
91-
self.vulnerability_items.extend(vul_list)
92-
9346
def get_vulnerability_items(self):
9447
nvd_url = [vul_item.nvd_url for vul_item in self.vulnerability_items]
9548
return ", ".join(nvd_url)
9649

97-
def set_comment(self, value):
98-
if self.comment:
99-
self.comment = f"{self.comment} / {value}"
100-
else:
101-
self.comment = value
102-
103-
def set_bin_name(self, value):
104-
self.bin_name = value
105-
106-
def set_exclude(self, value):
107-
self.exclude = value
108-
109-
def set_checksum(self, value):
110-
self.checksum = value
111-
112-
def set_tlsh(self, value):
113-
self.tlsh = value
114-
115-
def get_comment(self):
116-
return self.comment
117-
11850
def get_print_binary_only(self):
119-
return (self.binary_strip_root + "\t" + self.checksum + "\t" + self.tlsh)
51+
return (self.source_name_or_path + "\t" + self.checksum + "\t" + self.tlsh)
12052

121-
def get_oss_report(self):
122-
comment = ""
123-
if len(self.oss_items) > 0:
53+
def get_print_array(self):
54+
items = []
55+
if self.oss_items:
12456
for oss in self.oss_items:
125-
exclude = _EXCLUDE_TRUE_VALUE if (self.exclude or oss.exclude) else ""
57+
lic = ",".join(oss.license)
58+
exclude = EXCLUDE_TRUE_VALUE if (self.exclude or oss.exclude) else ""
12659
nvd_url = self.get_vulnerability_items()
127-
128-
if self.comment:
129-
if oss.comment:
130-
comment = f"{self.comment} / {oss.comment}"
131-
else:
132-
comment = self.comment
133-
else:
134-
comment = oss.comment
135-
136-
yield [self.binary_strip_root, oss.name, oss.version,
137-
oss.license, oss.dl_url, '', '', exclude, comment,
138-
nvd_url, self.tlsh, self.checksum]
60+
items.append([self.source_name_or_path, oss.name, oss.version,
61+
lic, oss.download_location, oss.homepage,
62+
oss.copyright, exclude, oss.comment,
63+
nvd_url, self.tlsh, self.checksum])
13964
else:
140-
exclude = _EXCLUDE_TRUE_VALUE if self.exclude else ""
141-
yield [self.binary_strip_root, '',
142-
'', '', '', '', '', exclude, self.comment, '', self.tlsh, self.checksum]
143-
144-
def set_checksum_tlsh(self):
145-
self.checksum, self.tlsh, error, msg = get_checksum_and_tlsh(
146-
self.bin_name)
147-
return error, msg
148-
149-
150-
def get_checksum_and_tlsh(bin_with_path):
151-
checksum_value = _TLSH_CHECKSUM_NULL
152-
tlsh_value = _TLSH_CHECKSUM_NULL
153-
error_msg = ""
154-
error = False
155-
try:
156-
f = open(bin_with_path, "rb")
157-
byte = f.read()
158-
sha1_hash = hashlib.sha1(byte)
159-
checksum_value = str(sha1_hash.hexdigest())
160-
try:
161-
tlsh_value = str(tlsh.hash(byte))
162-
except:
163-
tlsh_value = _TLSH_CHECKSUM_NULL
164-
f.close()
165-
except Exception as ex:
166-
error_msg = f"(Error) Get_checksum, tlsh: {ex}"
167-
error = True
168-
return checksum_value, tlsh_value, error, error_msg
65+
exclude = EXCLUDE_TRUE_VALUE if self.exclude else ""
66+
items.append([self.source_name_or_path, '',
67+
'', '', '', '', '', exclude, self.comment, '',
68+
self.tlsh, self.checksum])
69+
return items
70+
71+
def get_print_json(self):
72+
items = []
73+
if self.oss_items:
74+
for oss in self.oss_items:
75+
json_item = {}
76+
json_item["name"] = oss.name
77+
json_item["version"] = oss.version
78+
79+
if self.source_name_or_path:
80+
json_item["source path"] = self.source_name_or_path
81+
if len(oss.license) > 0:
82+
json_item["license"] = oss.license
83+
if oss.download_location:
84+
json_item["download location"] = oss.download_location
85+
if oss.homepage:
86+
json_item["homepage"] = oss.homepage
87+
if oss.copyright:
88+
json_item["copyright text"] = oss.copyright
89+
if self.exclude or oss.exclude:
90+
json_item["exclude"] = True
91+
if oss.comment:
92+
json_item["comment"] = oss.comment
93+
items.append(json_item)
94+
else:
95+
json_item = {}
96+
if self.source_name_or_path:
97+
json_item["source path"] = self.source_name_or_path
98+
if self.exclude:
99+
json_item["exclude"] = True
100+
if self.comment:
101+
json_item["comment"] = self.comment
102+
return items

src/fosslight_binary/_binary_dao.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import psycopg2
99
import pandas as pd
1010
from urllib.parse import urlparse
11-
from ._binary import _TLSH_CHECKSUM_NULL, OssItem
11+
from ._binary import TLSH_CHECKSUM_NULL
12+
from fosslight_util.oss_item import OssItem
1213
import fosslight_util.constant as constant
1314

1415
columns = ['filename', 'pathname', 'checksum', 'tlshchecksum', 'ossname',
@@ -43,10 +44,10 @@ def get_oss_info_from_db(bin_info_list, dburl=""):
4344
if not item.found_in_owasp:
4445
oss_from_db = OssItem(row['ossname'], row['ossversion'], row['license'])
4546
bin_oss_items.append(oss_from_db)
46-
item.set_comment("Binary DB result")
4747

4848
if bin_oss_items:
4949
item.set_oss_items(bin_oss_items)
50+
item.comment = "Binary DB result"
5051

5152
disconnect_lge_bin_db()
5253
return bin_info_list, _cnt_auto_identified
@@ -97,14 +98,14 @@ def get_oss_info_by_tlsh_and_filename(file_name, checksum_value, tlsh_value):
9798
sql_statement_filename, ['tlshchecksum'])
9899
if df_result is None or len(df_result) <= 0:
99100
final_result_item = ""
100-
elif tlsh_value == _TLSH_CHECKSUM_NULL: # Couldn't get the tlsh of a file.
101+
elif tlsh_value == TLSH_CHECKSUM_NULL: # Couldn't get the tlsh of a file.
101102
final_result_item = ""
102103
else:
103104
matched_tlsh = ""
104105
matched_tlsh_diff = -1
105106
for row in df_result.tlshchecksum:
106107
try:
107-
if row != _TLSH_CHECKSUM_NULL:
108+
if row != TLSH_CHECKSUM_NULL:
108109
tlsh_diff = tlsh.diff(row, tlsh_value)
109110
if tlsh_diff <= 120: # MATCHED
110111
if (matched_tlsh_diff < 0) or (tlsh_diff < matched_tlsh_diff):

src/fosslight_binary/_jar_analysis.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
import os
99
import sys
1010
import fosslight_util.constant as constant
11-
from ._binary import BinaryItem, OssItem, VulnerabilityItem
11+
from ._binary import BinaryItem, VulnerabilityItem
12+
from fosslight_util.oss_item import OssItem
1213
from dependency_check import run as dependency_check_run
1314

1415

@@ -63,21 +64,21 @@ def merge_binary_list(owasp_items, vulnerability_items, bin_list):
6364
for key, value in owasp_items.items():
6465
found = False
6566
for bin in bin_list:
66-
if bin.binary_strip_root == key:
67+
if bin.source_name_or_path == key:
6768
for oss in value:
6869
if oss.name and oss.license:
6970
bin.found_in_owasp = True
7071
break
7172
bin.set_oss_items(value)
72-
if vulnerability_items is not None:
73-
bin.set_vulnerability_items(vulnerability_items.get(key))
73+
if vulnerability_items and vulnerability_items.get(key):
74+
bin.vulnerability_items.extend(vulnerability_items.get(key))
7475
found = True
7576
break
7677

7778
if not found:
7879
bin_item = BinaryItem(os.path.abspath(key))
7980
bin_item.binary_name_without_path = os.path.basename(key)
80-
bin_item.binary_strip_root = key
81+
bin_item.source_name_or_path = key
8182
bin_item.set_oss_items(value)
8283
not_found_bin.append(bin_item)
8384

@@ -261,7 +262,7 @@ def analyze_jar_file(path_to_find_bin, path_to_exclude):
261262

262263
if oss_name != "" or oss_ver != "" or oss_license != "" or oss_dl_url != "":
263264
oss = OssItem(oss_name, oss_ver, oss_license, oss_dl_url)
264-
oss.set_comment("OWASP result")
265+
oss.comment = "OWASP result"
265266

266267
remove_owasp_item = owasp_items.get(file_with_path)
267268
if remove_owasp_item:

0 commit comments

Comments
 (0)