BasicAuth

Adding Basic Authentication

BasicAuth

The BasicAuth middleware is a quick way to restrict access to your services to known users.

Configuration Examples

  1. # Declaring the user list
  2. #
  3. # Note: all dollar signs in the hash need to be doubled for escaping.
  4. # To create user:password pair, it's possible to use this command:
  5. # echo $(htpasswd -nb user password) | sed -e s/\\$/\\$\\$/g
  6. labels:
  7. - "traefik.http.middlewares.test-auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"
  1. # Declaring the user list
  2. apiVersion: traefik.containo.us/v1alpha1
  3. kind: Middleware
  4. metadata:
  5. name: test-auth
  6. spec:
  7. basicAuth:
  8. secret: secretName
  1. "labels": {
  2. "traefik.http.middlewares.test-auth.basicauth.users": "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"
  3. }
  1. # Declaring the user list
  2. labels:
  3. - "traefik.http.middlewares.test-auth.basicauth.users=test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"
  1. # Declaring the user list
  2. [http.middlewares]
  3. [http.middlewares.test-auth.basicAuth]
  4. users = [
  5. "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
  6. "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
  7. ]
  1. # Declaring the user list
  2. http:
  3. middlewares:
  4. test-auth:
  5. basicAuth:
  6. users:
  7. - "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"
  8. - "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"

Configuration Options

General

Passwords must be encoded using MD5, SHA1, or BCrypt.

Tip

Use htpasswd to generate the passwords.

users

The users option is an array of authorized users. Each user will be declared using the name:encoded-password format.

Note

  • If both users and usersFile are provided, the two are merged. The contents of usersFile have precedence over the values in users.
  • For security reasons, the field users doesn't exist for Kubernetes IngressRoute, and one should use the secret field instead.
  1. # Declaring the user list
  2. #
  3. # Note: all dollar signs in the hash need to be doubled for escaping.
  4. # To create user:password pair, it's possible to use this command:
  5. # echo $(htpasswd -nb user password) | sed -e s/\\$/\\$\\$/g
  6. labels:
  7. - "traefik.http.middlewares.test-auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"
  1. # Declaring the user list
  2. apiVersion: traefik.containo.us/v1alpha1
  3. kind: Middleware
  4. metadata:
  5. name: test-auth
  6. spec:
  7. basicAuth:
  8. secret: authsecret
  9. ---
  10. apiVersion: v1
  11. kind: Secret
  12. metadata:
  13. name: authsecret
  14. namespace: default
  15. data:
  16. users: |2
  17. dGVzdDokYXByMSRINnVza2trVyRJZ1hMUDZld1RyU3VCa1RycUU4d2ovCnRlc3QyOiRhcHIxJGQ5
  18. aHI5SEJCJDRIeHdnVWlyM0hQNEVzZ2dQL1FObzAK
  1. "labels": {
  2. "traefik.http.middlewares.test-auth.basicauth.users": "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"
  3. }
  1. # Declaring the user list
  2. labels:
  3. - "traefik.http.middlewares.test-auth.basicauth.users=test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"
  1. # Declaring the user list
  2. [http.middlewares]
  3. [http.middlewares.test-auth.basicAuth]
  4. users = [
  5. "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/",
  6. "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
  7. ]
  1. # Declaring the user list
  2. http:
  3. middlewares:
  4. test-auth:
  5. basicAuth:
  6. users:
  7. - "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/"
  8. - "test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0"

usersFile

The usersFile option is the path to an external file that contains the authorized users for the middleware.

The file content is a list of name:encoded-password.

Note

  • If both users and usersFile are provided, the two are merged. The contents of usersFile have precedence over the values in users.
  • Because it does not make much sense to refer to a file path on Kubernetes, the usersFile field doesn't exist for Kubernetes IngressRoute, and one should use the secret field instead.
  1. labels:
  2. - "traefik.http.middlewares.test-auth.basicauth.usersfile=/path/to/my/usersfile"
  1. apiVersion: traefik.containo.us/v1alpha1
  2. kind: Middleware
  3. metadata:
  4. name: test-auth
  5. spec:
  6. basicAuth:
  7. secret: authsecret
  8. ---
  9. apiVersion: v1
  10. kind: Secret
  11. metadata:
  12. name: authsecret
  13. namespace: default
  14. data:
  15. users: |2
  16. dGVzdDokYXByMSRINnVza2trVyRJZ1hMUDZld1RyU3VCa1RycUU4d2ovCnRlc3QyOiRhcHIxJGQ5
  17. aHI5SEJCJDRIeHdnVWlyM0hQNEVzZ2dQL1FObzAK
  1. "labels": {
  2. "traefik.http.middlewares.test-auth.basicauth.usersfile": "/path/to/my/usersfile"
  3. }
  1. labels:
  2. - "traefik.http.middlewares.test-auth.basicauth.usersfile=/path/to/my/usersfile"
  1. [http.middlewares]
  2. [http.middlewares.test-auth.basicAuth]
  3. usersFile = "/path/to/my/usersfile"
  1. http:
  2. middlewares:
  3. test-auth:
  4. basicAuth:
  5. usersFile: "/path/to/my/usersfile"

A file containing test/test and test2/test2

  1. test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/
  2. test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0

realm

You can customize the realm for the authentication with the realm option. The default value is traefik.

  1. labels:
  2. - "traefik.http.middlewares.test-auth.basicauth.realm=MyRealm"
  1. apiVersion: traefik.containo.us/v1alpha1
  2. kind: Middleware
  3. metadata:
  4. name: test-auth
  5. spec:
  6. basicAuth:
  7. realm: MyRealm
  1. "labels": {
  2. "traefik.http.middlewares.test-auth.basicauth.realm": "MyRealm"
  3. }
  1. labels:
  2. - "traefik.http.middlewares.test-auth.basicauth.realm=MyRealm"
  1. [http.middlewares]
  2. [http.middlewares.test-auth.basicAuth]
  3. realm = "MyRealm"
  1. http:
  2. middlewares:
  3. test-auth:
  4. basicAuth:
  5. realm: "MyRealm"

headerField

You can define a header field to store the authenticated user using the headerFieldoption.

  1. labels:
  2. - "traefik.http.middlewares.my-auth.basicauth.headerField=X-WebAuth-User"
  1. apiVersion: traefik.containo.us/v1alpha1
  2. kind: Middleware
  3. metadata:
  4. name: my-auth
  5. spec:
  6. basicAuth:
  7. # ...
  8. headerField: X-WebAuth-User
  1. "labels": {
  2. "traefik.http.middlewares.my-auth.basicauth.headerField": "X-WebAuth-User"
  3. }
  1. [http.middlewares.my-auth.basicAuth]
  2. # ...
  3. headerField = "X-WebAuth-User"
  1. http:
  2. middlewares:
  3. my-auth:
  4. basicAuth:
  5. # ...
  6. headerField: "X-WebAuth-User"

removeHeader

Set the removeHeader option to true to remove the authorization header before forwarding the request to your service. (Default value is false.)

  1. labels:
  2. - "traefik.http.middlewares.test-auth.basicauth.removeheader=true"
  1. apiVersion: traefik.containo.us/v1alpha1
  2. kind: Middleware
  3. metadata:
  4. name: test-auth
  5. spec:
  6. basicAuth:
  7. removeHeader: true
  1. "labels": {
  2. "traefik.http.middlewares.test-auth.basicauth.removeheader": "true"
  3. }
  1. labels:
  2. - "traefik.http.middlewares.test-auth.basicauth.removeheader=true"
  1. [http.middlewares]
  2. [http.middlewares.test-auth.basicAuth]
  3. removeHeader = true
  1. http:
  2. middlewares:
  3. test-auth:
  4. basicAuth:
  5. removeHeader: true