test: add HTTPS upload test for systemd-report
Extend fake-report-server.py with optional --cert, --key, --port arguments for TLS support. Add a test case that generates a self-signed certificate and tests HTTPS upload of metrics and facts. Also exercise the --header param. Co-developed-by: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
+18
-4
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/python3
|
||||
# SPDX-License-Identifier: LGPL-2.1-or-later
|
||||
|
||||
import json, os, socket
|
||||
import argparse, json, os, socket, ssl
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
def sd_notify(state: str) -> bool:
|
||||
@@ -22,6 +22,10 @@ class Handler(BaseHTTPRequestHandler):
|
||||
length = int(self.headers.get("Content-Length", 0))
|
||||
body = self.rfile.read(length)
|
||||
|
||||
# Check optional attribute
|
||||
if auth := self.headers.get("Authorization"):
|
||||
print(f"Authorization: {auth}")
|
||||
|
||||
# Validate JSON structure
|
||||
try:
|
||||
data = json.loads(body)
|
||||
@@ -45,10 +49,20 @@ class Handler(BaseHTTPRequestHandler):
|
||||
def log_message(self, fmt, *args):
|
||||
print(f"{self.address_string()} - {fmt % args}")
|
||||
|
||||
PORT = 8089
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--port", type=int, default=8089)
|
||||
parser.add_argument("--cert", help="TLS certificate file")
|
||||
parser.add_argument("--key", help="TLS private key file")
|
||||
args = parser.parse_args()
|
||||
|
||||
server = HTTPServer(("", PORT), Handler)
|
||||
print(f"Serving on http://localhost:{PORT}/")
|
||||
server = HTTPServer(("", args.port), Handler)
|
||||
if args.cert and args.key:
|
||||
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
|
||||
ctx.load_cert_chain(args.cert, args.key)
|
||||
server.socket = ctx.wrap_socket(server.socket, server_side=True)
|
||||
print(f"Serving on https://localhost:{args.port}/")
|
||||
else:
|
||||
print(f"Serving on http://localhost:{args.port}/")
|
||||
try:
|
||||
sd_notify("READY=1")
|
||||
server.serve_forever()
|
||||
|
||||
Reference in New Issue
Block a user