diff --git a/ontologytimemachine/custom_proxy.py b/ontologytimemachine/custom_proxy.py index 3e93056..145b1f4 100644 --- a/ontologytimemachine/custom_proxy.py +++ b/ontologytimemachine/custom_proxy.py @@ -19,8 +19,7 @@ import sys from ontologytimemachine.utils.config import ( HttpsInterception, - ClientConfigViaProxyAuth, - logger, + ClientConfigViaProxyAuth ) @@ -36,36 +35,22 @@ def __init__(self, *args, **kwargs): logger.info(f"Init - Object ID: {id(self)}") super().__init__(*args, **kwargs) self.config = config + logger.debug('debug') logger.info(f"Config: {self.config}") def before_upstream_connection(self, request: HttpParser) -> HttpParser | None: - # self.client.config = None + # self.client.config = QUOTE_NONE logger.info("Before upstream connection hook") - logger.info( - f"Request method: {request.method} - Request host: {request.host} - Request path: {request.path} - Request headers: {request.headers}" - ) + logger.info(f"Request method: {request.method} - Request host: {request.host} - Request path: {request.path} - Request headers: {request.headers}") wrapped_request = HttpRequestWrapper(request) - if ( - self.config.clientConfigViaProxyAuth == ClientConfigViaProxyAuth.REQUIRED - or self.config.clientConfigViaProxyAuth == ClientConfigViaProxyAuth.OPTIONAL - ): + if (self.config.clientConfigViaProxyAuth == ClientConfigViaProxyAuth.REQUIRED or self.config.clientConfigViaProxyAuth == ClientConfigViaProxyAuth.OPTIONAL): config_from_auth = evaluate_configuration(wrapped_request, self.config) - if ( - not config_from_auth - and self.config.clientConfigViaProxyAuth - == ClientConfigViaProxyAuth.REQUIRED - ): - logger.info( - "Client configuration via proxy auth is required btu configuration is not provided, return 500." - ) + if (not config_from_auth and self.config.clientConfigViaProxyAuth == ClientConfigViaProxyAuth.REQUIRED): + logger.info( "Client configuration via proxy auth is required btu configuration is not provided, return 500.") self.queue_response(mock_response_500) return None - if ( - not config_from_auth - and self.config.clientConfigViaProxyAuth - == ClientConfigViaProxyAuth.OPTIONAL - ): + if (not config_from_auth and self.config.clientConfigViaProxyAuth == ClientConfigViaProxyAuth.OPTIONAL): logger.info("Auth configuration is optional, not provided.") if config_from_auth and not hasattr(self.client, "config"): self.client.config = config_from_auth @@ -83,9 +68,7 @@ def before_upstream_connection(self, request: HttpParser) -> HttpParser | None: config = self.config if wrapped_request.is_connect_request(): - logger.info( - f"Handling CONNECT request: configured HTTPS interception mode: {config.httpsInterception}" - ) + logger.info(f"Handling CONNECT request: configured HTTPS interception mode: {config.httpsInterception}") # Check whether to allow CONNECT requests since they can impose a security risk if not do_block_CONNECT_request(config): @@ -121,33 +104,22 @@ def do_intercept(self, _request: HttpParser) -> bool: logger.info("Intercepting no HTTPS requests") return False elif config.httpsInterception == HttpsInterception.BLOCK: - logger.error( - "Reached code block for interception decision in block mode which should have been blocked before" - ) + logger.error("Reached code block for interception decision in block mode which should have been blocked before") # this should actually be not triggered as the CONNECT request should have been blocked before return False elif config.httpsInterception == HttpsInterception.ARCHIVO: if is_archivo_ontology_request(wrapped_request): - logger.info( - "Intercepting HTTPS request since it is an Archivo ontology request" - ) + logger.info("Intercepting HTTPS request since it is an Archivo ontology request") return True - logger.info( - "No Interception of HTTPS request since it is NOT an Archivo ontology request" - ) + logger.info("No Interception of HTTPS request since it is NOT an Archivo ontology request") return False else: - logger.info( - "Unknown Option for httpsInterception: %s -> fallback to no interception", - self.config.httpsInterception, - ) + logger.info("Unknown Option for httpsInterception: %s -> fallback to no interception", self.config.httpsInterception,) return False def handle_client_request(self, request: HttpParser) -> HttpParser: logger.info("Handle client request hook") - logger.info( - f"Request method: {request.method} - Request host: {request.host} - Request path: {request.path} - Request headers: {request.headers}" - ) + logger.info(f"Request method: {request.method} - Request host: {request.host} - Request path: {request.path} - Request headers: {request.headers}") return request @@ -172,6 +144,7 @@ def queue_response(self, response): if __name__ == "__main__": config = parse_arguments() + from ontologytimemachine.utils.config import logger sys.argv = [sys.argv[0]] diff --git a/ontologytimemachine/utils/config.py b/ontologytimemachine/utils/config.py index e815f1e..e49ea27 100644 --- a/ontologytimemachine/utils/config.py +++ b/ontologytimemachine/utils/config.py @@ -223,14 +223,19 @@ def parse_arguments(config_str: str = "") -> Config: # print the default configuration with all nested members # print(default_cfg) # TODO remove - - if args.logLevel != LogLevel.INFO: - logging.basicConfig( - level=args.logLevel.value, - format="%(asctime)s - %(levelname)s - %(message)s", - ) - logger = logging.getLogger(__name__) - logger.info(f"Logging level set to: {args.logLevel}") + global logger + if args.logLevel == LogLevel.DEBUG: + logger.setLevel(logging.DEBUG) + logger.debug(f"Logging level set to: {args.logLevel}") + elif args.logLevel == LogLevel.WARNING: + logger.setLevel(logging.WARNING) + logger.warning(f"Logging level set to: {args.logLevel}") + elif args.logLevel == LogLevel.ERROR: + logger.setLevel(logging.ERROR) + logger.error(f"Logging level set to: {args.logLevel}") + elif args.logLevel == LogLevel.CRITICAL: + logger.setLevel(logging.CRITICAL) + logger.critical(f"Logging level set to: {args.logLevel}") # Initialize the Config class with parsed arguments config = Config( diff --git a/ontologytimemachine/utils/proxy_logic.py b/ontologytimemachine/utils/proxy_logic.py index 39d0fc2..275511f 100644 --- a/ontologytimemachine/utils/proxy_logic.py +++ b/ontologytimemachine/utils/proxy_logic.py @@ -153,6 +153,7 @@ def request_ontology( url=url, headers=headers, allow_redirects=allow_redirects, timeout=5 ) logger.info("Successfully fetched ontology") + print(response.content) return response except Exception as e: logger.error(f"Error fetching original ontology: {e}") @@ -221,7 +222,7 @@ def fetch_failover(wrapped_request, headers, disableRemovingRedirects): if response_mime_type in requested_mimetypes: return original_response else: - logging.info(f"The returned type is not the same as the requested one") + logger.info(f"The returned type is not the same as the requested one") return fetch_latest_archived(wrapped_request, headers) else: logger.info( diff --git a/tests/test_config.py b/tests/test_config.py index ef4d31c..db31947 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -11,18 +11,10 @@ def test_parse_arguments(self): "test", "--ontoFormat", "turtle", - "--ontoPrecedence", - "enforcedPriority", - "--patchAcceptUpstream", - "False", "--ontoVersion", "original", "--httpsInterception", "none", - "--disableRemovingRedirects", - "False", - "--logLevel", - "info", ] sys.argv = test_args config = parse_arguments()