checkbodyplugin

package module
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2021 License: MIT Imports: 8 Imported by: 0

README

Check Body Request

This check body request plugin for Traefik which checks the incoming request for specific body and their values to be present and matching the configuration. If the request does not validate against the configured body, the middleware will return a 400 Bad Request status code.

Limitations

  • JSON request are not working and it's will be make your traefik crash (We are fixing)

Configuration

Static
pilot:
  token: xxxx

experimental:
  plugins:
    traefik-check-body:
      modulename: github.com/soulbalz/traefik-check-body
      version: v1.0.5
Dynamic configuration
http:
  routers:
    my-router:
      rule: Path(`/whoami`)
      service: service-whoami
      entryPoints:
        - http
      middlewares:
        - traefik-check-body

  services:
   service-whoami:
      loadBalancer:
        servers:
          - url: http://127.0.0.1:5000
  
  middlewares:
    traefik-check-body:
      plugin:
        traefik-check-body:
          response:
            code: '1234'
            message: 'test'
            status: 401
            raw: |
              {
                "error": "example error"
              }
          body:
            - name: "BODY_1"
              matchtype: one
              values: 
                - "VALUE_1"
                - "VALUE_99"
            - name: "BODY_2"
              matchtype: one
              values: 
                - "VALUE_2"
            - name: "BODY_3"
              matchtype: one
              values: 
                - "VALUE_3"
              required: false
            - name: "BODY_4"
              matchtype: all
              values: 
                - "LUE_4"
                - "VALUE_5"
              contains: true
              required: true

Configuration documentation

Supported configurations per body

Setting Allowed values Required Description
name string Yes Name of the request body
matchtype one, all Yes Match on all values or one of the values specified. The value 'all' is only allowed in combination with the 'contains' setting.
values []string Yes A list of allowed values which are matched against the request header value
contains boolean No If set to true (default false), the request is allowed if the request body value contains the value specified in the configuration
required boolean No If set to false (default true), the request is allowed if the body is absent or the value is empty

response

Setting Allowed values Required Description
code string No Response error message code (default: 400)
message string No Response error message (default: "Invalid Request.")
status integer No Response error status code (default: 400)
raw string No Custom your response message whatever you want

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 created a new BodyMatch plugin.

Types

type BodyMatch

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

BodyMatch demonstrates a BodyMatch plugin.

func (*BodyMatch) ServeHTTP

func (a *BodyMatch) ServeHTTP(rw http.ResponseWriter, r *http.Request)

type Config

type Config struct {
	Body     []SingleBody
	Response ResponseError
}

Config the plugin configuration.

func CreateConfig

func CreateConfig() *Config

CreateConfig creates the default plugin configuration.

func (*Config) Validate

func (config *Config) Validate() error

Validate validate config data

type MatchType

type MatchType string

MatchType defines an enum which can be used to specify the match type for the 'contains' config.

const (
	//MatchAll requires all values to be matched
	MatchAll MatchType = "all"
	//MatchOne requires only one value to be matched
	MatchOne MatchType = "one"
)

type ResponseError

type ResponseError struct {
	Code    string `json:"code,omitempty"`
	Message string `json:"message,omitempty"`
	Status  int    `json:"status,omitempty"`
	Raw     string `json:"raw,omitempty"`
}

ResponseError contains a failuer message

func (*ResponseError) GetMessage

func (resErr *ResponseError) GetMessage() string

GetMessage get response message

func (*ResponseError) Response

func (resErr *ResponseError) Response(rw http.ResponseWriter)

Response write error response to client

type SingleBody

type SingleBody struct {
	Name      string   `json:"name,omitempty"`
	Values    []string `json:"values,omitempty"`
	MatchType string   `json:"matchtype,omitempty"`
	Required  *bool    `json:"required,omitempty"`
	Contains  *bool    `json:"contains,omitempty"`
	URLDecode *bool    `json:"urldecode,omitempty"`
}

SingleBody contains a single body keypair

func (*SingleBody) IsContains

func (s *SingleBody) IsContains() bool

IsContains checks whether a body value should contain the configured value

func (*SingleBody) IsRequired

func (s *SingleBody) IsRequired() bool

IsRequired checks whether a body is mandatory in the request, defaults to 'true'

func (*SingleBody) IsValid

func (s *SingleBody) IsValid(data *forms.Data) bool

IsValid checks is validation valid

Jump to

Keyboard shortcuts

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