Skip to content

Commit

Permalink
Merge pull request #105 from kuefmz/main
Browse files Browse the repository at this point in the history
Some small bugfixes
  • Loading branch information
JJ-Author authored Oct 21, 2024
2 parents b62f415 + 8615680 commit 97bbc24
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 59 deletions.
57 changes: 15 additions & 42 deletions ontologytimemachine/custom_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import sys
from ontologytimemachine.utils.config import (
HttpsInterception,
ClientConfigViaProxyAuth,
logger,
ClientConfigViaProxyAuth
)


Expand All @@ -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
Expand All @@ -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):
Expand Down Expand Up @@ -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

Expand All @@ -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]]

Expand Down
21 changes: 13 additions & 8 deletions ontologytimemachine/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion ontologytimemachine/utils/proxy_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}")
Expand Down Expand Up @@ -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(
Expand Down
8 changes: 0 additions & 8 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 97bbc24

Please sign in to comment.