Skip to content

Commit

Permalink
feat: add a devcontainer for easier development
Browse files Browse the repository at this point in the history
This change adds a devcontainer that properly sets up a test environment
such that a user has a working `cscli` and `crowdsec` command for their
hub checkout.  This makes it much easier to developer parsers in this
repository, greatly reducing the barrier to entry.

This change also adds a workflow to make sure the devcontainer can
build.  This could be further extended to also run tests in the
devcontainer.

I verified this works by running the example test command in the
`README.md`:
```sh
cscli hubtest run dovecot-logs
INFO[26-02-2025 19:00:00] Running test 'dovecot-logs'                  
INFO[26-02-2025 19:00:03] Test 'dovecot-logs' passed successfully (387 assertions) 
───────────────────────
 Test           Result 
───────────────────────
 dovecot-logs   ✅     
───────────────────────
```

Additionally, I ran all tests with `cscli hubtest run --all`, which
outputted this: https://gist.github.com/sdwilsh/6ed59b6904dac9cabbea63c55567c070
  • Loading branch information
sdwilsh committed Feb 28, 2025
1 parent e8d75e9 commit fb9c6c6
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# The `builder` fetches the latest crowdsec release and untars it so it can be
# used by the actual devcontainer.
FROM alpine:latest AS builder
WORKDIR /app
RUN wget -q https://github.com/crowdsecurity/crowdsec/releases/latest/download/crowdsec-release.tgz \
&& tar xvzf crowdsec-release.tgz \
&& mv $(find -name 'crowdsec-v*') /crowdsec

#
# The actual devcontainer.
#
FROM mcr.microsoft.com/devcontainers/base:debian

# Install Required Dependencies
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
yq \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

# Install crowdsec
COPY --from=builder /crowdsec /workspaces/crowdsec
RUN chown 1000:1000 -R /workspaces/crowdsec

# Install wrapper scripts.
COPY bin/crowdsec /usr/local/bin/crowdsec
COPY bin/cscli /usr/local/bin/cscli
8 changes: 8 additions & 0 deletions .devcontainer/bin/crowdsec
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env sh

set -e

# This is a lightweight wrapper around the test environment's crowdsec command.
# It uses a configuration that is setup to "just work" from the devcontainer.

/workspaces/crowdsec/tests/crowdsec -c /workspaces/crowdsec/tests/dev.yaml $@
8 changes: 8 additions & 0 deletions .devcontainer/bin/cscli
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/usr/bin/env sh

set -e

# This is a lightweight wrapper around the test environment's cscli command.
# It uses a configuration that is setup to "just work" from the devcontainer.

/workspaces/crowdsec/tests/cscli -c /workspaces/crowdsec/tests/dev.yaml $@
9 changes: 9 additions & 0 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "CrowdSec Hub",
"build": {
"dockerfile": "Dockerfile"
},
"postCreateCommand": "sh -f .devcontainer/postCreate.sh",
"workspaceFolder": "/workspaces/hub",
"workspaceMount": "source=${localWorkspaceFolder},target=/workspaces/hub,type=bind"
}
27 changes: 27 additions & 0 deletions .devcontainer/postCreate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env sh

set -e

# Setup the test environment.
cd /workspaces/crowdsec/
./test_env.sh
cd tests/

# Update config to point to correct paths for this devcontainer.
yq -iy '.config_paths.hub_dir = "/workspaces/hub/"' dev.yaml
yq -iy '.config_paths.index_path = "/workspaces/hub/.index"' dev.yaml

# As documented in https://github.com/crowdsecurity/crowdsec/issues/3183,
# running ./test_env.sh sets a bunch of relative paths with simply do not work
# as we need them to in a devcontainer. The following lines set absolute paths
# that do work.
yq -iy '.api.client.credentials_path = "/workspaces/crowdsec/tests/config/local_api_credentials.yaml"' dev.yaml
yq -iy '.api.server.console_path = "/workspaces/crowdsec/tests/config/console.yaml"' dev.yaml
yq -iy '.api.server.online_client.credentials_path = "/workspaces/crowdsec/tests/config/online_api_credentials.yaml"' dev.yaml
yq -iy '.api.server.profiles_path = "/workspaces/crowdsec/tests/config/profiles.yaml"' dev.yaml
yq -iy '.config_paths.config_dir = "/workspaces/crowdsec/tests/config/"' dev.yaml
yq -iy '.config_paths.data_dir = "/workspaces/crowdsec/tests/data/"' dev.yaml
yq -iy '.config_paths.notification_dir = "/workspaces/crowdsec/tests/notifications/"' dev.yaml
yq -iy '.config_paths.plugin_dir = "/workspaces/crowdsec/tests/plugins/"' dev.yaml
yq -iy '.crowdsec_service.acquisition_path = "/workspaces/crowdsec/tests/config/acquis.yaml"' dev.yaml
yq -iy '.db_config.db_path = "/workspaces/crowdsec/tests/data/crowdsec.db"' dev.yaml
22 changes: 22 additions & 0 deletions .github/workflows/devcontainer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Build Developer Container
on:
push:
branches:
- master
paths:
- .devcontainer/**
pull_request:
branches:
- master
paths:
- .devcontainer/**

jobs:
devcontainer-build:
runs-on: ubuntu-latest
steps:
- name: checkout repo
uses: actions/checkout@v4

- name: Build devcontainer image
uses: devcontainers/[email protected]

0 comments on commit fb9c6c6

Please sign in to comment.