headerproxy

package module
v0.5.1-0...-ce09b3f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2021 License: MIT Imports: 4 Imported by: 0

README

Header Proxy plugin for traefik

This plugin proxies requests to desired destination (outside of traefik) based on headers provided in the configuration.

Dev traefik.yml configuration file for traefik

pilot:
  token: [REDACTED]

experimental:
  devPlugin:
    goPath: /home/user/go
    moduleName: github.com/apwe/headerproxy

entryPoints:
  http:
    address: ":8000"
    forwardedHeaders:
      insecure: true

providers:
  file:
    filename: rules-headerproxy.yaml 

How to dev

$ docker run -d --network host containous/whoami -port 5000
# traefik --config-file traefik.yml

How to use

Headers

Provide header names:

  • 'TenantHeader' : a header controlling the tenant that the requests will be proxied to
  • 'ServiceHeader' : a header controlling a service from the tenant that the request will be proxied to
          Headers:
            TenantHeader: 'Tenant'
            ServiceHeader: 'Service'

So, in order to match the configuration, you need to hit traefik with a request containing 'Tenant: tenant-name' and 'Service: service-name' headers.

Tenants

Provide tenant names as the keys - these are the values of your 'TenantHeader: $$$%%#^$' header passed to traefik:

          Tenants:
            tenant-name-a:
...
            tenant-name-b:
...
Services

Provide service names as the keys - these are the values of your 'ServiceHeader: &$^#$&#$' header passed to traefik

            tenant-name-a:
              Services:
                ServiceA:
...
                ServiceB: 
...
                ServiceC: 
...
            tenant-name-b:
              Services:
                ServiceA: 
...
                ServiceB:
...
                ServiceC: 
...
Destination

Provide value to the 'Destination' key with the url of the service you want to proxy the requests. urls MUST contain protocol scheme, as the plugin is parsing the url and using the scheme in the communication.

                ServiceA: 
                  Destination: 'https://example-a.com'
                ServiceB: 
                  Destination: 'https://example-b.com'
                ServiceC: 
                  Destination: 'https://example-c.com'
Example configuration for traefik file provider
http:
  routers:
    my-router:
      entryPoints:
      - http
      middlewares:
      - headerproxy
      service: service-whoami
      rule: Path(`/whoami`)

  services:
    service-whoami:
      loadBalancer:
        servers:
        - url: http://localhost:5000/
      passHostHeader: false
  middlewares:
    headerproxy:
      plugin:
        dev:
          Headers:
            TenantHeader: 'Tenant'
            ServiceHeader: 'Service'
          Tenants:
            tenant-name-a:
              Services:
                ServiceA: 
                  Destination: 'https://example-a.com'
                ServiceB: 
                  Destination: 'https://example-b.com'
                ServiceC: 
                  Destination: 'https://example-c.com'
            tenant-name-b:
              Services:
                ServiceA: 
                  Destination: 'https://example-a.com'
                ServiceB: 
                  Destination: 'https://example-b.com'

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error)

New instantiates and returns the required components used to handle a HTTP request

func ProxyReq

func ProxyReq(rw http.ResponseWriter, req *http.Request, service Service)

Send the request

Types

type Config

type Config struct {
	Tenants map[string]Tenant // `yaml:"Tenants"`
	Headers []Header
}

Config struct holds configuration to be passed to the plugin

func CreateConfig

func CreateConfig() *Config

CreateConfig creates the default plugin configuration.

type Header struct {
	TenantHeader  string `yaml:"TenantHeader"`
	ServiceHeader string `yaml:"ServiceHeader"`
}

type HostReplacement

type HostReplacement struct {
	// contains filtered or unexported fields
}

HostReplacement holds the necessary components of a Traefik plugin

func (*HostReplacement) ServeHTTP

func (u *HostReplacement) ServeHTTP(rw http.ResponseWriter, req *http.Request)

Iterate over headers (if provided in the configuration) to match the ones that come in the request & match the headers & send a request to a specified tenant and service configuration

type Service

type Service struct {
	Destination string `yaml:"Destination"`
}

Service struct

type Tenant

type Tenant struct {
	Services map[string]Service `yaml:"Services"`
}

Tenant struct

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL