Skip to content

Commit a497503

Browse files
author
Jim Ryan
authored
API Key Auth Policy Docs (nginx#5752)
* add API Key to top list of policies * add api key docs * fix typo * add detail the api keys are base64 encoded in the secret * add clarification to merging behaviour
1 parent dbba378 commit a497503

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed

docs/content/configuration/policy-resource.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ spec:
3838
|``accessControl`` | The access control policy based on the client IP address. | [accessControl](#accesscontrol) | No |
3939
|``ingressClassName`` | Specifies which instance of NGINX Ingress Controller must handle the Policy resource. | ``string`` | No |
4040
|``rateLimit`` | The rate limit policy controls the rate of processing requests per a defined key. | [rateLimit](#ratelimit) | No |
41+
|``apiKey`` | The API Key policy configures NGINX to authorize requests which provide a valid API Key in a specified header or query param. | [apiKey](#apikey) | No |
4142
|``basicAuth`` | The basic auth policy configures NGINX to authenticate client requests using HTTP Basic authentication credentials. | [basicAuth](#basicauth) | No |
4243
|``jwt`` | The JWT policy configures NGINX Plus to authenticate client requests using JSON Web Tokens. | [jwt](#jwt) | No |
4344
|``ingressMTLS`` | The IngressMTLS policy configures client certificate verification. | [ingressMTLS](#ingressmtls) | No |
@@ -141,6 +142,84 @@ policies:
141142

142143
When you reference more than one rate limit policy, NGINX Ingress Controller will configure NGINX to use all referenced rate limits. When you define multiple policies, each additional policy inherits the `dryRun`, `logLevel`, and `rejectCode` parameters from the first policy referenced (`rate-limit-policy-one`, in the example above).
143144

145+
146+
### APIKey
147+
148+
The API Key auth policy configures NGINX to authorize client requests based on the presence of a valid API Key in a header or query param specified in the policy.
149+
150+
> Note: The feature is implemented using NGINX [ngx_http_auth_request_module](http://nginx.org/en/docs/http/ngx_http_auth_request_module.html) and [NGINX JavaScript (NJS)](https://nginx.org/en/docs/njs/).
151+
152+
The policies' API keys are securely stored using SHA-256 hashing. When a client sends an API Key, it is hashed by NJS and then compared to the hashed API Key in the NGINX config.
153+
154+
If the hashed keys match, the NGINX JavaScript (NJS) subrequest issues a 204 No Content response to the `auth_request` directive, indicating successful authorization. Conversely, if no API Key is provided in the specified header or query parameter, a 401 Unauthorized response is returned. Similarly, if an invalid key is presented in the expected header or query parameter, a 403 Forbidden response is issued, denying access.
155+
156+
It is possible to use the [errorPages](/nginx-ingress-controller/configuration/virtualserver-and-virtualserverroute-resources/#errorpage) property on a route, to change the default behaviour of 401 or 403 errors.
157+
158+
An API Key policy can be disabled on a route by adding the [location snippet](/nginx-ingress-controller/configuration/virtualserver-and-virtualserverroute-resources/#using-snippets) `auth_request off;`
159+
160+
At least one header or query param is required.
161+
162+
The policy below configures NGINX Ingress Controller to require the API Key `password` in the header "my-header".
163+
164+
```yaml
165+
apiKey:
166+
suppliedIn:
167+
header:
168+
- "my-header"
169+
clientSecret: api-key-secret
170+
```
171+
172+
```yaml
173+
apiVersion: v1
174+
kind: Secret
175+
metadata:
176+
name: api-key-secret
177+
type: nginx.org/apikey
178+
data:
179+
client1: cGFzc3dvcmQ= # password
180+
```
181+
182+
{{% table %}}
183+
|Field | Description | Type | Required |
184+
| ---| ---| ---| --- |
185+
|``suppliedIn.header`` | An array of headers that the API Key may appear in. | ``string[]`` | No |
186+
|``suppliedIn.query`` | An array of query params that the API Key may appear in. | ``string[]`` | No |
187+
|``clientSecret`` | The name of the Kubernetes secret that stores the API Key(s). It must be in the same namespace as the Policy resource. The secret must be of the type ``nginx.org/apikey``, and the API Key(s) must be stored in a key: val format where each key is a unique clientID and each value is a unique base64 encoded API Key | ``string`` | Yes |
188+
{{% /table %}}
189+
190+
#### APIKey Merging Behavior
191+
192+
A VirtualServer or VirtualServerRoute can be associated with only one API Key policy per route or subroute. However, it is possible to replace an API Key policy from a higher-level with a different policy defined on a more specific route.
193+
194+
For example, a VirtualServer can implement different API Key policies at various levels. In the configuration below, the server-wide api-key-policy-server applies to /backend1 for authorization, as it lacks a more specific policy. Meanwhile, /backend2 uses the api-key-policy-route defined at the route level.
195+
196+
```yaml
197+
apiVersion: k8s.nginx.org/v1
198+
kind: VirtualServer
199+
metadata:
200+
name: virtual-server
201+
spec:
202+
host: virtual-server.example.com
203+
policies:
204+
- name: api-key-policy-server
205+
upstreams:
206+
- name: backend2
207+
service: backend2-svc
208+
port: 80
209+
- name: backend1
210+
service: backend1-svc
211+
port: 80
212+
routes:
213+
- path: /backend1
214+
action:
215+
pass: backend1
216+
- path: /backend2
217+
action:
218+
pass: backend2
219+
policies:
220+
- name: api-key-policy-route
221+
```
222+
144223
### BasicAuth
145224

146225
The basic auth policy configures NGINX to authenticate client requests using the [HTTP Basic authentication scheme](https://developer.mozilla.org/en-US/docs/Web/HTTP/Authentication).

0 commit comments

Comments
 (0)