Hello everyone, new user here,
First of all, thanks for the creator of Crowdsec, I was always looking for a more container compatible fail2ban and your solution seems interesting. So much so, that I’m on the way to right a Traefik bouncer to fit my need. (You can found the work in progress here).
I’ve manage to make Crowdsec work with my Kubernetes cluster … with the barebone docker container and my custom bouncer. My problem is managing to keep data between restart of container and more specifically bouncer API key. For that I want to use my Postgresql database (another container postgres:12.4-alpine):
I’ve followed the doc to create user and database with the following init script:
init-crowdsec.sh : |
#!/bin/bash
set -e
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER $CROWDSEC_USER PASSWORD '$CROWDSEC_PASSWORD';
CREATE DATABASE $CROWDSEC_USER;
GRANT ALL PRIVILEGES ON DATABASE $CROWDSEC_SCHEMA TO $CROWDSEC_USER;
EOSQL
I have create a custom config.yaml from a copy of the container’s one:
common:
daemonize: false
pid_dir: /var/run/
log_media: stdout
log_level: info
log_dir: /var/log/
working_dir: .
config_paths:
config_dir: /etc/crowdsec/
data_dir: /var/lib/crowdsec/data/
simulation_path: /etc/crowdsec/simulation.yaml
hub_dir: /etc/crowdsec/hub/
index_path: /etc/crowdsec/hub/.index.json
notification_dir: /etc/crowdsec/notifications/
plugin_dir: /usr/local/lib/crowdsec/plugins/
crowdsec_service:
acquisition_path: /etc/crowdsec/acquis.yaml
parser_routines: 1
plugin_config:
user: nobody
group: nobody
cscli:
output: human
db_config:
log_level: info
type: postgres
user: ${CROWDSEC_USER}
password: ${POSTGRES_PASSWORD}
db_name: ${CROWDSEC_SCHEMA}
host: postgresql.XXX.XXX.XXX.XXX # sanitized
port: 5432
sslmode: disable
flush:
max_items: 5000
max_age: 7d
api:
client:
insecure_skip_verify: false
credentials_path: /etc/crowdsec/local_api_credentials.yaml
server:
log_level: info
listen_uri: 0.0.0.0:8080
profiles_path: /etc/crowdsec/profiles.yaml
online_client: # Central API credentials (to push signals and receive bad IPs)
credentials_path: /etc/crowdsec/online_api_credentials.yaml
# tls:
# cert_file: /etc/crowdsec/ssl/cert.pem
# key_file: /etc/crowdsec/ssl/key.pem
prometheus:
enabled: true
level: full
listen_addr: 0.0.0.0
listen_port: 6060
Checking the database’s schema, I see a couple of empty tables created (bouncer, alerts, decision etc) so I guess database connection if fine. Also, too be sure it is not a variable problem, I’ve both check env values with echo $ENV and directly write values in the file.
After a restart, from the logs it seems like crowdsec can’t access it’s API:
fbonalair:~$ kubectl -n kube-system logs crowdsec-agent-765d965f9f-542kx --previous
Check if the container has already been started (ignore if agent is disabled)
time="03-10-2021 02:03:58 PM" level=info msg="push and pull to Central API disabled"
localhost 127.0.0.1 2021-10-03T14:00:42+02:00 ✔️ v1.2.0-docker-0ecfe7568790a15791011da27eb24e96e7d4a39f
Check if lapi need to register automatically an agent
registering agent localhost to lapi
AGENT PASSWORD : test
time="03-10-2021 02:03:58 PM" level=info msg="push and pull to Central API disabled"
time="03-10-2021 02:03:58 PM" level=fatal msg="unable to create machine: user 'localhost': user already exist"
time="03-10-2021 02:03:58 PM" level=warning msg="can't load CAPI credentials from '/etc/crowdsec/online_api_credentials.yaml' (missing field)"
time="03-10-2021 02:03:58 PM" level=info msg="push and pull to Central API disabled"
time="03-10-2021 02:03:58 PM" level=info msg="Successfully registered to Central API (CAPI)"
time="03-10-2021 02:03:58 PM" level=info msg="Central API credentials dumped to '/etc/crowdsec/online_api_credentials.yaml'"
time="03-10-2021 02:03:58 PM" level=warning msg="Run 'sudo systemctl reload crowdsec' for the new configuration to be effective."
time="03-10-2021 02:03:59 PM" level=info msg="Wrote new 138822 bytes index to /etc/crowdsec/hub/.index.json"
time="03-10-2021 02:03:59 PM" level=info msg="update for collection crowdsecurity/sshd available (currently:0.1, latest:0.2)"
time="03-10-2021 02:03:59 PM" level=info msg="dependency of crowdsecurity/linux : outdated collections crowdsecurity/sshd"
time="03-10-2021 02:03:59 PM" level=warning msg="crowdsecurity/syslog-logs : overwrite"
time="03-10-2021 02:03:59 PM" level=warning msg="crowdsecurity/geoip-enrich : overwrite"
time="03-10-2021 02:03:59 PM" level=warning msg="crowdsecurity/dateparse-enrich : overwrite"
time="03-10-2021 02:03:59 PM" level=warning msg="crowdsecurity/sshd-logs : overwrite"
time="03-10-2021 02:03:59 PM" level=warning msg="crowdsecurity/ssh-bf : overwrite"
time="03-10-2021 02:03:59 PM" level=info msg="crowdsecurity/ssh-slow-bf : OK"
time="03-10-2021 02:03:59 PM" level=info msg="Enabled scenarios : crowdsecurity/ssh-slow-bf"
time="03-10-2021 02:03:59 PM" level=warning msg="crowdsecurity/sshd : overwrite"
time="03-10-2021 02:03:59 PM" level=warning msg="crowdsecurity/sshd : overwrite"
time="03-10-2021 02:03:59 PM" level=warning msg="crowdsecurity/linux : overwrite"
time="03-10-2021 02:03:59 PM" level=info msg="📦 crowdsecurity/linux : updated"
time="03-10-2021 02:03:59 PM" level=info msg="Upgraded 1 items"
time="03-10-2021 02:03:59 PM" level=info msg="Run 'sudo systemctl reload crowdsec' for the new configuration to be effective."
time="03-10-2021 02:03:59 PM" level=info msg="crowdsecurity/whitelists : up-to-date"
time="03-10-2021 02:03:59 PM" level=info msg="Item 'crowdsecurity/whitelists' is up-to-date"
time="03-10-2021 02:03:59 PM" level=info msg="Run 'sudo systemctl reload crowdsec' for the new configuration to be effective."
time="03-10-2021 14:03:59" level=info msg="Crowdsec v1.2.0-docker-0ecfe7568790a15791011da27eb24e96e7d4a39f"
time="03-10-2021 14:03:59" level=info msg="Loading prometheus collectors"
time="03-10-2021 14:03:59" level=info msg="Loading CAPI pusher"
time="03-10-2021 14:03:59" level=info msg="start crowdsec api push (interval: 30s)"
time="03-10-2021 14:03:59" level=info msg="start crowdsec api pull (interval: 2h)"
time="03-10-2021 14:03:59" level=info msg="last CAPI pull is newer than 1h30, skip."
time="03-10-2021 14:03:59" level=info msg="start crowdsec api send metrics (interval: 30m)"
time="03-10-2021 14:03:59" level=info msg="Loading grok library /etc/crowdsec/patterns"
time="03-10-2021 14:03:59" level=info msg="Error machine login for : ent: machine not found "
time="03-10-2021 14:03:59" level=info msg="127.0.0.1 - [Sun, 03 Oct 2021 14:03:59 CEST] \"POST /v1/watchers/login HTTP/1.1 401 837.822µs \"crowdsec/v1.2.0-docker-0ecfe7568790a15791011da27eb24e96e7d4a39f\" \""
time="03-10-2021 14:04:01" level=info msg="Loading enrich plugins"
time="03-10-2021 14:04:01" level=info msg="Successfully registered enricher 'GeoIpCity'"
time="03-10-2021 14:04:01" level=info msg="Successfully registered enricher 'GeoIpASN'"
time="03-10-2021 14:04:01" level=info msg="Successfully registered enricher 'IpToRange'"
time="03-10-2021 14:04:01" level=info msg="Successfully registered enricher 'reverse_dns'"
time="03-10-2021 14:04:01" level=info msg="Successfully registered enricher 'ParseDate'"
time="03-10-2021 14:04:01" level=info msg="Loading parsers 5 stages"
time="03-10-2021 14:04:01" level=info msg="Loaded 2 parser nodes" file=/etc/crowdsec/parsers/s00-raw/syslog-logs.yaml
time="03-10-2021 14:04:01" level=info msg="Loaded 1 parser nodes" file=/etc/crowdsec/parsers/s01-parse/sshd-logs.yaml
time="03-10-2021 14:04:01" level=info msg="Loaded 1 parser nodes" file=/etc/crowdsec/parsers/s02-enrich/dateparse-enrich.yaml
time="03-10-2021 14:04:01" level=info msg="Loaded 1 parser nodes" file=/etc/crowdsec/parsers/s02-enrich/geoip-enrich.yaml
time="03-10-2021 14:04:01" level=info msg="Loaded 1 parser nodes" file=/etc/crowdsec/parsers/s02-enrich/whitelists.yaml
time="03-10-2021 14:04:01" level=info msg="Loaded 6 nodes, 3 stages"
time="03-10-2021 14:04:01" level=info msg="Loading postoverflow Parsers"
time="03-10-2021 14:04:01" level=info msg="Loaded 0 nodes, 0 stages"
time="03-10-2021 14:04:01" level=info msg="Loading 2 scenario files"
time="03-10-2021 14:04:01" level=info msg="Adding leaky bucket" cfg=autumn-glitter file=/etc/crowdsec/scenarios/ssh-slow-bf.yaml name=crowdsecurity/ssh-slow-bf
time="03-10-2021 14:04:01" level=info msg="Adding leaky bucket" cfg=polished-snow file=/etc/crowdsec/scenarios/ssh-slow-bf.yaml name=crowdsecurity/ssh-slow-bf_user-enum
time="03-10-2021 14:04:01" level=info msg="Adding leaky bucket" cfg=bitter-sea file=/etc/crowdsec/scenarios/ssh-bf.yaml name=crowdsecurity/ssh-bf
time="03-10-2021 14:04:01" level=info msg="Adding leaky bucket" cfg=muddy-water file=/etc/crowdsec/scenarios/ssh-bf.yaml name=crowdsecurity/ssh-bf_user-enum
time="03-10-2021 14:04:01" level=warning msg="Loaded 4 scenarios"
time="03-10-2021 14:04:01" level=info msg="loading acquisition file : /etc/crowdsec/acquis.yaml"
time="03-10-2021 14:04:01" level=warning msg="No matching files for pattern /var/log/nginx/*.log" type=file
time="03-10-2021 14:04:01" level=warning msg="No matching files for pattern ./tests/nginx/nginx.log" type=file
time="03-10-2021 14:04:01" level=warning msg="No matching files for pattern /var/log/auth.log" type=file
time="03-10-2021 14:04:01" level=warning msg="No matching files for pattern /var/log/syslog" type=file
time="03-10-2021 14:04:01" level=warning msg="No matching files for pattern /var/log/apache2/*.log" type=file
time="03-10-2021 14:04:01" level=warning msg="Starting processing data"
time="03-10-2021 14:04:01" level=info msg="Error machine login for : ent: machine not found "
time="03-10-2021 14:04:01" level=info msg="127.0.0.1 - [Sun, 03 Oct 2021 14:04:01 CEST] \"POST /v1/watchers/login HTTP/1.1 401 718.993µs \"crowdsec/v1.2.0-docker-0ecfe7568790a15791011da27eb24e96e7d4a39f\" \""
time="03-10-2021 14:04:01" level=fatal msg="starting outputs error : authenticate watcher (): Post \"http://127.0.0.1:8080/v1/watchers/login\": API error: ent: machine not found"
time="03-10-2021 14:04:01" level=fatal msg="starting outputs error : authenticate watcher (): Post \"http://127.0.0.1:8080/v1/watchers/login\": API error: ent: machine not found"
To be sure, I’ve tried sqlite as database, by mounting a volume for the folder /var/lib/crowdsec/data/
. The container take much more time to start, work on first time but seems like I’ve got the same problem on restart:
fbonalair:~$ kubectl -n kube-system logs crowdsec-agent-765d965f9f-sx4cz --previous
unable to retrieve container logs for containerd://71a0e4a1dd5707237e8498e599833e404798b71523ac3715253c69a245436888fbonalair@popos-workstation:~$ kubectl -n kube-system logs crowdsec-agent-765d965f9f-sx4cz --previous
Check if the container has already been started (ignore if agent is disabled)
time="03-10-2021 02:50:56 PM" level=info msg="push and pull to Central API disabled"
localhost 127.0.0.1 2021-10-03T14:49:59+02:00 ✔️ v1.2.0-docker-0ecfe7568790a15791011da27eb24e96e7d4a39f
Check if lapi need to register automatically an agent
time="03-10-2021 02:50:56 PM" level=warning msg="can't load CAPI credentials from '/etc/crowdsec/online_api_credentials.yaml' (missing field)"
time="03-10-2021 02:50:56 PM" level=info msg="push and pull to Central API disabled"
time="03-10-2021 02:50:56 PM" level=info msg="Successfully registered to Central API (CAPI)"
time="03-10-2021 02:50:56 PM" level=info msg="Central API credentials dumped to '/etc/crowdsec/online_api_credentials.yaml'"
time="03-10-2021 02:50:56 PM" level=warning msg="Run 'sudo systemctl reload crowdsec' for the new configuration to be effective."
time="03-10-2021 02:50:57 PM" level=info msg="Wrote new 138822 bytes index to /etc/crowdsec/hub/.index.json"
time="03-10-2021 02:50:57 PM" level=info msg="dependency of crowdsecurity/linux : outdated collections crowdsecurity/sshd"
time="03-10-2021 02:50:57 PM" level=info msg="update for collection crowdsecurity/sshd available (currently:0.1, latest:0.2)"
time="03-10-2021 02:50:57 PM" level=warning msg="crowdsecurity/syslog-logs : overwrite"
time="03-10-2021 02:50:57 PM" level=warning msg="crowdsecurity/geoip-enrich : overwrite"
time="03-10-2021 02:50:57 PM" level=warning msg="crowdsecurity/dateparse-enrich : overwrite"
time="03-10-2021 02:50:57 PM" level=warning msg="crowdsecurity/sshd-logs : overwrite"
time="03-10-2021 02:50:57 PM" level=warning msg="crowdsecurity/ssh-bf : overwrite"
time="03-10-2021 02:50:57 PM" level=info msg="crowdsecurity/ssh-slow-bf : OK"
time="03-10-2021 02:50:57 PM" level=info msg="Enabled scenarios : crowdsecurity/ssh-slow-bf"
time="03-10-2021 02:50:57 PM" level=warning msg="crowdsecurity/sshd : overwrite"
time="03-10-2021 02:50:57 PM" level=warning msg="crowdsecurity/sshd : overwrite"
time="03-10-2021 02:50:57 PM" level=warning msg="crowdsecurity/linux : overwrite"
time="03-10-2021 02:50:57 PM" level=info msg="📦 crowdsecurity/linux : updated"
time="03-10-2021 02:50:57 PM" level=info msg="Upgraded 1 items"
time="03-10-2021 02:50:57 PM" level=info msg="Run 'sudo systemctl reload crowdsec' for the new configuration to be effective."
time="03-10-2021 02:50:57 PM" level=info msg="crowdsecurity/whitelists : up-to-date"
time="03-10-2021 02:50:57 PM" level=info msg="Item 'crowdsecurity/whitelists' is up-to-date"
time="03-10-2021 02:50:57 PM" level=info msg="Run 'sudo systemctl reload crowdsec' for the new configuration to be effective."
time="03-10-2021 14:50:57" level=info msg="Crowdsec v1.2.0-docker-0ecfe7568790a15791011da27eb24e96e7d4a39f"
time="03-10-2021 14:50:57" level=info msg="Loading prometheus collectors"
time="03-10-2021 14:50:57" level=info msg="Loading CAPI pusher"
time="03-10-2021 14:50:57" level=info msg="start crowdsec api push (interval: 30s)"
time="03-10-2021 14:50:57" level=info msg="start crowdsec api pull (interval: 2h)"
time="03-10-2021 14:50:57" level=info msg="start crowdsec api send metrics (interval: 30m)"
time="03-10-2021 14:50:57" level=info msg="last CAPI pull is newer than 1h30, skip."
time="03-10-2021 14:50:57" level=info msg="Loading grok library /etc/crowdsec/patterns"
time="03-10-2021 14:50:59" level=info msg="Loading enrich plugins"
time="03-10-2021 14:50:59" level=info msg="Successfully registered enricher 'GeoIpCity'"
time="03-10-2021 14:50:59" level=info msg="Successfully registered enricher 'GeoIpASN'"
time="03-10-2021 14:50:59" level=info msg="Successfully registered enricher 'IpToRange'"
time="03-10-2021 14:50:59" level=info msg="Successfully registered enricher 'reverse_dns'"
time="03-10-2021 14:50:59" level=info msg="Successfully registered enricher 'ParseDate'"
time="03-10-2021 14:50:59" level=info msg="Loading parsers 5 stages"
time="03-10-2021 14:50:59" level=info msg="Loaded 2 parser nodes" file=/etc/crowdsec/parsers/s00-raw/syslog-logs.yaml
time="03-10-2021 14:50:59" level=info msg="Loaded 1 parser nodes" file=/etc/crowdsec/parsers/s01-parse/sshd-logs.yaml
time="03-10-2021 14:50:59" level=info msg="Loaded 1 parser nodes" file=/etc/crowdsec/parsers/s02-enrich/dateparse-enrich.yaml
time="03-10-2021 14:50:59" level=info msg="Loaded 1 parser nodes" file=/etc/crowdsec/parsers/s02-enrich/geoip-enrich.yaml
time="03-10-2021 14:50:59" level=info msg="Loaded 1 parser nodes" file=/etc/crowdsec/parsers/s02-enrich/whitelists.yaml
time="03-10-2021 14:50:59" level=info msg="Loaded 6 nodes, 3 stages"
time="03-10-2021 14:50:59" level=info msg="Loading postoverflow Parsers"
time="03-10-2021 14:50:59" level=info msg="Loaded 0 nodes, 0 stages"
time="03-10-2021 14:50:59" level=info msg="Loading 2 scenario files"
time="03-10-2021 14:50:59" level=info msg="Adding leaky bucket" cfg=bold-night file=/etc/crowdsec/scenarios/ssh-slow-bf.yaml name=crowdsecurity/ssh-slow-bf
time="03-10-2021 14:50:59" level=info msg="Adding leaky bucket" cfg=dry-meadow file=/etc/crowdsec/scenarios/ssh-slow-bf.yaml name=crowdsecurity/ssh-slow-bf_user-enum
time="03-10-2021 14:50:59" level=info msg="Adding leaky bucket" cfg=polished-snowflake file=/etc/crowdsec/scenarios/ssh-bf.yaml name=crowdsecurity/ssh-bf
time="03-10-2021 14:50:59" level=info msg="Adding leaky bucket" cfg=dark-pine file=/etc/crowdsec/scenarios/ssh-bf.yaml name=crowdsecurity/ssh-bf_user-enum
time="03-10-2021 14:50:59" level=warning msg="Loaded 4 scenarios"
time="03-10-2021 14:50:59" level=info msg="loading acquisition file : /etc/crowdsec/acquis.yaml"
time="03-10-2021 14:50:59" level=warning msg="No matching files for pattern /var/log/nginx/*.log" type=file
time="03-10-2021 14:50:59" level=warning msg="No matching files for pattern ./tests/nginx/nginx.log" type=file
time="03-10-2021 14:50:59" level=warning msg="No matching files for pattern /var/log/auth.log" type=file
time="03-10-2021 14:50:59" level=warning msg="No matching files for pattern /var/log/syslog" type=file
time="03-10-2021 14:50:59" level=warning msg="No matching files for pattern /var/log/apache2/*.log" type=file
time="03-10-2021 14:50:59" level=warning msg="Starting processing data"
time="03-10-2021 14:50:59" level=info msg="Error machine login for : ent: machine not found "
time="03-10-2021 14:50:59" level=info msg="127.0.0.1 - [Sun, 03 Oct 2021 14:50:59 CEST] \"POST /v1/watchers/login HTTP/1.1 401 593.839µs \"crowdsec/v1.2.0-docker-0ecfe7568790a15791011da27eb24e96e7d4a39f\" \""
time="03-10-2021 14:50:59" level=fatal msg="starting outputs error : authenticate watcher (): Post \"http://127.0.0.1:8080/v1/watchers/login\": API error: ent: machine not found"
time="03-10-2021 14:50:59" level=fatal msg="starting outputs error : authenticate watcher (): Post \"http://127.0.0.1:8080/v1/watchers/login\": API error: ent: machine not found"
Anybody have an idea were the problem can be?
PS: I have also tried to mount /etc/crowdsec/
to a Kubernetes volume but K8s mount an empty folder and Crowdsec don’t like it, even it I put the config.yaml aside and change its path with env var CONFIG_FILE