From 24d91509d6fdc81015cf9c811a5992eaadb4285e Mon Sep 17 00:00:00 2001 From: sdimovv <36302090+sdimovv@users.noreply.github.com> Date: Fri, 27 Oct 2023 19:15:37 +0100 Subject: [PATCH] feat(traefik): Add traefik-plugin-rewrite-headers (#13961) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit **Description** ⚒️ Fixes # Adding a simple plugin that allows for editing/rewriting response headers. It can be used to rewrite any response header. In the chart values, I have given a (perhaps not very exciting) example with the `Location` header. **⚙️ Type of change** - [X] ⚙️ Feature/App addition - [ ] 🪛 Bugfix - [ ] ⚠️ Breaking change (fix or feature that would cause existing functionality to not work as expected) - [ ] 🔃 Refactor of current code **🧪 How Has This Been Tested?** Tested with native helm - deployed Traefik, added an ingress in front of a simple custom-app that returns an HTTP301 on every request. With this plugin, I was able to rewrite the Location (or any other) header in the returned response. **📃 Notes:** Plugin: https://plugins.traefik.io/plugins/628c9eb5108ecc83915d7758/rewrite-header Corresponding container changes PR: https://github.com/truecharts/containers/pull/33731 **✔️ Checklist:** - [X] ⚖️ My code follows the style guidelines of this project - [X] 👀 I have performed a self-review of my own code - [X] #️⃣ I have commented my code, particularly in hard-to-understand areas - [ ] 📄 I have made corresponding changes to the documentation - [X] ⚠️ My changes generate no new warnings - [X] 🧪 I have added tests to this description that prove my fix is effective or that my feature works - [X] ⬆️ I increased versions for any altered app according to semantic versioning **➕ App addition** If this PR is an app addition please make sure you have done the following. - [X] 🪞 I have opened a PR on [truecharts/containers](https://github.com/truecharts/containers) adding the container to TrueCharts mirror repo. - [ ] 🖼️ I have added an icon in the Chart's root directory called `icon.png` --- _Please don't blindly check all the boxes. Read them and only check those that apply. Those checkboxes are there for the reviewer to see what is this all about and the status of this PR with a quick glance._ --------- Signed-off-by: sdimovv <36302090+sdimovv@users.noreply.github.com> --- charts/enterprise/traefik/Chart.yaml | 2 +- charts/enterprise/traefik/questions.yaml | 1 + charts/enterprise/traefik/templates/_args.tpl | 5 ++ .../middlewares/rewriteResponseHeaders.yaml | 17 +++++++ charts/enterprise/traefik/values.yaml | 9 ++++ .../rewriteResponseHeadersMiddleware.yaml | 49 +++++++++++++++++++ 6 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 charts/enterprise/traefik/templates/middlewares/rewriteResponseHeaders.yaml create mode 100644 templates/questions/traefik/rewriteResponseHeadersMiddleware.yaml diff --git a/charts/enterprise/traefik/Chart.yaml b/charts/enterprise/traefik/Chart.yaml index 807f1fbc6f7..fda690a8f35 100644 --- a/charts/enterprise/traefik/Chart.yaml +++ b/charts/enterprise/traefik/Chart.yaml @@ -23,7 +23,7 @@ sources: - https://github.com/traefik/traefik-helm-chart - https://traefik.io/ type: application -version: 21.0.9 +version: 21.1.0 annotations: truecharts.org/catagories: | - network diff --git a/charts/enterprise/traefik/questions.yaml b/charts/enterprise/traefik/questions.yaml index 9ce5b7829d4..17a6dae57dd 100644 --- a/charts/enterprise/traefik/questions.yaml +++ b/charts/enterprise/traefik/questions.yaml @@ -187,6 +187,7 @@ questions: # Include{bufferingMiddleware} # Include{customRequestHeadersMiddleware} # Include{customResponseHeadersMiddleware} +# Include{rewriteResponseHeadersMiddleware} # Include{customFrameOptionsValueMiddleware} # Include{chainMiddleware} # Include{redirectSchemeMiddleware} diff --git a/charts/enterprise/traefik/templates/_args.tpl b/charts/enterprise/traefik/templates/_args.tpl index d318ebf0c36..06e39a46890 100644 --- a/charts/enterprise/traefik/templates/_args.tpl +++ b/charts/enterprise/traefik/templates/_args.tpl @@ -181,6 +181,11 @@ args: - "--experimental.localPlugins.traefik-modsecurity-plugin.modulename=github.com/acouvreur/traefik-modsecurity-plugin" {{- end }} {{/* End of ModSecurity */}} + {{/* RewriteResponseHeaders */}} + {{- if .Values.middlewares.rewriteResponseHeaders }} + - "--experimental.localPlugins.rewriteResponseHeaders.modulename=github.com/XciD/traefik-plugin-rewrite-headers" + {{- end }} + {{/* End of RewriteResponseHeaders */}} {{- with .Values.additionalArguments }} {{- range . }} - {{ . | quote }} diff --git a/charts/enterprise/traefik/templates/middlewares/rewriteResponseHeaders.yaml b/charts/enterprise/traefik/templates/middlewares/rewriteResponseHeaders.yaml new file mode 100644 index 00000000000..d7bfdcdbe07 --- /dev/null +++ b/charts/enterprise/traefik/templates/middlewares/rewriteResponseHeaders.yaml @@ -0,0 +1,17 @@ +{{- range $index, $middlewareData := .Values.middlewares.rewriteResponseHeaders }} +--- +apiVersion: traefik.io/v1alpha1 +kind: Middleware +metadata: + name: {{ ternary (printf "%v-%v" $.Release.Name $middlewareData.name) $middlewareData.name $.Values.ingressClass.enabled }} + namespace: {{ $.Release.Namespace }} +spec: + plugin: + rewriteResponseHeaders: + rewrites: + {{- range $index, $rewriteResponseHeader := $middlewareData.headers }} + - header: {{ $rewriteResponseHeader.name }} + regex: {{ $rewriteResponseHeader.regex | quote }} + replacement: {{ $rewriteResponseHeader.replacement | quote }} + {{- end }} +{{- end -}} diff --git a/charts/enterprise/traefik/values.yaml b/charts/enterprise/traefik/values.yaml index 35edce81716..d2c944889a8 100644 --- a/charts/enterprise/traefik/values.yaml +++ b/charts/enterprise/traefik/values.yaml @@ -349,6 +349,15 @@ middlewares: # value: "foobar" # - name: X-Header-To-Remove # value: "" + rewriteResponseHeaders: [] + # - name: rewriteResponseHeadersName + # headers: + # - name: "Location" + # regex: "^http://(.+)$" + # replacement: "https://$1" + # - name: "Date" + # regex: "^[^,]+,\\s*(.+)$" + # replacement: "$1" customFrameOptionsValue: [] # - name: customFrameOptionsValueExample # value: "SAMEORIGIN" diff --git a/templates/questions/traefik/rewriteResponseHeadersMiddleware.yaml b/templates/questions/traefik/rewriteResponseHeadersMiddleware.yaml new file mode 100644 index 00000000000..7fd21839a39 --- /dev/null +++ b/templates/questions/traefik/rewriteResponseHeadersMiddleware.yaml @@ -0,0 +1,49 @@ + - variable: rewriteResponseHeaders + label: Rewrite Response Headers + schema: + type: list + default: [] + items: + - variable: rewriteResponseHeadersEntry + label: "" + schema: + additional_attrs: true + type: dict + attrs: + - variable: name + label: Name + schema: + type: string + required: true + - variable: headers + label: Headers To Rewrite + schema: + type: list + default: [] + items: + - variable: headersEntry + label: "" + schema: + additional_attrs: true + type: dict + attrs: + - variable: name + label: Header Name + description: Name of a header to modified in responses, eg. X-Custom-Header + schema: + valid_chars: ^[a-zA-Z0-9_\-]*$ + type: string + required: true + default: "" + - variable: regex + label: Regex + description: The value of the header to match. Accepts regex expression. + schema: + type: string + default: "" + - variable: replacement + label: Replacement Regex + description: The new value of the header. Accepts regex expression. + schema: + type: string + default: ""