|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" Web server that logs HTTP POST requests """ |
| 3 | + |
| 4 | +import logging |
| 5 | +import os |
| 6 | +import json |
| 7 | +from flask import Flask |
| 8 | +from flask import request |
| 9 | +from waitress import serve |
| 10 | +from .lib.constants import VERSION, BUILD, W001, W002, W003 |
| 11 | +log = logging.getLogger('csp') |
| 12 | + |
| 13 | + |
| 14 | +class CSP(): |
| 15 | + """ The main CSP class """ |
| 16 | + |
| 17 | + settings = { |
| 18 | + 'max_content_length': 32768, |
| 19 | + 'address': '*', |
| 20 | + 'port': 9180, |
| 21 | + 'enable_healthz_version': False, |
| 22 | + 'csp_path': '/csp', |
| 23 | + 'healthz_path': '/healthz', |
| 24 | + 'metrics_path': '/metrics', # Placeholder |
| 25 | + } |
| 26 | + |
| 27 | + def __init__(self, **kwargs): |
| 28 | + for k, v in kwargs.items(): |
| 29 | + if k in self.settings: |
| 30 | + self.settings[k] = v |
| 31 | + else: |
| 32 | + log.warning(f'{k} not found in settings. Ignoring.') |
| 33 | + self.server = Flask(__name__) |
| 34 | + self.server.secret_key = os.urandom(64).hex() |
| 35 | + self.server.add_url_rule(self.settings['csp_path'], 'csp', self.log_csp, methods=['POST']) |
| 36 | + self.server.add_url_rule(self.settings['healthz_path'], 'healthz', self.healthz, methods=['GET']) |
| 37 | + |
| 38 | + def log_csp(self): |
| 39 | + """ Logs the content posted """ |
| 40 | + |
| 41 | + result = ('OK', 200) |
| 42 | + |
| 43 | + try: |
| 44 | + if request.content_length == 0: |
| 45 | + raise TypeError(W002) |
| 46 | + if request.content_length > self.settings['max_content_length']: |
| 47 | + log.warning(f'{W001} ({request.content_length}). Dropping.') |
| 48 | + result = (W001, 413) |
| 49 | + except TypeError: |
| 50 | + log.warning(W002) |
| 51 | + result = (W002, 422) |
| 52 | + |
| 53 | + if result == ('OK', 200): |
| 54 | + log.debug(f"{request.environ}") |
| 55 | + content = request.get_data(as_text=True) |
| 56 | + try: |
| 57 | + json_content = json.loads(content) |
| 58 | + log.info(f'{json.dumps(json_content)}') |
| 59 | + # Placeholder: json_content can now be analysed. Ideally, with a function outside of the CSP class |
| 60 | + except json.decoder.JSONDecodeError: |
| 61 | + log.debug(f'{W003}: `{content}`') |
| 62 | + result = (W003, 422) |
| 63 | + |
| 64 | + return result |
| 65 | + |
| 66 | + def healthz(self): |
| 67 | + """ Healthcheck """ |
| 68 | + version_string = f'{__package__} {VERSION}-{BUILD}' |
| 69 | + |
| 70 | + log.debug(f'Healthcheck {version_string}') |
| 71 | + |
| 72 | + message = 'OK' |
| 73 | + if self.settings['enable_healthz_version']: |
| 74 | + message = version_string |
| 75 | + return (message, 200) |
| 76 | + |
| 77 | + def start(self): |
| 78 | + """ Start the web server """ |
| 79 | + serve( |
| 80 | + self.server, |
| 81 | + host=self.settings['address'], |
| 82 | + port=self.settings['port'], |
| 83 | + ident=None, |
| 84 | + ) |
| 85 | + |
| 86 | + def get_port(self) -> int: |
| 87 | + """ returns the configured port from self.settings['port'] """ |
| 88 | + return self.settings['port'] |
| 89 | + |
| 90 | + def get_address(self) -> int: |
| 91 | + """ returns the configured address from self.settings['port'] """ |
| 92 | + return self.settings['address'] |
0 commit comments