From 4525fa265ec7af08de364bdda321626a104ad36a Mon Sep 17 00:00:00 2001 From: Giacomo Sanchietti Date: Fri, 11 Aug 2023 14:28:11 +0200 Subject: [PATCH] set-route: merge headers config to one file --- imageroot/actions/set-route/20writeconfig | 19 +++++- imageroot/actions/set-route/21writeconfigfile | 67 ------------------- 2 files changed, 16 insertions(+), 70 deletions(-) delete mode 100755 imageroot/actions/set-route/21writeconfigfile diff --git a/imageroot/actions/set-route/20writeconfig b/imageroot/actions/set-route/20writeconfig index 65f1b30..3943ab2 100755 --- a/imageroot/actions/set-route/20writeconfig +++ b/imageroot/actions/set-route/20writeconfig @@ -61,6 +61,7 @@ elif data.get("host") is not None: else: route = { "rule": f'Path(`{path}`) || PathPrefix(`{path_prefix}`)', "priority": "1"} +# Setup routers route["service"] = data["instance"] router_http = { f'{data["instance"]}-http': route } route_s = route.copy() @@ -95,13 +96,25 @@ if data.get('user_created') is not None and data["user_created"] is True: if data.get("forward_auth"): middlewares[f'{data["instance"]}-auth'] = { "forwardAuth": {"address": data["forward_auth"]["address"], "tls": { "insecureSkipVerify": data["forward_auth"].get("skip_tls_verify", False) }}} router_http[f'{data["instance"]}-http']["middlewares"].append(f'{data["instance"]}-auth') - router_https[f'{data["instance"]}-httw']["middlewares"].append(f'{data["instance"]}-auth') + router_https[f'{data["instance"]}-https']["middlewares"].append(f'{data["instance"]}-auth') +# Write headers configuration +if "headers" in data and data["headers"]: + headers = { 'headers': {} } + if ("request" in data["headers"] and data["headers"]["request"]): + headers['headers']['customRequestHeaders'] = data['headers']['request'] + if ("response" in data["headers"] and data["headers"]["response"]): + headers['headers']['customResponseHeaders'] = data['headers']['response'] + if headers['headers']: + middlewares[f'{data["instance"]}-headers'] = headers + + router_http[f'{data["instance"]}-http']["middlewares"].append(f'{data["instance"]}-headers') + router_https[f'{data["instance"]}-https']["middlewares"].append(f'{data["instance"]}-headers') + +# Write the configuration config = { "http": {"services": services, "routers": [router_http, router_https] }} if middlewares: config["middlewares"] = middlewares - -# Write the configuration with open(f'configs/{data["instance"]}.yml', 'w') as fp: fp.write(yaml.dump(config)) diff --git a/imageroot/actions/set-route/21writeconfigfile b/imageroot/actions/set-route/21writeconfigfile deleted file mode 100755 index 1c2f175..0000000 --- a/imageroot/actions/set-route/21writeconfigfile +++ /dev/null @@ -1,67 +0,0 @@ -#!/usr/bin/env python3 - -# -# Copyright (C) 2023 Nethesis S.r.l. -# SPDX-License-Identifier: GPL-3.0-or-later -# - -import json -import sys -import os -import agent -import yaml -from pathlib import Path - -# Try to parse the stdin as JSON. -# If parsing fails, output everything to stderr -data = json.load(sys.stdin) - -agent_id = os.getenv("AGENT_ID", "") -if not agent_id: - raise Exception("AGENT_ID not found inside the environemnt") - -# Connect to redis -r = agent.redis_connect(privileged=True) - -# Prepare common key prefix -prefix=f'{agent_id}/kv/http' -router=f'{prefix}/routers/{data["instance"]}-http' -router_s=f'{prefix}/routers/{data["instance"]}-https' -middlewares=f'{prefix}/middlewares' - -# Cleanup headers middleware from route -r.delete(f'{router}/middlewares/30') -r.delete(f'{router_s}/middlewares/30') - -# Remove previous configuration -p = Path(f'configs/{data["instance"]}-route.yml') -p.unlink(missing_ok=True) - -# Prepare common dicts -route = {'http': {}} -middlewares = {} - -# Write headers configuration -if "headers" in data and data["headers"]: - - headers = { 'headers': {} } - - if ("request" in data["headers"] and data["headers"]["request"]): - headers['headers']['customRequestHeaders'] = data['headers']['request'] - - if ("response" in data["headers"] and data["headers"]["response"]): - headers['headers']['customResponseHeaders'] = data['headers']['response'] - - if headers['headers']: - middlewares[f'{data["instance"]}-headers'] = headers - -if middlewares: - route['http']['middlewares'] = middlewares - -if route['http']: - with open(p.resolve(), 'w') as file: - yaml.dump(route, file) - r.set(f'{router}/middlewares/30', f'{data["instance"]}-headers@file') - r.set(f'{router_s}/middlewares/30', f'{data["instance"]}-headers@file') - -r.close()