Skip to content

[infrastructure] Add CrowdSec LAPI container to self-hosted setup script#5880

Merged
lixmal merged 2 commits intomainfrom
crowdsec-selfhosted-setup
Apr 16, 2026
Merged

[infrastructure] Add CrowdSec LAPI container to self-hosted setup script#5880
lixmal merged 2 commits intomainfrom
crowdsec-selfhosted-setup

Conversation

@lixmal
Copy link
Copy Markdown
Collaborator

@lixmal lixmal commented Apr 14, 2026

Describe your changes

Add optional CrowdSec IP reputation support to the self-hosted getting-started.sh script.

  • Prompt to enable CrowdSec when the proxy is enabled (built-in Traefik option)
  • Add CrowdSec LAPI container (pinned to v1.7.7) to docker-compose with healthcheck and depends_on
  • Wait for LAPI readiness, register a bouncer via cscli, and write the API URL/key to proxy.env
  • Print CrowdSec Console enrollment instructions at the end
  • Gracefully skip CrowdSec setup if the LAPI doesn't become ready or bouncer registration fails

Related PRs:

Stack

Checklist

  • Is it a bug fix
  • Is a typo/documentation fix
  • Is a feature enhancement
  • It is a refactor
  • Created tests that fail without the change (if possible)

By submitting this pull request, you confirm that you have read and agree to the terms of the Contributor License Agreement.

Documentation

Select exactly one:

  • I added/updated documentation for this change
  • Documentation is not needed for this change (explain why)

Docs PR URL (required if "docs added" is checked)

Paste the PR link from https://github.com/netbirdio/docs here:

netbirdio/docs#698

Summary by CodeRabbit

  • New Features

    • Optional CrowdSec IP reputation blocking during setup, enabled only when using the built‑in proxy and proxy is enabled.
    • Interactive prompt to enroll in CrowdSec and automatic storage of enrollment key when successful.
    • Built‑in proxy configuration now includes CrowdSec service and database when enabled; setup will skip CrowdSec if enrollment fails.
  • Chores

    • Cleanup process removes generated CrowdSec configuration directory.
  • Documentation

    • Post‑setup instructions include CrowdSec enrollment guidance.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 14, 2026

📝 Walkthrough

Walkthrough

Adds optional CrowdSec integration to the getting-started script: prompt, config variables, docker-compose service generation, bouncer registration with retries, conditional proxy env entries, cleanup, and post-setup enrollment instructions.

Changes

Cohort / File(s) Summary
CrowdSec integration
infrastructure_files/getting-started.sh
Adds read_enable_crowdsec() and new globals ENABLE_CROWDSEC, CROWDSEC_BOUNCER_KEY. Creates crowdsec/ dir when enabled, injects crowdsec service and crowdsec_db volume into built-in Traefik docker-compose, updates proxy depends_on, performs cscli bouncer registration with readiness polling (30 attempts), conditionally appends NB_PROXY_CROWDSEC_API_URL/NB_PROXY_CROWDSEC_API_KEY to proxy env, removes crowdsec/ on reinit, and adds post-setup enrollment instructions.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Script as getting-started.sh
    participant Traefik as Built-in Traefik (proxy)
    participant Cscli as cscli (local)
    participant CrowdSec as crowdsec container

    User->>Script: run setup (select built-in Traefik + enable proxy)
    Script->>Script: read_enable_crowdsec() prompt
    alt CrowdSec enabled
        Script->>Script: create local crowdsec/ dir & render docker-compose with crowdsec service
        Script->>Traefik: start core services (phase 1, depends_on crowdsec health)
        Traefik->>Script: proxy token created
        Script->>Cscli: attempt bouncer registration (poll readiness, up to 30 tries)
        alt cscli returns bouncer key
            Cscli->>Script: return bouncer key
            Script->>Script: persist CROWDSEC_BOUNCER_KEY
            Script->>Traefik: append NB_PROXY_CROWDSEC_API_URL/KEY to proxy env
        else readiness failed or empty key
            Script->>Script: set ENABLE_CROWDSEC="false" and skip CrowdSec setup
        end
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested reviewers

  • braginini

Poem

🐰 I hopped into scripts at break of day,

I asked, "Shall CrowdSec guard the way?"
With retries I tried to fetch the key,
Then tucked it safe where proxies see.
Hooray — the warren sleeps more soundly today.

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 27.27% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title clearly summarizes the main change: adding CrowdSec LAPI container to the self-hosted setup script. It is concise, specific, and directly reflects the primary objective of the changeset.
Description check ✅ Passed The pull request description includes a clear summary of changes, related PRs, proper checklist completion (feature enhancement selected), and documentation confirmation with a docs PR link, meeting all template requirements.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch crowdsec-selfhosted-setup

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@infrastructure_files/getting-started.sh`:
- Around line 649-653: The healthcheck currently uses "cscli capi status" which
checks Central API and can cause rate limiting; it should use "cscli lapi
status" to properly check Local API readiness for CrowdSec. Update the
healthcheck test command from "cscli capi status" to "cscli lapi status" in the
healthcheck section and similarly update the wait loop command at the specified
earlier line to use "cscli lapi status" instead of "cscli capi status".
🪄 Autofix (Beta)

❌ Autofix failed (check again to retry)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: f2c6c6fb-d416-4559-86cb-d36f2c02c8fb

📥 Commits

Reviewing files that changed from the base of the PR and between c562330 and 49b24b9.

📒 Files selected for processing (1)
  • infrastructure_files/getting-started.sh

Comment thread infrastructure_files/getting-started.sh
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 14, 2026

Note

Autofix is a beta feature. Expect some limitations and changes as we gather feedback and continue to improve it.

⚠️ Branch updated during autofix.

The branch was updated while autofix was in progress. Please try again.

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@sonarqubecloud
Copy link
Copy Markdown

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
infrastructure_files/getting-started.sh (1)

472-486: ⚠️ Potential issue | 🟡 Minor

Use cscli lapi status for the wait loop to match the healthcheck.

The wait loop uses cscli capi status while the healthcheck (line 650) correctly uses cscli lapi status. This inconsistency should be resolved—bouncer registration requires the Local API to be ready, not the Central API. Additionally, frequent cscli capi status calls can trigger rate-limiting on CrowdSec's Central API.

🔧 Suggested fix
       if [[ "$ENABLE_CROWDSEC" == "true" ]]; then
         echo "Registering CrowdSec bouncer..."
         local cs_retries=0
-        while ! $DOCKER_COMPOSE_COMMAND exec -T crowdsec cscli capi status >/dev/null 2>&1; do
+        while ! $DOCKER_COMPOSE_COMMAND exec -T crowdsec cscli lapi status >/dev/null 2>&1; do
           cs_retries=$((cs_retries + 1))
           if [[ $cs_retries -ge 30 ]]; then
             echo "WARNING: CrowdSec did not become ready. Skipping CrowdSec setup." > /dev/stderr
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@infrastructure_files/getting-started.sh` around lines 472 - 486, The wait
loop currently checks CrowdSec readiness with "cscli capi status" which queries
the Central API and can trigger rate limits; change the command used in the loop
to "cscli lapi status" so it waits on the Local API (matching the healthcheck
and ensuring bouncer registration waits for the correct service). Update the
while condition that references $DOCKER_COMPOSE_COMMAND exec -T crowdsec cscli
capi status to use cscli lapi status, keeping the retry/sleep logic, error
messages, and the ENABLE_CROWDSEC toggle unchanged.
🧹 Nitpick comments (1)
infrastructure_files/getting-started.sh (1)

447-452: Consider using an array for service names.

The unquoted $core_services relies on word splitting to pass multiple service names. While functional, using a bash array would be more explicit and avoids shellcheck warnings.

♻️ Optional improvement
     if [[ "$ENABLE_PROXY" == "true" ]]; then
       # Phase 1: Start core services (without proxy)
-      local core_services="traefik dashboard netbird-server"
+      local -a core_services=(traefik dashboard netbird-server)
       if [[ "$ENABLE_CROWDSEC" == "true" ]]; then
-        core_services="$core_services crowdsec"
+        core_services+=(crowdsec)
       fi
       echo "Starting core services..."
-      $DOCKER_COMPOSE_COMMAND up -d $core_services
+      $DOCKER_COMPOSE_COMMAND up -d "${core_services[@]}"
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@infrastructure_files/getting-started.sh` around lines 447 - 452, The code
builds a space-separated string in the local variable core_services and relies
on unquoted word splitting when calling $DOCKER_COMPOSE_COMMAND up -d
$core_services; change core_services to a Bash array (e.g., declare -a
core_services) and append items with core_services+=("traefik" "dashboard"
"netbird-server") and core_services+=("crowdsec") inside the ENABLE_CROWDSEC
branch, then invoke the compose command with "$DOCKER_COMPOSE_COMMAND" up -d
"${core_services[@]}" to avoid word-splitting issues and shellcheck warnings.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@infrastructure_files/getting-started.sh`:
- Around line 472-486: The wait loop currently checks CrowdSec readiness with
"cscli capi status" which queries the Central API and can trigger rate limits;
change the command used in the loop to "cscli lapi status" so it waits on the
Local API (matching the healthcheck and ensuring bouncer registration waits for
the correct service). Update the while condition that references
$DOCKER_COMPOSE_COMMAND exec -T crowdsec cscli capi status to use cscli lapi
status, keeping the retry/sleep logic, error messages, and the ENABLE_CROWDSEC
toggle unchanged.

---

Nitpick comments:
In `@infrastructure_files/getting-started.sh`:
- Around line 447-452: The code builds a space-separated string in the local
variable core_services and relies on unquoted word splitting when calling
$DOCKER_COMPOSE_COMMAND up -d $core_services; change core_services to a Bash
array (e.g., declare -a core_services) and append items with
core_services+=("traefik" "dashboard" "netbird-server") and
core_services+=("crowdsec") inside the ENABLE_CROWDSEC branch, then invoke the
compose command with "$DOCKER_COMPOSE_COMMAND" up -d "${core_services[@]}" to
avoid word-splitting issues and shellcheck warnings.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 25141a47-9468-4cd2-94ea-c762c95aec0b

📥 Commits

Reviewing files that changed from the base of the PR and between 49b24b9 and 6f845fb.

📒 Files selected for processing (1)
  • infrastructure_files/getting-started.sh

@lixmal lixmal merged commit a39787d into main Apr 16, 2026
46 of 48 checks passed
@lixmal lixmal deleted the crowdsec-selfhosted-setup branch April 16, 2026 16:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants