traefik_unleash_plugin

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2024 License: Apache-2.0 Imports: 13 Imported by: 0

README

Quality Gate Status Coverage Code Smells Bugs Security Rating Vulnerabilities

Traefik Unleash Middleware

This Traefik middleware validates requests against an Unleash (feature flag) server and rewrites the path, headers and host of the request based on input parameters defined in the YAML configuration file.

Table of Contents

Installation

Follow the Traefik instructions for installing plugins.

Configuration

To configure this middleware, you need to define the parameters in the dynamic.yml file.

Input Parameters
Parameter Type Required Description
url string Yes URL of the Unleash server
app string Yes Name of the application in Unleash
interval int No Update interval in seconds
metrics.interval int No Metrics reporting interval in seconds
toggles list Yes List of feature flag toggles
toggles[].feature string Yes Name of the feature flag
toggles[].pathRewrite.pathMatcher string No Path to be validated
toggles[].pathRewrite.rewriteRule string No Path to redirect to if the feature flag is active
toggles[].hostRewrite.hostMatcher string No Host to be validated
toggles[].hostRewrite.rewriteRule string No Host to redirect to if the feature flag is active
toggles[].headerModifers list No List of headers to be added to the request or response
toggles[].headerModifiers[].headerName string Yes Header key
toggles[].headerModifiers[].headerValue string Yes Header value
toggles[].headerModifiers[].context string Yes Context of the header (either "request" or "response")
offlineMode boolean No The plugin is configured in local mode without external dependencies

Usage

  1. Define the configuration in the dynamic.yml file:

    unleash:
      url: "http://unleash:4242/api/"
      app: "test-app"
      interval: 10
      metrics:
        interval: 10
      toggles:
        - feature: "test-toggle-user-id"
          pathRewrite:
            pathMatcher: "/foo"
            rewriteRule: "/bar"
          hostRewrite:
            hostMatcher: "localhost"
            rewriteRule: "whoami2"
        - feature: "test-toggle"
          pathRewrite:
            pathMatcher: "/bar"
            rewriteRule: "/foo"
          hostRewrite:
            hostMatcher: "localhost"
            rewriteRule: "whoami2"
        - feature: "test-toggle-path"
          pathRewrite:
            pathMatcher: "/john"
            rewriteRule: "/doe"
        - feature: "test-toggle-host"
          hostRewrite:
            hostMatcher: "localhost"
            rewriteRule: "whoami1"
    
  2. Apply the middleware to your routers in the Traefik configuration:

    http:
      routers:
        my-router:
          rule: "Host(`example.com`)"
          service: "my-service"
          middlewares: 
            - "unleash"
    

Example

Here is a complete configuration example:

http:
  routers:
    my-router:
      rule: "Host(`example.com`)"
      service: "my-service"
      middlewares:
        - "unleash"

  middlewares:
    unleash:
      plugin:
        unleash:
          url: "http://unleash:4242/api/"
          app: "test-app"
          interval: 10
          metrics:
            interval: 10
          toggles:
            - feature: "test-toggle-user-id"
              pathRewrite:
                pathMatcher: "/foo"
                rewriteRule: "/bar"
              hostRewrite:
                hostMatcher: "localhost"
                rewriteRule: "whoami2"
              headerModifiers:
                - headerName: "X-Foo"
                  headerValue: "Bar"
                  context: "request"
                - headerName: "X-Served-By"
                  headerValue: "whoami2"
                  context: "response"
            - feature: "test-toggle"
              pathRewrite:
                pathMatcher: "/bar"
                rewriteRule: "/foo"
              hostRewrite:
                value: "localhost"
                rewrite: "whoami2"
            - feature: "test-toggle-path"
              pathRewrite:
                pathMatcher: "/john"
                rewriteRule: "/doe"
            - feature: "test-toggle-host"
              hostRewrite:
                hostMatcher: "localhost"
                rewriteRule: "whoami1"

Contributing

Contributions are welcome! If you want to contribute, please open an issue or a pull request in this repository.

License

This project is licensed under the MIT License.

Documentation

Index

Constants

View Source
const (
	SchemeHTTP     = "http"
	SchemeHTTPS    = "https"
	RequestHeader  = "request"
	ResponseHeader = "response"
	UserIdHeader   = "X-Unleash-User-Id"
)
View Source
const DefaultInterval = 10

Variables

This section is empty.

Functions

func New

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

Types

type Config

type Config struct {
	Url      string `yaml:"url"`
	App      string `yaml:"app"`
	Interval *int   `yaml:"interval"`
	Metrics  *struct {
		Interval *int `yaml:"interval"`
	} `yaml:"metrics"`
	Toggles []struct {
		HeaderModifiers *[]struct {
			HeaderName  string `yaml:"headerName"`
			HeaderValue string `yaml:"headerValue"`
			Context     string `yaml:"context"`
		} `yaml:"headerModifiers"`
		PathRewrite *struct {
			PathMatcher string `yaml:"pathMatcher"`
			RewriteRule string `yaml:"rewriteRule"`
		} `yaml:"pathRewrite"`
		HostRewrite *struct {
			HostMatcher string `yaml:"hostMatcher"`
			RewriteRule string `yaml:"rewriteRule"`
		} `yaml:"hostRewrite"`
		Feature string `yaml:"feature"`
	} `yaml:"toggles"`
	OfflineMode bool `yaml:"offlineMode"`
}

func CreateConfig

func CreateConfig() *Config

type FeatureToggle

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

type HeaderModifier added in v0.1.0

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

type HostRewrite added in v0.1.0

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

type PathRewrite added in v0.1.0

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

type Unleash

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

func (*Unleash) ServeHTTP

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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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