feat(traefik): Add traefik-plugin-rewrite-headers (#13961)

**Description**
<!--
Please include a summary of the change and which issue is fixed. Please
also include relevant motivation and context. List any dependencies that
are required for this change.
-->
⚒️ Fixes  # <!--(issue)-->

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?**
<!--
Please describe the tests that you ran to verify your changes. Provide
instructions so we can reproduce. Please also list any relevant details
for your test configuration
-->

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:**
<!-- Please enter any other relevant information here -->
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>
This commit is contained in:
sdimovv 2023-10-27 19:15:37 +01:00 committed by GitHub
parent 6c79d7f557
commit 24d91509d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 82 additions and 1 deletions

View File

@ -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

View File

@ -187,6 +187,7 @@ questions:
# Include{bufferingMiddleware}
# Include{customRequestHeadersMiddleware}
# Include{customResponseHeadersMiddleware}
# Include{rewriteResponseHeadersMiddleware}
# Include{customFrameOptionsValueMiddleware}
# Include{chainMiddleware}
# Include{redirectSchemeMiddleware}

View File

@ -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 }}

View File

@ -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 -}}

View File

@ -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"

View File

@ -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: ""