-
Notifications
You must be signed in to change notification settings - Fork 5
Add network and DB connection status logging #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ | |
| import logging | ||
| import psycopg2 | ||
| import pandas as pd | ||
| import socket | ||
| from urllib.parse import urlparse | ||
| from ._binary import TLSH_CHECKSUM_NULL | ||
| from fosslight_util.oss_item import OssItem | ||
|
|
@@ -18,57 +19,78 @@ | |
| conn = "" | ||
| cur = "" | ||
| logger = logging.getLogger(constant.LOGGER_NAME) | ||
| DB_URL_DEFAULT = "postgresql://bin_analysis_script_user:[email protected]:5432/bat" | ||
|
|
||
|
|
||
| def get_oss_info_from_db(bin_info_list, dburl=""): | ||
| _cnt_auto_identified = 0 | ||
| conn_str = get_connection_string(dburl) | ||
| connect_to_lge_bin_db(conn_str) | ||
|
|
||
| if conn != "" and cur != "": | ||
| for item in bin_info_list: | ||
| bin_oss_items = [] | ||
| tlsh_value = item.tlsh | ||
| checksum_value = item.checksum | ||
| bin_file_name = item.binary_name_without_path | ||
|
|
||
| df_result = get_oss_info_by_tlsh_and_filename( | ||
| bin_file_name, checksum_value, tlsh_value) | ||
| if df_result is not None and len(df_result) > 0: | ||
| _cnt_auto_identified += 1 | ||
| # Initialize the saved contents at .jar analyzing only once | ||
| if not item.found_in_owasp and item.oss_items: | ||
| item.oss_items = [] | ||
|
|
||
| for idx, row in df_result.iterrows(): | ||
| if not item.found_in_owasp: | ||
| oss_from_db = OssItem(row['ossname'], row['ossversion'], row['license']) | ||
|
|
||
| if bin_oss_items: | ||
| if not any(oss_item.name == oss_from_db.name | ||
| and oss_item.version == oss_from_db.version | ||
| and oss_item.license == oss_from_db.license | ||
| for oss_item in bin_oss_items): | ||
| conn_str, dbc = get_connection_string(dburl) | ||
| # DB URL에서 host 추출 | ||
| try: | ||
| db_host = dbc.hostname | ||
| except Exception as ex: | ||
| logger.warning(f"Failed to parse DB URL for host: {ex}") | ||
| db_host = None | ||
|
|
||
| is_internal = False | ||
| if db_host: | ||
| try: | ||
| # DNS lookup 시도 | ||
| socket.gethostbyname(db_host) | ||
| is_internal = True | ||
| except Exception: | ||
| is_internal = False | ||
|
|
||
| if is_internal: | ||
| connect_to_lge_bin_db(conn_str) | ||
| if conn != "" and cur != "": | ||
| for item in bin_info_list: | ||
| bin_oss_items = [] | ||
| tlsh_value = item.tlsh | ||
| checksum_value = item.checksum | ||
| bin_file_name = item.binary_name_without_path | ||
|
|
||
| df_result = get_oss_info_by_tlsh_and_filename( | ||
| bin_file_name, checksum_value, tlsh_value) | ||
| if df_result is not None and len(df_result) > 0: | ||
| _cnt_auto_identified += 1 | ||
| # Initialize the saved contents at .jar analyzing only once | ||
| if not item.found_in_owasp and item.oss_items: | ||
| item.oss_items = [] | ||
|
|
||
| for idx, row in df_result.iterrows(): | ||
| if not item.found_in_owasp: | ||
| oss_from_db = OssItem(row['ossname'], row['ossversion'], row['license']) | ||
|
|
||
| if bin_oss_items: | ||
| if not any(oss_item.name == oss_from_db.name | ||
| and oss_item.version == oss_from_db.version | ||
| and oss_item.license == oss_from_db.license | ||
| for oss_item in bin_oss_items): | ||
| bin_oss_items.append(oss_from_db) | ||
| else: | ||
| bin_oss_items.append(oss_from_db) | ||
| else: | ||
| bin_oss_items.append(oss_from_db) | ||
|
|
||
| if bin_oss_items: | ||
| item.set_oss_items(bin_oss_items) | ||
| item.comment = "Binary DB result" | ||
| item.found_in_binary = True | ||
|
|
||
| disconnect_lge_bin_db() | ||
| if bin_oss_items: | ||
| item.set_oss_items(bin_oss_items) | ||
| item.comment = "Binary DB result" | ||
| item.found_in_binary = True | ||
| else: | ||
| logger.warning(f"Internal network detected, but DB connection to '{db_host}' failed. Skipping DB query.") | ||
| disconnect_lge_bin_db() | ||
| else: | ||
| logger.debug(f"Binary DB host '{db_host}' is not reachable. Skipping DB query.") | ||
| return bin_info_list, _cnt_auto_identified | ||
|
|
||
|
|
||
| def get_connection_string(dburl): | ||
| # dburl format : 'postgresql://username:password@host:port/database_name' | ||
| connection_string = "" | ||
| user_dburl = True | ||
| dbc = "" | ||
| if dburl == "" or dburl is None: | ||
| user_dburl = False | ||
| dburl = "postgresql://bin_analysis_script_user:[email protected]:5432/bat" | ||
| dburl = DB_URL_DEFAULT | ||
| try: | ||
| if user_dburl: | ||
| logger.debug("DB URL:" + dburl) | ||
|
|
@@ -83,7 +105,7 @@ def get_connection_string(dburl): | |
| if user_dburl: | ||
| logger.warning(f"(Minor) Failed to parsing db url : {ex}") | ||
|
|
||
| return connection_string | ||
| return connection_string, dbc | ||
|
|
||
|
|
||
| def get_oss_info_by_tlsh_and_filename(file_name, checksum_value, tlsh_value): | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.