Skip to content

Commit

Permalink
transform config to dict from tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
Jenifer Tabita Ciuciu-Kiss committed Oct 4, 2024
1 parent 7b4c919 commit 2d3ceaa
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 29 deletions.
49 changes: 23 additions & 26 deletions ontologytimemachine/custom_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
IP = '0.0.0.0'
PORT = '8899'

config = ({'format': 'turtle', 'precedence': 'enforcedPriority', 'patchAcceptUpstream': False}, 'originalFailoverLiveLatest', False, 'all', False, True, None, None)
config = None

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
Expand All @@ -25,57 +25,54 @@ class OntologyTimeMachinePlugin(HttpProxyBasePlugin):
def __init__(self, *args, **kwargs):
logger.info('Init')
super().__init__(*args, **kwargs)
(self.ontoFormat, self.ontoVersion, self.restrictedAccess,
self.httpsInterception, self.disableRemovingRedirects,
self.forward_headers, self.timestamp, self.manifest) = config
self.config = config

def before_upstream_connection(self, request: HttpParser):
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}')
wrapped_request = HttpRequestWrapper(request)

if wrapped_request.is_connect_request():
logger.info(f'HTTPS interception mode: {self.httpsInterception}')
logger.info(f'HTTPS interception mode: {self.config["httpsInterception"]}')
# Only intercept if interception is enabled
# Move this to the utils
if if_intercept_host(self.httpsInterception):
if if_intercept_host(self.config["httpsInterception"]):
logger.info('HTTPS interception is on, forwardig the request')
return request
else:
logger.info('HTTPS interception is turned off')
return None

# If only ontology mode, return None in all other cases
if do_deny_request_due_non_archivo_ontology_uri(wrapped_request, self.restrictedAccess):
logger.warning('Request denied: not an ontology request and only ontologies mode is enabled')
self.queue_response(mock_response_403)
return None

if is_archivo_ontology_request(wrapped_request):
logger.debug('The request is for an ontology')
response = proxy_logic(wrapped_request, self.ontoFormat, self.ontoVersion, self.disableRemovingRedirects, self.timestamp, self.manifest)
self.queue_response(response)
return None

return request
# # If only ontology mode, return None in all other cases
# response = get_response_from_request(wrapped_request, config)
# if response:
# self.queue_response(mock_response_403)
# return None
# return request

def handle_client_request(self, request: 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}')

wrapped_request = HttpRequestWrapper(request)
if wrapped_request.is_connect_request():
return request
return request

if not do_deny_request_due_non_archivo_ontology_uri(wrapped_request):
if do_deny_request_due_non_archivo_ontology_uri(wrapped_request, config["restrictedAccess"]):
logger.info('The requested IRI is not part of DBpedia Archivo')
return request

response = proxy_logic(wrapped_request, self.ontoFormat, self.ontoVersion, self.disableRemovingRedirects, self.timestamp, self.manifest)
return request

print("proxy logic")
response = proxy_logic(wrapped_request,
config["ontoFormat"],
config["ontoVersion"],
config["disableRemovingRedirects"],
config["timestamp"],
config["manifest"])
self.queue_response(response)

return None

def handle_upstream_chunk(self, chunk: memoryview):
return chunk

Expand All @@ -99,7 +96,7 @@ def queue_response(self, response):
sys.argv = [sys.argv[0]]

# check it https interception is enabled
if config[3] != 'none':
if config["httpsInterception"] != 'none':
sys.argv += [
'--ca-key-file', 'ca-key.pem',
'--ca-cert-file', 'ca-cert.pem',
Expand Down
27 changes: 25 additions & 2 deletions ontologytimemachine/utils/proxy_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ontologytimemachine.utils.utils import parse_accept_header_with_priority
from ontologytimemachine.utils.utils import dbpedia_api, passthrough_status_codes
from ontologytimemachine.utils.mock_responses import mock_response_500
from ontologytimemachine.utils.mock_responses import mock_response_404
from ontologytimemachine.utils.mock_responses import mock_response_404, mock_response_403
from typing import Set, Tuple


Expand All @@ -18,13 +18,17 @@


def if_intercept_host(https_intercept):
if https_intercept in ['all']:
print(https_intercept)
if https_intercept in ['none', 'all']:
return True
elif https_intercept in ['block']:
return False
return False


def do_deny_request_due_non_archivo_ontology_uri (wrapped_request, only_ontologies):
if only_ontologies:
print(only_ontologies)
is_archivo_ontology = is_archivo_ontology_request(wrapped_request)
if not is_archivo_ontology:
return True
Expand All @@ -34,6 +38,7 @@ def do_deny_request_due_non_archivo_ontology_uri (wrapped_request, only_ontologi
def load_archivo_urls() -> None:
"""Load the archivo URLs into the global variable if not already loaded."""
global ARCHIVO_PARSED_URLS
print(ARCHIVO_PARSED_URLS)
if not ARCHIVO_PARSED_URLS: # Load only if the set is empty
logger.info('Loading archivo ontologies from file')
with open('ontologytimemachine/utils/archivo_ontologies.txt', 'r') as file:
Expand All @@ -42,6 +47,22 @@ def load_archivo_urls() -> None:
}


def get_response_from_request(wrapped_request, config):
if do_deny_request_due_non_archivo_ontology_uri(wrapped_request, config["restrictedAccess"]):
logger.warning('Request denied: not an ontology request and only ontologies mode is enabled')
return mock_response_403

if is_archivo_ontology_request(wrapped_request):
logger.debug('The request is for an ontology')
response = proxy_logic(wrapped_request,
config["ontoFormat"],
config["ontoVersion"],
config["disableRemovingRedirects"],
config["timestamp"],
config["manifest"])
return response


def is_archivo_ontology_request(wrapped_request) -> bool:
"""Check if the requested ontology is in the archivo."""
logger.info('Check if the requested ontology is in archivo')
Expand All @@ -52,6 +73,8 @@ def is_archivo_ontology_request(wrapped_request) -> bool:
# Extract the request's host and path
request_host = wrapped_request.get_request().host.decode('utf-8')
request_path = wrapped_request.get_request().path.decode('utf-8')

print((request_host, request_path) in ARCHIVO_PARSED_URLS)

# Check if the (host, path) tuple exists in ARCHIVO_PARSED_URLS
return (request_host, request_path) in ARCHIVO_PARSED_URLS
Expand Down
14 changes: 13 additions & 1 deletion ontologytimemachine/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,19 @@ def parse_arguments():
logger.info(f'HTTPS Interception: {args.httpsInterception}')
logger.info(f'Inspect Redirects: {args.disableRemovingRedirects}')
logger.info(f'Forward Headers: {args.forwardHeaders}')
return ontoFormat, args.ontoVersion, args.restrictedAccess, args.httpsInterception, args.disableRemovingRedirects, args.forwardHeaders, timestamp, manifest

config = {
"ontoFormat": ontoFormat,
"ontoVersion": args.ontoVersion,
"restrictedAccess": args.restrictedAccess,
"httpsInterception": args.httpsInterception,
"disableRemovingRedirects": args.disableRemovingRedirects,
"forward_headers": args.forwardHeaders,
"timestamp": timestamp,
"manifest": manifest,
}

return config


def get_mime_type(format='turtle'):
Expand Down

0 comments on commit 2d3ceaa

Please sign in to comment.