Skip to content

Commit 10b0aec

Browse files
committed
Add network and DB connection status logging
1 parent b758d29 commit 10b0aec

File tree

1 file changed

+59
-37
lines changed

1 file changed

+59
-37
lines changed

src/fosslight_binary/_binary_dao.py

Lines changed: 59 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import psycopg2
99
import pandas as pd
10+
import socket
1011
from urllib.parse import urlparse
1112
from ._binary import TLSH_CHECKSUM_NULL
1213
from fosslight_util.oss_item import OssItem
@@ -18,57 +19,78 @@
1819
conn = ""
1920
cur = ""
2021
logger = logging.getLogger(constant.LOGGER_NAME)
22+
DB_URL_DEFAULT = "postgresql://bin_analysis_script_user:[email protected]:5432/bat"
2123

2224

2325
def get_oss_info_from_db(bin_info_list, dburl=""):
2426
_cnt_auto_identified = 0
25-
conn_str = get_connection_string(dburl)
26-
connect_to_lge_bin_db(conn_str)
27-
28-
if conn != "" and cur != "":
29-
for item in bin_info_list:
30-
bin_oss_items = []
31-
tlsh_value = item.tlsh
32-
checksum_value = item.checksum
33-
bin_file_name = item.binary_name_without_path
34-
35-
df_result = get_oss_info_by_tlsh_and_filename(
36-
bin_file_name, checksum_value, tlsh_value)
37-
if df_result is not None and len(df_result) > 0:
38-
_cnt_auto_identified += 1
39-
# Initialize the saved contents at .jar analyzing only once
40-
if not item.found_in_owasp and item.oss_items:
41-
item.oss_items = []
42-
43-
for idx, row in df_result.iterrows():
44-
if not item.found_in_owasp:
45-
oss_from_db = OssItem(row['ossname'], row['ossversion'], row['license'])
46-
47-
if bin_oss_items:
48-
if not any(oss_item.name == oss_from_db.name
49-
and oss_item.version == oss_from_db.version
50-
and oss_item.license == oss_from_db.license
51-
for oss_item in bin_oss_items):
27+
conn_str, dbc = get_connection_string(dburl)
28+
# DB URL에서 host 추출
29+
try:
30+
db_host = dbc.hostname
31+
except Exception as ex:
32+
logger.warning(f"Failed to parse DB URL for host: {ex}")
33+
db_host = None
34+
35+
is_internal = False
36+
if db_host:
37+
try:
38+
# DNS lookup 시도
39+
socket.gethostbyname(db_host)
40+
is_internal = True
41+
except Exception:
42+
is_internal = False
43+
44+
if is_internal:
45+
connect_to_lge_bin_db(conn_str)
46+
if conn != "" and cur != "":
47+
for item in bin_info_list:
48+
bin_oss_items = []
49+
tlsh_value = item.tlsh
50+
checksum_value = item.checksum
51+
bin_file_name = item.binary_name_without_path
52+
53+
df_result = get_oss_info_by_tlsh_and_filename(
54+
bin_file_name, checksum_value, tlsh_value)
55+
if df_result is not None and len(df_result) > 0:
56+
_cnt_auto_identified += 1
57+
# Initialize the saved contents at .jar analyzing only once
58+
if not item.found_in_owasp and item.oss_items:
59+
item.oss_items = []
60+
61+
for idx, row in df_result.iterrows():
62+
if not item.found_in_owasp:
63+
oss_from_db = OssItem(row['ossname'], row['ossversion'], row['license'])
64+
65+
if bin_oss_items:
66+
if not any(oss_item.name == oss_from_db.name
67+
and oss_item.version == oss_from_db.version
68+
and oss_item.license == oss_from_db.license
69+
for oss_item in bin_oss_items):
70+
bin_oss_items.append(oss_from_db)
71+
else:
5272
bin_oss_items.append(oss_from_db)
53-
else:
54-
bin_oss_items.append(oss_from_db)
55-
56-
if bin_oss_items:
57-
item.set_oss_items(bin_oss_items)
58-
item.comment = "Binary DB result"
59-
item.found_in_binary = True
6073

61-
disconnect_lge_bin_db()
74+
if bin_oss_items:
75+
item.set_oss_items(bin_oss_items)
76+
item.comment = "Binary DB result"
77+
item.found_in_binary = True
78+
else:
79+
logger.warning(f"Internal network detected, but DB connection to '{db_host}' failed. Skipping DB query.")
80+
disconnect_lge_bin_db()
81+
else:
82+
logger.debug(f"Binary DB host '{db_host}' is not reachable. Skipping DB query.")
6283
return bin_info_list, _cnt_auto_identified
6384

6485

6586
def get_connection_string(dburl):
6687
# dburl format : 'postgresql://username:password@host:port/database_name'
6788
connection_string = ""
6889
user_dburl = True
90+
dbc = ""
6991
if dburl == "" or dburl is None:
7092
user_dburl = False
71-
dburl = "postgresql://bin_analysis_script_user:[email protected]:5432/bat"
93+
dburl = DB_URL_DEFAULT
7294
try:
7395
if user_dburl:
7496
logger.debug("DB URL:" + dburl)
@@ -83,7 +105,7 @@ def get_connection_string(dburl):
83105
if user_dburl:
84106
logger.warning(f"(Minor) Failed to parsing db url : {ex}")
85107

86-
return connection_string
108+
return connection_string, dbc
87109

88110

89111
def get_oss_info_by_tlsh_and_filename(file_name, checksum_value, tlsh_value):

0 commit comments

Comments
 (0)