IP based middleware

Hello,

I want to set CORS restriction using middleware based on source IP for API endpoint & wondering if I can do this with Traefik V2 Middleware ?

Appreciated any kind of help here.

If you want to create different middlewares per IP, then you need to create different router per IP and assign different middlewares.

Not per IP, but per subnet possible ? can you provide some example ?

Create (multiple) router using Host() && PathPrefix() && ClientIP(), see doc. Attach a different middleware to each. You can use the same target service for each.

1 Like

I am not much experience with CORS. Does following CORS Middleware looks correct ?

Allowed CORS

apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
  name: cors-1
spec:
  headers:
    accessControlAllowMethods:
      - "GET"
      - "OPTIONS"
      - "PUT"
      - "POST"
      - "DELETE"
    accessControlAllowHeaders:
      - "*"
    accessControlAllowOriginList:
      - "https://*.example.org"
    accessControlMaxAge: 100
    addVaryHeader: true

Blocked CORS

apiVersion: traefik.io/v1alpha1
kind: Middleware
metadata:
  name: cors-2
spec:
  headers:
    accessControlAllowMethods: []
    accessControlAllowHeaders: []
    accessControlAllowOriginList: []