Add configurable logging for mtls-bridge proxy
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import time
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
from flask import Flask, Response, request
|
from flask import Flask, Response, request
|
||||||
@@ -19,6 +20,16 @@ CLIENT_KEY = os.environ.get("CLIENT_KEY", "/certs/client.key")
|
|||||||
CA_CERT = os.environ.get("CA_CERT", "/certs/ca.crt")
|
CA_CERT = os.environ.get("CA_CERT", "/certs/ca.crt")
|
||||||
TIMEOUT = int(os.environ.get("TIMEOUT", "5"))
|
TIMEOUT = int(os.environ.get("TIMEOUT", "5"))
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
"mtls-bridge starting target_url=%s timeout=%ss cert=%s key=%s ca=%s log_level=%s",
|
||||||
|
TARGET_URL,
|
||||||
|
TIMEOUT,
|
||||||
|
CLIENT_CERT,
|
||||||
|
CLIENT_KEY,
|
||||||
|
CA_CERT,
|
||||||
|
os.environ.get("LOG_LEVEL", "INFO"),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/health", methods=["GET"])
|
@app.route("/health", methods=["GET"])
|
||||||
def health():
|
def health():
|
||||||
@@ -28,28 +39,48 @@ def health():
|
|||||||
@app.route("/", defaults={"path": ""}, methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
|
@app.route("/", defaults={"path": ""}, methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
|
||||||
@app.route("/<path:path>", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
|
@app.route("/<path:path>", methods=["GET", "POST", "PUT", "DELETE", "PATCH"])
|
||||||
def proxy(path):
|
def proxy(path):
|
||||||
logger.info("request method=%s path=/%s", request.method, path)
|
request_path = f"/{path}" if path else "/"
|
||||||
|
request_size = len(request.get_data(cache=True))
|
||||||
|
logger.info(
|
||||||
|
"incoming request method=%s path=%s query=%s remote=%s bytes=%s",
|
||||||
|
request.method,
|
||||||
|
request_path,
|
||||||
|
request.query_string.decode("utf-8", "ignore"),
|
||||||
|
request.remote_addr,
|
||||||
|
request_size,
|
||||||
|
)
|
||||||
|
|
||||||
if not TARGET_URL:
|
if not TARGET_URL:
|
||||||
|
logger.error("TARGET_URL is not set; cannot proxy request")
|
||||||
return Response("TARGET_URL is not set", status=500)
|
return Response("TARGET_URL is not set", status=500)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
url = f"{TARGET_URL.rstrip('/')}/{path}".rstrip("/")
|
url = f"{TARGET_URL.rstrip('/')}/{path}".rstrip("/")
|
||||||
|
start_time = time.time()
|
||||||
headers = {k: v for k, v in request.headers if k.lower() != "host"}
|
headers = {k: v for k, v in request.headers if k.lower() != "host"}
|
||||||
headers["X-Forwarded-By"] = "mtls-bridge"
|
headers["X-Forwarded-By"] = "mtls-bridge"
|
||||||
|
|
||||||
|
logger.debug("forwarding request to upstream url=%s headers=%s", url, headers)
|
||||||
|
|
||||||
resp = requests.request(
|
resp = requests.request(
|
||||||
method=request.method,
|
method=request.method,
|
||||||
url=url,
|
url=url,
|
||||||
headers=headers,
|
headers=headers,
|
||||||
data=request.get_data(),
|
data=request.get_data(cache=True),
|
||||||
cookies=request.cookies,
|
cookies=request.cookies,
|
||||||
cert=(CLIENT_CERT, CLIENT_KEY),
|
cert=(CLIENT_CERT, CLIENT_KEY),
|
||||||
verify=CA_CERT,
|
verify=CA_CERT,
|
||||||
timeout=TIMEOUT,
|
timeout=TIMEOUT,
|
||||||
)
|
)
|
||||||
|
|
||||||
logger.info("upstream status=%s url=%s", resp.status_code, url)
|
elapsed_ms = int((time.time() - start_time) * 1000)
|
||||||
|
logger.info(
|
||||||
|
"upstream response status=%s url=%s elapsed_ms=%s response_bytes=%s",
|
||||||
|
resp.status_code,
|
||||||
|
url,
|
||||||
|
elapsed_ms,
|
||||||
|
len(resp.content),
|
||||||
|
)
|
||||||
|
|
||||||
excluded_headers = ["content-encoding", "content-length", "transfer-encoding", "connection"]
|
excluded_headers = ["content-encoding", "content-length", "transfer-encoding", "connection"]
|
||||||
response_headers = [
|
response_headers = [
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ services:
|
|||||||
- CLIENT_KEY=/certs/clients/office-pc/office-pc.key
|
- CLIENT_KEY=/certs/clients/office-pc/office-pc.key
|
||||||
- CA_CERT=/certs/ca/clents-ca.crt
|
- CA_CERT=/certs/ca/clents-ca.crt
|
||||||
- TIMEOUT=5
|
- TIMEOUT=5
|
||||||
|
- LOG_LEVEL=${MTLS_BRIDGE_LOG_LEVEL:-INFO}
|
||||||
volumes:
|
volumes:
|
||||||
- ${PROJECT_ROOT}/core/traefik/certs:/certs:ro
|
- ${PROJECT_ROOT}/core/traefik/certs:/certs:ro
|
||||||
healthcheck:
|
healthcheck:
|
||||||
|
|||||||
Reference in New Issue
Block a user