Skip to content

Commit

Permalink
Merge pull request #64 from CanDIG/daisieh/logging
Browse files Browse the repository at this point in the history
DIG-1719: switch to candigv2-logging module
  • Loading branch information
daisieh authored Aug 16, 2024
2 parents 5d5dbb9 + 900a137 commit 16fd64d
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 102 deletions.
64 changes: 0 additions & 64 deletions candig_federation/apilog.py

This file was deleted.

13 changes: 7 additions & 6 deletions candig_federation/authz.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from flask import Flask
import authx.auth
import os
from candigv2_logging.logging import CanDIGLogger


logger = CanDIGLogger(__file__)


app = Flask(__name__)
Expand All @@ -9,8 +13,7 @@

def is_testing(request):
if request.headers.get("Test_Key") == TEST_KEY:
print("WARNING: TEST MODE, AUTHORIZATION IS DISABLED")
app.logger.warning("WARNING: TEST MODE, AUTHORIZATION IS DISABLED")
logger.warning("TEST MODE, AUTHORIZATION IS DISABLED")
return True


Expand All @@ -19,14 +22,12 @@ def is_site_admin(request):
Is the user associated with the token a site admin?
"""
if request.headers.get("Test_Key") == TEST_KEY:
print("WARNING: TEST MODE, AUTHORIZATION IS DISABLED")
app.logger.warning("WARNING: TEST MODE, AUTHORIZATION IS DISABLED")
logger.warning("TEST MODE, AUTHORIZATION IS DISABLED")
return True # no auth
if "Authorization" in request.headers:
try:
return authx.auth.is_site_admin(request)
except Exception as e:
print(f"Couldn't authorize site_admin: {type(e)} {str(e)}")
app.logger.warning(f"Couldn't authorize site_admin: {type(e)} {str(e)}")
logger.error(f"Couldn't authorize site_admin: {type(e)} {str(e)}")
return False
return False
13 changes: 7 additions & 6 deletions candig_federation/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

import json
import requests
from flask import current_app
from requests_futures.sessions import FuturesSession
from network import get_registered_servers, get_registered_services
from candigv2_logging.logging import CanDIGLogger


logger = CanDIGLogger(__file__)


class FederationResponse:
Expand Down Expand Up @@ -50,14 +53,13 @@ def __init__(self, request, endpoint_path, endpoint_payload, request_dict, endpo
self.endpoint_service = endpoint_service
self.return_mimetype = return_mimetype
self.request_dict = request_dict
self.logger = current_app.logger
self.servers = get_registered_servers()
self.services = get_registered_services()

try:
self.token = self.request_dict.headers['Authorization']
except KeyError as e:
self.logger.warn("Request lacking Authorization header")
logger.warning("Request lacking Authorization header")
self.token = ""

self.header = {
Expand All @@ -81,7 +83,7 @@ def announce_fed_out(self, request_type, destination, path):
:param path: API endpoint of service
:type path: Str
"""
self.logger.info(json.dumps({"Sending": "{} -> {}/{}".format(
logger.info(json.dumps({"Sending": "{} -> {}/{}".format(
request_type, destination, path
)}))

Expand All @@ -94,7 +96,7 @@ def announce_fed_in(self, source, code):
:param code: Response code
:type code: int
"""
self.logger.info(json.dumps({"Received": "{} From {}".format(
logger.info(json.dumps({"Received": "{} From {}".format(
code, source
)}))

Expand Down Expand Up @@ -377,7 +379,6 @@ def get_response_object(self):

# now deconvolute the result to an array:
response_array = []
# print(json.dumps(response, indent=3))
for server in response['location'].keys():
r = {
'location': response['location'][server],
Expand Down
15 changes: 10 additions & 5 deletions candig_federation/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
from flask import current_app
import authx.auth
import os
from candigv2_logging.logging import CanDIGLogger


logger = CanDIGLogger(__file__)


TYK_FEDERATION_API_ID = os.getenv("TYK_FEDERATION_API_ID")

Expand All @@ -17,7 +22,7 @@ def get_registered_servers():
stored_servers_dict, status_code = authx.auth.set_service_store_secret("federation", key="servers", value=json.dumps({"servers": {}}))
return {}
if status_code != 200:
print(f"Error in get_registered_servers: {stored_servers_dict}")
logger.error(f"Error in get_registered_servers: {stored_servers_dict}")
return None
return stored_servers_dict["servers"]

Expand Down Expand Up @@ -52,7 +57,7 @@ def register_server(obj):

stored_servers_dict, status_code = authx.auth.set_service_store_secret("federation", key="servers", value=json.dumps({"servers": servers}))
if status_code != 200:
print(f"Error in register_server: {stored_servers_dict}")
logger.error(f"Error in register_server: {stored_servers_dict}")
return obj['server']


Expand All @@ -63,7 +68,7 @@ def unregister_server(server_id):
result = servers.pop(server_id)
stored_servers_dict, status_code = authx.auth.set_service_store_secret("federation", key="servers", value=json.dumps({"servers": servers}))
if status_code != 200:
print(f"Error in register_server: {stored_servers_dict}")
logger.error(f"Error in register_server: {stored_servers_dict}")
return result


Expand All @@ -74,7 +79,7 @@ def get_registered_services():
stored_services_dict, status_code = authx.auth.set_service_store_secret("federation", key="services", value=json.dumps({"services": {}}))
return {}
if status_code != 200:
print(f"Error in get_registered_services: {stored_services_dict}")
logger.error(f"Error in get_registered_services: {stored_services_dict}")
return None
return stored_services_dict["services"]

Expand All @@ -85,7 +90,7 @@ def register_service(obj):
services[obj['id']] = obj
stored_services_dict, status_code = authx.auth.set_service_store_secret("federation", key="services", value=json.dumps({"services": services}))
if status_code != 200:
print(f"Error in register_service: {stored_services_dict}")
logger.error(f"Error in register_service: {stored_services_dict}")
return None
return obj

Expand Down
25 changes: 14 additions & 11 deletions candig_federation/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
import connexion
from werkzeug.exceptions import UnsupportedMediaType
from flask import request, Flask
from apilog import apilog
from federation import FederationResponse
from network import get_registered_servers, get_registered_services, register_server, register_service, unregister_server, unregister_service
from candigv2_logging.logging import CanDIGLogger


logger = CanDIGLogger(__file__)


app = Flask(__name__)
Expand All @@ -33,7 +36,6 @@ def service_info():
return response, 200


@apilog
def list_servers():
"""
:return: Dictionary of registered peer servers.
Expand All @@ -42,10 +44,10 @@ def list_servers():
if servers is not None:
result = map(lambda x: x["server"], servers.values())
return list(result), 200
logger.debug(f"Couldn't list servers", request)
return {"message": "Couldn't list servers"}, 500


@apilog
def add_server(register=False):
"""
:return: Server added.
Expand All @@ -59,6 +61,7 @@ def add_server(register=False):
for server in existing_servers:
register_server(existing_servers[server])
except Exception as e:
logger.debug(f"Couldn't register server", request)
return {"message": f"Couldn't register servers: {type(e)} {str(e)} {connexion.request}"}, 500
try:
if connexion.request.json is not None and 'server' in connexion.request.json:
Expand All @@ -70,10 +73,10 @@ def add_server(register=False):
# this is the exception that gets thrown if the requestbody is null
return get_registered_servers(), 200
except Exception as e:
logger.debug(f"Couldn't register server", request)
return {"message": f"Couldn't add server: {type(e)} {str(e)} {connexion.request}"}, 500


@apilog
@app.route('/servers/<path:server_id>')
def get_server(server_id):
"""
Expand All @@ -83,10 +86,10 @@ def get_server(server_id):
if servers is not None and server_id in servers:
return servers[server_id], 200
else:
logger.debug(f"Couldn't find server {server_id}", request)
return {"message": f"Couldn't find server {server_id}"}, 404


@apilog
@app.route('/servers/<path:server_id>')
def delete_server(server_id):
"""
Expand All @@ -96,19 +99,18 @@ def delete_server(server_id):
return {"message": "User is not authorized to POST"}, 403
result = unregister_server(server_id)
if result is None:
logger.debug(f"Server not found", request)
return {"message": f"Server {server_id} not found"}, 404
return result, 200


@apilog
def list_services():
"""
:return: Dictionary of registered services.
"""
return list(get_registered_services().values()), 200


@apilog
@app.route('/services/<path:service_id>')
def get_service(service_id):
"""
Expand All @@ -118,10 +120,10 @@ def get_service(service_id):
if services is not None and service_id in services:
return services[service_id], 200
else:
logger.debug(f"Couldn't find service {service_id}", request)
return {"message": f"Couldn't find service {service_id}"}, 404


@apilog
def add_service(register=False):
"""
:return: Service added.
Expand All @@ -140,11 +142,11 @@ def add_service(register=False):
# this is the exception that gets thrown if the requestbody is null
return get_registered_services(), 200
except Exception as e:
logger.debug(f"Couldn't add service", request)
return {"message": f"Couldn't add service: {type(e)} {str(e)} {connexion.request}"}, 500
return get_registered_services()[new_service['id']], 200


@apilog
@app.route('/services/<path:service_id>')
def delete_service(service_id):
"""
Expand All @@ -154,11 +156,11 @@ def delete_service(service_id):
return {"message": "User is not authorized to POST"}, 403
result = unregister_service(service_id)
if result is None:
logger.debug(f"Couldn't find service", request)
return {"message": f"Service {service_id} not found"}, 404
return result, 200


@apilog
def post_search():
"""
Send a POST request to CanDIG services and possibly federate it.
Expand All @@ -183,7 +185,7 @@ def post_search():
ServiceName - Name of service (used for logstash tagging)
"""
try:

logger.debug("Sending federated request", request)
data = connexion.request.json
request_type = data["method"]
endpoint_path = data["path"]
Expand Down Expand Up @@ -213,6 +215,7 @@ def post_search():
have a valid request_type, endpoint_path and endpoint_payload. A KeyError occuring here
will be due to the service dictionary receiving an invalid key.
"""
logger.error(f"{type(e)} {str(e)}", request)
return {
"response": f"{type(e)} {str(e)}",
"status": 404,
Expand Down
12 changes: 3 additions & 9 deletions candig_federation/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
from flask_cors import CORS
import os.path
import network
import logging
import os
import candigv2_logging.logging

candigv2_logging.logging.initialize()


def main():
Expand All @@ -21,14 +23,6 @@ def main():
"""

CONFIG_DIR = os.getenv("CONFIG_DIR", "../config")
# Logging configuration

log_handler = logging.FileHandler(f"{CONFIG_DIR}/federation.log")
numeric_loglevel = getattr(logging, "INFO")
log_handler.setLevel(numeric_loglevel)

APP.app.logger.addHandler(log_handler)
APP.app.logger.setLevel(numeric_loglevel)

return APP

Expand Down
2 changes: 1 addition & 1 deletion federation.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module = wsgi:application
chdir = candig_federation
http = 0.0.0.0:4232

master = true
log-master = true
processes = 3

gid = candig
Expand Down
Loading

0 comments on commit 16fd64d

Please sign in to comment.