Skip to content

Commit df76131

Browse files
authored
improve nginx ingress documentation (#1079)
* improve nginx ingress documentation * updating for secrets * update * add a small warning
1 parent 7426380 commit df76131

3 files changed

Lines changed: 235 additions & 42 deletions

File tree

crowdsec-docs/docs/appsec/quickstart/nginx-ingress.mdx

Lines changed: 86 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,28 @@ If CrowdSec is already deployed with Helm in this cluster, the repository entry
5858

5959
### Update CrowdSec configuration
6060

61+
Store the nginx bouncer key in a Kubernetes secret, following the same pattern
62+
used by the Envoy quickstart.
63+
64+
Create or update the secret used by CrowdSec LAPI:
65+
66+
```yaml title="crowdsec-keys.yaml"
67+
apiVersion: v1
68+
kind: Secret
69+
metadata:
70+
name: crowdsec-keys
71+
namespace: crowdsec
72+
type: Opaque
73+
stringData:
74+
BOUNCER_KEY_nginx_ingress_waf: "<choose-a-long-random-key>"
75+
```
76+
77+
Apply it:
78+
79+
```bash
80+
kubectl apply -f crowdsec-keys.yaml
81+
```
82+
6183
Add this to the CrowdSec `values.yaml` with the AppSec acquisition datasource (see the [AppSec datasource](/log_processor/data_sources/appsec.md)) and the default [AppSec configuration](/appsec/configuration.md):
6284

6385
```yaml title="values.yaml"
@@ -74,13 +96,42 @@ appsec:
7496
env:
7597
- name: COLLECTIONS
7698
value: crowdsecurity/appsec-virtual-patching crowdsecurity/appsec-generic-rules
99+
lapi:
100+
env:
101+
- name: BOUNCER_KEY_nginx_ingress_waf
102+
valueFrom:
103+
secretKeyRef:
104+
name: crowdsec-keys
105+
key: BOUNCER_KEY_nginx_ingress_waf
106+
```
107+
108+
:::warning
109+
The Helm chart still enables the CrowdSec agent by default. If you do not want
110+
the agent, disable it explicitly.
111+
:::
112+
113+
<details>
114+
<summary>Snippet to disable the agent</summary>
115+
116+
```yaml title="values.yaml"
117+
agent:
118+
enabled: false
77119
```
78120
121+
</details>
122+
123+
:::note
124+
Although this is the same bouncer key value, you need two `Secret` objects here:
125+
one in `crowdsec` and one in `ingress-nginx`. Kubernetes secrets are
126+
namespace-scoped, so the ingress controller cannot read a secret from the
127+
`crowdsec` namespace.
128+
:::
129+
79130
This YAML configuration snippet exposes the important configuration items:
80131
* `listen_addr: 0.0.0.0:7422` exposes the AppSec API inside the cluster.
81132
* `appsec_configs` loads the [AppSec configuration(s)](/appsec/configuration.md) that define which rules are evaluated (in-band vs out-of-band).
82133
* The two collections provide virtual patching and generic rule coverage.
83-
* The chart bootstraps a bouncer named `nginx_ingress_waf` using the key you export locally.
134+
* `lapi.env` forces the `nginx_ingress_waf` bouncer key from the `crowdsec-keys` Secret.
84135

85136
And now we apply the new configuration with:
86137

@@ -98,6 +149,26 @@ You should see `crowdsec-agent` pods, the `crowdsec-lapi` pod and the `crowdsec-
98149

99150
## Enable the CrowdSec Lua plugin on NGINX Ingress
100151

152+
Create the secret holding the same CrowdSec bouncer key in the
153+
`ingress-nginx` namespace:
154+
155+
```yaml title="crowdsec-ingress-bouncer-secret.yaml"
156+
apiVersion: v1
157+
kind: Secret
158+
metadata:
159+
name: crowdsec-ingress-bouncer-secrets
160+
namespace: ingress-nginx
161+
type: Opaque
162+
stringData:
163+
api-key: "<same-value-as-BOUNCER_KEY_nginx_ingress_waf>"
164+
```
165+
166+
Apply it:
167+
168+
```bash
169+
kubectl apply -f crowdsec-ingress-bouncer-secret.yaml
170+
```
171+
101172
To extend the ingress controller with the CrowdSec plugin and point it to the
102173
AppSec API, create the file named `ingress-values.yaml`. You can read the entire
103174
file in the snippet below.
@@ -107,8 +178,8 @@ controller:
107178
image:
108179
registry: docker.io
109180
image: crowdsecurity/controller
110-
tag: v1.13.2
111-
digest: sha256:4575be24781cad35f8e58437db6a3f492df2a3167fed2b6759a6ff0dc3488d56
181+
tag: v1.14.3
182+
digest: sha256:9ab8791635f4cde9964ab2562fb8b15faf72fe0205f0fe288089a87e1455675d
112183
extraVolumes:
113184
- name: crowdsec-bouncer-plugin
114185
emptyDir: {}
@@ -120,7 +191,10 @@ controller:
120191
- name: API_URL
121192
value: "http://crowdsec-service.crowdsec.svc.cluster.local:8080"
122193
- name: API_KEY
123-
value: privateKey-foo
194+
valueFrom:
195+
secretKeyRef:
196+
name: crowdsec-ingress-bouncer-secrets
197+
key: api-key
124198
- name: BOUNCER_CONFIG
125199
value: "/crowdsec/crowdsec-bouncer.conf"
126200
- name: APPSEC_URL
@@ -153,13 +227,13 @@ controller:
153227
plugins: "crowdsec"
154228
lua-shared-dicts: "crowdsec_cache: 50m"
155229
server-snippet: |
156-
lua_ssl_trusted_certificate "/etc/ssl/certs/ca-certificates.crt"
230+
lua_ssl_trusted_certificate "/etc/ssl/certs/ca-certificates.crt";
157231
resolver local=on ipv6=off;
158232
```
159233

160234

161235
- `API_URL` targets the Local API service exposed by the Helm chart.
162-
- `API_KEY` defines the key for the bouncer to be able to connect to CrowdSec LAPI
236+
- `API_KEY` is read from the `crowdsec-ingress-bouncer-secrets` Secret in the `ingress-nginx` namespace.
163237
- `APPSEC_URL` points to the AppSec service; keep the namespace in sync with your CrowdSec release.
164238
- The plugin copies the Lua files from the init container into an `emptyDir` that is mounted at runtime.
165239

@@ -184,8 +258,8 @@ controller:
184258
image:
185259
registry: docker.io
186260
image: crowdsecurity/controller
187-
tag: v1.13.2
188-
digest: sha256:...
261+
tag: v1.14.3
262+
digest: sha256:9ab8791635f4cde9964ab2562fb8b15faf72fe0205f0fe288089a87e1455675d
189263
```
190264

191265
The controller image is replaced with a CrowdSec-enabled build that includes the
@@ -212,7 +286,10 @@ extraInitContainers:
212286
- name: API_URL
213287
value: "http://crowdsec-service.crowdsec.svc.cluster.local:8080"
214288
- name: API_KEY
215-
value: privateKey-foo
289+
valueFrom:
290+
secretKeyRef:
291+
name: crowdsec-ingress-bouncer-secrets
292+
key: api-key
216293
- name: BOUNCER_CONFIG
217294
value: "/crowdsec/crowdsec-bouncer.conf"
218295
- name: APPSEC_URL

crowdsec-docs/unversioned/bouncers/ingress-nginx.mdx

Lines changed: 74 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,66 @@ The Ingress nginx controller should be installed using the [official helm chart]
5757

5858
First you need to create new ingress-nginx chart values file (`crowdsec-ingress-values.yaml`) to upgrade the ingress controller with the crowdsec plugin.
5959

60+
Store the CrowdSec bouncer key in Kubernetes Secrets instead of embedding it
61+
directly in the Helm values.
62+
63+
Create or update the secret used by CrowdSec LAPI:
64+
65+
```yaml title="crowdsec-keys.yaml"
66+
apiVersion: v1
67+
kind: Secret
68+
metadata:
69+
name: crowdsec-keys
70+
namespace: crowdsec
71+
type: Opaque
72+
stringData:
73+
BOUNCER_KEY_nginx_ingress_waf: "<choose-a-long-random-key>"
74+
```
75+
76+
Apply it:
77+
78+
```bash
79+
kubectl apply -f crowdsec-keys.yaml
80+
```
81+
82+
Then reference it from your CrowdSec values:
83+
84+
```yaml title="crowdsec-values.yaml"
85+
lapi:
86+
env:
87+
- name: BOUNCER_KEY_nginx_ingress_waf
88+
valueFrom:
89+
secretKeyRef:
90+
name: crowdsec-keys
91+
key: BOUNCER_KEY_nginx_ingress_waf
92+
```
93+
94+
:::note
95+
Although this is the same bouncer key value, you need two `Secret` objects here:
96+
one in `crowdsec` and one in `ingress-nginx`. Kubernetes secrets are
97+
namespace-scoped, so the ingress controller cannot read a secret from the
98+
`crowdsec` namespace.
99+
:::
100+
101+
Create the secret holding the same key in the `ingress-nginx` namespace:
102+
103+
```yaml title="crowdsec-ingress-bouncer-secret.yaml"
104+
apiVersion: v1
105+
kind: Secret
106+
metadata:
107+
name: crowdsec-ingress-bouncer-secrets
108+
namespace: ingress-nginx
109+
type: Opaque
110+
stringData:
111+
api-key: "<choose-a-long-random-key>"
112+
```
113+
114+
Apply it:
115+
116+
```bash
117+
kubectl apply -f crowdsec-ingress-bouncer-secret.yaml
118+
```
119+
60120
:::warning
61121

62122
Lua support has been removed from mainline ingress nginx in version 1.12. As
@@ -70,9 +130,9 @@ controller:
70130
PullPolicy: IfNotPresent
71131
image: crowdsecurity/controller
72132
# Crowdsec Remediation with Ingress Nginx requires to use our controller image
73-
tag: v1.13.2
133+
tag: v1.14.3
74134
# If you update the tag, the digest needs to be updated as well
75-
digest: sha256:4575be24781cad35f8e58437db6a3f492df2a3167fed2b6759a6ff0dc3488d56
135+
digest: sha256:9ab8791635f4cde9964ab2562fb8b15faf72fe0205f0fe288089a87e1455675d
76136
registry: docker.io
77137
extraVolumes:
78138
- name: crowdsec-bouncer-plugin
@@ -85,7 +145,10 @@ controller:
85145
- name: API_URL
86146
value: "http://crowdsec-service.crowdsec.svc.cluster.local:8080" # crowdsec lapi service-name
87147
- name: API_KEY
88-
value: "<API KEY>" # generated with `cscli bouncers add <bouncer_name>
148+
valueFrom:
149+
secretKeyRef:
150+
name: crowdsec-ingress-bouncer-secrets
151+
key: api-key
89152
- name: BOUNCER_CONFIG
90153
value: "/crowdsec/crowdsec-bouncer.conf"
91154
- name: CAPTCHA_PROVIDER
@@ -133,36 +196,19 @@ controller:
133196
resolver local=on ipv6=off;
134197
```
135198

136-
<details>
137-
<summary>You already have a deployed ingress nginx</summary>
138-
139-
This values.yaml upgrade your ingress deployment to add crowdsec lua lib as a
140-
plugin and run with the crowdsec maintained nginx ingress controller with lua
141-
support. It uses [this docker
199+
Use this values file to deploy or upgrade ingress-nginx with the CrowdSec Lua
200+
plugin and the CrowdSec-maintained ingress controller image with Lua support.
201+
It uses [this docker
142202
image](https://hub.docker.com/r/crowdsecurity/lua-bouncer-plugin) to copy the
143-
crowdsec lua library. You can upgrade the ingress-nginx using this `crowdsec-ingress-values.yaml`
203+
CrowdSec Lua library.
144204

145205
```bash
146-
helm -n ingress-nginx upgrade -f ingress-nginx-values.yaml -f crowdsec-ingress-values.yaml ingress-nginx/ingress-nginx
206+
helm upgrade --install ingress-nginx ingress-nginx/ingress-nginx \
207+
-n ingress-nginx \
208+
--create-namespace \
209+
-f crowdsec-ingress-values.yaml
147210
```
148211

149-
</details>
150-
151-
<details>
152-
<summary>You don't have a deployed ingress nginx</summary>
153-
154-
This values.yaml install your ingress deployment to add crowdsec lua lib as a
155-
plugin and run with the crowdsec maintained nginx ingress controller with lua
156-
support. It uses [this docker
157-
image](https://hub.docker.com/r/crowdsecurity/lua-bouncer-plugin) to copy the
158-
crowdsec lua library. You can install the ingress-nginx using this `crowdsec-ingress-values.yaml`
159-
160-
```bash
161-
helm -n ingress-nginx install -f crowdsec-ingress-values.yaml ingress-nginx ingress-nginx/ingress-nginx
162-
```
163-
164-
</details>
165-
166212
And then check if the ingress controller is running well.
167213

168214
```bash

0 commit comments

Comments
 (0)