From 08c9a1c7ec5849520d3ba5f5ec69045bed1b55b3 Mon Sep 17 00:00:00 2001 From: Marvin Martinson Date: Wed, 11 Aug 2021 14:47:50 +0000 Subject: [PATCH] Create custom logging --- pinecrypt/server/api/access.py | 4 ++-- pinecrypt/server/api/bootstrap.py | 3 ++- pinecrypt/server/api/request.py | 3 ++- pinecrypt/server/api/revoked.py | 3 ++- pinecrypt/server/api/script.py | 3 ++- pinecrypt/server/api/session.py | 4 ++-- pinecrypt/server/api/signed.py | 4 ++-- pinecrypt/server/api/token.py | 3 ++- pinecrypt/server/api/utils/firewall.py | 4 ++-- pinecrypt/server/authority.py | 4 +++- pinecrypt/server/cli.py | 8 ++++--- pinecrypt/server/logger.py | 28 +++++++++++++++++++++++++ pinecrypt/server/mongolog.py | 29 ++++++++++---------------- 13 files changed, 65 insertions(+), 35 deletions(-) create mode 100644 pinecrypt/server/logger.py diff --git a/pinecrypt/server/api/access.py b/pinecrypt/server/api/access.py index e04798d..6aaddeb 100644 --- a/pinecrypt/server/api/access.py +++ b/pinecrypt/server/api/access.py @@ -6,9 +6,9 @@ from pinecrypt.server import authority, errors, db from bson.objectid import ObjectId from pinecrypt.server.decorators import csrf_protection from .utils.firewall import login_required, authorize_admin +from pinecrypt.server.mongolog import LogHandler -logger = logging.getLogger(__name__) - +logger = LogHandler() class DisableEnableAccessToInstance(object): @csrf_protection diff --git a/pinecrypt/server/api/bootstrap.py b/pinecrypt/server/api/bootstrap.py index 969385e..092449d 100644 --- a/pinecrypt/server/api/bootstrap.py +++ b/pinecrypt/server/api/bootstrap.py @@ -3,8 +3,9 @@ import logging from pinecrypt.server import authority, const, config from pinecrypt.server.common import cert_to_dn from pinecrypt.server.decorators import serialize +from pinecrypt.server.mongolog import LogHandler -logger = logging.getLogger(__name__) +logger = LogHandler() class BootstrapResource(object): @serialize diff --git a/pinecrypt/server/api/request.py b/pinecrypt/server/api/request.py index 9018436..b831cd0 100644 --- a/pinecrypt/server/api/request.py +++ b/pinecrypt/server/api/request.py @@ -8,11 +8,12 @@ from asn1crypto.csr import CertificationRequest from pinecrypt.server import const, errors, authority from pinecrypt.server.decorators import csrf_protection, MyEncoder from pinecrypt.server.user import DirectoryConnection +from pinecrypt.server.mongolog import LogHandler from oscrypto import asymmetric from .utils.firewall import whitelist_subnets, whitelist_content_types, \ login_required, login_optional, authorize_admin, validate_clock_skew -logger = logging.getLogger(__name__) +logger = LogHandler() """ openssl genrsa -out test.key 1024 diff --git a/pinecrypt/server/api/revoked.py b/pinecrypt/server/api/revoked.py index 3d7cc50..0c5bf9d 100644 --- a/pinecrypt/server/api/revoked.py +++ b/pinecrypt/server/api/revoked.py @@ -2,8 +2,9 @@ import falcon import logging from pinecrypt.server import authority, const, errors from .utils.firewall import whitelist_subnets +from pinecrypt.server.mongolog import LogHandler -logger = logging.getLogger(__name__) +logger = LogHandler() class RevocationListResource(object): @whitelist_subnets(const.CRL_SUBNETS) diff --git a/pinecrypt/server/api/script.py b/pinecrypt/server/api/script.py index e61fc51..fd92981 100644 --- a/pinecrypt/server/api/script.py +++ b/pinecrypt/server/api/script.py @@ -3,9 +3,10 @@ import os from pinecrypt.server import authority, const from jinja2 import Environment, FileSystemLoader from .utils.firewall import whitelist_subject +from pinecrypt.server.mongolog import LogHandler -logger = logging.getLogger(__name__) env = Environment(loader=FileSystemLoader(const.SCRIPT_DIR), trim_blocks=True) +logger = LogHandler() class ScriptResource(object): @whitelist_subject diff --git a/pinecrypt/server/api/session.py b/pinecrypt/server/api/session.py index 21fb0c0..ad1cf3d 100644 --- a/pinecrypt/server/api/session.py +++ b/pinecrypt/server/api/session.py @@ -4,9 +4,9 @@ from pinecrypt.server import authority, const, config from pinecrypt.server.decorators import serialize, csrf_protection from pinecrypt.server.user import User from .utils.firewall import login_required, authorize_admin, register_session +from pinecrypt.server.mongolog import LogHandler -logger = logging.getLogger(__name__) - +logger = LogHandler() class CertificateAuthorityResource(object): def on_get(self, req, resp): diff --git a/pinecrypt/server/api/signed.py b/pinecrypt/server/api/signed.py index f44fde8..98cecd7 100644 --- a/pinecrypt/server/api/signed.py +++ b/pinecrypt/server/api/signed.py @@ -1,4 +1,3 @@ - import falcon import logging import json @@ -6,8 +5,9 @@ import hashlib from pinecrypt.server import authority, errors, db from pinecrypt.server.decorators import csrf_protection from .utils.firewall import login_required, authorize_admin +from pinecrypt.server.mongolog import LogHandler -logger = logging.getLogger(__name__) +logger = LogHandler() class SignedCertificateDetailResource(object): def on_get_cn(self, req, resp, cn): diff --git a/pinecrypt/server/api/token.py b/pinecrypt/server/api/token.py index 28ab841..6de346d 100644 --- a/pinecrypt/server/api/token.py +++ b/pinecrypt/server/api/token.py @@ -7,8 +7,9 @@ from pinecrypt.server import const, errors, authority from pinecrypt.server.decorators import serialize from pinecrypt.server.user import User from .utils.firewall import login_required, authorize_admin +from pinecrypt.server.mongolog import LogHandler -logger = logging.getLogger(__name__) +logger = LogHandler() class TokenResource(object): def __init__(self, manager): diff --git a/pinecrypt/server/api/utils/firewall.py b/pinecrypt/server/api/utils/firewall.py index 1cb0d09..5ed415e 100644 --- a/pinecrypt/server/api/utils/firewall.py +++ b/pinecrypt/server/api/utils/firewall.py @@ -15,6 +15,7 @@ from datetime import datetime, timedelta from pinecrypt.server.user import User from pinecrypt.server import const, errors, db from prometheus_client import Counter, Histogram +from pinecrypt.server.mongolog import LogHandler clock_skew = Histogram( "pinecrypt_gateway_clock_skew", @@ -26,8 +27,7 @@ whitelist_blocked_requests = Counter( "Requests blocked by whitelists.", ["method", "path"]) -logger = logging.getLogger(__name__) - +logger = LogHandler() def whitelist_subnets(subnets): """ diff --git a/pinecrypt/server/authority.py b/pinecrypt/server/authority.py index 831f7d9..4904aa3 100644 --- a/pinecrypt/server/authority.py +++ b/pinecrypt/server/authority.py @@ -10,12 +10,14 @@ from asn1crypto.csr import CertificationRequest from certbuilder import CertificateBuilder from pinecrypt.server import mailer, const, errors, config, db from pinecrypt.server.common import cn_to_dn, generate_serial, cert_to_dn +from pinecrypt.server.mongolog import LogHandler from crlbuilder import CertificateListBuilder, pem_armor_crl from csrbuilder import CSRBuilder, pem_armor_csr from datetime import datetime, timedelta from bson.objectid import ObjectId -logger = logging.getLogger(__name__) +#logger = logging.getLogger(__name__) +logger = LogHandler() # Cache CA certificate with open(const.AUTHORITY_CERTIFICATE_PATH, "rb") as fh: diff --git a/pinecrypt/server/cli.py b/pinecrypt/server/cli.py index 9dd3cc0..99bf209 100644 --- a/pinecrypt/server/cli.py +++ b/pinecrypt/server/cli.py @@ -26,12 +26,14 @@ from math import log, ceil from pinecrypt.server import const, mongolog, mailer, db from pinecrypt.server.middleware import NormalizeMiddleware, PrometheusEndpoint from pinecrypt.server.common import cn_to_dn, generate_serial +from pinecrypt.server.mongolog import LogHandler +#from pinecrypt.server.logger import CertidudeLogger from time import sleep from wsgiref.simple_server import make_server -logger = logging.getLogger(__name__) -mongolog.register() - +#logger = logging.getLogger(__name__) +#logger = CertidudeLogger() +logger = LogHandler() def graceful_exit(signal_number, stack_frame): print("Received signal %d, exiting now" % signal_number) diff --git a/pinecrypt/server/logger.py b/pinecrypt/server/logger.py new file mode 100644 index 0000000..8db8453 --- /dev/null +++ b/pinecrypt/server/logger.py @@ -0,0 +1,28 @@ +import time + +class LoggerObject(object): + msg = None + args = None + levelname = None + created = None + + +class CertidudeLogger(object): + + def info(self, msg, *args): + self.pre_emit(msg, *args, level="Info") + def warning(self, msg, *args): + self.pre_emit(msg, *args, level="Warning") + def error(self, msg, *args): + self.pre_emit(msg, *args, level="Error") + def debug(self, msg, *args): + self.pre_emit(msg, *args, level="Debug") + def pre_emit(self, msg, *args, level): + record = LoggerObject() + record.msg = msg + record.args = args + record.levelname = level + record.created = time.time() + self.emit(record) + def emit(self,record): + console.log(record) diff --git a/pinecrypt/server/mongolog.py b/pinecrypt/server/mongolog.py index abcefc1..d84a7ad 100644 --- a/pinecrypt/server/mongolog.py +++ b/pinecrypt/server/mongolog.py @@ -1,27 +1,20 @@ - -import logging from datetime import datetime from pinecrypt.server import db +from pinecrypt.server.logger import CertidudeLogger -class LogHandler(logging.Handler): +class LogHandler(CertidudeLogger): def emit(self, record): d= {} d["created"] = datetime.utcfromtimestamp(record.created) - d["facility"] = record.name - d["level"] = record.levelno +# d["facility"] = record.name +# d["level"] = record.levelno d["severity"] = record.levelname.lower() d["message"] = record.msg % record.args - d["module"] = record.module - d["func"] = record.funcName - d["lineno"] = record.lineno - d["exception"] = logging._defaultFormatter.formatException(record.exc_info) if record.exc_info else "", - d["process"] = record.process - d["thread"] = record.thread - d["thread_name"] = record.threadName +# d["module"] = record.module +# d["func"] = record.funcName +# d["lineno"] = record.lineno +# d["exception"] = logging._defaultFormatter.formatException(record.exc_info) if record.exc_info else "", +# d["process"] = record.process +# d["thread"] = record.thread +# d["thread_name"] = record.threadName db.eventlog.insert(d, w=1) - -def register(): - for j in logging.Logger.manager.loggerDict.values(): - if isinstance(j, logging.Logger): - j.setLevel(logging.DEBUG) - j.addHandler(LogHandler())