Skip to content

Commit 5b1e0e5

Browse files
committed
Add network and DB connection status logging
1 parent b758d29 commit 5b1e0e5

File tree

1 file changed

+58
-37
lines changed

1 file changed

+58
-37
lines changed

src/fosslight_binary/_binary_dao.py

Lines changed: 58 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,47 +19,67 @@
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

@@ -68,7 +89,7 @@ def get_connection_string(dburl):
6889
user_dburl = True
6990
if dburl == "" or dburl is None:
7091
user_dburl = False
71-
dburl = "postgresql://bin_analysis_script_user:[email protected]:5432/bat"
92+
dburl = DB_URL_DEFAULT
7293
try:
7394
if user_dburl:
7495
logger.debug("DB URL:" + dburl)
@@ -83,7 +104,7 @@ def get_connection_string(dburl):
83104
if user_dburl:
84105
logger.warning(f"(Minor) Failed to parsing db url : {ex}")
85106

86-
return connection_string
107+
return connection_string, dbc
87108

88109

89110
def get_oss_info_by_tlsh_and_filename(file_name, checksum_value, tlsh_value):

0 commit comments

Comments
 (0)