|
| 1 | +# Support for HTTP Basic Authentication |
| 2 | + |
| 3 | +NGINX supports authenticating requests with [ngx_http_auth_basic_module](https://nginx.org/en/docs/http/ngx_http_auth_basic_module.html). |
| 4 | + |
| 5 | +The Ingress controller provides the following 4 annotations for configuring JWT validation: |
| 6 | + |
| 7 | +* Required: ```nginx.org/basic-auth-secret: "secret"``` -- specifies a Secret resource with a htpasswd user list. The htpasswd must be stored in the `htpasswd` data field. The type of the secret must be `nginx.org/htpasswd`. |
| 8 | +* Optional: ```nginx.org/basic-auth-realm: "realm"``` -- specifies a realm. |
| 9 | + |
| 10 | +``` |
| 11 | +
|
| 12 | +## Example 1: The Same Htpasswd for All Paths |
| 13 | +
|
| 14 | +In the following example we enable HTTP Basic authentication for the cafe-ingress Ingress for all paths using the same htpasswd `cafe-htpasswd`: |
| 15 | +```yaml |
| 16 | +apiVersion: networking.k8s.io/v1 |
| 17 | +kind: Ingress |
| 18 | +metadata: |
| 19 | + name: cafe-ingress |
| 20 | + annotations: |
| 21 | + nginx.org/basic-auth-secret: "cafe-passwd" |
| 22 | + nginx.org/basic-auth-realm: "Cafe App" |
| 23 | +spec: |
| 24 | + tls: |
| 25 | + - hosts: |
| 26 | + - cafe.example.com |
| 27 | + secretName: cafe-secret |
| 28 | + rules: |
| 29 | + - host: cafe.example.com |
| 30 | + http: |
| 31 | + paths: |
| 32 | + - path: /tea |
| 33 | + backend: |
| 34 | + serviceName: tea-svc |
| 35 | + servicePort: 80 |
| 36 | + - path: /coffee |
| 37 | + backend: |
| 38 | + serviceName: coffee-svc |
| 39 | + servicePort: 80 |
| 40 | +``` |
| 41 | +* The keys must be deployed separately in the Secret `cafe-jwk`. |
| 42 | +* The realm is `Cafe App`. |
| 43 | + |
| 44 | +## Example 2: a Separate Htpasswd Per Path |
| 45 | + |
| 46 | +In the following example we enable JWT validation for the [mergeable Ingresses](../mergeable-ingress-types) with a separate JWT key per path: |
| 47 | + |
| 48 | +* Master: |
| 49 | + ```yaml |
| 50 | + apiVersion: networking.k8s.io/v1 |
| 51 | + kind: Ingress |
| 52 | + metadata: |
| 53 | + name: cafe-ingress-master |
| 54 | + annotations: |
| 55 | + kubernetes.io/ingress.class: "nginx" |
| 56 | + nginx.org/mergeable-ingress-type: "master" |
| 57 | + spec: |
| 58 | + tls: |
| 59 | + - hosts: |
| 60 | + - cafe.example.com |
| 61 | + secretName: cafe-secret |
| 62 | + rules: |
| 63 | + - host: cafe.example.com |
| 64 | + ``` |
| 65 | +
|
| 66 | +* Tea minion: |
| 67 | + ```yaml |
| 68 | + apiVersion: networking.k8s.io/v1 |
| 69 | + kind: Ingress |
| 70 | + metadata: |
| 71 | + name: cafe-ingress-tea-minion |
| 72 | + annotations: |
| 73 | + nginx.org/mergeable-ingress-type: "minion" |
| 74 | + nginx.org/basic-auth-secret: "tea-passwd" |
| 75 | + nginx.org/basic-auth-realm: "Tea" |
| 76 | + spec: |
| 77 | + rules: |
| 78 | + - host: cafe.example.com |
| 79 | + http: |
| 80 | + paths: |
| 81 | + - path: /tea |
| 82 | + pathType: Prefix |
| 83 | + backend: |
| 84 | + service: |
| 85 | + name: tea-svc |
| 86 | + port: |
| 87 | + number: 80 |
| 88 | + ``` |
| 89 | +
|
| 90 | +* Coffee minion: |
| 91 | + ```yaml |
| 92 | + apiVersion: networking.k8s.io/v1 |
| 93 | + kind: Ingress |
| 94 | + metadata: |
| 95 | + name: cafe-ingress-coffee-minion |
| 96 | + annotations: |
| 97 | + nginx.org/mergeable-ingress-type: "minion" |
| 98 | + nginx.org/basic-auth-secret: "coffee-passwd" |
| 99 | + nginx.org/basic-auth-realm: "Coffee" |
| 100 | + spec: |
| 101 | + rules: |
| 102 | + - host: cafe.example.com |
| 103 | + http: |
| 104 | + paths: |
| 105 | + - path: /coffee |
| 106 | + pathType: Prefix |
| 107 | + backend: |
| 108 | + service: |
| 109 | + name: coffee-svc |
| 110 | + port: |
| 111 | + number: 80 |
| 112 | + ``` |
0 commit comments