Skip to content

Commit 24c582d

Browse files
committed
add traefik for kubernetes
1 parent 9076ed8 commit 24c582d

File tree

1 file changed

+252
-55
lines changed

1 file changed

+252
-55
lines changed

crowdsec-docs/docs/appsec/quickstart/traefik.mdx

Lines changed: 252 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,112 +3,238 @@ id: traefik
33
title: QuickStart - Traefik
44
---
55

6-
import FormattedTabs from '@site/src/components/formatted-tabs';
7-
import UnderlineTooltip from '@site/src/components/underline-tooltip';
6+
import UnderlineTooltip from '@site/src/components/underline-tooltip';
7+
import Tabs from '@theme/Tabs';
8+
import TabItem from '@theme/TabItem';
89

910
# CrowdSec WAF QuickStart for Traefik
1011

1112
## Objectives
1213

13-
The goal of this quickstart is to set up the [AppSec Component](/appsec/intro.md#introduction) to safeguard web applications running on [Traefik](https://doc.traefik.io/traefik/) reverse proxy.
14-
15-
We'll deploy a [set of rules](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-virtual-patching) designed to block [well-known attacks](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-generic-rules) and [currently exploited vulnerabilities](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-virtual-patching).
16-
17-
Additionally, we'll show how to monitor these alerts through the [console](https://app.crowdsec.net/).
14+
The goal of this quickstart is to set up the [AppSec
15+
Component](/appsec/intro.md#introduction) to safeguard web applications running
16+
on [Traefik](https://doc.traefik.io/traefik/) reverse proxy. We'll deploy a [set
17+
of
18+
rules](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-virtual-patching)
19+
designed to block [well-known
20+
attacks](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-generic-rules)
21+
and [currently exploited
22+
vulnerabilities](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-virtual-patching).
23+
Additionally, we'll show how to monitor these alerts through the
24+
[console](https://app.crowdsec.net/).
1825

1926
## Pre-requisites
2027

21-
1. If you're new to the [AppSec Component](/appsec/intro.md#introduction) or **W**eb **A**pplication **F**irewalls, start with the [Introduction](/appsec/intro.md#introduction) for a better understanding.
28+
1. If you're new to the [AppSec Component](/appsec/intro.md#introduction) or
29+
**W**eb **A**pplication **F**irewalls, start with the
30+
[Introduction](/appsec/intro.md#introduction) for a better understanding.
2231

2332
2. It's assumed that you have already installed:
2433
- **CrowdSec [Security Engine](intro.mdx)**: for installation, refer to the [QuickStart guide](/u/getting_started/installation/linux). The AppSec Component, which analyzes HTTP requests, is included within the security engine as a <UnderlineTooltip tooltip="Acquisition files tell CrowdSec where to find logs and which application they belong to.">Acquisition</UnderlineTooltip>.
2534
- Traefik Plugin **[Remediation Component](/u/bouncers/intro)**: Thanks to [maxlerebourg](https://github.com/maxlerebourg) and team they created a [Traefik Plugin](https://plugins.traefik.io/plugins/6335346ca4caa9ddeffda116/crowdsec-bouncer-traefik-plugin) that allows you to block requests directly from Traefik.
2635

2736
:::info
28-
Prior to starting the guide ensure you are using the [Traefik Plugin](https://plugins.traefik.io/plugins/6335346ca4caa9ddeffda116/crowdsec-bouncer-traefik-plugin) and **NOT** the older [traefik-crowdsec-bouncer](https://app.crowdsec.net/hub/author/fbonalair/remediation-components/traefik-crowdsec-bouncer) as it hasnt received updates to use the new AppSec Component.
37+
Prior to starting the guide ensure you are using the [Traefik
38+
Plugin](https://plugins.traefik.io/plugins/6335346ca4caa9ddeffda116/crowdsec-bouncer-traefik-plugin)
39+
and **NOT** the older and deprecated
40+
[traefik-crowdsec-bouncer](https://app.crowdsec.net/hub/author/fbonalair/remediation-components/traefik-crowdsec-bouncer)
41+
as it hasnt received updates to use the new AppSec Component.
2942
:::
3043

3144
:::warning
32-
This guide will assume you already have a working Traefik setup using the Traefik Plugin. If you need help setting up Traefik, refer to the [official documentation](https://doc.traefik.io/traefik/) and the [Traefik Plugin](https://plugins.traefik.io/plugins/6335346ca4caa9ddeffda116/crowdsec-bouncer-traefik-plugin) documentation.
45+
This guide will assume you already have a working Traefik setup using the
46+
Traefik Plugin. If you need help setting up Traefik, refer to the [official
47+
documentation](https://doc.traefik.io/traefik/) and the [Traefik
48+
Plugin](https://plugins.traefik.io/plugins/6335346ca4caa9ddeffda116/crowdsec-bouncer-traefik-plugin)
49+
documentation.
3350
:::
3451

3552
## AppSec Component Setup
3653

3754
### Collection installation
3855

39-
To begin setting up the AppSec Component, the initial step is to install a relevant set of rules.
56+
To begin setting up the AppSec Component, the initial step is to install a
57+
relevant set of rules.
4058

41-
We will utilize the [crowdsecurity/appsec-virtual-patching](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-virtual-patching) collection, which offers a wide range of rules aimed at identifying and preventing the exploitation of known vulnerabilities.
59+
We will utilize the
60+
[crowdsecurity/appsec-virtual-patching](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-virtual-patching)
61+
collection, which offers a wide range of rules aimed at identifying and
62+
preventing the exploitation of known vulnerabilities.
4263

43-
This <UnderlineTooltip tooltip="Collections are bundle of parsers, scenarios, postoverflows that form a coherent package.">collection</UnderlineTooltip> is regularly updated to include protection against newly discovered vulnerabilities. Upon installation, it receives automatic daily updates to ensure your protection is always current.
64+
This <UnderlineTooltip tooltip="Collections are bundle of parsers, scenarios,
65+
postoverflows that form a coherent package.">collection</UnderlineTooltip> is
66+
regularly updated to include protection against newly discovered
67+
vulnerabilities. Upon installation, it receives automatic daily updates to
68+
ensure your protection is always current.
4469

45-
Furthermore we also install the [crowdsecurity/appsec-generic-rules](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-generic-rules) collection. This collection contains detection scenarios for generic attack vectors. It provides some protection in cases where specific scenarios for vulnerabilities do not exist (yet).
70+
Furthermore we also install the
71+
[crowdsecurity/appsec-generic-rules](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-generic-rules)
72+
collection. This collection contains detection scenarios for generic attack
73+
vectors. It provides some protection in cases where specific scenarios for
74+
vulnerabilities do not exist (yet).
4675

47-
On the machine where the Security Engine is installed, just execute the following command:
4876

4977
:::info
5078
You can always view the content of a [collection on the hub](https://app.crowdsec.net/hub/author/crowdsecurity/collections/appsec-virtual-patching)
5179
:::
5280

53-
<FormattedTabs
54-
docker={`## This command should be used when you are persisting /etc/crowdsec/ on the host
55-
docker exec -it crowdsec cscli collections install crowdsecurity/appsec-virtual-patching crowdsecurity/appsec-generic-rules`}
56-
dockerCompose={`services:
81+
<Tabs
82+
groupId="crowdsec-appsec-collection"
83+
defaultValue="docker"
84+
values={[
85+
{ label: 'Docker', value: 'docker' },
86+
{ label: 'Docker Compose', value: 'dockerCompose' },
87+
{ label: 'Kubernetes (Helm)', value: 'kubernetes' },
88+
]}
89+
>
90+
91+
<TabItem value="docker">
92+
93+
```bash
94+
## This command should be used when you are persisting /etc/crowdsec/ on the host
95+
docker exec -it crowdsec cscli collections install crowdsecurity/appsec-virtual-patching crowdsecurity/appsec-generic-rules
96+
```
97+
98+
This command installs the needed appsec hub configuration items.
99+
100+
</TabItem>
101+
<TabItem value="dockerCompose">
102+
103+
```yaml title="values.yaml"
104+
services:
57105
crowdsec:
58-
environment
59-
## Please note the spaces between the collections names (hence why the quotes are needed)
60-
- 'COLLECTIONS=crowdsecurity/appsec-virtual-patching crowdsecurity/appsec-generic-rules'`}
61-
/>
106+
environment:
107+
- 'COLLECTIONS=crowdsecurity/appsec-virtual-patching crowdsecurity/appsec-generic-rules'
108+
```
109+
110+
:::warning
111+
Please note the spaces between the collection names (hence why the quotes are needed).
112+
:::
62113
63-
Executing this command or updating the compose will install the following items:
114+
This compose configuration file will add some needed hub configuration items.
64115
65-
- The [*AppSec Rules*](/appsec/rules_syntax.md) contain the definition of malevolent requests to be matched and stopped.
66-
- The [*AppSec Configuration*](/appsec/configuration.md#appsec-configuration-files) links together a set of rules to provide a coherent set.
67-
- The <UnderlineTooltip tooltip="YAML files that extract relevant data from logs, such as IP addresses, timestamps, or request paths.">CrowdSec Parser</UnderlineTooltip> and <UnderlineTooltip tooltip="Behavioral rules written in a domain-specific language that define what malicious activity looks like, such as multiple failed logins in a short time.">CrowdSec Scenario(s)</UnderlineTooltip> are used to detect and remediate persistent attacks.
116+
</TabItem>
117+
<TabItem value="kubernetes">
68118
69-
Once you have updated your compose or installed via the command line, will we need to restart the container. However, before we do that, we need to setup the acquisition for the AppSec Component.
119+
Please add this in your `values.yaml` for your CrowdSec release.
120+
121+
```yaml title="values.yaml"
122+
appsec:
123+
env:
124+
- name: COLLECTIONS
125+
value: "[...] crowdsecurity/appsec-virtual-patching crowdsecurity/appsec-generic-rules [...]"
126+
```
127+
128+
129+
:::warning
130+
Please note the spaces between the collection names (hence why the double quotes are needed).
131+
:::
132+
133+
Now you can apply it with:
134+
```
135+
helm upgrade crowdsec crowdsec/crowdsec -n crowdsec --create-namespace -f ./crowdsec-values.yaml
136+
```
137+
138+
This `values.yaml` modification will add some needed hub configuration items.
139+
140+
141+
</TabItem>
142+
</Tabs>
143+
144+
Those needed hub configuration items are:
145+
146+
- The [*AppSec Rules*](/appsec/rules_syntax.md) contain the definition of
147+
malevolent requests to be matched and stopped.
148+
- The [*AppSec
149+
Configuration*](/appsec/configuration.md#appsec-configuration-files) links
150+
together a set of rules to provide a coherent set.
151+
- The <UnderlineTooltip tooltip="YAML files that extract relevant data from
152+
logs, such as IP addresses, timestamps, or request paths.">CrowdSec
153+
Parser</UnderlineTooltip> and <UnderlineTooltip tooltip="Behavioral rules
154+
written in a domain-specific language that define what malicious activity
155+
looks like, such as multiple failed logins in a short time.">CrowdSec
156+
Scenario(s)</UnderlineTooltip> are used to detect and remediate persistent
157+
attacks.
158+
159+
Once you have updated your compose or installed via the command line, will we
160+
need to restart the container. However, before we do that, we need to setup the
161+
acquisition for the AppSec Component.
70162

71163
### Setup the Acquisition
72164

73-
Depending on how you are running the CrowdSec Security Engine, you will need to configure the acquisition for the AppSec Component.
165+
You now need to setup the acquisition for AppSec. The way it's done highly
166+
depends on how you run CrowdSec.
167+
168+
<Tabs
169+
groupId="crowdsec-appsec-acquisition"
170+
defaultValue="docker"
171+
values={[
172+
{ label: 'Docker', value: 'docker' },
173+
{ label: 'Docker Compose', value: 'dockerCompose' },
174+
{ label: 'Kubernetes (Helm)', value: 'kubernetes' },
175+
]}
176+
>
74177

75-
If you have a folder in which you are persisting the configuration files, you can create a `appsec.yaml` and mount it into the container.
178+
<TabItem value="docker">
76179

77-
There steps will change depending on how you are running the Security Engine. If you are running via `docker run` then you should launch the container within the same directory as the `appsec.yaml` file. If you are using `docker-compose` you can use a relative file mount to mount the `appsec.yaml` file.
180+
In the directory where you persist configuration files, create an `appsec.yaml` file and mount it into the container.
78181

79-
Steps:
80-
1. Change to the location where you executed the `docker run` or `docker compose` command.
81-
2. Create a `appsec.yaml` file at the base of the directory.
82-
3. Add the following content to the `appsec.yaml` file.
182+
**Steps**
183+
184+
1. Change to the directory where you ran the `docker run` or `docker compose` command.
185+
2. Create a file named `appsec.yaml` in this directory.
186+
3. Add the following content:
83187

84188
```yaml title="appsec.yaml"
85-
appsec_config: crowdsecurity/appsec-default
189+
appsec_config: crowdsecurity/appsec-desfault
86190
labels:
87-
type: appsec
191+
type: appsec
88192
listen_addr: 0.0.0.0:7422
89193
source: appsec
90194
```
91-
:::note
92-
Since CrowdSec is running inside a container you must set the `listen_addr` to `0.0.0.0` instead of the typical `127.0.0.1` as the container is running in a separate network.
93-
:::
94195

95-
4. Edit the `docker run` or `docker-compose` command to include the `appsec.yaml` file.
196+
Because CrowdSec runs inside a container, set listen_addr to 0.0.0.0 instead of
197+
127.0.0.1 so it can accept connections from outside the container.
96198

97-
<FormattedTabs
98-
docker={`# Note if you have a docker run already running you will need to stop it before running this command
99-
docker run -d --name crowdsec -v /path/to/original:/etc/crowdsec -v ./appsec.yaml:/etc/crowdsec/acquis.d/appsec.yaml crowdsecurity/crowdsec`}
100-
dockerCompose={`services:
101-
crowdsec:
102-
volumes:
103-
- /path/to/original:/etc/crowdsec ## or named volumes
104-
- ./appsec.yaml:/etc/crowdsec/acquis.d/appsec.yaml`}
105-
/>
199+
Edit your docker run command to mount the file:
106200

107-
Once you have created the `appsec.yaml` file and mounted it into the container, you can recreate the container.
201+
If a crowdsec container is already running, stop/remove it before re-running with the updated mounts.
108202

109-
:::note
110-
If you are using `docker run` you can skip to the [Remediation Component Setup](#remediation-component-setup) section.
111-
:::
203+
```bash
204+
docker run -d --name crowdsec \
205+
-v /path/to/original:/etc/crowdsec \
206+
-v ./appsec.yaml:/etc/crowdsec/acquis.d/appsec.yaml \
207+
crowdsecurity/crowdsec
208+
```
209+
210+
</TabItem>
211+
212+
<TabItem value="dockerCompose">
213+
214+
In the directory where you persist configuration files, create an appsec.yaml file and mount it into the container.
215+
216+
**Steps**
217+
218+
1. Change to the directory where you ran the docker compose (or docker run) command.
219+
2. Create a file named appsec.yaml in this directory.
220+
3. Add the following content to the `appsec.yaml`
221+
222+
appsec_config: crowdsecurity/appsec-default
223+
labels:
224+
type: appsec
225+
listen_addr: 0.0.0.0:7422
226+
source: appsec
227+
228+
Because CrowdSec runs in a container, set listen_addr to 0.0.0.0 (not 127.0.0.1) so it listens on the container’s network interface.
229+
230+
Mount the file in your Compose service:
231+
```
232+
services:
233+
crowdsec:
234+
volumes:
235+
- /path/to/original:/etc/crowdsec # or a named volume
236+
- ./appsec.yaml:/etc/crowdsec/acquis.d/appsec.yaml
237+
```
112238

113239
Once you have updated the compose file to include the volume mount and the updated environment variable, you can restart the container.
114240

@@ -122,6 +248,27 @@ docker compose up -d crowdsec
122248
The previous compose commands presume the container is named `crowdsec`. If you have named the container something else, you will need to replace `crowdsec` with the name of your container.
123249
:::
124250

251+
</TabItem>
252+
<TabItem value="kubernetes">
253+
With kubernetes the acquisition setup is twofolds:
254+
We have to add
255+
```yaml title="values.yaml"
256+
appsec:
257+
acquisitions:
258+
- appsec_config: crowdsecurity/appsec-default
259+
labels:
260+
type: appsec
261+
listen_addr: 0.0.0.0:7422
262+
path: /
263+
source: appsec
264+
enabled: true
265+
```
266+
267+
268+
</TabItem>
269+
</Tabs>
270+
271+
125272
## Remediation Component Setup
126273

127274
As stated previously this guide already presumes you have the Traefik Plugin installed. If you do not have the Traefik Plugin installed, please refer to the [official documentation](https://plugins.traefik.io/plugins/6335346ca4caa9ddeffda116/crowdsec-bouncer-traefik-plugin) for installation instructions.
@@ -134,6 +281,15 @@ Depending on how you configured the Traefik Plugin, you will need to update the
134281
Currently AppSec does not support mTLS authentication for the AppSec Component. If you have mTLS enabled, and wish to use the AppSec Component, you can define seperate middlewares for the AppSec Component.
135282
:::
136283

284+
<Tabs
285+
groupId="traefik-remediation"
286+
defaultValue="dynamic"
287+
values={[
288+
{ label: 'Traefik dynamic configuration', value: 'dynamic' },
289+
{ label: 'Traefik middleware (Kubernetes)', value: 'kubernetes' },
290+
]}
291+
>
292+
<TabItem value="dynamic">
137293
If you have defined a dynamic configuration file for Traefik, you can add the following configuration to the file.
138294

139295
```yaml title="traefik_dynamic.yaml"
@@ -166,6 +322,7 @@ http:
166322
crowdsecLapiKey: privateKey-foo
167323
```
168324
325+
169326
Instead if you define the configuration using labels on the containers you can add the following labels to the Traefik Plugin container.
170327
171328
```yaml
@@ -175,8 +332,48 @@ Instead if you define the configuration using labels on the containers you can a
175332
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.crowdsecAppsecHost=crowdsec:7422"
176333
- "traefik.http.middlewares.crowdsec-bar.plugin.bouncer.crowdsecLapiKey=privateKey-foo"
177334
```
335+
</TabItem>
336+
<TabItem value="kubernetes">
337+
Here's a Traefik Middleware ressource you can apply with
338+
```bash
339+
kubectl apply -f traefik-middleware.yaml
340+
```
341+
342+
```yaml values="traefik-middleware.yaml"
343+
apiVersion: traefik.io/v1alpha1
344+
kind: Middleware
345+
metadata:
346+
name: crowdsec
347+
namespace: traefik
348+
spec:
349+
plugin:
350+
crowdsec-bouncer-traefik-plugin:
351+
enabled: true
352+
crowdsecMode: stream
353+
crowdsecLapiScheme: http
354+
crowdsecLapiHost: crowdsec-service.crowdsec.svc.cluster.local:8080
355+
crowdsecLapiKey: <shadowed>
356+
htttTimeoutSeconds: 60
357+
forwardedheaderstrustedips:
358+
- 10.0.0.0/8
359+
- 192.168.0.0/16
360+
- 134.209.137.94
361+
- 2a03:b0c0:2:f0::f557:a001
362+
crowdsecAppsecEnabled: false
363+
crowdsecAppsecHost: crowdsec:7422
364+
crowdsecAppsecFailureBlock: true
365+
crowdsecAppsecUnreachableBlock: true
366+
```
367+
368+
You can still add some route configuration through
369+
[IngressRoute](https://doc.traefik.io/traefik/reference/routing-configuration/kubernetes/crd/http/ingressroute/?utm_source=chatgpt.com)
370+
and attach the middleware to those routes.
371+
</TabItem>
372+
</Tabs>
178373
179-
For more comprehensive documentation on the Traefik Plugin configuration, please refer to the [official documentation](https://plugins.traefik.io/plugins/6335346ca4caa9ddeffda116/crowdsec-bouncer-traefik-plugin).
374+
For more comprehensive documentation on the Traefik Plugin configuration, please
375+
refer to the [official
376+
documentation](https://plugins.traefik.io/plugins/6335346ca4caa9ddeffda116/crowdsec-bouncer-traefik-plugin).
180377
181378
We can't cover all the possible configurations for Traefik in this guide, so please refer to the [official documentation](https://doc.traefik.io/traefik/) for more information.
182379

0 commit comments

Comments
 (0)