Skip to content

Setup basic auth

Apply annotations to ingress configuration:

Add this to the ingress manifest and apply changes.

metadata:
  annotations:
    nginx.ingress.kubernetes.io/auth-type: basic
    nginx.ingress.kubernetes.io/auth-secret: basic-auth
    nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required'

Create secret

  1. Create a htpasswd file with the desired username. It will prompt for a password twice.

    htpasswd -c auth <username>
    

  2. Create a secret from the generated file

    kubectl create secret generic basic-auth --from-file=auth
    
    Make sure you create this secret in the same namespace as the ingress that needs basic auth.

  3. Verify the created secret. If the namespace is not in your context you will need to add -n [namespace]

    kubectl get secret basic-auth -o yaml
    

  4. Test the basic auth.

    • Should return 401.

      curl -IL http://<IP>/ -H 'Host: <domain>'
      

    • Should return the headers of the application you're running.

      curl -IL http://<IP>/ -H 'Host: <domain>' -u '<user>:<password'