generated from traefik/plugindemo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Created RemoteAddr plugin * Added CODEOWNERS * Fix doublequote * Make linter happy * Fix package name * Added note to Dockerfile * Updates * Updated package comment * Update golangci-lint config * Update golangci-lint config * Update golangci-lint config * Don't run double workflows * Replaced icon * Fix badge * Fix badge * More badges * Updated comment * Added note on package name * Converted tests to table tests * Skip explicit struct fields
- Loading branch information
Showing
15 changed files
with
380 additions
and
416 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.git | ||
.assets | ||
.github | ||
.gitignore | ||
.golangci.yml | ||
*_test.go | ||
README.md | ||
Makefile | ||
Dockerfile | ||
.dockerignore |
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# SPDX-FileCopyrightText: 2024 Risk.Ident GmbH <[email protected]> | ||
# | ||
# SPDX-License-Identifier: CC0-1.0 | ||
|
||
* @RiskIdent/platform |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
name: Go Matrix | ||
on: [push, pull_request] | ||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
|
||
jobs: | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,11 @@ | ||
displayName: Demo Plugin | ||
displayName: RemoteAddr (fix X-Forwarded-Port) | ||
type: middleware | ||
iconPath: .assets/icon.png | ||
|
||
import: github.com/traefik/plugindemo | ||
import: github.com/RiskIdent/traefik-remoteaddr-plugin | ||
|
||
summary: '[Demo] Add Request Header' | ||
summary: "Add request header for the client port, to make X-Forwarded-Port act like in nginx" | ||
|
||
testData: | ||
Headers: | ||
X-Demo: test | ||
X-URL: '{{URL}}' | ||
headers: | ||
port: X-Forwarded-Port |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# When using this Dockerfile, make sure to also add static Traefik config for | ||
# loading the local plugin. Such as via CLI: | ||
# --experimental.localplugins.remoteaddr.modulename=github.com/RiskIdent/traefik-remoteaddr-plugin | ||
|
||
ARG TRAEFIK_VERSION=v3.0.0 | ||
ARG BASE_IMAGE=docker.io/traefik:${TRAEFIK_VERSION} | ||
FROM ${BASE_IMAGE} | ||
|
||
COPY . plugins-local/src/github.com/RiskIdent/traefik-remoteaddr-plugin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
# Traefik RemoteAddr plugin | ||
|
||
[![Main workflow](https://github.com/RiskIdent/traefik-remoteaddr-plugin/actions/workflows/main.yml/badge.svg)](https://github.com/RiskIdent/traefik-remoteaddr-plugin/actions/workflows/main.yml) | ||
[![Go matrix workflow](https://github.com/RiskIdent/traefik-remoteaddr-plugin/actions/workflows/go-cross.yml/badge.svg)](https://github.com/RiskIdent/traefik-remoteaddr-plugin/actions/workflows/go-cross.yml) | ||
|
||
## Usage | ||
|
||
This plugin is very simple: take the **client** IP and port and write them to some headers. | ||
This is done by using the Go field [`net/http.Request.RemoteAddr`](https://pkg.go.dev/net/http#Request) | ||
which is composed of `IP:port` of the client connection. | ||
|
||
To mimic nginx's behaviour of `X-Forwarded-Port`, where it sets that header to the client's port, then use the dynamic middleware config: | ||
|
||
```yaml | ||
middlewares: | ||
my-middleware: | ||
plugin: | ||
remoteaddr: | ||
headers: | ||
port: X-Forwarded-Port | ||
``` | ||
Alternatively, you could use the non-standard `X-Real-Port` to not override Traefik's behavior: | ||
|
||
```yaml | ||
middlewares: | ||
my-middleware: | ||
plugin: | ||
remoteaddr: | ||
headers: | ||
port: X-Real-Port | ||
``` | ||
|
||
### Configuration | ||
|
||
Traefik static configuration must define the module name (as is usual for Go packages). | ||
|
||
The following declaration (given here in YAML) defines a plugin: | ||
|
||
<details open><summary>File (YAML)</summary> | ||
|
||
```yaml | ||
# Static configuration | ||
experimental: | ||
plugins: | ||
remoteaddr: | ||
moduleName: github.com/RiskIdent/traefik-remoteaddr-plugin | ||
version: v0.1.0 | ||
``` | ||
|
||
</details> | ||
|
||
<details><summary>CLI</summary> | ||
|
||
```bash | ||
# Static configuration | ||
--experimental.plugins.remoteaddr.moduleName=github.com/RiskIdent/traefik-remoteaddr-plugin | ||
--experimental.plugins.remoteaddr.version=v0.1.0 | ||
``` | ||
|
||
</details> | ||
|
||
Here is an example of a file provider dynamic configuration (given here in YAML), where the interesting part is the `http.middlewares` section: | ||
|
||
<details open><summary>File (YAML)</summary> | ||
|
||
```yaml | ||
# Dynamic configuration | ||
http: | ||
middlewares: | ||
my-middleware: | ||
plugin: | ||
remoteaddr: | ||
headers: | ||
# if set, then set header "X-Real-Address" to the RemoteAddr (e.g "192.168.1.2:1234") | ||
address: X-Real-Address | ||
# if set, then set header "X-Real-Ip" to the IP of RemoteAddr (e.g "192.168.1.2") | ||
ip: X-Real-Ip | ||
# if set, then set header "X-Real-Port" to the port of RemoteAddr (e.g "1234") | ||
port: X-Real-Port | ||
``` | ||
|
||
</details> | ||
|
||
<details><summary>Kubernetes</summary> | ||
|
||
```yaml | ||
# Dynamic configuration | ||
apiVersion: traefik.io/v1alpha1 | ||
kind: Middleware | ||
metadata: | ||
name: my-middleware | ||
spec: | ||
plugin: | ||
remoteaddr: | ||
headers: | ||
# if set, then set header "X-Real-Address" to the RemoteAddr (e.g "192.168.1.2:1234") | ||
address: X-Real-Address | ||
# if set, then set header "X-Real-Ip" to the IP of RemoteAddr (e.g "192.168.1.2") | ||
ip: X-Real-Ip | ||
# if set, then set header "X-Real-Port" to the port of RemoteAddr (e.g "1234") | ||
port: X-Real-Port | ||
``` | ||
|
||
</details> | ||
|
||
### Local Mode | ||
|
||
Traefik also offers a developer mode that can be used for temporary testing of plugins not hosted on GitHub. | ||
To use a plugin in local mode, the Traefik static configuration must define the module name (as is usual for Go packages) and a path to a [Go workspace](https://golang.org/doc/gopath_code.html#Workspaces), which can be the local GOPATH or any directory. | ||
|
||
The plugins must be placed in `./plugins-local` directory, | ||
which should be in the working directory of the process running the Traefik binary. | ||
The source code of the plugin should be organized as follows: | ||
|
||
```console | ||
$ tree ./plugins-local/ | ||
./plugins-local/ | ||
└── src | ||
└── github.com | ||
└── RiskIdent | ||
└── traefik-remoteaddr-plugin | ||
├── plugin.go | ||
├── plugin_test.go | ||
├── go.mod | ||
├── LICENSE | ||
├── Makefile | ||
└── README.md | ||
``` | ||
|
||
<details open><summary>File (YAML)</summary> | ||
|
||
```yaml | ||
# Static configuration | ||
experimental: | ||
localPlugins: | ||
remoteaddr: | ||
moduleName: github.com/RiskIdent/traefik-remoteaddr-plugin | ||
``` | ||
|
||
</details> | ||
|
||
<details><summary>CLI</summary> | ||
|
||
```bash | ||
# Static configuration | ||
--experimental.localPlugins.remoteaddr.moduleName=github.com/RiskIdent/traefik-remoteaddr-plugin | ||
``` | ||
|
||
</details> | ||
|
||
(In the above example, the `traefik-remoteaddr-plugin` plugin will be loaded from the path `./plugins-local/src/github.com/RiskIdent/traefik-remoteaddr-plugin`.) |
Oops, something went wrong.