|
| 1 | +--- |
| 2 | +title: Basic authentication with username and password |
| 3 | +sidebar_label: Basic Auth |
| 4 | +--- |
| 5 | + |
| 6 | +Headlamp does not provide a built-in username/password authentication mechanism. |
| 7 | +When running Headlamp in-cluster and exposing it via a public or internal URL, |
| 8 | +it is recommended to protect access using **ingress-level basic authentication**. |
| 9 | + |
| 10 | +This approach adds a simple username/password gate **before the Headlamp UI |
| 11 | +loads**, while Headlamp itself continues to rely on Kubernetes authentication |
| 12 | +(tokens, certificates) and RBAC for authorization. |
| 13 | + |
| 14 | +--- |
| 15 | + |
| 16 | +## When should this be used? |
| 17 | + |
| 18 | +Ingress-level basic authentication is useful when: |
| 19 | + |
| 20 | +- Headlamp is exposed via an Ingress, LoadBalancer, or NodePort |
| 21 | +- You want to prevent unauthenticated users from accessing the UI |
| 22 | +- You do not want to configure OIDC or an external identity provider |
| 23 | +- The dashboard is intended for internal, demo, or small-team use |
| 24 | + |
| 25 | +This is commonly used for: |
| 26 | + |
| 27 | +- In-cluster deployments |
| 28 | +- Helm-based installations |
| 29 | +- Local or internal Kubernetes dashboards |
| 30 | + |
| 31 | +--- |
| 32 | + |
| 33 | +## How it works |
| 34 | + |
| 35 | +With basic authentication enabled at the ingress level, the request flow looks |
| 36 | +like this: |
| 37 | + |
| 38 | +1. A user accesses the Headlamp URL |
| 39 | +2. The ingress controller prompts for a username and password |
| 40 | +3. If authentication succeeds, the request is forwarded to Headlamp |
| 41 | +4. Headlamp then performs its usual Kubernetes authentication and authorization |
| 42 | + |
| 43 | +The username/password check is handled entirely by the ingress controller. |
| 44 | +Headlamp never sees or manages these credentials. |
| 45 | + |
| 46 | +--- |
| 47 | + |
| 48 | +## Example: NGINX Ingress basic authentication |
| 49 | + |
| 50 | +The following example shows how to protect an in-cluster Headlamp deployment |
| 51 | +using NGINX Ingress basic authentication. |
| 52 | + |
| 53 | +### 1. Create a password file |
| 54 | + |
| 55 | +Use `htpasswd` to create a username and password: |
| 56 | +```bash |
| 57 | +htpasswd -c auth admin |
| 58 | +``` |
| 59 | + |
| 60 | +This creates a file named `auth` containing a hashed password. |
| 61 | + |
| 62 | +> **ℹ️ Note:** Multiple users can be added by running `htpasswd auth <username>` again (without the `-c` flag). |
| 63 | +
|
| 64 | +### 2. Store credentials in a Kubernetes Secret |
| 65 | + |
| 66 | +Create a secret from the password file: |
| 67 | +```bash |
| 68 | +kubectl create secret generic headlamp-basic-auth \ |
| 69 | + --from-file=auth \ |
| 70 | + -n <headlamp-namespace> |
| 71 | +``` |
| 72 | + |
| 73 | +### 3. Configure Ingress with basic authentication |
| 74 | + |
| 75 | +Add the following annotations to the ingress resource that exposes Headlamp: |
| 76 | +```yaml |
| 77 | +apiVersion: networking.k8s.io/v1 |
| 78 | +kind: Ingress |
| 79 | +metadata: |
| 80 | + name: headlamp-ingress |
| 81 | + namespace: <headlamp-namespace> |
| 82 | + annotations: |
| 83 | + nginx.ingress.kubernetes.io/auth-type: basic |
| 84 | + nginx.ingress.kubernetes.io/auth-secret: headlamp-basic-auth |
| 85 | + nginx.ingress.kubernetes.io/auth-realm: "Authentication Required" |
| 86 | +spec: |
| 87 | + ingressClassName: nginx |
| 88 | + rules: |
| 89 | + - host: headlamp.example.com |
| 90 | + http: |
| 91 | + paths: |
| 92 | + - path: / |
| 93 | + pathType: Prefix |
| 94 | + backend: |
| 95 | + service: |
| 96 | + name: headlamp |
| 97 | + port: |
| 98 | + number: 80 |
| 99 | +``` |
| 100 | +
|
| 101 | +After applying this configuration, users accessing the Headlamp URL will be |
| 102 | +prompted by their browser to enter a username and password. |
| 103 | +
|
| 104 | +--- |
| 105 | +
|
| 106 | +## Password management |
| 107 | +
|
| 108 | +- Passwords are managed by cluster administrators, not by Headlamp |
| 109 | +- Credentials are stored securely in Kubernetes Secrets |
| 110 | +- Passwords can be rotated by updating the secret without restarting Headlamp |
| 111 | +- Multiple users can be supported by adding entries to the password file |
| 112 | +
|
| 113 | +To update a password: |
| 114 | +```bash |
| 115 | +# Update the password file |
| 116 | +htpasswd auth admin |
| 117 | + |
| 118 | +# Update the secret |
| 119 | +kubectl create secret generic headlamp-basic-auth \ |
| 120 | + --from-file=auth \ |
| 121 | + -n <headlamp-namespace> \ |
| 122 | + --dry-run=client -o yaml | kubectl apply -f - |
| 123 | +``` |
| 124 | + |
| 125 | +--- |
| 126 | + |
| 127 | +## Important notes |
| 128 | + |
| 129 | +- **Basic authentication only controls access to the UI**. Headlamp will still require Kubernetes authentication (token or kubeconfig) after the UI loads. |
| 130 | +- **Authorization within the UI is controlled entirely by Kubernetes RBAC**. The username/password does not determine what resources users can view or modify. |
| 131 | +- This approach is intended for simple access control and is **not a replacement for full identity management solutions**. |
| 132 | +- For environments requiring per-user identity, auditing, or single sign-on, [OIDC-based authentication](../oidc/) is recommended instead. |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +## Other ingress controllers |
| 137 | + |
| 138 | +A similar setup can be achieved using other ingress controllers such as **Traefik** |
| 139 | +by configuring their respective basic authentication middleware. |
| 140 | + |
| 141 | +### Example: Traefik basic authentication |
| 142 | + |
| 143 | +For Traefik, you can use a similar approach with middleware: |
| 144 | +```yaml |
| 145 | +apiVersion: traefik.containo.us/v1alpha1 |
| 146 | +kind: Middleware |
| 147 | +metadata: |
| 148 | + name: headlamp-basic-auth |
| 149 | + namespace: <headlamp-namespace> |
| 150 | +spec: |
| 151 | + basicAuth: |
| 152 | + secret: headlamp-basic-auth |
| 153 | +``` |
| 154 | +
|
| 155 | +Then reference the middleware in your Ingress: |
| 156 | +```yaml |
| 157 | +apiVersion: networking.k8s.io/v1 |
| 158 | +kind: Ingress |
| 159 | +metadata: |
| 160 | + name: headlamp-ingress |
| 161 | + namespace: <headlamp-namespace> |
| 162 | + annotations: |
| 163 | + traefik.ingress.kubernetes.io/router.middlewares: <headlamp-namespace>-headlamp-basic-auth@kubernetescrd |
| 164 | +spec: |
| 165 | + rules: |
| 166 | + - host: headlamp.example.com |
| 167 | + http: |
| 168 | + paths: |
| 169 | + - path: / |
| 170 | + pathType: Prefix |
| 171 | + backend: |
| 172 | + service: |
| 173 | + name: headlamp |
| 174 | + port: |
| 175 | + number: 80 |
| 176 | +``` |
0 commit comments