Compare commits
294 commits
master
...
coraza_poc
Author | SHA1 | Date | |
---|---|---|---|
|
7365c39b62 | ||
|
8bb3b8933b | ||
|
eb1bea26cd | ||
|
c1a04ead79 | ||
|
b21fa99902 | ||
|
18b53128a5 | ||
|
8fa84e5cd9 | ||
|
493880824b | ||
|
fe78511b48 | ||
|
0c61726971 | ||
|
c9e4aebd00 | ||
|
dce1f3cd8c | ||
|
00d899ee8e | ||
|
25635a306f | ||
|
5503b2374a | ||
|
169e39a4a9 | ||
|
f7c5726a0a | ||
|
0c030a3bb5 | ||
|
9b79a37eff | ||
|
63f230b24b | ||
|
17384368ae | ||
|
bd2c59b054 | ||
|
91a6263b5b | ||
|
aa02a00fc2 | ||
|
cce83d1bdc | ||
|
b86ac92b11 | ||
|
bb307dd339 | ||
|
52c1e16216 | ||
|
1a1f4f6169 | ||
|
722ce46946 | ||
|
059c0adb93 | ||
|
2089ad6663 | ||
|
8046690219 | ||
|
bff93d7b01 | ||
|
c3a4066646 | ||
|
42e1da2507 | ||
|
1c22783661 | ||
|
e637e7bf8b | ||
|
ac451ccaf3 | ||
|
b01901b04e | ||
|
cb030beaca | ||
|
6fb965bb3f | ||
|
3d3bf0bb0e | ||
|
393a8b8ef5 | ||
|
2a920124fe | ||
|
60faeaa7d7 | ||
|
d9355e8c3a | ||
|
872e218b31 | ||
|
17cfc9909e | ||
|
410e36e6a3 | ||
|
7e1fd33c7e | ||
|
1ffece8872 | ||
|
3836780d90 | ||
|
68148e031c | ||
|
a258cc0b4a | ||
|
1eab34eb3f | ||
|
0cd2a2da20 | ||
|
008480420c | ||
|
4b7b138be7 | ||
|
eed9ff0c46 | ||
|
5f254769ae | ||
|
fe005f87e5 | ||
|
b31d48a797 | ||
|
8999154f76 | ||
|
5ca2ee2f2e | ||
|
3683a7a02a | ||
|
3eb272c4e0 | ||
|
d851490790 | ||
|
dc39866250 | ||
|
e7505f5b2e | ||
|
b1653aea63 | ||
|
946fbbb8a2 | ||
|
f77d9e043a | ||
|
118da5b423 | ||
|
710d8a438a | ||
|
b6899e0c10 | ||
|
dd6e539717 | ||
|
56c616f70d | ||
|
ef9b6acbf8 | ||
|
5abc8e0e14 | ||
|
2c652ef92f | ||
|
9580f8e14d | ||
|
e4b92af78c | ||
|
1509c2d97c | ||
|
7b1074f0cb | ||
|
2d01e4680f | ||
|
4a265ca4af | ||
|
6b317f0723 | ||
|
8173e1ba42 | ||
|
94a378d230 | ||
|
017331ca7f | ||
|
6718d82765 | ||
|
9af30e2a3d | ||
|
55491be528 | ||
|
0e717cb558 | ||
|
d40e9fb760 | ||
|
9864d2c459 | ||
|
9db48e2110 | ||
|
db40ba7b3b | ||
|
56ad2bbf98 | ||
|
65473d4e05 | ||
|
d9b0d440bf | ||
|
c8af58d1bf | ||
|
79d019f9a2 | ||
|
056c979455 | ||
|
4a6fd338e0 | ||
|
f8c91d20b0 | ||
|
120f7cf578 | ||
|
042d316fab | ||
|
6dec8a24bb | ||
|
07d463f4f0 | ||
|
d6f9bbc0c3 | ||
|
4bfa0a7b4d | ||
|
d5c7870826 | ||
|
9d7ed12950 | ||
|
ab8de19506 | ||
|
f80d841188 | ||
|
ec4b5bdc86 | ||
|
a0b0745f9d | ||
|
927310a439 | ||
|
1154ada2df | ||
|
694028f769 | ||
|
152c940774 | ||
|
f4b5bcb865 | ||
|
ad54b99bf9 | ||
|
84be2b8c97 | ||
|
bfd94ceda7 | ||
|
41d19de092 | ||
|
26c876dc38 | ||
|
450c263826 | ||
|
fcd6c468c4 | ||
|
590a19b768 | ||
|
84ffde1844 | ||
|
17662e59a9 | ||
|
2e0b9683f3 | ||
|
d136cc4734 | ||
|
81645c96aa | ||
|
83d5211193 | ||
|
c96c8f19c9 | ||
|
57b5f5c27c | ||
|
37c5d54e43 | ||
|
b0e7da06b9 | ||
|
e5906e6eea | ||
|
01ddc45a2c | ||
|
bb59d9852a | ||
|
31a3b8a4ef | ||
|
495c6f9e8a | ||
|
6b8ed0c9d0 | ||
|
cd1cefbc8b | ||
|
0cebf833c7 | ||
|
82bb8a2789 | ||
|
f18b554177 | ||
|
6cbeefead6 | ||
|
e49f33b4a7 | ||
|
46ae0b3822 | ||
|
676352b5b1 | ||
|
4bfca8cab5 | ||
|
eafffe7c94 | ||
|
9edde09608 | ||
|
1f3801f390 | ||
|
c02c74b5fe | ||
|
b2bb15bb49 | ||
|
dd49620922 | ||
|
685006508c | ||
|
03650401c5 | ||
|
00e1ffbf58 | ||
|
bd9df8f480 | ||
|
1b9d8c8226 | ||
|
c00b1abd72 | ||
|
2ff238d5f8 | ||
|
dca6faab08 | ||
|
ac98256602 | ||
|
b110c74487 | ||
|
5dbc2758fa | ||
|
0acda36d33 | ||
|
1468bb9681 | ||
|
68c78249d5 | ||
|
ef118a49ff | ||
|
15120a6d8f | ||
|
350e8979b1 | ||
|
b89c5652ca | ||
|
88e4f7c157 | ||
|
ecbdf2f0e1 | ||
|
2600ffbd19 | ||
|
c89b42939e | ||
|
98fb84d3e7 | ||
|
511468b8fe | ||
|
57d3ebba12 | ||
|
be6555e46c | ||
|
4eae40865e | ||
|
810a8adcf0 | ||
|
325003bb69 | ||
|
f496bd1692 | ||
|
a00bae6039 | ||
|
734ba46e6a | ||
|
7db5bf8979 | ||
|
d3bb9f8ae1 | ||
|
92a3c4b2fb | ||
|
dd7fa82543 | ||
|
535738b962 | ||
|
d3ce4cbf8e | ||
|
d5e0c8a36b | ||
|
7fdd4d04fe | ||
|
ca930cce09 | ||
|
502e21bc5b | ||
|
42341222df | ||
|
a8321b5cc5 | ||
|
6a47b9e97d | ||
|
7081666199 | ||
|
2e60e8021c | ||
|
c435447d8e | ||
|
6930b1e3e5 | ||
|
1286efc74f | ||
|
5a0b1b72d3 | ||
|
1a5799e058 | ||
|
4e26e23725 | ||
|
24d2c264a7 | ||
|
0379574b14 | ||
|
e0bd4dc928 | ||
|
4846701ed5 | ||
|
a4ee1e717e | ||
|
59e3d0dfce | ||
|
885c283097 | ||
|
cbf06c25fb | ||
|
353926ec91 | ||
|
4332598cd1 | ||
|
51295ef577 | ||
|
da37b5566d | ||
|
343d22e7b3 | ||
|
e381d85314 | ||
|
711f0474d9 | ||
|
dd83bdea6b | ||
|
fc8a0ee9d4 | ||
|
4a38cb5bbb | ||
|
e4e2bb5504 | ||
|
a7cd86f725 | ||
|
c41386056a | ||
|
dd5e38a2c5 | ||
|
2f5a6fbb4f | ||
|
f7e098047f | ||
|
792961d757 | ||
|
01ced8fb99 | ||
|
4993758b36 | ||
|
c17b103f06 | ||
|
a326ffbb1e | ||
|
b33ba277bf | ||
|
54fd2e4e70 | ||
|
779ea2e262 | ||
|
472f40b9d4 | ||
|
ab2c152627 | ||
|
7d8c931d00 | ||
|
8ba692b115 | ||
|
cd5cb55a7e | ||
|
d946286e5c | ||
|
d0af521b9e | ||
|
faf2042258 | ||
|
e543523ba3 | ||
|
f7eaefa518 | ||
|
ef4fe8f5d3 | ||
|
57547c32c9 | ||
|
a6ba0e869c | ||
|
8baeb70998 | ||
|
84b6570554 | ||
|
7098e971c7 | ||
|
13512891e4 | ||
|
3fe6e3be14 | ||
|
877d4fc32d | ||
|
07b60233db | ||
|
9180ac7be9 | ||
|
805752dc62 | ||
|
40f65de7b9 | ||
|
fa172bed56 | ||
|
a2e6359880 | ||
|
c46e2ccdad | ||
|
61e1cc29d5 | ||
|
415e2dc68d | ||
|
739d086325 | ||
|
30455a8eb6 | ||
|
d123254949 | ||
|
ee8b31348b | ||
|
4a7e26af02 | ||
|
a7d80aacd6 | ||
|
7078d79ce4 | ||
|
65884fb4be | ||
|
44a5c81199 | ||
|
abaa6a5c56 | ||
|
6d3b2b354b | ||
|
6ac0a9ef9d | ||
|
cacdcd75b6 | ||
|
53c73a5e05 | ||
|
1e94b24a74 | ||
|
d335e74c81 | ||
|
1973aa1a56 | ||
|
1d9891a244 |
437 changed files with 16051 additions and 22017 deletions
9
.github/governance.yml
vendored
9
.github/governance.yml
vendored
|
@ -42,7 +42,7 @@ issue:
|
||||||
3. Check [Releases](https://github.com/crowdsecurity/crowdsec/releases/latest) to make sure your agent is on the latest version.
|
3. Check [Releases](https://github.com/crowdsecurity/crowdsec/releases/latest) to make sure your agent is on the latest version.
|
||||||
|
|
||||||
- prefix: kind
|
- prefix: kind
|
||||||
list: ['feature', 'bug', 'packaging', 'enhancement', 'refactoring']
|
list: ['feature', 'bug', 'packaging', 'enhancement']
|
||||||
multiple: false
|
multiple: false
|
||||||
author_association:
|
author_association:
|
||||||
author: true
|
author: true
|
||||||
|
@ -54,7 +54,6 @@ issue:
|
||||||
@$AUTHOR: There are no 'kind' label on this issue. You need a 'kind' label to start the triage process.
|
@$AUTHOR: There are no 'kind' label on this issue. You need a 'kind' label to start the triage process.
|
||||||
* `/kind feature`
|
* `/kind feature`
|
||||||
* `/kind enhancement`
|
* `/kind enhancement`
|
||||||
* `/kind refactoring`
|
|
||||||
* `/kind bug`
|
* `/kind bug`
|
||||||
* `/kind packaging`
|
* `/kind packaging`
|
||||||
|
|
||||||
|
@ -66,13 +65,12 @@ pull_request:
|
||||||
labels:
|
labels:
|
||||||
- prefix: kind
|
- prefix: kind
|
||||||
multiple: false
|
multiple: false
|
||||||
list: [ 'feature', 'enhancement', 'fix', 'chore', 'dependencies', 'refactoring']
|
list: [ 'feature', 'enhancement', 'fix', 'chore', 'dependencies']
|
||||||
needs:
|
needs:
|
||||||
comment: |
|
comment: |
|
||||||
@$AUTHOR: There are no 'kind' label on this PR. You need a 'kind' label to generate the release automatically.
|
@$AUTHOR: There are no 'kind' label on this PR. You need a 'kind' label to generate the release automatically.
|
||||||
* `/kind feature`
|
* `/kind feature`
|
||||||
* `/kind enhancement`
|
* `/kind enhancement`
|
||||||
* `/kind refactoring`
|
|
||||||
* `/kind fix`
|
* `/kind fix`
|
||||||
* `/kind chore`
|
* `/kind chore`
|
||||||
* `/kind dependencies`
|
* `/kind dependencies`
|
||||||
|
@ -83,7 +81,7 @@ pull_request:
|
||||||
failure: Missing kind label to generate release automatically.
|
failure: Missing kind label to generate release automatically.
|
||||||
|
|
||||||
- prefix: area
|
- prefix: area
|
||||||
list: [ "agent", "local-api", "cscli", "security", "configuration", "appsec"]
|
list: [ "agent", "local-api", "cscli", "security", "configuration"]
|
||||||
multiple: true
|
multiple: true
|
||||||
needs:
|
needs:
|
||||||
comment: |
|
comment: |
|
||||||
|
@ -91,7 +89,6 @@ pull_request:
|
||||||
* `/area agent`
|
* `/area agent`
|
||||||
* `/area local-api`
|
* `/area local-api`
|
||||||
* `/area cscli`
|
* `/area cscli`
|
||||||
* `/area appsec`
|
|
||||||
* `/area security`
|
* `/area security`
|
||||||
* `/area configuration`
|
* `/area configuration`
|
||||||
|
|
||||||
|
|
27
.github/workflows/bats-hub.yml
vendored
27
.github/workflows/bats-hub.yml
vendored
|
@ -1,4 +1,4 @@
|
||||||
name: (sub) Bats / Hub
|
name: Hub tests
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_call:
|
workflow_call:
|
||||||
|
@ -8,13 +8,16 @@ on:
|
||||||
GIST_BADGES_ID:
|
GIST_BADGES_ID:
|
||||||
required: true
|
required: true
|
||||||
|
|
||||||
|
env:
|
||||||
|
PREFIX_TEST_NAMES_WITH_FILE: true
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
strategy:
|
strategy:
|
||||||
matrix:
|
matrix:
|
||||||
test-file: ["hub-1.bats", "hub-2.bats", "hub-3.bats"]
|
go-version: ["1.21.4"]
|
||||||
|
|
||||||
name: "Functional tests"
|
name: "Build + tests"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 30
|
timeout-minutes: 30
|
||||||
steps:
|
steps:
|
||||||
|
@ -25,35 +28,33 @@ jobs:
|
||||||
echo githubciXXXXXXXXXXXXXXXXXXXXXXXX | sudo tee /etc/machine-id
|
echo githubciXXXXXXXXXXXXXXXXXXXXXXXX | sudo tee /etc/machine-id
|
||||||
|
|
||||||
- name: "Check out CrowdSec repository"
|
- name: "Check out CrowdSec repository"
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
submodules: true
|
submodules: true
|
||||||
|
|
||||||
- name: "Set up Go"
|
- name: "Set up Go ${{ matrix.go-version }}"
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: "1.22.2"
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
- name: "Install bats dependencies"
|
- name: "Install bats dependencies"
|
||||||
env:
|
env:
|
||||||
GOBIN: /usr/local/bin
|
GOBIN: /usr/local/bin
|
||||||
run: |
|
run: |
|
||||||
sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq libre2-dev
|
sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd libre2-dev
|
||||||
|
|
||||||
- name: "Build crowdsec and fixture"
|
- name: "Build crowdsec and fixture"
|
||||||
run: make bats-clean bats-build bats-fixture BUILD_STATIC=1
|
run: make bats-clean bats-build bats-fixture BUILD_STATIC=1
|
||||||
|
|
||||||
- name: "Run hub tests"
|
- name: "Run hub tests"
|
||||||
run: |
|
run: make bats-test-hub
|
||||||
./test/bin/generate-hub-tests
|
|
||||||
./test/run-tests ./test/dyn-bats/${{ matrix.test-file }} --formatter $(pwd)/test/lib/color-formatter
|
|
||||||
|
|
||||||
- name: "Collect hub coverage"
|
- name: "Collect hub coverage"
|
||||||
run: ./test/bin/collect-hub-coverage >> $GITHUB_ENV
|
run: ./test/bin/collect-hub-coverage >> $GITHUB_ENV
|
||||||
|
|
||||||
- name: "Create Parsers badge"
|
- name: "Create Parsers badge"
|
||||||
uses: schneegans/dynamic-badges-action@v1.7.0
|
uses: schneegans/dynamic-badges-action@v1.6.0
|
||||||
if: ${{ github.ref == 'refs/heads/master' && github.repository_owner == 'crowdsecurity' }}
|
if: ${{ github.ref == 'refs/heads/master' && github.repository_owner == 'crowdsecurity' }}
|
||||||
with:
|
with:
|
||||||
auth: ${{ secrets.GIST_BADGES_SECRET }}
|
auth: ${{ secrets.GIST_BADGES_SECRET }}
|
||||||
|
@ -64,7 +65,7 @@ jobs:
|
||||||
color: ${{ env.SCENARIO_BADGE_COLOR }}
|
color: ${{ env.SCENARIO_BADGE_COLOR }}
|
||||||
|
|
||||||
- name: "Create Scenarios badge"
|
- name: "Create Scenarios badge"
|
||||||
uses: schneegans/dynamic-badges-action@v1.7.0
|
uses: schneegans/dynamic-badges-action@v1.6.0
|
||||||
if: ${{ github.ref == 'refs/heads/master' && github.repository_owner == 'crowdsecurity' }}
|
if: ${{ github.ref == 'refs/heads/master' && github.repository_owner == 'crowdsecurity' }}
|
||||||
with:
|
with:
|
||||||
auth: ${{ secrets.GIST_BADGES_SECRET }}
|
auth: ${{ secrets.GIST_BADGES_SECRET }}
|
||||||
|
|
23
.github/workflows/bats-mysql.yml
vendored
23
.github/workflows/bats-mysql.yml
vendored
|
@ -1,4 +1,4 @@
|
||||||
name: (sub) Bats / MySQL
|
name: Functional tests (MySQL)
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_call:
|
workflow_call:
|
||||||
|
@ -7,9 +7,16 @@ on:
|
||||||
required: true
|
required: true
|
||||||
type: string
|
type: string
|
||||||
|
|
||||||
|
env:
|
||||||
|
PREFIX_TEST_NAMES_WITH_FILE: true
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
name: "Functional tests"
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: ["1.21.4"]
|
||||||
|
|
||||||
|
name: "Build + tests"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 30
|
timeout-minutes: 30
|
||||||
services:
|
services:
|
||||||
|
@ -28,21 +35,21 @@ jobs:
|
||||||
echo githubciXXXXXXXXXXXXXXXXXXXXXXXX | sudo tee /etc/machine-id
|
echo githubciXXXXXXXXXXXXXXXXXXXXXXXX | sudo tee /etc/machine-id
|
||||||
|
|
||||||
- name: "Check out CrowdSec repository"
|
- name: "Check out CrowdSec repository"
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
submodules: true
|
submodules: true
|
||||||
|
|
||||||
- name: "Set up Go"
|
- name: "Set up Go ${{ matrix.go-version }}"
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: "1.22.2"
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
- name: "Install bats dependencies"
|
- name: "Install bats dependencies"
|
||||||
env:
|
env:
|
||||||
GOBIN: /usr/local/bin
|
GOBIN: /usr/local/bin
|
||||||
run: |
|
run: |
|
||||||
sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq libre2-dev
|
sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd libre2-dev
|
||||||
|
|
||||||
- name: "Build crowdsec and fixture"
|
- name: "Build crowdsec and fixture"
|
||||||
run: |
|
run: |
|
||||||
|
@ -55,7 +62,7 @@ jobs:
|
||||||
MYSQL_USER: root
|
MYSQL_USER: root
|
||||||
|
|
||||||
- name: "Run tests"
|
- name: "Run tests"
|
||||||
run: ./test/run-tests ./test/bats --formatter $(pwd)/test/lib/color-formatter
|
run: make bats-test
|
||||||
env:
|
env:
|
||||||
DB_BACKEND: mysql
|
DB_BACKEND: mysql
|
||||||
MYSQL_HOST: 127.0.0.1
|
MYSQL_HOST: 127.0.0.1
|
||||||
|
|
23
.github/workflows/bats-postgres.yml
vendored
23
.github/workflows/bats-postgres.yml
vendored
|
@ -1,11 +1,18 @@
|
||||||
name: (sub) Bats / Postgres
|
name: Functional tests (Postgres)
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_call:
|
workflow_call:
|
||||||
|
|
||||||
|
env:
|
||||||
|
PREFIX_TEST_NAMES_WITH_FILE: true
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
name: "Functional tests"
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: ["1.21.4"]
|
||||||
|
|
||||||
|
name: "Build + tests"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 30
|
timeout-minutes: 30
|
||||||
services:
|
services:
|
||||||
|
@ -37,21 +44,21 @@ jobs:
|
||||||
echo githubciXXXXXXXXXXXXXXXXXXXXXXXX | sudo tee /etc/machine-id
|
echo githubciXXXXXXXXXXXXXXXXXXXXXXXX | sudo tee /etc/machine-id
|
||||||
|
|
||||||
- name: "Check out CrowdSec repository"
|
- name: "Check out CrowdSec repository"
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
submodules: true
|
submodules: true
|
||||||
|
|
||||||
- name: "Set up Go"
|
- name: "Set up Go ${{ matrix.go-version }}"
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: "1.22.2"
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
- name: "Install bats dependencies"
|
- name: "Install bats dependencies"
|
||||||
env:
|
env:
|
||||||
GOBIN: /usr/local/bin
|
GOBIN: /usr/local/bin
|
||||||
run: |
|
run: |
|
||||||
sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq libre2-dev
|
sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd libre2-dev
|
||||||
|
|
||||||
- name: "Build crowdsec and fixture (DB_BACKEND: pgx)"
|
- name: "Build crowdsec and fixture (DB_BACKEND: pgx)"
|
||||||
run: |
|
run: |
|
||||||
|
@ -64,7 +71,7 @@ jobs:
|
||||||
PGUSER: postgres
|
PGUSER: postgres
|
||||||
|
|
||||||
- name: "Run tests (DB_BACKEND: pgx)"
|
- name: "Run tests (DB_BACKEND: pgx)"
|
||||||
run: ./test/run-tests ./test/bats --formatter $(pwd)/test/lib/color-formatter
|
run: make bats-test
|
||||||
env:
|
env:
|
||||||
DB_BACKEND: pgx
|
DB_BACKEND: pgx
|
||||||
PGHOST: 127.0.0.1
|
PGHOST: 127.0.0.1
|
||||||
|
|
24
.github/workflows/bats-sqlite-coverage.yml
vendored
24
.github/workflows/bats-sqlite-coverage.yml
vendored
|
@ -1,14 +1,19 @@
|
||||||
name: (sub) Bats / sqlite + coverage
|
name: Functional tests (sqlite)
|
||||||
|
|
||||||
on:
|
on:
|
||||||
workflow_call:
|
workflow_call:
|
||||||
|
|
||||||
env:
|
env:
|
||||||
|
PREFIX_TEST_NAMES_WITH_FILE: true
|
||||||
TEST_COVERAGE: true
|
TEST_COVERAGE: true
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
name: "Functional tests"
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: ["1.21.4"]
|
||||||
|
|
||||||
|
name: "Build + tests"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 20
|
timeout-minutes: 20
|
||||||
|
|
||||||
|
@ -20,28 +25,28 @@ jobs:
|
||||||
echo githubciXXXXXXXXXXXXXXXXXXXXXXXX | sudo tee /etc/machine-id
|
echo githubciXXXXXXXXXXXXXXXXXXXXXXXX | sudo tee /etc/machine-id
|
||||||
|
|
||||||
- name: "Check out CrowdSec repository"
|
- name: "Check out CrowdSec repository"
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
submodules: true
|
submodules: true
|
||||||
|
|
||||||
- name: "Set up Go"
|
- name: "Set up Go ${{ matrix.go-version }}"
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: "1.22.2"
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
- name: "Install bats dependencies"
|
- name: "Install bats dependencies"
|
||||||
env:
|
env:
|
||||||
GOBIN: /usr/local/bin
|
GOBIN: /usr/local/bin
|
||||||
run: |
|
run: |
|
||||||
sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq libre2-dev
|
sudo apt -qq -y -o=Dpkg::Use-Pty=0 install build-essential daemonize jq netcat-openbsd libre2-dev
|
||||||
|
|
||||||
- name: "Build crowdsec and fixture"
|
- name: "Build crowdsec and fixture"
|
||||||
run: |
|
run: |
|
||||||
make clean bats-build bats-fixture BUILD_STATIC=1
|
make clean bats-build bats-fixture BUILD_STATIC=1
|
||||||
|
|
||||||
- name: "Run tests"
|
- name: "Run tests"
|
||||||
run: ./test/run-tests ./test/bats --formatter $(pwd)/test/lib/color-formatter
|
run: make bats-test
|
||||||
|
|
||||||
- name: "Collect coverage data"
|
- name: "Collect coverage data"
|
||||||
run: |
|
run: |
|
||||||
|
@ -77,8 +82,7 @@ jobs:
|
||||||
if: ${{ always() }}
|
if: ${{ always() }}
|
||||||
|
|
||||||
- name: Upload crowdsec coverage to codecov
|
- name: Upload crowdsec coverage to codecov
|
||||||
uses: codecov/codecov-action@v4
|
uses: codecov/codecov-action@v3
|
||||||
with:
|
with:
|
||||||
files: ./coverage-bats.out
|
files: ./coverage-bats.out
|
||||||
flags: bats
|
flags: bats
|
||||||
token: ${{ secrets.CODECOV_TOKEN }}
|
|
||||||
|
|
2
.github/workflows/cache-cleanup.yaml
vendored
2
.github/workflows/cache-cleanup.yaml
vendored
|
@ -11,7 +11,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
- name: Check out code
|
- name: Check out code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
|
|
||||||
- name: Cleanup
|
- name: Cleanup
|
||||||
run: |
|
run: |
|
||||||
|
|
14
.github/workflows/ci-windows-build-msi.yml
vendored
14
.github/workflows/ci-windows-build-msi.yml
vendored
|
@ -21,26 +21,30 @@ on:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: ["1.21.4"]
|
||||||
|
|
||||||
name: Build
|
name: Build
|
||||||
runs-on: windows-2019
|
runs-on: windows-2019
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
|
||||||
- name: Check out code into the Go module directory
|
- name: Check out code into the Go module directory
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
submodules: false
|
submodules: false
|
||||||
|
|
||||||
- name: "Set up Go"
|
- name: "Set up Go ${{ matrix.go-version }}"
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: "1.22.2"
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: make windows_installer BUILD_RE2_WASM=1
|
run: make windows_installer BUILD_RE2_WASM=1
|
||||||
- name: Upload MSI
|
- name: Upload MSI
|
||||||
uses: actions/upload-artifact@v4
|
uses: actions/upload-artifact@v3
|
||||||
with:
|
with:
|
||||||
path: crowdsec*msi
|
path: crowdsec*msi
|
||||||
name: crowdsec.msi
|
name: crowdsec.msi
|
||||||
|
|
2
.github/workflows/ci_release-drafter.yml
vendored
2
.github/workflows/ci_release-drafter.yml
vendored
|
@ -12,7 +12,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
# Drafts your next Release notes as Pull Requests are merged into "master"
|
# Drafts your next Release notes as Pull Requests are merged into "master"
|
||||||
- uses: release-drafter/release-drafter@v6
|
- uses: release-drafter/release-drafter@v5
|
||||||
with:
|
with:
|
||||||
config-name: release-drafter.yml
|
config-name: release-drafter.yml
|
||||||
# (Optional) specify config name to use, relative to .github/. Default: release-drafter.yml
|
# (Optional) specify config name to use, relative to .github/. Default: release-drafter.yml
|
||||||
|
|
20
.github/workflows/codeql-analysis.yml
vendored
20
.github/workflows/codeql-analysis.yml
vendored
|
@ -44,20 +44,14 @@ jobs:
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
# required to pick up tags for BUILD_VERSION
|
# required to pick up tags for BUILD_VERSION
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: "Set up Go"
|
|
||||||
uses: actions/setup-go@v5
|
|
||||||
with:
|
|
||||||
go-version: "1.22.2"
|
|
||||||
cache-dependency-path: "**/go.sum"
|
|
||||||
|
|
||||||
# Initializes the CodeQL tools for scanning.
|
# Initializes the CodeQL tools for scanning.
|
||||||
- name: Initialize CodeQL
|
- name: Initialize CodeQL
|
||||||
uses: github/codeql-action/init@v3
|
uses: github/codeql-action/init@v2
|
||||||
with:
|
with:
|
||||||
languages: ${{ matrix.language }}
|
languages: ${{ matrix.language }}
|
||||||
# If you wish to specify custom queries, you can do so here or in a config file.
|
# If you wish to specify custom queries, you can do so here or in a config file.
|
||||||
|
@ -68,7 +62,7 @@ jobs:
|
||||||
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
|
||||||
# If this step fails, then you should remove it and run the build manually (see below)
|
# If this step fails, then you should remove it and run the build manually (see below)
|
||||||
# - name: Autobuild
|
# - name: Autobuild
|
||||||
# uses: github/codeql-action/autobuild@v3
|
# uses: github/codeql-action/autobuild@v2
|
||||||
|
|
||||||
# ℹ️ Command-line programs to run using the OS shell.
|
# ℹ️ Command-line programs to run using the OS shell.
|
||||||
# 📚 https://git.io/JvXDl
|
# 📚 https://git.io/JvXDl
|
||||||
|
@ -77,8 +71,14 @@ jobs:
|
||||||
# and modify them (or add more) to build your code if your project
|
# and modify them (or add more) to build your code if your project
|
||||||
# uses a compiled language
|
# uses a compiled language
|
||||||
|
|
||||||
|
- name: "Set up Go"
|
||||||
|
uses: actions/setup-go@v4
|
||||||
|
with:
|
||||||
|
go-version: "1.21.0"
|
||||||
|
cache-dependency-path: "**/go.sum"
|
||||||
|
|
||||||
- run: |
|
- run: |
|
||||||
make clean build BUILD_RE2_WASM=1
|
make clean build BUILD_RE2_WASM=1
|
||||||
|
|
||||||
- name: Perform CodeQL Analysis
|
- name: Perform CodeQL Analysis
|
||||||
uses: github/codeql-action/analyze@v3
|
uses: github/codeql-action/analyze@v2
|
||||||
|
|
68
.github/workflows/docker-tests.yml
vendored
68
.github/workflows/docker-tests.yml
vendored
|
@ -15,42 +15,59 @@ on:
|
||||||
- 'README.md'
|
- 'README.md'
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
test_flavor:
|
test_docker_image:
|
||||||
strategy:
|
|
||||||
# we could test all the flavors in a single pytest job,
|
|
||||||
# but let's split them (and the image build) in multiple runners for performance
|
|
||||||
matrix:
|
|
||||||
# can be slim, full or debian (no debian slim).
|
|
||||||
flavor: ["slim", "debian"]
|
|
||||||
|
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 30
|
timeout-minutes: 30
|
||||||
steps:
|
steps:
|
||||||
|
|
||||||
- name: Check out the repo
|
- name: Check out the repo
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v2
|
||||||
with:
|
with:
|
||||||
config: .github/buildkit.toml
|
config: .github/buildkit.toml
|
||||||
|
|
||||||
- name: "Build image"
|
- name: "Build flavor: slim"
|
||||||
uses: docker/build-push-action@v5
|
uses: docker/build-push-action@v4
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
file: ./Dockerfile${{ matrix.flavor == 'debian' && '.debian' || '' }}
|
file: ./Dockerfile
|
||||||
tags: crowdsecurity/crowdsec:test${{ matrix.flavor == 'full' && '' || '-' }}${{ matrix.flavor == 'full' && '' || matrix.flavor }}
|
tags: crowdsecurity/crowdsec:test-slim
|
||||||
target: ${{ matrix.flavor == 'debian' && 'full' || matrix.flavor }}
|
target: slim
|
||||||
|
platforms: linux/amd64
|
||||||
|
load: true
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=min
|
||||||
|
|
||||||
|
- name: "Build flavor: full"
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./Dockerfile
|
||||||
|
tags: crowdsecurity/crowdsec:test
|
||||||
|
target: full
|
||||||
|
platforms: linux/amd64
|
||||||
|
load: true
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=min
|
||||||
|
|
||||||
|
- name: "Build flavor: full (debian)"
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./Dockerfile.debian
|
||||||
|
tags: crowdsecurity/crowdsec:test-debian
|
||||||
|
target: full
|
||||||
platforms: linux/amd64
|
platforms: linux/amd64
|
||||||
load: true
|
load: true
|
||||||
cache-from: type=gha
|
cache-from: type=gha
|
||||||
cache-to: type=gha,mode=min
|
cache-to: type=gha,mode=min
|
||||||
|
|
||||||
- name: "Setup Python"
|
- name: "Setup Python"
|
||||||
uses: actions/setup-python@v5
|
uses: actions/setup-python@v4
|
||||||
with:
|
with:
|
||||||
python-version: "3.x"
|
python-version: "3.x"
|
||||||
|
|
||||||
|
@ -59,15 +76,15 @@ jobs:
|
||||||
cd docker/test
|
cd docker/test
|
||||||
python -m pip install --upgrade pipenv wheel
|
python -m pip install --upgrade pipenv wheel
|
||||||
|
|
||||||
#- name: "Cache virtualenvs"
|
- name: "Cache virtualenvs"
|
||||||
# id: cache-pipenv
|
id: cache-pipenv
|
||||||
# uses: actions/cache@v4
|
uses: actions/cache@v3
|
||||||
# with:
|
with:
|
||||||
# path: ~/.local/share/virtualenvs
|
path: ~/.local/share/virtualenvs
|
||||||
# key: ${{ runner.os }}-pipenv-${{ hashFiles('**/Pipfile.lock') }}
|
key: ${{ runner.os }}-pipenv-${{ hashFiles('**/Pipfile.lock') }}
|
||||||
|
|
||||||
- name: "Install dependencies"
|
- name: "Install dependencies"
|
||||||
#if: steps.cache-pipenv.outputs.cache-hit != 'true'
|
if: steps.cache-pipenv.outputs.cache-hit != 'true'
|
||||||
run: |
|
run: |
|
||||||
cd docker/test
|
cd docker/test
|
||||||
pipenv install --deploy
|
pipenv install --deploy
|
||||||
|
@ -78,10 +95,9 @@ jobs:
|
||||||
- name: "Run tests"
|
- name: "Run tests"
|
||||||
env:
|
env:
|
||||||
CROWDSEC_TEST_VERSION: test
|
CROWDSEC_TEST_VERSION: test
|
||||||
CROWDSEC_TEST_FLAVORS: ${{ matrix.flavor }}
|
CROWDSEC_TEST_FLAVORS: slim,debian
|
||||||
CROWDSEC_TEST_NETWORK: net-test
|
CROWDSEC_TEST_NETWORK: net-test
|
||||||
CROWDSEC_TEST_TIMEOUT: 90
|
CROWDSEC_TEST_TIMEOUT: 90
|
||||||
# running serially to reduce test flakiness
|
|
||||||
run: |
|
run: |
|
||||||
cd docker/test
|
cd docker/test
|
||||||
pipenv run pytest -n 1 --durations=0 --color=yes
|
pipenv run pytest -n 2 --durations=0 --color=yes
|
||||||
|
|
19
.github/workflows/go-tests-windows.yml
vendored
19
.github/workflows/go-tests-windows.yml
vendored
|
@ -20,21 +20,25 @@ env:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: ["1.21.4"]
|
||||||
|
|
||||||
name: "Build + tests"
|
name: "Build + tests"
|
||||||
runs-on: windows-2022
|
runs-on: windows-2022
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
|
|
||||||
- name: Check out CrowdSec repository
|
- name: Check out CrowdSec repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
submodules: false
|
submodules: false
|
||||||
|
|
||||||
- name: "Set up Go"
|
- name: "Set up Go ${{ matrix.go-version }}"
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: "1.22.2"
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
- name: Build
|
- name: Build
|
||||||
run: |
|
run: |
|
||||||
|
@ -48,16 +52,15 @@ jobs:
|
||||||
cat out.txt | sed 's/ *coverage:.*of statements in.*//' | richgo testfilter
|
cat out.txt | sed 's/ *coverage:.*of statements in.*//' | richgo testfilter
|
||||||
|
|
||||||
- name: Upload unit coverage to Codecov
|
- name: Upload unit coverage to Codecov
|
||||||
uses: codecov/codecov-action@v4
|
uses: codecov/codecov-action@v3
|
||||||
with:
|
with:
|
||||||
files: coverage.out
|
files: coverage.out
|
||||||
flags: unit-windows
|
flags: unit-windows
|
||||||
token: ${{ secrets.CODECOV_TOKEN }}
|
|
||||||
|
|
||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v4
|
uses: golangci/golangci-lint-action@v3
|
||||||
with:
|
with:
|
||||||
version: v1.57
|
version: v1.54
|
||||||
args: --issues-exit-code=1 --timeout 10m
|
args: --issues-exit-code=1 --timeout 10m
|
||||||
only-new-issues: false
|
only-new-issues: false
|
||||||
# the cache is already managed above, enabling it here
|
# the cache is already managed above, enabling it here
|
||||||
|
|
37
.github/workflows/go-tests.yml
vendored
37
.github/workflows/go-tests.yml
vendored
|
@ -24,18 +24,23 @@ env:
|
||||||
RICHGO_FORCE_COLOR: 1
|
RICHGO_FORCE_COLOR: 1
|
||||||
AWS_HOST: localstack
|
AWS_HOST: localstack
|
||||||
# these are to mimic aws config
|
# these are to mimic aws config
|
||||||
AWS_ACCESS_KEY_ID: test
|
AWS_ACCESS_KEY_ID: AKIAIOSFODNN7EXAMPLE
|
||||||
AWS_SECRET_ACCESS_KEY: test
|
AWS_SECRET_ACCESS_KEY: wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
|
||||||
AWS_REGION: us-east-1
|
AWS_REGION: us-east-1
|
||||||
|
KINESIS_INITIALIZE_STREAMS: "stream-1-shard:1,stream-2-shards:2"
|
||||||
CROWDSEC_FEATURE_DISABLE_HTTP_RETRY_BACKOFF: true
|
CROWDSEC_FEATURE_DISABLE_HTTP_RETRY_BACKOFF: true
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: ["1.21.4"]
|
||||||
|
|
||||||
name: "Build + tests"
|
name: "Build + tests"
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
services:
|
services:
|
||||||
localstack:
|
localstack:
|
||||||
image: localstack/localstack:3.0
|
image: localstack/localstack:1.3.0
|
||||||
ports:
|
ports:
|
||||||
- 4566:4566 # Localstack exposes all services on the same port
|
- 4566:4566 # Localstack exposes all services on the same port
|
||||||
env:
|
env:
|
||||||
|
@ -44,7 +49,7 @@ jobs:
|
||||||
KINESIS_ERROR_PROBABILITY: ""
|
KINESIS_ERROR_PROBABILITY: ""
|
||||||
DOCKER_HOST: unix:///var/run/docker.sock
|
DOCKER_HOST: unix:///var/run/docker.sock
|
||||||
KINESIS_INITIALIZE_STREAMS: ${{ env.KINESIS_INITIALIZE_STREAMS }}
|
KINESIS_INITIALIZE_STREAMS: ${{ env.KINESIS_INITIALIZE_STREAMS }}
|
||||||
LOCALSTACK_HOST: ${{ env.AWS_HOST }} # Required so that resource urls are provided properly
|
HOSTNAME_EXTERNAL: ${{ env.AWS_HOST }} # Required so that resource urls are provided properly
|
||||||
# e.g sqs url will get localhost if we don't set this env to map our service
|
# e.g sqs url will get localhost if we don't set this env to map our service
|
||||||
options: >-
|
options: >-
|
||||||
--name=localstack
|
--name=localstack
|
||||||
|
@ -53,7 +58,7 @@ jobs:
|
||||||
--health-timeout=5s
|
--health-timeout=5s
|
||||||
--health-retries=3
|
--health-retries=3
|
||||||
zoo1:
|
zoo1:
|
||||||
image: confluentinc/cp-zookeeper:7.4.3
|
image: confluentinc/cp-zookeeper:7.3.0
|
||||||
ports:
|
ports:
|
||||||
- "2181:2181"
|
- "2181:2181"
|
||||||
env:
|
env:
|
||||||
|
@ -104,7 +109,7 @@ jobs:
|
||||||
--health-retries 5
|
--health-retries 5
|
||||||
|
|
||||||
loki:
|
loki:
|
||||||
image: grafana/loki:2.9.1
|
image: grafana/loki:2.8.0
|
||||||
ports:
|
ports:
|
||||||
- "3100:3100"
|
- "3100:3100"
|
||||||
options: >-
|
options: >-
|
||||||
|
@ -118,20 +123,15 @@ jobs:
|
||||||
steps:
|
steps:
|
||||||
|
|
||||||
- name: Check out CrowdSec repository
|
- name: Check out CrowdSec repository
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
submodules: false
|
submodules: false
|
||||||
|
|
||||||
- name: "Set up Go"
|
- name: "Set up Go ${{ matrix.go-version }}"
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: "1.22.2"
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
- name: Create localstack streams
|
|
||||||
run: |
|
|
||||||
aws --endpoint-url=http://127.0.0.1:4566 --region us-east-1 kinesis create-stream --stream-name stream-1-shard --shard-count 1
|
|
||||||
aws --endpoint-url=http://127.0.0.1:4566 --region us-east-1 kinesis create-stream --stream-name stream-2-shards --shard-count 2
|
|
||||||
|
|
||||||
- name: Build and run tests, static
|
- name: Build and run tests, static
|
||||||
run: |
|
run: |
|
||||||
|
@ -149,16 +149,15 @@ jobs:
|
||||||
make go-acc | sed 's/ *coverage:.*of statements in.*//' | richgo testfilter
|
make go-acc | sed 's/ *coverage:.*of statements in.*//' | richgo testfilter
|
||||||
|
|
||||||
- name: Upload unit coverage to Codecov
|
- name: Upload unit coverage to Codecov
|
||||||
uses: codecov/codecov-action@v4
|
uses: codecov/codecov-action@v3
|
||||||
with:
|
with:
|
||||||
files: coverage.out
|
files: coverage.out
|
||||||
flags: unit-linux
|
flags: unit-linux
|
||||||
token: ${{ secrets.CODECOV_TOKEN }}
|
|
||||||
|
|
||||||
- name: golangci-lint
|
- name: golangci-lint
|
||||||
uses: golangci/golangci-lint-action@v4
|
uses: golangci/golangci-lint-action@v3
|
||||||
with:
|
with:
|
||||||
version: v1.57
|
version: v1.54
|
||||||
args: --issues-exit-code=1 --timeout 10m
|
args: --issues-exit-code=1 --timeout 10m
|
||||||
only-new-issues: false
|
only-new-issues: false
|
||||||
# the cache is already managed above, enabling it here
|
# the cache is already managed above, enabling it here
|
||||||
|
|
2
.github/workflows/governance-bot.yaml
vendored
2
.github/workflows/governance-bot.yaml
vendored
|
@ -23,7 +23,7 @@ jobs:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
# Semantic versioning, lock to different version: v2, v2.0 or a commit hash.
|
# Semantic versioning, lock to different version: v2, v2.0 or a commit hash.
|
||||||
- uses: BirthdayResearch/oss-governance-bot@v4
|
- uses: BirthdayResearch/oss-governance-bot@v3
|
||||||
with:
|
with:
|
||||||
# You can use a PAT to post a comment/label/status so that it shows up as a user instead of github-actions
|
# You can use a PAT to post a comment/label/status so that it shows up as a user instead of github-actions
|
||||||
github-token: ${{secrets.GITHUB_TOKEN}} # optional, default to '${{ github.token }}'
|
github-token: ${{secrets.GITHUB_TOKEN}} # optional, default to '${{ github.token }}'
|
||||||
|
|
47
.github/workflows/publish-docker-master.yml
vendored
47
.github/workflows/publish-docker-master.yml
vendored
|
@ -1,47 +0,0 @@
|
||||||
name: (push-master) Publish latest Docker images
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [ master ]
|
|
||||||
paths:
|
|
||||||
- 'pkg/**'
|
|
||||||
- 'cmd/**'
|
|
||||||
- 'mk/**'
|
|
||||||
- 'docker/docker_start.sh'
|
|
||||||
- 'docker/config.yaml'
|
|
||||||
- '.github/workflows/publish-docker-master.yml'
|
|
||||||
- '.github/workflows/publish-docker.yml'
|
|
||||||
- 'Dockerfile'
|
|
||||||
- 'Dockerfile.debian'
|
|
||||||
- 'go.mod'
|
|
||||||
- 'go.sum'
|
|
||||||
- 'Makefile'
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
dev-alpine:
|
|
||||||
uses: ./.github/workflows/publish-docker.yml
|
|
||||||
with:
|
|
||||||
platform: linux/amd64
|
|
||||||
crowdsec_version: ""
|
|
||||||
image_version: dev
|
|
||||||
latest: false
|
|
||||||
push: true
|
|
||||||
slim: false
|
|
||||||
debian: false
|
|
||||||
secrets:
|
|
||||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
|
||||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
|
||||||
|
|
||||||
dev-debian:
|
|
||||||
uses: ./.github/workflows/publish-docker.yml
|
|
||||||
with:
|
|
||||||
platform: linux/amd64
|
|
||||||
crowdsec_version: ""
|
|
||||||
image_version: dev
|
|
||||||
latest: false
|
|
||||||
push: true
|
|
||||||
slim: false
|
|
||||||
debian: true
|
|
||||||
secrets:
|
|
||||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
|
||||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
|
48
.github/workflows/publish-docker-release.yml
vendored
48
.github/workflows/publish-docker-release.yml
vendored
|
@ -1,48 +0,0 @@
|
||||||
name: (manual) Publish Docker images
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_dispatch:
|
|
||||||
inputs:
|
|
||||||
image_version:
|
|
||||||
description: Docker Image version (base tag, i.e. v1.6.0-2)
|
|
||||||
required: true
|
|
||||||
crowdsec_version:
|
|
||||||
description: Crowdsec version (BUILD_VERSION)
|
|
||||||
required: true
|
|
||||||
latest:
|
|
||||||
description: Overwrite latest (and slim) tags?
|
|
||||||
default: false
|
|
||||||
required: true
|
|
||||||
push:
|
|
||||||
description: Really push?
|
|
||||||
default: false
|
|
||||||
required: true
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
alpine:
|
|
||||||
uses: ./.github/workflows/publish-docker.yml
|
|
||||||
secrets:
|
|
||||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
|
||||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
|
||||||
with:
|
|
||||||
image_version: ${{ github.event.inputs.image_version }}
|
|
||||||
crowdsec_version: ${{ github.event.inputs.crowdsec_version }}
|
|
||||||
latest: ${{ github.event.inputs.latest == 'true' }}
|
|
||||||
push: ${{ github.event.inputs.push == 'true' }}
|
|
||||||
slim: true
|
|
||||||
debian: false
|
|
||||||
platform: "linux/amd64,linux/386,linux/arm64,linux/arm/v7,linux/arm/v6"
|
|
||||||
|
|
||||||
debian:
|
|
||||||
uses: ./.github/workflows/publish-docker.yml
|
|
||||||
secrets:
|
|
||||||
DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }}
|
|
||||||
DOCKER_PASSWORD: ${{ secrets.DOCKER_PASSWORD }}
|
|
||||||
with:
|
|
||||||
image_version: ${{ github.event.inputs.image_version }}
|
|
||||||
crowdsec_version: ${{ github.event.inputs.crowdsec_version }}
|
|
||||||
latest: ${{ github.event.inputs.latest == 'true' }}
|
|
||||||
push: ${{ github.event.inputs.push == 'true' }}
|
|
||||||
slim: false
|
|
||||||
debian: true
|
|
||||||
platform: "linux/amd64,linux/386,linux/arm64"
|
|
125
.github/workflows/publish-docker.yml
vendored
125
.github/workflows/publish-docker.yml
vendored
|
@ -1,125 +0,0 @@
|
||||||
name: (sub) Publish Docker images
|
|
||||||
|
|
||||||
on:
|
|
||||||
workflow_call:
|
|
||||||
secrets:
|
|
||||||
DOCKER_USERNAME:
|
|
||||||
required: true
|
|
||||||
DOCKER_PASSWORD:
|
|
||||||
required: true
|
|
||||||
inputs:
|
|
||||||
platform:
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
image_version:
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
crowdsec_version:
|
|
||||||
required: true
|
|
||||||
type: string
|
|
||||||
latest:
|
|
||||||
required: true
|
|
||||||
type: boolean
|
|
||||||
push:
|
|
||||||
required: true
|
|
||||||
type: boolean
|
|
||||||
slim:
|
|
||||||
required: true
|
|
||||||
type: boolean
|
|
||||||
debian:
|
|
||||||
required: true
|
|
||||||
type: boolean
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
push_to_registry:
|
|
||||||
name: Push Docker image to registries
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
|
|
||||||
- name: Check out the repo
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Set up QEMU
|
|
||||||
uses: docker/setup-qemu-action@v3
|
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
|
||||||
uses: docker/setup-buildx-action@v3
|
|
||||||
with:
|
|
||||||
config: .github/buildkit.toml
|
|
||||||
|
|
||||||
- name: Login to DockerHub
|
|
||||||
uses: docker/login-action@v3
|
|
||||||
with:
|
|
||||||
username: ${{ secrets.DOCKER_USERNAME }}
|
|
||||||
password: ${{ secrets.DOCKER_PASSWORD }}
|
|
||||||
|
|
||||||
- name: Login to GitHub Container Registry
|
|
||||||
uses: docker/login-action@v3
|
|
||||||
with:
|
|
||||||
registry: ghcr.io
|
|
||||||
username: ${{ github.repository_owner }}
|
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
- name: Prepare (slim)
|
|
||||||
if: ${{ inputs.slim }}
|
|
||||||
id: slim
|
|
||||||
run: |
|
|
||||||
DOCKERHUB_IMAGE=${{ secrets.DOCKER_USERNAME }}/crowdsec
|
|
||||||
GHCR_IMAGE=ghcr.io/${{ github.repository_owner }}/crowdsec
|
|
||||||
VERSION=${{ inputs.image_version }}
|
|
||||||
DEBIAN=${{ inputs.debian && '-debian' || '' }}
|
|
||||||
TAGS="${DOCKERHUB_IMAGE}:${VERSION}-slim${DEBIAN},${GHCR_IMAGE}:${VERSION}-slim${DEBIAN}"
|
|
||||||
if [[ ${{ inputs.latest }} == true ]]; then
|
|
||||||
TAGS=$TAGS,${DOCKERHUB_IMAGE}:slim${DEBIAN},${GHCR_IMAGE}:slim${DEBIAN}
|
|
||||||
fi
|
|
||||||
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
|
|
||||||
echo "created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
|
|
||||||
|
|
||||||
- name: Prepare (full)
|
|
||||||
id: full
|
|
||||||
run: |
|
|
||||||
DOCKERHUB_IMAGE=${{ secrets.DOCKER_USERNAME }}/crowdsec
|
|
||||||
GHCR_IMAGE=ghcr.io/${{ github.repository_owner }}/crowdsec
|
|
||||||
VERSION=${{ inputs.image_version }}
|
|
||||||
DEBIAN=${{ inputs.debian && '-debian' || '' }}
|
|
||||||
TAGS="${DOCKERHUB_IMAGE}:${VERSION}${DEBIAN},${GHCR_IMAGE}:${VERSION}${DEBIAN}"
|
|
||||||
if [[ ${{ inputs.latest }} == true ]]; then
|
|
||||||
TAGS=$TAGS,${DOCKERHUB_IMAGE}:latest${DEBIAN},${GHCR_IMAGE}:latest${DEBIAN}
|
|
||||||
fi
|
|
||||||
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
|
|
||||||
echo "created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
|
|
||||||
|
|
||||||
- name: Build and push image (slim)
|
|
||||||
if: ${{ inputs.slim }}
|
|
||||||
uses: docker/build-push-action@v5
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: ./Dockerfile${{ inputs.debian && '.debian' || '' }}
|
|
||||||
push: ${{ inputs.push }}
|
|
||||||
tags: ${{ steps.slim.outputs.tags }}
|
|
||||||
target: slim
|
|
||||||
platforms: ${{ inputs.platform }}
|
|
||||||
labels: |
|
|
||||||
org.opencontainers.image.source=${{ github.event.repository.html_url }}
|
|
||||||
org.opencontainers.image.created=${{ steps.slim.outputs.created }}
|
|
||||||
org.opencontainers.image.revision=${{ github.sha }}
|
|
||||||
build-args: |
|
|
||||||
BUILD_VERSION=${{ inputs.crowdsec_version }}
|
|
||||||
|
|
||||||
- name: Build and push image (full)
|
|
||||||
uses: docker/build-push-action@v5
|
|
||||||
with:
|
|
||||||
context: .
|
|
||||||
file: ./Dockerfile${{ inputs.debian && '.debian' || '' }}
|
|
||||||
push: ${{ inputs.push }}
|
|
||||||
tags: ${{ steps.full.outputs.tags }}
|
|
||||||
target: full
|
|
||||||
platforms: ${{ inputs.platform }}
|
|
||||||
labels: |
|
|
||||||
org.opencontainers.image.source=${{ github.event.repository.html_url }}
|
|
||||||
org.opencontainers.image.created=${{ steps.full.outputs.created }}
|
|
||||||
org.opencontainers.image.revision=${{ github.sha }}
|
|
||||||
build-args: |
|
|
||||||
BUILD_VERSION=${{ inputs.crowdsec_version }}
|
|
70
.github/workflows/publish_docker-image_on_master-debian.yml
vendored
Normal file
70
.github/workflows/publish_docker-image_on_master-debian.yml
vendored
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
name: Publish Debian Docker image on Push to Master
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
paths:
|
||||||
|
- 'pkg/**'
|
||||||
|
- 'cmd/**'
|
||||||
|
- 'plugins/**'
|
||||||
|
- 'docker/docker_start.sh'
|
||||||
|
- 'docker/config.yaml'
|
||||||
|
- '.github/workflows/publish_docker-image_on_master-debian.yml'
|
||||||
|
- 'Dockerfile.debian'
|
||||||
|
- 'go.mod'
|
||||||
|
- 'go.sum'
|
||||||
|
- 'Makefile'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
push_to_registry:
|
||||||
|
name: Push Debian Docker image to Docker Hub
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
|
||||||
|
- name: Check out the repo
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
id: prep
|
||||||
|
run: |
|
||||||
|
DOCKER_IMAGE=crowdsecurity/crowdsec
|
||||||
|
GHCR_IMAGE=ghcr.io/${{ github.repository_owner }}/crowdsec
|
||||||
|
VERSION=dev-debian
|
||||||
|
TAGS="${DOCKER_IMAGE}:${VERSION},${GHCR_IMAGE}:${VERSION}"
|
||||||
|
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
|
||||||
|
echo "created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
with:
|
||||||
|
config: .github/buildkit.toml
|
||||||
|
|
||||||
|
- name: Login to DockerHub
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Login to GitHub Container Registry
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.repository_owner }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build and push full image
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./Dockerfile.debian
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.prep.outputs.tags }}
|
||||||
|
platforms: linux/amd64
|
||||||
|
labels: |
|
||||||
|
org.opencontainers.image.source=${{ github.event.repository.html_url }}
|
||||||
|
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
|
||||||
|
org.opencontainers.image.revision=${{ github.sha }}
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=min
|
70
.github/workflows/publish_docker-image_on_master.yml
vendored
Normal file
70
.github/workflows/publish_docker-image_on_master.yml
vendored
Normal file
|
@ -0,0 +1,70 @@
|
||||||
|
name: Publish Docker image on Push to Master
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
paths:
|
||||||
|
- 'pkg/**'
|
||||||
|
- 'cmd/**'
|
||||||
|
- 'plugins/**'
|
||||||
|
- 'docker/docker_start.sh'
|
||||||
|
- 'docker/config.yaml'
|
||||||
|
- '.github/workflows/publish_docker-image_on_master.yml'
|
||||||
|
- 'Dockerfile'
|
||||||
|
- 'go.mod'
|
||||||
|
- 'go.sum'
|
||||||
|
- 'Makefile'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
push_to_registry:
|
||||||
|
name: Push Docker image to Docker Hub
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
|
||||||
|
- name: Check out the repo
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Prepare
|
||||||
|
id: prep
|
||||||
|
run: |
|
||||||
|
DOCKER_IMAGE=crowdsecurity/crowdsec
|
||||||
|
GHCR_IMAGE=ghcr.io/${{ github.repository_owner }}/crowdsec
|
||||||
|
VERSION=dev
|
||||||
|
TAGS="${DOCKER_IMAGE}:${VERSION},${GHCR_IMAGE}:${VERSION}"
|
||||||
|
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
|
||||||
|
echo "created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
|
||||||
|
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
with:
|
||||||
|
config: .github/buildkit.toml
|
||||||
|
|
||||||
|
- name: Login to DockerHub
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Login to GitHub Container Registry
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.repository_owner }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build and push full image
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./Dockerfile
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.prep.outputs.tags }}
|
||||||
|
platforms: linux/amd64
|
||||||
|
labels: |
|
||||||
|
org.opencontainers.image.source=${{ github.event.repository.html_url }}
|
||||||
|
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
|
||||||
|
org.opencontainers.image.revision=${{ github.sha }}
|
||||||
|
cache-from: type=gha
|
||||||
|
cache-to: type=gha,mode=min
|
|
@ -1,5 +1,5 @@
|
||||||
# .github/workflows/build-docker-image.yml
|
# .github/workflows/build-docker-image.yml
|
||||||
name: Release
|
name: build
|
||||||
|
|
||||||
on:
|
on:
|
||||||
release:
|
release:
|
||||||
|
@ -12,20 +12,24 @@ permissions:
|
||||||
|
|
||||||
jobs:
|
jobs:
|
||||||
build:
|
build:
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
go-version: ["1.21.4"]
|
||||||
|
|
||||||
name: Build and upload binary package
|
name: Build and upload binary package
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
steps:
|
steps:
|
||||||
|
|
||||||
- name: Check out code into the Go module directory
|
- name: Check out code into the Go module directory
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
with:
|
with:
|
||||||
fetch-depth: 0
|
fetch-depth: 0
|
||||||
submodules: false
|
submodules: false
|
||||||
|
|
||||||
- name: "Set up Go"
|
- name: "Set up Go ${{ matrix.go-version }}"
|
||||||
uses: actions/setup-go@v5
|
uses: actions/setup-go@v4
|
||||||
with:
|
with:
|
||||||
go-version: "1.22.2"
|
go-version: ${{ matrix.go-version }}
|
||||||
|
|
||||||
- name: Build the binaries
|
- name: Build the binaries
|
||||||
run: |
|
run: |
|
61
.github/workflows/release_publish_docker-image-debian.yml
vendored
Normal file
61
.github/workflows/release_publish_docker-image-debian.yml
vendored
Normal file
|
@ -0,0 +1,61 @@
|
||||||
|
name: Publish Docker Debian image
|
||||||
|
|
||||||
|
on:
|
||||||
|
release:
|
||||||
|
types:
|
||||||
|
- released
|
||||||
|
- prereleased
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
push_to_registry:
|
||||||
|
name: Push Docker debian image to Docker Hub
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Check out the repo
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
- name: Prepare
|
||||||
|
id: prep
|
||||||
|
run: |
|
||||||
|
DOCKER_IMAGE=crowdsecurity/crowdsec
|
||||||
|
VERSION=bullseye
|
||||||
|
if [[ $GITHUB_REF == refs/tags/* ]]; then
|
||||||
|
VERSION=${GITHUB_REF#refs/tags/}
|
||||||
|
elif [[ $GITHUB_REF == refs/heads/* ]]; then
|
||||||
|
VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -E 's#/+#-#g')
|
||||||
|
elif [[ $GITHUB_REF == refs/pull/* ]]; then
|
||||||
|
VERSION=pr-${{ github.event.number }}
|
||||||
|
fi
|
||||||
|
TAGS="${DOCKER_IMAGE}:${VERSION}-debian"
|
||||||
|
if [[ "${{ github.event.action }}" == "released" ]]; then
|
||||||
|
TAGS=$TAGS,${DOCKER_IMAGE}:latest-debian
|
||||||
|
fi
|
||||||
|
echo "version=${VERSION}" >> $GITHUB_OUTPUT
|
||||||
|
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
|
||||||
|
echo "created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
with:
|
||||||
|
config: .github/buildkit.toml
|
||||||
|
|
||||||
|
- name: Login to DockerHub
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
- name: Build and push
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./Dockerfile.debian
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.prep.outputs.tags }}
|
||||||
|
platforms: linux/amd64,linux/arm64,linux/386
|
||||||
|
labels: |
|
||||||
|
org.opencontainers.image.source=${{ github.event.repository.html_url }}
|
||||||
|
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
|
||||||
|
org.opencontainers.image.revision=${{ github.sha }}
|
86
.github/workflows/release_publish_docker-image.yml
vendored
Normal file
86
.github/workflows/release_publish_docker-image.yml
vendored
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
name: Publish Docker image
|
||||||
|
|
||||||
|
on:
|
||||||
|
release:
|
||||||
|
types:
|
||||||
|
- released
|
||||||
|
- prereleased
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
push_to_registry:
|
||||||
|
name: Push Docker image to Docker Hub
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- name: Check out the repo
|
||||||
|
uses: actions/checkout@v3
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
- name: Prepare
|
||||||
|
id: prep
|
||||||
|
run: |
|
||||||
|
DOCKER_IMAGE=crowdsecurity/crowdsec
|
||||||
|
GHCR_IMAGE=ghcr.io/${{ github.repository_owner }}/crowdsec
|
||||||
|
VERSION=edge
|
||||||
|
if [[ $GITHUB_REF == refs/tags/* ]]; then
|
||||||
|
VERSION=${GITHUB_REF#refs/tags/}
|
||||||
|
elif [[ $GITHUB_REF == refs/heads/* ]]; then
|
||||||
|
VERSION=$(echo ${GITHUB_REF#refs/heads/} | sed -E 's#/+#-#g')
|
||||||
|
elif [[ $GITHUB_REF == refs/pull/* ]]; then
|
||||||
|
VERSION=pr-${{ github.event.number }}
|
||||||
|
fi
|
||||||
|
TAGS="${DOCKER_IMAGE}:${VERSION},${GHCR_IMAGE}:${VERSION}"
|
||||||
|
TAGS_SLIM="${DOCKER_IMAGE}:${VERSION}-slim"
|
||||||
|
if [[ ${{ github.event.action }} == released ]]; then
|
||||||
|
TAGS=$TAGS,${DOCKER_IMAGE}:latest,${GHCR_IMAGE}:latest
|
||||||
|
TAGS_SLIM=$TAGS_SLIM,${DOCKER_IMAGE}:slim
|
||||||
|
fi
|
||||||
|
echo "version=${VERSION}" >> $GITHUB_OUTPUT
|
||||||
|
echo "tags=${TAGS}" >> $GITHUB_OUTPUT
|
||||||
|
echo "tags_slim=${TAGS_SLIM}" >> $GITHUB_OUTPUT
|
||||||
|
echo "created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
|
||||||
|
- name: Set up QEMU
|
||||||
|
uses: docker/setup-qemu-action@v2
|
||||||
|
- name: Set up Docker Buildx
|
||||||
|
uses: docker/setup-buildx-action@v2
|
||||||
|
with:
|
||||||
|
config: .github/buildkit.toml
|
||||||
|
|
||||||
|
- name: Login to DockerHub
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
username: ${{ secrets.DOCKER_USERNAME }}
|
||||||
|
password: ${{ secrets.DOCKER_PASSWORD }}
|
||||||
|
|
||||||
|
- name: Login to GitHub Container Registry
|
||||||
|
uses: docker/login-action@v2
|
||||||
|
with:
|
||||||
|
registry: ghcr.io
|
||||||
|
username: ${{ github.repository_owner }}
|
||||||
|
password: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
|
||||||
|
- name: Build and push slim image
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./Dockerfile
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.prep.outputs.tags_slim }}
|
||||||
|
target: slim
|
||||||
|
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/386
|
||||||
|
labels: |
|
||||||
|
org.opencontainers.image.source=${{ github.event.repository.html_url }}
|
||||||
|
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
|
||||||
|
org.opencontainers.image.revision=${{ github.sha }}
|
||||||
|
|
||||||
|
- name: Build and push full image
|
||||||
|
uses: docker/build-push-action@v4
|
||||||
|
with:
|
||||||
|
context: .
|
||||||
|
file: ./Dockerfile
|
||||||
|
push: ${{ github.event_name != 'pull_request' }}
|
||||||
|
tags: ${{ steps.prep.outputs.tags }}
|
||||||
|
platforms: linux/amd64,linux/arm64,linux/arm/v7,linux/arm/v6,linux/386
|
||||||
|
labels: |
|
||||||
|
org.opencontainers.image.source=${{ github.event.repository.html_url }}
|
||||||
|
org.opencontainers.image.created=${{ steps.prep.outputs.created }}
|
||||||
|
org.opencontainers.image.revision=${{ github.sha }}
|
4
.github/workflows/update_docker_hub_doc.yml
vendored
4
.github/workflows/update_docker_hub_doc.yml
vendored
|
@ -1,4 +1,4 @@
|
||||||
name: (push-master) Update Docker Hub README
|
name: Update Docker Hub README
|
||||||
|
|
||||||
on:
|
on:
|
||||||
push:
|
push:
|
||||||
|
@ -13,7 +13,7 @@ jobs:
|
||||||
steps:
|
steps:
|
||||||
-
|
-
|
||||||
name: Check out the repo
|
name: Check out the repo
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v3
|
||||||
if: ${{ github.repository_owner == 'crowdsecurity' }}
|
if: ${{ github.repository_owner == 'crowdsecurity' }}
|
||||||
-
|
-
|
||||||
name: Update docker hub README
|
name: Update docker hub README
|
||||||
|
|
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -6,10 +6,7 @@
|
||||||
*.dylib
|
*.dylib
|
||||||
*~
|
*~
|
||||||
.pc
|
.pc
|
||||||
|
|
||||||
# IDEs
|
|
||||||
.vscode
|
.vscode
|
||||||
.idea
|
|
||||||
|
|
||||||
# If vendor is included, allow prebuilt (wasm?) libraries.
|
# If vendor is included, allow prebuilt (wasm?) libraries.
|
||||||
!vendor/**/*.so
|
!vendor/**/*.so
|
||||||
|
@ -37,7 +34,7 @@ test/coverage/*
|
||||||
*.swo
|
*.swo
|
||||||
|
|
||||||
# Dependencies are not vendored by default, but a tarball is created by "make vendor"
|
# Dependencies are not vendored by default, but a tarball is created by "make vendor"
|
||||||
# and provided in the release. Used by gentoo, etc.
|
# and provided in the release. Used by freebsd, gentoo, etc.
|
||||||
vendor/
|
vendor/
|
||||||
vendor.tgz
|
vendor.tgz
|
||||||
|
|
||||||
|
|
308
.golangci.yml
308
.golangci.yml
|
@ -1,66 +1,38 @@
|
||||||
# https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
|
# https://github.com/golangci/golangci-lint/blob/master/.golangci.reference.yml
|
||||||
|
|
||||||
|
run:
|
||||||
|
skip-dirs:
|
||||||
|
- pkg/time/rate
|
||||||
|
skip-files:
|
||||||
|
- pkg/database/ent/generate.go
|
||||||
|
- pkg/yamlpatch/merge.go
|
||||||
|
- pkg/yamlpatch/merge_test.go
|
||||||
|
|
||||||
linters-settings:
|
linters-settings:
|
||||||
cyclop:
|
|
||||||
# lower this after refactoring
|
|
||||||
max-complexity: 48
|
|
||||||
|
|
||||||
gci:
|
|
||||||
sections:
|
|
||||||
- standard
|
|
||||||
- default
|
|
||||||
- prefix(github.com/crowdsecurity)
|
|
||||||
- prefix(github.com/crowdsecurity/crowdsec)
|
|
||||||
|
|
||||||
gomoddirectives:
|
|
||||||
replace-allow-list:
|
|
||||||
- golang.org/x/time/rate
|
|
||||||
|
|
||||||
gocognit:
|
|
||||||
# lower this after refactoring
|
|
||||||
min-complexity: 145
|
|
||||||
|
|
||||||
gocyclo:
|
gocyclo:
|
||||||
# lower this after refactoring
|
min-complexity: 30
|
||||||
min-complexity: 48
|
|
||||||
|
|
||||||
funlen:
|
funlen:
|
||||||
# Checks the number of lines in a function.
|
# Checks the number of lines in a function.
|
||||||
# If lower than 0, disable the check.
|
# If lower than 0, disable the check.
|
||||||
# Default: 60
|
# Default: 60
|
||||||
# lower this after refactoring
|
lines: -1
|
||||||
lines: 437
|
|
||||||
# Checks the number of statements in a function.
|
# Checks the number of statements in a function.
|
||||||
# If lower than 0, disable the check.
|
# If lower than 0, disable the check.
|
||||||
# Default: 40
|
# Default: 40
|
||||||
# lower this after refactoring
|
statements: -1
|
||||||
statements: 122
|
|
||||||
|
|
||||||
govet:
|
govet:
|
||||||
enable-all: true
|
check-shadowing: true
|
||||||
disable:
|
|
||||||
- reflectvaluecompare
|
|
||||||
- fieldalignment
|
|
||||||
|
|
||||||
lll:
|
lll:
|
||||||
# lower this after refactoring
|
line-length: 140
|
||||||
line-length: 2607
|
|
||||||
|
|
||||||
maintidx:
|
|
||||||
# raise this after refactoring
|
|
||||||
under: 11
|
|
||||||
|
|
||||||
misspell:
|
misspell:
|
||||||
locale: US
|
locale: US
|
||||||
|
|
||||||
nestif:
|
|
||||||
# lower this after refactoring
|
|
||||||
min-complexity: 28
|
|
||||||
|
|
||||||
nlreturn:
|
|
||||||
block-size: 5
|
|
||||||
|
|
||||||
nolintlint:
|
nolintlint:
|
||||||
|
allow-leading-space: true # don't require machine-readable nolint directives (i.e. with no leading space)
|
||||||
allow-unused: false # report any unused nolint directives
|
allow-unused: false # report any unused nolint directives
|
||||||
require-explanation: false # don't require an explanation for nolint directives
|
require-explanation: false # don't require an explanation for nolint directives
|
||||||
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
|
require-specific: false # don't require nolint directives to be specific about which linter is being skipped
|
||||||
|
@ -68,183 +40,105 @@ linters-settings:
|
||||||
interfacebloat:
|
interfacebloat:
|
||||||
max: 12
|
max: 12
|
||||||
|
|
||||||
depguard:
|
|
||||||
rules:
|
|
||||||
wrap:
|
|
||||||
deny:
|
|
||||||
- pkg: "github.com/pkg/errors"
|
|
||||||
desc: "errors.Wrap() is deprecated in favor of fmt.Errorf()"
|
|
||||||
files:
|
|
||||||
- "!**/pkg/database/*.go"
|
|
||||||
- "!**/pkg/exprhelpers/*.go"
|
|
||||||
- "!**/pkg/acquisition/modules/appsec/appsec.go"
|
|
||||||
- "!**/pkg/acquisition/modules/loki/internal/lokiclient/loki_client.go"
|
|
||||||
- "!**/pkg/apiserver/controllers/v1/errors.go"
|
|
||||||
yaml:
|
|
||||||
files:
|
|
||||||
- "!**/pkg/acquisition/acquisition.go"
|
|
||||||
- "!**/pkg/acquisition/acquisition_test.go"
|
|
||||||
- "!**/pkg/acquisition/modules/appsec/appsec.go"
|
|
||||||
- "!**/pkg/acquisition/modules/cloudwatch/cloudwatch.go"
|
|
||||||
- "!**/pkg/acquisition/modules/docker/docker.go"
|
|
||||||
- "!**/pkg/acquisition/modules/file/file.go"
|
|
||||||
- "!**/pkg/acquisition/modules/journalctl/journalctl.go"
|
|
||||||
- "!**/pkg/acquisition/modules/kafka/kafka.go"
|
|
||||||
- "!**/pkg/acquisition/modules/kinesis/kinesis.go"
|
|
||||||
- "!**/pkg/acquisition/modules/kubernetesaudit/k8s_audit.go"
|
|
||||||
- "!**/pkg/acquisition/modules/loki/loki.go"
|
|
||||||
- "!**/pkg/acquisition/modules/loki/timestamp_test.go"
|
|
||||||
- "!**/pkg/acquisition/modules/s3/s3.go"
|
|
||||||
- "!**/pkg/acquisition/modules/syslog/syslog.go"
|
|
||||||
- "!**/pkg/acquisition/modules/wineventlog/wineventlog_windows.go"
|
|
||||||
- "!**/pkg/appsec/appsec.go"
|
|
||||||
- "!**/pkg/appsec/loader.go"
|
|
||||||
- "!**/pkg/csplugin/broker.go"
|
|
||||||
- "!**/pkg/csplugin/broker_test.go"
|
|
||||||
- "!**/pkg/dumps/bucket_dump.go"
|
|
||||||
- "!**/pkg/dumps/parser_dump.go"
|
|
||||||
- "!**/pkg/hubtest/coverage.go"
|
|
||||||
- "!**/pkg/hubtest/hubtest_item.go"
|
|
||||||
- "!**/pkg/hubtest/parser_assert.go"
|
|
||||||
- "!**/pkg/hubtest/scenario_assert.go"
|
|
||||||
- "!**/pkg/leakybucket/buckets_test.go"
|
|
||||||
- "!**/pkg/leakybucket/manager_load.go"
|
|
||||||
- "!**/pkg/metabase/metabase.go"
|
|
||||||
- "!**/pkg/parser/node.go"
|
|
||||||
- "!**/pkg/parser/node_test.go"
|
|
||||||
- "!**/pkg/parser/parsing_test.go"
|
|
||||||
- "!**/pkg/parser/stage.go"
|
|
||||||
deny:
|
|
||||||
- pkg: "gopkg.in/yaml.v2"
|
|
||||||
desc: "yaml.v2 is deprecated for new code in favor of yaml.v3"
|
|
||||||
|
|
||||||
wsl:
|
|
||||||
# Allow blocks to end with comments
|
|
||||||
allow-trailing-comment: true
|
|
||||||
|
|
||||||
linters:
|
linters:
|
||||||
enable-all: true
|
enable-all: true
|
||||||
disable:
|
disable:
|
||||||
#
|
#
|
||||||
# DEPRECATED by golangi-lint
|
# DEPRECATED by golangi-lint
|
||||||
#
|
#
|
||||||
- deadcode
|
- deadcode # The owner seems to have abandoned the linter. Replaced by unused.
|
||||||
- exhaustivestruct
|
- exhaustivestruct # The owner seems to have abandoned the linter. Replaced by exhaustruct.
|
||||||
- golint
|
- golint # Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes
|
||||||
- ifshort
|
- ifshort # Checks that your code uses short syntax for if-statements whenever possible
|
||||||
- interfacer
|
- interfacer # Linter that suggests narrower interface types
|
||||||
- maligned
|
- maligned # Tool to detect Go structs that would take less memory if their fields were sorted
|
||||||
- nosnakecase
|
- nosnakecase # nosnakecase is a linter that detects snake case of variable naming and function name.
|
||||||
- scopelint
|
- scopelint # Scopelint checks for unpinned variables in go programs
|
||||||
- structcheck
|
- structcheck # The owner seems to have abandoned the linter. Replaced by unused.
|
||||||
- varcheck
|
- varcheck # The owner seems to have abandoned the linter. Replaced by unused.
|
||||||
|
|
||||||
#
|
|
||||||
# Disabled until fixed for go 1.22
|
|
||||||
#
|
|
||||||
|
|
||||||
- copyloopvar # copyloopvar is a linter detects places where loop variables are copied
|
|
||||||
- intrange # intrange is a linter to find places where for loops could make use of an integer range.
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Enabled
|
# Enabled
|
||||||
#
|
#
|
||||||
|
|
||||||
# - asasalint # check for pass []any as any in variadic func(...any)
|
# - asasalint # check for pass []any as any in variadic func(...any)
|
||||||
# - asciicheck # checks that all code identifiers does not have non-ASCII symbols in the name
|
# - asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers
|
||||||
# - bidichk # Checks for dangerous unicode character sequences
|
# - bidichk # Checks for dangerous unicode character sequences
|
||||||
# - bodyclose # checks whether HTTP response body is closed successfully
|
|
||||||
# - cyclop # checks function and package cyclomatic complexity
|
|
||||||
# - decorder # check declaration order and count of types, constants, variables and functions
|
# - decorder # check declaration order and count of types, constants, variables and functions
|
||||||
# - depguard # Go linter that checks if package imports are in a list of acceptable packages
|
|
||||||
# - dupword # checks for duplicate words in the source code
|
# - dupword # checks for duplicate words in the source code
|
||||||
# - durationcheck # check for two durations multiplied together
|
# - durationcheck # check for two durations multiplied together
|
||||||
# - errcheck # errcheck is a program for checking for unchecked errors in Go code. These unchecked errors can be critical bugs in some cases
|
# - errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases
|
||||||
# - errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
|
|
||||||
# - execinquery # execinquery is a linter about query string checker in Query function which reads your Go src files and warning it finds
|
|
||||||
# - exportloopref # checks for pointers to enclosing loop variables
|
# - exportloopref # checks for pointers to enclosing loop variables
|
||||||
# - funlen # Tool for detection of long functions
|
# - funlen # Tool for detection of long functions
|
||||||
# - ginkgolinter # enforces standards of using ginkgo and gomega
|
# - ginkgolinter # enforces standards of using ginkgo and gomega
|
||||||
# - gocheckcompilerdirectives # Checks that go compiler directive comments (//go:) are valid.
|
|
||||||
# - gochecknoinits # Checks that no init functions are present in Go code
|
# - gochecknoinits # Checks that no init functions are present in Go code
|
||||||
# - gochecksumtype # Run exhaustiveness checks on Go "sum types"
|
|
||||||
# - gocognit # Computes and checks the cognitive complexity of functions
|
|
||||||
# - gocritic # Provides diagnostics that check for bugs, performance and style issues.
|
# - gocritic # Provides diagnostics that check for bugs, performance and style issues.
|
||||||
# - gocyclo # Computes and checks the cyclomatic complexity of functions
|
|
||||||
# - goheader # Checks is file header matches to pattern
|
# - goheader # Checks is file header matches to pattern
|
||||||
# - gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
|
# - gomoddirectives # Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.
|
||||||
# - gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations.
|
# - gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations.
|
||||||
# - goprintffuncname # Checks that printf-like functions are named with `f` at the end
|
# - goprintffuncname # Checks that printf-like functions are named with `f` at the end
|
||||||
# - gosimple # (megacheck): Linter for Go source code that specializes in simplifying code
|
# - gosimple # (megacheck): Linter for Go source code that specializes in simplifying a code
|
||||||
# - gosmopolitan # Report certain i18n/l10n anti-patterns in your Go codebase
|
# - govet # (vet, vetshadow): Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string
|
||||||
# - govet # (vet, vetshadow): Vet examines Go source code and reports suspicious constructs. It is roughly the same as 'go vet' and uses its passes.
|
# - grouper # An analyzer to analyze expression groups.
|
||||||
# - grouper # Analyze expression groups.
|
|
||||||
# - importas # Enforces consistent import aliases
|
# - importas # Enforces consistent import aliases
|
||||||
# - ineffassign # Detects when assignments to existing variables are not used
|
# - ineffassign # Detects when assignments to existing variables are not used
|
||||||
# - interfacebloat # A linter that checks the number of methods inside an interface.
|
# - interfacebloat # A linter that checks the number of methods inside an interface.
|
||||||
# - lll # Reports long lines
|
|
||||||
# - loggercheck # (logrlint): Checks key value pairs for common logger libraries (kitlog,klog,logr,zap).
|
|
||||||
# - logrlint # Check logr arguments.
|
# - logrlint # Check logr arguments.
|
||||||
# - maintidx # maintidx measures the maintainability index of each function.
|
|
||||||
# - makezero # Finds slice declarations with non-zero initial length
|
# - makezero # Finds slice declarations with non-zero initial length
|
||||||
# - mirror # reports wrong mirror patterns of bytes/strings usage
|
# - misspell # Finds commonly misspelled English words in comments
|
||||||
# - misspell # Finds commonly misspelled English words
|
|
||||||
# - nakedret # Checks that functions with naked returns are not longer than a maximum size (can be zero).
|
|
||||||
# - nestif # Reports deeply nested if statements
|
|
||||||
# - nilerr # Finds the code that returns nil even if it checks that the error is not nil.
|
# - nilerr # Finds the code that returns nil even if it checks that the error is not nil.
|
||||||
# - nolintlint # Reports ill-formed or insufficient nolint directives
|
# - nolintlint # Reports ill-formed or insufficient nolint directives
|
||||||
# - nonamedreturns # Reports all named returns
|
|
||||||
# - nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL.
|
|
||||||
# - perfsprint # Checks that fmt.Sprintf can be replaced with a faster alternative.
|
|
||||||
# - predeclared # find code that shadows one of Go's predeclared identifiers
|
# - predeclared # find code that shadows one of Go's predeclared identifiers
|
||||||
# - reassign # Checks that package variables are not reassigned
|
# - reassign # Checks that package variables are not reassigned
|
||||||
# - rowserrcheck # checks whether Rows.Err of rows is checked successfully
|
# - rowserrcheck # checks whether Err of rows is checked successfully
|
||||||
# - sloglint # ensure consistent code style when using log/slog
|
# - sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed.
|
||||||
# - spancheck # Checks for mistakes with OpenTelemetry/Census spans.
|
# - staticcheck # (megacheck): Staticcheck is a go vet on steroids, applying a ton of static analysis checks
|
||||||
# - sqlclosecheck # Checks that sql.Rows, sql.Stmt, sqlx.NamedStmt, pgx.Query are closed.
|
|
||||||
# - staticcheck # (megacheck): It's a set of rules from staticcheck. It's not the same thing as the staticcheck binary. The author of staticcheck doesn't support or approve the use of staticcheck as a library inside golangci-lint.
|
|
||||||
# - tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17
|
|
||||||
# - testableexamples # linter checks if examples are testable (have an expected output)
|
# - testableexamples # linter checks if examples are testable (have an expected output)
|
||||||
# - testifylint # Checks usage of github.com/stretchr/testify.
|
# - tenv # tenv is analyzer that detects using os.Setenv instead of t.Setenv since Go1.17
|
||||||
# - tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes
|
# - tparallel # tparallel detects inappropriate usage of t.Parallel() method in your Go test codes
|
||||||
|
# - typecheck # Like the front-end of a Go compiler, parses and type-checks Go code
|
||||||
# - unconvert # Remove unnecessary type conversions
|
# - unconvert # Remove unnecessary type conversions
|
||||||
# - unused # (megacheck): Checks Go code for unused constants, variables, functions and types
|
# - unused # (megacheck): Checks Go code for unused constants, variables, functions and types
|
||||||
# - usestdlibvars # A linter that detect the possibility to use variables/constants from the Go standard library.
|
# - usestdlibvars # A linter that detect the possibility to use variables/constants from the Go standard library.
|
||||||
# - wastedassign # Finds wasted assignment statements
|
|
||||||
# - zerologlint # Detects the wrong usage of `zerolog` that a user forgets to dispatch with `Send` or `Msg`
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Recommended? (easy)
|
# Recommended? (easy)
|
||||||
#
|
#
|
||||||
|
|
||||||
|
- depguard # Go linter that checks if package imports are in a list of acceptable packages
|
||||||
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
|
- dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f())
|
||||||
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and reports occations, where the check for the returned error can be omitted.
|
- errchkjson # Checks types passed to the json encoding functions. Reports unsupported types and optionally reports occations, where the check for the returned error can be omitted.
|
||||||
|
- errorlint # errorlint is a linter for that can be used to find code that will cause problems with the error wrapping scheme introduced in Go 1.13.
|
||||||
- exhaustive # check exhaustiveness of enum switch statements
|
- exhaustive # check exhaustiveness of enum switch statements
|
||||||
- gci # Gci control golang package import order and make it always deterministic.
|
- gci # Gci control golang package import order and make it always deterministic.
|
||||||
- godot # Check if comments end in a period
|
- godot # Check if comments end in a period
|
||||||
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification
|
- gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification
|
||||||
- goimports # Check import statements are formatted according to the 'goimport' command. Reformat imports in autofix mode.
|
- goimports # In addition to fixing imports, goimports also formats your code in the same style as gofmt.
|
||||||
- gosec # (gas): Inspects source code for security problems
|
- gosec # (gas): Inspects source code for security problems
|
||||||
- inamedparam # reports interfaces with unnamed method parameters
|
- lll # Reports long lines
|
||||||
- musttag # enforce field tags in (un)marshaled structs
|
- musttag # enforce field tags in (un)marshaled structs
|
||||||
|
- nakedret # Finds naked returns in functions greater than a specified function length
|
||||||
|
- nonamedreturns # Reports all named returns
|
||||||
|
- nosprintfhostport # Checks for misuse of Sprintf to construct a host with port in a URL.
|
||||||
- promlinter # Check Prometheus metrics naming via promlint
|
- promlinter # Check Prometheus metrics naming via promlint
|
||||||
- protogetter # Reports direct reads from proto message fields when getters should be used
|
|
||||||
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint.
|
- revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint.
|
||||||
- tagalign # check that struct tags are well aligned
|
- tagalign # check that struct tags are well aligned [fast: true, auto-fix: true]
|
||||||
- thelper # thelper detects tests helpers which is not start with t.Helper() method.
|
- thelper # thelper detects golang test helpers without t.Helper() call and checks the consistency of test helpers
|
||||||
|
- wastedassign # wastedassign finds wasted assignment statements.
|
||||||
- wrapcheck # Checks that errors returned from external packages are wrapped
|
- wrapcheck # Checks that errors returned from external packages are wrapped
|
||||||
|
|
||||||
#
|
#
|
||||||
# Recommended? (requires some work)
|
# Recommended? (requires some work)
|
||||||
#
|
#
|
||||||
|
|
||||||
|
- bodyclose # checks whether HTTP response body is closed successfully
|
||||||
- containedctx # containedctx is a linter that detects struct contained context.Context field
|
- containedctx # containedctx is a linter that detects struct contained context.Context field
|
||||||
- contextcheck # check whether the function uses a non-inherited context
|
- contextcheck # check the function whether use a non-inherited context
|
||||||
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`.
|
- errname # Checks that sentinel errors are prefixed with the `Err` and error types are suffixed with the `Error`.
|
||||||
- gomnd # An analyzer to detect magic numbers.
|
- gomnd # An analyzer to detect magic numbers.
|
||||||
- ireturn # Accept Interfaces, Return Concrete Types
|
- ireturn # Accept Interfaces, Return Concrete Types
|
||||||
- nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value.
|
- nilnil # Checks that there is no simultaneous return of `nil` error and an invalid value.
|
||||||
- noctx # Finds sending http request without context.Context
|
- noctx # noctx finds sending http request without context.Context
|
||||||
- unparam # Reports unused function parameters
|
- unparam # Reports unused function parameters
|
||||||
|
|
||||||
#
|
#
|
||||||
|
@ -253,25 +147,31 @@ linters:
|
||||||
|
|
||||||
- gofumpt # Gofumpt checks whether code was gofumpt-ed.
|
- gofumpt # Gofumpt checks whether code was gofumpt-ed.
|
||||||
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity
|
- nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity
|
||||||
- whitespace # Whitespace is a linter that checks for unnecessary newlines at the start and end of functions, if, for, etc.
|
- whitespace # Tool for detection of leading and trailing whitespace
|
||||||
- wsl # add or remove empty lines
|
- wsl # Whitespace Linter - Forces you to use empty lines!
|
||||||
|
|
||||||
#
|
#
|
||||||
# Well intended, but not ready for this
|
# Well intended, but not ready for this
|
||||||
#
|
#
|
||||||
|
- cyclop # checks function and package cyclomatic complexity
|
||||||
- dupl # Tool for code clone detection
|
- dupl # Tool for code clone detection
|
||||||
- forcetypeassert # finds forced type assertions
|
- forcetypeassert # finds forced type assertions
|
||||||
|
- gocognit # Computes and checks the cognitive complexity of functions
|
||||||
|
- gocyclo # Computes and checks the cyclomatic complexity of functions
|
||||||
- godox # Tool for detection of FIXME, TODO and other comment keywords
|
- godox # Tool for detection of FIXME, TODO and other comment keywords
|
||||||
- goerr113 # Go linter to check the errors handling expressions
|
- goerr113 # Golang linter to check the errors handling expressions
|
||||||
- paralleltest # Detects missing usage of t.Parallel() method in your Go test
|
- maintidx # maintidx measures the maintainability index of each function.
|
||||||
|
- nestif # Reports deeply nested if statements
|
||||||
|
- paralleltest # paralleltest detects missing usage of t.Parallel() method in your Go test
|
||||||
- testpackage # linter that makes you use a separate _test package
|
- testpackage # linter that makes you use a separate _test package
|
||||||
|
|
||||||
#
|
#
|
||||||
# Too strict / too many false positives (for now?)
|
# Too strict / too many false positives (for now?)
|
||||||
#
|
#
|
||||||
|
- execinquery # execinquery is a linter about query string checker in Query function which reads your Go src files and warning it finds
|
||||||
- exhaustruct # Checks if all structure fields are initialized
|
- exhaustruct # Checks if all structure fields are initialized
|
||||||
- forbidigo # Forbids identifiers
|
- forbidigo # Forbids identifiers
|
||||||
- gochecknoglobals # Check that no global variables exist.
|
- gochecknoglobals # check that no global variables exist
|
||||||
- goconst # Finds repeated strings that could be replaced by a constant
|
- goconst # Finds repeated strings that could be replaced by a constant
|
||||||
- stylecheck # Stylecheck is a replacement for golint
|
- stylecheck # Stylecheck is a replacement for golint
|
||||||
- tagliatelle # Checks the struct tags.
|
- tagliatelle # Checks the struct tags.
|
||||||
|
@ -288,30 +188,41 @@ issues:
|
||||||
# “Look, that’s why there’s rules, understand? So that you think before you
|
# “Look, that’s why there’s rules, understand? So that you think before you
|
||||||
# break ‘em.” ― Terry Pratchett
|
# break ‘em.” ― Terry Pratchett
|
||||||
|
|
||||||
exclude-dirs:
|
|
||||||
- pkg/time/rate
|
|
||||||
|
|
||||||
exclude-files:
|
|
||||||
- pkg/yamlpatch/merge.go
|
|
||||||
- pkg/yamlpatch/merge_test.go
|
|
||||||
|
|
||||||
exclude-generated-strict: true
|
|
||||||
|
|
||||||
max-issues-per-linter: 0
|
max-issues-per-linter: 0
|
||||||
max-same-issues: 0
|
max-same-issues: 10
|
||||||
exclude-rules:
|
exclude-rules:
|
||||||
|
- path: go.mod
|
||||||
# Won't fix:
|
text: "replacement are not allowed: golang.org/x/time/rate"
|
||||||
|
|
||||||
# `err` is often shadowed, we may continue to do it
|
# `err` is often shadowed, we may continue to do it
|
||||||
- linters:
|
- linters:
|
||||||
- govet
|
- govet
|
||||||
text: "shadow: declaration of \"err\" shadows declaration"
|
text: "shadow: declaration of \"err\" shadows declaration"
|
||||||
|
|
||||||
|
#
|
||||||
|
# typecheck
|
||||||
|
#
|
||||||
|
|
||||||
|
- linters:
|
||||||
|
- typecheck
|
||||||
|
text: "undefined: min"
|
||||||
|
|
||||||
|
- linters:
|
||||||
|
- typecheck
|
||||||
|
text: "undefined: max"
|
||||||
|
|
||||||
|
#
|
||||||
|
# errcheck
|
||||||
|
#
|
||||||
|
|
||||||
- linters:
|
- linters:
|
||||||
- errcheck
|
- errcheck
|
||||||
text: "Error return value of `.*` is not checked"
|
text: "Error return value of `.*` is not checked"
|
||||||
|
|
||||||
|
#
|
||||||
|
# gocritic
|
||||||
|
#
|
||||||
|
|
||||||
- linters:
|
- linters:
|
||||||
- gocritic
|
- gocritic
|
||||||
text: "ifElseChain: rewrite if-else to switch statement"
|
text: "ifElseChain: rewrite if-else to switch statement"
|
||||||
|
@ -328,55 +239,6 @@ issues:
|
||||||
- gocritic
|
- gocritic
|
||||||
text: "commentFormatting: put a space between `//` and comment text"
|
text: "commentFormatting: put a space between `//` and comment text"
|
||||||
|
|
||||||
# Will fix, trivial - just beware of merge conflicts
|
|
||||||
|
|
||||||
- linters:
|
- linters:
|
||||||
- perfsprint
|
- staticcheck
|
||||||
text: "fmt.Sprintf can be replaced .*"
|
text: "x509.ParseCRL has been deprecated since Go 1.19: Use ParseRevocationList instead"
|
||||||
|
|
||||||
- linters:
|
|
||||||
- perfsprint
|
|
||||||
text: "fmt.Errorf can be replaced with errors.New"
|
|
||||||
|
|
||||||
#
|
|
||||||
# Will fix, easy but some neurons required
|
|
||||||
#
|
|
||||||
|
|
||||||
- linters:
|
|
||||||
- errorlint
|
|
||||||
text: "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
|
|
||||||
|
|
||||||
- linters:
|
|
||||||
- errorlint
|
|
||||||
text: "type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors"
|
|
||||||
|
|
||||||
- linters:
|
|
||||||
- errorlint
|
|
||||||
text: "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
|
|
||||||
|
|
||||||
- linters:
|
|
||||||
- errorlint
|
|
||||||
text: "type assertion on error will fail on wrapped errors. Use errors.Is to check for specific errors"
|
|
||||||
|
|
||||||
- linters:
|
|
||||||
- errorlint
|
|
||||||
text: "comparing with .* will fail on wrapped errors. Use errors.Is to check for a specific error"
|
|
||||||
|
|
||||||
- linters:
|
|
||||||
- errorlint
|
|
||||||
text: "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors"
|
|
||||||
|
|
||||||
- linters:
|
|
||||||
- nosprintfhostport
|
|
||||||
text: "host:port in url should be constructed with net.JoinHostPort and not directly with fmt.Sprintf"
|
|
||||||
|
|
||||||
# https://github.com/timakin/bodyclose
|
|
||||||
- linters:
|
|
||||||
- bodyclose
|
|
||||||
text: "response body must be closed"
|
|
||||||
|
|
||||||
# named/naked returns are evil, with a single exception
|
|
||||||
# https://go.dev/wiki/CodeReviewComments#named-result-parameters
|
|
||||||
- linters:
|
|
||||||
- nonamedreturns
|
|
||||||
text: "named return .* with type .* found"
|
|
||||||
|
|
37
Dockerfile
37
Dockerfile
|
@ -1,13 +1,12 @@
|
||||||
# vim: set ft=dockerfile:
|
# vim: set ft=dockerfile:
|
||||||
FROM golang:1.22.2-alpine3.18 AS build
|
ARG GOVERSION=1.21.4
|
||||||
|
|
||||||
ARG BUILD_VERSION
|
FROM golang:${GOVERSION}-alpine AS build
|
||||||
|
|
||||||
WORKDIR /go/src/crowdsec
|
WORKDIR /go/src/crowdsec
|
||||||
|
|
||||||
# We like to choose the release of re2 to use, and Alpine does not ship a static version anyway.
|
# We like to choose the release of re2 to use, and Alpine does not ship a static version anyway.
|
||||||
ENV RE2_VERSION=2023-03-01
|
ENV RE2_VERSION=2023-03-01
|
||||||
ENV BUILD_VERSION=${BUILD_VERSION}
|
|
||||||
|
|
||||||
# wizard.sh requires GNU coreutils
|
# wizard.sh requires GNU coreutils
|
||||||
RUN apk add --no-cache git g++ gcc libc-dev make bash gettext binutils-gold coreutils pkgconfig && \
|
RUN apk add --no-cache git g++ gcc libc-dev make bash gettext binutils-gold coreutils pkgconfig && \
|
||||||
|
@ -16,7 +15,7 @@ RUN apk add --no-cache git g++ gcc libc-dev make bash gettext binutils-gold core
|
||||||
cd re2-${RE2_VERSION} && \
|
cd re2-${RE2_VERSION} && \
|
||||||
make install && \
|
make install && \
|
||||||
echo "githubciXXXXXXXXXXXXXXXXXXXXXXXX" > /etc/machine-id && \
|
echo "githubciXXXXXXXXXXXXXXXXXXXXXXXX" > /etc/machine-id && \
|
||||||
go install github.com/mikefarah/yq/v4@v4.43.1
|
go install github.com/mikefarah/yq/v4@v4.34.1
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
|
@ -39,25 +38,31 @@ RUN apk add --no-cache --repository=http://dl-cdn.alpinelinux.org/alpine/edge/co
|
||||||
mkdir -p /staging/var/lib/crowdsec && \
|
mkdir -p /staging/var/lib/crowdsec && \
|
||||||
mkdir -p /var/lib/crowdsec/data
|
mkdir -p /var/lib/crowdsec/data
|
||||||
|
|
||||||
COPY --from=build /go/bin/yq /usr/local/bin/crowdsec /usr/local/bin/cscli /usr/local/bin/
|
COPY --from=build /go/bin/yq /usr/local/bin/yq
|
||||||
COPY --from=build /etc/crowdsec /staging/etc/crowdsec
|
COPY --from=build /etc/crowdsec /staging/etc/crowdsec
|
||||||
|
COPY --from=build /usr/local/bin/crowdsec /usr/local/bin/crowdsec
|
||||||
|
COPY --from=build /usr/local/bin/cscli /usr/local/bin/cscli
|
||||||
COPY --from=build /go/src/crowdsec/docker/docker_start.sh /
|
COPY --from=build /go/src/crowdsec/docker/docker_start.sh /
|
||||||
COPY --from=build /go/src/crowdsec/docker/config.yaml /staging/etc/crowdsec/config.yaml
|
COPY --from=build /go/src/crowdsec/docker/config.yaml /staging/etc/crowdsec/config.yaml
|
||||||
COPY --from=build /var/lib/crowdsec /staging/var/lib/crowdsec
|
|
||||||
RUN yq -n '.url="http://0.0.0.0:8080"' | install -m 0600 /dev/stdin /staging/etc/crowdsec/local_api_credentials.yaml
|
RUN yq -n '.url="http://0.0.0.0:8080"' | install -m 0600 /dev/stdin /staging/etc/crowdsec/local_api_credentials.yaml
|
||||||
|
|
||||||
ENTRYPOINT /bin/bash /docker_start.sh
|
ENTRYPOINT /bin/bash docker_start.sh
|
||||||
|
|
||||||
FROM slim as full
|
FROM slim as plugins
|
||||||
|
|
||||||
# Due to the wizard using cp -n, we have to copy the config files directly from the source as -n does not exist in busybox cp
|
# Due to the wizard using cp -n, we have to copy the config files directly from the source as -n does not exist in busybox cp
|
||||||
# The files are here for reference, as users will need to mount a new version to be actually able to use notifications
|
# The files are here for reference, as users will need to mount a new version to be actually able to use notifications
|
||||||
COPY --from=build \
|
COPY --from=build /go/src/crowdsec/cmd/notification-email/email.yaml /staging/etc/crowdsec/notifications/email.yaml
|
||||||
/go/src/crowdsec/cmd/notification-email/email.yaml \
|
COPY --from=build /go/src/crowdsec/cmd/notification-http/http.yaml /staging/etc/crowdsec/notifications/http.yaml
|
||||||
/go/src/crowdsec/cmd/notification-http/http.yaml \
|
COPY --from=build /go/src/crowdsec/cmd/notification-slack/slack.yaml /staging/etc/crowdsec/notifications/slack.yaml
|
||||||
/go/src/crowdsec/cmd/notification-slack/slack.yaml \
|
COPY --from=build /go/src/crowdsec/cmd/notification-splunk/splunk.yaml /staging/etc/crowdsec/notifications/splunk.yaml
|
||||||
/go/src/crowdsec/cmd/notification-splunk/splunk.yaml \
|
COPY --from=build /go/src/crowdsec/cmd/notification-sentinel/sentinel.yaml /staging/etc/crowdsec/notifications/sentinel.yaml
|
||||||
/go/src/crowdsec/cmd/notification-sentinel/sentinel.yaml \
|
|
||||||
/staging/etc/crowdsec/notifications/
|
|
||||||
|
|
||||||
COPY --from=build /usr/local/lib/crowdsec/plugins /usr/local/lib/crowdsec/plugins
|
COPY --from=build /usr/local/lib/crowdsec/plugins /usr/local/lib/crowdsec/plugins
|
||||||
|
|
||||||
|
FROM slim as geoip
|
||||||
|
|
||||||
|
COPY --from=build /var/lib/crowdsec /staging/var/lib/crowdsec
|
||||||
|
|
||||||
|
FROM plugins as full
|
||||||
|
|
||||||
|
COPY --from=build /var/lib/crowdsec /staging/var/lib/crowdsec
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# vim: set ft=dockerfile:
|
# vim: set ft=dockerfile:
|
||||||
FROM golang:1.22.2-bookworm AS build
|
ARG GOVERSION=1.21.4
|
||||||
|
|
||||||
ARG BUILD_VERSION
|
FROM golang:${GOVERSION}-bookworm AS build
|
||||||
|
|
||||||
WORKDIR /go/src/crowdsec
|
WORKDIR /go/src/crowdsec
|
||||||
|
|
||||||
|
@ -10,7 +10,6 @@ ENV DEBCONF_NOWARNINGS="yes"
|
||||||
|
|
||||||
# We like to choose the release of re2 to use, the debian version is usually older.
|
# We like to choose the release of re2 to use, the debian version is usually older.
|
||||||
ENV RE2_VERSION=2023-03-01
|
ENV RE2_VERSION=2023-03-01
|
||||||
ENV BUILD_VERSION=${BUILD_VERSION}
|
|
||||||
|
|
||||||
# wizard.sh requires GNU coreutils
|
# wizard.sh requires GNU coreutils
|
||||||
RUN apt-get update && \
|
RUN apt-get update && \
|
||||||
|
@ -21,7 +20,7 @@ RUN apt-get update && \
|
||||||
make && \
|
make && \
|
||||||
make install && \
|
make install && \
|
||||||
echo "githubciXXXXXXXXXXXXXXXXXXXXXXXX" > /etc/machine-id && \
|
echo "githubciXXXXXXXXXXXXXXXXXXXXXXXX" > /etc/machine-id && \
|
||||||
go install github.com/mikefarah/yq/v4@v4.43.1
|
go install github.com/mikefarah/yq/v4@v4.34.1
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
|
@ -55,8 +54,10 @@ RUN apt-get update && \
|
||||||
mkdir -p /staging/var/lib/crowdsec && \
|
mkdir -p /staging/var/lib/crowdsec && \
|
||||||
mkdir -p /var/lib/crowdsec/data
|
mkdir -p /var/lib/crowdsec/data
|
||||||
|
|
||||||
COPY --from=build /go/bin/yq /usr/local/bin/crowdsec /usr/local/bin/cscli /usr/local/bin/
|
COPY --from=build /go/bin/yq /usr/local/bin/yq
|
||||||
COPY --from=build /etc/crowdsec /staging/etc/crowdsec
|
COPY --from=build /etc/crowdsec /staging/etc/crowdsec
|
||||||
|
COPY --from=build /usr/local/bin/crowdsec /usr/local/bin/crowdsec
|
||||||
|
COPY --from=build /usr/local/bin/cscli /usr/local/bin/cscli
|
||||||
COPY --from=build /go/src/crowdsec/docker/docker_start.sh /
|
COPY --from=build /go/src/crowdsec/docker/docker_start.sh /
|
||||||
COPY --from=build /go/src/crowdsec/docker/config.yaml /staging/etc/crowdsec/config.yaml
|
COPY --from=build /go/src/crowdsec/docker/config.yaml /staging/etc/crowdsec/config.yaml
|
||||||
RUN yq -n '.url="http://0.0.0.0:8080"' | install -m 0600 /dev/stdin /staging/etc/crowdsec/local_api_credentials.yaml && \
|
RUN yq -n '.url="http://0.0.0.0:8080"' | install -m 0600 /dev/stdin /staging/etc/crowdsec/local_api_credentials.yaml && \
|
||||||
|
@ -68,14 +69,11 @@ FROM slim as plugins
|
||||||
|
|
||||||
# Due to the wizard using cp -n, we have to copy the config files directly from the source as -n does not exist in busybox cp
|
# Due to the wizard using cp -n, we have to copy the config files directly from the source as -n does not exist in busybox cp
|
||||||
# The files are here for reference, as users will need to mount a new version to be actually able to use notifications
|
# The files are here for reference, as users will need to mount a new version to be actually able to use notifications
|
||||||
COPY --from=build \
|
COPY --from=build /go/src/crowdsec/cmd/notification-email/email.yaml /staging/etc/crowdsec/notifications/email.yaml
|
||||||
/go/src/crowdsec/cmd/notification-email/email.yaml \
|
COPY --from=build /go/src/crowdsec/cmd/notification-http/http.yaml /staging/etc/crowdsec/notifications/http.yaml
|
||||||
/go/src/crowdsec/cmd/notification-http/http.yaml \
|
COPY --from=build /go/src/crowdsec/cmd/notification-slack/slack.yaml /staging/etc/crowdsec/notifications/slack.yaml
|
||||||
/go/src/crowdsec/cmd/notification-slack/slack.yaml \
|
COPY --from=build /go/src/crowdsec/cmd/notification-splunk/splunk.yaml /staging/etc/crowdsec/notifications/splunk.yaml
|
||||||
/go/src/crowdsec/cmd/notification-splunk/splunk.yaml \
|
COPY --from=build /go/src/crowdsec/cmd/notification-sentinel/sentinel.yaml /staging/etc/crowdsec/notifications/sentinel.yaml
|
||||||
/go/src/crowdsec/cmd/notification-sentinel/sentinel.yaml \
|
|
||||||
/staging/etc/crowdsec/notifications/
|
|
||||||
|
|
||||||
COPY --from=build /usr/local/lib/crowdsec/plugins /usr/local/lib/crowdsec/plugins
|
COPY --from=build /usr/local/lib/crowdsec/plugins /usr/local/lib/crowdsec/plugins
|
||||||
|
|
||||||
FROM slim as geoip
|
FROM slim as geoip
|
||||||
|
|
56
Makefile
56
Makefile
|
@ -128,10 +128,11 @@ endif
|
||||||
#--------------------------------------
|
#--------------------------------------
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
build: pre-build goversion crowdsec cscli plugins ## Build crowdsec, cscli and plugins
|
build: pre-build goversion crowdsec cscli plugins
|
||||||
|
|
||||||
|
# Sanity checks and build information
|
||||||
.PHONY: pre-build
|
.PHONY: pre-build
|
||||||
pre-build: ## Sanity checks and build information
|
pre-build:
|
||||||
$(info Building $(BUILD_VERSION) ($(BUILD_TAG)) $(BUILD_TYPE) for $(GOOS)/$(GOARCH))
|
$(info Building $(BUILD_VERSION) ($(BUILD_TAG)) $(BUILD_TYPE) for $(GOOS)/$(GOARCH))
|
||||||
|
|
||||||
ifneq (,$(RE2_FAIL))
|
ifneq (,$(RE2_FAIL))
|
||||||
|
@ -152,14 +153,14 @@ ifeq ($(call bool,$(TEST_COVERAGE)),1)
|
||||||
$(info Test coverage collection enabled)
|
$(info Test coverage collection enabled)
|
||||||
endif
|
endif
|
||||||
|
|
||||||
# intentional, empty line
|
|
||||||
$(info )
|
$(info )
|
||||||
|
|
||||||
|
|
||||||
.PHONY: all
|
.PHONY: all
|
||||||
all: clean test build ## Clean, test and build (requires localstack)
|
all: clean test build
|
||||||
|
|
||||||
.PHONY: plugins
|
.PHONY: plugins
|
||||||
plugins: ## Build notification plugins
|
plugins:
|
||||||
@$(foreach plugin,$(PLUGINS), \
|
@$(foreach plugin,$(PLUGINS), \
|
||||||
$(MAKE) -C $(PLUGINS_DIR_PREFIX)$(plugin) build $(MAKE_FLAGS); \
|
$(MAKE) -C $(PLUGINS_DIR_PREFIX)$(plugin) build $(MAKE_FLAGS); \
|
||||||
)
|
)
|
||||||
|
@ -183,7 +184,7 @@ clean-rpm:
|
||||||
@$(RM) -r rpm/SRPMS
|
@$(RM) -r rpm/SRPMS
|
||||||
|
|
||||||
.PHONY: clean
|
.PHONY: clean
|
||||||
clean: clean-debian clean-rpm testclean ## Remove build artifacts
|
clean: clean-debian clean-rpm testclean
|
||||||
@$(MAKE) -C $(CROWDSEC_FOLDER) clean $(MAKE_FLAGS)
|
@$(MAKE) -C $(CROWDSEC_FOLDER) clean $(MAKE_FLAGS)
|
||||||
@$(MAKE) -C $(CSCLI_FOLDER) clean $(MAKE_FLAGS)
|
@$(MAKE) -C $(CSCLI_FOLDER) clean $(MAKE_FLAGS)
|
||||||
@$(RM) $(CROWDSEC_BIN) $(WIN_IGNORE_ERR)
|
@$(RM) $(CROWDSEC_BIN) $(WIN_IGNORE_ERR)
|
||||||
|
@ -195,20 +196,21 @@ clean: clean-debian clean-rpm testclean ## Remove build artifacts
|
||||||
)
|
)
|
||||||
|
|
||||||
.PHONY: cscli
|
.PHONY: cscli
|
||||||
cscli: goversion ## Build cscli
|
cscli: goversion
|
||||||
@$(MAKE) -C $(CSCLI_FOLDER) build $(MAKE_FLAGS)
|
@$(MAKE) -C $(CSCLI_FOLDER) build $(MAKE_FLAGS)
|
||||||
|
|
||||||
.PHONY: crowdsec
|
.PHONY: crowdsec
|
||||||
crowdsec: goversion ## Build crowdsec
|
crowdsec: goversion
|
||||||
@$(MAKE) -C $(CROWDSEC_FOLDER) build $(MAKE_FLAGS)
|
@$(MAKE) -C $(CROWDSEC_FOLDER) build $(MAKE_FLAGS)
|
||||||
|
|
||||||
.PHONY: generate
|
.PHONY: notification-email
|
||||||
generate: ## Generate code for the database and APIs
|
notification-email: goversion
|
||||||
$(GO) generate ./pkg/database/ent
|
@$(MAKE) -C cmd/notification-email build $(MAKE_FLAGS)
|
||||||
$(GO) generate ./pkg/models
|
|
||||||
|
|
||||||
|
|
||||||
.PHONY: testclean
|
.PHONY: testclean
|
||||||
testclean: bats-clean ## Remove test artifacts
|
testclean: bats-clean
|
||||||
@$(RM) pkg/apiserver/ent $(WIN_IGNORE_ERR)
|
@$(RM) pkg/apiserver/ent $(WIN_IGNORE_ERR)
|
||||||
@$(RM) pkg/cwhub/hubdir $(WIN_IGNORE_ERR)
|
@$(RM) pkg/cwhub/hubdir $(WIN_IGNORE_ERR)
|
||||||
@$(RM) pkg/cwhub/install $(WIN_IGNORE_ERR)
|
@$(RM) pkg/cwhub/install $(WIN_IGNORE_ERR)
|
||||||
|
@ -216,39 +218,41 @@ testclean: bats-clean ## Remove test artifacts
|
||||||
|
|
||||||
# for the tests with localstack
|
# for the tests with localstack
|
||||||
export AWS_ENDPOINT_FORCE=http://localhost:4566
|
export AWS_ENDPOINT_FORCE=http://localhost:4566
|
||||||
export AWS_ACCESS_KEY_ID=test
|
export AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPLE
|
||||||
export AWS_SECRET_ACCESS_KEY=test
|
export AWS_SECRET_ACCESS_KEY=wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
|
||||||
|
|
||||||
testenv:
|
testenv:
|
||||||
@echo 'NOTE: You need Docker, docker-compose and run "make localstack" in a separate shell ("make localstack-stop" to terminate it)'
|
@echo 'NOTE: You need Docker, docker-compose and run "make localstack" in a separate shell ("make localstack-stop" to terminate it)'
|
||||||
|
|
||||||
|
# run the tests with localstack
|
||||||
.PHONY: test
|
.PHONY: test
|
||||||
test: testenv goversion ## Run unit tests with localstack
|
test: testenv goversion
|
||||||
$(GOTEST) $(LD_OPTS) ./...
|
$(GOTEST) $(LD_OPTS) ./...
|
||||||
|
|
||||||
|
# run the tests with localstack and coverage
|
||||||
.PHONY: go-acc
|
.PHONY: go-acc
|
||||||
go-acc: testenv goversion ## Run unit tests with localstack + coverage
|
go-acc: testenv goversion
|
||||||
go-acc ./... -o coverage.out --ignore database,notifications,protobufs,cwversion,cstest,models -- $(LD_OPTS)
|
go-acc ./... -o coverage.out --ignore database,notifications,protobufs,cwversion,cstest,models -- $(LD_OPTS)
|
||||||
|
|
||||||
# mock AWS services
|
# mock AWS services
|
||||||
.PHONY: localstack
|
.PHONY: localstack
|
||||||
localstack: ## Run localstack containers (required for unit testing)
|
localstack:
|
||||||
docker-compose -f test/localstack/docker-compose.yml up
|
docker-compose -f test/localstack/docker-compose.yml up
|
||||||
|
|
||||||
.PHONY: localstack-stop
|
.PHONY: localstack-stop
|
||||||
localstack-stop: ## Stop localstack containers
|
localstack-stop:
|
||||||
docker-compose -f test/localstack/docker-compose.yml down
|
docker-compose -f test/localstack/docker-compose.yml down
|
||||||
|
|
||||||
# build vendor.tgz to be distributed with the release
|
# build vendor.tgz to be distributed with the release
|
||||||
.PHONY: vendor
|
.PHONY: vendor
|
||||||
vendor: vendor-remove ## CI only - vendor dependencies and archive them for packaging
|
vendor: vendor-remove
|
||||||
$(GO) mod vendor
|
$(GO) mod vendor
|
||||||
tar czf vendor.tgz vendor
|
tar czf vendor.tgz vendor
|
||||||
tar --create --auto-compress --file=$(RELDIR)-vendor.tar.xz vendor
|
tar --create --auto-compress --file=$(RELDIR)-vendor.tar.xz vendor
|
||||||
|
|
||||||
# remove vendor directories and vendor.tgz
|
# remove vendor directories and vendor.tgz
|
||||||
.PHONY: vendor-remove
|
.PHONY: vendor-remove
|
||||||
vendor-remove: ## Remove vendor dependencies and archives
|
vendor-remove:
|
||||||
$(RM) vendor vendor.tgz *-vendor.tar.xz
|
$(RM) vendor vendor.tgz *-vendor.tar.xz
|
||||||
|
|
||||||
.PHONY: package
|
.PHONY: package
|
||||||
|
@ -280,15 +284,18 @@ else
|
||||||
@if (Test-Path -Path $(RELDIR)) { echo "$(RELDIR) already exists, abort" ; exit 1 ; }
|
@if (Test-Path -Path $(RELDIR)) { echo "$(RELDIR) already exists, abort" ; exit 1 ; }
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
# build a release tarball
|
||||||
.PHONY: release
|
.PHONY: release
|
||||||
release: check_release build package ## Build a release tarball
|
release: check_release build package
|
||||||
|
|
||||||
|
# build the windows installer
|
||||||
.PHONY: windows_installer
|
.PHONY: windows_installer
|
||||||
windows_installer: build ## Windows - build the installer
|
windows_installer: build
|
||||||
@.\make_installer.ps1 -version $(BUILD_VERSION)
|
@.\make_installer.ps1 -version $(BUILD_VERSION)
|
||||||
|
|
||||||
|
# build the chocolatey package
|
||||||
.PHONY: chocolatey
|
.PHONY: chocolatey
|
||||||
chocolatey: windows_installer ## Windows - build the chocolatey package
|
chocolatey: windows_installer
|
||||||
@.\make_chocolatey.ps1 -version $(BUILD_VERSION)
|
@.\make_chocolatey.ps1 -version $(BUILD_VERSION)
|
||||||
|
|
||||||
# Include test/bats.mk only if it exists
|
# Include test/bats.mk only if it exists
|
||||||
|
@ -301,4 +308,3 @@ include test/bats.mk
|
||||||
endif
|
endif
|
||||||
|
|
||||||
include mk/goversion.mk
|
include mk/goversion.mk
|
||||||
include mk/help.mk
|
|
||||||
|
|
|
@ -15,13 +15,19 @@ pool:
|
||||||
stages:
|
stages:
|
||||||
- stage: Build
|
- stage: Build
|
||||||
jobs:
|
jobs:
|
||||||
- job: Build
|
- job:
|
||||||
displayName: "Build"
|
displayName: "Build"
|
||||||
steps:
|
steps:
|
||||||
- task: GoTool@0
|
- task: DotNetCoreCLI@2
|
||||||
displayName: "Install Go"
|
displayName: "Install SignClient"
|
||||||
inputs:
|
inputs:
|
||||||
version: '1.22.2'
|
command: 'custom'
|
||||||
|
custom: 'tool'
|
||||||
|
arguments: 'install --global SignClient --version 1.3.155'
|
||||||
|
- task: GoTool@0
|
||||||
|
displayName: "Install Go 1.20"
|
||||||
|
inputs:
|
||||||
|
version: '1.21.4'
|
||||||
|
|
||||||
- pwsh: |
|
- pwsh: |
|
||||||
choco install -y make
|
choco install -y make
|
||||||
|
@ -33,14 +39,24 @@ stages:
|
||||||
#we are not calling make windows_installer because we want to sign the binaries before they are added to the MSI
|
#we are not calling make windows_installer because we want to sign the binaries before they are added to the MSI
|
||||||
script: |
|
script: |
|
||||||
make build BUILD_RE2_WASM=1
|
make build BUILD_RE2_WASM=1
|
||||||
|
- task: AzureKeyVault@2
|
||||||
|
inputs:
|
||||||
|
azureSubscription: 'Azure subscription 1(8a93ab40-7e99-445e-ad47-0f6a3e2ef546)'
|
||||||
|
KeyVaultName: 'CodeSigningSecrets'
|
||||||
|
SecretsFilter: 'CodeSigningUser,CodeSigningPassword'
|
||||||
|
RunAsPreJob: false
|
||||||
|
|
||||||
|
- task: DownloadSecureFile@1
|
||||||
|
inputs:
|
||||||
|
secureFile: appsettings.json
|
||||||
|
|
||||||
|
- pwsh: |
|
||||||
|
SignClient.exe Sign --name "crowdsec-binaries" `
|
||||||
|
--input "**/*.exe" --config (Join-Path -Path $(Agent.TempDirectory) -ChildPath "appsettings.json") `
|
||||||
|
--user $(CodeSigningUser) --secret '$(CodeSigningPassword)'
|
||||||
|
displayName: "Sign Crowdsec binaries + plugins"
|
||||||
- pwsh: |
|
- pwsh: |
|
||||||
$build_version=$env:BUILD_SOURCEBRANCHNAME
|
$build_version=$env:BUILD_SOURCEBRANCHNAME
|
||||||
#Override the version if it's set in the pipeline
|
|
||||||
if ( ${env:USERBUILDVERSION} -ne "")
|
|
||||||
{
|
|
||||||
$build_version = ${env:USERBUILDVERSION}
|
|
||||||
}
|
|
||||||
if ($build_version.StartsWith("v"))
|
if ($build_version.StartsWith("v"))
|
||||||
{
|
{
|
||||||
$build_version = $build_version.Substring(1)
|
$build_version = $build_version.Substring(1)
|
||||||
|
@ -53,112 +69,35 @@ stages:
|
||||||
displayName: GetCrowdsecVersion
|
displayName: GetCrowdsecVersion
|
||||||
name: GetCrowdsecVersion
|
name: GetCrowdsecVersion
|
||||||
- pwsh: |
|
- pwsh: |
|
||||||
Get-ChildItem -Path .\cmd -Directory | ForEach-Object {
|
.\make_installer.ps1 -version '$(GetCrowdsecVersion.BuildVersion)'
|
||||||
$dirName = $_.Name
|
|
||||||
Get-ChildItem -Path .\cmd\$dirName -File -Filter '*.exe' | ForEach-Object {
|
|
||||||
$fileName = $_.Name
|
|
||||||
$destDir = Join-Path $(Build.ArtifactStagingDirectory) cmd\$dirName
|
|
||||||
New-Item -ItemType Directory -Path $destDir -Force
|
|
||||||
Copy-Item -Path .\cmd\$dirName\$fileName -Destination $destDir
|
|
||||||
}
|
|
||||||
}
|
|
||||||
displayName: "Copy binaries to staging directory"
|
|
||||||
- task: PublishPipelineArtifact@1
|
|
||||||
inputs:
|
|
||||||
targetPath: '$(Build.ArtifactStagingDirectory)'
|
|
||||||
artifact: 'unsigned_binaries'
|
|
||||||
displayName: "Upload binaries artifact"
|
|
||||||
|
|
||||||
- stage: Sign
|
|
||||||
dependsOn: Build
|
|
||||||
variables:
|
|
||||||
- group: 'FOSS Build Variables'
|
|
||||||
- name: BuildVersion
|
|
||||||
value: $[ stageDependencies.Build.Build.outputs['GetCrowdsecVersion.BuildVersion'] ]
|
|
||||||
condition: succeeded()
|
|
||||||
jobs:
|
|
||||||
- job: Sign
|
|
||||||
displayName: "Sign"
|
|
||||||
steps:
|
|
||||||
- download: current
|
|
||||||
artifact: unsigned_binaries
|
|
||||||
displayName: "Download binaries artifact"
|
|
||||||
- task: CopyFiles@2
|
|
||||||
inputs:
|
|
||||||
SourceFolder: '$(Pipeline.Workspace)/unsigned_binaries'
|
|
||||||
TargetFolder: '$(Build.SourcesDirectory)'
|
|
||||||
displayName: "Copy binaries to workspace"
|
|
||||||
- task: DotNetCoreCLI@2
|
|
||||||
displayName: "Install SignTool tool"
|
|
||||||
inputs:
|
|
||||||
command: 'custom'
|
|
||||||
custom: 'tool'
|
|
||||||
arguments: install --global sign --version 0.9.0-beta.23127.3
|
|
||||||
- task: AzureKeyVault@2
|
|
||||||
displayName: "Get signing parameters"
|
|
||||||
inputs:
|
|
||||||
azureSubscription: "Azure subscription"
|
|
||||||
KeyVaultName: "$(KeyVaultName)"
|
|
||||||
SecretsFilter: "TenantId,ClientId,ClientSecret,Certificate,KeyVaultUrl"
|
|
||||||
- pwsh: |
|
|
||||||
sign code azure-key-vault `
|
|
||||||
"**/*.exe" `
|
|
||||||
--base-directory "$(Build.SourcesDirectory)/cmd/" `
|
|
||||||
--publisher-name "CrowdSec" `
|
|
||||||
--description "CrowdSec" `
|
|
||||||
--description-url "https://github.com/crowdsecurity/crowdsec" `
|
|
||||||
--azure-key-vault-tenant-id "$(TenantId)" `
|
|
||||||
--azure-key-vault-client-id "$(ClientId)" `
|
|
||||||
--azure-key-vault-client-secret "$(ClientSecret)" `
|
|
||||||
--azure-key-vault-certificate "$(Certificate)" `
|
|
||||||
--azure-key-vault-url "$(KeyVaultUrl)"
|
|
||||||
displayName: "Sign crowdsec binaries"
|
|
||||||
- pwsh: |
|
|
||||||
.\make_installer.ps1 -version '$(BuildVersion)'
|
|
||||||
displayName: "Build Crowdsec MSI"
|
displayName: "Build Crowdsec MSI"
|
||||||
name: BuildMSI
|
name: BuildMSI
|
||||||
- pwsh: |
|
|
||||||
.\make_chocolatey.ps1 -version '$(BuildVersion)'
|
|
||||||
displayName: "Build Chocolatey nupkg"
|
|
||||||
- pwsh: |
|
|
||||||
sign code azure-key-vault `
|
|
||||||
"*.msi" `
|
|
||||||
--base-directory "$(Build.SourcesDirectory)" `
|
|
||||||
--publisher-name "CrowdSec" `
|
|
||||||
--description "CrowdSec" `
|
|
||||||
--description-url "https://github.com/crowdsecurity/crowdsec" `
|
|
||||||
--azure-key-vault-tenant-id "$(TenantId)" `
|
|
||||||
--azure-key-vault-client-id "$(ClientId)" `
|
|
||||||
--azure-key-vault-client-secret "$(ClientSecret)" `
|
|
||||||
--azure-key-vault-certificate "$(Certificate)" `
|
|
||||||
--azure-key-vault-url "$(KeyVaultUrl)"
|
|
||||||
displayName: "Sign MSI package"
|
|
||||||
- pwsh: |
|
|
||||||
sign code azure-key-vault `
|
|
||||||
"*.nupkg" `
|
|
||||||
--base-directory "$(Build.SourcesDirectory)" `
|
|
||||||
--publisher-name "CrowdSec" `
|
|
||||||
--description "CrowdSec" `
|
|
||||||
--description-url "https://github.com/crowdsecurity/crowdsec" `
|
|
||||||
--azure-key-vault-tenant-id "$(TenantId)" `
|
|
||||||
--azure-key-vault-client-id "$(ClientId)" `
|
|
||||||
--azure-key-vault-client-secret "$(ClientSecret)" `
|
|
||||||
--azure-key-vault-certificate "$(Certificate)" `
|
|
||||||
--azure-key-vault-url "$(KeyVaultUrl)"
|
|
||||||
displayName: "Sign nuget package"
|
|
||||||
- task: PublishPipelineArtifact@1
|
|
||||||
inputs:
|
|
||||||
targetPath: '$(Build.SourcesDirectory)/crowdsec_$(BuildVersion).msi'
|
|
||||||
artifact: 'signed_msi_package'
|
|
||||||
displayName: "Upload signed MSI artifact"
|
|
||||||
- task: PublishPipelineArtifact@1
|
|
||||||
inputs:
|
|
||||||
targetPath: '$(Build.SourcesDirectory)/crowdsec.$(BuildVersion).nupkg'
|
|
||||||
artifact: 'signed_nuget_package'
|
|
||||||
displayName: "Upload signed nuget artifact"
|
|
||||||
|
|
||||||
|
- pwsh: |
|
||||||
|
.\make_chocolatey.ps1 -version '$(GetCrowdsecVersion.BuildVersion)'
|
||||||
|
displayName: "Build Chocolatey nupkg"
|
||||||
|
|
||||||
|
- pwsh: |
|
||||||
|
SignClient.exe Sign --name "crowdsec-msi" `
|
||||||
|
--input "*.msi" --config (Join-Path -Path $(Agent.TempDirectory) -ChildPath "appsettings.json") `
|
||||||
|
--user $(CodeSigningUser) --secret '$(CodeSigningPassword)'
|
||||||
|
displayName: "Sign Crowdsec MSI"
|
||||||
|
|
||||||
|
- task: PublishBuildArtifacts@1
|
||||||
|
inputs:
|
||||||
|
PathtoPublish: '$(Build.Repository.LocalPath)\\crowdsec_$(GetCrowdsecVersion.BuildVersion).msi'
|
||||||
|
ArtifactName: 'crowdsec.msi'
|
||||||
|
publishLocation: 'Container'
|
||||||
|
displayName: "Upload MSI artifact"
|
||||||
|
|
||||||
|
- task: PublishBuildArtifacts@1
|
||||||
|
inputs:
|
||||||
|
PathtoPublish: '$(Build.Repository.LocalPath)\\windows\\Chocolatey\\crowdsec\\crowdsec.$(GetCrowdsecVersion.BuildVersion).nupkg'
|
||||||
|
ArtifactName: 'crowdsec.nupkg'
|
||||||
|
publishLocation: 'Container'
|
||||||
|
displayName: "Upload nupkg artifact"
|
||||||
- stage: Publish
|
- stage: Publish
|
||||||
dependsOn: Sign
|
dependsOn: Build
|
||||||
jobs:
|
jobs:
|
||||||
- deployment: "Publish"
|
- deployment: "Publish"
|
||||||
displayName: "Publish to GitHub"
|
displayName: "Publish to GitHub"
|
||||||
|
@ -180,7 +119,8 @@ stages:
|
||||||
assetUploadMode: 'replace'
|
assetUploadMode: 'replace'
|
||||||
addChangeLog: false
|
addChangeLog: false
|
||||||
isPreRelease: true #we force prerelease because the pipeline is invoked on tag creation, which happens when we do a prerelease
|
isPreRelease: true #we force prerelease because the pipeline is invoked on tag creation, which happens when we do a prerelease
|
||||||
|
#the .. is an ugly hack, but I can't find the var that gives D:\a\1 ...
|
||||||
assets: |
|
assets: |
|
||||||
$(Pipeline.Workspace)/signed_msi_package/*.msi
|
$(Build.ArtifactStagingDirectory)\..\crowdsec.msi/*.msi
|
||||||
$(Pipeline.Workspace)/signed_nuget_package/*.nupkg
|
$(Build.ArtifactStagingDirectory)\..\crowdsec.nupkg/*.nupkg
|
||||||
condition: ne(variables['GetLatestPrelease.LatestPreRelease'], '')
|
condition: ne(variables['GetLatestPrelease.LatestPreRelease'], '')
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -12,64 +11,103 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/go-openapi/strfmt"
|
"github.com/go-openapi/strfmt"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/version"
|
"github.com/crowdsecurity/go-cs-lib/version"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func DecisionsFromAlert(alert *models.Alert) string {
|
func DecisionsFromAlert(alert *models.Alert) string {
|
||||||
ret := ""
|
ret := ""
|
||||||
decMap := make(map[string]int)
|
var decMap = make(map[string]int)
|
||||||
|
|
||||||
for _, decision := range alert.Decisions {
|
for _, decision := range alert.Decisions {
|
||||||
k := *decision.Type
|
k := *decision.Type
|
||||||
if *decision.Simulated {
|
if *decision.Simulated {
|
||||||
k = fmt.Sprintf("(simul)%s", k)
|
k = fmt.Sprintf("(simul)%s", k)
|
||||||
}
|
}
|
||||||
|
|
||||||
v := decMap[k]
|
v := decMap[k]
|
||||||
decMap[k] = v + 1
|
decMap[k] = v + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range decMap {
|
for k, v := range decMap {
|
||||||
if len(ret) > 0 {
|
if len(ret) > 0 {
|
||||||
ret += " "
|
ret += " "
|
||||||
}
|
}
|
||||||
|
|
||||||
ret += fmt.Sprintf("%s:%d", k, v)
|
ret += fmt.Sprintf("%s:%d", k, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliAlerts) alertsToTable(alerts *models.GetAlertsResponse, printMachine bool) error {
|
func DateFromAlert(alert *models.Alert) string {
|
||||||
switch cli.cfg().Cscli.Output {
|
ts, err := time.Parse(time.RFC3339, alert.CreatedAt)
|
||||||
case "raw":
|
if err != nil {
|
||||||
|
log.Infof("while parsing %s with %s : %s", alert.CreatedAt, time.RFC3339, err)
|
||||||
|
return alert.CreatedAt
|
||||||
|
}
|
||||||
|
return ts.Format(time.RFC822)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SourceFromAlert(alert *models.Alert) string {
|
||||||
|
|
||||||
|
//more than one item, just number and scope
|
||||||
|
if len(alert.Decisions) > 1 {
|
||||||
|
return fmt.Sprintf("%d %ss (%s)", len(alert.Decisions), *alert.Decisions[0].Scope, *alert.Decisions[0].Origin)
|
||||||
|
}
|
||||||
|
|
||||||
|
//fallback on single decision information
|
||||||
|
if len(alert.Decisions) == 1 {
|
||||||
|
return fmt.Sprintf("%s:%s", *alert.Decisions[0].Scope, *alert.Decisions[0].Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
//try to compose a human friendly version
|
||||||
|
if *alert.Source.Value != "" && *alert.Source.Scope != "" {
|
||||||
|
scope := ""
|
||||||
|
scope = fmt.Sprintf("%s:%s", *alert.Source.Scope, *alert.Source.Value)
|
||||||
|
extra := ""
|
||||||
|
if alert.Source.Cn != "" {
|
||||||
|
extra = alert.Source.Cn
|
||||||
|
}
|
||||||
|
if alert.Source.AsNumber != "" {
|
||||||
|
extra += fmt.Sprintf("/%s", alert.Source.AsNumber)
|
||||||
|
}
|
||||||
|
if alert.Source.AsName != "" {
|
||||||
|
extra += fmt.Sprintf("/%s", alert.Source.AsName)
|
||||||
|
}
|
||||||
|
|
||||||
|
if extra != "" {
|
||||||
|
scope += " (" + extra + ")"
|
||||||
|
}
|
||||||
|
return scope
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func AlertsToTable(alerts *models.GetAlertsResponse, printMachine bool) error {
|
||||||
|
|
||||||
|
if csConfig.Cscli.Output == "raw" {
|
||||||
csvwriter := csv.NewWriter(os.Stdout)
|
csvwriter := csv.NewWriter(os.Stdout)
|
||||||
header := []string{"id", "scope", "value", "reason", "country", "as", "decisions", "created_at"}
|
header := []string{"id", "scope", "value", "reason", "country", "as", "decisions", "created_at"}
|
||||||
|
|
||||||
if printMachine {
|
if printMachine {
|
||||||
header = append(header, "machine")
|
header = append(header, "machine")
|
||||||
}
|
}
|
||||||
|
err := csvwriter.Write(header)
|
||||||
if err := csvwriter.Write(header); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, alertItem := range *alerts {
|
for _, alertItem := range *alerts {
|
||||||
row := []string{
|
row := []string{
|
||||||
strconv.FormatInt(alertItem.ID, 10),
|
fmt.Sprintf("%d", alertItem.ID),
|
||||||
*alertItem.Source.Scope,
|
*alertItem.Source.Scope,
|
||||||
*alertItem.Source.Value,
|
*alertItem.Source.Value,
|
||||||
*alertItem.Scenario,
|
*alertItem.Scenario,
|
||||||
|
@ -81,32 +119,28 @@ func (cli *cliAlerts) alertsToTable(alerts *models.GetAlertsResponse, printMachi
|
||||||
if printMachine {
|
if printMachine {
|
||||||
row = append(row, alertItem.MachineID)
|
row = append(row, alertItem.MachineID)
|
||||||
}
|
}
|
||||||
|
err := csvwriter.Write(row)
|
||||||
if err := csvwriter.Write(row); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
csvwriter.Flush()
|
csvwriter.Flush()
|
||||||
case "json":
|
} else if csConfig.Cscli.Output == "json" {
|
||||||
if *alerts == nil {
|
if *alerts == nil {
|
||||||
// avoid returning "null" in json
|
// avoid returning "null" in json
|
||||||
// could be cleaner if we used slice of alerts directly
|
// could be cleaner if we used slice of alerts directly
|
||||||
fmt.Println("[]")
|
fmt.Println("[]")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
x, _ := json.MarshalIndent(alerts, "", " ")
|
x, _ := json.MarshalIndent(alerts, "", " ")
|
||||||
fmt.Print(string(x))
|
fmt.Printf("%s", string(x))
|
||||||
case "human":
|
} else if csConfig.Cscli.Output == "human" {
|
||||||
if len(*alerts) == 0 {
|
if len(*alerts) == 0 {
|
||||||
fmt.Println("No active alerts")
|
fmt.Println("No active alerts")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
alertsTable(color.Output, alerts, printMachine)
|
alertsTable(color.Output, alerts, printMachine)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,13 +162,14 @@ var alertTemplate = `
|
||||||
|
|
||||||
`
|
`
|
||||||
|
|
||||||
func (cli *cliAlerts) displayOneAlert(alert *models.Alert, withDetail bool) error {
|
func DisplayOneAlert(alert *models.Alert, withDetail bool) error {
|
||||||
|
if csConfig.Cscli.Output == "human" {
|
||||||
tmpl, err := template.New("alert").Parse(alertTemplate)
|
tmpl, err := template.New("alert").Parse(alertTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
err = tmpl.Execute(os.Stdout, alert)
|
||||||
if err = tmpl.Execute(os.Stdout, alert); err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -145,17 +180,14 @@ func (cli *cliAlerts) displayOneAlert(alert *models.Alert, withDetail bool) erro
|
||||||
sort.Slice(alert.Meta, func(i, j int) bool {
|
sort.Slice(alert.Meta, func(i, j int) bool {
|
||||||
return alert.Meta[i].Key < alert.Meta[j].Key
|
return alert.Meta[i].Key < alert.Meta[j].Key
|
||||||
})
|
})
|
||||||
|
|
||||||
table := newTable(color.Output)
|
table := newTable(color.Output)
|
||||||
table.SetRowLines(false)
|
table.SetRowLines(false)
|
||||||
table.SetHeaders("Key", "Value")
|
table.SetHeaders("Key", "Value")
|
||||||
|
|
||||||
for _, meta := range alert.Meta {
|
for _, meta := range alert.Meta {
|
||||||
var valSlice []string
|
var valSlice []string
|
||||||
if err := json.Unmarshal([]byte(meta.Value), &valSlice); err != nil {
|
if err := json.Unmarshal([]byte(meta.Value), &valSlice); err != nil {
|
||||||
return fmt.Errorf("unknown context value type '%s': %w", meta.Value, err)
|
return fmt.Errorf("unknown context value type '%s' : %s", meta.Value, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, value := range valSlice {
|
for _, value := range valSlice {
|
||||||
table.AddRow(
|
table.AddRow(
|
||||||
meta.Key,
|
meta.Key,
|
||||||
|
@ -163,74 +195,60 @@ func (cli *cliAlerts) displayOneAlert(alert *models.Alert, withDetail bool) erro
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
table.Render()
|
table.Render()
|
||||||
}
|
}
|
||||||
|
|
||||||
if withDetail {
|
if withDetail {
|
||||||
fmt.Printf("\n - Events :\n")
|
fmt.Printf("\n - Events :\n")
|
||||||
|
|
||||||
for _, event := range alert.Events {
|
for _, event := range alert.Events {
|
||||||
alertEventTable(color.Output, event)
|
alertEventTable(color.Output, event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type cliAlerts struct {
|
func NewAlertsCmd() *cobra.Command {
|
||||||
client *apiclient.ApiClient
|
var cmdAlerts = &cobra.Command{
|
||||||
cfg configGetter
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLIAlerts(getconfig configGetter) *cliAlerts {
|
|
||||||
return &cliAlerts{
|
|
||||||
cfg: getconfig,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliAlerts) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "alerts [action]",
|
Use: "alerts [action]",
|
||||||
Short: "Manage alerts",
|
Short: "Manage alerts",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
Aliases: []string{"alert"},
|
Aliases: []string{"alert"},
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
var err error
|
||||||
if err := cfg.LoadAPIClient(); err != nil {
|
if err := csConfig.LoadAPIClient(); err != nil {
|
||||||
return fmt.Errorf("loading api client: %w", err)
|
return fmt.Errorf("loading api client: %w", err)
|
||||||
}
|
}
|
||||||
apiURL, err := url.Parse(cfg.API.Client.Credentials.URL)
|
apiURL, err := url.Parse(csConfig.API.Client.Credentials.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parsing api url %s: %w", apiURL, err)
|
return fmt.Errorf("parsing api url %s: %w", apiURL, err)
|
||||||
}
|
}
|
||||||
|
Client, err = apiclient.NewClient(&apiclient.Config{
|
||||||
cli.client, err = apiclient.NewClient(&apiclient.Config{
|
MachineID: csConfig.API.Client.Credentials.Login,
|
||||||
MachineID: cfg.API.Client.Credentials.Login,
|
Password: strfmt.Password(csConfig.API.Client.Credentials.Password),
|
||||||
Password: strfmt.Password(cfg.API.Client.Credentials.Password),
|
|
||||||
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
||||||
URL: apiURL,
|
URL: apiURL,
|
||||||
VersionPrefix: "v1",
|
VersionPrefix: "v1",
|
||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("new api client: %w", err)
|
return fmt.Errorf("new api client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.NewListCmd())
|
cmdAlerts.AddCommand(NewAlertsListCmd())
|
||||||
cmd.AddCommand(cli.NewInspectCmd())
|
cmdAlerts.AddCommand(NewAlertsInspectCmd())
|
||||||
cmd.AddCommand(cli.NewFlushCmd())
|
cmdAlerts.AddCommand(NewAlertsFlushCmd())
|
||||||
cmd.AddCommand(cli.NewDeleteCmd())
|
cmdAlerts.AddCommand(NewAlertsDeleteCmd())
|
||||||
|
|
||||||
return cmd
|
return cmdAlerts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliAlerts) NewListCmd() *cobra.Command {
|
func NewAlertsListCmd() *cobra.Command {
|
||||||
alertListFilter := apiclient.AlertsListOpts{
|
var alertListFilter = apiclient.AlertsListOpts{
|
||||||
ScopeEquals: new(string),
|
ScopeEquals: new(string),
|
||||||
ValueEquals: new(string),
|
ValueEquals: new(string),
|
||||||
ScenarioEquals: new(string),
|
ScenarioEquals: new(string),
|
||||||
|
@ -242,24 +260,21 @@ func (cli *cliAlerts) NewListCmd() *cobra.Command {
|
||||||
IncludeCAPI: new(bool),
|
IncludeCAPI: new(bool),
|
||||||
OriginEquals: new(string),
|
OriginEquals: new(string),
|
||||||
}
|
}
|
||||||
|
var limit = new(int)
|
||||||
limit := new(int)
|
|
||||||
contained := new(bool)
|
contained := new(bool)
|
||||||
|
|
||||||
var printMachine bool
|
var printMachine bool
|
||||||
|
var cmdAlertsList = &cobra.Command{
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "list [filters]",
|
Use: "list [filters]",
|
||||||
Short: "List alerts",
|
Short: "List alerts",
|
||||||
Example: `cscli alerts list
|
Example: `cscli alerts list
|
||||||
cscli alerts list --ip 1.2.3.4
|
cscli alerts list --ip 1.2.3.4
|
||||||
cscli alerts list --range 1.2.3.0/24
|
cscli alerts list --range 1.2.3.0/24
|
||||||
cscli alerts list --origin lists
|
|
||||||
cscli alerts list -s crowdsecurity/ssh-bf
|
cscli alerts list -s crowdsecurity/ssh-bf
|
||||||
cscli alerts list --type ban`,
|
cscli alerts list --type ban`,
|
||||||
Long: `List alerts with optional filters`,
|
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
if err := manageCliDecisionAlerts(alertListFilter.IPEquals, alertListFilter.RangeEquals,
|
if err := manageCliDecisionAlerts(alertListFilter.IPEquals, alertListFilter.RangeEquals,
|
||||||
alertListFilter.ScopeEquals, alertListFilter.ValueEquals); err != nil {
|
alertListFilter.ScopeEquals, alertListFilter.ValueEquals); err != nil {
|
||||||
printHelp(cmd)
|
printHelp(cmd)
|
||||||
|
@ -325,56 +340,50 @@ cscli alerts list --type ban`,
|
||||||
alertListFilter.Contains = new(bool)
|
alertListFilter.Contains = new(bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
alerts, _, err := cli.client.Alerts.List(context.Background(), alertListFilter)
|
alerts, _, err := Client.Alerts.List(context.Background(), alertListFilter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to list alerts: %w", err)
|
return fmt.Errorf("unable to list alerts: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = cli.alertsToTable(alerts, printMachine); err != nil {
|
err = AlertsToTable(alerts, printMachine)
|
||||||
return fmt.Errorf("unable to list alerts: %w", err)
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to list alerts: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmdAlertsList.Flags().SortFlags = false
|
||||||
|
cmdAlertsList.Flags().BoolVarP(alertListFilter.IncludeCAPI, "all", "a", false, "Include decisions from Central API")
|
||||||
|
cmdAlertsList.Flags().StringVar(alertListFilter.Until, "until", "", "restrict to alerts older than until (ie. 4h, 30d)")
|
||||||
|
cmdAlertsList.Flags().StringVar(alertListFilter.Since, "since", "", "restrict to alerts newer than since (ie. 4h, 30d)")
|
||||||
|
cmdAlertsList.Flags().StringVarP(alertListFilter.IPEquals, "ip", "i", "", "restrict to alerts from this source ip (shorthand for --scope ip --value <IP>)")
|
||||||
|
cmdAlertsList.Flags().StringVarP(alertListFilter.ScenarioEquals, "scenario", "s", "", "the scenario (ie. crowdsecurity/ssh-bf)")
|
||||||
|
cmdAlertsList.Flags().StringVarP(alertListFilter.RangeEquals, "range", "r", "", "restrict to alerts from this range (shorthand for --scope range --value <RANGE/X>)")
|
||||||
|
cmdAlertsList.Flags().StringVar(alertListFilter.TypeEquals, "type", "", "restrict to alerts with given decision type (ie. ban, captcha)")
|
||||||
|
cmdAlertsList.Flags().StringVar(alertListFilter.ScopeEquals, "scope", "", "restrict to alerts of this scope (ie. ip,range)")
|
||||||
|
cmdAlertsList.Flags().StringVarP(alertListFilter.ValueEquals, "value", "v", "", "the value to match for in the specified scope")
|
||||||
|
cmdAlertsList.Flags().StringVar(alertListFilter.OriginEquals, "origin", "", fmt.Sprintf("the value to match for the specified origin (%s ...)", strings.Join(types.GetOrigins(), ",")))
|
||||||
|
cmdAlertsList.Flags().BoolVar(contained, "contained", false, "query decisions contained by range")
|
||||||
|
cmdAlertsList.Flags().BoolVarP(&printMachine, "machine", "m", false, "print machines that sent alerts")
|
||||||
|
cmdAlertsList.Flags().IntVarP(limit, "limit", "l", 50, "limit size of alerts list table (0 to view all alerts)")
|
||||||
|
|
||||||
flags := cmd.Flags()
|
return cmdAlertsList
|
||||||
flags.SortFlags = false
|
|
||||||
flags.BoolVarP(alertListFilter.IncludeCAPI, "all", "a", false, "Include decisions from Central API")
|
|
||||||
flags.StringVar(alertListFilter.Until, "until", "", "restrict to alerts older than until (ie. 4h, 30d)")
|
|
||||||
flags.StringVar(alertListFilter.Since, "since", "", "restrict to alerts newer than since (ie. 4h, 30d)")
|
|
||||||
flags.StringVarP(alertListFilter.IPEquals, "ip", "i", "", "restrict to alerts from this source ip (shorthand for --scope ip --value <IP>)")
|
|
||||||
flags.StringVarP(alertListFilter.ScenarioEquals, "scenario", "s", "", "the scenario (ie. crowdsecurity/ssh-bf)")
|
|
||||||
flags.StringVarP(alertListFilter.RangeEquals, "range", "r", "", "restrict to alerts from this range (shorthand for --scope range --value <RANGE/X>)")
|
|
||||||
flags.StringVar(alertListFilter.TypeEquals, "type", "", "restrict to alerts with given decision type (ie. ban, captcha)")
|
|
||||||
flags.StringVar(alertListFilter.ScopeEquals, "scope", "", "restrict to alerts of this scope (ie. ip,range)")
|
|
||||||
flags.StringVarP(alertListFilter.ValueEquals, "value", "v", "", "the value to match for in the specified scope")
|
|
||||||
flags.StringVar(alertListFilter.OriginEquals, "origin", "", fmt.Sprintf("the value to match for the specified origin (%s ...)", strings.Join(types.GetOrigins(), ",")))
|
|
||||||
flags.BoolVar(contained, "contained", false, "query decisions contained by range")
|
|
||||||
flags.BoolVarP(&printMachine, "machine", "m", false, "print machines that sent alerts")
|
|
||||||
flags.IntVarP(limit, "limit", "l", 50, "limit size of alerts list table (0 to view all alerts)")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliAlerts) NewDeleteCmd() *cobra.Command {
|
func NewAlertsDeleteCmd() *cobra.Command {
|
||||||
var (
|
var ActiveDecision *bool
|
||||||
ActiveDecision *bool
|
var AlertDeleteAll bool
|
||||||
AlertDeleteAll bool
|
var delAlertByID string
|
||||||
delAlertByID string
|
contained := new(bool)
|
||||||
)
|
var alertDeleteFilter = apiclient.AlertsDeleteOpts{
|
||||||
|
|
||||||
alertDeleteFilter := apiclient.AlertsDeleteOpts{
|
|
||||||
ScopeEquals: new(string),
|
ScopeEquals: new(string),
|
||||||
ValueEquals: new(string),
|
ValueEquals: new(string),
|
||||||
ScenarioEquals: new(string),
|
ScenarioEquals: new(string),
|
||||||
IPEquals: new(string),
|
IPEquals: new(string),
|
||||||
RangeEquals: new(string),
|
RangeEquals: new(string),
|
||||||
}
|
}
|
||||||
|
var cmdAlertsDelete = &cobra.Command{
|
||||||
contained := new(bool)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "delete [filters] [--all]",
|
Use: "delete [filters] [--all]",
|
||||||
Short: `Delete alerts
|
Short: `Delete alerts
|
||||||
/!\ This command can be use only on the same machine than the local API.`,
|
/!\ This command can be use only on the same machine than the local API.`,
|
||||||
|
@ -384,7 +393,7 @@ cscli alerts delete -s crowdsecurity/ssh-bf"`,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
Aliases: []string{"remove"},
|
Aliases: []string{"remove"},
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
PreRunE: func(cmd *cobra.Command, _ []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if AlertDeleteAll {
|
if AlertDeleteAll {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -392,16 +401,16 @@ cscli alerts delete -s crowdsecurity/ssh-bf"`,
|
||||||
*alertDeleteFilter.ScenarioEquals == "" && *alertDeleteFilter.IPEquals == "" &&
|
*alertDeleteFilter.ScenarioEquals == "" && *alertDeleteFilter.IPEquals == "" &&
|
||||||
*alertDeleteFilter.RangeEquals == "" && delAlertByID == "" {
|
*alertDeleteFilter.RangeEquals == "" && delAlertByID == "" {
|
||||||
_ = cmd.Usage()
|
_ = cmd.Usage()
|
||||||
return errors.New("at least one filter or --all must be specified")
|
return fmt.Errorf("at least one filter or --all must be specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if !AlertDeleteAll {
|
if !AlertDeleteAll {
|
||||||
if err = manageCliDecisionAlerts(alertDeleteFilter.IPEquals, alertDeleteFilter.RangeEquals,
|
if err := manageCliDecisionAlerts(alertDeleteFilter.IPEquals, alertDeleteFilter.RangeEquals,
|
||||||
alertDeleteFilter.ScopeEquals, alertDeleteFilter.ValueEquals); err != nil {
|
alertDeleteFilter.ScopeEquals, alertDeleteFilter.ValueEquals); err != nil {
|
||||||
printHelp(cmd)
|
printHelp(cmd)
|
||||||
return err
|
return err
|
||||||
|
@ -437,14 +446,14 @@ cscli alerts delete -s crowdsecurity/ssh-bf"`,
|
||||||
|
|
||||||
var alerts *models.DeleteAlertsResponse
|
var alerts *models.DeleteAlertsResponse
|
||||||
if delAlertByID == "" {
|
if delAlertByID == "" {
|
||||||
alerts, _, err = cli.client.Alerts.Delete(context.Background(), alertDeleteFilter)
|
alerts, _, err = Client.Alerts.Delete(context.Background(), alertDeleteFilter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to delete alerts: %w", err)
|
return fmt.Errorf("unable to delete alerts : %v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
alerts, _, err = cli.client.Alerts.DeleteOne(context.Background(), delAlertByID)
|
alerts, _, err = Client.Alerts.DeleteOne(context.Background(), delAlertByID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to delete alert: %w", err)
|
return fmt.Errorf("unable to delete alert: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Infof("%s alert(s) deleted", alerts.NbDeleted)
|
log.Infof("%s alert(s) deleted", alerts.NbDeleted)
|
||||||
|
@ -452,99 +461,90 @@ cscli alerts delete -s crowdsecurity/ssh-bf"`,
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmdAlertsDelete.Flags().SortFlags = false
|
||||||
flags := cmd.Flags()
|
cmdAlertsDelete.Flags().StringVar(alertDeleteFilter.ScopeEquals, "scope", "", "the scope (ie. ip,range)")
|
||||||
flags.SortFlags = false
|
cmdAlertsDelete.Flags().StringVarP(alertDeleteFilter.ValueEquals, "value", "v", "", "the value to match for in the specified scope")
|
||||||
flags.StringVar(alertDeleteFilter.ScopeEquals, "scope", "", "the scope (ie. ip,range)")
|
cmdAlertsDelete.Flags().StringVarP(alertDeleteFilter.ScenarioEquals, "scenario", "s", "", "the scenario (ie. crowdsecurity/ssh-bf)")
|
||||||
flags.StringVarP(alertDeleteFilter.ValueEquals, "value", "v", "", "the value to match for in the specified scope")
|
cmdAlertsDelete.Flags().StringVarP(alertDeleteFilter.IPEquals, "ip", "i", "", "Source ip (shorthand for --scope ip --value <IP>)")
|
||||||
flags.StringVarP(alertDeleteFilter.ScenarioEquals, "scenario", "s", "", "the scenario (ie. crowdsecurity/ssh-bf)")
|
cmdAlertsDelete.Flags().StringVarP(alertDeleteFilter.RangeEquals, "range", "r", "", "Range source ip (shorthand for --scope range --value <RANGE>)")
|
||||||
flags.StringVarP(alertDeleteFilter.IPEquals, "ip", "i", "", "Source ip (shorthand for --scope ip --value <IP>)")
|
cmdAlertsDelete.Flags().StringVar(&delAlertByID, "id", "", "alert ID")
|
||||||
flags.StringVarP(alertDeleteFilter.RangeEquals, "range", "r", "", "Range source ip (shorthand for --scope range --value <RANGE>)")
|
cmdAlertsDelete.Flags().BoolVarP(&AlertDeleteAll, "all", "a", false, "delete all alerts")
|
||||||
flags.StringVar(&delAlertByID, "id", "", "alert ID")
|
cmdAlertsDelete.Flags().BoolVar(contained, "contained", false, "query decisions contained by range")
|
||||||
flags.BoolVarP(&AlertDeleteAll, "all", "a", false, "delete all alerts")
|
return cmdAlertsDelete
|
||||||
flags.BoolVar(contained, "contained", false, "query decisions contained by range")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliAlerts) NewInspectCmd() *cobra.Command {
|
func NewAlertsInspectCmd() *cobra.Command {
|
||||||
var details bool
|
var details bool
|
||||||
|
var cmdAlertsInspect = &cobra.Command{
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: `inspect "alert_id"`,
|
Use: `inspect "alert_id"`,
|
||||||
Short: `Show info about an alert`,
|
Short: `Show info about an alert`,
|
||||||
Example: `cscli alerts inspect 123`,
|
Example: `cscli alerts inspect 123`,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
printHelp(cmd)
|
printHelp(cmd)
|
||||||
return errors.New("missing alert_id")
|
return fmt.Errorf("missing alert_id")
|
||||||
}
|
}
|
||||||
for _, alertID := range args {
|
for _, alertID := range args {
|
||||||
id, err := strconv.Atoi(alertID)
|
id, err := strconv.Atoi(alertID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("bad alert id %s", alertID)
|
return fmt.Errorf("bad alert id %s", alertID)
|
||||||
}
|
}
|
||||||
alert, _, err := cli.client.Alerts.GetByID(context.Background(), id)
|
alert, _, err := Client.Alerts.GetByID(context.Background(), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't find alert with id %s: %w", alertID, err)
|
return fmt.Errorf("can't find alert with id %s: %s", alertID, err)
|
||||||
}
|
}
|
||||||
switch cfg.Cscli.Output {
|
switch csConfig.Cscli.Output {
|
||||||
case "human":
|
case "human":
|
||||||
if err := cli.displayOneAlert(alert, details); err != nil {
|
if err := DisplayOneAlert(alert, details); err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
case "json":
|
case "json":
|
||||||
data, err := json.MarshalIndent(alert, "", " ")
|
data, err := json.MarshalIndent(alert, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to marshal alert with id %s: %w", alertID, err)
|
return fmt.Errorf("unable to marshal alert with id %s: %s", alertID, err)
|
||||||
}
|
}
|
||||||
fmt.Printf("%s\n", string(data))
|
fmt.Printf("%s\n", string(data))
|
||||||
case "raw":
|
case "raw":
|
||||||
data, err := yaml.Marshal(alert)
|
data, err := yaml.Marshal(alert)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to marshal alert with id %s: %w", alertID, err)
|
return fmt.Errorf("unable to marshal alert with id %s: %s", alertID, err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(data))
|
fmt.Printf("%s\n", string(data))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmdAlertsInspect.Flags().SortFlags = false
|
||||||
|
cmdAlertsInspect.Flags().BoolVarP(&details, "details", "d", false, "show alerts with events")
|
||||||
|
|
||||||
cmd.Flags().SortFlags = false
|
return cmdAlertsInspect
|
||||||
cmd.Flags().BoolVarP(&details, "details", "d", false, "show alerts with events")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliAlerts) NewFlushCmd() *cobra.Command {
|
func NewAlertsFlushCmd() *cobra.Command {
|
||||||
var (
|
var maxItems int
|
||||||
maxItems int
|
var maxAge string
|
||||||
maxAge string
|
var cmdAlertsFlush = &cobra.Command{
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: `flush`,
|
Use: `flush`,
|
||||||
Short: `Flush alerts
|
Short: `Flush alerts
|
||||||
/!\ This command can be used only on the same machine than the local API`,
|
/!\ This command can be used only on the same machine than the local API`,
|
||||||
Example: `cscli alerts flush --max-items 1000 --max-age 7d`,
|
Example: `cscli alerts flush --max-items 1000 --max-age 7d`,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
var err error
|
||||||
if err := require.LAPI(cfg); err != nil {
|
if err := require.LAPI(csConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
db, err := database.NewClient(cfg.DbConfig)
|
dbClient, err = database.NewClient(csConfig.DbConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to create new database client: %w", err)
|
return fmt.Errorf("unable to create new database client: %s", err)
|
||||||
}
|
}
|
||||||
log.Info("Flushing alerts. !! This may take a long time !!")
|
log.Info("Flushing alerts. !! This may take a long time !!")
|
||||||
err = db.FlushAlerts(maxAge, maxItems)
|
err = dbClient.FlushAlerts(maxAge, maxItems)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to flush alerts: %w", err)
|
return fmt.Errorf("unable to flush alerts: %s", err)
|
||||||
}
|
}
|
||||||
log.Info("Alerts flushed")
|
log.Info("Alerts flushed")
|
||||||
|
|
||||||
|
@ -552,9 +552,9 @@ func (cli *cliAlerts) NewFlushCmd() *cobra.Command {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().SortFlags = false
|
cmdAlertsFlush.Flags().SortFlags = false
|
||||||
cmd.Flags().IntVar(&maxItems, "max-items", 5000, "Maximum number of alert items to keep in the database")
|
cmdAlertsFlush.Flags().IntVar(&maxItems, "max-items", 5000, "Maximum number of alert items to keep in the database")
|
||||||
cmd.Flags().StringVar(&maxAge, "max-age", "7d", "Maximum age of alert items to keep in the database")
|
cmdAlertsFlush.Flags().StringVar(&maxAge, "max-age", "7d", "Maximum age of alert items to keep in the database")
|
||||||
|
|
||||||
return cmd
|
return cmdAlertsFlush
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,10 +3,8 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"io"
|
||||||
"slices"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -14,6 +12,7 @@ import (
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
|
middlewares "github.com/crowdsecurity/crowdsec/pkg/apiserver/middlewares/v1"
|
||||||
|
@ -21,34 +20,246 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func askYesNo(message string, defaultAnswer bool) (bool, error) {
|
func getBouncers(out io.Writer, dbClient *database.Client) error {
|
||||||
|
bouncers, err := dbClient.ListBouncers()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to list bouncers: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch csConfig.Cscli.Output {
|
||||||
|
case "human":
|
||||||
|
getBouncersTable(out, bouncers)
|
||||||
|
case "json":
|
||||||
|
enc := json.NewEncoder(out)
|
||||||
|
enc.SetIndent("", " ")
|
||||||
|
if err := enc.Encode(bouncers); err != nil {
|
||||||
|
return fmt.Errorf("failed to unmarshal: %w", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
case "raw":
|
||||||
|
csvwriter := csv.NewWriter(out)
|
||||||
|
err := csvwriter.Write([]string{"name", "ip", "revoked", "last_pull", "type", "version", "auth_type"})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write raw header: %w", err)
|
||||||
|
}
|
||||||
|
for _, b := range bouncers {
|
||||||
|
var revoked string
|
||||||
|
if !b.Revoked {
|
||||||
|
revoked = "validated"
|
||||||
|
} else {
|
||||||
|
revoked = "pending"
|
||||||
|
}
|
||||||
|
err := csvwriter.Write([]string{b.Name, b.IPAddress, revoked, b.LastPull.Format(time.RFC3339), b.Type, b.Version, b.AuthType})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to write raw: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
csvwriter.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBouncersListCmd() *cobra.Command {
|
||||||
|
cmdBouncersList := &cobra.Command{
|
||||||
|
Use: "list",
|
||||||
|
Short: "list all bouncers within the database",
|
||||||
|
Example: `cscli bouncers list`,
|
||||||
|
Args: cobra.ExactArgs(0),
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
RunE: func(cmd *cobra.Command, arg []string) error {
|
||||||
|
err := getBouncers(color.Output, dbClient)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to list bouncers: %s", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmdBouncersList
|
||||||
|
}
|
||||||
|
|
||||||
|
func runBouncersAdd(cmd *cobra.Command, args []string) error {
|
||||||
|
keyLength := 32
|
||||||
|
|
||||||
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
key, err := flags.GetString("key")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
keyName := args[0]
|
||||||
|
var apiKey string
|
||||||
|
|
||||||
|
if keyName == "" {
|
||||||
|
return fmt.Errorf("please provide a name for the api key")
|
||||||
|
}
|
||||||
|
apiKey = key
|
||||||
|
if key == "" {
|
||||||
|
apiKey, err = middlewares.GenerateAPIKey(keyLength)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to generate api key: %s", err)
|
||||||
|
}
|
||||||
|
_, err = dbClient.CreateBouncer(keyName, "", middlewares.HashSHA512(apiKey), types.ApiKeyAuthType)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to create bouncer: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch csConfig.Cscli.Output {
|
||||||
|
case "human":
|
||||||
|
fmt.Printf("API key for '%s':\n\n", keyName)
|
||||||
|
fmt.Printf(" %s\n\n", apiKey)
|
||||||
|
fmt.Print("Please keep this key since you will not be able to retrieve it!\n")
|
||||||
|
case "raw":
|
||||||
|
fmt.Printf("%s", apiKey)
|
||||||
|
case "json":
|
||||||
|
j, err := json.Marshal(apiKey)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to marshal api key")
|
||||||
|
}
|
||||||
|
fmt.Printf("%s", string(j))
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBouncersAddCmd() *cobra.Command {
|
||||||
|
cmdBouncersAdd := &cobra.Command{
|
||||||
|
Use: "add MyBouncerName",
|
||||||
|
Short: "add a single bouncer to the database",
|
||||||
|
Example: `cscli bouncers add MyBouncerName
|
||||||
|
cscli bouncers add MyBouncerName --key <random-key>`,
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
RunE: runBouncersAdd,
|
||||||
|
}
|
||||||
|
|
||||||
|
flags := cmdBouncersAdd.Flags()
|
||||||
|
flags.StringP("length", "l", "", "length of the api key")
|
||||||
|
flags.MarkDeprecated("length", "use --key instead")
|
||||||
|
flags.StringP("key", "k", "", "api key for the bouncer")
|
||||||
|
|
||||||
|
return cmdBouncersAdd
|
||||||
|
}
|
||||||
|
|
||||||
|
func runBouncersDelete(cmd *cobra.Command, args []string) error {
|
||||||
|
for _, bouncerID := range args {
|
||||||
|
err := dbClient.DeleteBouncer(bouncerID)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to delete bouncer '%s': %s", bouncerID, err)
|
||||||
|
}
|
||||||
|
log.Infof("bouncer '%s' deleted successfully", bouncerID)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBouncersDeleteCmd() *cobra.Command {
|
||||||
|
cmdBouncersDelete := &cobra.Command{
|
||||||
|
Use: "delete MyBouncerName",
|
||||||
|
Short: "delete bouncer(s) from the database",
|
||||||
|
Args: cobra.MinimumNArgs(1),
|
||||||
|
Aliases: []string{"remove"},
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
|
var err error
|
||||||
|
dbClient, err = getDBClient()
|
||||||
|
if err != nil {
|
||||||
|
cobra.CompError("unable to create new database client: " + err.Error())
|
||||||
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
|
bouncers, err := dbClient.ListBouncers()
|
||||||
|
if err != nil {
|
||||||
|
cobra.CompError("unable to list bouncers " + err.Error())
|
||||||
|
}
|
||||||
|
ret := make([]string, 0)
|
||||||
|
for _, bouncer := range bouncers {
|
||||||
|
if strings.Contains(bouncer.Name, toComplete) && !slices.Contains(args, bouncer.Name) {
|
||||||
|
ret = append(ret, bouncer.Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
},
|
||||||
|
RunE: runBouncersDelete,
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmdBouncersDelete
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBouncersPruneCmd() *cobra.Command {
|
||||||
|
var parsedDuration time.Duration
|
||||||
|
cmdBouncersPrune := &cobra.Command{
|
||||||
|
Use: "prune",
|
||||||
|
Short: "prune multiple bouncers from the database",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
Example: `cscli bouncers prune -d 60m
|
||||||
|
cscli bouncers prune -d 60m --force`,
|
||||||
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
dur, _ := cmd.Flags().GetString("duration")
|
||||||
|
var err error
|
||||||
|
parsedDuration, err = time.ParseDuration(fmt.Sprintf("-%s", dur))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to parse duration '%s': %s", dur, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
force, _ := cmd.Flags().GetBool("force")
|
||||||
|
if parsedDuration >= 0-2*time.Minute {
|
||||||
var answer bool
|
var answer bool
|
||||||
|
|
||||||
prompt := &survey.Confirm{
|
prompt := &survey.Confirm{
|
||||||
Message: message,
|
Message: "The duration you provided is less than or equal 2 minutes this may remove active bouncers continue ?",
|
||||||
Default: defaultAnswer,
|
Default: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := survey.AskOne(prompt, &answer); err != nil {
|
if err := survey.AskOne(prompt, &answer); err != nil {
|
||||||
return defaultAnswer, err
|
return fmt.Errorf("unable to ask about prune check: %s", err)
|
||||||
|
}
|
||||||
|
if !answer {
|
||||||
|
fmt.Println("user aborted prune no changes were made")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bouncers, err := dbClient.QueryBouncersLastPulltimeLT(time.Now().UTC().Add(parsedDuration))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to query bouncers: %s", err)
|
||||||
|
}
|
||||||
|
if len(bouncers) == 0 {
|
||||||
|
fmt.Println("no bouncers to prune")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
getBouncersTable(color.Output, bouncers)
|
||||||
|
if !force {
|
||||||
|
var answer bool
|
||||||
|
prompt := &survey.Confirm{
|
||||||
|
Message: "You are about to PERMANENTLY remove the above bouncers from the database these will NOT be recoverable, continue ?",
|
||||||
|
Default: false,
|
||||||
|
}
|
||||||
|
if err := survey.AskOne(prompt, &answer); err != nil {
|
||||||
|
return fmt.Errorf("unable to ask about prune check: %s", err)
|
||||||
|
}
|
||||||
|
if !answer {
|
||||||
|
fmt.Println("user aborted prune no changes were made")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nbDeleted, err := dbClient.BulkDeleteBouncers(bouncers)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to prune bouncers: %s", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("successfully delete %d bouncers\n", nbDeleted)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmdBouncersPrune.Flags().StringP("duration", "d", "60m", "duration of time since last pull")
|
||||||
|
cmdBouncersPrune.Flags().Bool("force", false, "force prune without asking for confirmation")
|
||||||
|
return cmdBouncersPrune
|
||||||
}
|
}
|
||||||
|
|
||||||
return answer, nil
|
func NewBouncersCmd() *cobra.Command {
|
||||||
}
|
var cmdBouncers = &cobra.Command{
|
||||||
|
|
||||||
type cliBouncers struct {
|
|
||||||
db *database.Client
|
|
||||||
cfg configGetter
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLIBouncers(cfg configGetter) *cliBouncers {
|
|
||||||
return &cliBouncers{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliBouncers) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "bouncers [action]",
|
Use: "bouncers [action]",
|
||||||
Short: "Manage bouncers [requires local API]",
|
Short: "Manage bouncers [requires local API]",
|
||||||
Long: `To list/add/delete/prune bouncers.
|
Long: `To list/add/delete/prune bouncers.
|
||||||
|
@ -57,264 +268,24 @@ Note: This command requires database direct access, so is intended to be run on
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
Aliases: []string{"bouncer"},
|
Aliases: []string{"bouncer"},
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
var err error
|
var err error
|
||||||
|
if err = require.LAPI(csConfig); err != nil {
|
||||||
cfg := cli.cfg()
|
|
||||||
|
|
||||||
if err = require.LAPI(cfg); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
cli.db, err = database.NewClient(cfg.DbConfig)
|
dbClient, err = database.NewClient(csConfig.DbConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't connect to the database: %w", err)
|
return fmt.Errorf("unable to create new database client: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.newListCmd())
|
cmdBouncers.AddCommand(NewBouncersListCmd())
|
||||||
cmd.AddCommand(cli.newAddCmd())
|
cmdBouncers.AddCommand(NewBouncersAddCmd())
|
||||||
cmd.AddCommand(cli.newDeleteCmd())
|
cmdBouncers.AddCommand(NewBouncersDeleteCmd())
|
||||||
cmd.AddCommand(cli.newPruneCmd())
|
cmdBouncers.AddCommand(NewBouncersPruneCmd())
|
||||||
|
|
||||||
return cmd
|
return cmdBouncers
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliBouncers) list() error {
|
|
||||||
out := color.Output
|
|
||||||
|
|
||||||
bouncers, err := cli.db.ListBouncers()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to list bouncers: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch cli.cfg().Cscli.Output {
|
|
||||||
case "human":
|
|
||||||
getBouncersTable(out, bouncers)
|
|
||||||
case "json":
|
|
||||||
enc := json.NewEncoder(out)
|
|
||||||
enc.SetIndent("", " ")
|
|
||||||
|
|
||||||
if err := enc.Encode(bouncers); err != nil {
|
|
||||||
return fmt.Errorf("failed to marshal: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
case "raw":
|
|
||||||
csvwriter := csv.NewWriter(out)
|
|
||||||
|
|
||||||
if err := csvwriter.Write([]string{"name", "ip", "revoked", "last_pull", "type", "version", "auth_type"}); err != nil {
|
|
||||||
return fmt.Errorf("failed to write raw header: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, b := range bouncers {
|
|
||||||
valid := "validated"
|
|
||||||
if b.Revoked {
|
|
||||||
valid = "pending"
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := csvwriter.Write([]string{b.Name, b.IPAddress, valid, b.LastPull.Format(time.RFC3339), b.Type, b.Version, b.AuthType}); err != nil {
|
|
||||||
return fmt.Errorf("failed to write raw: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
csvwriter.Flush()
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliBouncers) newListCmd() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "list",
|
|
||||||
Short: "list all bouncers within the database",
|
|
||||||
Example: `cscli bouncers list`,
|
|
||||||
Args: cobra.ExactArgs(0),
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
return cli.list()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliBouncers) add(bouncerName string, key string) error {
|
|
||||||
var err error
|
|
||||||
|
|
||||||
keyLength := 32
|
|
||||||
|
|
||||||
if key == "" {
|
|
||||||
key, err = middlewares.GenerateAPIKey(keyLength)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to generate api key: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = cli.db.CreateBouncer(bouncerName, "", middlewares.HashSHA512(key), types.ApiKeyAuthType)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to create bouncer: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch cli.cfg().Cscli.Output {
|
|
||||||
case "human":
|
|
||||||
fmt.Printf("API key for '%s':\n\n", bouncerName)
|
|
||||||
fmt.Printf(" %s\n\n", key)
|
|
||||||
fmt.Print("Please keep this key since you will not be able to retrieve it!\n")
|
|
||||||
case "raw":
|
|
||||||
fmt.Print(key)
|
|
||||||
case "json":
|
|
||||||
j, err := json.Marshal(key)
|
|
||||||
if err != nil {
|
|
||||||
return errors.New("unable to marshal api key")
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Print(string(j))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliBouncers) newAddCmd() *cobra.Command {
|
|
||||||
var key string
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "add MyBouncerName",
|
|
||||||
Short: "add a single bouncer to the database",
|
|
||||||
Example: `cscli bouncers add MyBouncerName
|
|
||||||
cscli bouncers add MyBouncerName --key <random-key>`,
|
|
||||||
Args: cobra.ExactArgs(1),
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
|
||||||
return cli.add(args[0], key)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
flags := cmd.Flags()
|
|
||||||
flags.StringP("length", "l", "", "length of the api key")
|
|
||||||
_ = flags.MarkDeprecated("length", "use --key instead")
|
|
||||||
flags.StringVarP(&key, "key", "k", "", "api key for the bouncer")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliBouncers) deleteValid(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
||||||
bouncers, err := cli.db.ListBouncers()
|
|
||||||
if err != nil {
|
|
||||||
cobra.CompError("unable to list bouncers " + err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := []string{}
|
|
||||||
|
|
||||||
for _, bouncer := range bouncers {
|
|
||||||
if strings.Contains(bouncer.Name, toComplete) && !slices.Contains(args, bouncer.Name) {
|
|
||||||
ret = append(ret, bouncer.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret, cobra.ShellCompDirectiveNoFileComp
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliBouncers) delete(bouncers []string) error {
|
|
||||||
for _, bouncerID := range bouncers {
|
|
||||||
err := cli.db.DeleteBouncer(bouncerID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to delete bouncer '%s': %w", bouncerID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("bouncer '%s' deleted successfully", bouncerID)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliBouncers) newDeleteCmd() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "delete MyBouncerName",
|
|
||||||
Short: "delete bouncer(s) from the database",
|
|
||||||
Args: cobra.MinimumNArgs(1),
|
|
||||||
Aliases: []string{"remove"},
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
ValidArgsFunction: cli.deleteValid,
|
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
|
||||||
return cli.delete(args)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliBouncers) prune(duration time.Duration, force bool) error {
|
|
||||||
if duration < 2*time.Minute {
|
|
||||||
if yes, err := askYesNo(
|
|
||||||
"The duration you provided is less than 2 minutes. " +
|
|
||||||
"This may remove active bouncers. Continue?", false); err != nil {
|
|
||||||
return err
|
|
||||||
} else if !yes {
|
|
||||||
fmt.Println("User aborted prune. No changes were made.")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bouncers, err := cli.db.QueryBouncersLastPulltimeLT(time.Now().UTC().Add(-duration))
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to query bouncers: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(bouncers) == 0 {
|
|
||||||
fmt.Println("No bouncers to prune.")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
getBouncersTable(color.Output, bouncers)
|
|
||||||
|
|
||||||
if !force {
|
|
||||||
if yes, err := askYesNo(
|
|
||||||
"You are about to PERMANENTLY remove the above bouncers from the database. " +
|
|
||||||
"These will NOT be recoverable. Continue?", false); err != nil {
|
|
||||||
return err
|
|
||||||
} else if !yes {
|
|
||||||
fmt.Println("User aborted prune. No changes were made.")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deleted, err := cli.db.BulkDeleteBouncers(bouncers)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to prune bouncers: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "Successfully deleted %d bouncers\n", deleted)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliBouncers) newPruneCmd() *cobra.Command {
|
|
||||||
var (
|
|
||||||
duration time.Duration
|
|
||||||
force bool
|
|
||||||
)
|
|
||||||
|
|
||||||
const defaultDuration = 60 * time.Minute
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "prune",
|
|
||||||
Short: "prune multiple bouncers from the database",
|
|
||||||
Args: cobra.NoArgs,
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
Example: `cscli bouncers prune -d 45m
|
|
||||||
cscli bouncers prune -d 45m --force`,
|
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
return cli.prune(duration, force)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
flags := cmd.Flags()
|
|
||||||
flags.DurationVarP(&duration, "duration", "d", defaultDuration, "duration of time since last pull")
|
|
||||||
flags.BoolVar(&force, "force", false, "force prune without asking for confirmation")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aquasecurity/table"
|
"github.com/aquasecurity/table"
|
||||||
|
"github.com/enescakir/emoji"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/emoji"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func getBouncersTable(out io.Writer, bouncers []*ent.Bouncer) {
|
func getBouncersTable(out io.Writer, bouncers []*ent.Bouncer) {
|
||||||
|
@ -17,9 +17,11 @@ func getBouncersTable(out io.Writer, bouncers []*ent.Bouncer) {
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
for _, b := range bouncers {
|
for _, b := range bouncers {
|
||||||
revoked := emoji.CheckMark
|
var revoked string
|
||||||
if b.Revoked {
|
if !b.Revoked {
|
||||||
revoked = emoji.Prohibited
|
revoked = emoji.CheckMark.String()
|
||||||
|
} else {
|
||||||
|
revoked = emoji.Prohibited.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
t.AddRow(b.Name, b.IPAddress, revoked, b.LastPull.Format(time.RFC3339), b.Type, b.Version, b.AuthType)
|
t.AddRow(b.Name, b.IPAddress, revoked, b.LastPull.Format(time.RFC3339), b.Type, b.Version, b.AuthType)
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -10,46 +9,35 @@ import (
|
||||||
"github.com/go-openapi/strfmt"
|
"github.com/go-openapi/strfmt"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/version"
|
"github.com/crowdsecurity/go-cs-lib/version"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const CAPIBaseURL string = "https://api.crowdsec.net/"
|
||||||
CAPIBaseURL = "https://api.crowdsec.net/"
|
const CAPIURLPrefix = "v3"
|
||||||
CAPIURLPrefix = "v3"
|
|
||||||
)
|
|
||||||
|
|
||||||
type cliCapi struct {
|
func NewCapiCmd() *cobra.Command {
|
||||||
cfg configGetter
|
var cmdCapi = &cobra.Command{
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLICapi(cfg configGetter) *cliCapi {
|
|
||||||
return &cliCapi{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliCapi) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "capi [action]",
|
Use: "capi [action]",
|
||||||
Short: "Manage interaction with Central API (CAPI)",
|
Short: "Manage interaction with Central API (CAPI)",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
if err := require.LAPI(csConfig); err != nil {
|
||||||
if err := require.LAPI(cfg); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := require.CAPI(cfg); err != nil {
|
if err := require.CAPI(csConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -57,27 +45,32 @@ func (cli *cliCapi) NewCommand() *cobra.Command {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.newRegisterCmd())
|
cmdCapi.AddCommand(NewCapiRegisterCmd())
|
||||||
cmd.AddCommand(cli.newStatusCmd())
|
cmdCapi.AddCommand(NewCapiStatusCmd())
|
||||||
|
|
||||||
return cmd
|
return cmdCapi
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliCapi) register(capiUserPrefix string, outputFile string) error {
|
func NewCapiRegisterCmd() *cobra.Command {
|
||||||
cfg := cli.cfg()
|
var capiUserPrefix string
|
||||||
|
var outputFile string
|
||||||
|
|
||||||
|
var cmdCapiRegister = &cobra.Command{
|
||||||
|
Use: "register",
|
||||||
|
Short: "Register to Central API (CAPI)",
|
||||||
|
Args: cobra.MinimumNArgs(0),
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
var err error
|
||||||
capiUser, err := generateID(capiUserPrefix)
|
capiUser, err := generateID(capiUserPrefix)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to generate machine id: %w", err)
|
return fmt.Errorf("unable to generate machine id: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
password := strfmt.Password(generatePassword(passwordLength))
|
password := strfmt.Password(generatePassword(passwordLength))
|
||||||
|
|
||||||
apiurl, err := url.Parse(types.CAPIBaseURL)
|
apiurl, err := url.Parse(types.CAPIBaseURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to parse api url %s: %w", types.CAPIBaseURL, err)
|
return fmt.Errorf("unable to parse api url %s: %w", types.CAPIBaseURL, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = apiclient.RegisterClient(&apiclient.Config{
|
_, err = apiclient.RegisterClient(&apiclient.Config{
|
||||||
MachineID: capiUser,
|
MachineID: capiUser,
|
||||||
Password: password,
|
Password: password,
|
||||||
|
@ -85,102 +78,91 @@ func (cli *cliCapi) register(capiUserPrefix string, outputFile string) error {
|
||||||
URL: apiurl,
|
URL: apiurl,
|
||||||
VersionPrefix: CAPIURLPrefix,
|
VersionPrefix: CAPIURLPrefix,
|
||||||
}, nil)
|
}, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("api client register ('%s'): %w", types.CAPIBaseURL, err)
|
return fmt.Errorf("api client register ('%s'): %w", types.CAPIBaseURL, err)
|
||||||
}
|
}
|
||||||
|
log.Printf("Successfully registered to Central API (CAPI)")
|
||||||
log.Infof("Successfully registered to Central API (CAPI)")
|
|
||||||
|
|
||||||
var dumpFile string
|
var dumpFile string
|
||||||
|
|
||||||
switch {
|
if outputFile != "" {
|
||||||
case outputFile != "":
|
|
||||||
dumpFile = outputFile
|
dumpFile = outputFile
|
||||||
case cfg.API.Server.OnlineClient.CredentialsFilePath != "":
|
} else if csConfig.API.Server.OnlineClient.CredentialsFilePath != "" {
|
||||||
dumpFile = cfg.API.Server.OnlineClient.CredentialsFilePath
|
dumpFile = csConfig.API.Server.OnlineClient.CredentialsFilePath
|
||||||
default:
|
} else {
|
||||||
dumpFile = ""
|
dumpFile = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
apiCfg := csconfig.ApiCredentialsCfg{
|
apiCfg := csconfig.ApiCredentialsCfg{
|
||||||
Login: capiUser,
|
Login: capiUser,
|
||||||
Password: password.String(),
|
Password: password.String(),
|
||||||
URL: types.CAPIBaseURL,
|
URL: types.CAPIBaseURL,
|
||||||
}
|
}
|
||||||
|
if fflag.PapiClient.IsEnabled() {
|
||||||
|
apiCfg.PapiURL = types.PAPIBaseURL
|
||||||
|
}
|
||||||
apiConfigDump, err := yaml.Marshal(apiCfg)
|
apiConfigDump, err := yaml.Marshal(apiCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to marshal api credentials: %w", err)
|
return fmt.Errorf("unable to marshal api credentials: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if dumpFile != "" {
|
if dumpFile != "" {
|
||||||
err = os.WriteFile(dumpFile, apiConfigDump, 0o600)
|
err = os.WriteFile(dumpFile, apiConfigDump, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("write api credentials in '%s' failed: %w", dumpFile, err)
|
return fmt.Errorf("write api credentials in '%s' failed: %w", dumpFile, err)
|
||||||
}
|
}
|
||||||
|
log.Printf("Central API credentials written to '%s'", dumpFile)
|
||||||
log.Infof("Central API credentials written to '%s'", dumpFile)
|
|
||||||
} else {
|
} else {
|
||||||
fmt.Println(string(apiConfigDump))
|
fmt.Printf("%s\n", string(apiConfigDump))
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Warning(ReloadMessage())
|
log.Warning(ReloadMessage())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliCapi) newRegisterCmd() *cobra.Command {
|
|
||||||
var (
|
|
||||||
capiUserPrefix string
|
|
||||||
outputFile string
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "register",
|
|
||||||
Short: "Register to Central API (CAPI)",
|
|
||||||
Args: cobra.MinimumNArgs(0),
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
return cli.register(capiUserPrefix, outputFile)
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmdCapiRegister.Flags().StringVarP(&outputFile, "file", "f", "", "output file destination")
|
||||||
cmd.Flags().StringVarP(&outputFile, "file", "f", "", "output file destination")
|
cmdCapiRegister.Flags().StringVar(&capiUserPrefix, "schmilblick", "", "set a schmilblick (use in tests only)")
|
||||||
cmd.Flags().StringVar(&capiUserPrefix, "schmilblick", "", "set a schmilblick (use in tests only)")
|
if err := cmdCapiRegister.Flags().MarkHidden("schmilblick"); err != nil {
|
||||||
|
|
||||||
if err := cmd.Flags().MarkHidden("schmilblick"); err != nil {
|
|
||||||
log.Fatalf("failed to hide flag: %s", err)
|
log.Fatalf("failed to hide flag: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdCapiRegister
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliCapi) status() error {
|
func NewCapiStatusCmd() *cobra.Command {
|
||||||
cfg := cli.cfg()
|
var cmdCapiStatus = &cobra.Command{
|
||||||
|
Use: "status",
|
||||||
if err := require.CAPIRegistered(cfg); err != nil {
|
Short: "Check status with the Central API (CAPI)",
|
||||||
return err
|
Args: cobra.MinimumNArgs(0),
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if csConfig.API.Server.OnlineClient == nil {
|
||||||
|
return fmt.Errorf("please provide credentials for the Central API (CAPI) in '%s'", csConfig.API.Server.OnlineClient.CredentialsFilePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
password := strfmt.Password(cfg.API.Server.OnlineClient.Credentials.Password)
|
if csConfig.API.Server.OnlineClient.Credentials == nil {
|
||||||
|
return fmt.Errorf("no credentials for Central API (CAPI) in '%s'", csConfig.API.Server.OnlineClient.CredentialsFilePath)
|
||||||
|
}
|
||||||
|
|
||||||
apiurl, err := url.Parse(cfg.API.Server.OnlineClient.Credentials.URL)
|
password := strfmt.Password(csConfig.API.Server.OnlineClient.Credentials.Password)
|
||||||
|
|
||||||
|
apiurl, err := url.Parse(csConfig.API.Server.OnlineClient.Credentials.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parsing api url ('%s'): %w", cfg.API.Server.OnlineClient.Credentials.URL, err)
|
return fmt.Errorf("parsing api url ('%s'): %w", csConfig.API.Server.OnlineClient.Credentials.URL, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hub, err := require.Hub(cfg, nil, nil)
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS)
|
scenarios, err := hub.GetInstalledItemNames(cwhub.SCENARIOS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get scenarios: %w", err)
|
return fmt.Errorf("failed to get scenarios: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(scenarios) == 0 {
|
if len(scenarios) == 0 {
|
||||||
return errors.New("no scenarios installed, abort")
|
return fmt.Errorf("no scenarios installed, abort")
|
||||||
}
|
}
|
||||||
|
|
||||||
Client, err = apiclient.NewDefaultClient(apiurl, CAPIURLPrefix, fmt.Sprintf("crowdsec/%s", version.String()), nil)
|
Client, err = apiclient.NewDefaultClient(apiurl, CAPIURLPrefix, fmt.Sprintf("crowdsec/%s", version.String()), nil)
|
||||||
|
@ -189,34 +171,23 @@ func (cli *cliCapi) status() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
t := models.WatcherAuthRequest{
|
t := models.WatcherAuthRequest{
|
||||||
MachineID: &cfg.API.Server.OnlineClient.Credentials.Login,
|
MachineID: &csConfig.API.Server.OnlineClient.Credentials.Login,
|
||||||
Password: &password,
|
Password: &password,
|
||||||
Scenarios: scenarios,
|
Scenarios: scenarios,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Loaded credentials from %s", cfg.API.Server.OnlineClient.CredentialsFilePath)
|
log.Infof("Loaded credentials from %s", csConfig.API.Server.OnlineClient.CredentialsFilePath)
|
||||||
log.Infof("Trying to authenticate with username %s on %s", cfg.API.Server.OnlineClient.Credentials.Login, apiurl)
|
log.Infof("Trying to authenticate with username %s on %s", csConfig.API.Server.OnlineClient.Credentials.Login, apiurl)
|
||||||
|
|
||||||
_, _, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
|
_, _, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to authenticate to Central API (CAPI): %w", err)
|
return fmt.Errorf("failed to authenticate to Central API (CAPI): %w", err)
|
||||||
}
|
}
|
||||||
|
log.Infof("You can successfully interact with Central API (CAPI)")
|
||||||
log.Info("You can successfully interact with Central API (CAPI)")
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliCapi) newStatusCmd() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "status",
|
|
||||||
Short: "Check status with the Central API (CAPI)",
|
|
||||||
Args: cobra.MinimumNArgs(0),
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
return cli.status()
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdCapiStatus
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,8 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCompletionCmd() *cobra.Command {
|
func NewCompletionCmd() *cobra.Command {
|
||||||
completionCmd := &cobra.Command{
|
|
||||||
|
var completionCmd = &cobra.Command{
|
||||||
Use: "completion [bash|zsh|powershell|fish]",
|
Use: "completion [bash|zsh|powershell|fish]",
|
||||||
Short: "Generate completion script",
|
Short: "Generate completion script",
|
||||||
Long: `To load completions:
|
Long: `To load completions:
|
||||||
|
@ -81,6 +82,5 @@ func NewCompletionCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return completionCmd
|
return completionCmd
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,29 +4,19 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
type cliConfig struct {
|
func NewConfigCmd() *cobra.Command {
|
||||||
cfg configGetter
|
cmdConfig := &cobra.Command{
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLIConfig(cfg configGetter) *cliConfig {
|
|
||||||
return &cliConfig{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliConfig) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "config [command]",
|
Use: "config [command]",
|
||||||
Short: "Allows to view current config",
|
Short: "Allows to view current config",
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.newShowCmd())
|
cmdConfig.AddCommand(NewConfigShowCmd())
|
||||||
cmd.AddCommand(cli.newShowYAMLCmd())
|
cmdConfig.AddCommand(NewConfigShowYAMLCmd())
|
||||||
cmd.AddCommand(cli.newBackupCmd())
|
cmdConfig.AddCommand(NewConfigBackupCmd())
|
||||||
cmd.AddCommand(cli.newRestoreCmd())
|
cmdConfig.AddCommand(NewConfigRestoreCmd())
|
||||||
cmd.AddCommand(cli.newFeatureFlagsCmd())
|
cmdConfig.AddCommand(NewConfigFeatureFlagsCmd())
|
||||||
|
|
||||||
return cmd
|
return cmdConfig
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -10,12 +9,15 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cli *cliConfig) backupHub(dirPath string) error {
|
func backupHub(dirPath string) error {
|
||||||
hub, err := require.Hub(cli.cfg(), nil, nil)
|
var itemDirectory string
|
||||||
|
var upstreamParsers []string
|
||||||
|
|
||||||
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -24,20 +26,16 @@ func (cli *cliConfig) backupHub(dirPath string) error {
|
||||||
clog := log.WithFields(log.Fields{
|
clog := log.WithFields(log.Fields{
|
||||||
"type": itemType,
|
"type": itemType,
|
||||||
})
|
})
|
||||||
|
|
||||||
itemMap := hub.GetItemMap(itemType)
|
itemMap := hub.GetItemMap(itemType)
|
||||||
if itemMap == nil {
|
if itemMap == nil {
|
||||||
clog.Infof("No %s to backup.", itemType)
|
clog.Infof("No %s to backup.", itemType)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
itemDirectory = fmt.Sprintf("%s/%s/", dirPath, itemType)
|
||||||
itemDirectory := fmt.Sprintf("%s/%s/", dirPath, itemType)
|
|
||||||
if err = os.MkdirAll(itemDirectory, os.ModePerm); err != nil {
|
if err = os.MkdirAll(itemDirectory, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("error while creating %s: %w", itemDirectory, err)
|
return fmt.Errorf("error while creating %s : %s", itemDirectory, err)
|
||||||
}
|
}
|
||||||
|
upstreamParsers = []string{}
|
||||||
upstreamParsers := []string{}
|
|
||||||
|
|
||||||
for k, v := range itemMap {
|
for k, v := range itemMap {
|
||||||
clog = clog.WithFields(log.Fields{
|
clog = clog.WithFields(log.Fields{
|
||||||
"file": v.Name,
|
"file": v.Name,
|
||||||
|
@ -53,39 +51,31 @@ func (cli *cliConfig) backupHub(dirPath string) error {
|
||||||
if itemType == cwhub.PARSERS || itemType == cwhub.POSTOVERFLOWS {
|
if itemType == cwhub.PARSERS || itemType == cwhub.POSTOVERFLOWS {
|
||||||
fstagedir := fmt.Sprintf("%s%s", itemDirectory, v.Stage)
|
fstagedir := fmt.Sprintf("%s%s", itemDirectory, v.Stage)
|
||||||
if err = os.MkdirAll(fstagedir, os.ModePerm); err != nil {
|
if err = os.MkdirAll(fstagedir, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("error while creating stage dir %s: %w", fstagedir, err)
|
return fmt.Errorf("error while creating stage dir %s : %s", fstagedir, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
clog.Debugf("[%s]: backing up file (tainted:%t local:%t up-to-date:%t)", k, v.State.Tainted, v.State.IsLocal(), v.State.UpToDate)
|
clog.Debugf("[%s]: backing up file (tainted:%t local:%t up-to-date:%t)", k, v.State.Tainted, v.State.IsLocal(), v.State.UpToDate)
|
||||||
|
|
||||||
tfile := fmt.Sprintf("%s%s/%s", itemDirectory, v.Stage, v.FileName)
|
tfile := fmt.Sprintf("%s%s/%s", itemDirectory, v.Stage, v.FileName)
|
||||||
if err = CopyFile(v.State.LocalPath, tfile); err != nil {
|
if err = CopyFile(v.State.LocalPath, tfile); err != nil {
|
||||||
return fmt.Errorf("failed copy %s %s to %s: %w", itemType, v.State.LocalPath, tfile, err)
|
return fmt.Errorf("failed copy %s %s to %s : %s", itemType, v.State.LocalPath, tfile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
clog.Infof("local/tainted saved %s to %s", v.State.LocalPath, tfile)
|
clog.Infof("local/tainted saved %s to %s", v.State.LocalPath, tfile)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
clog.Debugf("[%s] : from hub, just backup name (up-to-date:%t)", k, v.State.UpToDate)
|
clog.Debugf("[%s] : from hub, just backup name (up-to-date:%t)", k, v.State.UpToDate)
|
||||||
clog.Infof("saving, version:%s, up-to-date:%t", v.Version, v.State.UpToDate)
|
clog.Infof("saving, version:%s, up-to-date:%t", v.Version, v.State.UpToDate)
|
||||||
upstreamParsers = append(upstreamParsers, v.Name)
|
upstreamParsers = append(upstreamParsers, v.Name)
|
||||||
}
|
}
|
||||||
//write the upstream items
|
//write the upstream items
|
||||||
upstreamParsersFname := fmt.Sprintf("%s/upstream-%s.json", itemDirectory, itemType)
|
upstreamParsersFname := fmt.Sprintf("%s/upstream-%s.json", itemDirectory, itemType)
|
||||||
|
|
||||||
upstreamParsersContent, err := json.MarshalIndent(upstreamParsers, "", " ")
|
upstreamParsersContent, err := json.MarshalIndent(upstreamParsers, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed marshaling upstream parsers: %w", err)
|
return fmt.Errorf("failed marshaling upstream parsers : %s", err)
|
||||||
}
|
}
|
||||||
|
err = os.WriteFile(upstreamParsersFname, upstreamParsersContent, 0644)
|
||||||
err = os.WriteFile(upstreamParsersFname, upstreamParsersContent, 0o644)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to write to %s %s: %w", itemType, upstreamParsersFname, err)
|
return fmt.Errorf("unable to write to %s %s : %s", itemType, upstreamParsersFname, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
clog.Infof("Wrote %d entries for %s to %s", len(upstreamParsers), itemType, upstreamParsersFname)
|
clog.Infof("Wrote %d entries for %s to %s", len(upstreamParsers), itemType, upstreamParsersFname)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -103,13 +93,11 @@ func (cli *cliConfig) backupHub(dirPath string) error {
|
||||||
- Tainted/local/out-of-date scenarios, parsers, postoverflows and collections
|
- Tainted/local/out-of-date scenarios, parsers, postoverflows and collections
|
||||||
- Acquisition files (acquis.yaml, acquis.d/*.yaml)
|
- Acquisition files (acquis.yaml, acquis.d/*.yaml)
|
||||||
*/
|
*/
|
||||||
func (cli *cliConfig) backup(dirPath string) error {
|
func backupConfigToDirectory(dirPath string) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
cfg := cli.cfg()
|
|
||||||
|
|
||||||
if dirPath == "" {
|
if dirPath == "" {
|
||||||
return errors.New("directory path can't be empty")
|
return fmt.Errorf("directory path can't be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Starting configuration backup")
|
log.Infof("Starting configuration backup")
|
||||||
|
@ -124,10 +112,10 @@ func (cli *cliConfig) backup(dirPath string) error {
|
||||||
return fmt.Errorf("while creating %s: %w", dirPath, err)
|
return fmt.Errorf("while creating %s: %w", dirPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.ConfigPaths.SimulationFilePath != "" {
|
if csConfig.ConfigPaths.SimulationFilePath != "" {
|
||||||
backupSimulation := filepath.Join(dirPath, "simulation.yaml")
|
backupSimulation := filepath.Join(dirPath, "simulation.yaml")
|
||||||
if err = CopyFile(cfg.ConfigPaths.SimulationFilePath, backupSimulation); err != nil {
|
if err = CopyFile(csConfig.ConfigPaths.SimulationFilePath, backupSimulation); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", cfg.ConfigPaths.SimulationFilePath, backupSimulation, err)
|
return fmt.Errorf("failed copy %s to %s: %w", csConfig.ConfigPaths.SimulationFilePath, backupSimulation, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Saved simulation to %s", backupSimulation)
|
log.Infof("Saved simulation to %s", backupSimulation)
|
||||||
|
@ -137,22 +125,22 @@ func (cli *cliConfig) backup(dirPath string) error {
|
||||||
- backup AcquisitionFilePath
|
- backup AcquisitionFilePath
|
||||||
- backup the other files of acquisition directory
|
- backup the other files of acquisition directory
|
||||||
*/
|
*/
|
||||||
if cfg.Crowdsec != nil && cfg.Crowdsec.AcquisitionFilePath != "" {
|
if csConfig.Crowdsec != nil && csConfig.Crowdsec.AcquisitionFilePath != "" {
|
||||||
backupAcquisition := filepath.Join(dirPath, "acquis.yaml")
|
backupAcquisition := filepath.Join(dirPath, "acquis.yaml")
|
||||||
if err = CopyFile(cfg.Crowdsec.AcquisitionFilePath, backupAcquisition); err != nil {
|
if err = CopyFile(csConfig.Crowdsec.AcquisitionFilePath, backupAcquisition); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", cfg.Crowdsec.AcquisitionFilePath, backupAcquisition, err)
|
return fmt.Errorf("failed copy %s to %s: %s", csConfig.Crowdsec.AcquisitionFilePath, backupAcquisition, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
acquisBackupDir := filepath.Join(dirPath, "acquis")
|
acquisBackupDir := filepath.Join(dirPath, "acquis")
|
||||||
if err = os.Mkdir(acquisBackupDir, 0o700); err != nil {
|
if err = os.Mkdir(acquisBackupDir, 0o700); err != nil {
|
||||||
return fmt.Errorf("error while creating %s: %w", acquisBackupDir, err)
|
return fmt.Errorf("error while creating %s: %s", acquisBackupDir, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Crowdsec != nil && len(cfg.Crowdsec.AcquisitionFiles) > 0 {
|
if csConfig.Crowdsec != nil && len(csConfig.Crowdsec.AcquisitionFiles) > 0 {
|
||||||
for _, acquisFile := range cfg.Crowdsec.AcquisitionFiles {
|
for _, acquisFile := range csConfig.Crowdsec.AcquisitionFiles {
|
||||||
/*if it was the default one, it was already backup'ed*/
|
/*if it was the default one, it was already backup'ed*/
|
||||||
if cfg.Crowdsec.AcquisitionFilePath == acquisFile {
|
if csConfig.Crowdsec.AcquisitionFilePath == acquisFile {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,48 +160,56 @@ func (cli *cliConfig) backup(dirPath string) error {
|
||||||
if ConfigFilePath != "" {
|
if ConfigFilePath != "" {
|
||||||
backupMain := fmt.Sprintf("%s/config.yaml", dirPath)
|
backupMain := fmt.Sprintf("%s/config.yaml", dirPath)
|
||||||
if err = CopyFile(ConfigFilePath, backupMain); err != nil {
|
if err = CopyFile(ConfigFilePath, backupMain); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", ConfigFilePath, backupMain, err)
|
return fmt.Errorf("failed copy %s to %s: %s", ConfigFilePath, backupMain, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Saved default yaml to %s", backupMain)
|
log.Infof("Saved default yaml to %s", backupMain)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.API != nil && cfg.API.Server != nil && cfg.API.Server.OnlineClient != nil && cfg.API.Server.OnlineClient.CredentialsFilePath != "" {
|
if csConfig.API != nil && csConfig.API.Server != nil && csConfig.API.Server.OnlineClient != nil && csConfig.API.Server.OnlineClient.CredentialsFilePath != "" {
|
||||||
backupCAPICreds := fmt.Sprintf("%s/online_api_credentials.yaml", dirPath)
|
backupCAPICreds := fmt.Sprintf("%s/online_api_credentials.yaml", dirPath)
|
||||||
if err = CopyFile(cfg.API.Server.OnlineClient.CredentialsFilePath, backupCAPICreds); err != nil {
|
if err = CopyFile(csConfig.API.Server.OnlineClient.CredentialsFilePath, backupCAPICreds); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", cfg.API.Server.OnlineClient.CredentialsFilePath, backupCAPICreds, err)
|
return fmt.Errorf("failed copy %s to %s: %s", csConfig.API.Server.OnlineClient.CredentialsFilePath, backupCAPICreds, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Saved online API credentials to %s", backupCAPICreds)
|
log.Infof("Saved online API credentials to %s", backupCAPICreds)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.API != nil && cfg.API.Client != nil && cfg.API.Client.CredentialsFilePath != "" {
|
if csConfig.API != nil && csConfig.API.Client != nil && csConfig.API.Client.CredentialsFilePath != "" {
|
||||||
backupLAPICreds := fmt.Sprintf("%s/local_api_credentials.yaml", dirPath)
|
backupLAPICreds := fmt.Sprintf("%s/local_api_credentials.yaml", dirPath)
|
||||||
if err = CopyFile(cfg.API.Client.CredentialsFilePath, backupLAPICreds); err != nil {
|
if err = CopyFile(csConfig.API.Client.CredentialsFilePath, backupLAPICreds); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", cfg.API.Client.CredentialsFilePath, backupLAPICreds, err)
|
return fmt.Errorf("failed copy %s to %s: %s", csConfig.API.Client.CredentialsFilePath, backupLAPICreds, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Saved local API credentials to %s", backupLAPICreds)
|
log.Infof("Saved local API credentials to %s", backupLAPICreds)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.API != nil && cfg.API.Server != nil && cfg.API.Server.ProfilesPath != "" {
|
if csConfig.API != nil && csConfig.API.Server != nil && csConfig.API.Server.ProfilesPath != "" {
|
||||||
backupProfiles := fmt.Sprintf("%s/profiles.yaml", dirPath)
|
backupProfiles := fmt.Sprintf("%s/profiles.yaml", dirPath)
|
||||||
if err = CopyFile(cfg.API.Server.ProfilesPath, backupProfiles); err != nil {
|
if err = CopyFile(csConfig.API.Server.ProfilesPath, backupProfiles); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", cfg.API.Server.ProfilesPath, backupProfiles, err)
|
return fmt.Errorf("failed copy %s to %s: %s", csConfig.API.Server.ProfilesPath, backupProfiles, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Saved profiles to %s", backupProfiles)
|
log.Infof("Saved profiles to %s", backupProfiles)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = cli.backupHub(dirPath); err != nil {
|
if err = backupHub(dirPath); err != nil {
|
||||||
return fmt.Errorf("failed to backup hub config: %w", err)
|
return fmt.Errorf("failed to backup hub config: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliConfig) newBackupCmd() *cobra.Command {
|
func runConfigBackup(cmd *cobra.Command, args []string) error {
|
||||||
cmd := &cobra.Command{
|
if err := backupConfigToDirectory(args[0]); err != nil {
|
||||||
|
return fmt.Errorf("failed to backup config: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConfigBackupCmd() *cobra.Command {
|
||||||
|
cmdConfigBackup := &cobra.Command{
|
||||||
Use: `backup "directory"`,
|
Use: `backup "directory"`,
|
||||||
Short: "Backup current config",
|
Short: "Backup current config",
|
||||||
Long: `Backup the current crowdsec configuration including :
|
Long: `Backup the current crowdsec configuration including :
|
||||||
|
@ -227,14 +223,8 @@ func (cli *cliConfig) newBackupCmd() *cobra.Command {
|
||||||
Example: `cscli config backup ./my-backup`,
|
Example: `cscli config backup ./my-backup`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: runConfigBackup,
|
||||||
if err := cli.backup(args[0]); err != nil {
|
|
||||||
return fmt.Errorf("failed to backup config: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return cmdConfigBackup
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,14 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cli *cliConfig) featureFlags(showRetired bool) error {
|
func runConfigFeatureFlags(cmd *cobra.Command, args []string) error {
|
||||||
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
showRetired, err := flags.GetBool("retired")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
green := color.New(color.FgGreen).SprintFunc()
|
green := color.New(color.FgGreen).SprintFunc()
|
||||||
red := color.New(color.FgRed).SprintFunc()
|
red := color.New(color.FgRed).SprintFunc()
|
||||||
yellow := color.New(color.FgYellow).SprintFunc()
|
yellow := color.New(color.FgYellow).SprintFunc()
|
||||||
|
@ -37,7 +44,6 @@ func (cli *cliConfig) featureFlags(showRetired bool) error {
|
||||||
if feat.State == fflag.RetiredState {
|
if feat.State == fflag.RetiredState {
|
||||||
fmt.Printf("\n %s %s", magenta("RETIRED"), feat.DeprecationMsg)
|
fmt.Printf("\n %s %s", magenta("RETIRED"), feat.DeprecationMsg)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,12 +58,10 @@ func (cli *cliConfig) featureFlags(showRetired bool) error {
|
||||||
retired = append(retired, feat)
|
retired = append(retired, feat)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if feat.IsEnabled() {
|
if feat.IsEnabled() {
|
||||||
enabled = append(enabled, feat)
|
enabled = append(enabled, feat)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
disabled = append(disabled, feat)
|
disabled = append(disabled, feat)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,22 +118,18 @@ func (cli *cliConfig) featureFlags(showRetired bool) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliConfig) newFeatureFlagsCmd() *cobra.Command {
|
func NewConfigFeatureFlagsCmd() *cobra.Command {
|
||||||
var showRetired bool
|
cmdConfigFeatureFlags := &cobra.Command{
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "feature-flags",
|
Use: "feature-flags",
|
||||||
Short: "Displays feature flag status",
|
Short: "Displays feature flag status",
|
||||||
Long: `Displays the supported feature flags and their current status.`,
|
Long: `Displays the supported feature flags and their current status.`,
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: runConfigFeatureFlags,
|
||||||
return cli.featureFlags(showRetired)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmdConfigFeatureFlags.Flags()
|
||||||
flags.BoolVar(&showRetired, "retired", false, "Show retired features")
|
flags.Bool("retired", false, "Show retired features")
|
||||||
|
|
||||||
return cmd
|
return cmdConfigFeatureFlags
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,20 +3,26 @@ package main
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cli *cliConfig) restoreHub(dirPath string) error {
|
type OldAPICfg struct {
|
||||||
cfg := cli.cfg()
|
MachineID string `json:"machine_id"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
}
|
||||||
|
|
||||||
hub, err := require.Hub(cfg, require.RemoteHub(cfg), nil)
|
func restoreHub(dirPath string) error {
|
||||||
|
hub, err := require.Hub(csConfig, require.RemoteHub(csConfig))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -29,27 +35,23 @@ func (cli *cliConfig) restoreHub(dirPath string) error {
|
||||||
}
|
}
|
||||||
/*restore the upstream items*/
|
/*restore the upstream items*/
|
||||||
upstreamListFN := fmt.Sprintf("%s/upstream-%s.json", itemDirectory, itype)
|
upstreamListFN := fmt.Sprintf("%s/upstream-%s.json", itemDirectory, itype)
|
||||||
|
|
||||||
file, err := os.ReadFile(upstreamListFN)
|
file, err := os.ReadFile(upstreamListFN)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error while opening %s: %w", upstreamListFN, err)
|
return fmt.Errorf("error while opening %s : %s", upstreamListFN, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var upstreamList []string
|
var upstreamList []string
|
||||||
|
|
||||||
err = json.Unmarshal(file, &upstreamList)
|
err = json.Unmarshal(file, &upstreamList)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error unmarshaling %s: %w", upstreamListFN, err)
|
return fmt.Errorf("error unmarshaling %s : %s", upstreamListFN, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, toinstall := range upstreamList {
|
for _, toinstall := range upstreamList {
|
||||||
item := hub.GetItem(itype, toinstall)
|
item := hub.GetItem(itype, toinstall)
|
||||||
if item == nil {
|
if item == nil {
|
||||||
log.Errorf("Item %s/%s not found in hub", itype, toinstall)
|
log.Errorf("Item %s/%s not found in hub", itype, toinstall)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
err := item.Install(false, false)
|
||||||
if err = item.Install(false, false); err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error while installing %s : %s", toinstall, err)
|
log.Errorf("Error while installing %s : %s", toinstall, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -57,61 +59,51 @@ func (cli *cliConfig) restoreHub(dirPath string) error {
|
||||||
/*restore the local and tainted items*/
|
/*restore the local and tainted items*/
|
||||||
files, err := os.ReadDir(itemDirectory)
|
files, err := os.ReadDir(itemDirectory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed enumerating files of %s: %w", itemDirectory, err)
|
return fmt.Errorf("failed enumerating files of %s : %s", itemDirectory, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
//this was the upstream data
|
//this was the upstream data
|
||||||
if file.Name() == fmt.Sprintf("upstream-%s.json", itype) {
|
if file.Name() == fmt.Sprintf("upstream-%s.json", itype) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if itype == cwhub.PARSERS || itype == cwhub.POSTOVERFLOWS {
|
if itype == cwhub.PARSERS || itype == cwhub.POSTOVERFLOWS {
|
||||||
//we expect a stage here
|
//we expect a stage here
|
||||||
if !file.IsDir() {
|
if !file.IsDir() {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
stage := file.Name()
|
stage := file.Name()
|
||||||
stagedir := fmt.Sprintf("%s/%s/%s/", cfg.ConfigPaths.ConfigDir, itype, stage)
|
stagedir := fmt.Sprintf("%s/%s/%s/", csConfig.ConfigPaths.ConfigDir, itype, stage)
|
||||||
log.Debugf("Found stage %s in %s, target directory : %s", stage, itype, stagedir)
|
log.Debugf("Found stage %s in %s, target directory : %s", stage, itype, stagedir)
|
||||||
|
|
||||||
if err = os.MkdirAll(stagedir, os.ModePerm); err != nil {
|
if err = os.MkdirAll(stagedir, os.ModePerm); err != nil {
|
||||||
return fmt.Errorf("error while creating stage directory %s: %w", stagedir, err)
|
return fmt.Errorf("error while creating stage directory %s : %s", stagedir, err)
|
||||||
}
|
}
|
||||||
|
/*find items*/
|
||||||
// find items
|
|
||||||
ifiles, err := os.ReadDir(itemDirectory + "/" + stage + "/")
|
ifiles, err := os.ReadDir(itemDirectory + "/" + stage + "/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed enumerating files of %s: %w", itemDirectory+"/"+stage, err)
|
return fmt.Errorf("failed enumerating files of %s : %s", itemDirectory+"/"+stage, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
//finally copy item
|
//finally copy item
|
||||||
for _, tfile := range ifiles {
|
for _, tfile := range ifiles {
|
||||||
log.Infof("Going to restore local/tainted [%s]", tfile.Name())
|
log.Infof("Going to restore local/tainted [%s]", tfile.Name())
|
||||||
sourceFile := fmt.Sprintf("%s/%s/%s", itemDirectory, stage, tfile.Name())
|
sourceFile := fmt.Sprintf("%s/%s/%s", itemDirectory, stage, tfile.Name())
|
||||||
|
|
||||||
destinationFile := fmt.Sprintf("%s%s", stagedir, tfile.Name())
|
destinationFile := fmt.Sprintf("%s%s", stagedir, tfile.Name())
|
||||||
if err = CopyFile(sourceFile, destinationFile); err != nil {
|
if err = CopyFile(sourceFile, destinationFile); err != nil {
|
||||||
return fmt.Errorf("failed copy %s %s to %s: %w", itype, sourceFile, destinationFile, err)
|
return fmt.Errorf("failed copy %s %s to %s : %s", itype, sourceFile, destinationFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("restored %s to %s", sourceFile, destinationFile)
|
log.Infof("restored %s to %s", sourceFile, destinationFile)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Infof("Going to restore local/tainted [%s]", file.Name())
|
log.Infof("Going to restore local/tainted [%s]", file.Name())
|
||||||
sourceFile := fmt.Sprintf("%s/%s", itemDirectory, file.Name())
|
sourceFile := fmt.Sprintf("%s/%s", itemDirectory, file.Name())
|
||||||
destinationFile := fmt.Sprintf("%s/%s/%s", cfg.ConfigPaths.ConfigDir, itype, file.Name())
|
destinationFile := fmt.Sprintf("%s/%s/%s", csConfig.ConfigPaths.ConfigDir, itype, file.Name())
|
||||||
|
|
||||||
if err = CopyFile(sourceFile, destinationFile); err != nil {
|
if err = CopyFile(sourceFile, destinationFile); err != nil {
|
||||||
return fmt.Errorf("failed copy %s %s to %s: %w", itype, sourceFile, destinationFile, err)
|
return fmt.Errorf("failed copy %s %s to %s : %s", itype, sourceFile, destinationFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("restored %s to %s", sourceFile, destinationFile)
|
log.Infof("restored %s to %s", sourceFile, destinationFile)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -126,64 +118,90 @@ func (cli *cliConfig) restoreHub(dirPath string) error {
|
||||||
- Tainted/local/out-of-date scenarios, parsers, postoverflows and collections
|
- Tainted/local/out-of-date scenarios, parsers, postoverflows and collections
|
||||||
- Acquisition files (acquis.yaml, acquis.d/*.yaml)
|
- Acquisition files (acquis.yaml, acquis.d/*.yaml)
|
||||||
*/
|
*/
|
||||||
func (cli *cliConfig) restore(dirPath string) error {
|
func restoreConfigFromDirectory(dirPath string, oldBackup bool) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
cfg := cli.cfg()
|
if !oldBackup {
|
||||||
|
|
||||||
backupMain := fmt.Sprintf("%s/config.yaml", dirPath)
|
backupMain := fmt.Sprintf("%s/config.yaml", dirPath)
|
||||||
if _, err = os.Stat(backupMain); err == nil {
|
if _, err = os.Stat(backupMain); err == nil {
|
||||||
if cfg.ConfigPaths != nil && cfg.ConfigPaths.ConfigDir != "" {
|
if csConfig.ConfigPaths != nil && csConfig.ConfigPaths.ConfigDir != "" {
|
||||||
if err = CopyFile(backupMain, fmt.Sprintf("%s/config.yaml", cfg.ConfigPaths.ConfigDir)); err != nil {
|
if err = CopyFile(backupMain, fmt.Sprintf("%s/config.yaml", csConfig.ConfigPaths.ConfigDir)); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", backupMain, cfg.ConfigPaths.ConfigDir, err)
|
return fmt.Errorf("failed copy %s to %s : %s", backupMain, csConfig.ConfigPaths.ConfigDir, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now we have config.yaml, we should regenerate config struct to have rights paths etc
|
// Now we have config.yaml, we should regenerate config struct to have rights paths etc
|
||||||
ConfigFilePath = fmt.Sprintf("%s/config.yaml", cfg.ConfigPaths.ConfigDir)
|
ConfigFilePath = fmt.Sprintf("%s/config.yaml", csConfig.ConfigPaths.ConfigDir)
|
||||||
|
|
||||||
log.Debug("Reloading configuration")
|
initConfig()
|
||||||
|
|
||||||
csConfig, _, err = loadConfigFor("config")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to reload configuration: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cfg = cli.cfg()
|
|
||||||
|
|
||||||
backupCAPICreds := fmt.Sprintf("%s/online_api_credentials.yaml", dirPath)
|
backupCAPICreds := fmt.Sprintf("%s/online_api_credentials.yaml", dirPath)
|
||||||
if _, err = os.Stat(backupCAPICreds); err == nil {
|
if _, err = os.Stat(backupCAPICreds); err == nil {
|
||||||
if err = CopyFile(backupCAPICreds, cfg.API.Server.OnlineClient.CredentialsFilePath); err != nil {
|
if err = CopyFile(backupCAPICreds, csConfig.API.Server.OnlineClient.CredentialsFilePath); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", backupCAPICreds, cfg.API.Server.OnlineClient.CredentialsFilePath, err)
|
return fmt.Errorf("failed copy %s to %s : %s", backupCAPICreds, csConfig.API.Server.OnlineClient.CredentialsFilePath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
backupLAPICreds := fmt.Sprintf("%s/local_api_credentials.yaml", dirPath)
|
backupLAPICreds := fmt.Sprintf("%s/local_api_credentials.yaml", dirPath)
|
||||||
if _, err = os.Stat(backupLAPICreds); err == nil {
|
if _, err = os.Stat(backupLAPICreds); err == nil {
|
||||||
if err = CopyFile(backupLAPICreds, cfg.API.Client.CredentialsFilePath); err != nil {
|
if err = CopyFile(backupLAPICreds, csConfig.API.Client.CredentialsFilePath); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", backupLAPICreds, cfg.API.Client.CredentialsFilePath, err)
|
return fmt.Errorf("failed copy %s to %s : %s", backupLAPICreds, csConfig.API.Client.CredentialsFilePath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
backupProfiles := fmt.Sprintf("%s/profiles.yaml", dirPath)
|
backupProfiles := fmt.Sprintf("%s/profiles.yaml", dirPath)
|
||||||
if _, err = os.Stat(backupProfiles); err == nil {
|
if _, err = os.Stat(backupProfiles); err == nil {
|
||||||
if err = CopyFile(backupProfiles, cfg.API.Server.ProfilesPath); err != nil {
|
if err = CopyFile(backupProfiles, csConfig.API.Server.ProfilesPath); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", backupProfiles, cfg.API.Server.ProfilesPath, err)
|
return fmt.Errorf("failed copy %s to %s : %s", backupProfiles, csConfig.API.Server.ProfilesPath, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
var oldAPICfg OldAPICfg
|
||||||
|
backupOldAPICfg := fmt.Sprintf("%s/api_creds.json", dirPath)
|
||||||
|
|
||||||
|
jsonFile, err := os.Open(backupOldAPICfg)
|
||||||
|
if err != nil {
|
||||||
|
log.Warningf("failed to open %s : %s", backupOldAPICfg, err)
|
||||||
|
} else {
|
||||||
|
byteValue, _ := io.ReadAll(jsonFile)
|
||||||
|
err = json.Unmarshal(byteValue, &oldAPICfg)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("failed to load json file %s : %s", backupOldAPICfg, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
apiCfg := csconfig.ApiCredentialsCfg{
|
||||||
|
Login: oldAPICfg.MachineID,
|
||||||
|
Password: oldAPICfg.Password,
|
||||||
|
URL: CAPIBaseURL,
|
||||||
|
}
|
||||||
|
apiConfigDump, err := yaml.Marshal(apiCfg)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to dump api credentials: %s", err)
|
||||||
|
}
|
||||||
|
apiConfigDumpFile := fmt.Sprintf("%s/online_api_credentials.yaml", csConfig.ConfigPaths.ConfigDir)
|
||||||
|
if csConfig.API.Server.OnlineClient != nil && csConfig.API.Server.OnlineClient.CredentialsFilePath != "" {
|
||||||
|
apiConfigDumpFile = csConfig.API.Server.OnlineClient.CredentialsFilePath
|
||||||
|
}
|
||||||
|
err = os.WriteFile(apiConfigDumpFile, apiConfigDump, 0o600)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("write api credentials in '%s' failed: %s", apiConfigDumpFile, err)
|
||||||
|
}
|
||||||
|
log.Infof("Saved API credentials to %s", apiConfigDumpFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
backupSimulation := fmt.Sprintf("%s/simulation.yaml", dirPath)
|
backupSimulation := fmt.Sprintf("%s/simulation.yaml", dirPath)
|
||||||
if _, err = os.Stat(backupSimulation); err == nil {
|
if _, err = os.Stat(backupSimulation); err == nil {
|
||||||
if err = CopyFile(backupSimulation, cfg.ConfigPaths.SimulationFilePath); err != nil {
|
if err = CopyFile(backupSimulation, csConfig.ConfigPaths.SimulationFilePath); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", backupSimulation, cfg.ConfigPaths.SimulationFilePath, err)
|
return fmt.Errorf("failed copy %s to %s : %s", backupSimulation, csConfig.ConfigPaths.SimulationFilePath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*if there is a acquisition dir, restore its content*/
|
/*if there is a acquisition dir, restore its content*/
|
||||||
if cfg.Crowdsec.AcquisitionDirPath != "" {
|
if csConfig.Crowdsec.AcquisitionDirPath != "" {
|
||||||
if err = os.MkdirAll(cfg.Crowdsec.AcquisitionDirPath, 0o700); err != nil {
|
if err = os.MkdirAll(csConfig.Crowdsec.AcquisitionDirPath, 0o700); err != nil {
|
||||||
return fmt.Errorf("error while creating %s: %w", cfg.Crowdsec.AcquisitionDirPath, err)
|
return fmt.Errorf("error while creating %s : %s", csConfig.Crowdsec.AcquisitionDirPath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -192,16 +210,16 @@ func (cli *cliConfig) restore(dirPath string) error {
|
||||||
if _, err = os.Stat(backupAcquisition); err == nil {
|
if _, err = os.Stat(backupAcquisition); err == nil {
|
||||||
log.Debugf("restoring backup'ed %s", backupAcquisition)
|
log.Debugf("restoring backup'ed %s", backupAcquisition)
|
||||||
|
|
||||||
if err = CopyFile(backupAcquisition, cfg.Crowdsec.AcquisitionFilePath); err != nil {
|
if err = CopyFile(backupAcquisition, csConfig.Crowdsec.AcquisitionFilePath); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", backupAcquisition, cfg.Crowdsec.AcquisitionFilePath, err)
|
return fmt.Errorf("failed copy %s to %s : %s", backupAcquisition, csConfig.Crowdsec.AcquisitionFilePath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// if there are files in the acquis backup dir, restore them
|
// if there is files in the acquis backup dir, restore them
|
||||||
acquisBackupDir := filepath.Join(dirPath, "acquis", "*.yaml")
|
acquisBackupDir := filepath.Join(dirPath, "acquis", "*.yaml")
|
||||||
if acquisFiles, err := filepath.Glob(acquisBackupDir); err == nil {
|
if acquisFiles, err := filepath.Glob(acquisBackupDir); err == nil {
|
||||||
for _, acquisFile := range acquisFiles {
|
for _, acquisFile := range acquisFiles {
|
||||||
targetFname, err := filepath.Abs(cfg.Crowdsec.AcquisitionDirPath + "/" + filepath.Base(acquisFile))
|
targetFname, err := filepath.Abs(csConfig.Crowdsec.AcquisitionDirPath + "/" + filepath.Base(acquisFile))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("while saving %s to %s: %w", acquisFile, targetFname, err)
|
return fmt.Errorf("while saving %s to %s: %w", acquisFile, targetFname, err)
|
||||||
}
|
}
|
||||||
|
@ -209,17 +227,17 @@ func (cli *cliConfig) restore(dirPath string) error {
|
||||||
log.Debugf("restoring %s to %s", acquisFile, targetFname)
|
log.Debugf("restoring %s to %s", acquisFile, targetFname)
|
||||||
|
|
||||||
if err = CopyFile(acquisFile, targetFname); err != nil {
|
if err = CopyFile(acquisFile, targetFname); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", acquisFile, targetFname, err)
|
return fmt.Errorf("failed copy %s to %s : %s", acquisFile, targetFname, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Crowdsec != nil && len(cfg.Crowdsec.AcquisitionFiles) > 0 {
|
if csConfig.Crowdsec != nil && len(csConfig.Crowdsec.AcquisitionFiles) > 0 {
|
||||||
for _, acquisFile := range cfg.Crowdsec.AcquisitionFiles {
|
for _, acquisFile := range csConfig.Crowdsec.AcquisitionFiles {
|
||||||
log.Infof("backup filepath from dir -> %s", acquisFile)
|
log.Infof("backup filepath from dir -> %s", acquisFile)
|
||||||
|
|
||||||
// if it was the default one, it has already been backed up
|
// if it was the default one, it has already been backed up
|
||||||
if cfg.Crowdsec.AcquisitionFilePath == acquisFile {
|
if csConfig.Crowdsec.AcquisitionFilePath == acquisFile {
|
||||||
log.Infof("skip this one")
|
log.Infof("skip this one")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -230,22 +248,37 @@ func (cli *cliConfig) restore(dirPath string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = CopyFile(acquisFile, targetFname); err != nil {
|
if err = CopyFile(acquisFile, targetFname); err != nil {
|
||||||
return fmt.Errorf("failed copy %s to %s: %w", acquisFile, targetFname, err)
|
return fmt.Errorf("failed copy %s to %s : %s", acquisFile, targetFname, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Saved acquis %s to %s", acquisFile, targetFname)
|
log.Infof("Saved acquis %s to %s", acquisFile, targetFname)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = cli.restoreHub(dirPath); err != nil {
|
if err = restoreHub(dirPath); err != nil {
|
||||||
return fmt.Errorf("failed to restore hub config: %w", err)
|
return fmt.Errorf("failed to restore hub config : %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliConfig) newRestoreCmd() *cobra.Command {
|
func runConfigRestore(cmd *cobra.Command, args []string) error {
|
||||||
cmd := &cobra.Command{
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
oldBackup, err := flags.GetBool("old-backup")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := restoreConfigFromDirectory(args[0], oldBackup); err != nil {
|
||||||
|
return fmt.Errorf("failed to restore config from %s: %w", args[0], err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConfigRestoreCmd() *cobra.Command {
|
||||||
|
cmdConfigRestore := &cobra.Command{
|
||||||
Use: `restore "directory"`,
|
Use: `restore "directory"`,
|
||||||
Short: `Restore config in backup "directory"`,
|
Short: `Restore config in backup "directory"`,
|
||||||
Long: `Restore the crowdsec configuration from specified backup "directory" including:
|
Long: `Restore the crowdsec configuration from specified backup "directory" including:
|
||||||
|
@ -258,16 +291,11 @@ func (cli *cliConfig) newRestoreCmd() *cobra.Command {
|
||||||
- Backup of API credentials (local API and online API)`,
|
- Backup of API credentials (local API and online API)`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: runConfigRestore,
|
||||||
dirPath := args[0]
|
|
||||||
|
|
||||||
if err := cli.restore(dirPath); err != nil {
|
|
||||||
return fmt.Errorf("failed to restore config from %s: %w", dirPath, err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
flags := cmdConfigRestore.Flags()
|
||||||
},
|
flags.BoolP("old-backup", "", false, "To use when you are upgrading crowdsec v0.X to v1.X and you need to restore backup from v0.X")
|
||||||
}
|
|
||||||
|
|
||||||
return cmd
|
return cmdConfigRestore
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,15 +10,13 @@ import (
|
||||||
"github.com/sanity-io/litter"
|
"github.com/sanity-io/litter"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
|
"github.com/crowdsecurity/crowdsec/pkg/exprhelpers"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cli *cliConfig) showKey(key string) error {
|
func showConfigKey(key string) error {
|
||||||
cfg := cli.cfg()
|
|
||||||
|
|
||||||
type Env struct {
|
type Env struct {
|
||||||
Config *csconfig.Config
|
Config *csconfig.Config
|
||||||
}
|
}
|
||||||
|
@ -26,21 +24,20 @@ func (cli *cliConfig) showKey(key string) error {
|
||||||
opts := []expr.Option{}
|
opts := []expr.Option{}
|
||||||
opts = append(opts, exprhelpers.GetExprOptions(map[string]interface{}{})...)
|
opts = append(opts, exprhelpers.GetExprOptions(map[string]interface{}{})...)
|
||||||
opts = append(opts, expr.Env(Env{}))
|
opts = append(opts, expr.Env(Env{}))
|
||||||
|
|
||||||
program, err := expr.Compile(key, opts...)
|
program, err := expr.Compile(key, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
output, err := expr.Run(program, Env{Config: cfg})
|
output, err := expr.Run(program, Env{Config: csConfig})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
switch cfg.Cscli.Output {
|
switch csConfig.Cscli.Output {
|
||||||
case "human", "raw":
|
case "human", "raw":
|
||||||
// Don't use litter for strings, it adds quotes
|
// Don't use litter for strings, it adds quotes
|
||||||
// that would break compatibility with previous versions
|
// that we didn't have before
|
||||||
switch output.(type) {
|
switch output.(type) {
|
||||||
case string:
|
case string:
|
||||||
fmt.Println(output)
|
fmt.Println(output)
|
||||||
|
@ -53,14 +50,12 @@ func (cli *cliConfig) showKey(key string) error {
|
||||||
return fmt.Errorf("failed to marshal configuration: %w", err)
|
return fmt.Errorf("failed to marshal configuration: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(data))
|
fmt.Printf("%s\n", string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliConfig) template() string {
|
var configShowTemplate = `Global:
|
||||||
return `Global:
|
|
||||||
|
|
||||||
{{- if .ConfigPaths }}
|
{{- if .ConfigPaths }}
|
||||||
- Configuration Folder : {{.ConfigPaths.ConfigDir}}
|
- Configuration Folder : {{.ConfigPaths.ConfigDir}}
|
||||||
|
@ -103,7 +98,6 @@ API Client:
|
||||||
{{- if .API.Server }}
|
{{- if .API.Server }}
|
||||||
Local API Server{{if and .API.Server.Enable (not (ValueBool .API.Server.Enable))}} (disabled){{end}}:
|
Local API Server{{if and .API.Server.Enable (not (ValueBool .API.Server.Enable))}} (disabled){{end}}:
|
||||||
- Listen URL : {{.API.Server.ListenURI}}
|
- Listen URL : {{.API.Server.ListenURI}}
|
||||||
- Listen Socket : {{.API.Server.ListenSocket}}
|
|
||||||
- Profile File : {{.API.Server.ProfilesPath}}
|
- Profile File : {{.API.Server.ProfilesPath}}
|
||||||
|
|
||||||
{{- if .API.Server.TLS }}
|
{{- if .API.Server.TLS }}
|
||||||
|
@ -185,12 +179,25 @@ Central API:
|
||||||
{{- end }}
|
{{- end }}
|
||||||
{{- end }}
|
{{- end }}
|
||||||
`
|
`
|
||||||
|
|
||||||
|
func runConfigShow(cmd *cobra.Command, args []string) error {
|
||||||
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
if err := csConfig.LoadAPIClient(); err != nil {
|
||||||
|
log.Errorf("failed to load API client configuration: %s", err)
|
||||||
|
// don't return, we can still show the configuration
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliConfig) show() error {
|
key, err := flags.GetString("key")
|
||||||
cfg := cli.cfg()
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
switch cfg.Cscli.Output {
|
if key != "" {
|
||||||
|
return showConfigKey(key)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch csConfig.Cscli.Output {
|
||||||
case "human":
|
case "human":
|
||||||
// The tests on .Enable look funny because the option has a true default which has
|
// The tests on .Enable look funny because the option has a true default which has
|
||||||
// not been set yet (we don't really load the LAPI) and go templates don't dereference
|
// not been set yet (we don't really load the LAPI) and go templates don't dereference
|
||||||
|
@ -200,59 +207,44 @@ func (cli *cliConfig) show() error {
|
||||||
"ValueBool": func(b *bool) bool { return b!=nil && *b },
|
"ValueBool": func(b *bool) bool { return b!=nil && *b },
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp, err := template.New("config").Funcs(funcs).Parse(cli.template())
|
tmp, err := template.New("config").Funcs(funcs).Parse(configShowTemplate)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
err = tmp.Execute(os.Stdout, csConfig)
|
||||||
err = tmp.Execute(os.Stdout, cfg)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case "json":
|
case "json":
|
||||||
data, err := json.MarshalIndent(cfg, "", " ")
|
data, err := json.MarshalIndent(csConfig, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to marshal configuration: %w", err)
|
return fmt.Errorf("failed to marshal configuration: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(data))
|
fmt.Printf("%s\n", string(data))
|
||||||
case "raw":
|
case "raw":
|
||||||
data, err := yaml.Marshal(cfg)
|
data, err := yaml.Marshal(csConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to marshal configuration: %w", err)
|
return fmt.Errorf("failed to marshal configuration: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(data))
|
fmt.Printf("%s\n", string(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliConfig) newShowCmd() *cobra.Command {
|
func NewConfigShowCmd() *cobra.Command {
|
||||||
var key string
|
cmdConfigShow := &cobra.Command{
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "show",
|
Use: "show",
|
||||||
Short: "Displays current config",
|
Short: "Displays current config",
|
||||||
Long: `Displays the current cli configuration.`,
|
Long: `Displays the current cli configuration.`,
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: runConfigShow,
|
||||||
if err := cli.cfg().LoadAPIClient(); err != nil {
|
|
||||||
log.Errorf("failed to load API client configuration: %s", err)
|
|
||||||
// don't return, we can still show the configuration
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if key != "" {
|
flags := cmdConfigShow.Flags()
|
||||||
return cli.showKey(key)
|
flags.StringP("key", "", "", "Display only this value (Config.API.Server.ListenURI)")
|
||||||
}
|
|
||||||
|
|
||||||
return cli.show()
|
return cmdConfigShow
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
flags := cmd.Flags()
|
|
||||||
flags.StringVarP(&key, "key", "", "", "Display only this value (Config.API.Server.ListenURI)")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,21 +6,19 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cli *cliConfig) showYAML() error {
|
func runConfigShowYAML(cmd *cobra.Command, args []string) error {
|
||||||
fmt.Println(mergedConfig)
|
fmt.Println(mergedConfig)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliConfig) newShowYAMLCmd() *cobra.Command {
|
func NewConfigShowYAMLCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmdConfigShow := &cobra.Command{
|
||||||
Use: "show-yaml",
|
Use: "show-yaml",
|
||||||
Short: "Displays merged config.yaml + config.yaml.local",
|
Short: "Displays merged config.yaml + config.yaml.local",
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: runConfigShowYAML,
|
||||||
return cli.showYAML()
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdConfigShow
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,9 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/go-openapi/strfmt"
|
"github.com/go-openapi/strfmt"
|
||||||
|
@ -20,60 +17,40 @@ import (
|
||||||
"github.com/crowdsecurity/go-cs-lib/ptr"
|
"github.com/crowdsecurity/go-cs-lib/ptr"
|
||||||
"github.com/crowdsecurity/go-cs-lib/version"
|
"github.com/crowdsecurity/go-cs-lib/version"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type cliConsole struct {
|
func NewConsoleCmd() *cobra.Command {
|
||||||
cfg configGetter
|
var cmdConsole = &cobra.Command{
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLIConsole(cfg configGetter) *cliConsole {
|
|
||||||
return &cliConsole{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliConsole) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "console [action]",
|
Use: "console [action]",
|
||||||
Short: "Manage interaction with Crowdsec console (https://app.crowdsec.net)",
|
Short: "Manage interaction with Crowdsec console (https://app.crowdsec.net)",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
if err := require.LAPI(csConfig); err != nil {
|
||||||
if err := require.LAPI(cfg); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := require.CAPI(cfg); err != nil {
|
if err := require.CAPI(csConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := require.CAPIRegistered(cfg); err != nil {
|
if err := require.CAPIRegistered(csConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.newEnrollCmd())
|
|
||||||
cmd.AddCommand(cli.newEnableCmd())
|
|
||||||
cmd.AddCommand(cli.newDisableCmd())
|
|
||||||
cmd.AddCommand(cli.newStatusCmd())
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliConsole) newEnrollCmd() *cobra.Command {
|
|
||||||
name := ""
|
name := ""
|
||||||
overwrite := false
|
overwrite := false
|
||||||
tags := []string{}
|
tags := []string{}
|
||||||
opts := []string{}
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmdEnroll := &cobra.Command{
|
||||||
Use: "enroll [enroll-key]",
|
Use: "enroll [enroll-key]",
|
||||||
Short: "Enroll this instance to https://app.crowdsec.net [requires local API]",
|
Short: "Enroll this instance to https://app.crowdsec.net [requires local API]",
|
||||||
Long: `
|
Long: `
|
||||||
|
@ -81,115 +58,68 @@ Enroll this instance to https://app.crowdsec.net
|
||||||
|
|
||||||
You can get your enrollment key by creating an account on https://app.crowdsec.net.
|
You can get your enrollment key by creating an account on https://app.crowdsec.net.
|
||||||
After running this command your will need to validate the enrollment in the webapp.`,
|
After running this command your will need to validate the enrollment in the webapp.`,
|
||||||
Example: fmt.Sprintf(`cscli console enroll YOUR-ENROLL-KEY
|
Example: `cscli console enroll YOUR-ENROLL-KEY
|
||||||
cscli console enroll --name [instance_name] YOUR-ENROLL-KEY
|
cscli console enroll --name [instance_name] YOUR-ENROLL-KEY
|
||||||
cscli console enroll --name [instance_name] --tags [tag_1] --tags [tag_2] YOUR-ENROLL-KEY
|
cscli console enroll --name [instance_name] --tags [tag_1] --tags [tag_2] YOUR-ENROLL-KEY
|
||||||
cscli console enroll --enable context,manual YOUR-ENROLL-KEY
|
`,
|
||||||
|
|
||||||
valid options are : %s,all (see 'cscli console status' for details)`, strings.Join(csconfig.CONSOLE_CONFIGS, ",")),
|
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
password := strfmt.Password(csConfig.API.Server.OnlineClient.Credentials.Password)
|
||||||
password := strfmt.Password(cfg.API.Server.OnlineClient.Credentials.Password)
|
apiURL, err := url.Parse(csConfig.API.Server.OnlineClient.Credentials.URL)
|
||||||
|
|
||||||
apiURL, err := url.Parse(cfg.API.Server.OnlineClient.Credentials.URL)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not parse CAPI URL: %w", err)
|
return fmt.Errorf("could not parse CAPI URL: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hub, err := require.Hub(cfg, nil, nil)
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS)
|
scenarios, err := hub.GetInstalledItemNames(cwhub.SCENARIOS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get installed scenarios: %w", err)
|
return fmt.Errorf("failed to get installed scenarios: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(scenarios) == 0 {
|
if len(scenarios) == 0 {
|
||||||
scenarios = make([]string, 0)
|
scenarios = make([]string, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
enableOpts := []string{csconfig.SEND_MANUAL_SCENARIOS, csconfig.SEND_TAINTED_SCENARIOS}
|
|
||||||
if len(opts) != 0 {
|
|
||||||
for _, opt := range opts {
|
|
||||||
valid := false
|
|
||||||
if opt == "all" {
|
|
||||||
enableOpts = csconfig.CONSOLE_CONFIGS
|
|
||||||
break
|
|
||||||
}
|
|
||||||
for _, availableOpt := range csconfig.CONSOLE_CONFIGS {
|
|
||||||
if opt == availableOpt {
|
|
||||||
valid = true
|
|
||||||
enable := true
|
|
||||||
for _, enabledOpt := range enableOpts {
|
|
||||||
if opt == enabledOpt {
|
|
||||||
enable = false
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if enable {
|
|
||||||
enableOpts = append(enableOpts, opt)
|
|
||||||
}
|
|
||||||
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !valid {
|
|
||||||
return fmt.Errorf("option %s doesn't exist", opt)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
c, _ := apiclient.NewClient(&apiclient.Config{
|
c, _ := apiclient.NewClient(&apiclient.Config{
|
||||||
MachineID: cli.cfg().API.Server.OnlineClient.Credentials.Login,
|
MachineID: csConfig.API.Server.OnlineClient.Credentials.Login,
|
||||||
Password: password,
|
Password: password,
|
||||||
Scenarios: scenarios,
|
Scenarios: scenarios,
|
||||||
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
||||||
URL: apiURL,
|
URL: apiURL,
|
||||||
VersionPrefix: "v3",
|
VersionPrefix: "v3",
|
||||||
})
|
})
|
||||||
|
|
||||||
resp, err := c.Auth.EnrollWatcher(context.Background(), args[0], name, tags, overwrite)
|
resp, err := c.Auth.EnrollWatcher(context.Background(), args[0], name, tags, overwrite)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not enroll instance: %w", err)
|
return fmt.Errorf("could not enroll instance: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if resp.Response.StatusCode == 200 && !overwrite {
|
if resp.Response.StatusCode == 200 && !overwrite {
|
||||||
log.Warning("Instance already enrolled. You can use '--overwrite' to force enroll")
|
log.Warning("Instance already enrolled. You can use '--overwrite' to force enroll")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cli.setConsoleOpts(enableOpts, true); err != nil {
|
if err := SetConsoleOpts([]string{csconfig.SEND_MANUAL_SCENARIOS, csconfig.SEND_TAINTED_SCENARIOS}, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, opt := range enableOpts {
|
log.Info("Enabled tainted&manual alerts sharing, see 'cscli console status'.")
|
||||||
log.Infof("Enabled %s : %s", opt, csconfig.CONSOLE_CONFIGS_HELP[opt])
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Info("Watcher successfully enrolled. Visit https://app.crowdsec.net to accept it.")
|
log.Info("Watcher successfully enrolled. Visit https://app.crowdsec.net to accept it.")
|
||||||
log.Info("Please restart crowdsec after accepting the enrollment.")
|
log.Info("Please restart crowdsec after accepting the enrollment.")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmdEnroll.Flags().StringVarP(&name, "name", "n", "", "Name to display in the console")
|
||||||
|
cmdEnroll.Flags().BoolVarP(&overwrite, "overwrite", "", false, "Force enroll the instance")
|
||||||
|
cmdEnroll.Flags().StringSliceVarP(&tags, "tags", "t", tags, "Tags to display in the console")
|
||||||
|
cmdConsole.AddCommand(cmdEnroll)
|
||||||
|
|
||||||
flags := cmd.Flags()
|
var enableAll, disableAll bool
|
||||||
flags.StringVarP(&name, "name", "n", "", "Name to display in the console")
|
|
||||||
flags.BoolVarP(&overwrite, "overwrite", "", false, "Force enroll the instance")
|
|
||||||
flags.StringSliceVarP(&tags, "tags", "t", tags, "Tags to display in the console")
|
|
||||||
flags.StringSliceVarP(&opts, "enable", "e", opts, "Enable console options")
|
|
||||||
|
|
||||||
return cmd
|
cmdEnable := &cobra.Command{
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliConsole) newEnableCmd() *cobra.Command {
|
|
||||||
var enableAll bool
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "enable [option]",
|
Use: "enable [option]",
|
||||||
Short: "Enable a console option",
|
Short: "Enable a console option",
|
||||||
Example: "sudo cscli console enable tainted",
|
Example: "sudo cscli console enable tainted",
|
||||||
|
@ -197,36 +127,29 @@ func (cli *cliConsole) newEnableCmd() *cobra.Command {
|
||||||
Enable given information push to the central API. Allows to empower the console`,
|
Enable given information push to the central API. Allows to empower the console`,
|
||||||
ValidArgs: csconfig.CONSOLE_CONFIGS,
|
ValidArgs: csconfig.CONSOLE_CONFIGS,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if enableAll {
|
if enableAll {
|
||||||
if err := cli.setConsoleOpts(csconfig.CONSOLE_CONFIGS, true); err != nil {
|
if err := SetConsoleOpts(csconfig.CONSOLE_CONFIGS, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Infof("All features have been enabled successfully")
|
log.Infof("All features have been enabled successfully")
|
||||||
} else {
|
} else {
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return errors.New("you must specify at least one feature to enable")
|
return fmt.Errorf("you must specify at least one feature to enable")
|
||||||
}
|
}
|
||||||
if err := cli.setConsoleOpts(args, true); err != nil {
|
if err := SetConsoleOpts(args, true); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Infof("%v have been enabled", args)
|
log.Infof("%v have been enabled", args)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof(ReloadMessage())
|
log.Infof(ReloadMessage())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.Flags().BoolVarP(&enableAll, "all", "a", false, "Enable all console options")
|
cmdEnable.Flags().BoolVarP(&enableAll, "all", "a", false, "Enable all console options")
|
||||||
|
cmdConsole.AddCommand(cmdEnable)
|
||||||
|
|
||||||
return cmd
|
cmdDisable := &cobra.Command{
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliConsole) newDisableCmd() *cobra.Command {
|
|
||||||
var disableAll bool
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "disable [option]",
|
Use: "disable [option]",
|
||||||
Short: "Disable a console option",
|
Short: "Disable a console option",
|
||||||
Example: "sudo cscli console disable tainted",
|
Example: "sudo cscli console disable tainted",
|
||||||
|
@ -234,52 +157,47 @@ func (cli *cliConsole) newDisableCmd() *cobra.Command {
|
||||||
Disable given information push to the central API.`,
|
Disable given information push to the central API.`,
|
||||||
ValidArgs: csconfig.CONSOLE_CONFIGS,
|
ValidArgs: csconfig.CONSOLE_CONFIGS,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if disableAll {
|
if disableAll {
|
||||||
if err := cli.setConsoleOpts(csconfig.CONSOLE_CONFIGS, false); err != nil {
|
if err := SetConsoleOpts(csconfig.CONSOLE_CONFIGS, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Infof("All features have been disabled")
|
log.Infof("All features have been disabled")
|
||||||
} else {
|
} else {
|
||||||
if err := cli.setConsoleOpts(args, false); err != nil {
|
if err := SetConsoleOpts(args, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Infof("%v have been disabled", args)
|
log.Infof("%v have been disabled", args)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof(ReloadMessage())
|
log.Infof(ReloadMessage())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.Flags().BoolVarP(&disableAll, "all", "a", false, "Disable all console options")
|
cmdDisable.Flags().BoolVarP(&disableAll, "all", "a", false, "Disable all console options")
|
||||||
|
cmdConsole.AddCommand(cmdDisable)
|
||||||
|
|
||||||
return cmd
|
cmdConsoleStatus := &cobra.Command{
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliConsole) newStatusCmd() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "status",
|
Use: "status",
|
||||||
Short: "Shows status of the console options",
|
Short: "Shows status of the console options",
|
||||||
Example: `sudo cscli console status`,
|
Example: `sudo cscli console status`,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
switch csConfig.Cscli.Output {
|
||||||
consoleCfg := cfg.API.Server.ConsoleConfig
|
|
||||||
switch cfg.Cscli.Output {
|
|
||||||
case "human":
|
case "human":
|
||||||
cmdConsoleStatusTable(color.Output, *consoleCfg)
|
cmdConsoleStatusTable(color.Output, *csConfig)
|
||||||
case "json":
|
case "json":
|
||||||
|
c := csConfig.API.Server.ConsoleConfig
|
||||||
out := map[string](*bool){
|
out := map[string](*bool){
|
||||||
csconfig.SEND_MANUAL_SCENARIOS: consoleCfg.ShareManualDecisions,
|
csconfig.SEND_MANUAL_SCENARIOS: c.ShareManualDecisions,
|
||||||
csconfig.SEND_CUSTOM_SCENARIOS: consoleCfg.ShareCustomScenarios,
|
csconfig.SEND_CUSTOM_SCENARIOS: c.ShareCustomScenarios,
|
||||||
csconfig.SEND_TAINTED_SCENARIOS: consoleCfg.ShareTaintedScenarios,
|
csconfig.SEND_TAINTED_SCENARIOS: c.ShareTaintedScenarios,
|
||||||
csconfig.SEND_CONTEXT: consoleCfg.ShareContext,
|
csconfig.SEND_CONTEXT: c.ShareContext,
|
||||||
csconfig.CONSOLE_MANAGEMENT: consoleCfg.ConsoleManagement,
|
csconfig.CONSOLE_MANAGEMENT: c.ConsoleManagement,
|
||||||
}
|
}
|
||||||
data, err := json.MarshalIndent(out, "", " ")
|
data, err := json.MarshalIndent(out, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to marshal configuration: %w", err)
|
return fmt.Errorf("failed to marshal configuration: %s", err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(data))
|
fmt.Println(string(data))
|
||||||
case "raw":
|
case "raw":
|
||||||
|
@ -290,11 +208,11 @@ func (cli *cliConsole) newStatusCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
rows := [][]string{
|
rows := [][]string{
|
||||||
{csconfig.SEND_MANUAL_SCENARIOS, strconv.FormatBool(*consoleCfg.ShareManualDecisions)},
|
{csconfig.SEND_MANUAL_SCENARIOS, fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ShareManualDecisions)},
|
||||||
{csconfig.SEND_CUSTOM_SCENARIOS, strconv.FormatBool(*consoleCfg.ShareCustomScenarios)},
|
{csconfig.SEND_CUSTOM_SCENARIOS, fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ShareCustomScenarios)},
|
||||||
{csconfig.SEND_TAINTED_SCENARIOS, strconv.FormatBool(*consoleCfg.ShareTaintedScenarios)},
|
{csconfig.SEND_TAINTED_SCENARIOS, fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios)},
|
||||||
{csconfig.SEND_CONTEXT, strconv.FormatBool(*consoleCfg.ShareContext)},
|
{csconfig.SEND_CONTEXT, fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ShareContext)},
|
||||||
{csconfig.CONSOLE_MANAGEMENT, strconv.FormatBool(*consoleCfg.ConsoleManagement)},
|
{csconfig.CONSOLE_MANAGEMENT, fmt.Sprintf("%t", *csConfig.API.Server.ConsoleConfig.ConsoleManagement)},
|
||||||
}
|
}
|
||||||
for _, row := range rows {
|
for _, row := range rows {
|
||||||
err = csvwriter.Write(row)
|
err = csvwriter.Write(row)
|
||||||
|
@ -304,137 +222,131 @@ func (cli *cliConsole) newStatusCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
csvwriter.Flush()
|
csvwriter.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmdConsole.AddCommand(cmdConsoleStatus)
|
||||||
|
|
||||||
return cmd
|
return cmdConsole
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliConsole) dumpConfig() error {
|
func dumpConsoleConfig(c *csconfig.LocalApiServerCfg) error {
|
||||||
serverCfg := cli.cfg().API.Server
|
out, err := yaml.Marshal(c.ConsoleConfig)
|
||||||
|
|
||||||
out, err := yaml.Marshal(serverCfg.ConsoleConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("while marshaling ConsoleConfig (for %s): %w", serverCfg.ConsoleConfigPath, err)
|
return fmt.Errorf("while marshaling ConsoleConfig (for %s): %w", c.ConsoleConfigPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if serverCfg.ConsoleConfigPath == "" {
|
if c.ConsoleConfigPath == "" {
|
||||||
serverCfg.ConsoleConfigPath = csconfig.DefaultConsoleConfigFilePath
|
c.ConsoleConfigPath = csconfig.DefaultConsoleConfigFilePath
|
||||||
log.Debugf("Empty console_path, defaulting to %s", serverCfg.ConsoleConfigPath)
|
log.Debugf("Empty console_path, defaulting to %s", c.ConsoleConfigPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.WriteFile(serverCfg.ConsoleConfigPath, out, 0o600); err != nil {
|
if err := os.WriteFile(c.ConsoleConfigPath, out, 0600); err != nil {
|
||||||
return fmt.Errorf("while dumping console config to %s: %w", serverCfg.ConsoleConfigPath, err)
|
return fmt.Errorf("while dumping console config to %s: %w", c.ConsoleConfigPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliConsole) setConsoleOpts(args []string, wanted bool) error {
|
func SetConsoleOpts(args []string, wanted bool) error {
|
||||||
cfg := cli.cfg()
|
|
||||||
consoleCfg := cfg.API.Server.ConsoleConfig
|
|
||||||
|
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
switch arg {
|
switch arg {
|
||||||
case csconfig.CONSOLE_MANAGEMENT:
|
case csconfig.CONSOLE_MANAGEMENT:
|
||||||
|
if !fflag.PapiClient.IsEnabled() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
/*for each flag check if it's already set before setting it*/
|
/*for each flag check if it's already set before setting it*/
|
||||||
if consoleCfg.ConsoleManagement != nil {
|
if csConfig.API.Server.ConsoleConfig.ConsoleManagement != nil {
|
||||||
if *consoleCfg.ConsoleManagement == wanted {
|
if *csConfig.API.Server.ConsoleConfig.ConsoleManagement == wanted {
|
||||||
log.Debugf("%s already set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
|
log.Debugf("%s already set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
|
||||||
} else {
|
} else {
|
||||||
log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
|
log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
|
||||||
*consoleCfg.ConsoleManagement = wanted
|
*csConfig.API.Server.ConsoleConfig.ConsoleManagement = wanted
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
|
log.Infof("%s set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
|
||||||
consoleCfg.ConsoleManagement = ptr.Of(wanted)
|
csConfig.API.Server.ConsoleConfig.ConsoleManagement = ptr.Of(wanted)
|
||||||
}
|
}
|
||||||
|
if csConfig.API.Server.OnlineClient.Credentials != nil {
|
||||||
if cfg.API.Server.OnlineClient.Credentials != nil {
|
|
||||||
changed := false
|
changed := false
|
||||||
if wanted && cfg.API.Server.OnlineClient.Credentials.PapiURL == "" {
|
if wanted && csConfig.API.Server.OnlineClient.Credentials.PapiURL == "" {
|
||||||
changed = true
|
changed = true
|
||||||
cfg.API.Server.OnlineClient.Credentials.PapiURL = types.PAPIBaseURL
|
csConfig.API.Server.OnlineClient.Credentials.PapiURL = types.PAPIBaseURL
|
||||||
} else if !wanted && cfg.API.Server.OnlineClient.Credentials.PapiURL != "" {
|
} else if !wanted && csConfig.API.Server.OnlineClient.Credentials.PapiURL != "" {
|
||||||
changed = true
|
changed = true
|
||||||
cfg.API.Server.OnlineClient.Credentials.PapiURL = ""
|
csConfig.API.Server.OnlineClient.Credentials.PapiURL = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
if changed {
|
if changed {
|
||||||
fileContent, err := yaml.Marshal(cfg.API.Server.OnlineClient.Credentials)
|
fileContent, err := yaml.Marshal(csConfig.API.Server.OnlineClient.Credentials)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot marshal credentials: %w", err)
|
return fmt.Errorf("cannot marshal credentials: %s", err)
|
||||||
}
|
}
|
||||||
|
log.Infof("Updating credentials file: %s", csConfig.API.Server.OnlineClient.CredentialsFilePath)
|
||||||
log.Infof("Updating credentials file: %s", cfg.API.Server.OnlineClient.CredentialsFilePath)
|
err = os.WriteFile(csConfig.API.Server.OnlineClient.CredentialsFilePath, fileContent, 0600)
|
||||||
|
|
||||||
err = os.WriteFile(cfg.API.Server.OnlineClient.CredentialsFilePath, fileContent, 0o600)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot write credentials file: %w", err)
|
return fmt.Errorf("cannot write credentials file: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case csconfig.SEND_CUSTOM_SCENARIOS:
|
case csconfig.SEND_CUSTOM_SCENARIOS:
|
||||||
/*for each flag check if it's already set before setting it*/
|
/*for each flag check if it's already set before setting it*/
|
||||||
if consoleCfg.ShareCustomScenarios != nil {
|
if csConfig.API.Server.ConsoleConfig.ShareCustomScenarios != nil {
|
||||||
if *consoleCfg.ShareCustomScenarios == wanted {
|
if *csConfig.API.Server.ConsoleConfig.ShareCustomScenarios == wanted {
|
||||||
log.Debugf("%s already set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
|
log.Debugf("%s already set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
|
||||||
} else {
|
} else {
|
||||||
log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
|
log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
|
||||||
*consoleCfg.ShareCustomScenarios = wanted
|
*csConfig.API.Server.ConsoleConfig.ShareCustomScenarios = wanted
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
|
log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
|
||||||
consoleCfg.ShareCustomScenarios = ptr.Of(wanted)
|
csConfig.API.Server.ConsoleConfig.ShareCustomScenarios = ptr.Of(wanted)
|
||||||
}
|
}
|
||||||
case csconfig.SEND_TAINTED_SCENARIOS:
|
case csconfig.SEND_TAINTED_SCENARIOS:
|
||||||
/*for each flag check if it's already set before setting it*/
|
/*for each flag check if it's already set before setting it*/
|
||||||
if consoleCfg.ShareTaintedScenarios != nil {
|
if csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios != nil {
|
||||||
if *consoleCfg.ShareTaintedScenarios == wanted {
|
if *csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios == wanted {
|
||||||
log.Debugf("%s already set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
|
log.Debugf("%s already set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
|
||||||
} else {
|
} else {
|
||||||
log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
|
log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
|
||||||
*consoleCfg.ShareTaintedScenarios = wanted
|
*csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios = wanted
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
|
log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
|
||||||
consoleCfg.ShareTaintedScenarios = ptr.Of(wanted)
|
csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios = ptr.Of(wanted)
|
||||||
}
|
}
|
||||||
case csconfig.SEND_MANUAL_SCENARIOS:
|
case csconfig.SEND_MANUAL_SCENARIOS:
|
||||||
/*for each flag check if it's already set before setting it*/
|
/*for each flag check if it's already set before setting it*/
|
||||||
if consoleCfg.ShareManualDecisions != nil {
|
if csConfig.API.Server.ConsoleConfig.ShareManualDecisions != nil {
|
||||||
if *consoleCfg.ShareManualDecisions == wanted {
|
if *csConfig.API.Server.ConsoleConfig.ShareManualDecisions == wanted {
|
||||||
log.Debugf("%s already set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
|
log.Debugf("%s already set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
|
||||||
} else {
|
} else {
|
||||||
log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
|
log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
|
||||||
*consoleCfg.ShareManualDecisions = wanted
|
*csConfig.API.Server.ConsoleConfig.ShareManualDecisions = wanted
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
|
log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
|
||||||
consoleCfg.ShareManualDecisions = ptr.Of(wanted)
|
csConfig.API.Server.ConsoleConfig.ShareManualDecisions = ptr.Of(wanted)
|
||||||
}
|
}
|
||||||
case csconfig.SEND_CONTEXT:
|
case csconfig.SEND_CONTEXT:
|
||||||
/*for each flag check if it's already set before setting it*/
|
/*for each flag check if it's already set before setting it*/
|
||||||
if consoleCfg.ShareContext != nil {
|
if csConfig.API.Server.ConsoleConfig.ShareContext != nil {
|
||||||
if *consoleCfg.ShareContext == wanted {
|
if *csConfig.API.Server.ConsoleConfig.ShareContext == wanted {
|
||||||
log.Debugf("%s already set to %t", csconfig.SEND_CONTEXT, wanted)
|
log.Debugf("%s already set to %t", csconfig.SEND_CONTEXT, wanted)
|
||||||
} else {
|
} else {
|
||||||
log.Infof("%s set to %t", csconfig.SEND_CONTEXT, wanted)
|
log.Infof("%s set to %t", csconfig.SEND_CONTEXT, wanted)
|
||||||
*consoleCfg.ShareContext = wanted
|
*csConfig.API.Server.ConsoleConfig.ShareContext = wanted
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Infof("%s set to %t", csconfig.SEND_CONTEXT, wanted)
|
log.Infof("%s set to %t", csconfig.SEND_CONTEXT, wanted)
|
||||||
consoleCfg.ShareContext = ptr.Of(wanted)
|
csConfig.API.Server.ConsoleConfig.ShareContext = ptr.Of(wanted)
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unknown flag %s", arg)
|
return fmt.Errorf("unknown flag %s", arg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := cli.dumpConfig(); err != nil {
|
if err := dumpConsoleConfig(csConfig.API.Server); err != nil {
|
||||||
return fmt.Errorf("failed writing console config: %w", err)
|
return fmt.Errorf("failed writing console config: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -4,12 +4,12 @@ import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/aquasecurity/table"
|
"github.com/aquasecurity/table"
|
||||||
|
"github.com/enescakir/emoji"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/emoji"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func cmdConsoleStatusTable(out io.Writer, consoleCfg csconfig.ConsoleConfig) {
|
func cmdConsoleStatusTable(out io.Writer, csConfig csconfig.Config) {
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
|
|
||||||
|
@ -17,32 +17,43 @@ func cmdConsoleStatusTable(out io.Writer, consoleCfg csconfig.ConsoleConfig) {
|
||||||
t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
for _, option := range csconfig.CONSOLE_CONFIGS {
|
for _, option := range csconfig.CONSOLE_CONFIGS {
|
||||||
activated := emoji.CrossMark
|
|
||||||
|
|
||||||
switch option {
|
switch option {
|
||||||
case csconfig.SEND_CUSTOM_SCENARIOS:
|
case csconfig.SEND_CUSTOM_SCENARIOS:
|
||||||
if *consoleCfg.ShareCustomScenarios {
|
activated := string(emoji.CrossMark)
|
||||||
activated = emoji.CheckMarkButton
|
if *csConfig.API.Server.ConsoleConfig.ShareCustomScenarios {
|
||||||
}
|
activated = string(emoji.CheckMarkButton)
|
||||||
case csconfig.SEND_MANUAL_SCENARIOS:
|
|
||||||
if *consoleCfg.ShareManualDecisions {
|
|
||||||
activated = emoji.CheckMarkButton
|
|
||||||
}
|
|
||||||
case csconfig.SEND_TAINTED_SCENARIOS:
|
|
||||||
if *consoleCfg.ShareTaintedScenarios {
|
|
||||||
activated = emoji.CheckMarkButton
|
|
||||||
}
|
|
||||||
case csconfig.SEND_CONTEXT:
|
|
||||||
if *consoleCfg.ShareContext {
|
|
||||||
activated = emoji.CheckMarkButton
|
|
||||||
}
|
|
||||||
case csconfig.CONSOLE_MANAGEMENT:
|
|
||||||
if *consoleCfg.ConsoleManagement {
|
|
||||||
activated = emoji.CheckMarkButton
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.AddRow(option, activated, csconfig.CONSOLE_CONFIGS_HELP[option])
|
t.AddRow(option, activated, "Send alerts from custom scenarios to the console")
|
||||||
|
|
||||||
|
case csconfig.SEND_MANUAL_SCENARIOS:
|
||||||
|
activated := string(emoji.CrossMark)
|
||||||
|
if *csConfig.API.Server.ConsoleConfig.ShareManualDecisions {
|
||||||
|
activated = string(emoji.CheckMarkButton)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.AddRow(option, activated, "Send manual decisions to the console")
|
||||||
|
|
||||||
|
case csconfig.SEND_TAINTED_SCENARIOS:
|
||||||
|
activated := string(emoji.CrossMark)
|
||||||
|
if *csConfig.API.Server.ConsoleConfig.ShareTaintedScenarios {
|
||||||
|
activated = string(emoji.CheckMarkButton)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.AddRow(option, activated, "Send alerts from tainted scenarios to the console")
|
||||||
|
case csconfig.SEND_CONTEXT:
|
||||||
|
activated := string(emoji.CrossMark)
|
||||||
|
if *csConfig.API.Server.ConsoleConfig.ShareContext {
|
||||||
|
activated = string(emoji.CheckMarkButton)
|
||||||
|
}
|
||||||
|
t.AddRow(option, activated, "Send context with alerts to the console")
|
||||||
|
case csconfig.CONSOLE_MANAGEMENT:
|
||||||
|
activated := string(emoji.CrossMark)
|
||||||
|
if *csConfig.API.Server.ConsoleConfig.ConsoleManagement {
|
||||||
|
activated = string(emoji.CheckMarkButton)
|
||||||
|
}
|
||||||
|
t.AddRow(option, activated, "Receive decisions from console")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Render()
|
t.Render()
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
/*help to copy the file, ioutil doesn't offer the feature*/
|
/*help to copy the file, ioutil doesn't offer the feature*/
|
||||||
|
|
||||||
func copyFileContents(src, dst string) (err error) {
|
func copyFileContents(src, dst string) (err error) {
|
||||||
|
@ -17,66 +18,56 @@ func copyFileContents(src, dst string) (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer in.Close()
|
defer in.Close()
|
||||||
|
|
||||||
out, err := os.Create(dst)
|
out, err := os.Create(dst)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
cerr := out.Close()
|
cerr := out.Close()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = cerr
|
err = cerr
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if _, err = io.Copy(out, in); err != nil {
|
if _, err = io.Copy(out, in); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = out.Sync()
|
err = out.Sync()
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
/*copy the file, ioutile doesn't offer the feature*/
|
/*copy the file, ioutile doesn't offer the feature*/
|
||||||
func CopyFile(sourceSymLink, destinationFile string) error {
|
func CopyFile(sourceSymLink, destinationFile string) (err error) {
|
||||||
sourceFile, err := filepath.EvalSymlinks(sourceSymLink)
|
sourceFile, err := filepath.EvalSymlinks(sourceSymLink)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Infof("Not a symlink : %s", err)
|
log.Infof("Not a symlink : %s", err)
|
||||||
|
|
||||||
sourceFile = sourceSymLink
|
sourceFile = sourceSymLink
|
||||||
}
|
}
|
||||||
|
|
||||||
sourceFileStat, err := os.Stat(sourceFile)
|
sourceFileStat, err := os.Stat(sourceFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if !sourceFileStat.Mode().IsRegular() {
|
if !sourceFileStat.Mode().IsRegular() {
|
||||||
// cannot copy non-regular files (e.g., directories,
|
// cannot copy non-regular files (e.g., directories,
|
||||||
// symlinks, devices, etc.)
|
// symlinks, devices, etc.)
|
||||||
return fmt.Errorf("copyFile: non-regular source file %s (%q)", sourceFileStat.Name(), sourceFileStat.Mode().String())
|
return fmt.Errorf("copyFile: non-regular source file %s (%q)", sourceFileStat.Name(), sourceFileStat.Mode().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
destinationFileStat, err := os.Stat(destinationFile)
|
destinationFileStat, err := os.Stat(destinationFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if !os.IsNotExist(err) {
|
if !os.IsNotExist(err) {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if !(destinationFileStat.Mode().IsRegular()) {
|
if !(destinationFileStat.Mode().IsRegular()) {
|
||||||
return fmt.Errorf("copyFile: non-regular destination file %s (%q)", destinationFileStat.Name(), destinationFileStat.Mode().String())
|
return fmt.Errorf("copyFile: non-regular destination file %s (%q)", destinationFileStat.Name(), destinationFileStat.Mode().String())
|
||||||
}
|
}
|
||||||
|
|
||||||
if os.SameFile(sourceFileStat, destinationFileStat) {
|
if os.SameFile(sourceFileStat, destinationFileStat) {
|
||||||
return err
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = os.Link(sourceFile, destinationFile); err != nil {
|
if err = os.Link(sourceFile, destinationFile); err != nil {
|
||||||
err = copyFileContents(sourceFile, destinationFile)
|
err = copyFileContents(sourceFile, destinationFile)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,14 +19,15 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/metabase"
|
"github.com/crowdsecurity/crowdsec/pkg/metabase"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
metabaseUser = "crowdsec@crowdsec.net"
|
metabaseUser = "crowdsec@crowdsec.net"
|
||||||
metabasePassword string
|
metabasePassword string
|
||||||
metabaseDBPath string
|
metabaseDbPath string
|
||||||
metabaseConfigPath string
|
metabaseConfigPath string
|
||||||
metabaseConfigFolder = "metabase/"
|
metabaseConfigFolder = "metabase/"
|
||||||
metabaseConfigFile = "metabase.yaml"
|
metabaseConfigFile = "metabase.yaml"
|
||||||
|
@ -42,18 +43,9 @@ var (
|
||||||
// information needed to set up a random password on user's behalf
|
// information needed to set up a random password on user's behalf
|
||||||
)
|
)
|
||||||
|
|
||||||
type cliDashboard struct {
|
func NewDashboardCmd() *cobra.Command {
|
||||||
cfg configGetter
|
/* ---- UPDATE COMMAND */
|
||||||
}
|
var cmdDashboard = &cobra.Command{
|
||||||
|
|
||||||
func NewCLIDashboard(cfg configGetter) *cliDashboard {
|
|
||||||
return &cliDashboard{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliDashboard) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "dashboard [command]",
|
Use: "dashboard [command]",
|
||||||
Short: "Manage your metabase dashboard container [requires local API]",
|
Short: "Manage your metabase dashboard container [requires local API]",
|
||||||
Long: `Install/Start/Stop/Remove a metabase container exposing dashboard and metrics.
|
Long: `Install/Start/Stop/Remove a metabase container exposing dashboard and metrics.
|
||||||
|
@ -67,9 +59,8 @@ cscli dashboard start
|
||||||
cscli dashboard stop
|
cscli dashboard stop
|
||||||
cscli dashboard remove
|
cscli dashboard remove
|
||||||
`,
|
`,
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
if err := require.LAPI(csConfig); err != nil {
|
||||||
if err := require.LAPI(cfg); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,13 +68,13 @@ cscli dashboard remove
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
metabaseConfigFolderPath := filepath.Join(cfg.ConfigPaths.ConfigDir, metabaseConfigFolder)
|
metabaseConfigFolderPath := filepath.Join(csConfig.ConfigPaths.ConfigDir, metabaseConfigFolder)
|
||||||
metabaseConfigPath = filepath.Join(metabaseConfigFolderPath, metabaseConfigFile)
|
metabaseConfigPath = filepath.Join(metabaseConfigFolderPath, metabaseConfigFile)
|
||||||
if err := os.MkdirAll(metabaseConfigFolderPath, os.ModePerm); err != nil {
|
if err := os.MkdirAll(metabaseConfigFolderPath, os.ModePerm); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := require.DB(cfg); err != nil {
|
if err := require.DB(csConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -98,24 +89,23 @@ cscli dashboard remove
|
||||||
metabaseContainerID = oldContainerID
|
metabaseContainerID = oldContainerID
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.newSetupCmd())
|
cmdDashboard.AddCommand(NewDashboardSetupCmd())
|
||||||
cmd.AddCommand(cli.newStartCmd())
|
cmdDashboard.AddCommand(NewDashboardStartCmd())
|
||||||
cmd.AddCommand(cli.newStopCmd())
|
cmdDashboard.AddCommand(NewDashboardStopCmd())
|
||||||
cmd.AddCommand(cli.newShowPasswordCmd())
|
cmdDashboard.AddCommand(NewDashboardShowPasswordCmd())
|
||||||
cmd.AddCommand(cli.newRemoveCmd())
|
cmdDashboard.AddCommand(NewDashboardRemoveCmd())
|
||||||
|
|
||||||
return cmd
|
return cmdDashboard
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliDashboard) newSetupCmd() *cobra.Command {
|
func NewDashboardSetupCmd() *cobra.Command {
|
||||||
var force bool
|
var force bool
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
var cmdDashSetup = &cobra.Command{
|
||||||
Use: "setup",
|
Use: "setup",
|
||||||
Short: "Setup a metabase container.",
|
Short: "Setup a metabase container.",
|
||||||
Long: `Perform a metabase docker setup, download standard dashboards, create a fresh user and start the container`,
|
Long: `Perform a metabase docker setup, download standard dashboards, create a fresh user and start the container`,
|
||||||
|
@ -126,9 +116,9 @@ cscli dashboard setup
|
||||||
cscli dashboard setup --listen 0.0.0.0
|
cscli dashboard setup --listen 0.0.0.0
|
||||||
cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
|
cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
|
||||||
`,
|
`,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if metabaseDBPath == "" {
|
if metabaseDbPath == "" {
|
||||||
metabaseDBPath = cli.cfg().ConfigPaths.DataDir
|
metabaseDbPath = csConfig.ConfigPaths.DataDir
|
||||||
}
|
}
|
||||||
|
|
||||||
if metabasePassword == "" {
|
if metabasePassword == "" {
|
||||||
|
@ -149,10 +139,10 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err = cli.chownDatabase(dockerGroup.Gid); err != nil {
|
if err = chownDatabase(dockerGroup.Gid); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
mb, err := metabase.SetupMetabase(cli.cfg().API.Server.DbConfig, metabaseListenAddress, metabaseListenPort, metabaseUser, metabasePassword, metabaseDBPath, dockerGroup.Gid, metabaseContainerID, metabaseImage)
|
mb, err := metabase.SetupMetabase(csConfig.API.Server.DbConfig, metabaseListenAddress, metabaseListenPort, metabaseUser, metabasePassword, metabaseDbPath, dockerGroup.Gid, metabaseContainerID, metabaseImage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -165,32 +155,29 @@ cscli dashboard setup -l 0.0.0.0 -p 443 --password <password>
|
||||||
fmt.Printf("\tURL : '%s'\n", mb.Config.ListenURL)
|
fmt.Printf("\tURL : '%s'\n", mb.Config.ListenURL)
|
||||||
fmt.Printf("\tusername : '%s'\n", mb.Config.Username)
|
fmt.Printf("\tusername : '%s'\n", mb.Config.Username)
|
||||||
fmt.Printf("\tpassword : '%s'\n", mb.Config.Password)
|
fmt.Printf("\tpassword : '%s'\n", mb.Config.Password)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmdDashSetup.Flags().BoolVarP(&force, "force", "f", false, "Force setup : override existing files")
|
||||||
|
cmdDashSetup.Flags().StringVarP(&metabaseDbPath, "dir", "d", "", "Shared directory with metabase container")
|
||||||
|
cmdDashSetup.Flags().StringVarP(&metabaseListenAddress, "listen", "l", metabaseListenAddress, "Listen address of container")
|
||||||
|
cmdDashSetup.Flags().StringVar(&metabaseImage, "metabase-image", metabaseImage, "Metabase image to use")
|
||||||
|
cmdDashSetup.Flags().StringVarP(&metabaseListenPort, "port", "p", metabaseListenPort, "Listen port of container")
|
||||||
|
cmdDashSetup.Flags().BoolVarP(&forceYes, "yes", "y", false, "force yes")
|
||||||
|
//cmdDashSetup.Flags().StringVarP(&metabaseUser, "user", "u", "crowdsec@crowdsec.net", "metabase user")
|
||||||
|
cmdDashSetup.Flags().StringVar(&metabasePassword, "password", "", "metabase password")
|
||||||
|
|
||||||
flags := cmd.Flags()
|
return cmdDashSetup
|
||||||
flags.BoolVarP(&force, "force", "f", false, "Force setup : override existing files")
|
|
||||||
flags.StringVarP(&metabaseDBPath, "dir", "d", "", "Shared directory with metabase container")
|
|
||||||
flags.StringVarP(&metabaseListenAddress, "listen", "l", metabaseListenAddress, "Listen address of container")
|
|
||||||
flags.StringVar(&metabaseImage, "metabase-image", metabaseImage, "Metabase image to use")
|
|
||||||
flags.StringVarP(&metabaseListenPort, "port", "p", metabaseListenPort, "Listen port of container")
|
|
||||||
flags.BoolVarP(&forceYes, "yes", "y", false, "force yes")
|
|
||||||
// flags.StringVarP(&metabaseUser, "user", "u", "crowdsec@crowdsec.net", "metabase user")
|
|
||||||
flags.StringVar(&metabasePassword, "password", "", "metabase password")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliDashboard) newStartCmd() *cobra.Command {
|
func NewDashboardStartCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
var cmdDashStart = &cobra.Command{
|
||||||
Use: "start",
|
Use: "start",
|
||||||
Short: "Start the metabase container.",
|
Short: "Start the metabase container.",
|
||||||
Long: `Stats the metabase container using docker.`,
|
Long: `Stats the metabase container using docker.`,
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
mb, err := metabase.NewMetabase(metabaseConfigPath, metabaseContainerID)
|
mb, err := metabase.NewMetabase(metabaseConfigPath, metabaseContainerID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -204,57 +191,51 @@ func (cli *cliDashboard) newStartCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
log.Infof("Started metabase")
|
log.Infof("Started metabase")
|
||||||
log.Infof("url : http://%s:%s", mb.Config.ListenAddr, mb.Config.ListenPort)
|
log.Infof("url : http://%s:%s", mb.Config.ListenAddr, mb.Config.ListenPort)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmdDashStart.Flags().BoolVarP(&forceYes, "yes", "y", false, "force yes")
|
||||||
cmd.Flags().BoolVarP(&forceYes, "yes", "y", false, "force yes")
|
return cmdDashStart
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliDashboard) newStopCmd() *cobra.Command {
|
func NewDashboardStopCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
var cmdDashStop = &cobra.Command{
|
||||||
Use: "stop",
|
Use: "stop",
|
||||||
Short: "Stops the metabase container.",
|
Short: "Stops the metabase container.",
|
||||||
Long: `Stops the metabase container using docker.`,
|
Long: `Stops the metabase container using docker.`,
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if err := metabase.StopContainer(metabaseContainerID); err != nil {
|
if err := metabase.StopContainer(metabaseContainerID); err != nil {
|
||||||
return fmt.Errorf("unable to stop container '%s': %s", metabaseContainerID, err)
|
return fmt.Errorf("unable to stop container '%s': %s", metabaseContainerID, err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
return cmdDashStop
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliDashboard) newShowPasswordCmd() *cobra.Command {
|
func NewDashboardShowPasswordCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{Use: "show-password",
|
var cmdDashShowPassword = &cobra.Command{Use: "show-password",
|
||||||
Short: "displays password of metabase.",
|
Short: "displays password of metabase.",
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
m := metabase.Metabase{}
|
m := metabase.Metabase{}
|
||||||
if err := m.LoadConfig(metabaseConfigPath); err != nil {
|
if err := m.LoadConfig(metabaseConfigPath); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
log.Printf("'%s'", m.Config.Password)
|
log.Printf("'%s'", m.Config.Password)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
return cmdDashShowPassword
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliDashboard) newRemoveCmd() *cobra.Command {
|
func NewDashboardRemoveCmd() *cobra.Command {
|
||||||
var force bool
|
var force bool
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
var cmdDashRemove = &cobra.Command{
|
||||||
Use: "remove",
|
Use: "remove",
|
||||||
Short: "removes the metabase container.",
|
Short: "removes the metabase container.",
|
||||||
Long: `removes the metabase container using docker.`,
|
Long: `removes the metabase container using docker.`,
|
||||||
|
@ -264,7 +245,7 @@ func (cli *cliDashboard) newRemoveCmd() *cobra.Command {
|
||||||
cscli dashboard remove
|
cscli dashboard remove
|
||||||
cscli dashboard remove --force
|
cscli dashboard remove --force
|
||||||
`,
|
`,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if !forceYes {
|
if !forceYes {
|
||||||
var answer bool
|
var answer bool
|
||||||
prompt := &survey.Confirm{
|
prompt := &survey.Confirm{
|
||||||
|
@ -301,8 +282,8 @@ cscli dashboard remove --force
|
||||||
}
|
}
|
||||||
log.Infof("container %s stopped & removed", metabaseContainerID)
|
log.Infof("container %s stopped & removed", metabaseContainerID)
|
||||||
}
|
}
|
||||||
log.Debugf("Removing metabase db %s", cli.cfg().ConfigPaths.DataDir)
|
log.Debugf("Removing metabase db %s", csConfig.ConfigPaths.DataDir)
|
||||||
if err := metabase.RemoveDatabase(cli.cfg().ConfigPaths.DataDir); err != nil {
|
if err := metabase.RemoveDatabase(csConfig.ConfigPaths.DataDir); err != nil {
|
||||||
log.Warnf("failed to remove metabase internal db : %s", err)
|
log.Warnf("failed to remove metabase internal db : %s", err)
|
||||||
}
|
}
|
||||||
if force {
|
if force {
|
||||||
|
@ -316,25 +297,20 @@ cscli dashboard remove --force
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmdDashRemove.Flags().BoolVarP(&force, "force", "f", false, "Remove also the metabase image")
|
||||||
|
cmdDashRemove.Flags().BoolVarP(&forceYes, "yes", "y", false, "force yes")
|
||||||
|
|
||||||
flags := cmd.Flags()
|
return cmdDashRemove
|
||||||
flags.BoolVarP(&force, "force", "f", false, "Remove also the metabase image")
|
|
||||||
flags.BoolVarP(&forceYes, "yes", "y", false, "force yes")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func passwordIsValid(password string) bool {
|
func passwordIsValid(password string) bool {
|
||||||
hasDigit := false
|
hasDigit := false
|
||||||
|
|
||||||
for _, j := range password {
|
for _, j := range password {
|
||||||
if unicode.IsDigit(j) {
|
if unicode.IsDigit(j) {
|
||||||
hasDigit = true
|
hasDigit = true
|
||||||
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -342,8 +318,8 @@ func passwordIsValid(password string) bool {
|
||||||
if !hasDigit || len(password) < 6 {
|
if !hasDigit || len(password) < 6 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkSystemMemory(forceYes *bool) error {
|
func checkSystemMemory(forceYes *bool) error {
|
||||||
|
@ -351,10 +327,8 @@ func checkSystemMemory(forceYes *bool) error {
|
||||||
if totMem >= uint64(math.Pow(2, 30)) {
|
if totMem >= uint64(math.Pow(2, 30)) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !*forceYes {
|
if !*forceYes {
|
||||||
var answer bool
|
var answer bool
|
||||||
|
|
||||||
prompt := &survey.Confirm{
|
prompt := &survey.Confirm{
|
||||||
Message: "Metabase requires 1-2GB of RAM, your system is below this requirement continue ?",
|
Message: "Metabase requires 1-2GB of RAM, your system is below this requirement continue ?",
|
||||||
Default: true,
|
Default: true,
|
||||||
|
@ -362,16 +336,12 @@ func checkSystemMemory(forceYes *bool) error {
|
||||||
if err := survey.AskOne(prompt, &answer); err != nil {
|
if err := survey.AskOne(prompt, &answer); err != nil {
|
||||||
return fmt.Errorf("unable to ask about RAM check: %s", err)
|
return fmt.Errorf("unable to ask about RAM check: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !answer {
|
if !answer {
|
||||||
return fmt.Errorf("user stated no to continue")
|
return fmt.Errorf("user stated no to continue")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Warn("Metabase requires 1-2GB of RAM, your system is below this requirement")
|
log.Warn("Metabase requires 1-2GB of RAM, your system is below this requirement")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -379,32 +349,25 @@ func warnIfNotLoopback(addr string) {
|
||||||
if addr == "127.0.0.1" || addr == "::1" {
|
if addr == "127.0.0.1" || addr == "::1" {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Warnf("You are potentially exposing your metabase port to the internet (addr: %s), please consider using a reverse proxy", addr)
|
log.Warnf("You are potentially exposing your metabase port to the internet (addr: %s), please consider using a reverse proxy", addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
func disclaimer(forceYes *bool) error {
|
func disclaimer(forceYes *bool) error {
|
||||||
if !*forceYes {
|
if !*forceYes {
|
||||||
var answer bool
|
var answer bool
|
||||||
|
|
||||||
prompt := &survey.Confirm{
|
prompt := &survey.Confirm{
|
||||||
Message: "CrowdSec takes no responsibility for the security of your metabase instance. Do you accept these responsibilities ?",
|
Message: "CrowdSec takes no responsibility for the security of your metabase instance. Do you accept these responsibilities ?",
|
||||||
Default: true,
|
Default: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := survey.AskOne(prompt, &answer); err != nil {
|
if err := survey.AskOne(prompt, &answer); err != nil {
|
||||||
return fmt.Errorf("unable to ask to question: %s", err)
|
return fmt.Errorf("unable to ask to question: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !answer {
|
if !answer {
|
||||||
return fmt.Errorf("user stated no to responsibilities")
|
return fmt.Errorf("user stated no to responsibilities")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Warn("CrowdSec takes no responsibility for the security of your metabase instance. You used force yes, so you accept this disclaimer")
|
log.Warn("CrowdSec takes no responsibility for the security of your metabase instance. You used force yes, so you accept this disclaimer")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -413,24 +376,19 @@ func checkGroups(forceYes *bool) (*user.Group, error) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return dockerGroup, nil
|
return dockerGroup, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !*forceYes {
|
if !*forceYes {
|
||||||
var answer bool
|
var answer bool
|
||||||
|
|
||||||
prompt := &survey.Confirm{
|
prompt := &survey.Confirm{
|
||||||
Message: fmt.Sprintf("For metabase docker to be able to access SQLite file we need to add a new group called '%s' to the system, is it ok for you ?", crowdsecGroup),
|
Message: fmt.Sprintf("For metabase docker to be able to access SQLite file we need to add a new group called '%s' to the system, is it ok for you ?", crowdsecGroup),
|
||||||
Default: true,
|
Default: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := survey.AskOne(prompt, &answer); err != nil {
|
if err := survey.AskOne(prompt, &answer); err != nil {
|
||||||
return dockerGroup, fmt.Errorf("unable to ask to question: %s", err)
|
return dockerGroup, fmt.Errorf("unable to ask to question: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !answer {
|
if !answer {
|
||||||
return dockerGroup, fmt.Errorf("unable to continue without creating '%s' group", crowdsecGroup)
|
return dockerGroup, fmt.Errorf("unable to continue without creating '%s' group", crowdsecGroup)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
groupAddCmd, err := exec.LookPath("groupadd")
|
groupAddCmd, err := exec.LookPath("groupadd")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dockerGroup, fmt.Errorf("unable to find 'groupadd' command, can't continue")
|
return dockerGroup, fmt.Errorf("unable to find 'groupadd' command, can't continue")
|
||||||
|
@ -440,28 +398,23 @@ func checkGroups(forceYes *bool) (*user.Group, error) {
|
||||||
if err := groupAdd.Run(); err != nil {
|
if err := groupAdd.Run(); err != nil {
|
||||||
return dockerGroup, fmt.Errorf("unable to add group '%s': %s", dockerGroup, err)
|
return dockerGroup, fmt.Errorf("unable to add group '%s': %s", dockerGroup, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return user.LookupGroup(crowdsecGroup)
|
return user.LookupGroup(crowdsecGroup)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliDashboard) chownDatabase(gid string) error {
|
func chownDatabase(gid string) error {
|
||||||
cfg := cli.cfg()
|
|
||||||
intID, err := strconv.Atoi(gid)
|
intID, err := strconv.Atoi(gid)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to convert group ID to int: %s", err)
|
return fmt.Errorf("unable to convert group ID to int: %s", err)
|
||||||
}
|
}
|
||||||
|
if stat, err := os.Stat(csConfig.DbConfig.DbPath); !os.IsNotExist(err) {
|
||||||
if stat, err := os.Stat(cfg.DbConfig.DbPath); !os.IsNotExist(err) {
|
|
||||||
info := stat.Sys()
|
info := stat.Sys()
|
||||||
if err := os.Chown(cfg.DbConfig.DbPath, int(info.(*syscall.Stat_t).Uid), intID); err != nil {
|
if err := os.Chown(csConfig.DbConfig.DbPath, int(info.(*syscall.Stat_t).Uid), intID); err != nil {
|
||||||
return fmt.Errorf("unable to chown sqlite db file '%s': %s", cfg.DbConfig.DbPath, err)
|
return fmt.Errorf("unable to chown sqlite db file '%s': %s", csConfig.DbConfig.DbPath, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if csConfig.DbConfig.Type == "sqlite" && csConfig.DbConfig.UseWal != nil && *csConfig.DbConfig.UseWal {
|
||||||
if cfg.DbConfig.Type == "sqlite" && cfg.DbConfig.UseWal != nil && *cfg.DbConfig.UseWal {
|
|
||||||
for _, ext := range []string{"-wal", "-shm"} {
|
for _, ext := range []string{"-wal", "-shm"} {
|
||||||
file := cfg.DbConfig.DbPath + ext
|
file := csConfig.DbConfig.DbPath + ext
|
||||||
if stat, err := os.Stat(file); !os.IsNotExist(err) {
|
if stat, err := os.Stat(file); !os.IsNotExist(err) {
|
||||||
info := stat.Sys()
|
info := stat.Sys()
|
||||||
if err := os.Chown(file, int(info.(*syscall.Stat_t).Uid), intID); err != nil {
|
if err := os.Chown(file, int(info.(*syscall.Stat_t).Uid), intID); err != nil {
|
||||||
|
@ -470,6 +423,5 @@ func (cli *cliDashboard) chownDatabase(gid string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,24 +9,14 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
type cliDashboard struct{
|
func NewDashboardCmd() *cobra.Command {
|
||||||
cfg configGetter
|
var cmdDashboard = &cobra.Command{
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLIDashboard(cfg configGetter) *cliDashboard {
|
|
||||||
return &cliDashboard{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli cliDashboard) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "dashboard",
|
Use: "dashboard",
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
Run: func(_ *cobra.Command, _ []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
log.Infof("Dashboard command is disabled on %s", runtime.GOOS)
|
log.Infof("Dashboard command is disabled on %s", runtime.GOOS)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdDashboard
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
@ -26,7 +25,7 @@ import (
|
||||||
|
|
||||||
var Client *apiclient.ApiClient
|
var Client *apiclient.ApiClient
|
||||||
|
|
||||||
func (cli *cliDecisions) decisionsToTable(alerts *models.GetAlertsResponse, printMachine bool) error {
|
func DecisionsToTable(alerts *models.GetAlertsResponse, printMachine bool) error {
|
||||||
/*here we cheat a bit : to make it more readable for the user, we dedup some entries*/
|
/*here we cheat a bit : to make it more readable for the user, we dedup some entries*/
|
||||||
spamLimit := make(map[string]bool)
|
spamLimit := make(map[string]bool)
|
||||||
skipped := 0
|
skipped := 0
|
||||||
|
@ -34,36 +33,27 @@ func (cli *cliDecisions) decisionsToTable(alerts *models.GetAlertsResponse, prin
|
||||||
for aIdx := 0; aIdx < len(*alerts); aIdx++ {
|
for aIdx := 0; aIdx < len(*alerts); aIdx++ {
|
||||||
alertItem := (*alerts)[aIdx]
|
alertItem := (*alerts)[aIdx]
|
||||||
newDecisions := make([]*models.Decision, 0)
|
newDecisions := make([]*models.Decision, 0)
|
||||||
|
|
||||||
for _, decisionItem := range alertItem.Decisions {
|
for _, decisionItem := range alertItem.Decisions {
|
||||||
spamKey := fmt.Sprintf("%t:%s:%s:%s", *decisionItem.Simulated, *decisionItem.Type, *decisionItem.Scope, *decisionItem.Value)
|
spamKey := fmt.Sprintf("%t:%s:%s:%s", *decisionItem.Simulated, *decisionItem.Type, *decisionItem.Scope, *decisionItem.Value)
|
||||||
if _, ok := spamLimit[spamKey]; ok {
|
if _, ok := spamLimit[spamKey]; ok {
|
||||||
skipped++
|
skipped++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
spamLimit[spamKey] = true
|
spamLimit[spamKey] = true
|
||||||
|
|
||||||
newDecisions = append(newDecisions, decisionItem)
|
newDecisions = append(newDecisions, decisionItem)
|
||||||
}
|
}
|
||||||
|
|
||||||
alertItem.Decisions = newDecisions
|
alertItem.Decisions = newDecisions
|
||||||
}
|
}
|
||||||
|
if csConfig.Cscli.Output == "raw" {
|
||||||
switch cli.cfg().Cscli.Output {
|
|
||||||
case "raw":
|
|
||||||
csvwriter := csv.NewWriter(os.Stdout)
|
csvwriter := csv.NewWriter(os.Stdout)
|
||||||
header := []string{"id", "source", "ip", "reason", "action", "country", "as", "events_count", "expiration", "simulated", "alert_id"}
|
header := []string{"id", "source", "ip", "reason", "action", "country", "as", "events_count", "expiration", "simulated", "alert_id"}
|
||||||
|
|
||||||
if printMachine {
|
if printMachine {
|
||||||
header = append(header, "machine")
|
header = append(header, "machine")
|
||||||
}
|
}
|
||||||
|
|
||||||
err := csvwriter.Write(header)
|
err := csvwriter.Write(header)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, alertItem := range *alerts {
|
for _, alertItem := range *alerts {
|
||||||
for _, decisionItem := range alertItem.Decisions {
|
for _, decisionItem := range alertItem.Decisions {
|
||||||
raw := []string{
|
raw := []string{
|
||||||
|
@ -89,46 +79,31 @@ func (cli *cliDecisions) decisionsToTable(alerts *models.GetAlertsResponse, prin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
csvwriter.Flush()
|
csvwriter.Flush()
|
||||||
case "json":
|
} else if csConfig.Cscli.Output == "json" {
|
||||||
if *alerts == nil {
|
if *alerts == nil {
|
||||||
// avoid returning "null" in `json"
|
// avoid returning "null" in `json"
|
||||||
// could be cleaner if we used slice of alerts directly
|
// could be cleaner if we used slice of alerts directly
|
||||||
fmt.Println("[]")
|
fmt.Println("[]")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
x, _ := json.MarshalIndent(alerts, "", " ")
|
x, _ := json.MarshalIndent(alerts, "", " ")
|
||||||
fmt.Printf("%s", string(x))
|
fmt.Printf("%s", string(x))
|
||||||
case "human":
|
} else if csConfig.Cscli.Output == "human" {
|
||||||
if len(*alerts) == 0 {
|
if len(*alerts) == 0 {
|
||||||
fmt.Println("No active decisions")
|
fmt.Println("No active decisions")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
decisionsTable(color.Output, alerts, printMachine)
|
||||||
cli.decisionsTable(color.Output, alerts, printMachine)
|
|
||||||
|
|
||||||
if skipped > 0 {
|
if skipped > 0 {
|
||||||
fmt.Printf("%d duplicated entries skipped\n", skipped)
|
fmt.Printf("%d duplicated entries skipped\n", skipped)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type cliDecisions struct {
|
func NewDecisionsCmd() *cobra.Command {
|
||||||
cfg configGetter
|
var cmdDecisions = &cobra.Command{
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLIDecisions(cfg configGetter) *cliDecisions {
|
|
||||||
return &cliDecisions{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliDecisions) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "decisions [action]",
|
Use: "decisions [action]",
|
||||||
Short: "Manage decisions",
|
Short: "Manage decisions",
|
||||||
Long: `Add/List/Delete/Import decisions from LAPI`,
|
Long: `Add/List/Delete/Import decisions from LAPI`,
|
||||||
|
@ -137,18 +112,17 @@ func (cli *cliDecisions) NewCommand() *cobra.Command {
|
||||||
/*TBD example*/
|
/*TBD example*/
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
if err := csConfig.LoadAPIClient(); err != nil {
|
||||||
if err := cfg.LoadAPIClient(); err != nil {
|
|
||||||
return fmt.Errorf("loading api client: %w", err)
|
return fmt.Errorf("loading api client: %w", err)
|
||||||
}
|
}
|
||||||
password := strfmt.Password(cfg.API.Client.Credentials.Password)
|
password := strfmt.Password(csConfig.API.Client.Credentials.Password)
|
||||||
apiurl, err := url.Parse(cfg.API.Client.Credentials.URL)
|
apiurl, err := url.Parse(csConfig.API.Client.Credentials.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parsing api url %s: %w", cfg.API.Client.Credentials.URL, err)
|
return fmt.Errorf("parsing api url %s: %w", csConfig.API.Client.Credentials.URL, err)
|
||||||
}
|
}
|
||||||
Client, err = apiclient.NewClient(&apiclient.Config{
|
Client, err = apiclient.NewClient(&apiclient.Config{
|
||||||
MachineID: cfg.API.Client.Credentials.Login,
|
MachineID: csConfig.API.Client.Credentials.Login,
|
||||||
Password: password,
|
Password: password,
|
||||||
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
||||||
URL: apiurl,
|
URL: apiurl,
|
||||||
|
@ -157,20 +131,19 @@ func (cli *cliDecisions) NewCommand() *cobra.Command {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("creating api client: %w", err)
|
return fmt.Errorf("creating api client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.newListCmd())
|
cmdDecisions.AddCommand(NewDecisionsListCmd())
|
||||||
cmd.AddCommand(cli.newAddCmd())
|
cmdDecisions.AddCommand(NewDecisionsAddCmd())
|
||||||
cmd.AddCommand(cli.newDeleteCmd())
|
cmdDecisions.AddCommand(NewDecisionsDeleteCmd())
|
||||||
cmd.AddCommand(cli.newImportCmd())
|
cmdDecisions.AddCommand(NewDecisionsImportCmd())
|
||||||
|
|
||||||
return cmd
|
return cmdDecisions
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliDecisions) newListCmd() *cobra.Command {
|
func NewDecisionsListCmd() *cobra.Command {
|
||||||
var filter = apiclient.AlertsListOpts{
|
var filter = apiclient.AlertsListOpts{
|
||||||
ValueEquals: new(string),
|
ValueEquals: new(string),
|
||||||
ScopeEquals: new(string),
|
ScopeEquals: new(string),
|
||||||
|
@ -184,23 +157,21 @@ func (cli *cliDecisions) newListCmd() *cobra.Command {
|
||||||
IncludeCAPI: new(bool),
|
IncludeCAPI: new(bool),
|
||||||
Limit: new(int),
|
Limit: new(int),
|
||||||
}
|
}
|
||||||
|
|
||||||
NoSimu := new(bool)
|
NoSimu := new(bool)
|
||||||
contained := new(bool)
|
contained := new(bool)
|
||||||
|
|
||||||
var printMachine bool
|
var printMachine bool
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
var cmdDecisionsList = &cobra.Command{
|
||||||
Use: "list [options]",
|
Use: "list [options]",
|
||||||
Short: "List decisions from LAPI",
|
Short: "List decisions from LAPI",
|
||||||
Example: `cscli decisions list -i 1.2.3.4
|
Example: `cscli decisions list -i 1.2.3.4
|
||||||
cscli decisions list -r 1.2.3.0/24
|
cscli decisions list -r 1.2.3.0/24
|
||||||
cscli decisions list -s crowdsecurity/ssh-bf
|
cscli decisions list -s crowdsecurity/ssh-bf
|
||||||
cscli decisions list --origin lists --scenario list_name
|
cscli decisions list -t ban
|
||||||
`,
|
`,
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
var err error
|
var err error
|
||||||
/*take care of shorthand options*/
|
/*take care of shorthand options*/
|
||||||
if err = manageCliDecisionAlerts(filter.IPEquals, filter.RangeEquals, filter.ScopeEquals, filter.ValueEquals); err != nil {
|
if err = manageCliDecisionAlerts(filter.IPEquals, filter.RangeEquals, filter.ScopeEquals, filter.ValueEquals); err != nil {
|
||||||
|
@ -272,7 +243,7 @@ cscli decisions list --origin lists --scenario list_name
|
||||||
return fmt.Errorf("unable to retrieve decisions: %w", err)
|
return fmt.Errorf("unable to retrieve decisions: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = cli.decisionsToTable(alerts, printMachine)
|
err = DecisionsToTable(alerts, printMachine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to print decisions: %w", err)
|
return fmt.Errorf("unable to print decisions: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -280,26 +251,26 @@ cscli decisions list --origin lists --scenario list_name
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.Flags().SortFlags = false
|
cmdDecisionsList.Flags().SortFlags = false
|
||||||
cmd.Flags().BoolVarP(filter.IncludeCAPI, "all", "a", false, "Include decisions from Central API")
|
cmdDecisionsList.Flags().BoolVarP(filter.IncludeCAPI, "all", "a", false, "Include decisions from Central API")
|
||||||
cmd.Flags().StringVar(filter.Since, "since", "", "restrict to alerts newer than since (ie. 4h, 30d)")
|
cmdDecisionsList.Flags().StringVar(filter.Since, "since", "", "restrict to alerts newer than since (ie. 4h, 30d)")
|
||||||
cmd.Flags().StringVar(filter.Until, "until", "", "restrict to alerts older than until (ie. 4h, 30d)")
|
cmdDecisionsList.Flags().StringVar(filter.Until, "until", "", "restrict to alerts older than until (ie. 4h, 30d)")
|
||||||
cmd.Flags().StringVarP(filter.TypeEquals, "type", "t", "", "restrict to this decision type (ie. ban,captcha)")
|
cmdDecisionsList.Flags().StringVarP(filter.TypeEquals, "type", "t", "", "restrict to this decision type (ie. ban,captcha)")
|
||||||
cmd.Flags().StringVar(filter.ScopeEquals, "scope", "", "restrict to this scope (ie. ip,range,session)")
|
cmdDecisionsList.Flags().StringVar(filter.ScopeEquals, "scope", "", "restrict to this scope (ie. ip,range,session)")
|
||||||
cmd.Flags().StringVar(filter.OriginEquals, "origin", "", fmt.Sprintf("the value to match for the specified origin (%s ...)", strings.Join(types.GetOrigins(), ",")))
|
cmdDecisionsList.Flags().StringVar(filter.OriginEquals, "origin", "", fmt.Sprintf("the value to match for the specified origin (%s ...)", strings.Join(types.GetOrigins(), ",")))
|
||||||
cmd.Flags().StringVarP(filter.ValueEquals, "value", "v", "", "restrict to this value (ie. 1.2.3.4,userName)")
|
cmdDecisionsList.Flags().StringVarP(filter.ValueEquals, "value", "v", "", "restrict to this value (ie. 1.2.3.4,userName)")
|
||||||
cmd.Flags().StringVarP(filter.ScenarioEquals, "scenario", "s", "", "restrict to this scenario (ie. crowdsecurity/ssh-bf)")
|
cmdDecisionsList.Flags().StringVarP(filter.ScenarioEquals, "scenario", "s", "", "restrict to this scenario (ie. crowdsecurity/ssh-bf)")
|
||||||
cmd.Flags().StringVarP(filter.IPEquals, "ip", "i", "", "restrict to alerts from this source ip (shorthand for --scope ip --value <IP>)")
|
cmdDecisionsList.Flags().StringVarP(filter.IPEquals, "ip", "i", "", "restrict to alerts from this source ip (shorthand for --scope ip --value <IP>)")
|
||||||
cmd.Flags().StringVarP(filter.RangeEquals, "range", "r", "", "restrict to alerts from this source range (shorthand for --scope range --value <RANGE>)")
|
cmdDecisionsList.Flags().StringVarP(filter.RangeEquals, "range", "r", "", "restrict to alerts from this source range (shorthand for --scope range --value <RANGE>)")
|
||||||
cmd.Flags().IntVarP(filter.Limit, "limit", "l", 100, "number of alerts to get (use 0 to remove the limit)")
|
cmdDecisionsList.Flags().IntVarP(filter.Limit, "limit", "l", 100, "number of alerts to get (use 0 to remove the limit)")
|
||||||
cmd.Flags().BoolVar(NoSimu, "no-simu", false, "exclude decisions in simulation mode")
|
cmdDecisionsList.Flags().BoolVar(NoSimu, "no-simu", false, "exclude decisions in simulation mode")
|
||||||
cmd.Flags().BoolVarP(&printMachine, "machine", "m", false, "print machines that triggered decisions")
|
cmdDecisionsList.Flags().BoolVarP(&printMachine, "machine", "m", false, "print machines that triggered decisions")
|
||||||
cmd.Flags().BoolVar(contained, "contained", false, "query decisions contained by range")
|
cmdDecisionsList.Flags().BoolVar(contained, "contained", false, "query decisions contained by range")
|
||||||
|
|
||||||
return cmd
|
return cmdDecisionsList
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliDecisions) newAddCmd() *cobra.Command {
|
func NewDecisionsAddCmd() *cobra.Command {
|
||||||
var (
|
var (
|
||||||
addIP string
|
addIP string
|
||||||
addRange string
|
addRange string
|
||||||
|
@ -310,7 +281,7 @@ func (cli *cliDecisions) newAddCmd() *cobra.Command {
|
||||||
addType string
|
addType string
|
||||||
)
|
)
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
var cmdDecisionsAdd = &cobra.Command{
|
||||||
Use: "add [options]",
|
Use: "add [options]",
|
||||||
Short: "Add decision to LAPI",
|
Short: "Add decision to LAPI",
|
||||||
Example: `cscli decisions add --ip 1.2.3.4
|
Example: `cscli decisions add --ip 1.2.3.4
|
||||||
|
@ -321,7 +292,7 @@ cscli decisions add --scope username --value foobar
|
||||||
/*TBD : fix long and example*/
|
/*TBD : fix long and example*/
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(cmd *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
var err error
|
var err error
|
||||||
alerts := models.AddAlertsRequest{}
|
alerts := models.AddAlertsRequest{}
|
||||||
origin := types.CscliOrigin
|
origin := types.CscliOrigin
|
||||||
|
@ -335,7 +306,7 @@ cscli decisions add --scope username --value foobar
|
||||||
createdAt := time.Now().UTC().Format(time.RFC3339)
|
createdAt := time.Now().UTC().Format(time.RFC3339)
|
||||||
|
|
||||||
/*take care of shorthand options*/
|
/*take care of shorthand options*/
|
||||||
if err = manageCliDecisionAlerts(&addIP, &addRange, &addScope, &addValue); err != nil {
|
if err := manageCliDecisionAlerts(&addIP, &addRange, &addScope, &addValue); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,11 +318,11 @@ cscli decisions add --scope username --value foobar
|
||||||
addScope = types.Range
|
addScope = types.Range
|
||||||
} else if addValue == "" {
|
} else if addValue == "" {
|
||||||
printHelp(cmd)
|
printHelp(cmd)
|
||||||
return errors.New("missing arguments, a value is required (--ip, --range or --scope and --value)")
|
return fmt.Errorf("Missing arguments, a value is required (--ip, --range or --scope and --value)")
|
||||||
}
|
}
|
||||||
|
|
||||||
if addReason == "" {
|
if addReason == "" {
|
||||||
addReason = fmt.Sprintf("manual '%s' from '%s'", addType, cli.cfg().API.Client.Credentials.Login)
|
addReason = fmt.Sprintf("manual '%s' from '%s'", addType, csConfig.API.Client.Credentials.Login)
|
||||||
}
|
}
|
||||||
decision := models.Decision{
|
decision := models.Decision{
|
||||||
Duration: &addDuration,
|
Duration: &addDuration,
|
||||||
|
@ -394,25 +365,24 @@ cscli decisions add --scope username --value foobar
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Info("Decision successfully added")
|
log.Info("Decision successfully added")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().SortFlags = false
|
cmdDecisionsAdd.Flags().SortFlags = false
|
||||||
cmd.Flags().StringVarP(&addIP, "ip", "i", "", "Source ip (shorthand for --scope ip --value <IP>)")
|
cmdDecisionsAdd.Flags().StringVarP(&addIP, "ip", "i", "", "Source ip (shorthand for --scope ip --value <IP>)")
|
||||||
cmd.Flags().StringVarP(&addRange, "range", "r", "", "Range source ip (shorthand for --scope range --value <RANGE>)")
|
cmdDecisionsAdd.Flags().StringVarP(&addRange, "range", "r", "", "Range source ip (shorthand for --scope range --value <RANGE>)")
|
||||||
cmd.Flags().StringVarP(&addDuration, "duration", "d", "4h", "Decision duration (ie. 1h,4h,30m)")
|
cmdDecisionsAdd.Flags().StringVarP(&addDuration, "duration", "d", "4h", "Decision duration (ie. 1h,4h,30m)")
|
||||||
cmd.Flags().StringVarP(&addValue, "value", "v", "", "The value (ie. --scope username --value foobar)")
|
cmdDecisionsAdd.Flags().StringVarP(&addValue, "value", "v", "", "The value (ie. --scope username --value foobar)")
|
||||||
cmd.Flags().StringVar(&addScope, "scope", types.Ip, "Decision scope (ie. ip,range,username)")
|
cmdDecisionsAdd.Flags().StringVar(&addScope, "scope", types.Ip, "Decision scope (ie. ip,range,username)")
|
||||||
cmd.Flags().StringVarP(&addReason, "reason", "R", "", "Decision reason (ie. scenario-name)")
|
cmdDecisionsAdd.Flags().StringVarP(&addReason, "reason", "R", "", "Decision reason (ie. scenario-name)")
|
||||||
cmd.Flags().StringVarP(&addType, "type", "t", "ban", "Decision type (ie. ban,captcha,throttle)")
|
cmdDecisionsAdd.Flags().StringVarP(&addType, "type", "t", "ban", "Decision type (ie. ban,captcha,throttle)")
|
||||||
|
|
||||||
return cmd
|
return cmdDecisionsAdd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliDecisions) newDeleteCmd() *cobra.Command {
|
func NewDecisionsDeleteCmd() *cobra.Command {
|
||||||
delFilter := apiclient.DecisionsDeleteOpts{
|
var delFilter = apiclient.DecisionsDeleteOpts{
|
||||||
ScopeEquals: new(string),
|
ScopeEquals: new(string),
|
||||||
ValueEquals: new(string),
|
ValueEquals: new(string),
|
||||||
TypeEquals: new(string),
|
TypeEquals: new(string),
|
||||||
|
@ -421,14 +391,11 @@ func (cli *cliDecisions) newDeleteCmd() *cobra.Command {
|
||||||
ScenarioEquals: new(string),
|
ScenarioEquals: new(string),
|
||||||
OriginEquals: new(string),
|
OriginEquals: new(string),
|
||||||
}
|
}
|
||||||
|
var delDecisionId string
|
||||||
var delDecisionID string
|
|
||||||
|
|
||||||
var delDecisionAll bool
|
var delDecisionAll bool
|
||||||
|
|
||||||
contained := new(bool)
|
contained := new(bool)
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
var cmdDecisionsDelete = &cobra.Command{
|
||||||
Use: "delete [options]",
|
Use: "delete [options]",
|
||||||
Short: "Delete decisions",
|
Short: "Delete decisions",
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
|
@ -437,24 +404,23 @@ func (cli *cliDecisions) newDeleteCmd() *cobra.Command {
|
||||||
cscli decisions delete -i 1.2.3.4
|
cscli decisions delete -i 1.2.3.4
|
||||||
cscli decisions delete --id 42
|
cscli decisions delete --id 42
|
||||||
cscli decisions delete --type captcha
|
cscli decisions delete --type captcha
|
||||||
cscli decisions delete --origin lists --scenario list_name
|
|
||||||
`,
|
`,
|
||||||
/*TBD : refaire le Long/Example*/
|
/*TBD : refaire le Long/Example*/
|
||||||
PreRunE: func(cmd *cobra.Command, _ []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if delDecisionAll {
|
if delDecisionAll {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
if *delFilter.ScopeEquals == "" && *delFilter.ValueEquals == "" &&
|
if *delFilter.ScopeEquals == "" && *delFilter.ValueEquals == "" &&
|
||||||
*delFilter.TypeEquals == "" && *delFilter.IPEquals == "" &&
|
*delFilter.TypeEquals == "" && *delFilter.IPEquals == "" &&
|
||||||
*delFilter.RangeEquals == "" && *delFilter.ScenarioEquals == "" &&
|
*delFilter.RangeEquals == "" && *delFilter.ScenarioEquals == "" &&
|
||||||
*delFilter.OriginEquals == "" && delDecisionID == "" {
|
*delFilter.OriginEquals == "" && delDecisionId == "" {
|
||||||
cmd.Usage()
|
cmd.Usage()
|
||||||
return errors.New("at least one filter or --all must be specified")
|
return fmt.Errorf("at least one filter or --all must be specified")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
var err error
|
var err error
|
||||||
var decisions *models.DeleteDecisionResponse
|
var decisions *models.DeleteDecisionResponse
|
||||||
|
|
||||||
|
@ -487,37 +453,36 @@ cscli decisions delete --origin lists --scenario list_name
|
||||||
delFilter.Contains = new(bool)
|
delFilter.Contains = new(bool)
|
||||||
}
|
}
|
||||||
|
|
||||||
if delDecisionID == "" {
|
if delDecisionId == "" {
|
||||||
decisions, _, err = Client.Decisions.Delete(context.Background(), delFilter)
|
decisions, _, err = Client.Decisions.Delete(context.Background(), delFilter)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to delete decisions: %v", err)
|
return fmt.Errorf("Unable to delete decisions: %v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if _, err = strconv.Atoi(delDecisionID); err != nil {
|
if _, err = strconv.Atoi(delDecisionId); err != nil {
|
||||||
return fmt.Errorf("id '%s' is not an integer: %v", delDecisionID, err)
|
return fmt.Errorf("id '%s' is not an integer: %v", delDecisionId, err)
|
||||||
}
|
}
|
||||||
decisions, _, err = Client.Decisions.DeleteOne(context.Background(), delDecisionID)
|
decisions, _, err = Client.Decisions.DeleteOne(context.Background(), delDecisionId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to delete decision: %v", err)
|
return fmt.Errorf("Unable to delete decision: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Infof("%s decision(s) deleted", decisions.NbDeleted)
|
log.Infof("%s decision(s) deleted", decisions.NbDeleted)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().SortFlags = false
|
cmdDecisionsDelete.Flags().SortFlags = false
|
||||||
cmd.Flags().StringVarP(delFilter.IPEquals, "ip", "i", "", "Source ip (shorthand for --scope ip --value <IP>)")
|
cmdDecisionsDelete.Flags().StringVarP(delFilter.IPEquals, "ip", "i", "", "Source ip (shorthand for --scope ip --value <IP>)")
|
||||||
cmd.Flags().StringVarP(delFilter.RangeEquals, "range", "r", "", "Range source ip (shorthand for --scope range --value <RANGE>)")
|
cmdDecisionsDelete.Flags().StringVarP(delFilter.RangeEquals, "range", "r", "", "Range source ip (shorthand for --scope range --value <RANGE>)")
|
||||||
cmd.Flags().StringVarP(delFilter.TypeEquals, "type", "t", "", "the decision type (ie. ban,captcha)")
|
cmdDecisionsDelete.Flags().StringVarP(delFilter.TypeEquals, "type", "t", "", "the decision type (ie. ban,captcha)")
|
||||||
cmd.Flags().StringVarP(delFilter.ValueEquals, "value", "v", "", "the value to match for in the specified scope")
|
cmdDecisionsDelete.Flags().StringVarP(delFilter.ValueEquals, "value", "v", "", "the value to match for in the specified scope")
|
||||||
cmd.Flags().StringVarP(delFilter.ScenarioEquals, "scenario", "s", "", "the scenario name (ie. crowdsecurity/ssh-bf)")
|
cmdDecisionsDelete.Flags().StringVarP(delFilter.ScenarioEquals, "scenario", "s", "", "the scenario name (ie. crowdsecurity/ssh-bf)")
|
||||||
cmd.Flags().StringVar(delFilter.OriginEquals, "origin", "", fmt.Sprintf("the value to match for the specified origin (%s ...)", strings.Join(types.GetOrigins(), ",")))
|
cmdDecisionsDelete.Flags().StringVar(delFilter.OriginEquals, "origin", "", fmt.Sprintf("the value to match for the specified origin (%s ...)", strings.Join(types.GetOrigins(), ",")))
|
||||||
|
|
||||||
cmd.Flags().StringVar(&delDecisionID, "id", "", "decision id")
|
cmdDecisionsDelete.Flags().StringVar(&delDecisionId, "id", "", "decision id")
|
||||||
cmd.Flags().BoolVar(&delDecisionAll, "all", false, "delete all decisions")
|
cmdDecisionsDelete.Flags().BoolVar(&delDecisionAll, "all", false, "delete all decisions")
|
||||||
cmd.Flags().BoolVar(contained, "contained", false, "query decisions contained by range")
|
cmdDecisionsDelete.Flags().BoolVar(contained, "contained", false, "query decisions contained by range")
|
||||||
|
|
||||||
return cmd
|
return cmdDecisionsDelete
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -38,25 +37,21 @@ func parseDecisionList(content []byte, format string) ([]decisionRaw, error) {
|
||||||
switch format {
|
switch format {
|
||||||
case "values":
|
case "values":
|
||||||
log.Infof("Parsing values")
|
log.Infof("Parsing values")
|
||||||
|
|
||||||
scanner := bufio.NewScanner(bytes.NewReader(content))
|
scanner := bufio.NewScanner(bytes.NewReader(content))
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
value := strings.TrimSpace(scanner.Text())
|
value := strings.TrimSpace(scanner.Text())
|
||||||
ret = append(ret, decisionRaw{Value: value})
|
ret = append(ret, decisionRaw{Value: value})
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := scanner.Err(); err != nil {
|
if err := scanner.Err(); err != nil {
|
||||||
return nil, fmt.Errorf("unable to parse values: '%s'", err)
|
return nil, fmt.Errorf("unable to parse values: '%s'", err)
|
||||||
}
|
}
|
||||||
case "json":
|
case "json":
|
||||||
log.Infof("Parsing json")
|
log.Infof("Parsing json")
|
||||||
|
|
||||||
if err := json.Unmarshal(content, &ret); err != nil {
|
if err := json.Unmarshal(content, &ret); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
case "csv":
|
case "csv":
|
||||||
log.Infof("Parsing csv")
|
log.Infof("Parsing csv")
|
||||||
|
|
||||||
if err := csvutil.Unmarshal(content, &ret); err != nil {
|
if err := csvutil.Unmarshal(content, &ret); err != nil {
|
||||||
return nil, fmt.Errorf("unable to parse csv: '%s'", err)
|
return nil, fmt.Errorf("unable to parse csv: '%s'", err)
|
||||||
}
|
}
|
||||||
|
@ -68,7 +63,7 @@ func parseDecisionList(content []byte, format string) ([]decisionRaw, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func (cli *cliDecisions) runImport(cmd *cobra.Command, args []string) error {
|
func runDecisionsImport(cmd *cobra.Command, args []string) error {
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
|
||||||
input, err := flags.GetString("input")
|
input, err := flags.GetString("input")
|
||||||
|
@ -80,36 +75,32 @@ func (cli *cliDecisions) runImport(cmd *cobra.Command, args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if defaultDuration == "" {
|
if defaultDuration == "" {
|
||||||
return errors.New("--duration cannot be empty")
|
return fmt.Errorf("--duration cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultScope, err := flags.GetString("scope")
|
defaultScope, err := flags.GetString("scope")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if defaultScope == "" {
|
if defaultScope == "" {
|
||||||
return errors.New("--scope cannot be empty")
|
return fmt.Errorf("--scope cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultReason, err := flags.GetString("reason")
|
defaultReason, err := flags.GetString("reason")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if defaultReason == "" {
|
if defaultReason == "" {
|
||||||
return errors.New("--reason cannot be empty")
|
return fmt.Errorf("--reason cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
defaultType, err := flags.GetString("type")
|
defaultType, err := flags.GetString("type")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if defaultType == "" {
|
if defaultType == "" {
|
||||||
return errors.New("--type cannot be empty")
|
return fmt.Errorf("--type cannot be empty")
|
||||||
}
|
}
|
||||||
|
|
||||||
batchSize, err := flags.GetInt("batch")
|
batchSize, err := flags.GetInt("batch")
|
||||||
|
@ -137,7 +128,7 @@ func (cli *cliDecisions) runImport(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if format == "" {
|
if format == "" {
|
||||||
return errors.New("unable to guess format from file extension, please provide a format with --format flag")
|
return fmt.Errorf("unable to guess format from file extension, please provide a format with --format flag")
|
||||||
}
|
}
|
||||||
|
|
||||||
if input == "-" {
|
if input == "-" {
|
||||||
|
@ -161,7 +152,6 @@ func (cli *cliDecisions) runImport(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
decisions := make([]*models.Decision, len(decisionsListRaw))
|
decisions := make([]*models.Decision, len(decisionsListRaw))
|
||||||
|
|
||||||
for i, d := range decisionsListRaw {
|
for i, d := range decisionsListRaw {
|
||||||
if d.Value == "" {
|
if d.Value == "" {
|
||||||
return fmt.Errorf("item %d: missing 'value'", i)
|
return fmt.Errorf("item %d: missing 'value'", i)
|
||||||
|
@ -232,18 +222,17 @@ func (cli *cliDecisions) runImport(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Imported %d decisions", len(decisions))
|
log.Infof("Imported %d decisions", len(decisions))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliDecisions) newImportCmd() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
func NewDecisionsImportCmd() *cobra.Command {
|
||||||
|
var cmdDecisionsImport = &cobra.Command{
|
||||||
Use: "import [options]",
|
Use: "import [options]",
|
||||||
Short: "Import decisions from a file or pipe",
|
Short: "Import decisions from a file or pipe",
|
||||||
Long: "expected format:\n" +
|
Long: "expected format:\n" +
|
||||||
"csv : any of duration,reason,scope,type,value, with a header line\n" +
|
"csv : any of duration,reason,scope,type,value, with a header line\n" +
|
||||||
"json :" + "`{" + `"duration" : "24h", "reason" : "my_scenario", "scope" : "ip", "type" : "ban", "value" : "x.y.z.z"` + "}`",
|
"json :" + "`{" + `"duration" : "24h", "reason" : "my_scenario", "scope" : "ip", "type" : "ban", "value" : "x.y.z.z"` + "}`",
|
||||||
Args: cobra.NoArgs,
|
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
Example: `decisions.csv:
|
Example: `decisions.csv:
|
||||||
duration,scope,value
|
duration,scope,value
|
||||||
|
@ -261,10 +250,10 @@ Raw values, standard input:
|
||||||
|
|
||||||
$ echo "1.2.3.4" | cscli decisions import -i - --format values
|
$ echo "1.2.3.4" | cscli decisions import -i - --format values
|
||||||
`,
|
`,
|
||||||
RunE: cli.runImport,
|
RunE: runDecisionsImport,
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmdDecisionsImport.Flags()
|
||||||
flags.SortFlags = false
|
flags.SortFlags = false
|
||||||
flags.StringP("input", "i", "", "Input file")
|
flags.StringP("input", "i", "", "Input file")
|
||||||
flags.StringP("duration", "d", "4h", "Decision duration: 1h,4h,30m")
|
flags.StringP("duration", "d", "4h", "Decision duration: 1h,4h,30m")
|
||||||
|
@ -274,7 +263,7 @@ $ echo "1.2.3.4" | cscli decisions import -i - --format values
|
||||||
flags.Int("batch", 0, "Split import in batches of N decisions")
|
flags.Int("batch", 0, "Split import in batches of N decisions")
|
||||||
flags.String("format", "", "Input format: 'json', 'csv' or 'values' (each line is a value, no headers)")
|
flags.String("format", "", "Input format: 'json', 'csv' or 'values' (each line is a value, no headers)")
|
||||||
|
|
||||||
cmd.MarkFlagRequired("input")
|
cmdDecisionsImport.MarkFlagRequired("input")
|
||||||
|
|
||||||
return cmd
|
return cmdDecisionsImport
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,15 +8,13 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (cli *cliDecisions) decisionsTable(out io.Writer, alerts *models.GetAlertsResponse, printMachine bool) {
|
func decisionsTable(out io.Writer, alerts *models.GetAlertsResponse, printMachine bool) {
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
|
|
||||||
header := []string{"ID", "Source", "Scope:Value", "Reason", "Action", "Country", "AS", "Events", "expiration", "Alert ID"}
|
header := []string{"ID", "Source", "Scope:Value", "Reason", "Action", "Country", "AS", "Events", "expiration", "Alert ID"}
|
||||||
if printMachine {
|
if printMachine {
|
||||||
header = append(header, "Machine")
|
header = append(header, "Machine")
|
||||||
}
|
}
|
||||||
|
|
||||||
t.SetHeaders(header...)
|
t.SetHeaders(header...)
|
||||||
|
|
||||||
for _, alertItem := range *alerts {
|
for _, alertItem := range *alerts {
|
||||||
|
@ -24,7 +22,6 @@ func (cli *cliDecisions) decisionsTable(out io.Writer, alerts *models.GetAlertsR
|
||||||
if *alertItem.Simulated {
|
if *alertItem.Simulated {
|
||||||
*decisionItem.Type = fmt.Sprintf("(simul)%s", *decisionItem.Type)
|
*decisionItem.Type = fmt.Sprintf("(simul)%s", *decisionItem.Type)
|
||||||
}
|
}
|
||||||
|
|
||||||
row := []string{
|
row := []string{
|
||||||
strconv.Itoa(int(decisionItem.ID)),
|
strconv.Itoa(int(decisionItem.ID)),
|
||||||
*decisionItem.Origin,
|
*decisionItem.Origin,
|
||||||
|
@ -45,6 +42,5 @@ func (cli *cliDecisions) decisionsTable(out io.Writer, alerts *models.GetAlertsR
|
||||||
t.AddRow(row...)
|
t.AddRow(row...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/cobra/doc"
|
|
||||||
)
|
|
||||||
|
|
||||||
type cliDoc struct{}
|
|
||||||
|
|
||||||
func NewCLIDoc() *cliDoc {
|
|
||||||
return &cliDoc{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli cliDoc) NewCommand(rootCmd *cobra.Command) *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "doc",
|
|
||||||
Short: "Generate the documentation in `./doc/`. Directory must exist.",
|
|
||||||
Args: cobra.ExactArgs(0),
|
|
||||||
Hidden: true,
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
if err := doc.GenMarkdownTreeCustom(rootCmd, "./doc/", cli.filePrepender, cli.linkHandler); err != nil {
|
|
||||||
return fmt.Errorf("failed to generate cobra doc: %s", err)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli cliDoc) filePrepender(filename string) string {
|
|
||||||
const header = `---
|
|
||||||
id: %s
|
|
||||||
title: %s
|
|
||||||
---
|
|
||||||
`
|
|
||||||
|
|
||||||
name := filepath.Base(filename)
|
|
||||||
base := strings.TrimSuffix(name, filepath.Ext(name))
|
|
||||||
|
|
||||||
return fmt.Sprintf(header, base, strings.ReplaceAll(base, "_", " "))
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli cliDoc) linkHandler(name string) string {
|
|
||||||
return fmt.Sprintf("/cscli/%s", name)
|
|
||||||
}
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
@ -12,115 +11,78 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/dumps"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/hubtest"
|
"github.com/crowdsecurity/crowdsec/pkg/hubtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
func getLineCountForFile(filepath string) (int, error) {
|
func GetLineCountForFile(filepath string) (int, error) {
|
||||||
f, err := os.Open(filepath)
|
f, err := os.Open(filepath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
|
|
||||||
lc := 0
|
lc := 0
|
||||||
fs := bufio.NewReader(f)
|
fs := bufio.NewReader(f)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
input, err := fs.ReadBytes('\n')
|
input, err := fs.ReadBytes('\n')
|
||||||
if len(input) > 1 {
|
if len(input) > 1 {
|
||||||
lc++
|
lc++
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil && err == io.EOF {
|
if err != nil && err == io.EOF {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return lc, nil
|
return lc, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type cliExplain struct {
|
func runExplain(cmd *cobra.Command, args []string) error {
|
||||||
cfg configGetter
|
|
||||||
flags struct {
|
|
||||||
logFile string
|
|
||||||
dsn string
|
|
||||||
logLine string
|
|
||||||
logType string
|
|
||||||
details bool
|
|
||||||
skipOk bool
|
|
||||||
onlySuccessfulParsers bool
|
|
||||||
noClean bool
|
|
||||||
crowdsec string
|
|
||||||
labels string
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLIExplain(cfg configGetter) *cliExplain {
|
|
||||||
return &cliExplain{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliExplain) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "explain",
|
|
||||||
Short: "Explain log pipeline",
|
|
||||||
Long: `
|
|
||||||
Explain log pipeline
|
|
||||||
`,
|
|
||||||
Example: `
|
|
||||||
cscli explain --file ./myfile.log --type nginx
|
|
||||||
cscli explain --log "Sep 19 18:33:22 scw-d95986 sshd[24347]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=1.2.3.4" --type syslog
|
|
||||||
cscli explain --dsn "file://myfile.log" --type nginx
|
|
||||||
tail -n 5 myfile.log | cscli explain --type nginx -f -
|
|
||||||
`,
|
|
||||||
Args: cobra.ExactArgs(0),
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
return cli.run()
|
|
||||||
},
|
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
fileInfo, _ := os.Stdin.Stat()
|
|
||||||
if cli.flags.logFile == "-" && ((fileInfo.Mode() & os.ModeCharDevice) == os.ModeCharDevice) {
|
|
||||||
return errors.New("the option -f - is intended to work with pipes")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
|
|
||||||
flags.StringVarP(&cli.flags.logFile, "file", "f", "", "Log file to test")
|
logFile, err := flags.GetString("file")
|
||||||
flags.StringVarP(&cli.flags.dsn, "dsn", "d", "", "DSN to test")
|
if err != nil {
|
||||||
flags.StringVarP(&cli.flags.logLine, "log", "l", "", "Log line to test")
|
return err
|
||||||
flags.StringVarP(&cli.flags.logType, "type", "t", "", "Type of the acquisition to test")
|
|
||||||
flags.StringVar(&cli.flags.labels, "labels", "", "Additional labels to add to the acquisition format (key:value,key2:value2)")
|
|
||||||
flags.BoolVarP(&cli.flags.details, "verbose", "v", false, "Display individual changes")
|
|
||||||
flags.BoolVar(&cli.flags.skipOk, "failures", false, "Only show failed lines")
|
|
||||||
flags.BoolVar(&cli.flags.onlySuccessfulParsers, "only-successful-parsers", false, "Only show successful parsers")
|
|
||||||
flags.StringVar(&cli.flags.crowdsec, "crowdsec", "crowdsec", "Path to crowdsec")
|
|
||||||
flags.BoolVar(&cli.flags.noClean, "no-clean", false, "Don't clean runtime environment after tests")
|
|
||||||
|
|
||||||
cmd.MarkFlagRequired("type")
|
|
||||||
cmd.MarkFlagsOneRequired("log", "file", "dsn")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliExplain) run() error {
|
dsn, err := flags.GetString("dsn")
|
||||||
logFile := cli.flags.logFile
|
if err != nil {
|
||||||
logLine := cli.flags.logLine
|
return err
|
||||||
logType := cli.flags.logType
|
}
|
||||||
dsn := cli.flags.dsn
|
|
||||||
labels := cli.flags.labels
|
|
||||||
crowdsec := cli.flags.crowdsec
|
|
||||||
|
|
||||||
opts := dumps.DumpOpts{
|
logLine, err := flags.GetString("log")
|
||||||
Details: cli.flags.details,
|
if err != nil {
|
||||||
SkipOk: cli.flags.skipOk,
|
return err
|
||||||
ShowNotOkParsers: !cli.flags.onlySuccessfulParsers,
|
}
|
||||||
|
|
||||||
|
logType, err := flags.GetString("type")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
opts := hubtest.DumpOpts{}
|
||||||
|
|
||||||
|
opts.Details, err = flags.GetBool("verbose")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
opts.SkipOk, err = flags.GetBool("failures")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
opts.ShowNotOkParsers, err = flags.GetBool("only-successful-parsers")
|
||||||
|
opts.ShowNotOkParsers = !opts.ShowNotOkParsers
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
crowdsec, err := flags.GetString("crowdsec")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
labels, err := flags.GetString("labels")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var f *os.File
|
var f *os.File
|
||||||
|
@ -128,25 +90,19 @@ func (cli *cliExplain) run() error {
|
||||||
// using empty string fallback to /tmp
|
// using empty string fallback to /tmp
|
||||||
dir, err := os.MkdirTemp("", "cscli_explain")
|
dir, err := os.MkdirTemp("", "cscli_explain")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("couldn't create a temporary directory to store cscli explain result: %w", err)
|
return fmt.Errorf("couldn't create a temporary directory to store cscli explain result: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if cli.flags.noClean {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, err := os.Stat(dir); !os.IsNotExist(err) {
|
if _, err := os.Stat(dir); !os.IsNotExist(err) {
|
||||||
if err := os.RemoveAll(dir); err != nil {
|
if err := os.RemoveAll(dir); err != nil {
|
||||||
log.Errorf("unable to delete temporary directory '%s': %s", dir, err)
|
log.Errorf("unable to delete temporary directory '%s': %s", dir, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
tmpFile := ""
|
||||||
// we create a temporary log file if a log line/stdin has been provided
|
// we create a temporary log file if a log line/stdin has been provided
|
||||||
if logLine != "" || logFile == "-" {
|
if logLine != "" || logFile == "-" {
|
||||||
tmpFile := filepath.Join(dir, "cscli_test_tmp.log")
|
tmpFile = filepath.Join(dir, "cscli_test_tmp.log")
|
||||||
|
|
||||||
f, err = os.Create(tmpFile)
|
f, err = os.Create(tmpFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -160,27 +116,22 @@ func (cli *cliExplain) run() error {
|
||||||
} else if logFile == "-" {
|
} else if logFile == "-" {
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
errCount := 0
|
errCount := 0
|
||||||
|
|
||||||
for {
|
for {
|
||||||
input, err := reader.ReadBytes('\n')
|
input, err := reader.ReadBytes('\n')
|
||||||
if err != nil && errors.Is(err, io.EOF) {
|
if err != nil && err == io.EOF {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(input) > 1 {
|
if len(input) > 1 {
|
||||||
_, err = f.Write(input)
|
_, err = f.Write(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil || len(input) <= 1 {
|
if err != nil || len(input) <= 1 {
|
||||||
errCount++
|
errCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if errCount > 0 {
|
if errCount > 0 {
|
||||||
log.Warnf("Failed to write %d lines to %s", errCount, tmpFile)
|
log.Warnf("Failed to write %d lines to %s", errCount, tmpFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Close()
|
f.Close()
|
||||||
// this is the file that was going to be read by crowdsec anyway
|
// this is the file that was going to be read by crowdsec anyway
|
||||||
logFile = tmpFile
|
logFile = tmpFile
|
||||||
|
@ -191,59 +142,122 @@ func (cli *cliExplain) run() error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to get absolute path of '%s', exiting", logFile)
|
return fmt.Errorf("unable to get absolute path of '%s', exiting", logFile)
|
||||||
}
|
}
|
||||||
|
|
||||||
dsn = fmt.Sprintf("file://%s", absolutePath)
|
dsn = fmt.Sprintf("file://%s", absolutePath)
|
||||||
|
lineCount, err := GetLineCountForFile(absolutePath)
|
||||||
lineCount, err := getLineCountForFile(absolutePath)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("file %s has %d lines", absolutePath, lineCount)
|
log.Debugf("file %s has %d lines", absolutePath, lineCount)
|
||||||
|
|
||||||
if lineCount == 0 {
|
if lineCount == 0 {
|
||||||
return fmt.Errorf("the log file is empty: %s", absolutePath)
|
return fmt.Errorf("the log file is empty: %s", absolutePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
if lineCount > 100 {
|
if lineCount > 100 {
|
||||||
log.Warnf("%s contains %d lines. This may take a lot of resources.", absolutePath, lineCount)
|
log.Warnf("%s contains %d lines. This may take a lot of resources.", absolutePath, lineCount)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if dsn == "" {
|
if dsn == "" {
|
||||||
return errors.New("no acquisition (--file or --dsn) provided, can't run cscli test")
|
return fmt.Errorf("no acquisition (--file or --dsn) provided, can't run cscli test")
|
||||||
}
|
}
|
||||||
|
|
||||||
cmdArgs := []string{"-c", ConfigFilePath, "-type", logType, "-dsn", dsn, "-dump-data", dir, "-no-api"}
|
cmdArgs := []string{"-c", ConfigFilePath, "-type", logType, "-dsn", dsn, "-dump-data", dir, "-no-api"}
|
||||||
|
|
||||||
if labels != "" {
|
if labels != "" {
|
||||||
log.Debugf("adding labels %s", labels)
|
log.Debugf("adding labels %s", labels)
|
||||||
cmdArgs = append(cmdArgs, "-label", labels)
|
cmdArgs = append(cmdArgs, "-label", labels)
|
||||||
}
|
}
|
||||||
|
|
||||||
crowdsecCmd := exec.Command(crowdsec, cmdArgs...)
|
crowdsecCmd := exec.Command(crowdsec, cmdArgs...)
|
||||||
|
|
||||||
output, err := crowdsecCmd.CombinedOutput()
|
output, err := crowdsecCmd.CombinedOutput()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(string(output))
|
fmt.Println(string(output))
|
||||||
|
return fmt.Errorf("fail to run crowdsec for test: %v", err)
|
||||||
return fmt.Errorf("fail to run crowdsec for test: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parserDumpFile := filepath.Join(dir, hubtest.ParserResultFileName)
|
parserDumpFile := filepath.Join(dir, hubtest.ParserResultFileName)
|
||||||
bucketStateDumpFile := filepath.Join(dir, hubtest.BucketPourResultFileName)
|
bucketStateDumpFile := filepath.Join(dir, hubtest.BucketPourResultFileName)
|
||||||
|
|
||||||
parserDump, err := dumps.LoadParserDump(parserDumpFile)
|
parserDump, err := hubtest.LoadParserDump(parserDumpFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to load parser dump result: %w", err)
|
return fmt.Errorf("unable to load parser dump result: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
bucketStateDump, err := dumps.LoadBucketPourDump(bucketStateDumpFile)
|
bucketStateDump, err := hubtest.LoadBucketPourDump(bucketStateDumpFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to load bucket dump result: %w", err)
|
return fmt.Errorf("unable to load bucket dump result: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
dumps.DumpTree(*parserDump, *bucketStateDump, opts)
|
hubtest.DumpTree(*parserDump, *bucketStateDump, opts)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewExplainCmd() *cobra.Command {
|
||||||
|
cmdExplain := &cobra.Command{
|
||||||
|
Use: "explain",
|
||||||
|
Short: "Explain log pipeline",
|
||||||
|
Long: `
|
||||||
|
Explain log pipeline
|
||||||
|
`,
|
||||||
|
Example: `
|
||||||
|
cscli explain --file ./myfile.log --type nginx
|
||||||
|
cscli explain --log "Sep 19 18:33:22 scw-d95986 sshd[24347]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=1.2.3.4" --type syslog
|
||||||
|
cscli explain --dsn "file://myfile.log" --type nginx
|
||||||
|
tail -n 5 myfile.log | cscli explain --type nginx -f -
|
||||||
|
`,
|
||||||
|
Args: cobra.ExactArgs(0),
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
RunE: runExplain,
|
||||||
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
logFile, err := flags.GetString("file")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
dsn, err := flags.GetString("dsn")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logLine, err := flags.GetString("log")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
logType, err := flags.GetString("type")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if logLine == "" && logFile == "" && dsn == "" {
|
||||||
|
printHelp(cmd)
|
||||||
|
fmt.Println()
|
||||||
|
return fmt.Errorf("please provide --log, --file or --dsn flag")
|
||||||
|
}
|
||||||
|
if logType == "" {
|
||||||
|
printHelp(cmd)
|
||||||
|
fmt.Println()
|
||||||
|
return fmt.Errorf("please provide --type flag")
|
||||||
|
}
|
||||||
|
fileInfo, _ := os.Stdin.Stat()
|
||||||
|
if logFile == "-" && ((fileInfo.Mode() & os.ModeCharDevice) == os.ModeCharDevice) {
|
||||||
|
return fmt.Errorf("the option -f - is intended to work with pipes")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
flags := cmdExplain.Flags()
|
||||||
|
|
||||||
|
flags.StringP("file", "f", "", "Log file to test")
|
||||||
|
flags.StringP("dsn", "d", "", "DSN to test")
|
||||||
|
flags.StringP("log", "l", "", "Log line to test")
|
||||||
|
flags.StringP("type", "t", "", "Type of the acquisition to test")
|
||||||
|
flags.String("labels", "", "Additional labels to add to the acquisition format (key:value,key2:value2)")
|
||||||
|
flags.BoolP("verbose", "v", false, "Display individual changes")
|
||||||
|
flags.Bool("failures", false, "Only show failed lines")
|
||||||
|
flags.Bool("only-successful-parsers", false, "Only show successful parsers")
|
||||||
|
flags.String("crowdsec", "crowdsec", "Path to crowdsec")
|
||||||
|
|
||||||
|
return cmdExplain
|
||||||
|
}
|
||||||
|
|
|
@ -1,29 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
// Custom types for flag validation and conversion.
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
)
|
|
||||||
|
|
||||||
type MachinePassword string
|
|
||||||
|
|
||||||
func (p *MachinePassword) String() string {
|
|
||||||
return string(*p)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *MachinePassword) Set(v string) error {
|
|
||||||
// a password can't be more than 72 characters
|
|
||||||
// due to bcrypt limitations
|
|
||||||
if len(v) > 72 {
|
|
||||||
return errors.New("password too long (max 72 characters)")
|
|
||||||
}
|
|
||||||
|
|
||||||
*p = MachinePassword(v)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *MachinePassword) Type() string {
|
|
||||||
return "string"
|
|
||||||
}
|
|
|
@ -13,18 +13,8 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
type cliHub struct{
|
func NewHubCmd() *cobra.Command {
|
||||||
cfg configGetter
|
cmdHub := &cobra.Command{
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLIHub(cfg configGetter) *cliHub {
|
|
||||||
return &cliHub{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliHub) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "hub [action]",
|
Use: "hub [action]",
|
||||||
Short: "Manage hub index",
|
Short: "Manage hub index",
|
||||||
Long: `Hub management
|
Long: `Hub management
|
||||||
|
@ -38,16 +28,23 @@ cscli hub upgrade`,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.newListCmd())
|
cmdHub.AddCommand(NewHubListCmd())
|
||||||
cmd.AddCommand(cli.newUpdateCmd())
|
cmdHub.AddCommand(NewHubUpdateCmd())
|
||||||
cmd.AddCommand(cli.newUpgradeCmd())
|
cmdHub.AddCommand(NewHubUpgradeCmd())
|
||||||
cmd.AddCommand(cli.newTypesCmd())
|
cmdHub.AddCommand(NewHubTypesCmd())
|
||||||
|
|
||||||
return cmd
|
return cmdHub
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHub) list(all bool) error {
|
func runHubList(cmd *cobra.Command, args []string) error {
|
||||||
hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger())
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
all, err := flags.GetBool("all")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -77,31 +74,27 @@ func (cli *cliHub) list(all bool) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHub) newListCmd() *cobra.Command {
|
func NewHubListCmd() *cobra.Command {
|
||||||
var all bool
|
cmdHubList := &cobra.Command{
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "list [-a]",
|
Use: "list [-a]",
|
||||||
Short: "List all installed configurations",
|
Short: "List all installed configurations",
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: runHubList,
|
||||||
return cli.list(all)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmdHubList.Flags()
|
||||||
flags.BoolVarP(&all, "all", "a", false, "List disabled items as well")
|
flags.BoolP("all", "a", false, "List disabled items as well")
|
||||||
|
|
||||||
return cmd
|
return cmdHubList
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHub) update() error {
|
func runHubUpdate(cmd *cobra.Command, args []string) error {
|
||||||
local := cli.cfg().Hub
|
local := csConfig.Hub
|
||||||
remote := require.RemoteHub(cli.cfg())
|
remote := require.RemoteHub(csConfig)
|
||||||
|
|
||||||
// don't use require.Hub because if there is no index file, it would fail
|
// don't use require.Hub because if there is no index file, it would fail
|
||||||
hub, err := cwhub.NewHub(local, remote, true, log.StandardLogger())
|
hub, err := cwhub.NewHub(local, remote, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to update hub: %w", err)
|
return fmt.Errorf("failed to update hub: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -113,8 +106,8 @@ func (cli *cliHub) update() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHub) newUpdateCmd() *cobra.Command {
|
func NewHubUpdateCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmdHubUpdate := &cobra.Command{
|
||||||
Use: "update",
|
Use: "update",
|
||||||
Short: "Download the latest index (catalog of available configurations)",
|
Short: "Download the latest index (catalog of available configurations)",
|
||||||
Long: `
|
Long: `
|
||||||
|
@ -122,22 +115,27 @@ Fetches the .index.json file from the hub, containing the list of available conf
|
||||||
`,
|
`,
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: runHubUpdate,
|
||||||
return cli.update()
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdHubUpdate
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHub) upgrade(force bool) error {
|
func runHubUpgrade(cmd *cobra.Command, args []string) error {
|
||||||
hub, err := require.Hub(cli.cfg(), require.RemoteHub(cli.cfg()), log.StandardLogger())
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
force, err := flags.GetBool("force")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hub, err := require.Hub(csConfig, require.RemoteHub(csConfig))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, itemType := range cwhub.ItemTypes {
|
for _, itemType := range cwhub.ItemTypes {
|
||||||
items, err := hub.GetInstalledItemsByType(itemType)
|
items, err := hub.GetInstalledItems(itemType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -145,28 +143,23 @@ func (cli *cliHub) upgrade(force bool) error {
|
||||||
updated := 0
|
updated := 0
|
||||||
|
|
||||||
log.Infof("Upgrading %s", itemType)
|
log.Infof("Upgrading %s", itemType)
|
||||||
|
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
didUpdate, err := item.Upgrade(force)
|
didUpdate, err := item.Upgrade(force)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if didUpdate {
|
if didUpdate {
|
||||||
updated++
|
updated++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Upgraded %d %s", updated, itemType)
|
log.Infof("Upgraded %d %s", updated, itemType)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHub) newUpgradeCmd() *cobra.Command {
|
func NewHubUpgradeCmd() *cobra.Command {
|
||||||
var force bool
|
cmdHubUpgrade := &cobra.Command{
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "upgrade",
|
Use: "upgrade",
|
||||||
Short: "Upgrade all configurations to their latest version",
|
Short: "Upgrade all configurations to their latest version",
|
||||||
Long: `
|
Long: `
|
||||||
|
@ -174,44 +167,39 @@ Upgrade all configs installed from Crowdsec Hub. Run 'sudo cscli hub update' if
|
||||||
`,
|
`,
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: runHubUpgrade,
|
||||||
return cli.upgrade(force)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmdHubUpgrade.Flags()
|
||||||
flags.BoolVar(&force, "force", false, "Force upgrade: overwrite tainted and outdated files")
|
flags.Bool("force", false, "Force upgrade: overwrite tainted and outdated files")
|
||||||
|
|
||||||
return cmd
|
return cmdHubUpgrade
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHub) types() error {
|
func runHubTypes(cmd *cobra.Command, args []string) error {
|
||||||
switch cli.cfg().Cscli.Output {
|
switch csConfig.Cscli.Output {
|
||||||
case "human":
|
case "human":
|
||||||
s, err := yaml.Marshal(cwhub.ItemTypes)
|
s, err := yaml.Marshal(cwhub.ItemTypes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print(string(s))
|
fmt.Print(string(s))
|
||||||
case "json":
|
case "json":
|
||||||
jsonStr, err := json.Marshal(cwhub.ItemTypes)
|
jsonStr, err := json.Marshal(cwhub.ItemTypes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(jsonStr))
|
fmt.Println(string(jsonStr))
|
||||||
case "raw":
|
case "raw":
|
||||||
for _, itemType := range cwhub.ItemTypes {
|
for _, itemType := range cwhub.ItemTypes {
|
||||||
fmt.Println(itemType)
|
fmt.Println(itemType)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHub) newTypesCmd() *cobra.Command {
|
func NewHubTypesCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmdHubTypes := &cobra.Command{
|
||||||
Use: "types",
|
Use: "types",
|
||||||
Short: "List supported item types",
|
Short: "List supported item types",
|
||||||
Long: `
|
Long: `
|
||||||
|
@ -219,10 +207,8 @@ List the types of supported hub items.
|
||||||
`,
|
`,
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: runHubTypes,
|
||||||
return cli.types()
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdHubTypes
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,9 +13,8 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCLIAppsecConfig(cfg configGetter) *cliItem {
|
func NewAppsecConfigCLI() *itemCLI {
|
||||||
return &cliItem{
|
return &itemCLI{
|
||||||
cfg: cfg,
|
|
||||||
name: cwhub.APPSEC_CONFIGS,
|
name: cwhub.APPSEC_CONFIGS,
|
||||||
singular: "appsec-config",
|
singular: "appsec-config",
|
||||||
oneOrMore: "appsec-config(s)",
|
oneOrMore: "appsec-config(s)",
|
||||||
|
@ -47,49 +46,32 @@ cscli appsec-configs list crowdsecurity/vpatch`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCLIAppsecRule(cfg configGetter) *cliItem {
|
func NewAppsecRuleCLI() *itemCLI {
|
||||||
inspectDetail := func(item *cwhub.Item) error {
|
inspectDetail := func(item *cwhub.Item) error {
|
||||||
// Only show the converted rules in human mode
|
|
||||||
if csConfig.Cscli.Output != "human" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
appsecRule := appsec.AppsecCollectionConfig{}
|
appsecRule := appsec.AppsecCollectionConfig{}
|
||||||
|
|
||||||
yamlContent, err := os.ReadFile(item.State.LocalPath)
|
yamlContent, err := os.ReadFile(item.State.LocalPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to read file %s: %w", item.State.LocalPath, err)
|
return fmt.Errorf("unable to read file %s : %s", item.State.LocalPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := yaml.Unmarshal(yamlContent, &appsecRule); err != nil {
|
if err := yaml.Unmarshal(yamlContent, &appsecRule); err != nil {
|
||||||
return fmt.Errorf("unable to unmarshal yaml file %s: %w", item.State.LocalPath, err)
|
return fmt.Errorf("unable to unmarshal yaml file %s : %s", item.State.LocalPath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, ruleType := range appsec_rule.SupportedTypes() {
|
for _, ruleType := range appsec_rule.SupportedTypes() {
|
||||||
fmt.Printf("\n%s format:\n", cases.Title(language.Und, cases.NoLower).String(ruleType))
|
fmt.Printf("\n%s format:\n", cases.Title(language.Und, cases.NoLower).String(ruleType))
|
||||||
|
|
||||||
for _, rule := range appsecRule.Rules {
|
for _, rule := range appsecRule.Rules {
|
||||||
convertedRule, _, err := rule.Convert(ruleType, appsecRule.Name)
|
convertedRule, _, err := rule.Convert(ruleType, appsecRule.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to convert rule %s: %w", rule.Name, err)
|
return fmt.Errorf("unable to convert rule %s : %s", rule.Name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(convertedRule)
|
fmt.Println(convertedRule)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch ruleType { //nolint:gocritic
|
|
||||||
case appsec_rule.ModsecurityRuleType:
|
|
||||||
for _, rule := range appsecRule.SecLangRules {
|
|
||||||
fmt.Println(rule)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return &cliItem{
|
return &itemCLI{
|
||||||
cfg: cfg,
|
|
||||||
name: "appsec-rules",
|
name: "appsec-rules",
|
||||||
singular: "appsec-rule",
|
singular: "appsec-rule",
|
||||||
oneOrMore: "appsec-rule(s)",
|
oneOrMore: "appsec-rule(s)",
|
||||||
|
|
|
@ -4,9 +4,8 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCLICollection(cfg configGetter) *cliItem {
|
func NewCollectionCLI() *itemCLI {
|
||||||
return &cliItem{
|
return &itemCLI{
|
||||||
cfg: cfg,
|
|
||||||
name: cwhub.COLLECTIONS,
|
name: cwhub.COLLECTIONS,
|
||||||
singular: "collection",
|
singular: "collection",
|
||||||
oneOrMore: "collection(s)",
|
oneOrMore: "collection(s)",
|
||||||
|
|
|
@ -4,9 +4,8 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCLIContext(cfg configGetter) *cliItem {
|
func NewContextCLI() *itemCLI {
|
||||||
return &cliItem{
|
return &itemCLI{
|
||||||
cfg: cfg,
|
|
||||||
name: cwhub.CONTEXTS,
|
name: cwhub.CONTEXTS,
|
||||||
singular: "context",
|
singular: "context",
|
||||||
oneOrMore: "context(s)",
|
oneOrMore: "context(s)",
|
||||||
|
|
|
@ -4,9 +4,8 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCLIParser(cfg configGetter) *cliItem {
|
func NewParserCLI() *itemCLI {
|
||||||
return &cliItem{
|
return &itemCLI{
|
||||||
cfg: cfg,
|
|
||||||
name: cwhub.PARSERS,
|
name: cwhub.PARSERS,
|
||||||
singular: "parser",
|
singular: "parser",
|
||||||
oneOrMore: "parser(s)",
|
oneOrMore: "parser(s)",
|
||||||
|
|
|
@ -4,9 +4,8 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCLIPostOverflow(cfg configGetter) *cliItem {
|
func NewPostOverflowCLI() *itemCLI {
|
||||||
return &cliItem{
|
return &itemCLI{
|
||||||
cfg: cfg,
|
|
||||||
name: cwhub.POSTOVERFLOWS,
|
name: cwhub.POSTOVERFLOWS,
|
||||||
singular: "postoverflow",
|
singular: "postoverflow",
|
||||||
oneOrMore: "postoverflow(s)",
|
oneOrMore: "postoverflow(s)",
|
||||||
|
|
|
@ -4,9 +4,8 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewCLIScenario(cfg configGetter) *cliItem {
|
func NewScenarioCLI() *itemCLI {
|
||||||
return &cliItem{
|
return &itemCLI{
|
||||||
cfg: cfg,
|
|
||||||
name: cwhub.SCENARIOS,
|
name: cwhub.SCENARIOS,
|
||||||
singular: "scenario",
|
singular: "scenario",
|
||||||
oneOrMore: "scenario(s)",
|
oneOrMore: "scenario(s)",
|
||||||
|
|
|
@ -2,56 +2,39 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
|
||||||
|
|
||||||
"github.com/AlecAivazis/survey/v2"
|
"github.com/AlecAivazis/survey/v2"
|
||||||
|
"github.com/enescakir/emoji"
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/dumps"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/emoji"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/hubtest"
|
"github.com/crowdsecurity/crowdsec/pkg/hubtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var HubTest hubtest.HubTest
|
||||||
HubTest hubtest.HubTest
|
var HubAppsecTests hubtest.HubTest
|
||||||
HubAppsecTests hubtest.HubTest
|
var hubPtr *hubtest.HubTest
|
||||||
hubPtr *hubtest.HubTest
|
var isAppsecTest bool
|
||||||
isAppsecTest bool
|
|
||||||
)
|
|
||||||
|
|
||||||
type cliHubTest struct {
|
func NewHubTestCmd() *cobra.Command {
|
||||||
cfg configGetter
|
var hubPath string
|
||||||
}
|
var crowdsecPath string
|
||||||
|
var cscliPath string
|
||||||
|
|
||||||
func NewCLIHubTest(cfg configGetter) *cliHubTest {
|
var cmdHubTest = &cobra.Command{
|
||||||
return &cliHubTest{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliHubTest) NewCommand() *cobra.Command {
|
|
||||||
var (
|
|
||||||
hubPath string
|
|
||||||
crowdsecPath string
|
|
||||||
cscliPath string
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "hubtest",
|
Use: "hubtest",
|
||||||
Short: "Run functional tests on hub configurations",
|
Short: "Run functional tests on hub configurations",
|
||||||
Long: "Run functional tests on hub configurations (parsers, scenarios, collections...)",
|
Long: "Run functional tests on hub configurations (parsers, scenarios, collections...)",
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
var err error
|
var err error
|
||||||
HubTest, err = hubtest.NewHubTest(hubPath, crowdsecPath, cscliPath, false)
|
HubTest, err = hubtest.NewHubTest(hubPath, crowdsecPath, cscliPath, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -62,46 +45,41 @@ func (cli *cliHubTest) NewCommand() *cobra.Command {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to load appsec specific hubtest: %+v", err)
|
return fmt.Errorf("unable to load appsec specific hubtest: %+v", err)
|
||||||
}
|
}
|
||||||
|
/*commands will use the hubPtr, will point to the default hubTest object, or the one dedicated to appsec tests*/
|
||||||
// commands will use the hubPtr, will point to the default hubTest object, or the one dedicated to appsec tests
|
|
||||||
hubPtr = &HubTest
|
hubPtr = &HubTest
|
||||||
if isAppsecTest {
|
if isAppsecTest {
|
||||||
hubPtr = &HubAppsecTests
|
hubPtr = &HubAppsecTests
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.PersistentFlags().StringVar(&hubPath, "hub", ".", "Path to hub folder")
|
cmdHubTest.PersistentFlags().StringVar(&hubPath, "hub", ".", "Path to hub folder")
|
||||||
cmd.PersistentFlags().StringVar(&crowdsecPath, "crowdsec", "crowdsec", "Path to crowdsec")
|
cmdHubTest.PersistentFlags().StringVar(&crowdsecPath, "crowdsec", "crowdsec", "Path to crowdsec")
|
||||||
cmd.PersistentFlags().StringVar(&cscliPath, "cscli", "cscli", "Path to cscli")
|
cmdHubTest.PersistentFlags().StringVar(&cscliPath, "cscli", "cscli", "Path to cscli")
|
||||||
cmd.PersistentFlags().BoolVar(&isAppsecTest, "appsec", false, "Command relates to appsec tests")
|
cmdHubTest.PersistentFlags().BoolVar(&isAppsecTest, "appsec", false, "Command relates to appsec tests")
|
||||||
|
|
||||||
cmd.AddCommand(cli.NewCreateCmd())
|
cmdHubTest.AddCommand(NewHubTestCreateCmd())
|
||||||
cmd.AddCommand(cli.NewRunCmd())
|
cmdHubTest.AddCommand(NewHubTestRunCmd())
|
||||||
cmd.AddCommand(cli.NewCleanCmd())
|
cmdHubTest.AddCommand(NewHubTestCleanCmd())
|
||||||
cmd.AddCommand(cli.NewInfoCmd())
|
cmdHubTest.AddCommand(NewHubTestInfoCmd())
|
||||||
cmd.AddCommand(cli.NewListCmd())
|
cmdHubTest.AddCommand(NewHubTestListCmd())
|
||||||
cmd.AddCommand(cli.NewCoverageCmd())
|
cmdHubTest.AddCommand(NewHubTestCoverageCmd())
|
||||||
cmd.AddCommand(cli.NewEvalCmd())
|
cmdHubTest.AddCommand(NewHubTestEvalCmd())
|
||||||
cmd.AddCommand(cli.NewExplainCmd())
|
cmdHubTest.AddCommand(NewHubTestExplainCmd())
|
||||||
|
|
||||||
return cmd
|
return cmdHubTest
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHubTest) NewCreateCmd() *cobra.Command {
|
func NewHubTestCreateCmd() *cobra.Command {
|
||||||
var (
|
|
||||||
ignoreParsers bool
|
|
||||||
labels map[string]string
|
|
||||||
logType string
|
|
||||||
)
|
|
||||||
|
|
||||||
parsers := []string{}
|
parsers := []string{}
|
||||||
postoverflows := []string{}
|
postoverflows := []string{}
|
||||||
scenarios := []string{}
|
scenarios := []string{}
|
||||||
|
var ignoreParsers bool
|
||||||
|
var labels map[string]string
|
||||||
|
var logType string
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
var cmdHubTestCreate = &cobra.Command{
|
||||||
Use: "create",
|
Use: "create",
|
||||||
Short: "create [test_name]",
|
Short: "create [test_name]",
|
||||||
Example: `cscli hubtest create my-awesome-test --type syslog
|
Example: `cscli hubtest create my-awesome-test --type syslog
|
||||||
|
@ -109,19 +87,15 @@ cscli hubtest create my-nginx-custom-test --type nginx
|
||||||
cscli hubtest create my-scenario-test --parsers crowdsecurity/nginx --scenarios crowdsecurity/http-probing`,
|
cscli hubtest create my-scenario-test --parsers crowdsecurity/nginx --scenarios crowdsecurity/http-probing`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
testName := args[0]
|
testName := args[0]
|
||||||
testPath := filepath.Join(hubPtr.HubTestPath, testName)
|
testPath := filepath.Join(hubPtr.HubTestPath, testName)
|
||||||
if _, err := os.Stat(testPath); os.IsExist(err) {
|
if _, err := os.Stat(testPath); os.IsExist(err) {
|
||||||
return fmt.Errorf("test '%s' already exists in '%s', exiting", testName, testPath)
|
return fmt.Errorf("test '%s' already exists in '%s', exiting", testName, testPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
if isAppsecTest {
|
|
||||||
logType = "appsec"
|
|
||||||
}
|
|
||||||
|
|
||||||
if logType == "" {
|
if logType == "" {
|
||||||
return errors.New("please provide a type (--type) for the test")
|
return fmt.Errorf("please provide a type (--type) for the test")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := os.MkdirAll(testPath, os.ModePerm); err != nil {
|
if err := os.MkdirAll(testPath, os.ModePerm); err != nil {
|
||||||
|
@ -135,25 +109,17 @@ cscli hubtest create my-scenario-test --parsers crowdsecurity/nginx --scenarios
|
||||||
//create empty nuclei template file
|
//create empty nuclei template file
|
||||||
nucleiFileName := fmt.Sprintf("%s.yaml", testName)
|
nucleiFileName := fmt.Sprintf("%s.yaml", testName)
|
||||||
nucleiFilePath := filepath.Join(testPath, nucleiFileName)
|
nucleiFilePath := filepath.Join(testPath, nucleiFileName)
|
||||||
|
nucleiFile, err := os.Create(nucleiFilePath)
|
||||||
nucleiFile, err := os.OpenFile(nucleiFilePath, os.O_RDWR|os.O_CREATE, 0o755)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
ntpl := template.Must(template.New("nuclei").Parse(hubtest.TemplateNucleiFile))
|
|
||||||
if ntpl == nil {
|
|
||||||
return errors.New("unable to parse nuclei template")
|
|
||||||
}
|
|
||||||
ntpl.ExecuteTemplate(nucleiFile, "nuclei", struct{ TestName string }{TestName: testName})
|
|
||||||
nucleiFile.Close()
|
nucleiFile.Close()
|
||||||
configFileData.AppsecRules = []string{"./appsec-rules/<author>/your_rule_here.yaml"}
|
configFileData.AppsecRules = []string{"your_rule_here.yaml"}
|
||||||
configFileData.NucleiTemplate = nucleiFileName
|
configFileData.NucleiTemplate = nucleiFileName
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Printf(" Test name : %s\n", testName)
|
fmt.Printf(" Test name : %s\n", testName)
|
||||||
fmt.Printf(" Test path : %s\n", testPath)
|
fmt.Printf(" Test path : %s\n", testPath)
|
||||||
fmt.Printf(" Config File : %s\n", configFilePath)
|
fmt.Printf(" Nuclei Template : %s\n", nucleiFileName)
|
||||||
fmt.Printf(" Nuclei Template : %s\n", nucleiFilePath)
|
|
||||||
} else {
|
} else {
|
||||||
// create empty log file
|
// create empty log file
|
||||||
logFileName := fmt.Sprintf("%s.log", testName)
|
logFileName := fmt.Sprintf("%s.log", testName)
|
||||||
|
@ -203,59 +169,52 @@ cscli hubtest create my-scenario-test --parsers crowdsecurity/nginx --scenarios
|
||||||
fmt.Printf(" Parser assertion file : %s (please fill it with assertion)\n", parserAssertFilePath)
|
fmt.Printf(" Parser assertion file : %s (please fill it with assertion)\n", parserAssertFilePath)
|
||||||
fmt.Printf(" Scenario assertion file : %s (please fill it with assertion)\n", scenarioAssertFilePath)
|
fmt.Printf(" Scenario assertion file : %s (please fill it with assertion)\n", scenarioAssertFilePath)
|
||||||
fmt.Printf(" Configuration File : %s (please fill it with parsers, scenarios...)\n", configFilePath)
|
fmt.Printf(" Configuration File : %s (please fill it with parsers, scenarios...)\n", configFilePath)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fd, err := os.Create(configFilePath)
|
fd, err := os.Create(configFilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("open: %w", err)
|
return fmt.Errorf("open: %s", err)
|
||||||
}
|
}
|
||||||
data, err := yaml.Marshal(configFileData)
|
data, err := yaml.Marshal(configFileData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("marshal: %w", err)
|
return fmt.Errorf("marshal: %s", err)
|
||||||
}
|
}
|
||||||
_, err = fd.Write(data)
|
_, err = fd.Write(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("write: %w", err)
|
return fmt.Errorf("write: %s", err)
|
||||||
}
|
}
|
||||||
if err := fd.Close(); err != nil {
|
if err := fd.Close(); err != nil {
|
||||||
return fmt.Errorf("close: %w", err)
|
return fmt.Errorf("close: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.PersistentFlags().StringVarP(&logType, "type", "t", "", "Log type of the test")
|
cmdHubTestCreate.PersistentFlags().StringVarP(&logType, "type", "t", "", "Log type of the test")
|
||||||
cmd.Flags().StringSliceVarP(&parsers, "parsers", "p", parsers, "Parsers to add to test")
|
cmdHubTestCreate.Flags().StringSliceVarP(&parsers, "parsers", "p", parsers, "Parsers to add to test")
|
||||||
cmd.Flags().StringSliceVar(&postoverflows, "postoverflows", postoverflows, "Postoverflows to add to test")
|
cmdHubTestCreate.Flags().StringSliceVar(&postoverflows, "postoverflows", postoverflows, "Postoverflows to add to test")
|
||||||
cmd.Flags().StringSliceVarP(&scenarios, "scenarios", "s", scenarios, "Scenarios to add to test")
|
cmdHubTestCreate.Flags().StringSliceVarP(&scenarios, "scenarios", "s", scenarios, "Scenarios to add to test")
|
||||||
cmd.PersistentFlags().BoolVar(&ignoreParsers, "ignore-parsers", false, "Don't run test on parsers")
|
cmdHubTestCreate.PersistentFlags().BoolVar(&ignoreParsers, "ignore-parsers", false, "Don't run test on parsers")
|
||||||
|
|
||||||
return cmd
|
return cmdHubTestCreate
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHubTest) NewRunCmd() *cobra.Command {
|
func NewHubTestRunCmd() *cobra.Command {
|
||||||
var (
|
var noClean bool
|
||||||
noClean bool
|
var runAll bool
|
||||||
runAll bool
|
var forceClean bool
|
||||||
forceClean bool
|
|
||||||
NucleiTargetHost string
|
|
||||||
AppSecHost string
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
var cmdHubTestRun = &cobra.Command{
|
||||||
Use: "run",
|
Use: "run",
|
||||||
Short: "run [test_name]",
|
Short: "run [test_name]",
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
|
||||||
|
|
||||||
if !runAll && len(args) == 0 {
|
if !runAll && len(args) == 0 {
|
||||||
printHelp(cmd)
|
printHelp(cmd)
|
||||||
return errors.New("please provide test to run or --all flag")
|
return fmt.Errorf("please provide test to run or --all flag")
|
||||||
}
|
}
|
||||||
hubPtr.NucleiTargetHost = NucleiTargetHost
|
|
||||||
hubPtr.AppSecHost = AppSecHost
|
|
||||||
if runAll {
|
if runAll {
|
||||||
if err := hubPtr.LoadAllTests(); err != nil {
|
if err := hubPtr.LoadAllTests(); err != nil {
|
||||||
return fmt.Errorf("unable to load all tests: %+v", err)
|
return fmt.Errorf("unable to load all tests: %+v", err)
|
||||||
|
@ -264,7 +223,7 @@ func (cli *cliHubTest) NewRunCmd() *cobra.Command {
|
||||||
for _, testName := range args {
|
for _, testName := range args {
|
||||||
_, err := hubPtr.LoadTestItem(testName)
|
_, err := hubPtr.LoadTestItem(testName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to load test '%s': %w", testName, err)
|
return fmt.Errorf("unable to load test '%s': %s", testName, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -272,7 +231,7 @@ func (cli *cliHubTest) NewRunCmd() *cobra.Command {
|
||||||
// set timezone to avoid DST issues
|
// set timezone to avoid DST issues
|
||||||
os.Setenv("TZ", "UTC")
|
os.Setenv("TZ", "UTC")
|
||||||
for _, test := range hubPtr.Tests {
|
for _, test := range hubPtr.Tests {
|
||||||
if cfg.Cscli.Output == "human" {
|
if csConfig.Cscli.Output == "human" {
|
||||||
log.Infof("Running test '%s'", test.Name)
|
log.Infof("Running test '%s'", test.Name)
|
||||||
}
|
}
|
||||||
err := test.Run()
|
err := test.Run()
|
||||||
|
@ -283,9 +242,7 @@ func (cli *cliHubTest) NewRunCmd() *cobra.Command {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
PersistentPostRunE: func(_ *cobra.Command, _ []string) error {
|
PersistentPostRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
|
||||||
|
|
||||||
success := true
|
success := true
|
||||||
testResult := make(map[string]bool)
|
testResult := make(map[string]bool)
|
||||||
for _, test := range hubPtr.Tests {
|
for _, test := range hubPtr.Tests {
|
||||||
|
@ -302,7 +259,7 @@ func (cli *cliHubTest) NewRunCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
if !noClean {
|
if !noClean {
|
||||||
if err := test.Clean(); err != nil {
|
if err := test.Clean(); err != nil {
|
||||||
return fmt.Errorf("unable to clean test '%s' env: %w", test.Name, err)
|
return fmt.Errorf("unable to clean test '%s' env: %s", test.Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fmt.Printf("\nPlease fill your assert file(s) for test '%s', exiting\n", test.Name)
|
fmt.Printf("\nPlease fill your assert file(s) for test '%s', exiting\n", test.Name)
|
||||||
|
@ -310,18 +267,18 @@ func (cli *cliHubTest) NewRunCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
testResult[test.Name] = test.Success
|
testResult[test.Name] = test.Success
|
||||||
if test.Success {
|
if test.Success {
|
||||||
if cfg.Cscli.Output == "human" {
|
if csConfig.Cscli.Output == "human" {
|
||||||
log.Infof("Test '%s' passed successfully (%d assertions)\n", test.Name, test.ParserAssert.NbAssert+test.ScenarioAssert.NbAssert)
|
log.Infof("Test '%s' passed successfully (%d assertions)\n", test.Name, test.ParserAssert.NbAssert+test.ScenarioAssert.NbAssert)
|
||||||
}
|
}
|
||||||
if !noClean {
|
if !noClean {
|
||||||
if err := test.Clean(); err != nil {
|
if err := test.Clean(); err != nil {
|
||||||
return fmt.Errorf("unable to clean test '%s' env: %w", test.Name, err)
|
return fmt.Errorf("unable to clean test '%s' env: %s", test.Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
success = false
|
success = false
|
||||||
cleanTestEnv := false
|
cleanTestEnv := false
|
||||||
if cfg.Cscli.Output == "human" {
|
if csConfig.Cscli.Output == "human" {
|
||||||
if len(test.ParserAssert.Fails) > 0 {
|
if len(test.ParserAssert.Fails) > 0 {
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
log.Errorf("Parser test '%s' failed (%d errors)\n", test.Name, len(test.ParserAssert.Fails))
|
log.Errorf("Parser test '%s' failed (%d errors)\n", test.Name, len(test.ParserAssert.Fails))
|
||||||
|
@ -352,20 +309,20 @@ func (cli *cliHubTest) NewRunCmd() *cobra.Command {
|
||||||
Default: true,
|
Default: true,
|
||||||
}
|
}
|
||||||
if err := survey.AskOne(prompt, &cleanTestEnv); err != nil {
|
if err := survey.AskOne(prompt, &cleanTestEnv); err != nil {
|
||||||
return fmt.Errorf("unable to ask to remove runtime folder: %w", err)
|
return fmt.Errorf("unable to ask to remove runtime folder: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if cleanTestEnv || forceClean {
|
if cleanTestEnv || forceClean {
|
||||||
if err := test.Clean(); err != nil {
|
if err := test.Clean(); err != nil {
|
||||||
return fmt.Errorf("unable to clean test '%s' env: %w", test.Name, err)
|
return fmt.Errorf("unable to clean test '%s' env: %s", test.Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch cfg.Cscli.Output {
|
switch csConfig.Cscli.Output {
|
||||||
case "human":
|
case "human":
|
||||||
hubTestResultTable(color.Output, testResult)
|
hubTestResultTable(color.Output, testResult)
|
||||||
case "json":
|
case "json":
|
||||||
|
@ -381,11 +338,11 @@ func (cli *cliHubTest) NewRunCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
jsonStr, err := json.Marshal(jsonResult)
|
jsonStr, err := json.Marshal(jsonResult)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to json test result: %w", err)
|
return fmt.Errorf("unable to json test result: %s", err)
|
||||||
}
|
}
|
||||||
fmt.Println(string(jsonStr))
|
fmt.Println(string(jsonStr))
|
||||||
default:
|
default:
|
||||||
return errors.New("only human/json output modes are supported")
|
return fmt.Errorf("only human/json output modes are supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
if !success {
|
if !success {
|
||||||
|
@ -396,29 +353,27 @@ func (cli *cliHubTest) NewRunCmd() *cobra.Command {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flags().BoolVar(&noClean, "no-clean", false, "Don't clean runtime environment if test succeed")
|
cmdHubTestRun.Flags().BoolVar(&noClean, "no-clean", false, "Don't clean runtime environment if test succeed")
|
||||||
cmd.Flags().BoolVar(&forceClean, "clean", false, "Clean runtime environment if test fail")
|
cmdHubTestRun.Flags().BoolVar(&forceClean, "clean", false, "Clean runtime environment if test fail")
|
||||||
cmd.Flags().StringVar(&NucleiTargetHost, "target", hubtest.DefaultNucleiTarget, "Target for AppSec Test")
|
cmdHubTestRun.Flags().BoolVar(&runAll, "all", false, "Run all tests")
|
||||||
cmd.Flags().StringVar(&AppSecHost, "host", hubtest.DefaultAppsecHost, "Address to expose AppSec for hubtest")
|
|
||||||
cmd.Flags().BoolVar(&runAll, "all", false, "Run all tests")
|
|
||||||
|
|
||||||
return cmd
|
return cmdHubTestRun
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHubTest) NewCleanCmd() *cobra.Command {
|
func NewHubTestCleanCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
var cmdHubTestClean = &cobra.Command{
|
||||||
Use: "clean",
|
Use: "clean",
|
||||||
Short: "clean [test_name]",
|
Short: "clean [test_name]",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
for _, testName := range args {
|
for _, testName := range args {
|
||||||
test, err := hubPtr.LoadTestItem(testName)
|
test, err := hubPtr.LoadTestItem(testName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to load test '%s': %w", testName, err)
|
return fmt.Errorf("unable to load test '%s': %s", testName, err)
|
||||||
}
|
}
|
||||||
if err := test.Clean(); err != nil {
|
if err := test.Clean(); err != nil {
|
||||||
return fmt.Errorf("unable to clean test '%s' env: %w", test.Name, err)
|
return fmt.Errorf("unable to clean test '%s' env: %s", test.Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -426,20 +381,21 @@ func (cli *cliHubTest) NewCleanCmd() *cobra.Command {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdHubTestClean
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHubTest) NewInfoCmd() *cobra.Command {
|
func NewHubTestInfoCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
var cmdHubTestInfo = &cobra.Command{
|
||||||
Use: "info",
|
Use: "info",
|
||||||
Short: "info [test_name]",
|
Short: "info [test_name]",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
|
||||||
for _, testName := range args {
|
for _, testName := range args {
|
||||||
test, err := hubPtr.LoadTestItem(testName)
|
test, err := hubPtr.LoadTestItem(testName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to load test '%s': %w", testName, err)
|
return fmt.Errorf("unable to load test '%s': %s", testName, err)
|
||||||
}
|
}
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
fmt.Printf(" Test name : %s\n", test.Name)
|
fmt.Printf(" Test name : %s\n", test.Name)
|
||||||
|
@ -459,22 +415,20 @@ func (cli *cliHubTest) NewInfoCmd() *cobra.Command {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdHubTestInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHubTest) NewListCmd() *cobra.Command {
|
func NewHubTestListCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
var cmdHubTestList = &cobra.Command{
|
||||||
Use: "list",
|
Use: "list",
|
||||||
Short: "list",
|
Short: "list",
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
|
||||||
|
|
||||||
if err := hubPtr.LoadAllTests(); err != nil {
|
if err := hubPtr.LoadAllTests(); err != nil {
|
||||||
return fmt.Errorf("unable to load all tests: %w", err)
|
return fmt.Errorf("unable to load all tests: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch cfg.Cscli.Output {
|
switch csConfig.Cscli.Output {
|
||||||
case "human":
|
case "human":
|
||||||
hubTestListTable(color.Output, hubPtr.Tests)
|
hubTestListTable(color.Output, hubPtr.Tests)
|
||||||
case "json":
|
case "json":
|
||||||
|
@ -484,31 +438,27 @@ func (cli *cliHubTest) NewListCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
fmt.Println(string(j))
|
fmt.Println(string(j))
|
||||||
default:
|
default:
|
||||||
return errors.New("only human/json output modes are supported")
|
return fmt.Errorf("only human/json output modes are supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdHubTestList
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHubTest) NewCoverageCmd() *cobra.Command {
|
func NewHubTestCoverageCmd() *cobra.Command {
|
||||||
var (
|
var showParserCov bool
|
||||||
showParserCov bool
|
var showScenarioCov bool
|
||||||
showScenarioCov bool
|
var showOnlyPercent bool
|
||||||
showOnlyPercent bool
|
var showAppsecCov bool
|
||||||
showAppsecCov bool
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
var cmdHubTestCoverage = &cobra.Command{
|
||||||
Use: "coverage",
|
Use: "coverage",
|
||||||
Short: "coverage",
|
Short: "coverage",
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
|
||||||
|
|
||||||
//for this one we explicitly don't do for appsec
|
//for this one we explicitly don't do for appsec
|
||||||
if err := HubTest.LoadAllTests(); err != nil {
|
if err := HubTest.LoadAllTests(); err != nil {
|
||||||
return fmt.Errorf("unable to load all tests: %+v", err)
|
return fmt.Errorf("unable to load all tests: %+v", err)
|
||||||
|
@ -527,7 +477,7 @@ func (cli *cliHubTest) NewCoverageCmd() *cobra.Command {
|
||||||
if showParserCov || showAll {
|
if showParserCov || showAll {
|
||||||
parserCoverage, err = HubTest.GetParsersCoverage()
|
parserCoverage, err = HubTest.GetParsersCoverage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("while getting parser coverage: %w", err)
|
return fmt.Errorf("while getting parser coverage: %s", err)
|
||||||
}
|
}
|
||||||
parserTested := 0
|
parserTested := 0
|
||||||
for _, test := range parserCoverage {
|
for _, test := range parserCoverage {
|
||||||
|
@ -541,7 +491,7 @@ func (cli *cliHubTest) NewCoverageCmd() *cobra.Command {
|
||||||
if showScenarioCov || showAll {
|
if showScenarioCov || showAll {
|
||||||
scenarioCoverage, err = HubTest.GetScenariosCoverage()
|
scenarioCoverage, err = HubTest.GetScenariosCoverage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("while getting scenario coverage: %w", err)
|
return fmt.Errorf("while getting scenario coverage: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
scenarioTested := 0
|
scenarioTested := 0
|
||||||
|
@ -557,7 +507,7 @@ func (cli *cliHubTest) NewCoverageCmd() *cobra.Command {
|
||||||
if showAppsecCov || showAll {
|
if showAppsecCov || showAll {
|
||||||
appsecRuleCoverage, err = HubTest.GetAppsecCoverage()
|
appsecRuleCoverage, err = HubTest.GetAppsecCoverage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("while getting scenario coverage: %w", err)
|
return fmt.Errorf("while getting scenario coverage: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
appsecRuleTested := 0
|
appsecRuleTested := 0
|
||||||
|
@ -570,20 +520,19 @@ func (cli *cliHubTest) NewCoverageCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
if showOnlyPercent {
|
if showOnlyPercent {
|
||||||
switch {
|
if showAll {
|
||||||
case showAll:
|
|
||||||
fmt.Printf("parsers=%d%%\nscenarios=%d%%\nappsec_rules=%d%%", parserCoveragePercent, scenarioCoveragePercent, appsecRuleCoveragePercent)
|
fmt.Printf("parsers=%d%%\nscenarios=%d%%\nappsec_rules=%d%%", parserCoveragePercent, scenarioCoveragePercent, appsecRuleCoveragePercent)
|
||||||
case showParserCov:
|
} else if showParserCov {
|
||||||
fmt.Printf("parsers=%d%%", parserCoveragePercent)
|
fmt.Printf("parsers=%d%%", parserCoveragePercent)
|
||||||
case showScenarioCov:
|
} else if showScenarioCov {
|
||||||
fmt.Printf("scenarios=%d%%", scenarioCoveragePercent)
|
fmt.Printf("scenarios=%d%%", scenarioCoveragePercent)
|
||||||
case showAppsecCov:
|
} else if showAppsecCov {
|
||||||
fmt.Printf("appsec_rules=%d%%", appsecRuleCoveragePercent)
|
fmt.Printf("appsec_rules=%d%%", appsecRuleCoveragePercent)
|
||||||
}
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
switch cfg.Cscli.Output {
|
switch csConfig.Cscli.Output {
|
||||||
case "human":
|
case "human":
|
||||||
if showParserCov || showAll {
|
if showParserCov || showAll {
|
||||||
hubTestParserCoverageTable(color.Output, parserCoverage)
|
hubTestParserCoverageTable(color.Output, parserCoverage)
|
||||||
|
@ -624,30 +573,29 @@ func (cli *cliHubTest) NewCoverageCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
fmt.Printf("%s", dump)
|
fmt.Printf("%s", dump)
|
||||||
default:
|
default:
|
||||||
return errors.New("only human/json output modes are supported")
|
return fmt.Errorf("only human/json output modes are supported")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.PersistentFlags().BoolVar(&showOnlyPercent, "percent", false, "Show only percentages of coverage")
|
cmdHubTestCoverage.PersistentFlags().BoolVar(&showOnlyPercent, "percent", false, "Show only percentages of coverage")
|
||||||
cmd.PersistentFlags().BoolVar(&showParserCov, "parsers", false, "Show only parsers coverage")
|
cmdHubTestCoverage.PersistentFlags().BoolVar(&showParserCov, "parsers", false, "Show only parsers coverage")
|
||||||
cmd.PersistentFlags().BoolVar(&showScenarioCov, "scenarios", false, "Show only scenarios coverage")
|
cmdHubTestCoverage.PersistentFlags().BoolVar(&showScenarioCov, "scenarios", false, "Show only scenarios coverage")
|
||||||
cmd.PersistentFlags().BoolVar(&showAppsecCov, "appsec", false, "Show only appsec coverage")
|
cmdHubTestCoverage.PersistentFlags().BoolVar(&showAppsecCov, "appsec", false, "Show only appsec coverage")
|
||||||
|
|
||||||
return cmd
|
return cmdHubTestCoverage
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHubTest) NewEvalCmd() *cobra.Command {
|
func NewHubTestEvalCmd() *cobra.Command {
|
||||||
var evalExpression string
|
var evalExpression string
|
||||||
|
var cmdHubTestEval = &cobra.Command{
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "eval",
|
Use: "eval",
|
||||||
Short: "eval [test_name]",
|
Short: "eval [test_name]",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
for _, testName := range args {
|
for _, testName := range args {
|
||||||
test, err := hubPtr.LoadTestItem(testName)
|
test, err := hubPtr.LoadTestItem(testName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -671,18 +619,18 @@ func (cli *cliHubTest) NewEvalCmd() *cobra.Command {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.PersistentFlags().StringVarP(&evalExpression, "expr", "e", "", "Expression to eval")
|
cmdHubTestEval.PersistentFlags().StringVarP(&evalExpression, "expr", "e", "", "Expression to eval")
|
||||||
|
|
||||||
return cmd
|
return cmdHubTestEval
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliHubTest) NewExplainCmd() *cobra.Command {
|
func NewHubTestExplainCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
var cmdHubTestExplain = &cobra.Command{
|
||||||
Use: "explain",
|
Use: "explain",
|
||||||
Short: "explain [test_name]",
|
Short: "explain [test_name]",
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
for _, testName := range args {
|
for _, testName := range args {
|
||||||
test, err := HubTest.LoadTestItem(testName)
|
test, err := HubTest.LoadTestItem(testName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -695,7 +643,7 @@ func (cli *cliHubTest) NewExplainCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = test.ParserAssert.LoadTest(test.ParserResultFile); err != nil {
|
if err = test.ParserAssert.LoadTest(test.ParserResultFile); err != nil {
|
||||||
return fmt.Errorf("unable to load parser result after run: %w", err)
|
return fmt.Errorf("unable to load parser result after run: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -706,16 +654,16 @@ func (cli *cliHubTest) NewExplainCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile); err != nil {
|
if err = test.ScenarioAssert.LoadTest(test.ScenarioResultFile, test.BucketPourResultFile); err != nil {
|
||||||
return fmt.Errorf("unable to load scenario result after run: %w", err)
|
return fmt.Errorf("unable to load scenario result after run: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
opts := dumps.DumpOpts{}
|
opts := hubtest.DumpOpts{}
|
||||||
dumps.DumpTree(*test.ParserAssert.TestData, *test.ScenarioAssert.PourData, opts)
|
hubtest.DumpTree(*test.ParserAssert.TestData, *test.ScenarioAssert.PourData, opts)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdHubTestExplain
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,8 +5,8 @@ import (
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"github.com/aquasecurity/table"
|
"github.com/aquasecurity/table"
|
||||||
|
"github.com/enescakir/emoji"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/emoji"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/hubtest"
|
"github.com/crowdsecurity/crowdsec/pkg/hubtest"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -17,9 +17,9 @@ func hubTestResultTable(out io.Writer, testResult map[string]bool) {
|
||||||
t.SetAlignment(table.AlignLeft)
|
t.SetAlignment(table.AlignLeft)
|
||||||
|
|
||||||
for testName, success := range testResult {
|
for testName, success := range testResult {
|
||||||
status := emoji.CheckMarkButton
|
status := emoji.CheckMarkButton.String()
|
||||||
if !success {
|
if !success {
|
||||||
status = emoji.CrossMark
|
status = emoji.CrossMark.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
t.AddRow(testName, status)
|
t.AddRow(testName, status)
|
||||||
|
@ -50,12 +50,11 @@ func hubTestParserCoverageTable(out io.Writer, coverage []hubtest.Coverage) {
|
||||||
parserTested := 0
|
parserTested := 0
|
||||||
|
|
||||||
for _, test := range coverage {
|
for _, test := range coverage {
|
||||||
status := emoji.RedCircle
|
status := emoji.RedCircle.String()
|
||||||
if test.TestsCount > 0 {
|
if test.TestsCount > 0 {
|
||||||
status = emoji.GreenCircle
|
status = emoji.GreenCircle.String()
|
||||||
parserTested++
|
parserTested++
|
||||||
}
|
}
|
||||||
|
|
||||||
t.AddRow(test.Name, status, fmt.Sprintf("%d times (across %d tests)", test.TestsCount, len(test.PresentIn)))
|
t.AddRow(test.Name, status, fmt.Sprintf("%d times (across %d tests)", test.TestsCount, len(test.PresentIn)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,12 +70,11 @@ func hubTestAppsecRuleCoverageTable(out io.Writer, coverage []hubtest.Coverage)
|
||||||
parserTested := 0
|
parserTested := 0
|
||||||
|
|
||||||
for _, test := range coverage {
|
for _, test := range coverage {
|
||||||
status := emoji.RedCircle
|
status := emoji.RedCircle.String()
|
||||||
if test.TestsCount > 0 {
|
if test.TestsCount > 0 {
|
||||||
status = emoji.GreenCircle
|
status = emoji.GreenCircle.String()
|
||||||
parserTested++
|
parserTested++
|
||||||
}
|
}
|
||||||
|
|
||||||
t.AddRow(test.Name, status, fmt.Sprintf("%d times (across %d tests)", test.TestsCount, len(test.PresentIn)))
|
t.AddRow(test.Name, status, fmt.Sprintf("%d times (across %d tests)", test.TestsCount, len(test.PresentIn)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,12 +90,11 @@ func hubTestScenarioCoverageTable(out io.Writer, coverage []hubtest.Coverage) {
|
||||||
parserTested := 0
|
parserTested := 0
|
||||||
|
|
||||||
for _, test := range coverage {
|
for _, test := range coverage {
|
||||||
status := emoji.RedCircle
|
status := emoji.RedCircle.String()
|
||||||
if test.TestsCount > 0 {
|
if test.TestsCount > 0 {
|
||||||
status = emoji.GreenCircle
|
status = emoji.GreenCircle.String()
|
||||||
parserTested++
|
parserTested++
|
||||||
}
|
}
|
||||||
|
|
||||||
t.AddRow(test.Name, status, fmt.Sprintf("%d times (across %d tests)", test.TestsCount, len(test.PresentIn)))
|
t.AddRow(test.Name, status, fmt.Sprintf("%d times (across %d tests)", test.TestsCount, len(test.PresentIn)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,11 +33,10 @@ func ShowMetrics(hubItem *cwhub.Item) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case cwhub.APPSEC_RULES:
|
case cwhub.APPSEC_RULES:
|
||||||
metrics := GetAppsecRuleMetric(csConfig.Cscli.PrometheusUrl, hubItem.Name)
|
log.Error("FIXME: not implemented yet")
|
||||||
appsecMetricsTable(color.Output, hubItem.Name, metrics)
|
default:
|
||||||
default: // no metrics for this item type
|
// no metrics for this item type
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,27 +49,21 @@ func GetParserMetric(url string, itemName string) map[string]map[string]int {
|
||||||
if !strings.HasPrefix(fam.Name, "cs_") {
|
if !strings.HasPrefix(fam.Name, "cs_") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Tracef("round %d", idx)
|
log.Tracef("round %d", idx)
|
||||||
|
|
||||||
for _, m := range fam.Metrics {
|
for _, m := range fam.Metrics {
|
||||||
metric, ok := m.(prom2json.Metric)
|
metric, ok := m.(prom2json.Metric)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Debugf("failed to convert metric to prom2json.Metric")
|
log.Debugf("failed to convert metric to prom2json.Metric")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
name, ok := metric.Labels["name"]
|
name, ok := metric.Labels["name"]
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Debugf("no name in Metric %v", metric.Labels)
|
log.Debugf("no name in Metric %v", metric.Labels)
|
||||||
}
|
}
|
||||||
|
|
||||||
if name != itemName {
|
if name != itemName {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
source, ok := metric.Labels["source"]
|
source, ok := metric.Labels["source"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Debugf("no source in Metric %v", metric.Labels)
|
log.Debugf("no source in Metric %v", metric.Labels)
|
||||||
} else {
|
} else {
|
||||||
|
@ -78,15 +71,12 @@ func GetParserMetric(url string, itemName string) map[string]map[string]int {
|
||||||
source = srctype + ":" + source
|
source = srctype + ":" + source
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
value := m.(prom2json.Metric).Value
|
value := m.(prom2json.Metric).Value
|
||||||
|
|
||||||
fval, err := strconv.ParseFloat(value, 32)
|
fval, err := strconv.ParseFloat(value, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Unexpected int value %s : %s", value, err)
|
log.Errorf("Unexpected int value %s : %s", value, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ival := int(fval)
|
ival := int(fval)
|
||||||
|
|
||||||
switch fam.Name {
|
switch fam.Name {
|
||||||
|
@ -129,7 +119,6 @@ func GetParserMetric(url string, itemName string) map[string]map[string]int {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return stats
|
return stats
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,34 +136,26 @@ func GetScenarioMetric(url string, itemName string) map[string]int {
|
||||||
if !strings.HasPrefix(fam.Name, "cs_") {
|
if !strings.HasPrefix(fam.Name, "cs_") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Tracef("round %d", idx)
|
log.Tracef("round %d", idx)
|
||||||
|
|
||||||
for _, m := range fam.Metrics {
|
for _, m := range fam.Metrics {
|
||||||
metric, ok := m.(prom2json.Metric)
|
metric, ok := m.(prom2json.Metric)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Debugf("failed to convert metric to prom2json.Metric")
|
log.Debugf("failed to convert metric to prom2json.Metric")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
name, ok := metric.Labels["name"]
|
name, ok := metric.Labels["name"]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Debugf("no name in Metric %v", metric.Labels)
|
log.Debugf("no name in Metric %v", metric.Labels)
|
||||||
}
|
}
|
||||||
|
|
||||||
if name != itemName {
|
if name != itemName {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
value := m.(prom2json.Metric).Value
|
value := m.(prom2json.Metric).Value
|
||||||
|
|
||||||
fval, err := strconv.ParseFloat(value, 32)
|
fval, err := strconv.ParseFloat(value, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Unexpected int value %s : %s", value, err)
|
log.Errorf("Unexpected int value %s : %s", value, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
ival := int(fval)
|
ival := int(fval)
|
||||||
|
|
||||||
switch fam.Name {
|
switch fam.Name {
|
||||||
|
@ -193,72 +174,6 @@ func GetScenarioMetric(url string, itemName string) map[string]int {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return stats
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetAppsecRuleMetric(url string, itemName string) map[string]int {
|
|
||||||
stats := make(map[string]int)
|
|
||||||
|
|
||||||
stats["inband_hits"] = 0
|
|
||||||
stats["outband_hits"] = 0
|
|
||||||
|
|
||||||
results := GetPrometheusMetric(url)
|
|
||||||
for idx, fam := range results {
|
|
||||||
if !strings.HasPrefix(fam.Name, "cs_") {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Tracef("round %d", idx)
|
|
||||||
|
|
||||||
for _, m := range fam.Metrics {
|
|
||||||
metric, ok := m.(prom2json.Metric)
|
|
||||||
if !ok {
|
|
||||||
log.Debugf("failed to convert metric to prom2json.Metric")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
name, ok := metric.Labels["rule_name"]
|
|
||||||
|
|
||||||
if !ok {
|
|
||||||
log.Debugf("no rule_name in Metric %v", metric.Labels)
|
|
||||||
}
|
|
||||||
|
|
||||||
if name != itemName {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
band, ok := metric.Labels["type"]
|
|
||||||
if !ok {
|
|
||||||
log.Debugf("no type in Metric %v", metric.Labels)
|
|
||||||
}
|
|
||||||
|
|
||||||
value := m.(prom2json.Metric).Value
|
|
||||||
|
|
||||||
fval, err := strconv.ParseFloat(value, 32)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("Unexpected int value %s : %s", value, err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
ival := int(fval)
|
|
||||||
|
|
||||||
switch fam.Name {
|
|
||||||
case "cs_appsec_rule_hits":
|
|
||||||
switch band {
|
|
||||||
case "inband":
|
|
||||||
stats["inband_hits"] += ival
|
|
||||||
case "outband":
|
|
||||||
stats["outband_hits"] += ival
|
|
||||||
default:
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return stats
|
return stats
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -275,7 +190,6 @@ func GetPrometheusMetric(url string) []*prom2json.Family {
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer trace.CatchPanic("crowdsec/GetPrometheusMetric")
|
defer trace.CatchPanic("crowdsec/GetPrometheusMetric")
|
||||||
|
|
||||||
err := prom2json.FetchMetricFamilies(url, mfChan, transport)
|
err := prom2json.FetchMetricFamilies(url, mfChan, transport)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to fetch prometheus metrics : %v", err)
|
log.Fatalf("failed to fetch prometheus metrics : %v", err)
|
||||||
|
@ -286,7 +200,6 @@ func GetPrometheusMetric(url string) []*prom2json.Family {
|
||||||
for mf := range mfChan {
|
for mf := range mfChan {
|
||||||
result = append(result, prom2json.NewFamily(mf))
|
result = append(result, prom2json.NewFamily(mf))
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("Finished reading prometheus output, %d entries", len(result))
|
log.Debugf("Finished reading prometheus output, %d entries", len(result))
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
@ -309,7 +222,6 @@ var ranges = []unit{
|
||||||
|
|
||||||
func formatNumber(num int) string {
|
func formatNumber(num int) string {
|
||||||
goodUnit := unit{}
|
goodUnit := unit{}
|
||||||
|
|
||||||
for _, u := range ranges {
|
for _, u := range ranges {
|
||||||
if int64(num) >= u.value {
|
if int64(num) >= u.value {
|
||||||
goodUnit = u
|
goodUnit = u
|
||||||
|
@ -322,6 +234,5 @@ func formatNumber(num int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
res := math.Round(float64(num)/float64(goodUnit.value)*100) / 100
|
res := math.Round(float64(num)/float64(goodUnit.value)*100) / 100
|
||||||
|
|
||||||
return fmt.Sprintf("%.2f%s", res, goodUnit.symbol)
|
return fmt.Sprintf("%.2f%s", res, goodUnit.symbol)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,11 +2,11 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"slices"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/agext/levenshtein"
|
"github.com/agext/levenshtein"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
|
@ -37,7 +37,7 @@ func suggestNearestMessage(hub *cwhub.Hub, itemType string, itemName string) str
|
||||||
}
|
}
|
||||||
|
|
||||||
func compAllItems(itemType string, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
func compAllItems(itemType string, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
hub, err := require.Hub(csConfig, nil, nil)
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, cobra.ShellCompDirectiveDefault
|
return nil, cobra.ShellCompDirectiveDefault
|
||||||
}
|
}
|
||||||
|
@ -56,12 +56,12 @@ func compAllItems(itemType string, args []string, toComplete string) ([]string,
|
||||||
}
|
}
|
||||||
|
|
||||||
func compInstalledItems(itemType string, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
func compInstalledItems(itemType string, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
hub, err := require.Hub(csConfig, nil, nil)
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, cobra.ShellCompDirectiveDefault
|
return nil, cobra.ShellCompDirectiveDefault
|
||||||
}
|
}
|
||||||
|
|
||||||
items, err := hub.GetInstalledNamesByType(itemType)
|
items, err := hub.GetInstalledItemNames(itemType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cobra.CompDebugln(fmt.Sprintf("list installed %s err: %s", itemType, err), true)
|
cobra.CompDebugln(fmt.Sprintf("list installed %s err: %s", itemType, err), true)
|
||||||
return nil, cobra.ShellCompDirectiveDefault
|
return nil, cobra.ShellCompDirectiveDefault
|
||||||
|
|
|
@ -1,15 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
"github.com/hexops/gotextdiff"
|
|
||||||
"github.com/hexops/gotextdiff/myers"
|
|
||||||
"github.com/hexops/gotextdiff/span"
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
@ -28,8 +22,7 @@ type cliHelp struct {
|
||||||
example string
|
example string
|
||||||
}
|
}
|
||||||
|
|
||||||
type cliItem struct {
|
type itemCLI struct {
|
||||||
cfg configGetter
|
|
||||||
name string // plural, as used in the hub index
|
name string // plural, as used in the hub index
|
||||||
singular string
|
singular string
|
||||||
oneOrMore string // parenthetical pluralizaion: "parser(s)"
|
oneOrMore string // parenthetical pluralizaion: "parser(s)"
|
||||||
|
@ -42,40 +35,55 @@ type cliItem struct {
|
||||||
listHelp cliHelp
|
listHelp cliHelp
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliItem) NewCommand() *cobra.Command {
|
func (it itemCLI) NewCommand() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: coalesce.String(cli.help.use, fmt.Sprintf("%s <action> [item]...", cli.name)),
|
Use: coalesce.String(it.help.use, fmt.Sprintf("%s <action> [item]...", it.name)),
|
||||||
Short: coalesce.String(cli.help.short, fmt.Sprintf("Manage hub %s", cli.name)),
|
Short: coalesce.String(it.help.short, fmt.Sprintf("Manage hub %s", it.name)),
|
||||||
Long: cli.help.long,
|
Long: it.help.long,
|
||||||
Example: cli.help.example,
|
Example: it.help.example,
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
Aliases: []string{cli.singular},
|
Aliases: []string{it.singular},
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.newInstallCmd())
|
cmd.AddCommand(it.NewInstallCmd())
|
||||||
cmd.AddCommand(cli.newRemoveCmd())
|
cmd.AddCommand(it.NewRemoveCmd())
|
||||||
cmd.AddCommand(cli.newUpgradeCmd())
|
cmd.AddCommand(it.NewUpgradeCmd())
|
||||||
cmd.AddCommand(cli.newInspectCmd())
|
cmd.AddCommand(it.NewInspectCmd())
|
||||||
cmd.AddCommand(cli.newListCmd())
|
cmd.AddCommand(it.NewListCmd())
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliItem) install(args []string, downloadOnly bool, force bool, ignoreError bool) error {
|
func (it itemCLI) Install(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
flags := cmd.Flags()
|
||||||
|
|
||||||
hub, err := require.Hub(cfg, require.RemoteHub(cfg), log.StandardLogger())
|
downloadOnly, err := flags.GetBool("download-only")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
force, err := flags.GetBool("force")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
ignoreError, err := flags.GetBool("ignore")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hub, err := require.Hub(csConfig, require.RemoteHub(csConfig))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, name := range args {
|
for _, name := range args {
|
||||||
item := hub.GetItem(cli.name, name)
|
item := hub.GetItem(it.name, name)
|
||||||
if item == nil {
|
if item == nil {
|
||||||
msg := suggestNearestMessage(hub, cli.name, name)
|
msg := suggestNearestMessage(hub, it.name, name)
|
||||||
if !ignoreError {
|
if !ignoreError {
|
||||||
return errors.New(msg)
|
return fmt.Errorf(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Errorf(msg)
|
log.Errorf(msg)
|
||||||
|
@ -87,42 +95,32 @@ func (cli cliItem) install(args []string, downloadOnly bool, force bool, ignoreE
|
||||||
if !ignoreError {
|
if !ignoreError {
|
||||||
return fmt.Errorf("error while installing '%s': %w", item.Name, err)
|
return fmt.Errorf("error while installing '%s': %w", item.Name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Errorf("Error while installing '%s': %s", item.Name, err)
|
log.Errorf("Error while installing '%s': %s", item.Name, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof(ReloadMessage())
|
log.Infof(ReloadMessage())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliItem) newInstallCmd() *cobra.Command {
|
func (it itemCLI) NewInstallCmd() *cobra.Command {
|
||||||
var (
|
|
||||||
downloadOnly bool
|
|
||||||
force bool
|
|
||||||
ignoreError bool
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: coalesce.String(cli.installHelp.use, "install [item]..."),
|
Use: coalesce.String(it.installHelp.use, "install [item]..."),
|
||||||
Short: coalesce.String(cli.installHelp.short, fmt.Sprintf("Install given %s", cli.oneOrMore)),
|
Short: coalesce.String(it.installHelp.short, fmt.Sprintf("Install given %s", it.oneOrMore)),
|
||||||
Long: coalesce.String(cli.installHelp.long, fmt.Sprintf("Fetch and install one or more %s from the hub", cli.name)),
|
Long: coalesce.String(it.installHelp.long, fmt.Sprintf("Fetch and install one or more %s from the hub", it.name)),
|
||||||
Example: cli.installHelp.example,
|
Example: it.installHelp.example,
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
return compAllItems(cli.name, args, toComplete)
|
return compAllItems(it.name, args, toComplete)
|
||||||
},
|
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
|
||||||
return cli.install(args, downloadOnly, force, ignoreError)
|
|
||||||
},
|
},
|
||||||
|
RunE: it.Install,
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
|
flags.BoolP("download-only", "d", false, "Only download packages, don't enable")
|
||||||
flags.BoolVar(&force, "force", false, "Force install: overwrite tainted and outdated files")
|
flags.Bool("force", false, "Force install: overwrite tainted and outdated files")
|
||||||
flags.BoolVar(&ignoreError, "ignore", false, fmt.Sprintf("Ignore errors when installing multiple %s", cli.name))
|
flags.Bool("ignore", false, fmt.Sprintf("Ignore errors when installing multiple %s", it.name))
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
@ -140,19 +138,36 @@ func istalledParentNames(item *cwhub.Item) []string {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliItem) remove(args []string, purge bool, force bool, all bool) error {
|
func (it itemCLI) Remove(cmd *cobra.Command, args []string) error {
|
||||||
hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger())
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
purge, err := flags.GetBool("purge")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
force, err := flags.GetBool("force")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
all, err := flags.GetBool("all")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if all {
|
if all {
|
||||||
getter := hub.GetInstalledItemsByType
|
getter := hub.GetInstalledItems
|
||||||
if purge {
|
if purge {
|
||||||
getter = hub.GetItemsByType
|
getter = hub.GetAllItems
|
||||||
}
|
}
|
||||||
|
|
||||||
items, err := getter(cli.name)
|
items, err := getter(it.name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -164,16 +179,13 @@ func (cli cliItem) remove(args []string, purge bool, force bool, all bool) error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if didRemove {
|
if didRemove {
|
||||||
log.Infof("Removed %s", item.Name)
|
log.Infof("Removed %s", item.Name)
|
||||||
|
|
||||||
removed++
|
removed++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Removed %d %s", removed, cli.name)
|
log.Infof("Removed %d %s", removed, it.name)
|
||||||
|
|
||||||
if removed > 0 {
|
if removed > 0 {
|
||||||
log.Infof(ReloadMessage())
|
log.Infof(ReloadMessage())
|
||||||
}
|
}
|
||||||
|
@ -182,23 +194,22 @@ func (cli cliItem) remove(args []string, purge bool, force bool, all bool) error
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return fmt.Errorf("specify at least one %s to remove or '--all'", cli.singular)
|
return fmt.Errorf("specify at least one %s to remove or '--all'", it.singular)
|
||||||
}
|
}
|
||||||
|
|
||||||
removed := 0
|
removed := 0
|
||||||
|
|
||||||
for _, itemName := range args {
|
for _, itemName := range args {
|
||||||
item := hub.GetItem(cli.name, itemName)
|
item := hub.GetItem(it.name, itemName)
|
||||||
if item == nil {
|
if item == nil {
|
||||||
return fmt.Errorf("can't find '%s' in %s", itemName, cli.name)
|
return fmt.Errorf("can't find '%s' in %s", itemName, it.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
parents := istalledParentNames(item)
|
parents := istalledParentNames(item)
|
||||||
|
|
||||||
if !force && len(parents) > 0 {
|
if !force && len(parents) > 0 {
|
||||||
log.Warningf("%s belongs to collections: %s", item.Name, parents)
|
log.Warningf("%s belongs to collections: %s", item.Name, parents)
|
||||||
log.Warningf("Run 'sudo cscli %s remove %s --force' if you want to force remove this %s", item.Type, item.Name, cli.singular)
|
log.Warningf("Run 'sudo cscli %s remove %s --force' if you want to force remove this %s", item.Type, item.Name, it.singular)
|
||||||
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,13 +220,11 @@ func (cli cliItem) remove(args []string, purge bool, force bool, all bool) error
|
||||||
|
|
||||||
if didRemove {
|
if didRemove {
|
||||||
log.Infof("Removed %s", item.Name)
|
log.Infof("Removed %s", item.Name)
|
||||||
|
|
||||||
removed++
|
removed++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Removed %d %s", removed, cli.name)
|
log.Infof("Removed %d %s", removed, it.name)
|
||||||
|
|
||||||
if removed > 0 {
|
if removed > 0 {
|
||||||
log.Infof(ReloadMessage())
|
log.Infof(ReloadMessage())
|
||||||
}
|
}
|
||||||
|
@ -223,46 +232,48 @@ func (cli cliItem) remove(args []string, purge bool, force bool, all bool) error
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliItem) newRemoveCmd() *cobra.Command {
|
func (it itemCLI) NewRemoveCmd() *cobra.Command {
|
||||||
var (
|
|
||||||
purge bool
|
|
||||||
force bool
|
|
||||||
all bool
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: coalesce.String(cli.removeHelp.use, "remove [item]..."),
|
Use: coalesce.String(it.removeHelp.use, "remove [item]..."),
|
||||||
Short: coalesce.String(cli.removeHelp.short, fmt.Sprintf("Remove given %s", cli.oneOrMore)),
|
Short: coalesce.String(it.removeHelp.short, fmt.Sprintf("Remove given %s", it.oneOrMore)),
|
||||||
Long: coalesce.String(cli.removeHelp.long, fmt.Sprintf("Remove one or more %s", cli.name)),
|
Long: coalesce.String(it.removeHelp.long, fmt.Sprintf("Remove one or more %s", it.name)),
|
||||||
Example: cli.removeHelp.example,
|
Example: it.removeHelp.example,
|
||||||
Aliases: []string{"delete"},
|
Aliases: []string{"delete"},
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
return compInstalledItems(cli.name, args, toComplete)
|
return compInstalledItems(it.name, args, toComplete)
|
||||||
},
|
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
|
||||||
return cli.remove(args, purge, force, all)
|
|
||||||
},
|
},
|
||||||
|
RunE: it.Remove,
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.BoolVar(&purge, "purge", false, "Delete source file too")
|
flags.Bool("purge", false, "Delete source file too")
|
||||||
flags.BoolVar(&force, "force", false, "Force remove: remove tainted and outdated files")
|
flags.Bool("force", false, "Force remove: remove tainted and outdated files")
|
||||||
flags.BoolVar(&all, "all", false, fmt.Sprintf("Remove all the %s", cli.name))
|
flags.Bool("all", false, fmt.Sprintf("Remove all the %s", it.name))
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliItem) upgrade(args []string, force bool, all bool) error {
|
func (it itemCLI) Upgrade(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
flags := cmd.Flags()
|
||||||
|
|
||||||
hub, err := require.Hub(cfg, require.RemoteHub(cfg), log.StandardLogger())
|
force, err := flags.GetBool("force")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
all, err := flags.GetBool("all")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hub, err := require.Hub(csConfig, require.RemoteHub(csConfig))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if all {
|
if all {
|
||||||
items, err := hub.GetInstalledItemsByType(cli.name)
|
items, err := hub.GetInstalledItems(it.name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -274,13 +285,12 @@ func (cli cliItem) upgrade(args []string, force bool, all bool) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if didUpdate {
|
if didUpdate {
|
||||||
updated++
|
updated++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Updated %d %s", updated, cli.name)
|
log.Infof("Updated %d %s", updated, it.name)
|
||||||
|
|
||||||
if updated > 0 {
|
if updated > 0 {
|
||||||
log.Infof(ReloadMessage())
|
log.Infof(ReloadMessage())
|
||||||
|
@ -290,15 +300,15 @@ func (cli cliItem) upgrade(args []string, force bool, all bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return fmt.Errorf("specify at least one %s to upgrade or '--all'", cli.singular)
|
return fmt.Errorf("specify at least one %s to upgrade or '--all'", it.singular)
|
||||||
}
|
}
|
||||||
|
|
||||||
updated := 0
|
updated := 0
|
||||||
|
|
||||||
for _, itemName := range args {
|
for _, itemName := range args {
|
||||||
item := hub.GetItem(cli.name, itemName)
|
item := hub.GetItem(it.name, itemName)
|
||||||
if item == nil {
|
if item == nil {
|
||||||
return fmt.Errorf("can't find '%s' in %s", itemName, cli.name)
|
return fmt.Errorf("can't find '%s' in %s", itemName, it.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
didUpdate, err := item.Upgrade(force)
|
didUpdate, err := item.Upgrade(force)
|
||||||
|
@ -308,11 +318,9 @@ func (cli cliItem) upgrade(args []string, force bool, all bool) error {
|
||||||
|
|
||||||
if didUpdate {
|
if didUpdate {
|
||||||
log.Infof("Updated %s", item.Name)
|
log.Infof("Updated %s", item.Name)
|
||||||
|
|
||||||
updated++
|
updated++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if updated > 0 {
|
if updated > 0 {
|
||||||
log.Infof(ReloadMessage())
|
log.Infof(ReloadMessage())
|
||||||
}
|
}
|
||||||
|
@ -320,73 +328,59 @@ func (cli cliItem) upgrade(args []string, force bool, all bool) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliItem) newUpgradeCmd() *cobra.Command {
|
func (it itemCLI) NewUpgradeCmd() *cobra.Command {
|
||||||
var (
|
|
||||||
all bool
|
|
||||||
force bool
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: coalesce.String(cli.upgradeHelp.use, "upgrade [item]..."),
|
Use: coalesce.String(it.upgradeHelp.use, "upgrade [item]..."),
|
||||||
Short: coalesce.String(cli.upgradeHelp.short, fmt.Sprintf("Upgrade given %s", cli.oneOrMore)),
|
Short: coalesce.String(it.upgradeHelp.short, fmt.Sprintf("Upgrade given %s", it.oneOrMore)),
|
||||||
Long: coalesce.String(cli.upgradeHelp.long, fmt.Sprintf("Fetch and upgrade one or more %s from the hub", cli.name)),
|
Long: coalesce.String(it.upgradeHelp.long, fmt.Sprintf("Fetch and upgrade one or more %s from the hub", it.name)),
|
||||||
Example: cli.upgradeHelp.example,
|
Example: it.upgradeHelp.example,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
return compInstalledItems(cli.name, args, toComplete)
|
return compInstalledItems(it.name, args, toComplete)
|
||||||
},
|
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
|
||||||
return cli.upgrade(args, force, all)
|
|
||||||
},
|
},
|
||||||
|
RunE: it.Upgrade,
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.BoolVarP(&all, "all", "a", false, fmt.Sprintf("Upgrade all the %s", cli.name))
|
flags.BoolP("all", "a", false, fmt.Sprintf("Upgrade all the %s", it.name))
|
||||||
flags.BoolVar(&force, "force", false, "Force upgrade: overwrite tainted and outdated files")
|
flags.Bool("force", false, "Force upgrade: overwrite tainted and outdated files")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliItem) inspect(args []string, url string, diff bool, rev bool, noMetrics bool) error {
|
func (it itemCLI) Inspect(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
flags := cmd.Flags()
|
||||||
|
|
||||||
if rev && !diff {
|
url, err := flags.GetString("url")
|
||||||
return errors.New("--rev can only be used with --diff")
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if url != "" {
|
if url != "" {
|
||||||
cfg.Cscli.PrometheusUrl = url
|
csConfig.Cscli.PrometheusUrl = url
|
||||||
}
|
}
|
||||||
|
|
||||||
remote := (*cwhub.RemoteHubCfg)(nil)
|
noMetrics, err := flags.GetBool("no-metrics")
|
||||||
|
if err != nil {
|
||||||
if diff {
|
return err
|
||||||
remote = require.RemoteHub(cfg)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hub, err := require.Hub(cfg, remote, log.StandardLogger())
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, name := range args {
|
for _, name := range args {
|
||||||
item := hub.GetItem(cli.name, name)
|
item := hub.GetItem(it.name, name)
|
||||||
if item == nil {
|
if item == nil {
|
||||||
return fmt.Errorf("can't find '%s' in %s", name, cli.name)
|
return fmt.Errorf("can't find '%s' in %s", name, it.name)
|
||||||
}
|
}
|
||||||
|
if err = InspectItem(item, !noMetrics); err != nil {
|
||||||
if diff {
|
|
||||||
fmt.Println(cli.whyTainted(hub, item, rev))
|
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if err = inspectItem(item, !noMetrics); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cli.inspectDetail != nil {
|
if it.inspectDetail != nil {
|
||||||
if err = cli.inspectDetail(item); err != nil {
|
if err = it.inspectDetail(item); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -395,152 +389,66 @@ func (cli cliItem) inspect(args []string, url string, diff bool, rev bool, noMet
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliItem) newInspectCmd() *cobra.Command {
|
func (it itemCLI) NewInspectCmd() *cobra.Command {
|
||||||
var (
|
|
||||||
url string
|
|
||||||
diff bool
|
|
||||||
rev bool
|
|
||||||
noMetrics bool
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: coalesce.String(cli.inspectHelp.use, "inspect [item]..."),
|
Use: coalesce.String(it.inspectHelp.use, "inspect [item]..."),
|
||||||
Short: coalesce.String(cli.inspectHelp.short, fmt.Sprintf("Inspect given %s", cli.oneOrMore)),
|
Short: coalesce.String(it.inspectHelp.short, fmt.Sprintf("Inspect given %s", it.oneOrMore)),
|
||||||
Long: coalesce.String(cli.inspectHelp.long, fmt.Sprintf("Inspect the state of one or more %s", cli.name)),
|
Long: coalesce.String(it.inspectHelp.long, fmt.Sprintf("Inspect the state of one or more %s", it.name)),
|
||||||
Example: cli.inspectHelp.example,
|
Example: it.inspectHelp.example,
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
ValidArgsFunction: func(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
return compInstalledItems(cli.name, args, toComplete)
|
return compInstalledItems(it.name, args, toComplete)
|
||||||
},
|
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
|
||||||
return cli.inspect(args, url, diff, rev, noMetrics)
|
|
||||||
},
|
},
|
||||||
|
RunE: it.Inspect,
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.StringVarP(&url, "url", "u", "", "Prometheus url")
|
flags.StringP("url", "u", "", "Prometheus url")
|
||||||
flags.BoolVar(&diff, "diff", false, "Show diff with latest version (for tainted items)")
|
flags.Bool("no-metrics", false, "Don't show metrics (when cscli.output=human)")
|
||||||
flags.BoolVar(&rev, "rev", false, "Reverse diff output")
|
|
||||||
flags.BoolVar(&noMetrics, "no-metrics", false, "Don't show metrics (when cscli.output=human)")
|
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliItem) list(args []string, all bool) error {
|
func (it itemCLI) List(cmd *cobra.Command, args []string) error {
|
||||||
hub, err := require.Hub(cli.cfg(), nil, log.StandardLogger())
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
all, err := flags.GetBool("all")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
items := make(map[string][]*cwhub.Item)
|
items := make(map[string][]*cwhub.Item)
|
||||||
|
|
||||||
items[cli.name], err = selectItems(hub, cli.name, args, !all)
|
items[it.name], err = selectItems(hub, it.name, args, !all)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = listItems(color.Output, []string{cli.name}, items, false); err != nil {
|
if err = listItems(color.Output, []string{it.name}, items, false); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli cliItem) newListCmd() *cobra.Command {
|
func (it itemCLI) NewListCmd() *cobra.Command {
|
||||||
var all bool
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmd := &cobra.Command{
|
||||||
Use: coalesce.String(cli.listHelp.use, "list [item... | -a]"),
|
Use: coalesce.String(it.listHelp.use, "list [item... | -a]"),
|
||||||
Short: coalesce.String(cli.listHelp.short, fmt.Sprintf("List %s", cli.oneOrMore)),
|
Short: coalesce.String(it.listHelp.short, fmt.Sprintf("List %s", it.oneOrMore)),
|
||||||
Long: coalesce.String(cli.listHelp.long, fmt.Sprintf("List of installed/available/specified %s", cli.name)),
|
Long: coalesce.String(it.listHelp.long, fmt.Sprintf("List of installed/available/specified %s", it.name)),
|
||||||
Example: cli.listHelp.example,
|
Example: it.listHelp.example,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: it.List,
|
||||||
return cli.list(args, all)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.BoolVarP(&all, "all", "a", false, "List disabled items as well")
|
flags.BoolP("all", "a", false, "List disabled items as well")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// return the diff between the installed version and the latest version
|
|
||||||
func (cli cliItem) itemDiff(item *cwhub.Item, reverse bool) (string, error) {
|
|
||||||
if !item.State.Installed {
|
|
||||||
return "", fmt.Errorf("'%s' is not installed", item.FQName())
|
|
||||||
}
|
|
||||||
|
|
||||||
latestContent, remoteURL, err := item.FetchLatest()
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
localContent, err := os.ReadFile(item.State.LocalPath)
|
|
||||||
if err != nil {
|
|
||||||
return "", fmt.Errorf("while reading %s: %w", item.State.LocalPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
file1 := item.State.LocalPath
|
|
||||||
file2 := remoteURL
|
|
||||||
content1 := string(localContent)
|
|
||||||
content2 := string(latestContent)
|
|
||||||
|
|
||||||
if reverse {
|
|
||||||
file1, file2 = file2, file1
|
|
||||||
content1, content2 = content2, content1
|
|
||||||
}
|
|
||||||
|
|
||||||
edits := myers.ComputeEdits(span.URIFromPath(file1), content1, content2)
|
|
||||||
diff := gotextdiff.ToUnified(file1, file2, content1, edits)
|
|
||||||
|
|
||||||
return fmt.Sprintf("%s", diff), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli cliItem) whyTainted(hub *cwhub.Hub, item *cwhub.Item, reverse bool) string {
|
|
||||||
if !item.State.Installed {
|
|
||||||
return fmt.Sprintf("# %s is not installed", item.FQName())
|
|
||||||
}
|
|
||||||
|
|
||||||
if !item.State.Tainted {
|
|
||||||
return fmt.Sprintf("# %s is not tainted", item.FQName())
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(item.State.TaintedBy) == 0 {
|
|
||||||
return fmt.Sprintf("# %s is tainted but we don't know why. please report this as a bug", item.FQName())
|
|
||||||
}
|
|
||||||
|
|
||||||
ret := []string{
|
|
||||||
fmt.Sprintf("# Let's see why %s is tainted.", item.FQName()),
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, fqsub := range item.State.TaintedBy {
|
|
||||||
ret = append(ret, fmt.Sprintf("\n-> %s\n", fqsub))
|
|
||||||
|
|
||||||
sub, err := hub.GetItemFQ(fqsub)
|
|
||||||
if err != nil {
|
|
||||||
ret = append(ret, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
diff, err := cli.itemDiff(sub, reverse)
|
|
||||||
if err != nil {
|
|
||||||
ret = append(ret, err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
if diff != "" {
|
|
||||||
ret = append(ret, diff)
|
|
||||||
} else if len(sub.State.TaintedBy) > 0 {
|
|
||||||
taintList := strings.Join(sub.State.TaintedBy, ", ")
|
|
||||||
if sub.FQName() == taintList {
|
|
||||||
// hack: avoid message "item is tainted by itself"
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = append(ret, fmt.Sprintf("# %s is tainted by %s", sub.FQName(), taintList))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return strings.Join(ret, "\n")
|
|
||||||
}
|
|
||||||
|
|
|
@ -6,18 +6,17 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"slices"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
// selectItems returns a slice of items of a given type, selected by name and sorted by case-insensitive name
|
// selectItems returns a slice of items of a given type, selected by name and sorted by case-insensitive name
|
||||||
func selectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly bool) ([]*cwhub.Item, error) {
|
func selectItems(hub *cwhub.Hub, itemType string, args []string, installedOnly bool) ([]*cwhub.Item, error) {
|
||||||
itemNames := hub.GetNamesByType(itemType)
|
itemNames := hub.GetItemNames(itemType)
|
||||||
|
|
||||||
notExist := []string{}
|
notExist := []string{}
|
||||||
|
|
||||||
|
@ -58,17 +57,13 @@ func listItems(out io.Writer, itemTypes []string, items map[string][]*cwhub.Item
|
||||||
switch csConfig.Cscli.Output {
|
switch csConfig.Cscli.Output {
|
||||||
case "human":
|
case "human":
|
||||||
nothingToDisplay := true
|
nothingToDisplay := true
|
||||||
|
|
||||||
for _, itemType := range itemTypes {
|
for _, itemType := range itemTypes {
|
||||||
if omitIfEmpty && len(items[itemType]) == 0 {
|
if omitIfEmpty && len(items[itemType]) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
listHubItemTable(out, "\n"+strings.ToUpper(itemType), items[itemType])
|
listHubItemTable(out, "\n"+strings.ToUpper(itemType), items[itemType])
|
||||||
|
|
||||||
nothingToDisplay = false
|
nothingToDisplay = false
|
||||||
}
|
}
|
||||||
|
|
||||||
if nothingToDisplay {
|
if nothingToDisplay {
|
||||||
fmt.Println("No items to display")
|
fmt.Println("No items to display")
|
||||||
}
|
}
|
||||||
|
@ -89,14 +84,14 @@ func listItems(out io.Writer, itemTypes []string, items map[string][]*cwhub.Item
|
||||||
|
|
||||||
for i, item := range items[itemType] {
|
for i, item := range items[itemType] {
|
||||||
status := item.State.Text()
|
status := item.State.Text()
|
||||||
statusEmo := item.State.Emoji()
|
status_emo := item.State.Emoji()
|
||||||
hubStatus[itemType][i] = itemHubStatus{
|
hubStatus[itemType][i] = itemHubStatus{
|
||||||
Name: item.Name,
|
Name: item.Name,
|
||||||
LocalVersion: item.State.LocalVersion,
|
LocalVersion: item.State.LocalVersion,
|
||||||
LocalPath: item.State.LocalPath,
|
LocalPath: item.State.LocalPath,
|
||||||
Description: item.Description,
|
Description: item.Description,
|
||||||
Status: status,
|
Status: status,
|
||||||
UTF8Status: fmt.Sprintf("%v %s", statusEmo, status),
|
UTF8Status: fmt.Sprintf("%v %s", status_emo, status),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -116,7 +111,7 @@ func listItems(out io.Writer, itemTypes []string, items map[string][]*cwhub.Item
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := csvwriter.Write(header); err != nil {
|
if err := csvwriter.Write(header); err != nil {
|
||||||
return fmt.Errorf("failed to write header: %w", err)
|
return fmt.Errorf("failed to write header: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, itemType := range itemTypes {
|
for _, itemType := range itemTypes {
|
||||||
|
@ -130,50 +125,37 @@ func listItems(out io.Writer, itemTypes []string, items map[string][]*cwhub.Item
|
||||||
if len(itemTypes) > 1 {
|
if len(itemTypes) > 1 {
|
||||||
row = append(row, itemType)
|
row = append(row, itemType)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := csvwriter.Write(row); err != nil {
|
if err := csvwriter.Write(row); err != nil {
|
||||||
return fmt.Errorf("failed to write raw output: %w", err)
|
return fmt.Errorf("failed to write raw output: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
csvwriter.Flush()
|
csvwriter.Flush()
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unknown output format '%s'", csConfig.Cscli.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func inspectItem(item *cwhub.Item, showMetrics bool) error {
|
func InspectItem(item *cwhub.Item, showMetrics bool) error {
|
||||||
switch csConfig.Cscli.Output {
|
switch csConfig.Cscli.Output {
|
||||||
case "human", "raw":
|
case "human", "raw":
|
||||||
enc := yaml.NewEncoder(os.Stdout)
|
enc := yaml.NewEncoder(os.Stdout)
|
||||||
enc.SetIndent(2)
|
enc.SetIndent(2)
|
||||||
|
|
||||||
if err := enc.Encode(item); err != nil {
|
if err := enc.Encode(item); err != nil {
|
||||||
return fmt.Errorf("unable to encode item: %w", err)
|
return fmt.Errorf("unable to encode item: %s", err)
|
||||||
}
|
}
|
||||||
case "json":
|
case "json":
|
||||||
b, err := json.MarshalIndent(*item, "", " ")
|
b, err := json.MarshalIndent(*item, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to marshal item: %w", err)
|
return fmt.Errorf("unable to marshal item: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Print(string(b))
|
fmt.Print(string(b))
|
||||||
}
|
}
|
||||||
|
|
||||||
if csConfig.Cscli.Output != "human" {
|
if csConfig.Cscli.Output == "human" && showMetrics {
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if item.State.Tainted {
|
|
||||||
fmt.Println()
|
|
||||||
fmt.Printf(`This item is tainted. Use "%s %s inspect --diff %s" to see why.`, filepath.Base(os.Args[0]), item.Type, item.Name)
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
||||||
|
|
||||||
if showMetrics {
|
|
||||||
fmt.Printf("\nCurrent metrics: \n")
|
fmt.Printf("\nCurrent metrics: \n")
|
||||||
|
|
||||||
if err := ShowMetrics(item); err != nil {
|
if err := ShowMetrics(item); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,14 +6,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
|
||||||
"sort"
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-openapi/strfmt"
|
"github.com/go-openapi/strfmt"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v2"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/version"
|
"github.com/crowdsecurity/go-cs-lib/version"
|
||||||
|
|
||||||
|
@ -29,55 +29,39 @@ import (
|
||||||
|
|
||||||
const LAPIURLPrefix = "v1"
|
const LAPIURLPrefix = "v1"
|
||||||
|
|
||||||
type cliLapi struct {
|
func runLapiStatus(cmd *cobra.Command, args []string) error {
|
||||||
cfg configGetter
|
password := strfmt.Password(csConfig.API.Client.Credentials.Password)
|
||||||
}
|
apiurl, err := url.Parse(csConfig.API.Client.Credentials.URL)
|
||||||
|
login := csConfig.API.Client.Credentials.Login
|
||||||
func NewCLILapi(cfg configGetter) *cliLapi {
|
|
||||||
return &cliLapi{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliLapi) status() error {
|
|
||||||
cfg := cli.cfg()
|
|
||||||
password := strfmt.Password(cfg.API.Client.Credentials.Password)
|
|
||||||
login := cfg.API.Client.Credentials.Login
|
|
||||||
|
|
||||||
origURL := cfg.API.Client.Credentials.URL
|
|
||||||
|
|
||||||
apiURL, err := url.Parse(origURL)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parsing api url: %w", err)
|
return fmt.Errorf("parsing api url: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hub, err := require.Hub(cfg, nil, nil)
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS)
|
scenarios, err := hub.GetInstalledItemNames(cwhub.SCENARIOS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get scenarios: %w", err)
|
return fmt.Errorf("failed to get scenarios: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
Client, err = apiclient.NewDefaultClient(apiURL,
|
Client, err = apiclient.NewDefaultClient(apiurl,
|
||||||
LAPIURLPrefix,
|
LAPIURLPrefix,
|
||||||
fmt.Sprintf("crowdsec/%s", version.String()),
|
fmt.Sprintf("crowdsec/%s", version.String()),
|
||||||
nil)
|
nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("init default client: %w", err)
|
return fmt.Errorf("init default client: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t := models.WatcherAuthRequest{
|
t := models.WatcherAuthRequest{
|
||||||
MachineID: &login,
|
MachineID: &login,
|
||||||
Password: &password,
|
Password: &password,
|
||||||
Scenarios: scenarios,
|
Scenarios: scenarios,
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Loaded credentials from %s", cfg.API.Client.CredentialsFilePath)
|
log.Infof("Loaded credentials from %s", csConfig.API.Client.CredentialsFilePath)
|
||||||
// use the original string because apiURL would print 'http://unix/'
|
log.Infof("Trying to authenticate with username %s on %s", login, apiurl)
|
||||||
log.Infof("Trying to authenticate with username %s on %s", login, origURL)
|
|
||||||
|
|
||||||
_, _, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
|
_, _, err = Client.Auth.AuthenticateWatcher(context.Background(), t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -85,15 +69,26 @@ func (cli *cliLapi) status() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("You can successfully interact with Local API (LAPI)")
|
log.Infof("You can successfully interact with Local API (LAPI)")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliLapi) register(apiURL string, outputFile string, machine string) error {
|
func runLapiRegister(cmd *cobra.Command, args []string) error {
|
||||||
var err error
|
flags := cmd.Flags()
|
||||||
|
|
||||||
lapiUser := machine
|
apiURL, err := flags.GetString("url")
|
||||||
cfg := cli.cfg()
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
outputFile, err := flags.GetString("file")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
lapiUser, err := flags.GetString("machine")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if lapiUser == "" {
|
if lapiUser == "" {
|
||||||
lapiUser, err = generateID("")
|
lapiUser, err = generateID("")
|
||||||
|
@ -101,14 +96,25 @@ func (cli *cliLapi) register(apiURL string, outputFile string, machine string) e
|
||||||
return fmt.Errorf("unable to generate machine id: %w", err)
|
return fmt.Errorf("unable to generate machine id: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
password := strfmt.Password(generatePassword(passwordLength))
|
password := strfmt.Password(generatePassword(passwordLength))
|
||||||
|
if apiURL == "" {
|
||||||
apiurl, err := prepareAPIURL(cfg.API.Client, apiURL)
|
if csConfig.API.Client == nil || csConfig.API.Client.Credentials == nil || csConfig.API.Client.Credentials.URL == "" {
|
||||||
|
return fmt.Errorf("no Local API URL. Please provide it in your configuration or with the -u parameter")
|
||||||
|
}
|
||||||
|
apiURL = csConfig.API.Client.Credentials.URL
|
||||||
|
}
|
||||||
|
/*URL needs to end with /, but user doesn't care*/
|
||||||
|
if !strings.HasSuffix(apiURL, "/") {
|
||||||
|
apiURL += "/"
|
||||||
|
}
|
||||||
|
/*URL needs to start with http://, but user doesn't care*/
|
||||||
|
if !strings.HasPrefix(apiURL, "http://") && !strings.HasPrefix(apiURL, "https://") {
|
||||||
|
apiURL = "http://" + apiURL
|
||||||
|
}
|
||||||
|
apiurl, err := url.Parse(apiURL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("parsing api url: %w", err)
|
return fmt.Errorf("parsing api url: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = apiclient.RegisterClient(&apiclient.Config{
|
_, err = apiclient.RegisterClient(&apiclient.Config{
|
||||||
MachineID: lapiUser,
|
MachineID: lapiUser,
|
||||||
Password: password,
|
Password: password,
|
||||||
|
@ -116,6 +122,7 @@ func (cli *cliLapi) register(apiURL string, outputFile string, machine string) e
|
||||||
URL: apiurl,
|
URL: apiurl,
|
||||||
VersionPrefix: LAPIURLPrefix,
|
VersionPrefix: LAPIURLPrefix,
|
||||||
}, nil)
|
}, nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("api client register: %w", err)
|
return fmt.Errorf("api client register: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -123,165 +130,138 @@ func (cli *cliLapi) register(apiURL string, outputFile string, machine string) e
|
||||||
log.Printf("Successfully registered to Local API (LAPI)")
|
log.Printf("Successfully registered to Local API (LAPI)")
|
||||||
|
|
||||||
var dumpFile string
|
var dumpFile string
|
||||||
|
|
||||||
if outputFile != "" {
|
if outputFile != "" {
|
||||||
dumpFile = outputFile
|
dumpFile = outputFile
|
||||||
} else if cfg.API.Client.CredentialsFilePath != "" {
|
} else if csConfig.API.Client.CredentialsFilePath != "" {
|
||||||
dumpFile = cfg.API.Client.CredentialsFilePath
|
dumpFile = csConfig.API.Client.CredentialsFilePath
|
||||||
} else {
|
} else {
|
||||||
dumpFile = ""
|
dumpFile = ""
|
||||||
}
|
}
|
||||||
|
|
||||||
apiCfg := csconfig.ApiCredentialsCfg{
|
apiCfg := csconfig.ApiCredentialsCfg{
|
||||||
Login: lapiUser,
|
Login: lapiUser,
|
||||||
Password: password.String(),
|
Password: password.String(),
|
||||||
URL: apiURL,
|
URL: apiURL,
|
||||||
}
|
}
|
||||||
|
|
||||||
apiConfigDump, err := yaml.Marshal(apiCfg)
|
apiConfigDump, err := yaml.Marshal(apiCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to marshal api credentials: %w", err)
|
return fmt.Errorf("unable to marshal api credentials: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if dumpFile != "" {
|
if dumpFile != "" {
|
||||||
err = os.WriteFile(dumpFile, apiConfigDump, 0o600)
|
err = os.WriteFile(dumpFile, apiConfigDump, 0o600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("write api credentials to '%s' failed: %w", dumpFile, err)
|
return fmt.Errorf("write api credentials to '%s' failed: %w", dumpFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Local API credentials written to '%s'", dumpFile)
|
log.Printf("Local API credentials written to '%s'", dumpFile)
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("%s\n", string(apiConfigDump))
|
fmt.Printf("%s\n", string(apiConfigDump))
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Warning(ReloadMessage())
|
log.Warning(ReloadMessage())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// prepareAPIURL checks/fixes a LAPI connection url (http, https or socket) and returns an URL struct
|
func NewLapiStatusCmd() *cobra.Command {
|
||||||
func prepareAPIURL(clientCfg *csconfig.LocalApiClientCfg, apiURL string) (*url.URL, error) {
|
|
||||||
if apiURL == "" {
|
|
||||||
if clientCfg == nil || clientCfg.Credentials == nil || clientCfg.Credentials.URL == "" {
|
|
||||||
return nil, errors.New("no Local API URL. Please provide it in your configuration or with the -u parameter")
|
|
||||||
}
|
|
||||||
|
|
||||||
apiURL = clientCfg.Credentials.URL
|
|
||||||
}
|
|
||||||
|
|
||||||
// URL needs to end with /, but user doesn't care
|
|
||||||
if !strings.HasSuffix(apiURL, "/") {
|
|
||||||
apiURL += "/"
|
|
||||||
}
|
|
||||||
|
|
||||||
// URL needs to start with http://, but user doesn't care
|
|
||||||
if !strings.HasPrefix(apiURL, "http://") && !strings.HasPrefix(apiURL, "https://") && !strings.HasPrefix(apiURL, "/") {
|
|
||||||
apiURL = "http://" + apiURL
|
|
||||||
}
|
|
||||||
|
|
||||||
return url.Parse(apiURL)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliLapi) newStatusCmd() *cobra.Command {
|
|
||||||
cmdLapiStatus := &cobra.Command{
|
cmdLapiStatus := &cobra.Command{
|
||||||
Use: "status",
|
Use: "status",
|
||||||
Short: "Check authentication to Local API (LAPI)",
|
Short: "Check authentication to Local API (LAPI)",
|
||||||
Args: cobra.MinimumNArgs(0),
|
Args: cobra.MinimumNArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: runLapiStatus,
|
||||||
return cli.status()
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmdLapiStatus
|
return cmdLapiStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliLapi) newRegisterCmd() *cobra.Command {
|
func NewLapiRegisterCmd() *cobra.Command {
|
||||||
var (
|
cmdLapiRegister := &cobra.Command{
|
||||||
apiURL string
|
|
||||||
outputFile string
|
|
||||||
machine string
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "register",
|
Use: "register",
|
||||||
Short: "Register a machine to Local API (LAPI)",
|
Short: "Register a machine to Local API (LAPI)",
|
||||||
Long: `Register your machine to the Local API (LAPI).
|
Long: `Register your machine to the Local API (LAPI).
|
||||||
Keep in mind the machine needs to be validated by an administrator on LAPI side to be effective.`,
|
Keep in mind the machine needs to be validated by an administrator on LAPI side to be effective.`,
|
||||||
Args: cobra.MinimumNArgs(0),
|
Args: cobra.MinimumNArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: runLapiRegister,
|
||||||
return cli.register(apiURL, outputFile, machine)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmdLapiRegister.Flags()
|
||||||
flags.StringVarP(&apiURL, "url", "u", "", "URL of the API (ie. http://127.0.0.1)")
|
flags.StringP("url", "u", "", "URL of the API (ie. http://127.0.0.1)")
|
||||||
flags.StringVarP(&outputFile, "file", "f", "", "output file destination")
|
flags.StringP("file", "f", "", "output file destination")
|
||||||
flags.StringVar(&machine, "machine", "", "Name of the machine to register with")
|
flags.String("machine", "", "Name of the machine to register with")
|
||||||
|
|
||||||
return cmd
|
return cmdLapiRegister
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliLapi) NewCommand() *cobra.Command {
|
func NewLapiCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmdLapi := &cobra.Command{
|
||||||
Use: "lapi [action]",
|
Use: "lapi [action]",
|
||||||
Short: "Manage interaction with Local API (LAPI)",
|
Short: "Manage interaction with Local API (LAPI)",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if err := cli.cfg().LoadAPIClient(); err != nil {
|
if err := csConfig.LoadAPIClient(); err != nil {
|
||||||
return fmt.Errorf("loading api client: %w", err)
|
return fmt.Errorf("loading api client: %w", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.newRegisterCmd())
|
cmdLapi.AddCommand(NewLapiRegisterCmd())
|
||||||
cmd.AddCommand(cli.newStatusCmd())
|
cmdLapi.AddCommand(NewLapiStatusCmd())
|
||||||
cmd.AddCommand(cli.newContextCmd())
|
cmdLapi.AddCommand(NewLapiContextCmd())
|
||||||
|
|
||||||
return cmd
|
return cmdLapi
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliLapi) addContext(key string, values []string) error {
|
func AddContext(key string, values []string) error {
|
||||||
cfg := cli.cfg()
|
|
||||||
|
|
||||||
if err := alertcontext.ValidateContextExpr(key, values); err != nil {
|
if err := alertcontext.ValidateContextExpr(key, values); err != nil {
|
||||||
return fmt.Errorf("invalid context configuration: %w", err)
|
return fmt.Errorf("invalid context configuration :%s", err)
|
||||||
}
|
}
|
||||||
|
if _, ok := csConfig.Crowdsec.ContextToSend[key]; !ok {
|
||||||
if _, ok := cfg.Crowdsec.ContextToSend[key]; !ok {
|
csConfig.Crowdsec.ContextToSend[key] = make([]string, 0)
|
||||||
cfg.Crowdsec.ContextToSend[key] = make([]string, 0)
|
|
||||||
|
|
||||||
log.Infof("key '%s' added", key)
|
log.Infof("key '%s' added", key)
|
||||||
}
|
}
|
||||||
|
data := csConfig.Crowdsec.ContextToSend[key]
|
||||||
data := cfg.Crowdsec.ContextToSend[key]
|
|
||||||
|
|
||||||
for _, val := range values {
|
for _, val := range values {
|
||||||
if !slices.Contains(data, val) {
|
if !slices.Contains(data, val) {
|
||||||
log.Infof("value '%s' added to key '%s'", val, key)
|
log.Infof("value '%s' added to key '%s'", val, key)
|
||||||
data = append(data, val)
|
data = append(data, val)
|
||||||
}
|
}
|
||||||
|
csConfig.Crowdsec.ContextToSend[key] = data
|
||||||
cfg.Crowdsec.ContextToSend[key] = data
|
|
||||||
}
|
}
|
||||||
|
if err := csConfig.Crowdsec.DumpContextConfigFile(); err != nil {
|
||||||
if err := cfg.Crowdsec.DumpContextConfigFile(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliLapi) newContextAddCmd() *cobra.Command {
|
func NewLapiContextCmd() *cobra.Command {
|
||||||
var (
|
cmdContext := &cobra.Command{
|
||||||
keyToAdd string
|
Use: "context [command]",
|
||||||
valuesToAdd []string
|
Short: "Manage context to send with alerts",
|
||||||
)
|
DisableAutoGenTag: true,
|
||||||
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if err := csConfig.LoadCrowdsec(); err != nil {
|
||||||
|
fileNotFoundMessage := fmt.Sprintf("failed to open context file: open %s: no such file or directory", csConfig.Crowdsec.ConsoleContextPath)
|
||||||
|
if err.Error() != fileNotFoundMessage {
|
||||||
|
return fmt.Errorf("unable to start CrowdSec agent: %w", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if csConfig.DisableAgent {
|
||||||
|
return errors.New("agent is disabled and lapi context can only be used on the agent")
|
||||||
|
}
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
return nil
|
||||||
|
},
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
printHelp(cmd)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var keyToAdd string
|
||||||
|
var valuesToAdd []string
|
||||||
|
cmdContextAdd := &cobra.Command{
|
||||||
Use: "add",
|
Use: "add",
|
||||||
Short: "Add context to send with alerts. You must specify the output key with the expr value you want",
|
Short: "Add context to send with alerts. You must specify the output key with the expr value you want",
|
||||||
Example: `cscli lapi context add --key source_ip --value evt.Meta.source_ip
|
Example: `cscli lapi context add --key source_ip --value evt.Meta.source_ip
|
||||||
|
@ -289,18 +269,18 @@ cscli lapi context add --key file_source --value evt.Line.Src
|
||||||
cscli lapi context add --value evt.Meta.source_ip --value evt.Meta.target_user
|
cscli lapi context add --value evt.Meta.source_ip --value evt.Meta.target_user
|
||||||
`,
|
`,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
hub, err := require.Hub(cli.cfg(), nil, nil)
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = alertcontext.LoadConsoleContext(cli.cfg(), hub); err != nil {
|
if err = alertcontext.LoadConsoleContext(csConfig, hub); err != nil {
|
||||||
return fmt.Errorf("while loading context: %w", err)
|
return fmt.Errorf("while loading context: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if keyToAdd != "" {
|
if keyToAdd != "" {
|
||||||
if err := cli.addContext(keyToAdd, valuesToAdd); err != nil {
|
if err := AddContext(keyToAdd, valuesToAdd); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -310,7 +290,7 @@ cscli lapi context add --value evt.Meta.source_ip --value evt.Meta.target_user
|
||||||
keySlice := strings.Split(v, ".")
|
keySlice := strings.Split(v, ".")
|
||||||
key := keySlice[len(keySlice)-1]
|
key := keySlice[len(keySlice)-1]
|
||||||
value := []string{v}
|
value := []string{v}
|
||||||
if err := cli.addContext(key, value); err != nil {
|
if err := AddContext(key, value); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -318,37 +298,31 @@ cscli lapi context add --value evt.Meta.source_ip --value evt.Meta.target_user
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmdContextAdd.Flags().StringVarP(&keyToAdd, "key", "k", "", "The key of the different values to send")
|
||||||
|
cmdContextAdd.Flags().StringSliceVar(&valuesToAdd, "value", []string{}, "The expr fields to associate with the key")
|
||||||
|
cmdContextAdd.MarkFlagRequired("value")
|
||||||
|
cmdContext.AddCommand(cmdContextAdd)
|
||||||
|
|
||||||
flags := cmd.Flags()
|
cmdContextStatus := &cobra.Command{
|
||||||
flags.StringVarP(&keyToAdd, "key", "k", "", "The key of the different values to send")
|
|
||||||
flags.StringSliceVar(&valuesToAdd, "value", []string{}, "The expr fields to associate with the key")
|
|
||||||
cmd.MarkFlagRequired("value")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliLapi) newContextStatusCmd() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "status",
|
Use: "status",
|
||||||
Short: "List context to send with alerts",
|
Short: "List context to send with alerts",
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
hub, err := require.Hub(csConfig, nil)
|
||||||
hub, err := require.Hub(cfg, nil, nil)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = alertcontext.LoadConsoleContext(cfg, hub); err != nil {
|
if err = alertcontext.LoadConsoleContext(csConfig, hub); err != nil {
|
||||||
return fmt.Errorf("while loading context: %w", err)
|
return fmt.Errorf("while loading context: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(cfg.Crowdsec.ContextToSend) == 0 {
|
if len(csConfig.Crowdsec.ContextToSend) == 0 {
|
||||||
fmt.Println("No context found on this agent. You can use 'cscli lapi context add' to add context to your alerts.")
|
fmt.Println("No context found on this agent. You can use 'cscli lapi context add' to add context to your alerts.")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
dump, err := yaml.Marshal(cfg.Crowdsec.ContextToSend)
|
dump, err := yaml.Marshal(csConfig.Crowdsec.ContextToSend)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to show context status: %w", err)
|
return fmt.Errorf("unable to show context status: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -358,14 +332,10 @@ func (cli *cliLapi) newContextStatusCmd() *cobra.Command {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmdContext.AddCommand(cmdContextStatus)
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliLapi) newContextDetectCmd() *cobra.Command {
|
|
||||||
var detectAll bool
|
var detectAll bool
|
||||||
|
cmdContextDetect := &cobra.Command{
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "detect",
|
Use: "detect",
|
||||||
Short: "Detect available fields from the installed parsers",
|
Short: "Detect available fields from the installed parsers",
|
||||||
Example: `cscli lapi context detect --all
|
Example: `cscli lapi context detect --all
|
||||||
|
@ -373,7 +343,6 @@ cscli lapi context detect crowdsecurity/sshd-logs
|
||||||
`,
|
`,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
|
||||||
if !detectAll && len(args) == 0 {
|
if !detectAll && len(args) == 0 {
|
||||||
log.Infof("Please provide parsers to detect or --all flag.")
|
log.Infof("Please provide parsers to detect or --all flag.")
|
||||||
printHelp(cmd)
|
printHelp(cmd)
|
||||||
|
@ -386,13 +355,13 @@ cscli lapi context detect crowdsecurity/sshd-logs
|
||||||
return fmt.Errorf("failed to init expr helpers: %w", err)
|
return fmt.Errorf("failed to init expr helpers: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hub, err := require.Hub(cfg, nil, nil)
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
csParsers := parser.NewParsers(hub)
|
csParsers := parser.NewParsers(hub)
|
||||||
if csParsers, err = parser.LoadParsers(cfg, csParsers); err != nil {
|
if csParsers, err = parser.LoadParsers(csConfig, csParsers); err != nil {
|
||||||
return fmt.Errorf("unable to load parsers: %w", err)
|
return fmt.Errorf("unable to load parsers: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -449,85 +418,39 @@ cscli lapi context detect crowdsecurity/sshd-logs
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.Flags().BoolVarP(&detectAll, "all", "a", false, "Detect evt field for all installed parser")
|
cmdContextDetect.Flags().BoolVarP(&detectAll, "all", "a", false, "Detect evt field for all installed parser")
|
||||||
|
cmdContext.AddCommand(cmdContextDetect)
|
||||||
|
|
||||||
return cmd
|
cmdContextDelete := &cobra.Command{
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliLapi) newContextDeleteCmd() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "delete",
|
Use: "delete",
|
||||||
DisableAutoGenTag: true,
|
Deprecated: "please manually edit the context file.",
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
filePath := cli.cfg().Crowdsec.ConsoleContextPath
|
|
||||||
if filePath == "" {
|
|
||||||
filePath = "the context file"
|
|
||||||
}
|
}
|
||||||
fmt.Printf("Command 'delete' is deprecated, please manually edit %s.", filePath)
|
cmdContext.AddCommand(cmdContextDelete)
|
||||||
|
|
||||||
return nil
|
return cmdContext
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
func detectStaticField(GrokStatics []parser.ExtraField) []string {
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliLapi) newContextCmd() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "context [command]",
|
|
||||||
Short: "Manage context to send with alerts",
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
cfg := cli.cfg()
|
|
||||||
if err := cfg.LoadCrowdsec(); err != nil {
|
|
||||||
fileNotFoundMessage := fmt.Sprintf("failed to open context file: open %s: no such file or directory", cfg.Crowdsec.ConsoleContextPath)
|
|
||||||
if err.Error() != fileNotFoundMessage {
|
|
||||||
return fmt.Errorf("unable to load CrowdSec agent configuration: %w", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if cfg.DisableAgent {
|
|
||||||
return errors.New("agent is disabled and lapi context can only be used on the agent")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
Run: func(cmd *cobra.Command, _ []string) {
|
|
||||||
printHelp(cmd)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd.AddCommand(cli.newContextAddCmd())
|
|
||||||
cmd.AddCommand(cli.newContextStatusCmd())
|
|
||||||
cmd.AddCommand(cli.newContextDetectCmd())
|
|
||||||
cmd.AddCommand(cli.newContextDeleteCmd())
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func detectStaticField(grokStatics []parser.ExtraField) []string {
|
|
||||||
ret := make([]string, 0)
|
ret := make([]string, 0)
|
||||||
|
|
||||||
for _, static := range grokStatics {
|
for _, static := range GrokStatics {
|
||||||
if static.Parsed != "" {
|
if static.Parsed != "" {
|
||||||
fieldName := fmt.Sprintf("evt.Parsed.%s", static.Parsed)
|
fieldName := fmt.Sprintf("evt.Parsed.%s", static.Parsed)
|
||||||
if !slices.Contains(ret, fieldName) {
|
if !slices.Contains(ret, fieldName) {
|
||||||
ret = append(ret, fieldName)
|
ret = append(ret, fieldName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if static.Meta != "" {
|
if static.Meta != "" {
|
||||||
fieldName := fmt.Sprintf("evt.Meta.%s", static.Meta)
|
fieldName := fmt.Sprintf("evt.Meta.%s", static.Meta)
|
||||||
if !slices.Contains(ret, fieldName) {
|
if !slices.Contains(ret, fieldName) {
|
||||||
ret = append(ret, fieldName)
|
ret = append(ret, fieldName)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if static.TargetByName != "" {
|
if static.TargetByName != "" {
|
||||||
fieldName := static.TargetByName
|
fieldName := static.TargetByName
|
||||||
if !strings.HasPrefix(fieldName, "evt.") {
|
if !strings.HasPrefix(fieldName, "evt.") {
|
||||||
fieldName = "evt." + fieldName
|
fieldName = "evt." + fieldName
|
||||||
}
|
}
|
||||||
|
|
||||||
if !slices.Contains(ret, fieldName) {
|
if !slices.Contains(ret, fieldName) {
|
||||||
ret = append(ret, fieldName)
|
ret = append(ret, fieldName)
|
||||||
}
|
}
|
||||||
|
@ -584,7 +507,7 @@ func detectNode(node parser.Node, parserCTX parser.UnixParserCtx) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func detectSubNode(node parser.Node, parserCTX parser.UnixParserCtx) []string {
|
func detectSubNode(node parser.Node, parserCTX parser.UnixParserCtx) []string {
|
||||||
ret := make([]string, 0)
|
var ret = make([]string, 0)
|
||||||
|
|
||||||
for _, subnode := range node.LeavesNodes {
|
for _, subnode := range node.LeavesNodes {
|
||||||
if subnode.Grok.RunTimeRegexp != nil {
|
if subnode.Grok.RunTimeRegexp != nil {
|
||||||
|
@ -595,7 +518,6 @@ func detectSubNode(node parser.Node, parserCTX parser.UnixParserCtx) []string {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if subnode.Grok.RegexpName != "" {
|
if subnode.Grok.RegexpName != "" {
|
||||||
grokCompiled, err := parserCTX.Grok.Get(subnode.Grok.RegexpName)
|
grokCompiled, err := parserCTX.Grok.Get(subnode.Grok.RegexpName)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
|
@ -1,49 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPrepareAPIURL_NoProtocol(t *testing.T) {
|
|
||||||
url, err := prepareAPIURL(nil, "localhost:81")
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, "http://localhost:81/", url.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareAPIURL_Http(t *testing.T) {
|
|
||||||
url, err := prepareAPIURL(nil, "http://localhost:81")
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, "http://localhost:81/", url.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareAPIURL_Https(t *testing.T) {
|
|
||||||
url, err := prepareAPIURL(nil, "https://localhost:81")
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, "https://localhost:81/", url.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareAPIURL_UnixSocket(t *testing.T) {
|
|
||||||
url, err := prepareAPIURL(nil, "/path/socket")
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, "/path/socket/", url.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareAPIURL_Empty(t *testing.T) {
|
|
||||||
_, err := prepareAPIURL(nil, "")
|
|
||||||
require.Error(t, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestPrepareAPIURL_Empty_ConfigOverride(t *testing.T) {
|
|
||||||
url, err := prepareAPIURL(&csconfig.LocalApiClientCfg{
|
|
||||||
Credentials: &csconfig.ApiCredentialsCfg{
|
|
||||||
URL: "localhost:80",
|
|
||||||
},
|
|
||||||
}, "")
|
|
||||||
require.NoError(t, err)
|
|
||||||
assert.Equal(t, "http://localhost:80/", url.String())
|
|
||||||
}
|
|
|
@ -4,8 +4,8 @@ import (
|
||||||
saferand "crypto/rand"
|
saferand "crypto/rand"
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"slices"
|
||||||
|
@ -18,15 +18,16 @@ import (
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/crowdsecurity/machineid"
|
"github.com/crowdsecurity/machineid"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
const passwordLength = 64
|
const passwordLength = 64
|
||||||
|
@ -46,7 +47,6 @@ func generatePassword(length int) string {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed getting data from prng for password generation : %s", err)
|
log.Fatalf("failed getting data from prng for password generation : %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
buf[i] = charset[rInt.Int64()]
|
buf[i] = charset[rInt.Int64()]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,14 +61,12 @@ func generateIDPrefix() (string, error) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return prefix, nil
|
return prefix, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("failed to get machine-id with usual files: %s", err)
|
log.Debugf("failed to get machine-id with usual files: %s", err)
|
||||||
|
|
||||||
bID, err := uuid.NewRandom()
|
bId, err := uuid.NewRandom()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return bID.String(), nil
|
return bId.String(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return "", fmt.Errorf("generating machine id: %w", err)
|
return "", fmt.Errorf("generating machine id: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,14 +77,11 @@ func generateID(prefix string) (string, error) {
|
||||||
if prefix == "" {
|
if prefix == "" {
|
||||||
prefix, err = generateIDPrefix()
|
prefix, err = generateIDPrefix()
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix = strings.ReplaceAll(prefix, "-", "")[:32]
|
prefix = strings.ReplaceAll(prefix, "-", "")[:32]
|
||||||
suffix := generatePassword(16)
|
suffix := generatePassword(16)
|
||||||
|
|
||||||
return prefix + suffix, nil
|
return prefix + suffix, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -107,356 +102,255 @@ func getLastHeartbeat(m *ent.Machine) (string, bool) {
|
||||||
return hb, true
|
return hb, true
|
||||||
}
|
}
|
||||||
|
|
||||||
type cliMachines struct {
|
func getAgents(out io.Writer, dbClient *database.Client) error {
|
||||||
db *database.Client
|
machines, err := dbClient.ListMachines()
|
||||||
cfg configGetter
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLIMachines(cfg configGetter) *cliMachines {
|
|
||||||
return &cliMachines{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliMachines) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "machines [action]",
|
|
||||||
Short: "Manage local API machines [requires local API]",
|
|
||||||
Long: `To list/add/delete/validate/prune machines.
|
|
||||||
Note: This command requires database direct access, so is intended to be run on the local API machine.
|
|
||||||
`,
|
|
||||||
Example: `cscli machines [action]`,
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
Aliases: []string{"machine"},
|
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
var err error
|
|
||||||
if err = require.LAPI(cli.cfg()); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
cli.db, err = database.NewClient(cli.cfg().DbConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to create new database client: %w", err)
|
return fmt.Errorf("unable to list machines: %s", err)
|
||||||
}
|
}
|
||||||
|
if csConfig.Cscli.Output == "human" {
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
cmd.AddCommand(cli.newListCmd())
|
|
||||||
cmd.AddCommand(cli.newAddCmd())
|
|
||||||
cmd.AddCommand(cli.newDeleteCmd())
|
|
||||||
cmd.AddCommand(cli.newValidateCmd())
|
|
||||||
cmd.AddCommand(cli.newPruneCmd())
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliMachines) list() error {
|
|
||||||
out := color.Output
|
|
||||||
|
|
||||||
machines, err := cli.db.ListMachines()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to list machines: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch cli.cfg().Cscli.Output {
|
|
||||||
case "human":
|
|
||||||
getAgentsTable(out, machines)
|
getAgentsTable(out, machines)
|
||||||
case "json":
|
} else if csConfig.Cscli.Output == "json" {
|
||||||
enc := json.NewEncoder(out)
|
enc := json.NewEncoder(out)
|
||||||
enc.SetIndent("", " ")
|
enc.SetIndent("", " ")
|
||||||
|
|
||||||
if err := enc.Encode(machines); err != nil {
|
if err := enc.Encode(machines); err != nil {
|
||||||
return errors.New("failed to marshal")
|
return fmt.Errorf("failed to marshal")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
case "raw":
|
} else if csConfig.Cscli.Output == "raw" {
|
||||||
csvwriter := csv.NewWriter(out)
|
csvwriter := csv.NewWriter(out)
|
||||||
|
|
||||||
err := csvwriter.Write([]string{"machine_id", "ip_address", "updated_at", "validated", "version", "auth_type", "last_heartbeat"})
|
err := csvwriter.Write([]string{"machine_id", "ip_address", "updated_at", "validated", "version", "auth_type", "last_heartbeat"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write header: %w", err)
|
return fmt.Errorf("failed to write header: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, m := range machines {
|
for _, m := range machines {
|
||||||
validated := "false"
|
var validated string
|
||||||
if m.IsValidated {
|
if m.IsValidated {
|
||||||
validated = "true"
|
validated = "true"
|
||||||
|
} else {
|
||||||
|
validated = "false"
|
||||||
}
|
}
|
||||||
|
|
||||||
hb, _ := getLastHeartbeat(m)
|
hb, _ := getLastHeartbeat(m)
|
||||||
|
err := csvwriter.Write([]string{m.MachineId, m.IpAddress, m.UpdatedAt.Format(time.RFC3339), validated, m.Version, m.AuthType, hb})
|
||||||
if err := csvwriter.Write([]string{m.MachineId, m.IpAddress, m.UpdatedAt.Format(time.RFC3339), validated, m.Version, m.AuthType, hb}); err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to write raw output: %w", err)
|
return fmt.Errorf("failed to write raw output: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
csvwriter.Flush()
|
csvwriter.Flush()
|
||||||
|
} else {
|
||||||
|
log.Errorf("unknown output '%s'", csConfig.Cscli.Output)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliMachines) newListCmd() *cobra.Command {
|
func NewMachinesListCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmdMachinesList := &cobra.Command{
|
||||||
Use: "list",
|
Use: "list",
|
||||||
Short: "list all machines in the database",
|
Short: "list all machines in the database",
|
||||||
Long: `list all machines in the database with their status and last heartbeat`,
|
Long: `list all machines in the database with their status and last heartbeat`,
|
||||||
Example: `cscli machines list`,
|
Example: `cscli machines list`,
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return cli.list()
|
err := getAgents(color.Output, dbClient)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to list machines: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdMachinesList
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliMachines) newAddCmd() *cobra.Command {
|
func NewMachinesAddCmd() *cobra.Command {
|
||||||
var (
|
cmdMachinesAdd := &cobra.Command{
|
||||||
password MachinePassword
|
|
||||||
dumpFile string
|
|
||||||
apiURL string
|
|
||||||
interactive bool
|
|
||||||
autoAdd bool
|
|
||||||
force bool
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "add",
|
Use: "add",
|
||||||
Short: "add a single machine to the database",
|
Short: "add a single machine to the database",
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
Long: `Register a new machine in the database. cscli should be on the same machine as LAPI.`,
|
Long: `Register a new machine in the database. cscli should be on the same machine as LAPI.`,
|
||||||
Example: `cscli machines add --auto
|
Example: `
|
||||||
|
cscli machines add --auto
|
||||||
cscli machines add MyTestMachine --auto
|
cscli machines add MyTestMachine --auto
|
||||||
cscli machines add MyTestMachine --password MyPassword
|
cscli machines add MyTestMachine --password MyPassword
|
||||||
cscli machines add -f- --auto > /tmp/mycreds.yaml`,
|
`,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: runMachinesAdd,
|
||||||
return cli.add(args, string(password), dumpFile, apiURL, interactive, autoAdd, force)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
flags := cmdMachinesAdd.Flags()
|
||||||
|
flags.StringP("password", "p", "", "machine password to login to the API")
|
||||||
|
flags.StringP("file", "f", "", "output file destination (defaults to "+csconfig.DefaultConfigPath("local_api_credentials.yaml")+")")
|
||||||
|
flags.StringP("url", "u", "", "URL of the local API")
|
||||||
|
flags.BoolP("interactive", "i", false, "interfactive mode to enter the password")
|
||||||
|
flags.BoolP("auto", "a", false, "automatically generate password (and username if not provided)")
|
||||||
|
flags.Bool("force", false, "will force add the machine if it already exist")
|
||||||
|
|
||||||
|
return cmdMachinesAdd
|
||||||
|
}
|
||||||
|
|
||||||
|
func runMachinesAdd(cmd *cobra.Command, args []string) error {
|
||||||
|
var err error
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmd.Flags()
|
||||||
flags.VarP(&password, "password", "p", "machine password to login to the API")
|
|
||||||
flags.StringVarP(&dumpFile, "file", "f", "", "output file destination (defaults to "+csconfig.DefaultConfigPath("local_api_credentials.yaml")+")")
|
|
||||||
flags.StringVarP(&apiURL, "url", "u", "", "URL of the local API")
|
|
||||||
flags.BoolVarP(&interactive, "interactive", "i", false, "interfactive mode to enter the password")
|
|
||||||
flags.BoolVarP(&autoAdd, "auto", "a", false, "automatically generate password (and username if not provided)")
|
|
||||||
flags.BoolVar(&force, "force", false, "will force add the machine if it already exist")
|
|
||||||
|
|
||||||
return cmd
|
machinePassword, err := flags.GetString("password")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliMachines) add(args []string, machinePassword string, dumpFile string, apiURL string, interactive bool, autoAdd bool, force bool) error {
|
dumpFile, err := flags.GetString("file")
|
||||||
var (
|
if err != nil {
|
||||||
err error
|
return err
|
||||||
machineID string
|
}
|
||||||
)
|
|
||||||
|
apiURL, err := flags.GetString("url")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
interactive, err := flags.GetBool("interactive")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
autoAdd, err := flags.GetBool("auto")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
force, err := flags.GetBool("force")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var machineID string
|
||||||
|
|
||||||
// create machineID if not specified by user
|
// create machineID if not specified by user
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
if !autoAdd {
|
if !autoAdd {
|
||||||
return errors.New("please specify a machine name to add, or use --auto")
|
printHelp(cmd)
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
machineID, err = generateID("")
|
machineID, err = generateID("")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to generate machine id: %w", err)
|
return fmt.Errorf("unable to generate machine id: %s", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
machineID = args[0]
|
machineID = args[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
clientCfg := cli.cfg().API.Client
|
|
||||||
serverCfg := cli.cfg().API.Server
|
|
||||||
|
|
||||||
/*check if file already exists*/
|
/*check if file already exists*/
|
||||||
if dumpFile == "" && clientCfg != nil && clientCfg.CredentialsFilePath != "" {
|
if dumpFile == "" && csConfig.API.Client != nil && csConfig.API.Client.CredentialsFilePath != "" {
|
||||||
credFile := clientCfg.CredentialsFilePath
|
credFile := csConfig.API.Client.CredentialsFilePath
|
||||||
// use the default only if the file does not exist
|
// use the default only if the file does not exist
|
||||||
_, err = os.Stat(credFile)
|
_, err := os.Stat(credFile)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case os.IsNotExist(err) || force:
|
case os.IsNotExist(err) || force:
|
||||||
dumpFile = credFile
|
dumpFile = csConfig.API.Client.CredentialsFilePath
|
||||||
case err != nil:
|
case err != nil:
|
||||||
return fmt.Errorf("unable to stat '%s': %w", credFile, err)
|
return fmt.Errorf("unable to stat '%s': %s", credFile, err)
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf(`credentials file '%s' already exists: please remove it, use "--force" or specify a different file with "-f" ("-f -" for standard output)`, credFile)
|
return fmt.Errorf(`credentials file '%s' already exists: please remove it, use "--force" or specify a different file with "-f" ("-f -" for standard output)`, credFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if dumpFile == "" {
|
if dumpFile == "" {
|
||||||
return errors.New(`please specify a file to dump credentials to, with -f ("-f -" for standard output)`)
|
return fmt.Errorf(`please specify a file to dump credentials to, with -f ("-f -" for standard output)`)
|
||||||
}
|
}
|
||||||
|
|
||||||
// create a password if it's not specified by user
|
// create a password if it's not specified by user
|
||||||
if machinePassword == "" && !interactive {
|
if machinePassword == "" && !interactive {
|
||||||
if !autoAdd {
|
if !autoAdd {
|
||||||
return errors.New("please specify a password with --password or use --auto")
|
return fmt.Errorf("please specify a password with --password or use --auto")
|
||||||
}
|
}
|
||||||
|
|
||||||
machinePassword = generatePassword(passwordLength)
|
machinePassword = generatePassword(passwordLength)
|
||||||
} else if machinePassword == "" && interactive {
|
} else if machinePassword == "" && interactive {
|
||||||
qs := &survey.Password{
|
qs := &survey.Password{
|
||||||
Message: "Please provide a password for the machine:",
|
Message: "Please provide a password for the machine",
|
||||||
}
|
}
|
||||||
survey.AskOne(qs, &machinePassword)
|
survey.AskOne(qs, &machinePassword)
|
||||||
}
|
}
|
||||||
|
|
||||||
password := strfmt.Password(machinePassword)
|
password := strfmt.Password(machinePassword)
|
||||||
|
_, err = dbClient.CreateMachine(&machineID, &password, "", true, force, types.PasswordAuthType)
|
||||||
_, err = cli.db.CreateMachine(&machineID, &password, "", true, force, types.PasswordAuthType)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to create machine: %w", err)
|
return fmt.Errorf("unable to create machine: %s", err)
|
||||||
}
|
}
|
||||||
|
log.Infof("Machine '%s' successfully added to the local API", machineID)
|
||||||
fmt.Fprintf(os.Stderr, "Machine '%s' successfully added to the local API.\n", machineID)
|
|
||||||
|
|
||||||
if apiURL == "" {
|
if apiURL == "" {
|
||||||
if clientCfg != nil && clientCfg.Credentials != nil && clientCfg.Credentials.URL != "" {
|
if csConfig.API.Client != nil && csConfig.API.Client.Credentials != nil && csConfig.API.Client.Credentials.URL != "" {
|
||||||
apiURL = clientCfg.Credentials.URL
|
apiURL = csConfig.API.Client.Credentials.URL
|
||||||
} else if serverCfg.ClientURL() != "" {
|
} else if csConfig.API.Server != nil && csConfig.API.Server.ListenURI != "" {
|
||||||
apiURL = serverCfg.ClientURL()
|
apiURL = "http://" + csConfig.API.Server.ListenURI
|
||||||
} else {
|
} else {
|
||||||
return errors.New("unable to dump an api URL. Please provide it in your configuration or with the -u parameter")
|
return fmt.Errorf("unable to dump an api URL. Please provide it in your configuration or with the -u parameter")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
apiCfg := csconfig.ApiCredentialsCfg{
|
apiCfg := csconfig.ApiCredentialsCfg{
|
||||||
Login: machineID,
|
Login: machineID,
|
||||||
Password: password.String(),
|
Password: password.String(),
|
||||||
URL: apiURL,
|
URL: apiURL,
|
||||||
}
|
}
|
||||||
|
|
||||||
apiConfigDump, err := yaml.Marshal(apiCfg)
|
apiConfigDump, err := yaml.Marshal(apiCfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to marshal api credentials: %w", err)
|
return fmt.Errorf("unable to marshal api credentials: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if dumpFile != "" && dumpFile != "-" {
|
if dumpFile != "" && dumpFile != "-" {
|
||||||
if err = os.WriteFile(dumpFile, apiConfigDump, 0o600); err != nil {
|
err = os.WriteFile(dumpFile, apiConfigDump, 0644)
|
||||||
return fmt.Errorf("write api credentials in '%s' failed: %w", dumpFile, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "API credentials written to '%s'.\n", dumpFile)
|
|
||||||
} else {
|
|
||||||
fmt.Print(string(apiConfigDump))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliMachines) deleteValid(_ *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
|
||||||
machines, err := cli.db.ListMachines()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cobra.CompError("unable to list machines " + err.Error())
|
return fmt.Errorf("write api credentials in '%s' failed: %s", dumpFile, err)
|
||||||
}
|
}
|
||||||
|
log.Printf("API credentials written to '%s'", dumpFile)
|
||||||
ret := []string{}
|
} else {
|
||||||
|
fmt.Printf("%s\n", string(apiConfigDump))
|
||||||
for _, machine := range machines {
|
|
||||||
if strings.Contains(machine.MachineId, toComplete) && !slices.Contains(args, machine.MachineId) {
|
|
||||||
ret = append(ret, machine.MachineId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret, cobra.ShellCompDirectiveNoFileComp
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliMachines) delete(machines []string) error {
|
|
||||||
for _, machineID := range machines {
|
|
||||||
if err := cli.db.DeleteWatcher(machineID); err != nil {
|
|
||||||
log.Errorf("unable to delete machine '%s': %s", machineID, err)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("machine '%s' deleted successfully", machineID)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliMachines) newDeleteCmd() *cobra.Command {
|
func NewMachinesDeleteCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmdMachinesDelete := &cobra.Command{
|
||||||
Use: "delete [machine_name]...",
|
Use: "delete [machine_name]...",
|
||||||
Short: "delete machine(s) by name",
|
Short: "delete machine(s) by name",
|
||||||
Example: `cscli machines delete "machine1" "machine2"`,
|
Example: `cscli machines delete "machine1" "machine2"`,
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
Aliases: []string{"remove"},
|
Aliases: []string{"remove"},
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
ValidArgsFunction: cli.deleteValid,
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
machines, err := dbClient.ListMachines()
|
||||||
return cli.delete(args)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliMachines) prune(duration time.Duration, notValidOnly bool, force bool) error {
|
|
||||||
if duration < 2*time.Minute && !notValidOnly {
|
|
||||||
if yes, err := askYesNo(
|
|
||||||
"The duration you provided is less than 2 minutes. " +
|
|
||||||
"This can break installations if the machines are only temporarily disconnected. Continue?", false); err != nil {
|
|
||||||
return err
|
|
||||||
} else if !yes {
|
|
||||||
fmt.Println("User aborted prune. No changes were made.")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
machines := []*ent.Machine{}
|
|
||||||
if pending, err := cli.db.QueryPendingMachine(); err == nil {
|
|
||||||
machines = append(machines, pending...)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !notValidOnly {
|
|
||||||
if pending, err := cli.db.QueryLastValidatedHeartbeatLT(time.Now().UTC().Add(-duration)); err == nil {
|
|
||||||
machines = append(machines, pending...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(machines) == 0 {
|
|
||||||
fmt.Println("No machines to prune.")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
getAgentsTable(color.Output, machines)
|
|
||||||
|
|
||||||
if !force {
|
|
||||||
if yes, err := askYesNo(
|
|
||||||
"You are about to PERMANENTLY remove the above machines from the database. " +
|
|
||||||
"These will NOT be recoverable. Continue?", false); err != nil {
|
|
||||||
return err
|
|
||||||
} else if !yes {
|
|
||||||
fmt.Println("User aborted prune. No changes were made.")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
deleted, err := cli.db.BulkDeleteWatchers(machines)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to prune machines: %w", err)
|
cobra.CompError("unable to list machines " + err.Error())
|
||||||
|
}
|
||||||
|
ret := make([]string, 0)
|
||||||
|
for _, machine := range machines {
|
||||||
|
if strings.Contains(machine.MachineId, toComplete) && !slices.Contains(args, machine.MachineId) {
|
||||||
|
ret = append(ret, machine.MachineId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ret, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
},
|
||||||
|
RunE: runMachinesDelete,
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(os.Stderr, "successfully delete %d machines\n", deleted)
|
return cmdMachinesDelete
|
||||||
|
}
|
||||||
|
|
||||||
|
func runMachinesDelete(cmd *cobra.Command, args []string) error {
|
||||||
|
for _, machineID := range args {
|
||||||
|
err := dbClient.DeleteWatcher(machineID)
|
||||||
|
if err != nil {
|
||||||
|
log.Errorf("unable to delete machine '%s': %s", machineID, err)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
log.Infof("machine '%s' deleted successfully", machineID)
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliMachines) newPruneCmd() *cobra.Command {
|
func NewMachinesPruneCmd() *cobra.Command {
|
||||||
var (
|
var parsedDuration time.Duration
|
||||||
duration time.Duration
|
cmdMachinesPrune := &cobra.Command{
|
||||||
notValidOnly bool
|
|
||||||
force bool
|
|
||||||
)
|
|
||||||
|
|
||||||
const defaultDuration = 10 * time.Minute
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "prune",
|
Use: "prune",
|
||||||
Short: "prune multiple machines from the database",
|
Short: "prune multiple machines from the database",
|
||||||
Long: `prune multiple machines that are not validated or have not connected to the local API in a given duration.`,
|
Long: `prune multiple machines that are not validated or have not connected to the local API in a given duration.`,
|
||||||
|
@ -465,41 +359,125 @@ cscli machines prune --duration 1h
|
||||||
cscli machines prune --not-validated-only --force`,
|
cscli machines prune --not-validated-only --force`,
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return cli.prune(duration, notValidOnly, force)
|
dur, _ := cmd.Flags().GetString("duration")
|
||||||
|
var err error
|
||||||
|
parsedDuration, err = time.ParseDuration(fmt.Sprintf("-%s", dur))
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to parse duration '%s': %s", dur, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
notValidOnly, _ := cmd.Flags().GetBool("not-validated-only")
|
||||||
|
force, _ := cmd.Flags().GetBool("force")
|
||||||
|
if parsedDuration >= 0-60*time.Second && !notValidOnly {
|
||||||
|
var answer bool
|
||||||
|
prompt := &survey.Confirm{
|
||||||
|
Message: "The duration you provided is less than or equal 60 seconds this can break installations do you want to continue ?",
|
||||||
|
Default: false,
|
||||||
}
|
}
|
||||||
|
if err := survey.AskOne(prompt, &answer); err != nil {
|
||||||
flags := cmd.Flags()
|
return fmt.Errorf("unable to ask about prune check: %s", err)
|
||||||
flags.DurationVarP(&duration, "duration", "d", defaultDuration, "duration of time since validated machine last heartbeat")
|
|
||||||
flags.BoolVar(¬ValidOnly, "not-validated-only", false, "only prune machines that are not validated")
|
|
||||||
flags.BoolVar(&force, "force", false, "force prune without asking for confirmation")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
if !answer {
|
||||||
func (cli *cliMachines) validate(machineID string) error {
|
fmt.Println("user aborted prune no changes were made")
|
||||||
if err := cli.db.ValidateMachine(machineID); err != nil {
|
|
||||||
return fmt.Errorf("unable to validate machine '%s': %w", machineID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("machine '%s' validated successfully", machineID)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
machines := make([]*ent.Machine, 0)
|
||||||
|
if pending, err := dbClient.QueryPendingMachine(); err == nil {
|
||||||
|
machines = append(machines, pending...)
|
||||||
|
}
|
||||||
|
if !notValidOnly {
|
||||||
|
if pending, err := dbClient.QueryLastValidatedHeartbeatLT(time.Now().UTC().Add(parsedDuration)); err == nil {
|
||||||
|
machines = append(machines, pending...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(machines) == 0 {
|
||||||
|
fmt.Println("no machines to prune")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
getAgentsTable(color.Output, machines)
|
||||||
|
if !force {
|
||||||
|
var answer bool
|
||||||
|
prompt := &survey.Confirm{
|
||||||
|
Message: "You are about to PERMANENTLY remove the above machines from the database these will NOT be recoverable, continue ?",
|
||||||
|
Default: false,
|
||||||
|
}
|
||||||
|
if err := survey.AskOne(prompt, &answer); err != nil {
|
||||||
|
return fmt.Errorf("unable to ask about prune check: %s", err)
|
||||||
|
}
|
||||||
|
if !answer {
|
||||||
|
fmt.Println("user aborted prune no changes were made")
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
nbDeleted, err := dbClient.BulkDeleteWatchers(machines)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to prune machines: %s", err)
|
||||||
|
}
|
||||||
|
fmt.Printf("successfully delete %d machines\n", nbDeleted)
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmdMachinesPrune.Flags().StringP("duration", "d", "10m", "duration of time since validated machine last heartbeat")
|
||||||
|
cmdMachinesPrune.Flags().Bool("not-validated-only", false, "only prune machines that are not validated")
|
||||||
|
cmdMachinesPrune.Flags().Bool("force", false, "force prune without asking for confirmation")
|
||||||
|
|
||||||
func (cli *cliMachines) newValidateCmd() *cobra.Command {
|
return cmdMachinesPrune
|
||||||
cmd := &cobra.Command{
|
}
|
||||||
|
|
||||||
|
func NewMachinesValidateCmd() *cobra.Command {
|
||||||
|
cmdMachinesValidate := &cobra.Command{
|
||||||
Use: "validate",
|
Use: "validate",
|
||||||
Short: "validate a machine to access the local API",
|
Short: "validate a machine to access the local API",
|
||||||
Long: `validate a machine to access the local API.`,
|
Long: `validate a machine to access the local API.`,
|
||||||
Example: `cscli machines validate "machine_name"`,
|
Example: `cscli machines validate "machine_name"`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
return cli.validate(args[0])
|
machineID := args[0]
|
||||||
|
if err := dbClient.ValidateMachine(machineID); err != nil {
|
||||||
|
return fmt.Errorf("unable to validate machine '%s': %s", machineID, err)
|
||||||
|
}
|
||||||
|
log.Infof("machine '%s' validated successfully", machineID)
|
||||||
|
|
||||||
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdMachinesValidate
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewMachinesCmd() *cobra.Command {
|
||||||
|
var cmdMachines = &cobra.Command{
|
||||||
|
Use: "machines [action]",
|
||||||
|
Short: "Manage local API machines [requires local API]",
|
||||||
|
Long: `To list/add/delete/validate/prune machines.
|
||||||
|
Note: This command requires database direct access, so is intended to be run on the local API machine.
|
||||||
|
`,
|
||||||
|
Example: `cscli machines [action]`,
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
Aliases: []string{"machine"},
|
||||||
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
var err error
|
||||||
|
if err := require.LAPI(csConfig); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
dbClient, err = database.NewClient(csConfig.DbConfig)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("unable to create new database client: %s", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
cmdMachines.AddCommand(NewMachinesListCmd())
|
||||||
|
cmdMachines.AddCommand(NewMachinesAddCmd())
|
||||||
|
cmdMachines.AddCommand(NewMachinesDeleteCmd())
|
||||||
|
cmdMachines.AddCommand(NewMachinesValidateCmd())
|
||||||
|
cmdMachines.AddCommand(NewMachinesPruneCmd())
|
||||||
|
|
||||||
|
return cmdMachines
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,9 +5,9 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/aquasecurity/table"
|
"github.com/aquasecurity/table"
|
||||||
|
"github.com/enescakir/emoji"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
"github.com/crowdsecurity/crowdsec/pkg/database/ent"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/emoji"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func getAgentsTable(out io.Writer, machines []*ent.Machine) {
|
func getAgentsTable(out io.Writer, machines []*ent.Machine) {
|
||||||
|
@ -17,16 +17,17 @@ func getAgentsTable(out io.Writer, machines []*ent.Machine) {
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
for _, m := range machines {
|
for _, m := range machines {
|
||||||
validated := emoji.Prohibited
|
var validated string
|
||||||
if m.IsValidated {
|
if m.IsValidated {
|
||||||
validated = emoji.CheckMark
|
validated = emoji.CheckMark.String()
|
||||||
|
} else {
|
||||||
|
validated = emoji.Prohibited.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
hb, active := getLastHeartbeat(m)
|
hb, active := getLastHeartbeat(m)
|
||||||
if !active {
|
if !active {
|
||||||
hb = emoji.Warning + " " + hb
|
hb = emoji.Warning.String() + " " + hb
|
||||||
}
|
}
|
||||||
|
|
||||||
t.AddRow(m.MachineId, m.IpAddress, m.UpdatedAt.Format(time.RFC3339), validated, m.Version, m.AuthType, hb)
|
t.AddRow(m.MachineId, m.IpAddress, m.UpdatedAt.Format(time.RFC3339), validated, m.Version, m.AuthType, hb)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,111 +4,59 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"slices"
|
"slices"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
cc "github.com/ivanpirog/coloredcobra"
|
cc "github.com/ivanpirog/coloredcobra"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"github.com/spf13/cobra/doc"
|
||||||
"github.com/crowdsecurity/go-cs-lib/trace"
|
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
"github.com/crowdsecurity/crowdsec/pkg/fflag"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var trace_lvl, dbg_lvl, nfo_lvl, wrn_lvl, err_lvl bool
|
||||||
ConfigFilePath string
|
|
||||||
csConfig *csconfig.Config
|
|
||||||
dbClient *database.Client
|
|
||||||
)
|
|
||||||
|
|
||||||
type configGetter func() *csconfig.Config
|
var ConfigFilePath string
|
||||||
|
var csConfig *csconfig.Config
|
||||||
|
var dbClient *database.Client
|
||||||
|
|
||||||
|
var OutputFormat string
|
||||||
|
var OutputColor string
|
||||||
|
|
||||||
var mergedConfig string
|
var mergedConfig string
|
||||||
|
|
||||||
type cliRoot struct {
|
|
||||||
logTrace bool
|
|
||||||
logDebug bool
|
|
||||||
logInfo bool
|
|
||||||
logWarn bool
|
|
||||||
logErr bool
|
|
||||||
outputColor string
|
|
||||||
outputFormat string
|
|
||||||
// flagBranch overrides the value in csConfig.Cscli.HubBranch
|
// flagBranch overrides the value in csConfig.Cscli.HubBranch
|
||||||
flagBranch string
|
var flagBranch = ""
|
||||||
}
|
|
||||||
|
|
||||||
func newCliRoot() *cliRoot {
|
func initConfig() {
|
||||||
return &cliRoot{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// cfg() is a helper function to get the configuration loaded from config.yaml,
|
|
||||||
// we pass it to subcommands because the file is not read until the Execute() call
|
|
||||||
func (cli *cliRoot) cfg() *csconfig.Config {
|
|
||||||
return csConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
// wantedLogLevel returns the log level requested in the command line flags.
|
|
||||||
func (cli *cliRoot) wantedLogLevel() log.Level {
|
|
||||||
switch {
|
|
||||||
case cli.logTrace:
|
|
||||||
return log.TraceLevel
|
|
||||||
case cli.logDebug:
|
|
||||||
return log.DebugLevel
|
|
||||||
case cli.logInfo:
|
|
||||||
return log.InfoLevel
|
|
||||||
case cli.logWarn:
|
|
||||||
return log.WarnLevel
|
|
||||||
case cli.logErr:
|
|
||||||
return log.ErrorLevel
|
|
||||||
default:
|
|
||||||
return log.InfoLevel
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// loadConfigFor loads the configuration file for the given sub-command.
|
|
||||||
// If the sub-command does not need it, it returns a default configuration.
|
|
||||||
func loadConfigFor(command string) (*csconfig.Config, string, error) {
|
|
||||||
noNeedConfig := []string{
|
|
||||||
"doc",
|
|
||||||
"help",
|
|
||||||
"completion",
|
|
||||||
"version",
|
|
||||||
"hubtest",
|
|
||||||
}
|
|
||||||
|
|
||||||
if !slices.Contains(noNeedConfig, command) {
|
|
||||||
log.Debugf("Using %s as configuration file", ConfigFilePath)
|
|
||||||
|
|
||||||
config, merged, err := csconfig.NewConfig(ConfigFilePath, false, false, true)
|
|
||||||
if err != nil {
|
|
||||||
return nil, "", err
|
|
||||||
}
|
|
||||||
|
|
||||||
// set up directory for trace files
|
|
||||||
if err := trace.Init(filepath.Join(config.ConfigPaths.DataDir, "trace")); err != nil {
|
|
||||||
return nil, "", fmt.Errorf("while setting up trace directory: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return config, merged, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return csconfig.NewDefaultConfig(), "", nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// initialize is called before the subcommand is executed.
|
|
||||||
func (cli *cliRoot) initialize() {
|
|
||||||
var err error
|
var err error
|
||||||
|
if trace_lvl {
|
||||||
|
log.SetLevel(log.TraceLevel)
|
||||||
|
} else if dbg_lvl {
|
||||||
|
log.SetLevel(log.DebugLevel)
|
||||||
|
} else if nfo_lvl {
|
||||||
|
log.SetLevel(log.InfoLevel)
|
||||||
|
} else if wrn_lvl {
|
||||||
|
log.SetLevel(log.WarnLevel)
|
||||||
|
} else if err_lvl {
|
||||||
|
log.SetLevel(log.ErrorLevel)
|
||||||
|
}
|
||||||
|
|
||||||
log.SetLevel(cli.wantedLogLevel())
|
if !slices.Contains(NoNeedConfig, os.Args[1]) {
|
||||||
|
log.Debugf("Using %s as configuration file", ConfigFilePath)
|
||||||
csConfig, mergedConfig, err = loadConfigFor(os.Args[1])
|
csConfig, mergedConfig, err = csconfig.NewConfig(ConfigFilePath, false, false, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
csConfig = csconfig.NewDefaultConfig()
|
||||||
|
}
|
||||||
|
|
||||||
// recap of the enabled feature flags, because logging
|
// recap of the enabled feature flags, because logging
|
||||||
// was not enabled when we set them from envvars
|
// was not enabled when we set them from envvars
|
||||||
|
@ -116,22 +64,19 @@ func (cli *cliRoot) initialize() {
|
||||||
log.Debugf("Enabled feature flags: %s", fflist)
|
log.Debugf("Enabled feature flags: %s", fflist)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cli.flagBranch != "" {
|
if flagBranch != "" {
|
||||||
csConfig.Cscli.HubBranch = cli.flagBranch
|
csConfig.Cscli.HubBranch = flagBranch
|
||||||
}
|
}
|
||||||
|
|
||||||
if cli.outputFormat != "" {
|
if OutputFormat != "" {
|
||||||
csConfig.Cscli.Output = cli.outputFormat
|
csConfig.Cscli.Output = OutputFormat
|
||||||
|
if OutputFormat != "json" && OutputFormat != "raw" && OutputFormat != "human" {
|
||||||
|
log.Fatalf("output format %s unknown", OutputFormat)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if csConfig.Cscli.Output == "" {
|
if csConfig.Cscli.Output == "" {
|
||||||
csConfig.Cscli.Output = "human"
|
csConfig.Cscli.Output = "human"
|
||||||
}
|
}
|
||||||
|
|
||||||
if csConfig.Cscli.Output != "human" && csConfig.Cscli.Output != "json" && csConfig.Cscli.Output != "raw" {
|
|
||||||
log.Fatalf("output format '%s' not supported: must be one of human, json, raw", csConfig.Cscli.Output)
|
|
||||||
}
|
|
||||||
|
|
||||||
if csConfig.Cscli.Output == "json" {
|
if csConfig.Cscli.Output == "json" {
|
||||||
log.SetFormatter(&log.JSONFormatter{})
|
log.SetFormatter(&log.JSONFormatter{})
|
||||||
log.SetLevel(log.ErrorLevel)
|
log.SetLevel(log.ErrorLevel)
|
||||||
|
@ -139,44 +84,47 @@ func (cli *cliRoot) initialize() {
|
||||||
log.SetLevel(log.ErrorLevel)
|
log.SetLevel(log.ErrorLevel)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cli.outputColor != "" {
|
if OutputColor != "" {
|
||||||
csConfig.Cscli.Color = cli.outputColor
|
csConfig.Cscli.Color = OutputColor
|
||||||
|
if OutputColor != "yes" && OutputColor != "no" && OutputColor != "auto" {
|
||||||
if cli.outputColor != "yes" && cli.outputColor != "no" && cli.outputColor != "auto" {
|
log.Fatalf("output color %s unknown", OutputColor)
|
||||||
log.Fatalf("output color %s unknown", cli.outputColor)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// list of valid subcommands for the shell completion
|
|
||||||
var validArgs = []string{
|
var validArgs = []string{
|
||||||
"alerts", "appsec-configs", "appsec-rules", "bouncers", "capi", "collections",
|
"scenarios", "parsers", "collections", "capi", "contexts", "lapi", "postoverflows", "machines",
|
||||||
"completion", "config", "console", "contexts", "dashboard", "decisions", "explain",
|
"metrics", "bouncers", "alerts", "decisions", "simulation", "hub", "dashboard",
|
||||||
"hub", "hubtest", "lapi", "machines", "metrics", "notifications", "parsers",
|
"config", "completion", "version", "console", "notifications", "support",
|
||||||
"postoverflows", "scenarios", "simulation", "support", "version",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliRoot) colorize(cmd *cobra.Command) {
|
func prepender(filename string) string {
|
||||||
cc.Init(&cc.Config{
|
const header = `---
|
||||||
RootCmd: cmd,
|
id: %s
|
||||||
Headings: cc.Yellow,
|
title: %s
|
||||||
Commands: cc.Green + cc.Bold,
|
---
|
||||||
CmdShortDescr: cc.Cyan,
|
`
|
||||||
Example: cc.Italic,
|
name := filepath.Base(filename)
|
||||||
ExecName: cc.Bold,
|
base := strings.TrimSuffix(name, filepath.Ext(name))
|
||||||
Aliases: cc.Bold + cc.Italic,
|
return fmt.Sprintf(header, base, strings.ReplaceAll(base, "_", " "))
|
||||||
FlagsDataType: cc.White,
|
|
||||||
Flags: cc.Green,
|
|
||||||
FlagsDescr: cc.Cyan,
|
|
||||||
NoExtraNewlines: true,
|
|
||||||
NoBottomNewline: true,
|
|
||||||
})
|
|
||||||
cmd.SetOut(color.Output)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliRoot) NewCommand() *cobra.Command {
|
func linkHandler(name string) string {
|
||||||
|
return fmt.Sprintf("/cscli/%s", name)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
NoNeedConfig = []string{
|
||||||
|
"help",
|
||||||
|
"completion",
|
||||||
|
"version",
|
||||||
|
"hubtest",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
// set the formatter asap and worry about level later
|
// set the formatter asap and worry about level later
|
||||||
logFormatter := &log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true}
|
logFormatter := &log.TextFormatter{TimestampFormat: "2006-01-02 15:04:05", FullTimestamp: true}
|
||||||
log.SetFormatter(logFormatter)
|
log.SetFormatter(logFormatter)
|
||||||
|
|
||||||
if err := fflag.RegisterAllFeatures(); err != nil {
|
if err := fflag.RegisterAllFeatures(); err != nil {
|
||||||
|
@ -187,7 +135,7 @@ func (cli *cliRoot) NewCommand() *cobra.Command {
|
||||||
log.Fatalf("failed to set feature flags from env: %s", err)
|
log.Fatalf("failed to set feature flags from env: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "cscli",
|
Use: "cscli",
|
||||||
Short: "cscli allows you to manage crowdsec",
|
Short: "cscli allows you to manage crowdsec",
|
||||||
Long: `cscli is the main command to interact with your crowdsec service, scenarios & db.
|
Long: `cscli is the main command to interact with your crowdsec service, scenarios & db.
|
||||||
|
@ -199,25 +147,57 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
|
||||||
/*TBD examples*/
|
/*TBD examples*/
|
||||||
}
|
}
|
||||||
|
|
||||||
cli.colorize(cmd)
|
cc.Init(&cc.Config{
|
||||||
|
RootCmd: rootCmd,
|
||||||
|
Headings: cc.Yellow,
|
||||||
|
Commands: cc.Green + cc.Bold,
|
||||||
|
CmdShortDescr: cc.Cyan,
|
||||||
|
Example: cc.Italic,
|
||||||
|
ExecName: cc.Bold,
|
||||||
|
Aliases: cc.Bold + cc.Italic,
|
||||||
|
FlagsDataType: cc.White,
|
||||||
|
Flags: cc.Green,
|
||||||
|
FlagsDescr: cc.Cyan,
|
||||||
|
})
|
||||||
|
rootCmd.SetOut(color.Output)
|
||||||
|
|
||||||
/*don't sort flags so we can enforce order*/
|
var cmdDocGen = &cobra.Command{
|
||||||
cmd.Flags().SortFlags = false
|
Use: "doc",
|
||||||
|
Short: "Generate the documentation in `./doc/`. Directory must exist.",
|
||||||
|
Args: cobra.ExactArgs(0),
|
||||||
|
Hidden: true,
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if err := doc.GenMarkdownTreeCustom(rootCmd, "./doc/", prepender, linkHandler); err != nil {
|
||||||
|
return fmt.Errorf("Failed to generate cobra doc: %s", err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
rootCmd.AddCommand(cmdDocGen)
|
||||||
|
/*usage*/
|
||||||
|
var cmdVersion = &cobra.Command{
|
||||||
|
Use: "version",
|
||||||
|
Short: "Display version",
|
||||||
|
Args: cobra.ExactArgs(0),
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
cwversion.Show()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
rootCmd.AddCommand(cmdVersion)
|
||||||
|
|
||||||
pflags := cmd.PersistentFlags()
|
rootCmd.PersistentFlags().StringVarP(&ConfigFilePath, "config", "c", csconfig.DefaultConfigPath("config.yaml"), "path to crowdsec config file")
|
||||||
pflags.SortFlags = false
|
rootCmd.PersistentFlags().StringVarP(&OutputFormat, "output", "o", "", "Output format: human, json, raw")
|
||||||
|
rootCmd.PersistentFlags().StringVarP(&OutputColor, "color", "", "auto", "Output color: yes, no, auto")
|
||||||
|
rootCmd.PersistentFlags().BoolVar(&dbg_lvl, "debug", false, "Set logging to debug")
|
||||||
|
rootCmd.PersistentFlags().BoolVar(&nfo_lvl, "info", false, "Set logging to info")
|
||||||
|
rootCmd.PersistentFlags().BoolVar(&wrn_lvl, "warning", false, "Set logging to warning")
|
||||||
|
rootCmd.PersistentFlags().BoolVar(&err_lvl, "error", false, "Set logging to error")
|
||||||
|
rootCmd.PersistentFlags().BoolVar(&trace_lvl, "trace", false, "Set logging to trace")
|
||||||
|
|
||||||
pflags.StringVarP(&ConfigFilePath, "config", "c", csconfig.DefaultConfigPath("config.yaml"), "path to crowdsec config file")
|
rootCmd.PersistentFlags().StringVar(&flagBranch, "branch", "", "Override hub branch on github")
|
||||||
pflags.StringVarP(&cli.outputFormat, "output", "o", "", "Output format: human, json, raw")
|
if err := rootCmd.PersistentFlags().MarkHidden("branch"); err != nil {
|
||||||
pflags.StringVarP(&cli.outputColor, "color", "", "auto", "Output color: yes, no, auto")
|
|
||||||
pflags.BoolVar(&cli.logDebug, "debug", false, "Set logging to debug")
|
|
||||||
pflags.BoolVar(&cli.logInfo, "info", false, "Set logging to info")
|
|
||||||
pflags.BoolVar(&cli.logWarn, "warning", false, "Set logging to warning")
|
|
||||||
pflags.BoolVar(&cli.logErr, "error", false, "Set logging to error")
|
|
||||||
pflags.BoolVar(&cli.logTrace, "trace", false, "Set logging to trace")
|
|
||||||
pflags.StringVar(&cli.flagBranch, "branch", "", "Override hub branch on github")
|
|
||||||
|
|
||||||
if err := pflags.MarkHidden("branch"); err != nil {
|
|
||||||
log.Fatalf("failed to hide flag: %s", err)
|
log.Fatalf("failed to hide flag: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,47 +217,48 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(os.Args) > 1 {
|
if len(os.Args) > 1 {
|
||||||
cobra.OnInitialize(cli.initialize)
|
cobra.OnInitialize(initConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(NewCLIDoc().NewCommand(cmd))
|
/*don't sort flags so we can enforce order*/
|
||||||
cmd.AddCommand(NewCLIVersion().NewCommand())
|
rootCmd.Flags().SortFlags = false
|
||||||
cmd.AddCommand(NewCLIConfig(cli.cfg).NewCommand())
|
rootCmd.PersistentFlags().SortFlags = false
|
||||||
cmd.AddCommand(NewCLIHub(cli.cfg).NewCommand())
|
|
||||||
cmd.AddCommand(NewCLIMetrics(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewConfigCmd())
|
||||||
cmd.AddCommand(NewCLIDashboard(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewHubCmd())
|
||||||
cmd.AddCommand(NewCLIDecisions(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewMetricsCmd())
|
||||||
cmd.AddCommand(NewCLIAlerts(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewDashboardCmd())
|
||||||
cmd.AddCommand(NewCLISimulation(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewDecisionsCmd())
|
||||||
cmd.AddCommand(NewCLIBouncers(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewAlertsCmd())
|
||||||
cmd.AddCommand(NewCLIMachines(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewSimulationCmds())
|
||||||
cmd.AddCommand(NewCLICapi(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewBouncersCmd())
|
||||||
cmd.AddCommand(NewCLILapi(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewMachinesCmd())
|
||||||
cmd.AddCommand(NewCompletionCmd())
|
rootCmd.AddCommand(NewCapiCmd())
|
||||||
cmd.AddCommand(NewCLIConsole(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewLapiCmd())
|
||||||
cmd.AddCommand(NewCLIExplain(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewCompletionCmd())
|
||||||
cmd.AddCommand(NewCLIHubTest(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewConsoleCmd())
|
||||||
cmd.AddCommand(NewCLINotifications(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewExplainCmd())
|
||||||
cmd.AddCommand(NewCLISupport().NewCommand())
|
rootCmd.AddCommand(NewHubTestCmd())
|
||||||
cmd.AddCommand(NewCLIPapi(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewNotificationsCmd())
|
||||||
cmd.AddCommand(NewCLICollection(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewSupportCmd())
|
||||||
cmd.AddCommand(NewCLIParser(cli.cfg).NewCommand())
|
|
||||||
cmd.AddCommand(NewCLIScenario(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewCollectionCLI().NewCommand())
|
||||||
cmd.AddCommand(NewCLIPostOverflow(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewParserCLI().NewCommand())
|
||||||
cmd.AddCommand(NewCLIContext(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewScenarioCLI().NewCommand())
|
||||||
cmd.AddCommand(NewCLIAppsecConfig(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewPostOverflowCLI().NewCommand())
|
||||||
cmd.AddCommand(NewCLIAppsecRule(cli.cfg).NewCommand())
|
rootCmd.AddCommand(NewContextCLI().NewCommand())
|
||||||
|
rootCmd.AddCommand(NewAppsecConfigCLI().NewCommand())
|
||||||
|
rootCmd.AddCommand(NewAppsecRuleCLI().NewCommand())
|
||||||
|
|
||||||
if fflag.CscliSetup.IsEnabled() {
|
if fflag.CscliSetup.IsEnabled() {
|
||||||
cmd.AddCommand(NewSetupCmd())
|
rootCmd.AddCommand(NewSetupCmd())
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
if fflag.PapiClient.IsEnabled() {
|
||||||
|
rootCmd.AddCommand(NewPapiCmd())
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
if err := rootCmd.Execute(); err != nil {
|
||||||
cmd := newCliRoot().NewCommand()
|
|
||||||
if err := cmd.Execute(); err != nil {
|
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -17,63 +16,11 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/maptools"
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/trace"
|
"github.com/crowdsecurity/go-cs-lib/trace"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
// FormatPrometheusMetrics is a complete rip from prom2json
|
||||||
statAcquis map[string]map[string]int
|
func FormatPrometheusMetrics(out io.Writer, url string, formatType string) error {
|
||||||
statParser map[string]map[string]int
|
|
||||||
statBucket map[string]map[string]int
|
|
||||||
statWhitelist map[string]map[string]map[string]int
|
|
||||||
statLapi map[string]map[string]int
|
|
||||||
statLapiMachine map[string]map[string]map[string]int
|
|
||||||
statLapiBouncer map[string]map[string]map[string]int
|
|
||||||
statLapiDecision map[string]struct {
|
|
||||||
NonEmpty int
|
|
||||||
Empty int
|
|
||||||
}
|
|
||||||
statDecision map[string]map[string]map[string]int
|
|
||||||
statAppsecEngine map[string]map[string]int
|
|
||||||
statAppsecRule map[string]map[string]map[string]int
|
|
||||||
statAlert map[string]int
|
|
||||||
statStash map[string]struct {
|
|
||||||
Type string
|
|
||||||
Count int
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
var (
|
|
||||||
ErrMissingConfig = errors.New("prometheus section missing, can't show metrics")
|
|
||||||
ErrMetricsDisabled = errors.New("prometheus is not enabled, can't show metrics")
|
|
||||||
)
|
|
||||||
|
|
||||||
type metricSection interface {
|
|
||||||
Table(out io.Writer, noUnit bool, showEmpty bool)
|
|
||||||
Description() (string, string)
|
|
||||||
}
|
|
||||||
|
|
||||||
type metricStore map[string]metricSection
|
|
||||||
|
|
||||||
func NewMetricStore() metricStore {
|
|
||||||
return metricStore{
|
|
||||||
"acquisition": statAcquis{},
|
|
||||||
"scenarios": statBucket{},
|
|
||||||
"parsers": statParser{},
|
|
||||||
"lapi": statLapi{},
|
|
||||||
"lapi-machine": statLapiMachine{},
|
|
||||||
"lapi-bouncer": statLapiBouncer{},
|
|
||||||
"lapi-decisions": statLapiDecision{},
|
|
||||||
"decisions": statDecision{},
|
|
||||||
"alerts": statAlert{},
|
|
||||||
"stash": statStash{},
|
|
||||||
"appsec-engine": statAppsecEngine{},
|
|
||||||
"appsec-rule": statAppsecRule{},
|
|
||||||
"whitelists": statWhitelist{},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ms metricStore) Fetch(url string) error {
|
|
||||||
mfChan := make(chan *dto.MetricFamily, 1024)
|
mfChan := make(chan *dto.MetricFamily, 1024)
|
||||||
errChan := make(chan error, 1)
|
errChan := make(chan error, 1)
|
||||||
|
|
||||||
|
@ -86,10 +33,9 @@ func (ms metricStore) Fetch(url string) error {
|
||||||
transport.ResponseHeaderTimeout = time.Minute
|
transport.ResponseHeaderTimeout = time.Minute
|
||||||
go func() {
|
go func() {
|
||||||
defer trace.CatchPanic("crowdsec/ShowPrometheus")
|
defer trace.CatchPanic("crowdsec/ShowPrometheus")
|
||||||
|
|
||||||
err := prom2json.FetchMetricFamilies(url, mfChan, transport)
|
err := prom2json.FetchMetricFamilies(url, mfChan, transport)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errChan <- fmt.Errorf("failed to fetch metrics: %w", err)
|
errChan <- fmt.Errorf("failed to fetch prometheus metrics: %w", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
errChan <- nil
|
errChan <- nil
|
||||||
|
@ -104,42 +50,42 @@ func (ms metricStore) Fetch(url string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("Finished reading metrics output, %d entries", len(result))
|
log.Debugf("Finished reading prometheus output, %d entries", len(result))
|
||||||
/*walk*/
|
/*walk*/
|
||||||
|
lapi_decisions_stats := map[string]struct {
|
||||||
mAcquis := ms["acquisition"].(statAcquis)
|
NonEmpty int
|
||||||
mParser := ms["parsers"].(statParser)
|
Empty int
|
||||||
mBucket := ms["scenarios"].(statBucket)
|
}{}
|
||||||
mLapi := ms["lapi"].(statLapi)
|
acquis_stats := map[string]map[string]int{}
|
||||||
mLapiMachine := ms["lapi-machine"].(statLapiMachine)
|
parsers_stats := map[string]map[string]int{}
|
||||||
mLapiBouncer := ms["lapi-bouncer"].(statLapiBouncer)
|
buckets_stats := map[string]map[string]int{}
|
||||||
mLapiDecision := ms["lapi-decisions"].(statLapiDecision)
|
lapi_stats := map[string]map[string]int{}
|
||||||
mDecision := ms["decisions"].(statDecision)
|
lapi_machine_stats := map[string]map[string]map[string]int{}
|
||||||
mAppsecEngine := ms["appsec-engine"].(statAppsecEngine)
|
lapi_bouncer_stats := map[string]map[string]map[string]int{}
|
||||||
mAppsecRule := ms["appsec-rule"].(statAppsecRule)
|
decisions_stats := map[string]map[string]map[string]int{}
|
||||||
mAlert := ms["alerts"].(statAlert)
|
appsec_engine_stats := map[string]map[string]int{}
|
||||||
mStash := ms["stash"].(statStash)
|
appsec_rule_stats := map[string]map[string]map[string]int{}
|
||||||
mWhitelist := ms["whitelists"].(statWhitelist)
|
alerts_stats := map[string]int{}
|
||||||
|
stash_stats := map[string]struct {
|
||||||
|
Type string
|
||||||
|
Count int
|
||||||
|
}{}
|
||||||
|
|
||||||
for idx, fam := range result {
|
for idx, fam := range result {
|
||||||
if !strings.HasPrefix(fam.Name, "cs_") {
|
if !strings.HasPrefix(fam.Name, "cs_") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Tracef("round %d", idx)
|
log.Tracef("round %d", idx)
|
||||||
|
|
||||||
for _, m := range fam.Metrics {
|
for _, m := range fam.Metrics {
|
||||||
metric, ok := m.(prom2json.Metric)
|
metric, ok := m.(prom2json.Metric)
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Debugf("failed to convert metric to prom2json.Metric")
|
log.Debugf("failed to convert metric to prom2json.Metric")
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
name, ok := metric.Labels["name"]
|
name, ok := metric.Labels["name"]
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Debugf("no name in Metric %v", metric.Labels)
|
log.Debugf("no name in Metric %v", metric.Labels)
|
||||||
}
|
}
|
||||||
|
|
||||||
source, ok := metric.Labels["source"]
|
source, ok := metric.Labels["source"]
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Debugf("no source in Metric %v for %s", metric.Labels, fam.Name)
|
log.Debugf("no source in Metric %v for %s", metric.Labels, fam.Name)
|
||||||
|
@ -160,89 +106,148 @@ func (ms metricStore) Fetch(url string) error {
|
||||||
origin := metric.Labels["origin"]
|
origin := metric.Labels["origin"]
|
||||||
action := metric.Labels["action"]
|
action := metric.Labels["action"]
|
||||||
|
|
||||||
appsecEngine := metric.Labels["appsec_engine"]
|
|
||||||
appsecRule := metric.Labels["rule_name"]
|
|
||||||
|
|
||||||
mtype := metric.Labels["type"]
|
mtype := metric.Labels["type"]
|
||||||
|
|
||||||
fval, err := strconv.ParseFloat(value, 32)
|
fval, err := strconv.ParseFloat(value, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Unexpected int value %s : %s", value, err)
|
log.Errorf("Unexpected int value %s : %s", value, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ival := int(fval)
|
ival := int(fval)
|
||||||
|
|
||||||
switch fam.Name {
|
switch fam.Name {
|
||||||
//
|
/*buckets*/
|
||||||
// buckets
|
|
||||||
//
|
|
||||||
case "cs_bucket_created_total":
|
case "cs_bucket_created_total":
|
||||||
mBucket.Process(name, "instantiation", ival)
|
if _, ok := buckets_stats[name]; !ok {
|
||||||
|
buckets_stats[name] = make(map[string]int)
|
||||||
|
}
|
||||||
|
buckets_stats[name]["instantiation"] += ival
|
||||||
case "cs_buckets":
|
case "cs_buckets":
|
||||||
mBucket.Process(name, "curr_count", ival)
|
if _, ok := buckets_stats[name]; !ok {
|
||||||
|
buckets_stats[name] = make(map[string]int)
|
||||||
|
}
|
||||||
|
buckets_stats[name]["curr_count"] += ival
|
||||||
case "cs_bucket_overflowed_total":
|
case "cs_bucket_overflowed_total":
|
||||||
mBucket.Process(name, "overflow", ival)
|
if _, ok := buckets_stats[name]; !ok {
|
||||||
|
buckets_stats[name] = make(map[string]int)
|
||||||
|
}
|
||||||
|
buckets_stats[name]["overflow"] += ival
|
||||||
case "cs_bucket_poured_total":
|
case "cs_bucket_poured_total":
|
||||||
mBucket.Process(name, "pour", ival)
|
if _, ok := buckets_stats[name]; !ok {
|
||||||
mAcquis.Process(source, "pour", ival)
|
buckets_stats[name] = make(map[string]int)
|
||||||
|
}
|
||||||
|
if _, ok := acquis_stats[source]; !ok {
|
||||||
|
acquis_stats[source] = make(map[string]int)
|
||||||
|
}
|
||||||
|
buckets_stats[name]["pour"] += ival
|
||||||
|
acquis_stats[source]["pour"] += ival
|
||||||
case "cs_bucket_underflowed_total":
|
case "cs_bucket_underflowed_total":
|
||||||
mBucket.Process(name, "underflow", ival)
|
if _, ok := buckets_stats[name]; !ok {
|
||||||
//
|
buckets_stats[name] = make(map[string]int)
|
||||||
// parsers
|
}
|
||||||
//
|
buckets_stats[name]["underflow"] += ival
|
||||||
|
/*acquis*/
|
||||||
case "cs_parser_hits_total":
|
case "cs_parser_hits_total":
|
||||||
mAcquis.Process(source, "reads", ival)
|
if _, ok := acquis_stats[source]; !ok {
|
||||||
|
acquis_stats[source] = make(map[string]int)
|
||||||
|
}
|
||||||
|
acquis_stats[source]["reads"] += ival
|
||||||
case "cs_parser_hits_ok_total":
|
case "cs_parser_hits_ok_total":
|
||||||
mAcquis.Process(source, "parsed", ival)
|
if _, ok := acquis_stats[source]; !ok {
|
||||||
|
acquis_stats[source] = make(map[string]int)
|
||||||
|
}
|
||||||
|
acquis_stats[source]["parsed"] += ival
|
||||||
case "cs_parser_hits_ko_total":
|
case "cs_parser_hits_ko_total":
|
||||||
mAcquis.Process(source, "unparsed", ival)
|
if _, ok := acquis_stats[source]; !ok {
|
||||||
|
acquis_stats[source] = make(map[string]int)
|
||||||
|
}
|
||||||
|
acquis_stats[source]["unparsed"] += ival
|
||||||
case "cs_node_hits_total":
|
case "cs_node_hits_total":
|
||||||
mParser.Process(name, "hits", ival)
|
if _, ok := parsers_stats[name]; !ok {
|
||||||
|
parsers_stats[name] = make(map[string]int)
|
||||||
|
}
|
||||||
|
parsers_stats[name]["hits"] += ival
|
||||||
case "cs_node_hits_ok_total":
|
case "cs_node_hits_ok_total":
|
||||||
mParser.Process(name, "parsed", ival)
|
if _, ok := parsers_stats[name]; !ok {
|
||||||
|
parsers_stats[name] = make(map[string]int)
|
||||||
|
}
|
||||||
|
parsers_stats[name]["parsed"] += ival
|
||||||
case "cs_node_hits_ko_total":
|
case "cs_node_hits_ko_total":
|
||||||
mParser.Process(name, "unparsed", ival)
|
if _, ok := parsers_stats[name]; !ok {
|
||||||
//
|
parsers_stats[name] = make(map[string]int)
|
||||||
// whitelists
|
}
|
||||||
//
|
parsers_stats[name]["unparsed"] += ival
|
||||||
case "cs_node_wl_hits_total":
|
|
||||||
mWhitelist.Process(name, reason, "hits", ival)
|
|
||||||
case "cs_node_wl_hits_ok_total":
|
|
||||||
mWhitelist.Process(name, reason, "whitelisted", ival)
|
|
||||||
// track as well whitelisted lines at acquis level
|
|
||||||
mAcquis.Process(source, "whitelisted", ival)
|
|
||||||
//
|
|
||||||
// lapi
|
|
||||||
//
|
|
||||||
case "cs_lapi_route_requests_total":
|
case "cs_lapi_route_requests_total":
|
||||||
mLapi.Process(route, method, ival)
|
if _, ok := lapi_stats[route]; !ok {
|
||||||
|
lapi_stats[route] = make(map[string]int)
|
||||||
|
}
|
||||||
|
lapi_stats[route][method] += ival
|
||||||
case "cs_lapi_machine_requests_total":
|
case "cs_lapi_machine_requests_total":
|
||||||
mLapiMachine.Process(machine, route, method, ival)
|
if _, ok := lapi_machine_stats[machine]; !ok {
|
||||||
|
lapi_machine_stats[machine] = make(map[string]map[string]int)
|
||||||
|
}
|
||||||
|
if _, ok := lapi_machine_stats[machine][route]; !ok {
|
||||||
|
lapi_machine_stats[machine][route] = make(map[string]int)
|
||||||
|
}
|
||||||
|
lapi_machine_stats[machine][route][method] += ival
|
||||||
case "cs_lapi_bouncer_requests_total":
|
case "cs_lapi_bouncer_requests_total":
|
||||||
mLapiBouncer.Process(bouncer, route, method, ival)
|
if _, ok := lapi_bouncer_stats[bouncer]; !ok {
|
||||||
|
lapi_bouncer_stats[bouncer] = make(map[string]map[string]int)
|
||||||
|
}
|
||||||
|
if _, ok := lapi_bouncer_stats[bouncer][route]; !ok {
|
||||||
|
lapi_bouncer_stats[bouncer][route] = make(map[string]int)
|
||||||
|
}
|
||||||
|
lapi_bouncer_stats[bouncer][route][method] += ival
|
||||||
case "cs_lapi_decisions_ko_total", "cs_lapi_decisions_ok_total":
|
case "cs_lapi_decisions_ko_total", "cs_lapi_decisions_ok_total":
|
||||||
mLapiDecision.Process(bouncer, fam.Name, ival)
|
if _, ok := lapi_decisions_stats[bouncer]; !ok {
|
||||||
//
|
lapi_decisions_stats[bouncer] = struct {
|
||||||
// decisions
|
NonEmpty int
|
||||||
//
|
Empty int
|
||||||
|
}{}
|
||||||
|
}
|
||||||
|
x := lapi_decisions_stats[bouncer]
|
||||||
|
if fam.Name == "cs_lapi_decisions_ko_total" {
|
||||||
|
x.Empty += ival
|
||||||
|
} else if fam.Name == "cs_lapi_decisions_ok_total" {
|
||||||
|
x.NonEmpty += ival
|
||||||
|
}
|
||||||
|
lapi_decisions_stats[bouncer] = x
|
||||||
case "cs_active_decisions":
|
case "cs_active_decisions":
|
||||||
mDecision.Process(reason, origin, action, ival)
|
if _, ok := decisions_stats[reason]; !ok {
|
||||||
|
decisions_stats[reason] = make(map[string]map[string]int)
|
||||||
|
}
|
||||||
|
if _, ok := decisions_stats[reason][origin]; !ok {
|
||||||
|
decisions_stats[reason][origin] = make(map[string]int)
|
||||||
|
}
|
||||||
|
decisions_stats[reason][origin][action] += ival
|
||||||
case "cs_alerts":
|
case "cs_alerts":
|
||||||
mAlert.Process(reason, ival)
|
/*if _, ok := alerts_stats[scenario]; !ok {
|
||||||
//
|
alerts_stats[scenario] = make(map[string]int)
|
||||||
// stash
|
}*/
|
||||||
//
|
alerts_stats[reason] += ival
|
||||||
case "cs_cache_size":
|
case "cs_cache_size":
|
||||||
mStash.Process(name, mtype, ival)
|
stash_stats[name] = struct {
|
||||||
//
|
Type string
|
||||||
// appsec
|
Count int
|
||||||
//
|
}{Type: mtype, Count: ival}
|
||||||
case "cs_appsec_reqs_total":
|
case "cs_appsec_reqs_total":
|
||||||
mAppsecEngine.Process(appsecEngine, "processed", ival)
|
if _, ok := appsec_engine_stats[metric.Labels["appsec_engine"]]; !ok {
|
||||||
|
appsec_engine_stats[metric.Labels["appsec_engine"]] = make(map[string]int, 0)
|
||||||
|
}
|
||||||
|
appsec_engine_stats[metric.Labels["appsec_engine"]]["processed"] = ival
|
||||||
case "cs_appsec_block_total":
|
case "cs_appsec_block_total":
|
||||||
mAppsecEngine.Process(appsecEngine, "blocked", ival)
|
if _, ok := appsec_engine_stats[metric.Labels["appsec_engine"]]; !ok {
|
||||||
|
appsec_engine_stats[metric.Labels["appsec_engine"]] = make(map[string]int, 0)
|
||||||
|
}
|
||||||
|
appsec_engine_stats[metric.Labels["appsec_engine"]]["blocked"] = ival
|
||||||
case "cs_appsec_rule_hits":
|
case "cs_appsec_rule_hits":
|
||||||
mAppsecRule.Process(appsecEngine, appsecRule, "triggered", ival)
|
appsecEngine := metric.Labels["appsec_engine"]
|
||||||
|
ruleID := metric.Labels["rule_name"]
|
||||||
|
if _, ok := appsec_rule_stats[appsecEngine]; !ok {
|
||||||
|
appsec_rule_stats[appsecEngine] = make(map[string]map[string]int, 0)
|
||||||
|
}
|
||||||
|
if _, ok := appsec_rule_stats[appsecEngine][ruleID]; !ok {
|
||||||
|
appsec_rule_stats[appsecEngine][ruleID] = make(map[string]int, 0)
|
||||||
|
}
|
||||||
|
appsec_rule_stats[appsecEngine][ruleID]["triggered"] = ival
|
||||||
default:
|
default:
|
||||||
log.Debugf("unknown: %+v", fam.Name)
|
log.Debugf("unknown: %+v", fam.Name)
|
||||||
continue
|
continue
|
||||||
|
@ -250,50 +255,46 @@ func (ms metricStore) Fetch(url string) error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if formatType == "human" {
|
||||||
|
acquisStatsTable(out, acquis_stats)
|
||||||
|
bucketStatsTable(out, buckets_stats)
|
||||||
|
parserStatsTable(out, parsers_stats)
|
||||||
|
lapiStatsTable(out, lapi_stats)
|
||||||
|
lapiMachineStatsTable(out, lapi_machine_stats)
|
||||||
|
lapiBouncerStatsTable(out, lapi_bouncer_stats)
|
||||||
|
lapiDecisionStatsTable(out, lapi_decisions_stats)
|
||||||
|
decisionStatsTable(out, decisions_stats)
|
||||||
|
alertStatsTable(out, alerts_stats)
|
||||||
|
stashStatsTable(out, stash_stats)
|
||||||
|
appsecMetricsToTable(out, appsec_engine_stats)
|
||||||
|
appsecRulesToTable(out, appsec_rule_stats)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type cliMetrics struct {
|
stats := make(map[string]any)
|
||||||
cfg configGetter
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLIMetrics(cfg configGetter) *cliMetrics {
|
stats["acquisition"] = acquis_stats
|
||||||
return &cliMetrics{
|
stats["buckets"] = buckets_stats
|
||||||
cfg: cfg,
|
stats["parsers"] = parsers_stats
|
||||||
}
|
stats["lapi"] = lapi_stats
|
||||||
}
|
stats["lapi_machine"] = lapi_machine_stats
|
||||||
|
stats["lapi_bouncer"] = lapi_bouncer_stats
|
||||||
func (ms metricStore) Format(out io.Writer, sections []string, formatType string, noUnit bool) error {
|
stats["lapi_decisions"] = lapi_decisions_stats
|
||||||
// copy only the sections we want
|
stats["decisions"] = decisions_stats
|
||||||
want := map[string]metricSection{}
|
stats["alerts"] = alerts_stats
|
||||||
|
stats["stash"] = stash_stats
|
||||||
// if explicitly asking for sections, we want to show empty tables
|
|
||||||
showEmpty := len(sections) > 0
|
|
||||||
|
|
||||||
// if no sections are specified, we want all of them
|
|
||||||
if len(sections) == 0 {
|
|
||||||
sections = maptools.SortedKeys(ms)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, section := range sections {
|
|
||||||
want[section] = ms[section]
|
|
||||||
}
|
|
||||||
|
|
||||||
switch formatType {
|
switch formatType {
|
||||||
case "human":
|
|
||||||
for _, section := range maptools.SortedKeys(want) {
|
|
||||||
want[section].Table(out, noUnit, showEmpty)
|
|
||||||
}
|
|
||||||
case "json":
|
case "json":
|
||||||
x, err := json.MarshalIndent(want, "", " ")
|
x, err := json.MarshalIndent(stats, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to marshal metrics: %w", err)
|
return fmt.Errorf("failed to unmarshal metrics : %v", err)
|
||||||
}
|
}
|
||||||
out.Write(x)
|
out.Write(x)
|
||||||
case "raw":
|
case "raw":
|
||||||
x, err := yaml.Marshal(want)
|
x, err := yaml.Marshal(stats)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to marshal metrics: %w", err)
|
return fmt.Errorf("failed to unmarshal metrics : %v", err)
|
||||||
}
|
}
|
||||||
out.Write(x)
|
out.Write(x)
|
||||||
default:
|
default:
|
||||||
|
@ -303,195 +304,52 @@ func (ms metricStore) Format(out io.Writer, sections []string, formatType string
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliMetrics) show(sections []string, url string, noUnit bool) error {
|
var noUnit bool
|
||||||
cfg := cli.cfg()
|
|
||||||
|
func runMetrics(cmd *cobra.Command, args []string) error {
|
||||||
|
flags := cmd.Flags()
|
||||||
|
|
||||||
|
url, err := flags.GetString("url")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if url != "" {
|
if url != "" {
|
||||||
cfg.Cscli.PrometheusUrl = url
|
csConfig.Cscli.PrometheusUrl = url
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Prometheus == nil {
|
noUnit, err = flags.GetBool("no-unit")
|
||||||
return ErrMissingConfig
|
if err != nil {
|
||||||
}
|
|
||||||
|
|
||||||
if !cfg.Prometheus.Enabled {
|
|
||||||
return ErrMetricsDisabled
|
|
||||||
}
|
|
||||||
|
|
||||||
ms := NewMetricStore()
|
|
||||||
|
|
||||||
if err := ms.Fetch(cfg.Cscli.PrometheusUrl); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// any section that we don't have in the store is an error
|
if csConfig.Prometheus == nil {
|
||||||
for _, section := range sections {
|
return fmt.Errorf("prometheus section missing, can't show metrics")
|
||||||
if _, ok := ms[section]; !ok {
|
|
||||||
return fmt.Errorf("unknown metrics type: %s", section)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := ms.Format(color.Output, sections, cfg.Cscli.Output, noUnit); err != nil {
|
if !csConfig.Prometheus.Enabled {
|
||||||
|
return fmt.Errorf("prometheus is not enabled, can't show metrics")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = FormatPrometheusMetrics(color.Output, csConfig.Cscli.PrometheusUrl, csConfig.Cscli.Output); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliMetrics) NewCommand() *cobra.Command {
|
func NewMetricsCmd() *cobra.Command {
|
||||||
var (
|
cmdMetrics := &cobra.Command{
|
||||||
url string
|
|
||||||
noUnit bool
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "metrics",
|
Use: "metrics",
|
||||||
Short: "Display crowdsec prometheus metrics.",
|
Short: "Display crowdsec prometheus metrics.",
|
||||||
Long: `Fetch metrics from a Local API server and display them`,
|
Long: `Fetch metrics from the prometheus server and display them in a human-friendly way`,
|
||||||
Example: `# Show all Metrics, skip empty tables (same as "cecli metrics show")
|
|
||||||
cscli metrics
|
|
||||||
|
|
||||||
# Show only some metrics, connect to a different url
|
|
||||||
cscli metrics --url http://lapi.local:6060/metrics show acquisition parsers
|
|
||||||
|
|
||||||
# List available metric types
|
|
||||||
cscli metrics list`,
|
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: runMetrics,
|
||||||
return cli.show(nil, url, noUnit)
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
flags := cmd.Flags()
|
flags := cmdMetrics.PersistentFlags()
|
||||||
flags.StringVarP(&url, "url", "u", "", "Prometheus url (http://<ip>:<port>/metrics)")
|
flags.StringP("url", "u", "", "Prometheus url (http://<ip>:<port>/metrics)")
|
||||||
flags.BoolVar(&noUnit, "no-unit", false, "Show the real number instead of formatted with units")
|
flags.Bool("no-unit", false, "Show the real number instead of formatted with units")
|
||||||
|
|
||||||
cmd.AddCommand(cli.newShowCmd())
|
return cmdMetrics
|
||||||
cmd.AddCommand(cli.newListCmd())
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
// expandAlias returns a list of sections. The input can be a list of sections or alias.
|
|
||||||
func (cli *cliMetrics) expandAlias(args []string) []string {
|
|
||||||
ret := []string{}
|
|
||||||
|
|
||||||
for _, section := range args {
|
|
||||||
switch section {
|
|
||||||
case "engine":
|
|
||||||
ret = append(ret, "acquisition", "parsers", "scenarios", "stash", "whitelists")
|
|
||||||
case "lapi":
|
|
||||||
ret = append(ret, "alerts", "decisions", "lapi", "lapi-bouncer", "lapi-decisions", "lapi-machine")
|
|
||||||
case "appsec":
|
|
||||||
ret = append(ret, "appsec-engine", "appsec-rule")
|
|
||||||
default:
|
|
||||||
ret = append(ret, section)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ret
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliMetrics) newShowCmd() *cobra.Command {
|
|
||||||
var (
|
|
||||||
url string
|
|
||||||
noUnit bool
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "show [type]...",
|
|
||||||
Short: "Display all or part of the available metrics.",
|
|
||||||
Long: `Fetch metrics from a Local API server and display them, optionally filtering on specific types.`,
|
|
||||||
Example: `# Show all Metrics, skip empty tables
|
|
||||||
cscli metrics show
|
|
||||||
|
|
||||||
# Use an alias: "engine", "lapi" or "appsec" to show a group of metrics
|
|
||||||
cscli metrics show engine
|
|
||||||
|
|
||||||
# Show some specific metrics, show empty tables, connect to a different url
|
|
||||||
cscli metrics show acquisition parsers scenarios stash --url http://lapi.local:6060/metrics
|
|
||||||
|
|
||||||
# To list available metric types, use "cscli metrics list"
|
|
||||||
cscli metrics list; cscli metrics list -o json
|
|
||||||
|
|
||||||
# Show metrics in json format
|
|
||||||
cscli metrics show acquisition parsers scenarios stash -o json`,
|
|
||||||
// Positional args are optional
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
|
||||||
args = cli.expandAlias(args)
|
|
||||||
return cli.show(args, url, noUnit)
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
flags := cmd.Flags()
|
|
||||||
flags.StringVarP(&url, "url", "u", "", "Metrics url (http://<ip>:<port>/metrics)")
|
|
||||||
flags.BoolVar(&noUnit, "no-unit", false, "Show the real number instead of formatted with units")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliMetrics) list() error {
|
|
||||||
type metricType struct {
|
|
||||||
Type string `json:"type" yaml:"type"`
|
|
||||||
Title string `json:"title" yaml:"title"`
|
|
||||||
Description string `json:"description" yaml:"description"`
|
|
||||||
}
|
|
||||||
|
|
||||||
var allMetrics []metricType
|
|
||||||
|
|
||||||
ms := NewMetricStore()
|
|
||||||
for _, section := range maptools.SortedKeys(ms) {
|
|
||||||
title, description := ms[section].Description()
|
|
||||||
allMetrics = append(allMetrics, metricType{
|
|
||||||
Type: section,
|
|
||||||
Title: title,
|
|
||||||
Description: description,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
switch cli.cfg().Cscli.Output {
|
|
||||||
case "human":
|
|
||||||
t := newTable(color.Output)
|
|
||||||
t.SetRowLines(true)
|
|
||||||
t.SetHeaders("Type", "Title", "Description")
|
|
||||||
|
|
||||||
for _, metric := range allMetrics {
|
|
||||||
t.AddRow(metric.Type, metric.Title, metric.Description)
|
|
||||||
}
|
|
||||||
|
|
||||||
t.Render()
|
|
||||||
case "json":
|
|
||||||
x, err := json.MarshalIndent(allMetrics, "", " ")
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to marshal metric types: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(string(x))
|
|
||||||
case "raw":
|
|
||||||
x, err := yaml.Marshal(allMetrics)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to marshal metric types: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println(string(x))
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliMetrics) newListCmd() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "list",
|
|
||||||
Short: "List available types of metrics.",
|
|
||||||
Long: `List available types of metrics.`,
|
|
||||||
Args: cobra.ExactArgs(0),
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
return cli.list()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,33 +1,25 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/aquasecurity/table"
|
"github.com/aquasecurity/table"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/maptools"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ErrNilTable means a nil pointer was passed instead of a table instance. This is a programming error.
|
|
||||||
var ErrNilTable = errors.New("nil table")
|
|
||||||
|
|
||||||
func lapiMetricsToTable(t *table.Table, stats map[string]map[string]map[string]int) int {
|
func lapiMetricsToTable(t *table.Table, stats map[string]map[string]map[string]int) int {
|
||||||
// stats: machine -> route -> method -> count
|
// stats: machine -> route -> method -> count
|
||||||
|
|
||||||
// sort keys to keep consistent order when printing
|
// sort keys to keep consistent order when printing
|
||||||
machineKeys := []string{}
|
machineKeys := []string{}
|
||||||
for k := range stats {
|
for k := range stats {
|
||||||
machineKeys = append(machineKeys, k)
|
machineKeys = append(machineKeys, k)
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings(machineKeys)
|
sort.Strings(machineKeys)
|
||||||
|
|
||||||
numRows := 0
|
numRows := 0
|
||||||
|
|
||||||
for _, machine := range machineKeys {
|
for _, machine := range machineKeys {
|
||||||
// oneRow: route -> method -> count
|
// oneRow: route -> method -> count
|
||||||
machineRow := stats[machine]
|
machineRow := stats[machine]
|
||||||
|
@ -39,79 +31,41 @@ func lapiMetricsToTable(t *table.Table, stats map[string]map[string]map[string]i
|
||||||
methodName,
|
methodName,
|
||||||
}
|
}
|
||||||
if count != 0 {
|
if count != 0 {
|
||||||
row = append(row, strconv.Itoa(count))
|
row = append(row, fmt.Sprintf("%d", count))
|
||||||
} else {
|
} else {
|
||||||
row = append(row, "-")
|
row = append(row, "-")
|
||||||
}
|
}
|
||||||
|
|
||||||
t.AddRow(row...)
|
t.AddRow(row...)
|
||||||
|
|
||||||
numRows++
|
numRows++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return numRows
|
return numRows
|
||||||
}
|
}
|
||||||
|
|
||||||
func wlMetricsToTable(t *table.Table, stats map[string]map[string]map[string]int, noUnit bool) (int, error) {
|
func metricsToTable(t *table.Table, stats map[string]map[string]int, keys []string) (int, error) {
|
||||||
if t == nil {
|
if t == nil {
|
||||||
return 0, ErrNilTable
|
return 0, fmt.Errorf("nil table")
|
||||||
}
|
}
|
||||||
|
// sort keys to keep consistent order when printing
|
||||||
|
sortedKeys := []string{}
|
||||||
|
for k := range stats {
|
||||||
|
sortedKeys = append(sortedKeys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(sortedKeys)
|
||||||
|
|
||||||
numRows := 0
|
numRows := 0
|
||||||
|
for _, alabel := range sortedKeys {
|
||||||
for _, name := range maptools.SortedKeys(stats) {
|
|
||||||
for _, reason := range maptools.SortedKeys(stats[name]) {
|
|
||||||
row := []string{
|
|
||||||
name,
|
|
||||||
reason,
|
|
||||||
"-",
|
|
||||||
"-",
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, action := range maptools.SortedKeys(stats[name][reason]) {
|
|
||||||
value := stats[name][reason][action]
|
|
||||||
|
|
||||||
switch action {
|
|
||||||
case "whitelisted":
|
|
||||||
row[3] = strconv.Itoa(value)
|
|
||||||
case "hits":
|
|
||||||
row[2] = strconv.Itoa(value)
|
|
||||||
default:
|
|
||||||
log.Debugf("unexpected counter '%s' for whitelists = %d", action, value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
t.AddRow(row...)
|
|
||||||
|
|
||||||
numRows++
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return numRows, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func metricsToTable(t *table.Table, stats map[string]map[string]int, keys []string, noUnit bool) (int, error) {
|
|
||||||
if t == nil {
|
|
||||||
return 0, ErrNilTable
|
|
||||||
}
|
|
||||||
|
|
||||||
numRows := 0
|
|
||||||
|
|
||||||
for _, alabel := range maptools.SortedKeys(stats) {
|
|
||||||
astats, ok := stats[alabel]
|
astats, ok := stats[alabel]
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
row := []string{
|
row := []string{
|
||||||
alabel,
|
alabel,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, sl := range keys {
|
for _, sl := range keys {
|
||||||
if v, ok := astats[sl]; ok && v != 0 {
|
if v, ok := astats[sl]; ok && v != 0 {
|
||||||
numberToShow := strconv.Itoa(v)
|
numberToShow := fmt.Sprintf("%d", v)
|
||||||
if !noUnit {
|
if !noUnit {
|
||||||
numberToShow = formatNumber(v)
|
numberToShow = formatNumber(v)
|
||||||
}
|
}
|
||||||
|
@ -121,192 +75,76 @@ func metricsToTable(t *table.Table, stats map[string]map[string]int, keys []stri
|
||||||
row = append(row, "-")
|
row = append(row, "-")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t.AddRow(row...)
|
t.AddRow(row...)
|
||||||
|
|
||||||
numRows++
|
numRows++
|
||||||
}
|
}
|
||||||
|
|
||||||
return numRows, nil
|
return numRows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statBucket) Description() (string, string) {
|
func bucketStatsTable(out io.Writer, stats map[string]map[string]int) {
|
||||||
return "Scenario Metrics",
|
|
||||||
`Measure events in different scenarios. Current count is the number of buckets during metrics collection. ` +
|
|
||||||
`Overflows are past event-producing buckets, while Expired are the ones that didn’t receive enough events to Overflow.`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statBucket) Process(bucket, metric string, val int) {
|
|
||||||
if _, ok := s[bucket]; !ok {
|
|
||||||
s[bucket] = make(map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
s[bucket][metric] += val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statBucket) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
t.SetHeaders("Scenario", "Current Count", "Overflows", "Instantiated", "Poured", "Expired")
|
t.SetHeaders("Bucket", "Current Count", "Overflows", "Instantiated", "Poured", "Expired")
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
keys := []string{"curr_count", "overflow", "instantiation", "pour", "underflow"}
|
keys := []string{"curr_count", "overflow", "instantiation", "pour", "underflow"}
|
||||||
|
|
||||||
if numRows, err := metricsToTable(t, s, keys, noUnit); err != nil {
|
if numRows, err := metricsToTable(t, stats, keys); err != nil {
|
||||||
log.Warningf("while collecting scenario stats: %s", err)
|
log.Warningf("while collecting bucket stats: %s", err)
|
||||||
} else if numRows > 0 || showEmpty {
|
} else if numRows > 0 {
|
||||||
title, _ := s.Description()
|
renderTableTitle(out, "\nBucket Metrics:")
|
||||||
renderTableTitle(out, "\n"+title+":")
|
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statAcquis) Description() (string, string) {
|
func acquisStatsTable(out io.Writer, stats map[string]map[string]int) {
|
||||||
return "Acquisition Metrics",
|
|
||||||
`Measures the lines read, parsed, and unparsed per datasource. ` +
|
|
||||||
`Zero read lines indicate a misconfigured or inactive datasource. ` +
|
|
||||||
`Zero parsed lines mean the parser(s) failed. ` +
|
|
||||||
`Non-zero parsed lines are fine as crowdsec selects relevant lines.`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statAcquis) Process(source, metric string, val int) {
|
|
||||||
if _, ok := s[source]; !ok {
|
|
||||||
s[source] = make(map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
s[source][metric] += val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statAcquis) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
t.SetHeaders("Source", "Lines read", "Lines parsed", "Lines unparsed", "Lines poured to bucket", "Lines whitelisted")
|
t.SetHeaders("Source", "Lines read", "Lines parsed", "Lines unparsed", "Lines poured to bucket")
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
keys := []string{"reads", "parsed", "unparsed", "pour", "whitelisted"}
|
keys := []string{"reads", "parsed", "unparsed", "pour"}
|
||||||
|
|
||||||
if numRows, err := metricsToTable(t, s, keys, noUnit); err != nil {
|
if numRows, err := metricsToTable(t, stats, keys); err != nil {
|
||||||
log.Warningf("while collecting acquis stats: %s", err)
|
log.Warningf("while collecting acquis stats: %s", err)
|
||||||
} else if numRows > 0 || showEmpty {
|
} else if numRows > 0 {
|
||||||
title, _ := s.Description()
|
renderTableTitle(out, "\nAcquisition Metrics:")
|
||||||
renderTableTitle(out, "\n"+title+":")
|
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statAppsecEngine) Description() (string, string) {
|
func appsecMetricsToTable(out io.Writer, metrics map[string]map[string]int) {
|
||||||
return "Appsec Metrics",
|
|
||||||
`Measures the number of parsed and blocked requests by the AppSec Component.`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statAppsecEngine) Process(appsecEngine, metric string, val int) {
|
|
||||||
if _, ok := s[appsecEngine]; !ok {
|
|
||||||
s[appsecEngine] = make(map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
s[appsecEngine][metric] += val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statAppsecEngine) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
t.SetHeaders("Appsec Engine", "Processed", "Blocked")
|
t.SetHeaders("Appsec Engine", "Processed", "Blocked")
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
keys := []string{"processed", "blocked"}
|
keys := []string{"processed", "blocked"}
|
||||||
|
if numRows, err := metricsToTable(t, metrics, keys); err != nil {
|
||||||
if numRows, err := metricsToTable(t, s, keys, noUnit); err != nil {
|
|
||||||
log.Warningf("while collecting appsec stats: %s", err)
|
log.Warningf("while collecting appsec stats: %s", err)
|
||||||
} else if numRows > 0 || showEmpty {
|
} else if numRows > 0 {
|
||||||
title, _ := s.Description()
|
renderTableTitle(out, "\nAppsec Metrics:")
|
||||||
renderTableTitle(out, "\n"+title+":")
|
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statAppsecRule) Description() (string, string) {
|
func appsecRulesToTable(out io.Writer, metrics map[string]map[string]map[string]int) {
|
||||||
return "Appsec Rule Metrics",
|
for appsecEngine, appsecEngineRulesStats := range metrics {
|
||||||
`Provides “per AppSec Component” information about the number of matches for loaded AppSec Rules.`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statAppsecRule) Process(appsecEngine, appsecRule string, metric string, val int) {
|
|
||||||
if _, ok := s[appsecEngine]; !ok {
|
|
||||||
s[appsecEngine] = make(map[string]map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := s[appsecEngine][appsecRule]; !ok {
|
|
||||||
s[appsecEngine][appsecRule] = make(map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
s[appsecEngine][appsecRule][metric] += val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statAppsecRule) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
for appsecEngine, appsecEngineRulesStats := range s {
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
t.SetHeaders("Rule ID", "Triggered")
|
t.SetHeaders("Rule ID", "Triggered")
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
keys := []string{"triggered"}
|
keys := []string{"triggered"}
|
||||||
|
if numRows, err := metricsToTable(t, appsecEngineRulesStats, keys); err != nil {
|
||||||
if numRows, err := metricsToTable(t, appsecEngineRulesStats, keys, noUnit); err != nil {
|
|
||||||
log.Warningf("while collecting appsec rules stats: %s", err)
|
log.Warningf("while collecting appsec rules stats: %s", err)
|
||||||
} else if numRows > 0 || showEmpty {
|
} else if numRows > 0 {
|
||||||
renderTableTitle(out, fmt.Sprintf("\nAppsec '%s' Rules Metrics:", appsecEngine))
|
renderTableTitle(out, fmt.Sprintf("\nAppsec '%s' Rules Metrics:", appsecEngine))
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statWhitelist) Description() (string, string) {
|
func parserStatsTable(out io.Writer, stats map[string]map[string]int) {
|
||||||
return "Whitelist Metrics",
|
|
||||||
`Tracks the number of events processed and possibly whitelisted by each parser whitelist.`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statWhitelist) Process(whitelist, reason, metric string, val int) {
|
|
||||||
if _, ok := s[whitelist]; !ok {
|
|
||||||
s[whitelist] = make(map[string]map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := s[whitelist][reason]; !ok {
|
|
||||||
s[whitelist][reason] = make(map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
s[whitelist][reason][metric] += val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statWhitelist) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
t := newTable(out)
|
|
||||||
t.SetRowLines(false)
|
|
||||||
t.SetHeaders("Whitelist", "Reason", "Hits", "Whitelisted")
|
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
|
||||||
|
|
||||||
if numRows, err := wlMetricsToTable(t, s, noUnit); err != nil {
|
|
||||||
log.Warningf("while collecting parsers stats: %s", err)
|
|
||||||
} else if numRows > 0 || showEmpty {
|
|
||||||
title, _ := s.Description()
|
|
||||||
renderTableTitle(out, "\n"+title+":")
|
|
||||||
t.Render()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statParser) Description() (string, string) {
|
|
||||||
return "Parser Metrics",
|
|
||||||
`Tracks the number of events processed by each parser and indicates success of failure. ` +
|
|
||||||
`Zero parsed lines means the parer(s) failed. ` +
|
|
||||||
`Non-zero unparsed lines are fine as crowdsec select relevant lines.`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statParser) Process(parser, metric string, val int) {
|
|
||||||
if _, ok := s[parser]; !ok {
|
|
||||||
s[parser] = make(map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
s[parser][metric] += val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statParser) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
t.SetHeaders("Parsers", "Hits", "Parsed", "Unparsed")
|
t.SetHeaders("Parsers", "Hits", "Parsed", "Unparsed")
|
||||||
|
@ -314,302 +152,187 @@ func (s statParser) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
||||||
|
|
||||||
keys := []string{"hits", "parsed", "unparsed"}
|
keys := []string{"hits", "parsed", "unparsed"}
|
||||||
|
|
||||||
if numRows, err := metricsToTable(t, s, keys, noUnit); err != nil {
|
if numRows, err := metricsToTable(t, stats, keys); err != nil {
|
||||||
log.Warningf("while collecting parsers stats: %s", err)
|
log.Warningf("while collecting parsers stats: %s", err)
|
||||||
} else if numRows > 0 || showEmpty {
|
} else if numRows > 0 {
|
||||||
title, _ := s.Description()
|
renderTableTitle(out, "\nParser Metrics:")
|
||||||
renderTableTitle(out, "\n"+title+":")
|
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statStash) Description() (string, string) {
|
func stashStatsTable(out io.Writer, stats map[string]struct {
|
||||||
return "Parser Stash Metrics",
|
|
||||||
`Tracks the status of stashes that might be created by various parsers and scenarios.`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statStash) Process(name, mtype string, val int) {
|
|
||||||
s[name] = struct {
|
|
||||||
Type string
|
Type string
|
||||||
Count int
|
Count int
|
||||||
}{
|
}) {
|
||||||
Type: mtype,
|
|
||||||
Count: val,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statStash) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
t.SetHeaders("Name", "Type", "Items")
|
t.SetHeaders("Name", "Type", "Items")
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
// unfortunately, we can't reuse metricsToTable as the structure is too different :/
|
// unfortunately, we can't reuse metricsToTable as the structure is too different :/
|
||||||
numRows := 0
|
sortedKeys := []string{}
|
||||||
|
for k := range stats {
|
||||||
|
sortedKeys = append(sortedKeys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(sortedKeys)
|
||||||
|
|
||||||
for _, alabel := range maptools.SortedKeys(s) {
|
numRows := 0
|
||||||
astats := s[alabel]
|
for _, alabel := range sortedKeys {
|
||||||
|
astats := stats[alabel]
|
||||||
|
|
||||||
row := []string{
|
row := []string{
|
||||||
alabel,
|
alabel,
|
||||||
astats.Type,
|
astats.Type,
|
||||||
strconv.Itoa(astats.Count),
|
fmt.Sprintf("%d", astats.Count),
|
||||||
}
|
}
|
||||||
t.AddRow(row...)
|
t.AddRow(row...)
|
||||||
|
|
||||||
numRows++
|
numRows++
|
||||||
}
|
}
|
||||||
|
if numRows > 0 {
|
||||||
if numRows > 0 || showEmpty {
|
renderTableTitle(out, "\nParser Stash Metrics:")
|
||||||
title, _ := s.Description()
|
|
||||||
renderTableTitle(out, "\n"+title+":")
|
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statLapi) Description() (string, string) {
|
func lapiStatsTable(out io.Writer, stats map[string]map[string]int) {
|
||||||
return "Local API Metrics",
|
|
||||||
`Monitors the requests made to local API routes.`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statLapi) Process(route, method string, val int) {
|
|
||||||
if _, ok := s[route]; !ok {
|
|
||||||
s[route] = make(map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
s[route][method] += val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statLapi) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
t.SetHeaders("Route", "Method", "Hits")
|
t.SetHeaders("Route", "Method", "Hits")
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
// unfortunately, we can't reuse metricsToTable as the structure is too different :/
|
// unfortunately, we can't reuse metricsToTable as the structure is too different :/
|
||||||
numRows := 0
|
sortedKeys := []string{}
|
||||||
|
for k := range stats {
|
||||||
|
sortedKeys = append(sortedKeys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(sortedKeys)
|
||||||
|
|
||||||
for _, alabel := range maptools.SortedKeys(s) {
|
numRows := 0
|
||||||
astats := s[alabel]
|
for _, alabel := range sortedKeys {
|
||||||
|
astats := stats[alabel]
|
||||||
|
|
||||||
subKeys := []string{}
|
subKeys := []string{}
|
||||||
for skey := range astats {
|
for skey := range astats {
|
||||||
subKeys = append(subKeys, skey)
|
subKeys = append(subKeys, skey)
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings(subKeys)
|
sort.Strings(subKeys)
|
||||||
|
|
||||||
for _, sl := range subKeys {
|
for _, sl := range subKeys {
|
||||||
row := []string{
|
row := []string{
|
||||||
alabel,
|
alabel,
|
||||||
sl,
|
sl,
|
||||||
strconv.Itoa(astats[sl]),
|
fmt.Sprintf("%d", astats[sl]),
|
||||||
}
|
}
|
||||||
|
|
||||||
t.AddRow(row...)
|
t.AddRow(row...)
|
||||||
|
|
||||||
numRows++
|
numRows++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if numRows > 0 || showEmpty {
|
if numRows > 0 {
|
||||||
title, _ := s.Description()
|
renderTableTitle(out, "\nLocal API Metrics:")
|
||||||
renderTableTitle(out, "\n"+title+":")
|
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statLapiMachine) Description() (string, string) {
|
func lapiMachineStatsTable(out io.Writer, stats map[string]map[string]map[string]int) {
|
||||||
return "Local API Machines Metrics",
|
|
||||||
`Tracks the number of calls to the local API from each registered machine.`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statLapiMachine) Process(machine, route, method string, val int) {
|
|
||||||
if _, ok := s[machine]; !ok {
|
|
||||||
s[machine] = make(map[string]map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := s[machine][route]; !ok {
|
|
||||||
s[machine][route] = make(map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
s[machine][route][method] += val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statLapiMachine) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
t.SetHeaders("Machine", "Route", "Method", "Hits")
|
t.SetHeaders("Machine", "Route", "Method", "Hits")
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
numRows := lapiMetricsToTable(t, s)
|
numRows := lapiMetricsToTable(t, stats)
|
||||||
|
|
||||||
if numRows > 0 || showEmpty {
|
if numRows > 0 {
|
||||||
title, _ := s.Description()
|
renderTableTitle(out, "\nLocal API Machines Metrics:")
|
||||||
renderTableTitle(out, "\n"+title+":")
|
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statLapiBouncer) Description() (string, string) {
|
func lapiBouncerStatsTable(out io.Writer, stats map[string]map[string]map[string]int) {
|
||||||
return "Local API Bouncers Metrics",
|
|
||||||
`Tracks total hits to remediation component related API routes.`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statLapiBouncer) Process(bouncer, route, method string, val int) {
|
|
||||||
if _, ok := s[bouncer]; !ok {
|
|
||||||
s[bouncer] = make(map[string]map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := s[bouncer][route]; !ok {
|
|
||||||
s[bouncer][route] = make(map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
s[bouncer][route][method] += val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statLapiBouncer) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
t.SetHeaders("Bouncer", "Route", "Method", "Hits")
|
t.SetHeaders("Bouncer", "Route", "Method", "Hits")
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
numRows := lapiMetricsToTable(t, s)
|
numRows := lapiMetricsToTable(t, stats)
|
||||||
|
|
||||||
if numRows > 0 || showEmpty {
|
if numRows > 0 {
|
||||||
title, _ := s.Description()
|
renderTableTitle(out, "\nLocal API Bouncers Metrics:")
|
||||||
renderTableTitle(out, "\n"+title+":")
|
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statLapiDecision) Description() (string, string) {
|
func lapiDecisionStatsTable(out io.Writer, stats map[string]struct {
|
||||||
return "Local API Bouncers Decisions",
|
|
||||||
`Tracks the number of empty/non-empty answers from LAPI to bouncers that are working in "live" mode.`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statLapiDecision) Process(bouncer, fam string, val int) {
|
|
||||||
if _, ok := s[bouncer]; !ok {
|
|
||||||
s[bouncer] = struct {
|
|
||||||
NonEmpty int
|
NonEmpty int
|
||||||
Empty int
|
Empty int
|
||||||
}{}
|
},
|
||||||
}
|
) {
|
||||||
|
|
||||||
x := s[bouncer]
|
|
||||||
|
|
||||||
switch fam {
|
|
||||||
case "cs_lapi_decisions_ko_total":
|
|
||||||
x.Empty += val
|
|
||||||
case "cs_lapi_decisions_ok_total":
|
|
||||||
x.NonEmpty += val
|
|
||||||
}
|
|
||||||
|
|
||||||
s[bouncer] = x
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statLapiDecision) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
t.SetHeaders("Bouncer", "Empty answers", "Non-empty answers")
|
t.SetHeaders("Bouncer", "Empty answers", "Non-empty answers")
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
numRows := 0
|
numRows := 0
|
||||||
|
for bouncer, hits := range stats {
|
||||||
for bouncer, hits := range s {
|
|
||||||
t.AddRow(
|
t.AddRow(
|
||||||
bouncer,
|
bouncer,
|
||||||
strconv.Itoa(hits.Empty),
|
fmt.Sprintf("%d", hits.Empty),
|
||||||
strconv.Itoa(hits.NonEmpty),
|
fmt.Sprintf("%d", hits.NonEmpty),
|
||||||
)
|
)
|
||||||
|
|
||||||
numRows++
|
numRows++
|
||||||
}
|
}
|
||||||
|
|
||||||
if numRows > 0 || showEmpty {
|
if numRows > 0 {
|
||||||
title, _ := s.Description()
|
renderTableTitle(out, "\nLocal API Bouncers Decisions:")
|
||||||
renderTableTitle(out, "\n"+title+":")
|
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statDecision) Description() (string, string) {
|
func decisionStatsTable(out io.Writer, stats map[string]map[string]map[string]int) {
|
||||||
return "Local API Decisions",
|
|
||||||
`Provides information about all currently active decisions. ` +
|
|
||||||
`Includes both local (crowdsec) and global decisions (CAPI), and lists subscriptions (lists).`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statDecision) Process(reason, origin, action string, val int) {
|
|
||||||
if _, ok := s[reason]; !ok {
|
|
||||||
s[reason] = make(map[string]map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, ok := s[reason][origin]; !ok {
|
|
||||||
s[reason][origin] = make(map[string]int)
|
|
||||||
}
|
|
||||||
|
|
||||||
s[reason][origin][action] += val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statDecision) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
t.SetHeaders("Reason", "Origin", "Action", "Count")
|
t.SetHeaders("Reason", "Origin", "Action", "Count")
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
numRows := 0
|
numRows := 0
|
||||||
|
for reason, origins := range stats {
|
||||||
for reason, origins := range s {
|
|
||||||
for origin, actions := range origins {
|
for origin, actions := range origins {
|
||||||
for action, hits := range actions {
|
for action, hits := range actions {
|
||||||
t.AddRow(
|
t.AddRow(
|
||||||
reason,
|
reason,
|
||||||
origin,
|
origin,
|
||||||
action,
|
action,
|
||||||
strconv.Itoa(hits),
|
fmt.Sprintf("%d", hits),
|
||||||
)
|
)
|
||||||
|
|
||||||
numRows++
|
numRows++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if numRows > 0 || showEmpty {
|
if numRows > 0 {
|
||||||
title, _ := s.Description()
|
renderTableTitle(out, "\nLocal API Decisions:")
|
||||||
renderTableTitle(out, "\n"+title+":")
|
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s statAlert) Description() (string, string) {
|
func alertStatsTable(out io.Writer, stats map[string]int) {
|
||||||
return "Local API Alerts",
|
|
||||||
`Tracks the total number of past and present alerts for the installed scenarios.`
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statAlert) Process(reason string, val int) {
|
|
||||||
s[reason] += val
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s statAlert) Table(out io.Writer, noUnit bool, showEmpty bool) {
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetRowLines(false)
|
t.SetRowLines(false)
|
||||||
t.SetHeaders("Reason", "Count")
|
t.SetHeaders("Reason", "Count")
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
numRows := 0
|
numRows := 0
|
||||||
|
for scenario, hits := range stats {
|
||||||
for scenario, hits := range s {
|
|
||||||
t.AddRow(
|
t.AddRow(
|
||||||
scenario,
|
scenario,
|
||||||
strconv.Itoa(hits),
|
fmt.Sprintf("%d", hits),
|
||||||
)
|
)
|
||||||
|
|
||||||
numRows++
|
numRows++
|
||||||
}
|
}
|
||||||
|
|
||||||
if numRows > 0 || showEmpty {
|
if numRows > 0 {
|
||||||
title, _ := s.Description()
|
renderTableTitle(out, "\nLocal API Alerts:")
|
||||||
renderTableTitle(out, "\n"+title+":")
|
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"encoding/csv"
|
"encoding/csv"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
@ -24,13 +23,14 @@ import (
|
||||||
"github.com/crowdsecurity/go-cs-lib/ptr"
|
"github.com/crowdsecurity/go-cs-lib/ptr"
|
||||||
"github.com/crowdsecurity/go-cs-lib/version"
|
"github.com/crowdsecurity/go-cs-lib/version"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csplugin"
|
"github.com/crowdsecurity/crowdsec/pkg/csplugin"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csprofiles"
|
"github.com/crowdsecurity/crowdsec/pkg/csprofiles"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NotificationsCfg struct {
|
type NotificationsCfg struct {
|
||||||
|
@ -39,33 +39,22 @@ type NotificationsCfg struct {
|
||||||
ids []uint
|
ids []uint
|
||||||
}
|
}
|
||||||
|
|
||||||
type cliNotifications struct {
|
func NewNotificationsCmd() *cobra.Command {
|
||||||
cfg configGetter
|
var cmdNotifications = &cobra.Command{
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLINotifications(cfg configGetter) *cliNotifications {
|
|
||||||
return &cliNotifications{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliNotifications) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "notifications [action]",
|
Use: "notifications [action]",
|
||||||
Short: "Helper for notification plugin configuration",
|
Short: "Helper for notification plugin configuration",
|
||||||
Long: "To list/inspect/test notification template",
|
Long: "To list/inspect/test notification template",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
Aliases: []string{"notifications", "notification"},
|
Aliases: []string{"notifications", "notification"},
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
if err := require.LAPI(csConfig); err != nil {
|
||||||
if err := require.LAPI(cfg); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := cfg.LoadAPIClient(); err != nil {
|
if err := require.Profiles(csConfig); err != nil {
|
||||||
return fmt.Errorf("loading api client: %w", err)
|
return err
|
||||||
}
|
}
|
||||||
if err := require.Notifications(cfg); err != nil {
|
if err := require.Notifications(csConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,110 +62,108 @@ func (cli *cliNotifications) NewCommand() *cobra.Command {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.NewListCmd())
|
cmdNotifications.AddCommand(NewNotificationsListCmd())
|
||||||
cmd.AddCommand(cli.NewInspectCmd())
|
cmdNotifications.AddCommand(NewNotificationsInspectCmd())
|
||||||
cmd.AddCommand(cli.NewReinjectCmd())
|
cmdNotifications.AddCommand(NewNotificationsReinjectCmd())
|
||||||
cmd.AddCommand(cli.NewTestCmd())
|
cmdNotifications.AddCommand(NewNotificationsTestCmd())
|
||||||
|
|
||||||
return cmd
|
return cmdNotifications
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliNotifications) getPluginConfigs() (map[string]csplugin.PluginConfig, error) {
|
func getPluginConfigs() (map[string]csplugin.PluginConfig, error) {
|
||||||
cfg := cli.cfg()
|
|
||||||
pcfgs := map[string]csplugin.PluginConfig{}
|
pcfgs := map[string]csplugin.PluginConfig{}
|
||||||
wf := func(path string, info fs.FileInfo, err error) error {
|
wf := func(path string, info fs.FileInfo, err error) error {
|
||||||
if info == nil {
|
if info == nil {
|
||||||
return fmt.Errorf("error while traversing directory %s: %w", path, err)
|
return fmt.Errorf("error while traversing directory %s: %w", path, err)
|
||||||
}
|
}
|
||||||
|
name := filepath.Join(csConfig.ConfigPaths.NotificationDir, info.Name()) //Avoid calling info.Name() twice
|
||||||
name := filepath.Join(cfg.ConfigPaths.NotificationDir, info.Name()) // Avoid calling info.Name() twice
|
|
||||||
if (strings.HasSuffix(name, "yaml") || strings.HasSuffix(name, "yml")) && !(info.IsDir()) {
|
if (strings.HasSuffix(name, "yaml") || strings.HasSuffix(name, "yml")) && !(info.IsDir()) {
|
||||||
ts, err := csplugin.ParsePluginConfigFile(name)
|
ts, err := csplugin.ParsePluginConfigFile(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("loading notifification plugin configuration with %s: %w", name, err)
|
return fmt.Errorf("loading notifification plugin configuration with %s: %w", name, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, t := range ts {
|
for _, t := range ts {
|
||||||
csplugin.SetRequiredFields(&t)
|
csplugin.SetRequiredFields(&t)
|
||||||
pcfgs[t.Name] = t
|
pcfgs[t.Name] = t
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := filepath.Walk(cfg.ConfigPaths.NotificationDir, wf); err != nil {
|
if err := filepath.Walk(csConfig.ConfigPaths.NotificationDir, wf); err != nil {
|
||||||
return nil, fmt.Errorf("while loading notifification plugin configuration: %w", err)
|
return nil, fmt.Errorf("while loading notifification plugin configuration: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return pcfgs, nil
|
return pcfgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliNotifications) getProfilesConfigs() (map[string]NotificationsCfg, error) {
|
func getProfilesConfigs() (map[string]NotificationsCfg, error) {
|
||||||
cfg := cli.cfg()
|
|
||||||
// A bit of a tricky stuf now: reconcile profiles and notification plugins
|
// A bit of a tricky stuf now: reconcile profiles and notification plugins
|
||||||
pcfgs, err := cli.getPluginConfigs()
|
pcfgs, err := getPluginConfigs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
ncfgs := map[string]NotificationsCfg{}
|
ncfgs := map[string]NotificationsCfg{}
|
||||||
for _, pc := range pcfgs {
|
profiles, err := csprofiles.NewProfile(csConfig.API.Server.Profiles)
|
||||||
ncfgs[pc.Name] = NotificationsCfg{
|
|
||||||
Config: pc,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
profiles, err := csprofiles.NewProfile(cfg.API.Server.Profiles)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("while extracting profiles from configuration: %w", err)
|
return nil, fmt.Errorf("while extracting profiles from configuration: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for profileID, profile := range profiles {
|
for profileID, profile := range profiles {
|
||||||
|
loop:
|
||||||
for _, notif := range profile.Cfg.Notifications {
|
for _, notif := range profile.Cfg.Notifications {
|
||||||
pc, ok := pcfgs[notif]
|
for name, pc := range pcfgs {
|
||||||
if !ok {
|
if notif == name {
|
||||||
return nil, fmt.Errorf("notification plugin '%s' does not exist", notif)
|
if _, ok := ncfgs[pc.Name]; !ok {
|
||||||
|
ncfgs[pc.Name] = NotificationsCfg{
|
||||||
|
Config: pc,
|
||||||
|
Profiles: []*csconfig.ProfileCfg{profile.Cfg},
|
||||||
|
ids: []uint{uint(profileID)},
|
||||||
|
}
|
||||||
|
continue loop
|
||||||
|
}
|
||||||
|
tmp := ncfgs[pc.Name]
|
||||||
|
for _, pr := range tmp.Profiles {
|
||||||
|
var profiles []*csconfig.ProfileCfg
|
||||||
|
if pr.Name == profile.Cfg.Name {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
profiles = append(tmp.Profiles, profile.Cfg)
|
||||||
|
ids := append(tmp.ids, uint(profileID))
|
||||||
|
ncfgs[pc.Name] = NotificationsCfg{
|
||||||
|
Config: tmp.Config,
|
||||||
|
Profiles: profiles,
|
||||||
|
ids: ids,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp, ok := ncfgs[pc.Name]
|
|
||||||
if !ok {
|
|
||||||
return nil, fmt.Errorf("notification plugin '%s' does not exist", pc.Name)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tmp.Profiles = append(tmp.Profiles, profile.Cfg)
|
|
||||||
tmp.ids = append(tmp.ids, uint(profileID))
|
|
||||||
ncfgs[pc.Name] = tmp
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return ncfgs, nil
|
return ncfgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliNotifications) NewListCmd() *cobra.Command {
|
func NewNotificationsListCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
var cmdNotificationsList = &cobra.Command{
|
||||||
Use: "list",
|
Use: "list",
|
||||||
Short: "list active notifications plugins",
|
Short: "list active notifications plugins",
|
||||||
Long: `list active notifications plugins`,
|
Long: `list active notifications plugins`,
|
||||||
Example: `cscli notifications list`,
|
Example: `cscli notifications list`,
|
||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, arg []string) error {
|
||||||
cfg := cli.cfg()
|
ncfgs, err := getProfilesConfigs()
|
||||||
ncfgs, err := cli.getProfilesConfigs()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't build profiles configuration: %w", err)
|
return fmt.Errorf("can't build profiles configuration: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Cscli.Output == "human" {
|
if csConfig.Cscli.Output == "human" {
|
||||||
notificationListTable(color.Output, ncfgs)
|
notificationListTable(color.Output, ncfgs)
|
||||||
} else if cfg.Cscli.Output == "json" {
|
} else if csConfig.Cscli.Output == "json" {
|
||||||
x, err := json.MarshalIndent(ncfgs, "", " ")
|
x, err := json.MarshalIndent(ncfgs, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to marshal notification configuration: %w", err)
|
return fmt.Errorf("failed to marshal notification configuration: %w", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("%s", string(x))
|
fmt.Printf("%s", string(x))
|
||||||
} else if cfg.Cscli.Output == "raw" {
|
} else if csConfig.Cscli.Output == "raw" {
|
||||||
csvwriter := csv.NewWriter(os.Stdout)
|
csvwriter := csv.NewWriter(os.Stdout)
|
||||||
err := csvwriter.Write([]string{"Name", "Type", "Profile name"})
|
err := csvwriter.Write([]string{"Name", "Type", "Profile name"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -194,89 +181,90 @@ func (cli *cliNotifications) NewListCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
csvwriter.Flush()
|
csvwriter.Flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdNotificationsList
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliNotifications) NewInspectCmd() *cobra.Command {
|
func NewNotificationsInspectCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
var cmdNotificationsInspect = &cobra.Command{
|
||||||
Use: "inspect",
|
Use: "inspect",
|
||||||
Short: "Inspect active notifications plugin configuration",
|
Short: "Inspect active notifications plugin configuration",
|
||||||
Long: `Inspect active notifications plugin and show configuration`,
|
Long: `Inspect active notifications plugin and show configuration`,
|
||||||
Example: `cscli notifications inspect <plugin_name>`,
|
Example: `cscli notifications inspect <plugin_name>`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, args []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
if args[0] == "" {
|
||||||
ncfgs, err := cli.getProfilesConfigs()
|
return fmt.Errorf("please provide a plugin name to inspect")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
ncfgs, err := getProfilesConfigs()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't build profiles configuration: %w", err)
|
return fmt.Errorf("can't build profiles configuration: %w", err)
|
||||||
}
|
}
|
||||||
ncfg, ok := ncfgs[args[0]]
|
cfg, ok := ncfgs[args[0]]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("plugin '%s' does not exist or is not active", args[0])
|
return fmt.Errorf("plugin '%s' does not exist or is not active", args[0])
|
||||||
}
|
}
|
||||||
if cfg.Cscli.Output == "human" || cfg.Cscli.Output == "raw" {
|
if csConfig.Cscli.Output == "human" || csConfig.Cscli.Output == "raw" {
|
||||||
fmt.Printf(" - %15s: %15s\n", "Type", ncfg.Config.Type)
|
fmt.Printf(" - %15s: %15s\n", "Type", cfg.Config.Type)
|
||||||
fmt.Printf(" - %15s: %15s\n", "Name", ncfg.Config.Name)
|
fmt.Printf(" - %15s: %15s\n", "Name", cfg.Config.Name)
|
||||||
fmt.Printf(" - %15s: %15s\n", "Timeout", ncfg.Config.TimeOut)
|
fmt.Printf(" - %15s: %15s\n", "Timeout", cfg.Config.TimeOut)
|
||||||
fmt.Printf(" - %15s: %15s\n", "Format", ncfg.Config.Format)
|
fmt.Printf(" - %15s: %15s\n", "Format", cfg.Config.Format)
|
||||||
for k, v := range ncfg.Config.Config {
|
for k, v := range cfg.Config.Config {
|
||||||
fmt.Printf(" - %15s: %15v\n", k, v)
|
fmt.Printf(" - %15s: %15v\n", k, v)
|
||||||
}
|
}
|
||||||
} else if cfg.Cscli.Output == "json" {
|
} else if csConfig.Cscli.Output == "json" {
|
||||||
x, err := json.MarshalIndent(cfg, "", " ")
|
x, err := json.MarshalIndent(cfg, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to marshal notification configuration: %w", err)
|
return fmt.Errorf("failed to marshal notification configuration: %w", err)
|
||||||
}
|
}
|
||||||
fmt.Printf("%s", string(x))
|
fmt.Printf("%s", string(x))
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdNotificationsInspect
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliNotifications) NewTestCmd() *cobra.Command {
|
func NewNotificationsTestCmd() *cobra.Command {
|
||||||
var (
|
var (
|
||||||
pluginBroker csplugin.PluginBroker
|
pluginBroker csplugin.PluginBroker
|
||||||
pluginTomb tomb.Tomb
|
pluginTomb tomb.Tomb
|
||||||
alertOverride string
|
alertOverride string
|
||||||
)
|
)
|
||||||
|
var cmdNotificationsTest = &cobra.Command{
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "test [plugin name]",
|
Use: "test [plugin name]",
|
||||||
Short: "send a generic test alert to notification plugin",
|
Short: "send a generic test alert to notification plugin",
|
||||||
Long: `send a generic test alert to a notification plugin to test configuration even if is not active`,
|
Long: `send a generic test alert to a notification plugin to test configuration even if is not active`,
|
||||||
Example: `cscli notifications test [plugin_name]`,
|
Example: `cscli notifications test [plugin_name]`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
PreRunE: func(_ *cobra.Command, args []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
pconfigs, err := getPluginConfigs()
|
||||||
pconfigs, err := cli.getPluginConfigs()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't build profiles configuration: %w", err)
|
return fmt.Errorf("can't build profiles configuration: %w", err)
|
||||||
}
|
}
|
||||||
pcfg, ok := pconfigs[args[0]]
|
cfg, ok := pconfigs[args[0]]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("plugin name: '%s' does not exist", args[0])
|
return fmt.Errorf("plugin name: '%s' does not exist", args[0])
|
||||||
}
|
}
|
||||||
//Create a single profile with plugin name as notification name
|
//Create a single profile with plugin name as notification name
|
||||||
return pluginBroker.Init(cfg.PluginConfig, []*csconfig.ProfileCfg{
|
return pluginBroker.Init(csConfig.PluginConfig, []*csconfig.ProfileCfg{
|
||||||
{
|
{
|
||||||
Notifications: []string{
|
Notifications: []string{
|
||||||
pcfg.Name,
|
cfg.Name,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, cfg.ConfigPaths)
|
}, csConfig.ConfigPaths)
|
||||||
},
|
},
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
pluginTomb.Go(func() error {
|
pluginTomb.Go(func() error {
|
||||||
pluginBroker.Run(&pluginTomb)
|
pluginBroker.Run(&pluginTomb)
|
||||||
return nil
|
return nil
|
||||||
|
@ -315,31 +303,26 @@ func (cli *cliNotifications) NewTestCmd() *cobra.Command {
|
||||||
if err := yaml.Unmarshal([]byte(alertOverride), alert); err != nil {
|
if err := yaml.Unmarshal([]byte(alertOverride), alert); err != nil {
|
||||||
return fmt.Errorf("failed to unmarshal alert override: %w", err)
|
return fmt.Errorf("failed to unmarshal alert override: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
pluginBroker.PluginChannel <- csplugin.ProfileAlert{
|
pluginBroker.PluginChannel <- csplugin.ProfileAlert{
|
||||||
ProfileID: uint(0),
|
ProfileID: uint(0),
|
||||||
Alert: alert,
|
Alert: alert,
|
||||||
}
|
}
|
||||||
|
|
||||||
//time.Sleep(2 * time.Second) // There's no mechanism to ensure notification has been sent
|
//time.Sleep(2 * time.Second) // There's no mechanism to ensure notification has been sent
|
||||||
pluginTomb.Kill(errors.New("terminating"))
|
pluginTomb.Kill(fmt.Errorf("terminating"))
|
||||||
pluginTomb.Wait()
|
pluginTomb.Wait()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.Flags().StringVarP(&alertOverride, "alert", "a", "", "JSON string used to override alert fields in the generic alert (see crowdsec/pkg/models/alert.go in the source tree for the full definition of the object)")
|
cmdNotificationsTest.Flags().StringVarP(&alertOverride, "alert", "a", "", "JSON string used to override alert fields in the generic alert (see crowdsec/pkg/models/alert.go in the source tree for the full definition of the object)")
|
||||||
|
|
||||||
return cmd
|
return cmdNotificationsTest
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliNotifications) NewReinjectCmd() *cobra.Command {
|
func NewNotificationsReinjectCmd() *cobra.Command {
|
||||||
var (
|
var alertOverride string
|
||||||
alertOverride string
|
var alert *models.Alert
|
||||||
alert *models.Alert
|
|
||||||
)
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
var cmdNotificationsReinject = &cobra.Command{
|
||||||
Use: "reinject",
|
Use: "reinject",
|
||||||
Short: "reinject an alert into profiles to trigger notifications",
|
Short: "reinject an alert into profiles to trigger notifications",
|
||||||
Long: `reinject an alert into profiles to be evaluated by the filter and sent to matched notifications plugins`,
|
Long: `reinject an alert into profiles to be evaluated by the filter and sent to matched notifications plugins`,
|
||||||
|
@ -350,30 +333,25 @@ cscli notifications reinject <alert_id> -a '{"remediation": true,"scenario":"not
|
||||||
`,
|
`,
|
||||||
Args: cobra.ExactArgs(1),
|
Args: cobra.ExactArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
PreRunE: func(_ *cobra.Command, args []string) error {
|
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
var err error
|
var err error
|
||||||
alert, err = cli.fetchAlertFromArgString(args[0])
|
alert, err = FetchAlertFromArgString(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
var (
|
var (
|
||||||
pluginBroker csplugin.PluginBroker
|
pluginBroker csplugin.PluginBroker
|
||||||
pluginTomb tomb.Tomb
|
pluginTomb tomb.Tomb
|
||||||
)
|
)
|
||||||
|
|
||||||
cfg := cli.cfg()
|
|
||||||
|
|
||||||
if alertOverride != "" {
|
if alertOverride != "" {
|
||||||
if err := json.Unmarshal([]byte(alertOverride), alert); err != nil {
|
if err := json.Unmarshal([]byte(alertOverride), alert); err != nil {
|
||||||
return fmt.Errorf("can't unmarshal data in the alert flag: %w", err)
|
return fmt.Errorf("can't unmarshal data in the alert flag: %w", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
err := pluginBroker.Init(csConfig.PluginConfig, csConfig.API.Server.Profiles, csConfig.ConfigPaths)
|
||||||
err := pluginBroker.Init(cfg.PluginConfig, cfg.API.Server.Profiles, cfg.ConfigPaths)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("can't initialize plugins: %w", err)
|
return fmt.Errorf("can't initialize plugins: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -383,7 +361,7 @@ cscli notifications reinject <alert_id> -a '{"remediation": true,"scenario":"not
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
profiles, err := csprofiles.NewProfile(cfg.API.Server.Profiles)
|
profiles, err := csprofiles.NewProfile(csConfig.API.Server.Profiles)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cannot extract profiles from configuration: %w", err)
|
return fmt.Errorf("cannot extract profiles from configuration: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -409,42 +387,37 @@ cscli notifications reinject <alert_id> -a '{"remediation": true,"scenario":"not
|
||||||
default:
|
default:
|
||||||
time.Sleep(50 * time.Millisecond)
|
time.Sleep(50 * time.Millisecond)
|
||||||
log.Info("sleeping\n")
|
log.Info("sleeping\n")
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
if profile.Cfg.OnSuccess == "break" {
|
if profile.Cfg.OnSuccess == "break" {
|
||||||
log.Infof("The profile %s contains a 'on_success: break' so bailing out", profile.Cfg.Name)
|
log.Infof("The profile %s contains a 'on_success: break' so bailing out", profile.Cfg.Name)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//time.Sleep(2 * time.Second) // There's no mechanism to ensure notification has been sent
|
//time.Sleep(2 * time.Second) // There's no mechanism to ensure notification has been sent
|
||||||
pluginTomb.Kill(errors.New("terminating"))
|
pluginTomb.Kill(fmt.Errorf("terminating"))
|
||||||
pluginTomb.Wait()
|
pluginTomb.Wait()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
cmd.Flags().StringVarP(&alertOverride, "alert", "a", "", "JSON string used to override alert fields in the reinjected alert (see crowdsec/pkg/models/alert.go in the source tree for the full definition of the object)")
|
cmdNotificationsReinject.Flags().StringVarP(&alertOverride, "alert", "a", "", "JSON string used to override alert fields in the reinjected alert (see crowdsec/pkg/models/alert.go in the source tree for the full definition of the object)")
|
||||||
|
|
||||||
return cmd
|
return cmdNotificationsReinject
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliNotifications) fetchAlertFromArgString(toParse string) (*models.Alert, error) {
|
func FetchAlertFromArgString(toParse string) (*models.Alert, error) {
|
||||||
cfg := cli.cfg()
|
|
||||||
|
|
||||||
id, err := strconv.Atoi(toParse)
|
id, err := strconv.Atoi(toParse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("bad alert id %s", toParse)
|
return nil, fmt.Errorf("bad alert id %s", toParse)
|
||||||
}
|
}
|
||||||
|
apiURL, err := url.Parse(csConfig.API.Client.Credentials.URL)
|
||||||
apiURL, err := url.Parse(cfg.API.Client.Credentials.URL)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error parsing the URL of the API: %w", err)
|
return nil, fmt.Errorf("error parsing the URL of the API: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client, err := apiclient.NewClient(&apiclient.Config{
|
client, err := apiclient.NewClient(&apiclient.Config{
|
||||||
MachineID: cfg.API.Client.Credentials.Login,
|
MachineID: csConfig.API.Client.Credentials.Login,
|
||||||
Password: strfmt.Password(cfg.API.Client.Credentials.Password),
|
Password: strfmt.Password(csConfig.API.Client.Credentials.Password),
|
||||||
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
||||||
URL: apiURL,
|
URL: apiURL,
|
||||||
VersionPrefix: "v1",
|
VersionPrefix: "v1",
|
||||||
|
@ -452,11 +425,9 @@ func (cli *cliNotifications) fetchAlertFromArgString(toParse string) (*models.Al
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error creating the client for the API: %w", err)
|
return nil, fmt.Errorf("error creating the client for the API: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
alert, _, err := client.Alerts.GetByID(context.Background(), id)
|
alert, _, err := client.Alerts.GetByID(context.Background(), id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't find alert with id %d: %w", id, err)
|
return nil, fmt.Errorf("can't find alert with id %d: %w", id, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return alert, nil
|
return alert, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,43 +2,23 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/aquasecurity/table"
|
"github.com/aquasecurity/table"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/emoji"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func notificationListTable(out io.Writer, ncfgs map[string]NotificationsCfg) {
|
func notificationListTable(out io.Writer, ncfgs map[string]NotificationsCfg) {
|
||||||
t := newLightTable(out)
|
t := newLightTable(out)
|
||||||
t.SetHeaders("Active", "Name", "Type", "Profile name")
|
t.SetHeaders("Name", "Type", "Profile name")
|
||||||
t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetHeaderAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
t.SetAlignment(table.AlignLeft, table.AlignLeft, table.AlignLeft)
|
||||||
|
|
||||||
keys := make([]string, 0, len(ncfgs))
|
for _, b := range ncfgs {
|
||||||
for k := range ncfgs {
|
|
||||||
keys = append(keys, k)
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(keys, func(i, j int) bool {
|
|
||||||
return len(ncfgs[keys[i]].Profiles) > len(ncfgs[keys[j]].Profiles)
|
|
||||||
})
|
|
||||||
|
|
||||||
for _, k := range keys {
|
|
||||||
b := ncfgs[k]
|
|
||||||
profilesList := []string{}
|
profilesList := []string{}
|
||||||
|
|
||||||
for _, p := range b.Profiles {
|
for _, p := range b.Profiles {
|
||||||
profilesList = append(profilesList, p.Name)
|
profilesList = append(profilesList, p.Name)
|
||||||
}
|
}
|
||||||
|
t.AddRow(b.Config.Name, b.Config.Type, strings.Join(profilesList, ", "))
|
||||||
active := emoji.CheckMark
|
|
||||||
if len(profilesList) == 0 {
|
|
||||||
active = emoji.Prohibited
|
|
||||||
}
|
|
||||||
|
|
||||||
t.AddRow(active, b.Config.Name, b.Config.Type, strings.Join(profilesList, ", "))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Render()
|
t.Render()
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -10,76 +9,67 @@ import (
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/ptr"
|
"github.com/crowdsecurity/go-cs-lib/ptr"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/apiserver"
|
"github.com/crowdsecurity/crowdsec/pkg/apiserver"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/database"
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
type cliPapi struct {
|
func NewPapiCmd() *cobra.Command {
|
||||||
cfg configGetter
|
var cmdLapi = &cobra.Command{
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLIPapi(cfg configGetter) *cliPapi {
|
|
||||||
return &cliPapi{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliPapi) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "papi [action]",
|
Use: "papi [action]",
|
||||||
Short: "Manage interaction with Polling API (PAPI)",
|
Short: "Manage interaction with Polling API (PAPI)",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
cfg := cli.cfg()
|
if err := require.LAPI(csConfig); err != nil {
|
||||||
if err := require.LAPI(cfg); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := require.CAPI(cfg); err != nil {
|
if err := require.CAPI(csConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := require.PAPI(cfg); err != nil {
|
if err := require.PAPI(csConfig); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.NewStatusCmd())
|
cmdLapi.AddCommand(NewPapiStatusCmd())
|
||||||
cmd.AddCommand(cli.NewSyncCmd())
|
cmdLapi.AddCommand(NewPapiSyncCmd())
|
||||||
|
|
||||||
return cmd
|
return cmdLapi
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliPapi) NewStatusCmd() *cobra.Command {
|
func NewPapiStatusCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmdCapiStatus := &cobra.Command{
|
||||||
Use: "status",
|
Use: "status",
|
||||||
Short: "Get status of the Polling API",
|
Short: "Get status of the Polling API",
|
||||||
Args: cobra.MinimumNArgs(0),
|
Args: cobra.MinimumNArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
var err error
|
var err error
|
||||||
cfg := cli.cfg()
|
dbClient, err = database.NewClient(csConfig.DbConfig)
|
||||||
dbClient, err = database.NewClient(cfg.DbConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to initialize database client: %w", err)
|
log.Fatalf("unable to initialize database client : %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
apic, err := apiserver.NewAPIC(cfg.API.Server.OnlineClient, dbClient, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
|
apic, err := apiserver.NewAPIC(csConfig.API.Server.OnlineClient, dbClient, csConfig.API.Server.ConsoleConfig, csConfig.API.Server.CapiWhitelists)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to initialize API client: %w", err)
|
log.Fatalf("unable to initialize API client : %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
papi, err := apiserver.NewPAPI(apic, dbClient, cfg.API.Server.ConsoleConfig, log.GetLevel())
|
papi, err := apiserver.NewPAPI(apic, dbClient, csConfig.API.Server.ConsoleConfig, log.GetLevel())
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to initialize PAPI client: %w", err)
|
log.Fatalf("unable to initialize PAPI client : %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
perms, err := papi.GetPermissions()
|
perms, err := papi.GetPermissions()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to get PAPI permissions: %w", err)
|
log.Fatalf("unable to get PAPI permissions: %s", err)
|
||||||
}
|
}
|
||||||
var lastTimestampStr *string
|
var lastTimestampStr *string
|
||||||
lastTimestampStr, err = dbClient.GetConfigItem(apiserver.PapiPullKey)
|
lastTimestampStr, err = dbClient.GetConfigItem(apiserver.PapiPullKey)
|
||||||
|
@ -94,47 +84,45 @@ func (cli *cliPapi) NewStatusCmd() *cobra.Command {
|
||||||
for _, sub := range perms.Categories {
|
for _, sub := range perms.Categories {
|
||||||
log.Infof(" - %s", sub)
|
log.Infof(" - %s", sub)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdCapiStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliPapi) NewSyncCmd() *cobra.Command {
|
func NewPapiSyncCmd() *cobra.Command {
|
||||||
cmd := &cobra.Command{
|
cmdCapiSync := &cobra.Command{
|
||||||
Use: "sync",
|
Use: "sync",
|
||||||
Short: "Sync with the Polling API, pulling all non-expired orders for the instance",
|
Short: "Sync with the Polling API, pulling all non-expired orders for the instance",
|
||||||
Args: cobra.MinimumNArgs(0),
|
Args: cobra.MinimumNArgs(0),
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
var err error
|
var err error
|
||||||
cfg := cli.cfg()
|
|
||||||
t := tomb.Tomb{}
|
t := tomb.Tomb{}
|
||||||
|
dbClient, err = database.NewClient(csConfig.DbConfig)
|
||||||
dbClient, err = database.NewClient(cfg.DbConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to initialize database client: %w", err)
|
log.Fatalf("unable to initialize database client : %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
apic, err := apiserver.NewAPIC(cfg.API.Server.OnlineClient, dbClient, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
|
apic, err := apiserver.NewAPIC(csConfig.API.Server.OnlineClient, dbClient, csConfig.API.Server.ConsoleConfig, csConfig.API.Server.CapiWhitelists)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to initialize API client: %w", err)
|
log.Fatalf("unable to initialize API client : %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Go(apic.Push)
|
t.Go(apic.Push)
|
||||||
|
|
||||||
papi, err := apiserver.NewPAPI(apic, dbClient, cfg.API.Server.ConsoleConfig, log.GetLevel())
|
papi, err := apiserver.NewPAPI(apic, dbClient, csConfig.API.Server.ConsoleConfig, log.GetLevel())
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to initialize PAPI client: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("unable to initialize PAPI client : %s", err)
|
||||||
|
}
|
||||||
t.Go(papi.SyncDecisions)
|
t.Go(papi.SyncDecisions)
|
||||||
|
|
||||||
err = papi.PullOnce(time.Time{}, true)
|
err = papi.PullOnce(time.Time{}, true)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to sync decisions: %w", err)
|
log.Fatalf("unable to sync decisions: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Sending acknowledgements to CAPI")
|
log.Infof("Sending acknowledgements to CAPI")
|
||||||
|
@ -144,9 +132,8 @@ func (cli *cliPapi) NewSyncCmd() *cobra.Command {
|
||||||
t.Wait()
|
t.Wait()
|
||||||
time.Sleep(5 * time.Second) //FIXME: the push done by apic.Push is run inside a sub goroutine, sleep to make sure it's done
|
time.Sleep(5 * time.Second) //FIXME: the push done by apic.Push is run inside a sub goroutine, sleep to make sure it's done
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
return cmd
|
return cmdCapiSync
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,7 +56,3 @@ func HubBranch(cfg *csconfig.Config) string {
|
||||||
|
|
||||||
return branch
|
return branch
|
||||||
}
|
}
|
||||||
|
|
||||||
func HubURLTemplate(cfg *csconfig.Config) string {
|
|
||||||
return cfg.Cscli.HubURLTemplate
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,23 +1,19 @@
|
||||||
package require
|
package require
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LAPI(c *csconfig.Config) error {
|
func LAPI(c *csconfig.Config) error {
|
||||||
if err := c.LoadAPIServer(true); err != nil {
|
if err := c.LoadAPIServer(); err != nil {
|
||||||
return fmt.Errorf("failed to load Local API: %w", err)
|
return fmt.Errorf("failed to load Local API: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.DisableAPI {
|
if c.DisableAPI {
|
||||||
return errors.New("local API is disabled -- this command must be run on the local API machine")
|
return fmt.Errorf("local API is disabled -- this command must be run on the local API machine")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -33,7 +29,7 @@ func CAPI(c *csconfig.Config) error {
|
||||||
|
|
||||||
func PAPI(c *csconfig.Config) error {
|
func PAPI(c *csconfig.Config) error {
|
||||||
if c.API.Server.OnlineClient.Credentials.PapiURL == "" {
|
if c.API.Server.OnlineClient.Credentials.PapiURL == "" {
|
||||||
return errors.New("no PAPI URL in configuration")
|
return fmt.Errorf("no PAPI URL in configuration")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -41,23 +37,31 @@ func PAPI(c *csconfig.Config) error {
|
||||||
|
|
||||||
func CAPIRegistered(c *csconfig.Config) error {
|
func CAPIRegistered(c *csconfig.Config) error {
|
||||||
if c.API.Server.OnlineClient.Credentials == nil {
|
if c.API.Server.OnlineClient.Credentials == nil {
|
||||||
return errors.New("the Central API (CAPI) must be configured with 'cscli capi register'")
|
return fmt.Errorf("the Central API (CAPI) must be configured with 'cscli capi register'")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func DB(c *csconfig.Config) error {
|
func DB(c *csconfig.Config) error {
|
||||||
if err := c.LoadDBConfig(true); err != nil {
|
if err := c.LoadDBConfig(); err != nil {
|
||||||
return fmt.Errorf("this command requires direct database access (must be run on the local API machine): %w", err)
|
return fmt.Errorf("this command requires direct database access (must be run on the local API machine): %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Profiles(c *csconfig.Config) error {
|
||||||
|
if err := c.API.Server.LoadProfiles(); err != nil {
|
||||||
|
return fmt.Errorf("while loading profiles: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func Notifications(c *csconfig.Config) error {
|
func Notifications(c *csconfig.Config) error {
|
||||||
if c.ConfigPaths.NotificationDir == "" {
|
if c.ConfigPaths.NotificationDir == "" {
|
||||||
return errors.New("config_paths.notification_dir is not set in crowdsec config")
|
return fmt.Errorf("config_paths.notification_dir is not set in crowdsec config")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -67,10 +71,10 @@ func Notifications(c *csconfig.Config) error {
|
||||||
func RemoteHub(c *csconfig.Config) *cwhub.RemoteHubCfg {
|
func RemoteHub(c *csconfig.Config) *cwhub.RemoteHubCfg {
|
||||||
// set branch in config, and log if necessary
|
// set branch in config, and log if necessary
|
||||||
branch := HubBranch(c)
|
branch := HubBranch(c)
|
||||||
urlTemplate := HubURLTemplate(c)
|
|
||||||
remote := &cwhub.RemoteHubCfg {
|
remote := &cwhub.RemoteHubCfg {
|
||||||
Branch: branch,
|
Branch: branch,
|
||||||
URLTemplate: urlTemplate,
|
URLTemplate: "https://hub-cdn.crowdsec.net/%s/%s",
|
||||||
|
// URLTemplate: "http://localhost:8000/crowdsecurity/%s/hub/%s",
|
||||||
IndexPath: ".index.json",
|
IndexPath: ".index.json",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,19 +83,14 @@ func RemoteHub(c *csconfig.Config) *cwhub.RemoteHubCfg {
|
||||||
|
|
||||||
// Hub initializes the hub. If a remote configuration is provided, it can be used to download the index and items.
|
// Hub initializes the hub. If a remote configuration is provided, it can be used to download the index and items.
|
||||||
// If no remote parameter is provided, the hub can only be used for local operations.
|
// If no remote parameter is provided, the hub can only be used for local operations.
|
||||||
func Hub(c *csconfig.Config, remote *cwhub.RemoteHubCfg, logger *logrus.Logger) (*cwhub.Hub, error) {
|
func Hub(c *csconfig.Config, remote *cwhub.RemoteHubCfg) (*cwhub.Hub, error) {
|
||||||
local := c.Hub
|
local := c.Hub
|
||||||
|
|
||||||
if local == nil {
|
if local == nil {
|
||||||
return nil, errors.New("you must configure cli before interacting with hub")
|
return nil, fmt.Errorf("you must configure cli before interacting with hub")
|
||||||
}
|
}
|
||||||
|
|
||||||
if logger == nil {
|
hub, err := cwhub.NewHub(local, remote, false)
|
||||||
logger = logrus.New()
|
|
||||||
logger.SetOutput(io.Discard)
|
|
||||||
}
|
|
||||||
|
|
||||||
hub, err := cwhub.NewHub(local, remote, false, logger)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to read Hub index: %w. Run 'sudo cscli hub update' to download the index again", err)
|
return nil, fmt.Errorf("failed to read Hub index: %w. Run 'sudo cscli hub update' to download the index again", err)
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
@ -12,9 +11,10 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/setup"
|
"github.com/crowdsecurity/crowdsec/pkg/setup"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewSetupCmd defines the "cscli setup" command.
|
// NewSetupCmd defines the "cscli setup" command.
|
||||||
|
@ -119,11 +119,9 @@ func runSetupDetect(cmd *cobra.Command, args []string) error {
|
||||||
switch detectConfigFile {
|
switch detectConfigFile {
|
||||||
case "-":
|
case "-":
|
||||||
log.Tracef("Reading detection rules from stdin")
|
log.Tracef("Reading detection rules from stdin")
|
||||||
|
|
||||||
detectReader = os.Stdin
|
detectReader = os.Stdin
|
||||||
default:
|
default:
|
||||||
log.Tracef("Reading detection rules: %s", detectConfigFile)
|
log.Tracef("Reading detection rules: %s", detectConfigFile)
|
||||||
|
|
||||||
detectReader, err = os.Open(detectConfigFile)
|
detectReader, err = os.Open(detectConfigFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -174,7 +172,6 @@ func runSetupDetect(cmd *cobra.Command, args []string) error {
|
||||||
_, err := exec.LookPath("systemctl")
|
_, err := exec.LookPath("systemctl")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Debug("systemctl not available: snubbing systemd")
|
log.Debug("systemctl not available: snubbing systemd")
|
||||||
|
|
||||||
snubSystemd = true
|
snubSystemd = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -186,7 +183,6 @@ func runSetupDetect(cmd *cobra.Command, args []string) error {
|
||||||
|
|
||||||
if forcedOSFamily == "" && forcedOSID != "" {
|
if forcedOSFamily == "" && forcedOSID != "" {
|
||||||
log.Debug("force-os-id is set: force-os-family defaults to 'linux'")
|
log.Debug("force-os-id is set: force-os-family defaults to 'linux'")
|
||||||
|
|
||||||
forcedOSFamily = "linux"
|
forcedOSFamily = "linux"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -224,7 +220,6 @@ func runSetupDetect(cmd *cobra.Command, args []string) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(setup)
|
fmt.Println(setup)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -310,7 +305,7 @@ func runSetupInstallHub(cmd *cobra.Command, args []string) error {
|
||||||
return fmt.Errorf("while reading file %s: %w", fromFile, err)
|
return fmt.Errorf("while reading file %s: %w", fromFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
hub, err := require.Hub(csConfig, require.RemoteHub(csConfig), log.StandardLogger())
|
hub, err := require.Hub(csConfig, require.RemoteHub(csConfig))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -324,7 +319,6 @@ func runSetupInstallHub(cmd *cobra.Command, args []string) error {
|
||||||
|
|
||||||
func runSetupValidate(cmd *cobra.Command, args []string) error {
|
func runSetupValidate(cmd *cobra.Command, args []string) error {
|
||||||
fromFile := args[0]
|
fromFile := args[0]
|
||||||
|
|
||||||
input, err := os.ReadFile(fromFile)
|
input, err := os.ReadFile(fromFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("while reading stdin: %w", err)
|
return fmt.Errorf("while reading stdin: %w", err)
|
||||||
|
@ -332,7 +326,7 @@ func runSetupValidate(cmd *cobra.Command, args []string) error {
|
||||||
|
|
||||||
if err = setup.Validate(input); err != nil {
|
if err = setup.Validate(input); err != nil {
|
||||||
fmt.Printf("%v\n", err)
|
fmt.Printf("%v\n", err)
|
||||||
return errors.New("invalid setup file")
|
return fmt.Errorf("invalid setup file")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -1,209 +1,41 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v2"
|
||||||
|
"slices"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
)
|
)
|
||||||
|
|
||||||
type cliSimulation struct {
|
func addToExclusion(name string) error {
|
||||||
cfg configGetter
|
csConfig.Cscli.SimulationConfig.Exclusions = append(csConfig.Cscli.SimulationConfig.Exclusions, name)
|
||||||
}
|
|
||||||
|
|
||||||
func NewCLISimulation(cfg configGetter) *cliSimulation {
|
|
||||||
return &cliSimulation{
|
|
||||||
cfg: cfg,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliSimulation) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "simulation [command]",
|
|
||||||
Short: "Manage simulation status of scenarios",
|
|
||||||
Example: `cscli simulation status
|
|
||||||
cscli simulation enable crowdsecurity/ssh-bf
|
|
||||||
cscli simulation disable crowdsecurity/ssh-bf`,
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
PersistentPreRunE: func(_ *cobra.Command, _ []string) error {
|
|
||||||
if err := cli.cfg().LoadSimulation(); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if cli.cfg().Cscli.SimulationConfig == nil {
|
|
||||||
return errors.New("no simulation configured")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
|
||||||
PersistentPostRun: func(cmd *cobra.Command, _ []string) {
|
|
||||||
if cmd.Name() != "status" {
|
|
||||||
log.Infof(ReloadMessage())
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cmd.Flags().SortFlags = false
|
|
||||||
cmd.PersistentFlags().SortFlags = false
|
|
||||||
|
|
||||||
cmd.AddCommand(cli.NewEnableCmd())
|
|
||||||
cmd.AddCommand(cli.NewDisableCmd())
|
|
||||||
cmd.AddCommand(cli.NewStatusCmd())
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliSimulation) NewEnableCmd() *cobra.Command {
|
func removeFromExclusion(name string) error {
|
||||||
var forceGlobalSimulation bool
|
index := slices.Index(csConfig.Cscli.SimulationConfig.Exclusions, name)
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "enable [scenario] [-global]",
|
|
||||||
Short: "Enable the simulation, globally or on specified scenarios",
|
|
||||||
Example: `cscli simulation enable`,
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
hub, err := require.Hub(cli.cfg(), nil, nil)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(args) > 0 {
|
|
||||||
for _, scenario := range args {
|
|
||||||
item := hub.GetItem(cwhub.SCENARIOS, scenario)
|
|
||||||
if item == nil {
|
|
||||||
log.Errorf("'%s' doesn't exist or is not a scenario", scenario)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !item.State.Installed {
|
|
||||||
log.Warningf("'%s' isn't enabled", scenario)
|
|
||||||
}
|
|
||||||
isExcluded := slices.Contains(cli.cfg().Cscli.SimulationConfig.Exclusions, scenario)
|
|
||||||
if *cli.cfg().Cscli.SimulationConfig.Simulation && !isExcluded {
|
|
||||||
log.Warning("global simulation is already enabled")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !*cli.cfg().Cscli.SimulationConfig.Simulation && isExcluded {
|
|
||||||
log.Warningf("simulation for '%s' already enabled", scenario)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if *cli.cfg().Cscli.SimulationConfig.Simulation && isExcluded {
|
|
||||||
cli.removeFromExclusion(scenario)
|
|
||||||
log.Printf("simulation enabled for '%s'", scenario)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
cli.addToExclusion(scenario)
|
|
||||||
log.Printf("simulation mode for '%s' enabled", scenario)
|
|
||||||
}
|
|
||||||
if err := cli.dumpSimulationFile(); err != nil {
|
|
||||||
return fmt.Errorf("simulation enable: %w", err)
|
|
||||||
}
|
|
||||||
} else if forceGlobalSimulation {
|
|
||||||
if err := cli.enableGlobalSimulation(); err != nil {
|
|
||||||
return fmt.Errorf("unable to enable global simulation mode: %w", err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
printHelp(cmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cmd.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Enable global simulation (reverse mode)")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliSimulation) NewDisableCmd() *cobra.Command {
|
|
||||||
var forceGlobalSimulation bool
|
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "disable [scenario]",
|
|
||||||
Short: "Disable the simulation mode. Disable only specified scenarios",
|
|
||||||
Example: `cscli simulation disable`,
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
if len(args) > 0 {
|
|
||||||
for _, scenario := range args {
|
|
||||||
isExcluded := slices.Contains(cli.cfg().Cscli.SimulationConfig.Exclusions, scenario)
|
|
||||||
if !*cli.cfg().Cscli.SimulationConfig.Simulation && !isExcluded {
|
|
||||||
log.Warningf("%s isn't in simulation mode", scenario)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !*cli.cfg().Cscli.SimulationConfig.Simulation && isExcluded {
|
|
||||||
cli.removeFromExclusion(scenario)
|
|
||||||
log.Printf("simulation mode for '%s' disabled", scenario)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if isExcluded {
|
|
||||||
log.Warningf("simulation mode is enabled but is already disable for '%s'", scenario)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
cli.addToExclusion(scenario)
|
|
||||||
log.Printf("simulation mode for '%s' disabled", scenario)
|
|
||||||
}
|
|
||||||
if err := cli.dumpSimulationFile(); err != nil {
|
|
||||||
return fmt.Errorf("simulation disable: %w", err)
|
|
||||||
}
|
|
||||||
} else if forceGlobalSimulation {
|
|
||||||
if err := cli.disableGlobalSimulation(); err != nil {
|
|
||||||
return fmt.Errorf("unable to disable global simulation mode: %w", err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
printHelp(cmd)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
cmd.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Disable global simulation (reverse mode)")
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliSimulation) NewStatusCmd() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "status",
|
|
||||||
Short: "Show simulation mode status",
|
|
||||||
Example: `cscli simulation status`,
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
Run: func(_ *cobra.Command, _ []string) {
|
|
||||||
cli.status()
|
|
||||||
},
|
|
||||||
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliSimulation) addToExclusion(name string) {
|
|
||||||
cfg := cli.cfg()
|
|
||||||
cfg.Cscli.SimulationConfig.Exclusions = append(cfg.Cscli.SimulationConfig.Exclusions, name)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli *cliSimulation) removeFromExclusion(name string) {
|
|
||||||
cfg := cli.cfg()
|
|
||||||
index := slices.Index(cfg.Cscli.SimulationConfig.Exclusions, name)
|
|
||||||
|
|
||||||
// Remove element from the slice
|
// Remove element from the slice
|
||||||
cfg.Cscli.SimulationConfig.Exclusions[index] = cfg.Cscli.SimulationConfig.Exclusions[len(cfg.Cscli.SimulationConfig.Exclusions)-1]
|
csConfig.Cscli.SimulationConfig.Exclusions[index] = csConfig.Cscli.SimulationConfig.Exclusions[len(csConfig.Cscli.SimulationConfig.Exclusions)-1]
|
||||||
cfg.Cscli.SimulationConfig.Exclusions[len(cfg.Cscli.SimulationConfig.Exclusions)-1] = ""
|
csConfig.Cscli.SimulationConfig.Exclusions[len(csConfig.Cscli.SimulationConfig.Exclusions)-1] = ""
|
||||||
cfg.Cscli.SimulationConfig.Exclusions = cfg.Cscli.SimulationConfig.Exclusions[:len(cfg.Cscli.SimulationConfig.Exclusions)-1]
|
csConfig.Cscli.SimulationConfig.Exclusions = csConfig.Cscli.SimulationConfig.Exclusions[:len(csConfig.Cscli.SimulationConfig.Exclusions)-1]
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliSimulation) enableGlobalSimulation() error {
|
func enableGlobalSimulation() error {
|
||||||
cfg := cli.cfg()
|
csConfig.Cscli.SimulationConfig.Simulation = new(bool)
|
||||||
cfg.Cscli.SimulationConfig.Simulation = new(bool)
|
*csConfig.Cscli.SimulationConfig.Simulation = true
|
||||||
*cfg.Cscli.SimulationConfig.Simulation = true
|
csConfig.Cscli.SimulationConfig.Exclusions = []string{}
|
||||||
cfg.Cscli.SimulationConfig.Exclusions = []string{}
|
|
||||||
|
|
||||||
if err := cli.dumpSimulationFile(); err != nil {
|
if err := dumpSimulationFile(); err != nil {
|
||||||
return fmt.Errorf("unable to dump simulation file: %w", err)
|
log.Fatalf("unable to dump simulation file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("global simulation: enabled")
|
log.Printf("global simulation: enabled")
|
||||||
|
@ -211,72 +43,221 @@ func (cli *cliSimulation) enableGlobalSimulation() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliSimulation) dumpSimulationFile() error {
|
func dumpSimulationFile() error {
|
||||||
cfg := cli.cfg()
|
newConfigSim, err := yaml.Marshal(csConfig.Cscli.SimulationConfig)
|
||||||
|
|
||||||
newConfigSim, err := yaml.Marshal(cfg.Cscli.SimulationConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to marshal simulation configuration: %w", err)
|
return fmt.Errorf("unable to marshal simulation configuration: %s", err)
|
||||||
}
|
}
|
||||||
|
err = os.WriteFile(csConfig.ConfigPaths.SimulationFilePath, newConfigSim, 0644)
|
||||||
err = os.WriteFile(cfg.ConfigPaths.SimulationFilePath, newConfigSim, 0o644)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("write simulation config in '%s' failed: %w", cfg.ConfigPaths.SimulationFilePath, err)
|
return fmt.Errorf("write simulation config in '%s' failed: %s", csConfig.ConfigPaths.SimulationFilePath, err)
|
||||||
}
|
}
|
||||||
|
log.Debugf("updated simulation file %s", csConfig.ConfigPaths.SimulationFilePath)
|
||||||
log.Debugf("updated simulation file %s", cfg.ConfigPaths.SimulationFilePath)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliSimulation) disableGlobalSimulation() error {
|
func disableGlobalSimulation() error {
|
||||||
cfg := cli.cfg()
|
csConfig.Cscli.SimulationConfig.Simulation = new(bool)
|
||||||
cfg.Cscli.SimulationConfig.Simulation = new(bool)
|
*csConfig.Cscli.SimulationConfig.Simulation = false
|
||||||
*cfg.Cscli.SimulationConfig.Simulation = false
|
|
||||||
|
|
||||||
cfg.Cscli.SimulationConfig.Exclusions = []string{}
|
csConfig.Cscli.SimulationConfig.Exclusions = []string{}
|
||||||
|
newConfigSim, err := yaml.Marshal(csConfig.Cscli.SimulationConfig)
|
||||||
newConfigSim, err := yaml.Marshal(cfg.Cscli.SimulationConfig)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to marshal new simulation configuration: %w", err)
|
return fmt.Errorf("unable to marshal new simulation configuration: %s", err)
|
||||||
}
|
}
|
||||||
|
err = os.WriteFile(csConfig.ConfigPaths.SimulationFilePath, newConfigSim, 0644)
|
||||||
err = os.WriteFile(cfg.ConfigPaths.SimulationFilePath, newConfigSim, 0o644)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to write new simulation config in '%s': %w", cfg.ConfigPaths.SimulationFilePath, err)
|
return fmt.Errorf("unable to write new simulation config in '%s' : %s", csConfig.ConfigPaths.SimulationFilePath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("global simulation: disabled")
|
log.Printf("global simulation: disabled")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cli *cliSimulation) status() {
|
func simulationStatus() error {
|
||||||
cfg := cli.cfg()
|
if csConfig.Cscli.SimulationConfig == nil {
|
||||||
if cfg.Cscli.SimulationConfig == nil {
|
|
||||||
log.Printf("global simulation: disabled (configuration file is missing)")
|
log.Printf("global simulation: disabled (configuration file is missing)")
|
||||||
return
|
return nil
|
||||||
}
|
}
|
||||||
|
if *csConfig.Cscli.SimulationConfig.Simulation {
|
||||||
if *cfg.Cscli.SimulationConfig.Simulation {
|
|
||||||
log.Println("global simulation: enabled")
|
log.Println("global simulation: enabled")
|
||||||
|
if len(csConfig.Cscli.SimulationConfig.Exclusions) > 0 {
|
||||||
if len(cfg.Cscli.SimulationConfig.Exclusions) > 0 {
|
|
||||||
log.Println("Scenarios not in simulation mode :")
|
log.Println("Scenarios not in simulation mode :")
|
||||||
|
for _, scenario := range csConfig.Cscli.SimulationConfig.Exclusions {
|
||||||
for _, scenario := range cfg.Cscli.SimulationConfig.Exclusions {
|
|
||||||
log.Printf(" - %s", scenario)
|
log.Printf(" - %s", scenario)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
log.Println("global simulation: disabled")
|
log.Println("global simulation: disabled")
|
||||||
|
if len(csConfig.Cscli.SimulationConfig.Exclusions) > 0 {
|
||||||
if len(cfg.Cscli.SimulationConfig.Exclusions) > 0 {
|
|
||||||
log.Println("Scenarios in simulation mode :")
|
log.Println("Scenarios in simulation mode :")
|
||||||
|
for _, scenario := range csConfig.Cscli.SimulationConfig.Exclusions {
|
||||||
for _, scenario := range cfg.Cscli.SimulationConfig.Exclusions {
|
|
||||||
log.Printf(" - %s", scenario)
|
log.Printf(" - %s", scenario)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSimulationCmds() *cobra.Command {
|
||||||
|
var cmdSimulation = &cobra.Command{
|
||||||
|
Use: "simulation [command]",
|
||||||
|
Short: "Manage simulation status of scenarios",
|
||||||
|
Example: `cscli simulation status
|
||||||
|
cscli simulation enable crowdsecurity/ssh-bf
|
||||||
|
cscli simulation disable crowdsecurity/ssh-bf`,
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
if err := csConfig.LoadSimulation(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if csConfig.Cscli.SimulationConfig == nil {
|
||||||
|
return fmt.Errorf("no simulation configured")
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
||||||
|
if cmd.Name() != "status" {
|
||||||
|
log.Infof(ReloadMessage())
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmdSimulation.Flags().SortFlags = false
|
||||||
|
cmdSimulation.PersistentFlags().SortFlags = false
|
||||||
|
|
||||||
|
cmdSimulation.AddCommand(NewSimulationEnableCmd())
|
||||||
|
cmdSimulation.AddCommand(NewSimulationDisableCmd())
|
||||||
|
cmdSimulation.AddCommand(NewSimulationStatusCmd())
|
||||||
|
|
||||||
|
return cmdSimulation
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSimulationEnableCmd() *cobra.Command {
|
||||||
|
var forceGlobalSimulation bool
|
||||||
|
|
||||||
|
var cmdSimulationEnable = &cobra.Command{
|
||||||
|
Use: "enable [scenario] [-global]",
|
||||||
|
Short: "Enable the simulation, globally or on specified scenarios",
|
||||||
|
Example: `cscli simulation enable`,
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
hub, err := require.Hub(csConfig, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(args) > 0 {
|
||||||
|
for _, scenario := range args {
|
||||||
|
var item = hub.GetItem(cwhub.SCENARIOS, scenario)
|
||||||
|
if item == nil {
|
||||||
|
log.Errorf("'%s' doesn't exist or is not a scenario", scenario)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !item.State.Installed {
|
||||||
|
log.Warningf("'%s' isn't enabled", scenario)
|
||||||
|
}
|
||||||
|
isExcluded := slices.Contains(csConfig.Cscli.SimulationConfig.Exclusions, scenario)
|
||||||
|
if *csConfig.Cscli.SimulationConfig.Simulation && !isExcluded {
|
||||||
|
log.Warning("global simulation is already enabled")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !*csConfig.Cscli.SimulationConfig.Simulation && isExcluded {
|
||||||
|
log.Warningf("simulation for '%s' already enabled", scenario)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if *csConfig.Cscli.SimulationConfig.Simulation && isExcluded {
|
||||||
|
if err := removeFromExclusion(scenario); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Printf("simulation enabled for '%s'", scenario)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := addToExclusion(scenario); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Printf("simulation mode for '%s' enabled", scenario)
|
||||||
|
}
|
||||||
|
if err := dumpSimulationFile(); err != nil {
|
||||||
|
log.Fatalf("simulation enable: %s", err)
|
||||||
|
}
|
||||||
|
} else if forceGlobalSimulation {
|
||||||
|
if err := enableGlobalSimulation(); err != nil {
|
||||||
|
log.Fatalf("unable to enable global simulation mode : %s", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printHelp(cmd)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmdSimulationEnable.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Enable global simulation (reverse mode)")
|
||||||
|
|
||||||
|
return cmdSimulationEnable
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSimulationDisableCmd() *cobra.Command {
|
||||||
|
var forceGlobalSimulation bool
|
||||||
|
|
||||||
|
var cmdSimulationDisable = &cobra.Command{
|
||||||
|
Use: "disable [scenario]",
|
||||||
|
Short: "Disable the simulation mode. Disable only specified scenarios",
|
||||||
|
Example: `cscli simulation disable`,
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if len(args) > 0 {
|
||||||
|
for _, scenario := range args {
|
||||||
|
isExcluded := slices.Contains(csConfig.Cscli.SimulationConfig.Exclusions, scenario)
|
||||||
|
if !*csConfig.Cscli.SimulationConfig.Simulation && !isExcluded {
|
||||||
|
log.Warningf("%s isn't in simulation mode", scenario)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if !*csConfig.Cscli.SimulationConfig.Simulation && isExcluded {
|
||||||
|
if err := removeFromExclusion(scenario); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Printf("simulation mode for '%s' disabled", scenario)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if isExcluded {
|
||||||
|
log.Warningf("simulation mode is enabled but is already disable for '%s'", scenario)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if err := addToExclusion(scenario); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Printf("simulation mode for '%s' disabled", scenario)
|
||||||
|
}
|
||||||
|
if err := dumpSimulationFile(); err != nil {
|
||||||
|
log.Fatalf("simulation disable: %s", err)
|
||||||
|
}
|
||||||
|
} else if forceGlobalSimulation {
|
||||||
|
if err := disableGlobalSimulation(); err != nil {
|
||||||
|
log.Fatalf("unable to disable global simulation mode : %s", err)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
printHelp(cmd)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
cmdSimulationDisable.Flags().BoolVarP(&forceGlobalSimulation, "global", "g", false, "Disable global simulation (reverse mode)")
|
||||||
|
|
||||||
|
return cmdSimulationDisable
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSimulationStatusCmd() *cobra.Command {
|
||||||
|
var cmdSimulationStatus = &cobra.Command{
|
||||||
|
Use: "status",
|
||||||
|
Short: "Show simulation mode status",
|
||||||
|
Example: `cscli simulation status`,
|
||||||
|
DisableAutoGenTag: true,
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if err := simulationStatus(); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
return cmdSimulationStatus
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"archive/zip"
|
"archive/zip"
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -13,14 +12,12 @@ import (
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/blackfireio/osinfo"
|
"github.com/blackfireio/osinfo"
|
||||||
"github.com/go-openapi/strfmt"
|
"github.com/go-openapi/strfmt"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/trace"
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/version"
|
"github.com/crowdsecurity/go-cs-lib/version"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
||||||
|
@ -50,7 +47,6 @@ const (
|
||||||
SUPPORT_CAPI_STATUS_PATH = "capi_status.txt"
|
SUPPORT_CAPI_STATUS_PATH = "capi_status.txt"
|
||||||
SUPPORT_ACQUISITION_CONFIG_BASE_PATH = "config/acquis/"
|
SUPPORT_ACQUISITION_CONFIG_BASE_PATH = "config/acquis/"
|
||||||
SUPPORT_CROWDSEC_PROFILE_PATH = "config/profiles.yaml"
|
SUPPORT_CROWDSEC_PROFILE_PATH = "config/profiles.yaml"
|
||||||
SUPPORT_CRASH_PATH = "crash/"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// from https://github.com/acarl005/stripansi
|
// from https://github.com/acarl005/stripansi
|
||||||
|
@ -66,38 +62,32 @@ func collectMetrics() ([]byte, []byte, error) {
|
||||||
|
|
||||||
if csConfig.Cscli.PrometheusUrl == "" {
|
if csConfig.Cscli.PrometheusUrl == "" {
|
||||||
log.Warn("No Prometheus URL configured, metrics will not be collected")
|
log.Warn("No Prometheus URL configured, metrics will not be collected")
|
||||||
return nil, nil, errors.New("prometheus_uri is not set")
|
return nil, nil, fmt.Errorf("prometheus_uri is not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
humanMetrics := bytes.NewBuffer(nil)
|
humanMetrics := bytes.NewBuffer(nil)
|
||||||
|
err := FormatPrometheusMetrics(humanMetrics, csConfig.Cscli.PrometheusUrl, "human")
|
||||||
|
|
||||||
ms := NewMetricStore()
|
if err != nil {
|
||||||
|
return nil, nil, fmt.Errorf("could not fetch promtheus metrics: %s", err)
|
||||||
if err := ms.Fetch(csConfig.Cscli.PrometheusUrl); err != nil {
|
|
||||||
return nil, nil, fmt.Errorf("could not fetch prometheus metrics: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ms.Format(humanMetrics, nil, "human", false); err != nil {
|
|
||||||
return nil, nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
req, err := http.NewRequest(http.MethodGet, csConfig.Cscli.PrometheusUrl, nil)
|
req, err := http.NewRequest(http.MethodGet, csConfig.Cscli.PrometheusUrl, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("could not create requests to prometheus endpoint: %w", err)
|
return nil, nil, fmt.Errorf("could not create requests to prometheus endpoint: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
|
|
||||||
resp, err := client.Do(req)
|
resp, err := client.Do(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("could not get metrics from prometheus endpoint: %w", err)
|
return nil, nil, fmt.Errorf("could not get metrics from prometheus endpoint: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
|
|
||||||
body, err := io.ReadAll(resp.Body)
|
body, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, fmt.Errorf("could not read metrics from prometheus endpoint: %w", err)
|
return nil, nil, fmt.Errorf("could not read metrics from prometheus endpoint: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return humanMetrics.Bytes(), body, nil
|
return humanMetrics.Bytes(), body, nil
|
||||||
|
@ -110,33 +100,31 @@ func collectVersion() []byte {
|
||||||
|
|
||||||
func collectFeatures() []byte {
|
func collectFeatures() []byte {
|
||||||
log.Info("Collecting feature flags")
|
log.Info("Collecting feature flags")
|
||||||
|
|
||||||
enabledFeatures := fflag.Crowdsec.GetEnabledFeatures()
|
enabledFeatures := fflag.Crowdsec.GetEnabledFeatures()
|
||||||
|
|
||||||
w := bytes.NewBuffer(nil)
|
w := bytes.NewBuffer(nil)
|
||||||
for _, k := range enabledFeatures {
|
for _, k := range enabledFeatures {
|
||||||
fmt.Fprintf(w, "%s\n", k)
|
fmt.Fprintf(w, "%s\n", k)
|
||||||
}
|
}
|
||||||
|
|
||||||
return w.Bytes()
|
return w.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectOSInfo() ([]byte, error) {
|
func collectOSInfo() ([]byte, error) {
|
||||||
log.Info("Collecting OS info")
|
log.Info("Collecting OS info")
|
||||||
|
|
||||||
info, err := osinfo.GetOSInfo()
|
info, err := osinfo.GetOSInfo()
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
w := bytes.NewBuffer(nil)
|
w := bytes.NewBuffer(nil)
|
||||||
fmt.Fprintf(w, "Architecture: %s\n", info.Architecture)
|
w.WriteString(fmt.Sprintf("Architecture: %s\n", info.Architecture))
|
||||||
fmt.Fprintf(w, "Family: %s\n", info.Family)
|
w.WriteString(fmt.Sprintf("Family: %s\n", info.Family))
|
||||||
fmt.Fprintf(w, "ID: %s\n", info.ID)
|
w.WriteString(fmt.Sprintf("ID: %s\n", info.ID))
|
||||||
fmt.Fprintf(w, "Name: %s\n", info.Name)
|
w.WriteString(fmt.Sprintf("Name: %s\n", info.Name))
|
||||||
fmt.Fprintf(w, "Codename: %s\n", info.Codename)
|
w.WriteString(fmt.Sprintf("Codename: %s\n", info.Codename))
|
||||||
fmt.Fprintf(w, "Version: %s\n", info.Version)
|
w.WriteString(fmt.Sprintf("Version: %s\n", info.Version))
|
||||||
fmt.Fprintf(w, "Build: %s\n", info.Build)
|
w.WriteString(fmt.Sprintf("Build: %s\n", info.Build))
|
||||||
|
|
||||||
return w.Bytes(), nil
|
return w.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
@ -145,7 +133,6 @@ func collectHubItems(hub *cwhub.Hub, itemType string) []byte {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
out := bytes.NewBuffer(nil)
|
out := bytes.NewBuffer(nil)
|
||||||
|
|
||||||
log.Infof("Collecting %s list", itemType)
|
log.Infof("Collecting %s list", itemType)
|
||||||
|
|
||||||
items := make(map[string][]*cwhub.Item)
|
items := make(map[string][]*cwhub.Item)
|
||||||
|
@ -157,33 +144,24 @@ func collectHubItems(hub *cwhub.Hub, itemType string) []byte {
|
||||||
if err := listItems(out, []string{itemType}, items, false); err != nil {
|
if err := listItems(out, []string{itemType}, items, false); err != nil {
|
||||||
log.Warnf("could not collect %s list: %s", itemType, err)
|
log.Warnf("could not collect %s list: %s", itemType, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return out.Bytes()
|
return out.Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectBouncers(dbClient *database.Client) ([]byte, error) {
|
func collectBouncers(dbClient *database.Client) ([]byte, error) {
|
||||||
out := bytes.NewBuffer(nil)
|
out := bytes.NewBuffer(nil)
|
||||||
|
err := getBouncers(out, dbClient)
|
||||||
bouncers, err := dbClient.ListBouncers()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to list bouncers: %w", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
getBouncersTable(out, bouncers)
|
|
||||||
|
|
||||||
return out.Bytes(), nil
|
return out.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectAgents(dbClient *database.Client) ([]byte, error) {
|
func collectAgents(dbClient *database.Client) ([]byte, error) {
|
||||||
out := bytes.NewBuffer(nil)
|
out := bytes.NewBuffer(nil)
|
||||||
|
err := getAgents(out, dbClient)
|
||||||
machines, err := dbClient.ListMachines()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to list machines: %w", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
getAgentsTable(out, machines)
|
|
||||||
|
|
||||||
return out.Bytes(), nil
|
return out.Bytes(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -191,15 +169,13 @@ func collectAPIStatus(login string, password string, endpoint string, prefix str
|
||||||
if csConfig.API.Client == nil || csConfig.API.Client.Credentials == nil {
|
if csConfig.API.Client == nil || csConfig.API.Client.Credentials == nil {
|
||||||
return []byte("No agent credentials found, are we LAPI ?")
|
return []byte("No agent credentials found, are we LAPI ?")
|
||||||
}
|
}
|
||||||
|
|
||||||
pwd := strfmt.Password(password)
|
pwd := strfmt.Password(password)
|
||||||
|
|
||||||
apiurl, err := url.Parse(endpoint)
|
apiurl, err := url.Parse(endpoint)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte(fmt.Sprintf("cannot parse API URL: %s", err))
|
return []byte(fmt.Sprintf("cannot parse API URL: %s", err))
|
||||||
}
|
}
|
||||||
|
scenarios, err := hub.GetInstalledItemNames(cwhub.SCENARIOS)
|
||||||
scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte(fmt.Sprintf("could not collect scenarios: %s", err))
|
return []byte(fmt.Sprintf("could not collect scenarios: %s", err))
|
||||||
}
|
}
|
||||||
|
@ -211,7 +187,6 @@ func collectAPIStatus(login string, password string, endpoint string, prefix str
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte(fmt.Sprintf("could not init client: %s", err))
|
return []byte(fmt.Sprintf("could not init client: %s", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
t := models.WatcherAuthRequest{
|
t := models.WatcherAuthRequest{
|
||||||
MachineID: &login,
|
MachineID: &login,
|
||||||
Password: &pwd,
|
Password: &pwd,
|
||||||
|
@ -228,7 +203,6 @@ func collectAPIStatus(login string, password string, endpoint string, prefix str
|
||||||
|
|
||||||
func collectCrowdsecConfig() []byte {
|
func collectCrowdsecConfig() []byte {
|
||||||
log.Info("Collecting crowdsec config")
|
log.Info("Collecting crowdsec config")
|
||||||
|
|
||||||
config, err := os.ReadFile(*csConfig.FilePath)
|
config, err := os.ReadFile(*csConfig.FilePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte(fmt.Sprintf("could not read config file: %s", err))
|
return []byte(fmt.Sprintf("could not read config file: %s", err))
|
||||||
|
@ -241,18 +215,15 @@ func collectCrowdsecConfig() []byte {
|
||||||
|
|
||||||
func collectCrowdsecProfile() []byte {
|
func collectCrowdsecProfile() []byte {
|
||||||
log.Info("Collecting crowdsec profile")
|
log.Info("Collecting crowdsec profile")
|
||||||
|
|
||||||
config, err := os.ReadFile(csConfig.API.Server.ProfilesPath)
|
config, err := os.ReadFile(csConfig.API.Server.ProfilesPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte(fmt.Sprintf("could not read profile file: %s", err))
|
return []byte(fmt.Sprintf("could not read profile file: %s", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectAcquisitionConfig() map[string][]byte {
|
func collectAcquisitionConfig() map[string][]byte {
|
||||||
log.Info("Collecting acquisition config")
|
log.Info("Collecting acquisition config")
|
||||||
|
|
||||||
ret := make(map[string][]byte)
|
ret := make(map[string][]byte)
|
||||||
|
|
||||||
for _, filename := range csConfig.Crowdsec.AcquisitionFiles {
|
for _, filename := range csConfig.Crowdsec.AcquisitionFiles {
|
||||||
|
@ -267,19 +238,8 @@ func collectAcquisitionConfig() map[string][]byte {
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func collectCrash() ([]string, error) {
|
func NewSupportCmd() *cobra.Command {
|
||||||
log.Info("Collecting crash dumps")
|
var cmdSupport = &cobra.Command{
|
||||||
return trace.List()
|
|
||||||
}
|
|
||||||
|
|
||||||
type cliSupport struct{}
|
|
||||||
|
|
||||||
func NewCLISupport() *cliSupport {
|
|
||||||
return &cliSupport{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli cliSupport) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "support [action]",
|
Use: "support [action]",
|
||||||
Short: "Provide commands to help during support",
|
Short: "Provide commands to help during support",
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
|
@ -289,15 +249,9 @@ func (cli cliSupport) NewCommand() *cobra.Command {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
cmd.AddCommand(cli.NewDumpCmd())
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli cliSupport) NewDumpCmd() *cobra.Command {
|
|
||||||
var outFile string
|
var outFile string
|
||||||
|
|
||||||
cmd := &cobra.Command{
|
cmdDump := &cobra.Command{
|
||||||
Use: "dump",
|
Use: "dump",
|
||||||
Short: "Dump all your configuration to a zip file for easier support",
|
Short: "Dump all your configuration to a zip file for easier support",
|
||||||
Long: `Dump the following informations:
|
Long: `Dump the following informations:
|
||||||
|
@ -319,7 +273,7 @@ cscli support dump -f /tmp/crowdsec-support.zip
|
||||||
`,
|
`,
|
||||||
Args: cobra.NoArgs,
|
Args: cobra.NoArgs,
|
||||||
DisableAutoGenTag: true,
|
DisableAutoGenTag: true,
|
||||||
RunE: func(_ *cobra.Command, _ []string) error {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
var err error
|
var err error
|
||||||
var skipHub, skipDB, skipCAPI, skipLAPI, skipAgent bool
|
var skipHub, skipDB, skipCAPI, skipLAPI, skipAgent bool
|
||||||
infos := map[string][]byte{
|
infos := map[string][]byte{
|
||||||
|
@ -339,18 +293,18 @@ cscli support dump -f /tmp/crowdsec-support.zip
|
||||||
infos[SUPPORT_AGENTS_PATH] = []byte(err.Error())
|
infos[SUPPORT_AGENTS_PATH] = []byte(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = csConfig.LoadAPIServer(true); err != nil {
|
if err := csConfig.LoadAPIServer(); err != nil {
|
||||||
log.Warnf("could not load LAPI, skipping CAPI check")
|
log.Warnf("could not load LAPI, skipping CAPI check")
|
||||||
skipLAPI = true
|
skipLAPI = true
|
||||||
infos[SUPPORT_CAPI_STATUS_PATH] = []byte(err.Error())
|
infos[SUPPORT_CAPI_STATUS_PATH] = []byte(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = csConfig.LoadCrowdsec(); err != nil {
|
if err := csConfig.LoadCrowdsec(); err != nil {
|
||||||
log.Warnf("could not load agent config, skipping crowdsec config check")
|
log.Warnf("could not load agent config, skipping crowdsec config check")
|
||||||
skipAgent = true
|
skipAgent = true
|
||||||
}
|
}
|
||||||
|
|
||||||
hub, err := require.Hub(csConfig, nil, nil)
|
hub, err := require.Hub(csConfig, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Warn("Could not init hub, running on LAPI ? Hub related information will not be collected")
|
log.Warn("Could not init hub, running on LAPI ? Hub related information will not be collected")
|
||||||
skipHub = true
|
skipHub = true
|
||||||
|
@ -431,6 +385,7 @@ cscli support dump -f /tmp/crowdsec-support.zip
|
||||||
}
|
}
|
||||||
|
|
||||||
if !skipAgent {
|
if !skipAgent {
|
||||||
|
|
||||||
acquis := collectAcquisitionConfig()
|
acquis := collectAcquisitionConfig()
|
||||||
|
|
||||||
for filename, content := range acquis {
|
for filename, content := range acquis {
|
||||||
|
@ -439,31 +394,11 @@ cscli support dump -f /tmp/crowdsec-support.zip
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
crash, err := collectCrash()
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("could not collect crash dumps: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, filename := range crash {
|
|
||||||
content, err := os.ReadFile(filename)
|
|
||||||
if err != nil {
|
|
||||||
log.Errorf("could not read crash dump %s: %s", filename, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
infos[SUPPORT_CRASH_PATH+filepath.Base(filename)] = content
|
|
||||||
}
|
|
||||||
|
|
||||||
w := bytes.NewBuffer(nil)
|
w := bytes.NewBuffer(nil)
|
||||||
zipWriter := zip.NewWriter(w)
|
zipWriter := zip.NewWriter(w)
|
||||||
|
|
||||||
for filename, data := range infos {
|
for filename, data := range infos {
|
||||||
header := &zip.FileHeader{
|
fw, err := zipWriter.Create(filename)
|
||||||
Name: filename,
|
|
||||||
Method: zip.Deflate,
|
|
||||||
// TODO: retain mtime where possible (esp. trace)
|
|
||||||
Modified: time.Now(),
|
|
||||||
}
|
|
||||||
fw, err := zipWriter.CreateHeader(header)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Could not add zip entry for %s: %s", filename, err)
|
log.Errorf("Could not add zip entry for %s: %s", filename, err)
|
||||||
continue
|
continue
|
||||||
|
@ -473,23 +408,19 @@ cscli support dump -f /tmp/crowdsec-support.zip
|
||||||
|
|
||||||
err = zipWriter.Close()
|
err = zipWriter.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not finalize zip file: %s", err)
|
log.Fatalf("could not finalize zip file: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if outFile == "-" {
|
err = os.WriteFile(outFile, w.Bytes(), 0600)
|
||||||
_, err = os.Stdout.Write(w.Bytes())
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = os.WriteFile(outFile, w.Bytes(), 0o600)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("could not write zip file to %s: %s", outFile, err)
|
log.Fatalf("could not write zip file to %s: %s", outFile, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("Written zip file to %s", outFile)
|
log.Infof("Written zip file to %s", outFile)
|
||||||
return nil
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
cmdDump.Flags().StringVarP(&outFile, "outFile", "f", "", "File to dump the information to")
|
||||||
|
cmdSupport.AddCommand(cmdDump)
|
||||||
|
|
||||||
cmd.Flags().StringVarP(&outFile, "outFile", "f", "", "File to dump the information to")
|
return cmdSupport
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/database"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -25,7 +26,6 @@ func manageCliDecisionAlerts(ip *string, ipRange *string, scope *string, value *
|
||||||
return fmt.Errorf("%s isn't a valid range", *ipRange)
|
return fmt.Errorf("%s isn't a valid range", *ipRange)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if *ip != "" {
|
if *ip != "" {
|
||||||
ipRepr := net.ParseIP(*ip)
|
ipRepr := net.ParseIP(*ip)
|
||||||
if ipRepr == nil {
|
if ipRepr == nil {
|
||||||
|
@ -44,10 +44,20 @@ func manageCliDecisionAlerts(ip *string, ipRange *string, scope *string, value *
|
||||||
case "as":
|
case "as":
|
||||||
*scope = types.AS
|
*scope = types.AS
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getDBClient() (*database.Client, error) {
|
||||||
|
if err := csConfig.LoadAPIServer(); err != nil || csConfig.DisableAPI {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ret, err := database.NewClient(csConfig.DbConfig)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return ret, nil
|
||||||
|
}
|
||||||
|
|
||||||
func removeFromSlice(val string, slice []string) []string {
|
func removeFromSlice(val string, slice []string) []string {
|
||||||
var i int
|
var i int
|
||||||
var value string
|
var value string
|
||||||
|
|
|
@ -6,9 +6,9 @@ import (
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"github.com/aquasecurity/table"
|
"github.com/aquasecurity/table"
|
||||||
|
"github.com/enescakir/emoji"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/emoji"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func listHubItemTable(out io.Writer, title string, items []*cwhub.Item) {
|
func listHubItemTable(out io.Writer, title string, items []*cwhub.Item) {
|
||||||
|
@ -21,29 +21,14 @@ func listHubItemTable(out io.Writer, title string, items []*cwhub.Item) {
|
||||||
status := fmt.Sprintf("%v %s", item.State.Emoji(), item.State.Text())
|
status := fmt.Sprintf("%v %s", item.State.Emoji(), item.State.Text())
|
||||||
t.AddRow(item.Name, status, item.State.LocalVersion, item.State.LocalPath)
|
t.AddRow(item.Name, status, item.State.LocalVersion, item.State.LocalPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
renderTableTitle(out, title)
|
renderTableTitle(out, title)
|
||||||
t.Render()
|
t.Render()
|
||||||
}
|
}
|
||||||
|
|
||||||
func appsecMetricsTable(out io.Writer, itemName string, metrics map[string]int) {
|
|
||||||
t := newTable(out)
|
|
||||||
t.SetHeaders("Inband Hits", "Outband Hits")
|
|
||||||
|
|
||||||
t.AddRow(
|
|
||||||
strconv.Itoa(metrics["inband_hits"]),
|
|
||||||
strconv.Itoa(metrics["outband_hits"]),
|
|
||||||
)
|
|
||||||
|
|
||||||
renderTableTitle(out, fmt.Sprintf("\n - (AppSec Rule) %s:", itemName))
|
|
||||||
t.Render()
|
|
||||||
}
|
|
||||||
|
|
||||||
func scenarioMetricsTable(out io.Writer, itemName string, metrics map[string]int) {
|
func scenarioMetricsTable(out io.Writer, itemName string, metrics map[string]int) {
|
||||||
if metrics["instantiation"] == 0 {
|
if metrics["instantiation"] == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
t := newTable(out)
|
t := newTable(out)
|
||||||
t.SetHeaders("Current Count", "Overflows", "Instantiated", "Poured", "Expired")
|
t.SetHeaders("Current Count", "Overflows", "Instantiated", "Poured", "Expired")
|
||||||
|
|
||||||
|
@ -74,7 +59,6 @@ func parserMetricsTable(out io.Writer, itemName string, metrics map[string]map[s
|
||||||
strconv.Itoa(stats["parsed"]),
|
strconv.Itoa(stats["parsed"]),
|
||||||
strconv.Itoa(stats["unparsed"]),
|
strconv.Itoa(stats["unparsed"]),
|
||||||
)
|
)
|
||||||
|
|
||||||
showTable = true
|
showTable = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwversion"
|
|
||||||
)
|
|
||||||
|
|
||||||
type cliVersion struct{}
|
|
||||||
|
|
||||||
func NewCLIVersion() *cliVersion {
|
|
||||||
return &cliVersion{}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cli cliVersion) NewCommand() *cobra.Command {
|
|
||||||
cmd := &cobra.Command{
|
|
||||||
Use: "version",
|
|
||||||
Short: "Display version",
|
|
||||||
Args: cobra.ExactArgs(0),
|
|
||||||
DisableAutoGenTag: true,
|
|
||||||
Run: func(_ *cobra.Command, _ []string) {
|
|
||||||
cwversion.Show()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
return cmd
|
|
||||||
}
|
|
|
@ -1,11 +1,11 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/trace"
|
"github.com/crowdsecurity/go-cs-lib/trace"
|
||||||
|
@ -56,8 +56,7 @@ func initAPIServer(cConfig *csconfig.Config) (*apiserver.APIServer, error) {
|
||||||
return apiServer, nil
|
return apiServer, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveAPIServer(apiServer *apiserver.APIServer) {
|
func serveAPIServer(apiServer *apiserver.APIServer, apiReady chan bool) {
|
||||||
apiReady := make(chan bool, 1)
|
|
||||||
apiTomb.Go(func() error {
|
apiTomb.Go(func() error {
|
||||||
defer trace.CatchPanic("crowdsec/serveAPIServer")
|
defer trace.CatchPanic("crowdsec/serveAPIServer")
|
||||||
go func() {
|
go func() {
|
||||||
|
@ -81,7 +80,6 @@ func serveAPIServer(apiServer *apiserver.APIServer) {
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
<-apiReady
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func hasPlugins(profiles []*csconfig.ProfileCfg) bool {
|
func hasPlugins(profiles []*csconfig.ProfileCfg) bool {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -9,14 +8,13 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/trace"
|
"github.com/crowdsecurity/go-cs-lib/trace"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/acquisition"
|
"github.com/crowdsecurity/crowdsec/pkg/acquisition"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/alertcontext"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/appsec"
|
"github.com/crowdsecurity/crowdsec/pkg/appsec"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/alertcontext"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
leaky "github.com/crowdsecurity/crowdsec/pkg/leakybucket"
|
leaky "github.com/crowdsecurity/crowdsec/pkg/leakybucket"
|
||||||
|
@ -24,160 +22,130 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
// initCrowdsec prepares the log processor service
|
func initCrowdsec(cConfig *csconfig.Config, hub *cwhub.Hub) (*parser.Parsers, error) {
|
||||||
func initCrowdsec(cConfig *csconfig.Config, hub *cwhub.Hub) (*parser.Parsers, []acquisition.DataSource, error) {
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if err = alertcontext.LoadConsoleContext(cConfig, hub); err != nil {
|
if err = alertcontext.LoadConsoleContext(cConfig, hub); err != nil {
|
||||||
return nil, nil, fmt.Errorf("while loading context: %w", err)
|
return nil, fmt.Errorf("while loading context: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start loading configs
|
// Start loading configs
|
||||||
csParsers := parser.NewParsers(hub)
|
csParsers := parser.NewParsers(hub)
|
||||||
if csParsers, err = parser.LoadParsers(cConfig, csParsers); err != nil {
|
if csParsers, err = parser.LoadParsers(cConfig, csParsers); err != nil {
|
||||||
return nil, nil, fmt.Errorf("while loading parsers: %w", err)
|
return nil, fmt.Errorf("while loading parsers: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := LoadBuckets(cConfig, hub); err != nil {
|
if err := LoadBuckets(cConfig, hub); err != nil {
|
||||||
return nil, nil, fmt.Errorf("while loading scenarios: %w", err)
|
return nil, fmt.Errorf("while loading scenarios: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := appsec.LoadAppsecRules(hub); err != nil {
|
if err := appsec.LoadAppsecRules(hub); err != nil {
|
||||||
return nil, nil, fmt.Errorf("while loading appsec rules: %w", err)
|
return nil, fmt.Errorf("while loading appsec rules: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
datasources, err := LoadAcquisition(cConfig)
|
if err := LoadAcquisition(cConfig); err != nil {
|
||||||
if err != nil {
|
return nil, fmt.Errorf("while loading acquisition config: %w", err)
|
||||||
return nil, nil, fmt.Errorf("while loading acquisition config: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return csParsers, datasources, nil
|
return csParsers, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// runCrowdsec starts the log processor service
|
func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.Hub) error {
|
||||||
func runCrowdsec(cConfig *csconfig.Config, parsers *parser.Parsers, hub *cwhub.Hub, datasources []acquisition.DataSource) error {
|
|
||||||
inputEventChan = make(chan types.Event)
|
inputEventChan = make(chan types.Event)
|
||||||
inputLineChan = make(chan types.Event)
|
inputLineChan = make(chan types.Event)
|
||||||
|
|
||||||
//start go-routines for parsing, buckets pour and outputs.
|
//start go-routines for parsing, buckets pour and outputs.
|
||||||
parserWg := &sync.WaitGroup{}
|
parserWg := &sync.WaitGroup{}
|
||||||
|
|
||||||
parsersTomb.Go(func() error {
|
parsersTomb.Go(func() error {
|
||||||
parserWg.Add(1)
|
parserWg.Add(1)
|
||||||
|
|
||||||
for i := 0; i < cConfig.Crowdsec.ParserRoutinesCount; i++ {
|
for i := 0; i < cConfig.Crowdsec.ParserRoutinesCount; i++ {
|
||||||
parsersTomb.Go(func() error {
|
parsersTomb.Go(func() error {
|
||||||
defer trace.CatchPanic("crowdsec/runParse")
|
defer trace.CatchPanic("crowdsec/runParse")
|
||||||
|
if err := runParse(inputLineChan, inputEventChan, *parsers.Ctx, parsers.Nodes); err != nil { //this error will never happen as parser.Parse is not able to return errors
|
||||||
if err := runParse(inputLineChan, inputEventChan, *parsers.Ctx, parsers.Nodes); err != nil {
|
|
||||||
// this error will never happen as parser.Parse is not able to return errors
|
|
||||||
log.Fatalf("starting parse error : %s", err)
|
log.Fatalf("starting parse error : %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
parserWg.Done()
|
parserWg.Done()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
parserWg.Wait()
|
parserWg.Wait()
|
||||||
|
|
||||||
bucketWg := &sync.WaitGroup{}
|
bucketWg := &sync.WaitGroup{}
|
||||||
|
|
||||||
bucketsTomb.Go(func() error {
|
bucketsTomb.Go(func() error {
|
||||||
bucketWg.Add(1)
|
bucketWg.Add(1)
|
||||||
/*restore previous state as well if present*/
|
/*restore previous state as well if present*/
|
||||||
if cConfig.Crowdsec.BucketStateFile != "" {
|
if cConfig.Crowdsec.BucketStateFile != "" {
|
||||||
log.Warningf("Restoring buckets state from %s", cConfig.Crowdsec.BucketStateFile)
|
log.Warningf("Restoring buckets state from %s", cConfig.Crowdsec.BucketStateFile)
|
||||||
|
|
||||||
if err := leaky.LoadBucketsState(cConfig.Crowdsec.BucketStateFile, buckets, holders); err != nil {
|
if err := leaky.LoadBucketsState(cConfig.Crowdsec.BucketStateFile, buckets, holders); err != nil {
|
||||||
return fmt.Errorf("unable to restore buckets: %w", err)
|
return fmt.Errorf("unable to restore buckets : %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < cConfig.Crowdsec.BucketsRoutinesCount; i++ {
|
for i := 0; i < cConfig.Crowdsec.BucketsRoutinesCount; i++ {
|
||||||
bucketsTomb.Go(func() error {
|
bucketsTomb.Go(func() error {
|
||||||
defer trace.CatchPanic("crowdsec/runPour")
|
defer trace.CatchPanic("crowdsec/runPour")
|
||||||
|
|
||||||
if err := runPour(inputEventChan, holders, buckets, cConfig); err != nil {
|
if err := runPour(inputEventChan, holders, buckets, cConfig); err != nil {
|
||||||
log.Fatalf("starting pour error : %s", err)
|
log.Fatalf("starting pour error : %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
bucketWg.Done()
|
bucketWg.Done()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
bucketWg.Wait()
|
bucketWg.Wait()
|
||||||
|
|
||||||
apiClient, err := AuthenticatedLAPIClient(*cConfig.API.Client.Credentials, hub)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Debugf("Starting HeartBeat service")
|
|
||||||
apiClient.HeartBeat.StartHeartBeat(context.Background(), &outputsTomb)
|
|
||||||
|
|
||||||
outputWg := &sync.WaitGroup{}
|
outputWg := &sync.WaitGroup{}
|
||||||
|
|
||||||
outputsTomb.Go(func() error {
|
outputsTomb.Go(func() error {
|
||||||
outputWg.Add(1)
|
outputWg.Add(1)
|
||||||
|
|
||||||
for i := 0; i < cConfig.Crowdsec.OutputRoutinesCount; i++ {
|
for i := 0; i < cConfig.Crowdsec.OutputRoutinesCount; i++ {
|
||||||
outputsTomb.Go(func() error {
|
outputsTomb.Go(func() error {
|
||||||
defer trace.CatchPanic("crowdsec/runOutput")
|
defer trace.CatchPanic("crowdsec/runOutput")
|
||||||
|
if err := runOutput(inputEventChan, outputEventChan, buckets, *parsers.Povfwctx, parsers.Povfwnodes, *cConfig.API.Client.Credentials, hub); err != nil {
|
||||||
if err := runOutput(inputEventChan, outputEventChan, buckets, *parsers.Povfwctx, parsers.Povfwnodes, apiClient); err != nil {
|
|
||||||
log.Fatalf("starting outputs error : %s", err)
|
log.Fatalf("starting outputs error : %s", err)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
outputWg.Done()
|
outputWg.Done()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
outputWg.Wait()
|
outputWg.Wait()
|
||||||
|
|
||||||
if cConfig.Prometheus != nil && cConfig.Prometheus.Enabled {
|
if cConfig.Prometheus != nil && cConfig.Prometheus.Enabled {
|
||||||
aggregated := false
|
aggregated := false
|
||||||
if cConfig.Prometheus.Level == configuration.CFG_METRICS_AGGREGATE {
|
if cConfig.Prometheus.Level == "aggregated" {
|
||||||
aggregated = true
|
aggregated = true
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := acquisition.GetMetrics(dataSources, aggregated); err != nil {
|
if err := acquisition.GetMetrics(dataSources, aggregated); err != nil {
|
||||||
return fmt.Errorf("while fetching prometheus metrics for datasources: %w", err)
|
return fmt.Errorf("while fetching prometheus metrics for datasources: %w", err)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
log.Info("Starting processing data")
|
log.Info("Starting processing data")
|
||||||
|
|
||||||
if err := acquisition.StartAcquisition(dataSources, inputLineChan, &acquisTomb); err != nil {
|
if err := acquisition.StartAcquisition(dataSources, inputLineChan, &acquisTomb); err != nil {
|
||||||
return fmt.Errorf("starting acquisition error: %w", err)
|
log.Fatalf("starting acquisition error : %s", err)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// serveCrowdsec wraps the log processor service
|
func serveCrowdsec(parsers *parser.Parsers, cConfig *csconfig.Config, hub *cwhub.Hub, agentReady chan bool) {
|
||||||
func serveCrowdsec(parsers *parser.Parsers, cConfig *csconfig.Config, hub *cwhub.Hub, datasources []acquisition.DataSource, agentReady chan bool) {
|
|
||||||
crowdsecTomb.Go(func() error {
|
crowdsecTomb.Go(func() error {
|
||||||
defer trace.CatchPanic("crowdsec/serveCrowdsec")
|
defer trace.CatchPanic("crowdsec/serveCrowdsec")
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer trace.CatchPanic("crowdsec/runCrowdsec")
|
defer trace.CatchPanic("crowdsec/runCrowdsec")
|
||||||
// this logs every time, even at config reload
|
// this logs every time, even at config reload
|
||||||
log.Debugf("running agent after %s ms", time.Since(crowdsecT0))
|
log.Debugf("running agent after %s ms", time.Since(crowdsecT0))
|
||||||
agentReady <- true
|
agentReady <- true
|
||||||
|
if err := runCrowdsec(cConfig, parsers, hub); err != nil {
|
||||||
if err := runCrowdsec(cConfig, parsers, hub, datasources); err != nil {
|
|
||||||
log.Fatalf("unable to start crowdsec routines: %s", err)
|
log.Fatalf("unable to start crowdsec routines: %s", err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
@ -188,88 +156,74 @@ func serveCrowdsec(parsers *parser.Parsers, cConfig *csconfig.Config, hub *cwhub
|
||||||
*/
|
*/
|
||||||
waitOnTomb()
|
waitOnTomb()
|
||||||
log.Debugf("Shutting down crowdsec routines")
|
log.Debugf("Shutting down crowdsec routines")
|
||||||
|
|
||||||
if err := ShutdownCrowdsecRoutines(); err != nil {
|
if err := ShutdownCrowdsecRoutines(); err != nil {
|
||||||
log.Fatalf("unable to shutdown crowdsec routines: %s", err)
|
log.Fatalf("unable to shutdown crowdsec routines: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("everything is dead, return crowdsecTomb")
|
log.Debugf("everything is dead, return crowdsecTomb")
|
||||||
|
|
||||||
if dumpStates {
|
if dumpStates {
|
||||||
dumpParserState()
|
dumpParserState()
|
||||||
dumpOverflowState()
|
dumpOverflowState()
|
||||||
dumpBucketsPour()
|
dumpBucketsPour()
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func dumpBucketsPour() {
|
func dumpBucketsPour() {
|
||||||
fd, err := os.OpenFile(filepath.Join(parser.DumpFolder, "bucketpour-dump.yaml"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666)
|
fd, err := os.OpenFile(filepath.Join(parser.DumpFolder, "bucketpour-dump.yaml"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("open: %s", err)
|
log.Fatalf("open: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := yaml.Marshal(leaky.BucketPourCache)
|
out, err := yaml.Marshal(leaky.BucketPourCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("marshal: %s", err)
|
log.Fatalf("marshal: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := fd.Write(out)
|
b, err := fd.Write(out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("write: %s", err)
|
log.Fatalf("write: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Tracef("wrote %d bytes", b)
|
log.Tracef("wrote %d bytes", b)
|
||||||
|
|
||||||
if err := fd.Close(); err != nil {
|
if err := fd.Close(); err != nil {
|
||||||
log.Fatalf(" close: %s", err)
|
log.Fatalf(" close: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func dumpParserState() {
|
func dumpParserState() {
|
||||||
fd, err := os.OpenFile(filepath.Join(parser.DumpFolder, "parser-dump.yaml"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666)
|
|
||||||
|
fd, err := os.OpenFile(filepath.Join(parser.DumpFolder, "parser-dump.yaml"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("open: %s", err)
|
log.Fatalf("open: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := yaml.Marshal(parser.StageParseCache)
|
out, err := yaml.Marshal(parser.StageParseCache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("marshal: %s", err)
|
log.Fatalf("marshal: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := fd.Write(out)
|
b, err := fd.Write(out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("write: %s", err)
|
log.Fatalf("write: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Tracef("wrote %d bytes", b)
|
log.Tracef("wrote %d bytes", b)
|
||||||
|
|
||||||
if err := fd.Close(); err != nil {
|
if err := fd.Close(); err != nil {
|
||||||
log.Fatalf(" close: %s", err)
|
log.Fatalf(" close: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func dumpOverflowState() {
|
func dumpOverflowState() {
|
||||||
fd, err := os.OpenFile(filepath.Join(parser.DumpFolder, "bucket-dump.yaml"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o666)
|
|
||||||
|
fd, err := os.OpenFile(filepath.Join(parser.DumpFolder, "bucket-dump.yaml"), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0666)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("open: %s", err)
|
log.Fatalf("open: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
out, err := yaml.Marshal(bucketOverflows)
|
out, err := yaml.Marshal(bucketOverflows)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("marshal: %s", err)
|
log.Fatalf("marshal: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := fd.Write(out)
|
b, err := fd.Write(out)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("write: %s", err)
|
log.Fatalf("write: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Tracef("wrote %d bytes", b)
|
log.Tracef("wrote %d bytes", b)
|
||||||
|
|
||||||
if err := fd.Close(); err != nil {
|
if err := fd.Close(); err != nil {
|
||||||
log.Fatalf(" close: %s", err)
|
log.Fatalf(" close: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -281,7 +235,7 @@ func waitOnTomb() {
|
||||||
case <-acquisTomb.Dead():
|
case <-acquisTomb.Dead():
|
||||||
/*if it's acquisition dying it means that we were in "cat" mode.
|
/*if it's acquisition dying it means that we were in "cat" mode.
|
||||||
while shutting down, we need to give time for all buckets to process in flight data*/
|
while shutting down, we need to give time for all buckets to process in flight data*/
|
||||||
log.Info("Acquisition is finished, shutting down")
|
log.Warning("Acquisition is finished, shutting down")
|
||||||
/*
|
/*
|
||||||
While it might make sense to want to shut-down parser/buckets/etc. as soon as acquisition is finished,
|
While it might make sense to want to shut-down parser/buckets/etc. as soon as acquisition is finished,
|
||||||
we might have some pending buckets: buckets that overflowed, but whose LeakRoutine are still alive because they
|
we might have some pending buckets: buckets that overflowed, but whose LeakRoutine are still alive because they
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
)
|
|
||||||
|
|
||||||
// FatalHook is used to log fatal messages to stderr when the rest goes to a file
|
|
||||||
type FatalHook struct {
|
|
||||||
Writer io.Writer
|
|
||||||
LogLevels []log.Level
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hook *FatalHook) Fire(entry *log.Entry) error {
|
|
||||||
line, err := entry.String()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = hook.Writer.Write([]byte(line))
|
|
||||||
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func (hook *FatalHook) Levels() []log.Level {
|
|
||||||
return hook.LogLevels
|
|
||||||
}
|
|
|
@ -1,92 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"net/url"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-openapi/strfmt"
|
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/version"
|
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
|
||||||
)
|
|
||||||
|
|
||||||
func AuthenticatedLAPIClient(credentials csconfig.ApiCredentialsCfg, hub *cwhub.Hub) (*apiclient.ApiClient, error) {
|
|
||||||
scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("loading list of installed hub scenarios: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
appsecRules, err := hub.GetInstalledNamesByType(cwhub.APPSEC_RULES)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("loading list of installed hub appsec rules: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
installedScenariosAndAppsecRules := make([]string, 0, len(scenarios)+len(appsecRules))
|
|
||||||
installedScenariosAndAppsecRules = append(installedScenariosAndAppsecRules, scenarios...)
|
|
||||||
installedScenariosAndAppsecRules = append(installedScenariosAndAppsecRules, appsecRules...)
|
|
||||||
|
|
||||||
apiURL, err := url.Parse(credentials.URL)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("parsing api url ('%s'): %w", credentials.URL, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
papiURL, err := url.Parse(credentials.PapiURL)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("parsing polling api url ('%s'): %w", credentials.PapiURL, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
password := strfmt.Password(credentials.Password)
|
|
||||||
|
|
||||||
client, err := apiclient.NewClient(&apiclient.Config{
|
|
||||||
MachineID: credentials.Login,
|
|
||||||
Password: password,
|
|
||||||
Scenarios: installedScenariosAndAppsecRules,
|
|
||||||
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
|
||||||
URL: apiURL,
|
|
||||||
PapiURL: papiURL,
|
|
||||||
VersionPrefix: "v1",
|
|
||||||
UpdateScenario: func() ([]string, error) {
|
|
||||||
scenarios, err := hub.GetInstalledNamesByType(cwhub.SCENARIOS)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
appsecRules, err := hub.GetInstalledNamesByType(cwhub.APPSEC_RULES)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
ret := make([]string, 0, len(scenarios)+len(appsecRules))
|
|
||||||
ret = append(ret, scenarios...)
|
|
||||||
ret = append(ret, appsecRules...)
|
|
||||||
|
|
||||||
return ret, nil
|
|
||||||
},
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("new client api: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
authResp, _, err := client.Auth.AuthenticateWatcher(context.Background(), models.WatcherAuthRequest{
|
|
||||||
MachineID: &credentials.Login,
|
|
||||||
Password: &password,
|
|
||||||
Scenarios: installedScenariosAndAppsecRules,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("authenticate watcher (%s): %w", credentials.Login, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var expiration time.Time
|
|
||||||
if err := expiration.UnmarshalText([]byte(authResp.Expire)); err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to parse jwt expiration: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
client.GetClient().Transport.(*apiclient.JWTTransport).Token = authResp.Token
|
|
||||||
client.GetClient().Transport.(*apiclient.JWTTransport).Expiration = expiration
|
|
||||||
|
|
||||||
return client, nil
|
|
||||||
}
|
|
|
@ -1,22 +1,18 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/pprof"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/pkg/errors"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"gopkg.in/tomb.v2"
|
"gopkg.in/tomb.v2"
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/trace"
|
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/acquisition"
|
"github.com/crowdsecurity/crowdsec/pkg/acquisition"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csplugin"
|
"github.com/crowdsecurity/crowdsec/pkg/csplugin"
|
||||||
|
@ -75,11 +71,6 @@ type Flags struct {
|
||||||
DisableCAPI bool
|
DisableCAPI bool
|
||||||
Transform string
|
Transform string
|
||||||
OrderEvent bool
|
OrderEvent bool
|
||||||
CPUProfile string
|
|
||||||
}
|
|
||||||
|
|
||||||
func (f *Flags) haveTimeMachine() bool {
|
|
||||||
return f.OneShotDSN != ""
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type labelsMap map[string]string
|
type labelsMap map[string]string
|
||||||
|
@ -89,20 +80,18 @@ func LoadBuckets(cConfig *csconfig.Config, hub *cwhub.Hub) error {
|
||||||
err error
|
err error
|
||||||
files []string
|
files []string
|
||||||
)
|
)
|
||||||
|
|
||||||
for _, hubScenarioItem := range hub.GetItemMap(cwhub.SCENARIOS) {
|
for _, hubScenarioItem := range hub.GetItemMap(cwhub.SCENARIOS) {
|
||||||
if hubScenarioItem.State.Installed {
|
if hubScenarioItem.State.Installed {
|
||||||
files = append(files, hubScenarioItem.State.LocalPath)
|
files = append(files, hubScenarioItem.State.LocalPath)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
buckets = leakybucket.NewBuckets()
|
buckets = leakybucket.NewBuckets()
|
||||||
|
|
||||||
log.Infof("Loading %d scenario files", len(files))
|
log.Infof("Loading %d scenario files", len(files))
|
||||||
|
|
||||||
holders, outputEventChan, err = leakybucket.LoadBuckets(cConfig.Crowdsec, hub, files, &bucketsTomb, buckets, flags.OrderEvent)
|
holders, outputEventChan, err = leakybucket.LoadBuckets(cConfig.Crowdsec, hub, files, &bucketsTomb, buckets, flags.OrderEvent)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("scenario loading failed: %w", err)
|
return fmt.Errorf("scenario loading failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cConfig.Prometheus != nil && cConfig.Prometheus.Enabled {
|
if cConfig.Prometheus != nil && cConfig.Prometheus.Enabled {
|
||||||
|
@ -110,11 +99,10 @@ func LoadBuckets(cConfig *csconfig.Config, hub *cwhub.Hub) error {
|
||||||
holders[holderIndex].Profiling = true
|
holders[holderIndex].Profiling = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadAcquisition(cConfig *csconfig.Config) ([]acquisition.DataSource, error) {
|
func LoadAcquisition(cConfig *csconfig.Config) error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if flags.SingleFileType != "" && flags.OneShotDSN != "" {
|
if flags.SingleFileType != "" && flags.OneShotDSN != "" {
|
||||||
|
@ -123,20 +111,20 @@ func LoadAcquisition(cConfig *csconfig.Config) ([]acquisition.DataSource, error)
|
||||||
|
|
||||||
dataSources, err = acquisition.LoadAcquisitionFromDSN(flags.OneShotDSN, flags.Labels, flags.Transform)
|
dataSources, err = acquisition.LoadAcquisitionFromDSN(flags.OneShotDSN, flags.Labels, flags.Transform)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to configure datasource for %s: %w", flags.OneShotDSN, err)
|
return errors.Wrapf(err, "failed to configure datasource for %s", flags.OneShotDSN)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
dataSources, err = acquisition.LoadAcquisitionFromFile(cConfig.Crowdsec, cConfig.Prometheus)
|
dataSources, err = acquisition.LoadAcquisitionFromFile(cConfig.Crowdsec)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(dataSources) == 0 {
|
if len(dataSources) == 0 {
|
||||||
return nil, errors.New("no datasource enabled")
|
return fmt.Errorf("no datasource enabled")
|
||||||
}
|
}
|
||||||
|
|
||||||
return dataSources, nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -155,10 +143,8 @@ func (l labelsMap) Set(label string) error {
|
||||||
if len(split) != 2 {
|
if len(split) != 2 {
|
||||||
return fmt.Errorf("invalid format for label '%s', must be key:value", pair)
|
return fmt.Errorf("invalid format for label '%s', must be key:value", pair)
|
||||||
}
|
}
|
||||||
|
|
||||||
l[split[0]] = split[1]
|
l[split[0]] = split[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -182,13 +168,10 @@ func (f *Flags) Parse() {
|
||||||
flag.BoolVar(&f.DisableAPI, "no-api", false, "disable local API")
|
flag.BoolVar(&f.DisableAPI, "no-api", false, "disable local API")
|
||||||
flag.BoolVar(&f.DisableCAPI, "no-capi", false, "disable communication with Central API")
|
flag.BoolVar(&f.DisableCAPI, "no-capi", false, "disable communication with Central API")
|
||||||
flag.BoolVar(&f.OrderEvent, "order-event", false, "enforce event ordering with significant performance cost")
|
flag.BoolVar(&f.OrderEvent, "order-event", false, "enforce event ordering with significant performance cost")
|
||||||
|
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
flag.StringVar(&f.WinSvc, "winsvc", "", "Windows service Action: Install, Remove etc..")
|
flag.StringVar(&f.WinSvc, "winsvc", "", "Windows service Action: Install, Remove etc..")
|
||||||
}
|
}
|
||||||
|
|
||||||
flag.StringVar(&dumpFolder, "dump-data", "", "dump parsers/buckets raw outputs")
|
flag.StringVar(&dumpFolder, "dump-data", "", "dump parsers/buckets raw outputs")
|
||||||
flag.StringVar(&f.CPUProfile, "cpu-profile", "", "write cpu profile to file")
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,7 +205,6 @@ func newLogLevel(curLevelPtr *log.Level, f *Flags) *log.Level {
|
||||||
// avoid returning a new ptr to the same value
|
// avoid returning a new ptr to the same value
|
||||||
return curLevelPtr
|
return curLevelPtr
|
||||||
}
|
}
|
||||||
|
|
||||||
return &ret
|
return &ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -233,10 +215,6 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
|
||||||
return nil, fmt.Errorf("while loading configuration file: %w", err)
|
return nil, fmt.Errorf("while loading configuration file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := trace.Init(filepath.Join(cConfig.ConfigPaths.DataDir, "trace")); err != nil {
|
|
||||||
return nil, fmt.Errorf("while setting up trace directory: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cConfig.Common.LogLevel = newLogLevel(cConfig.Common.LogLevel, flags)
|
cConfig.Common.LogLevel = newLogLevel(cConfig.Common.LogLevel, flags)
|
||||||
|
|
||||||
if dumpFolder != "" {
|
if dumpFolder != "" {
|
||||||
|
@ -260,13 +238,6 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if cConfig.Common.LogMedia != "stdout" {
|
|
||||||
log.AddHook(&FatalHook{
|
|
||||||
Writer: os.Stderr,
|
|
||||||
LogLevels: []log.Level{log.FatalLevel, log.PanicLevel},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := csconfig.LoadFeatureFlagsFile(configFile, log.StandardLogger()); err != nil {
|
if err := csconfig.LoadFeatureFlagsFile(configFile, log.StandardLogger()); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -278,7 +249,7 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
|
||||||
}
|
}
|
||||||
|
|
||||||
if !cConfig.DisableAPI {
|
if !cConfig.DisableAPI {
|
||||||
if err := cConfig.LoadAPIServer(false); err != nil {
|
if err := cConfig.LoadAPIServer(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -288,7 +259,7 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
|
||||||
}
|
}
|
||||||
|
|
||||||
if cConfig.DisableAPI && cConfig.DisableAgent {
|
if cConfig.DisableAPI && cConfig.DisableAgent {
|
||||||
return nil, errors.New("you must run at least the API Server or crowdsec")
|
return nil, errors.New("You must run at least the API Server or crowdsec")
|
||||||
}
|
}
|
||||||
|
|
||||||
if flags.OneShotDSN != "" && flags.SingleFileType == "" {
|
if flags.OneShotDSN != "" && flags.SingleFileType == "" {
|
||||||
|
@ -311,7 +282,6 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
|
||||||
if cConfig.DisableAPI {
|
if cConfig.DisableAPI {
|
||||||
cConfig.Common.Daemonize = false
|
cConfig.Common.Daemonize = false
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infof("single file mode : log_media=%s daemonize=%t", cConfig.Common.LogMedia, cConfig.Common.Daemonize)
|
log.Infof("single file mode : log_media=%s daemonize=%t", cConfig.Common.LogMedia, cConfig.Common.Daemonize)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,7 +291,6 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
|
||||||
|
|
||||||
if cConfig.Common.Daemonize && runtime.GOOS == "windows" {
|
if cConfig.Common.Daemonize && runtime.GOOS == "windows" {
|
||||||
log.Debug("Daemonization is not supported on Windows, disabling")
|
log.Debug("Daemonization is not supported on Windows, disabling")
|
||||||
|
|
||||||
cConfig.Common.Daemonize = false
|
cConfig.Common.Daemonize = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,10 +308,6 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
|
||||||
var crowdsecT0 time.Time
|
var crowdsecT0 time.Time
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// The initial log level is INFO, even if the user provided an -error or -warning flag
|
|
||||||
// because we need feature flags before parsing cli flags
|
|
||||||
log.SetFormatter(&log.TextFormatter{TimestampFormat: time.RFC3339, FullTimestamp: true})
|
|
||||||
|
|
||||||
if err := fflag.RegisterAllFeatures(); err != nil {
|
if err := fflag.RegisterAllFeatures(); err != nil {
|
||||||
log.Fatalf("failed to register features: %s", err)
|
log.Fatalf("failed to register features: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -373,28 +338,9 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if flags.CPUProfile != "" {
|
|
||||||
f, err := os.Create(flags.CPUProfile)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("could not create CPU profile: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
log.Infof("CPU profile will be written to %s", flags.CPUProfile)
|
|
||||||
|
|
||||||
if err := pprof.StartCPUProfile(f); err != nil {
|
|
||||||
f.Close()
|
|
||||||
log.Fatalf("could not start CPU profile: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
defer f.Close()
|
|
||||||
defer pprof.StopCPUProfile()
|
|
||||||
}
|
|
||||||
|
|
||||||
err := StartRunSvc()
|
err := StartRunSvc()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
pprof.StopCPUProfile()
|
log.Fatal(err)
|
||||||
log.Fatal(err) //nolint:gocritic // Disable warning for the defer pprof.StopCPUProfile() call
|
|
||||||
}
|
}
|
||||||
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
|
@ -11,7 +12,6 @@ import (
|
||||||
"github.com/crowdsecurity/go-cs-lib/trace"
|
"github.com/crowdsecurity/go-cs-lib/trace"
|
||||||
"github.com/crowdsecurity/go-cs-lib/version"
|
"github.com/crowdsecurity/go-cs-lib/version"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
|
|
||||||
v1 "github.com/crowdsecurity/crowdsec/pkg/apiserver/controllers/v1"
|
v1 "github.com/crowdsecurity/crowdsec/pkg/apiserver/controllers/v1"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/cache"
|
"github.com/crowdsecurity/crowdsec/pkg/cache"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
|
@ -21,8 +21,7 @@ import (
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/parser"
|
"github.com/crowdsecurity/crowdsec/pkg/parser"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Prometheus
|
/*prometheus*/
|
||||||
|
|
||||||
var globalParserHits = prometheus.NewCounterVec(
|
var globalParserHits = prometheus.NewCounterVec(
|
||||||
prometheus.CounterOpts{
|
prometheus.CounterOpts{
|
||||||
Name: "cs_parser_hits_total",
|
Name: "cs_parser_hits_total",
|
||||||
|
@ -30,7 +29,6 @@ var globalParserHits = prometheus.NewCounterVec(
|
||||||
},
|
},
|
||||||
[]string{"source", "type"},
|
[]string{"source", "type"},
|
||||||
)
|
)
|
||||||
|
|
||||||
var globalParserHitsOk = prometheus.NewCounterVec(
|
var globalParserHitsOk = prometheus.NewCounterVec(
|
||||||
prometheus.CounterOpts{
|
prometheus.CounterOpts{
|
||||||
Name: "cs_parser_hits_ok_total",
|
Name: "cs_parser_hits_ok_total",
|
||||||
|
@ -38,7 +36,6 @@ var globalParserHitsOk = prometheus.NewCounterVec(
|
||||||
},
|
},
|
||||||
[]string{"source", "type"},
|
[]string{"source", "type"},
|
||||||
)
|
)
|
||||||
|
|
||||||
var globalParserHitsKo = prometheus.NewCounterVec(
|
var globalParserHitsKo = prometheus.NewCounterVec(
|
||||||
prometheus.CounterOpts{
|
prometheus.CounterOpts{
|
||||||
Name: "cs_parser_hits_ko_total",
|
Name: "cs_parser_hits_ko_total",
|
||||||
|
@ -105,8 +102,6 @@ var globalPourHistogram = prometheus.NewHistogramVec(
|
||||||
|
|
||||||
func computeDynamicMetrics(next http.Handler, dbClient *database.Client) http.HandlerFunc {
|
func computeDynamicMetrics(next http.Handler, dbClient *database.Client) http.HandlerFunc {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
// catch panics here because they are not handled by servePrometheus
|
|
||||||
defer trace.CatchPanic("crowdsec/computeDynamicMetrics")
|
|
||||||
//update cache metrics (stash)
|
//update cache metrics (stash)
|
||||||
cache.UpdateCacheMetrics()
|
cache.UpdateCacheMetrics()
|
||||||
//update cache metrics (regexp)
|
//update cache metrics (regexp)
|
||||||
|
@ -118,16 +113,14 @@ func computeDynamicMetrics(next http.Handler, dbClient *database.Client) http.Ha
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
decisions, err := dbClient.QueryDecisionCountByScenario()
|
decisionsFilters := make(map[string][]string, 0)
|
||||||
|
decisions, err := dbClient.QueryDecisionCountByScenario(decisionsFilters)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error querying decisions for metrics: %v", err)
|
log.Errorf("Error querying decisions for metrics: %v", err)
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
globalActiveDecisions.Reset()
|
globalActiveDecisions.Reset()
|
||||||
|
|
||||||
for _, d := range decisions {
|
for _, d := range decisions {
|
||||||
globalActiveDecisions.With(prometheus.Labels{"reason": d.Scenario, "origin": d.Origin, "action": d.Type}).Set(float64(d.Count))
|
globalActiveDecisions.With(prometheus.Labels{"reason": d.Scenario, "origin": d.Origin, "action": d.Type}).Set(float64(d.Count))
|
||||||
}
|
}
|
||||||
|
@ -139,10 +132,10 @@ func computeDynamicMetrics(next http.Handler, dbClient *database.Client) http.Ha
|
||||||
}
|
}
|
||||||
|
|
||||||
alerts, err := dbClient.AlertsCountPerScenario(alertsFilter)
|
alerts, err := dbClient.AlertsCountPerScenario(alertsFilter)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("Error querying alerts for metrics: %v", err)
|
log.Errorf("Error querying alerts for metrics: %v", err)
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -161,14 +154,14 @@ func registerPrometheus(config *csconfig.PrometheusCfg) {
|
||||||
|
|
||||||
// Registering prometheus
|
// Registering prometheus
|
||||||
// If in aggregated mode, do not register events associated with a source, to keep the cardinality low
|
// If in aggregated mode, do not register events associated with a source, to keep the cardinality low
|
||||||
if config.Level == configuration.CFG_METRICS_AGGREGATE {
|
if config.Level == "aggregated" {
|
||||||
log.Infof("Loading aggregated prometheus collectors")
|
log.Infof("Loading aggregated prometheus collectors")
|
||||||
prometheus.MustRegister(globalParserHits, globalParserHitsOk, globalParserHitsKo,
|
prometheus.MustRegister(globalParserHits, globalParserHitsOk, globalParserHitsKo,
|
||||||
globalCsInfo, globalParsingHistogram, globalPourHistogram,
|
globalCsInfo, globalParsingHistogram, globalPourHistogram,
|
||||||
leaky.BucketsUnderflow, leaky.BucketsCanceled, leaky.BucketsInstantiation, leaky.BucketsOverflow,
|
leaky.BucketsUnderflow, leaky.BucketsCanceled, leaky.BucketsInstantiation, leaky.BucketsOverflow,
|
||||||
v1.LapiRouteHits,
|
v1.LapiRouteHits,
|
||||||
leaky.BucketsCurrentCount,
|
leaky.BucketsCurrentCount,
|
||||||
cache.CacheMetrics, exprhelpers.RegexpCacheMetrics, parser.NodesWlHitsOk, parser.NodesWlHits,
|
cache.CacheMetrics, exprhelpers.RegexpCacheMetrics,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
log.Infof("Loading prometheus collectors")
|
log.Infof("Loading prometheus collectors")
|
||||||
|
@ -177,15 +170,14 @@ func registerPrometheus(config *csconfig.PrometheusCfg) {
|
||||||
globalCsInfo, globalParsingHistogram, globalPourHistogram,
|
globalCsInfo, globalParsingHistogram, globalPourHistogram,
|
||||||
v1.LapiRouteHits, v1.LapiMachineHits, v1.LapiBouncerHits, v1.LapiNilDecisions, v1.LapiNonNilDecisions, v1.LapiResponseTime,
|
v1.LapiRouteHits, v1.LapiMachineHits, v1.LapiBouncerHits, v1.LapiNilDecisions, v1.LapiNonNilDecisions, v1.LapiResponseTime,
|
||||||
leaky.BucketsPour, leaky.BucketsUnderflow, leaky.BucketsCanceled, leaky.BucketsInstantiation, leaky.BucketsOverflow, leaky.BucketsCurrentCount,
|
leaky.BucketsPour, leaky.BucketsUnderflow, leaky.BucketsCanceled, leaky.BucketsInstantiation, leaky.BucketsOverflow, leaky.BucketsCurrentCount,
|
||||||
globalActiveDecisions, globalAlerts, parser.NodesWlHitsOk, parser.NodesWlHits,
|
globalActiveDecisions, globalAlerts,
|
||||||
cache.CacheMetrics, exprhelpers.RegexpCacheMetrics,
|
cache.CacheMetrics, exprhelpers.RegexpCacheMetrics,
|
||||||
)
|
)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func servePrometheus(config *csconfig.PrometheusCfg, dbClient *database.Client, agentReady chan bool) {
|
func servePrometheus(config *csconfig.PrometheusCfg, dbClient *database.Client, apiReady chan bool, agentReady chan bool) {
|
||||||
<-agentReady
|
|
||||||
|
|
||||||
if !config.Enabled {
|
if !config.Enabled {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -193,11 +185,10 @@ func servePrometheus(config *csconfig.PrometheusCfg, dbClient *database.Client,
|
||||||
defer trace.CatchPanic("crowdsec/servePrometheus")
|
defer trace.CatchPanic("crowdsec/servePrometheus")
|
||||||
|
|
||||||
http.Handle("/metrics", computeDynamicMetrics(promhttp.Handler(), dbClient))
|
http.Handle("/metrics", computeDynamicMetrics(promhttp.Handler(), dbClient))
|
||||||
|
<-apiReady
|
||||||
|
<-agentReady
|
||||||
|
log.Debugf("serving metrics after %s ms", time.Since(crowdsecT0))
|
||||||
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", config.ListenAddr, config.ListenPort), nil); err != nil {
|
if err := http.ListenAndServe(fmt.Sprintf("%s:%d", config.ListenAddr, config.ListenPort), nil); err != nil {
|
||||||
// in time machine, we most likely have the LAPI using the port
|
|
||||||
if !flags.haveTimeMachine() {
|
|
||||||
log.Warningf("prometheus: %s", err)
|
log.Warningf("prometheus: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
|
@ -3,12 +3,18 @@ package main
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/url"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-openapi/strfmt"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/go-cs-lib/version"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
||||||
leaky "github.com/crowdsecurity/crowdsec/pkg/leakybucket"
|
leaky "github.com/crowdsecurity/crowdsec/pkg/leakybucket"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/models"
|
"github.com/crowdsecurity/crowdsec/pkg/models"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/parser"
|
"github.com/crowdsecurity/crowdsec/pkg/parser"
|
||||||
|
@ -16,6 +22,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func dedupAlerts(alerts []types.RuntimeAlert) ([]*models.Alert, error) {
|
func dedupAlerts(alerts []types.RuntimeAlert) ([]*models.Alert, error) {
|
||||||
|
|
||||||
var dedupCache []*models.Alert
|
var dedupCache []*models.Alert
|
||||||
|
|
||||||
for idx, alert := range alerts {
|
for idx, alert := range alerts {
|
||||||
|
@ -25,21 +32,16 @@ func dedupAlerts(alerts []types.RuntimeAlert) ([]*models.Alert, error) {
|
||||||
dedupCache = append(dedupCache, alert.Alert)
|
dedupCache = append(dedupCache, alert.Alert)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
for k, src := range alert.Sources {
|
for k, src := range alert.Sources {
|
||||||
refsrc := *alert.Alert //copy
|
refsrc := *alert.Alert //copy
|
||||||
|
|
||||||
log.Tracef("source[%s]", k)
|
log.Tracef("source[%s]", k)
|
||||||
|
|
||||||
refsrc.Source = &src
|
refsrc.Source = &src
|
||||||
dedupCache = append(dedupCache, &refsrc)
|
dedupCache = append(dedupCache, &refsrc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(dedupCache) != len(alerts) {
|
if len(dedupCache) != len(alerts) {
|
||||||
log.Tracef("went from %d to %d alerts", len(alerts), len(dedupCache))
|
log.Tracef("went from %d to %d alerts", len(alerts), len(dedupCache))
|
||||||
}
|
}
|
||||||
|
|
||||||
return dedupCache, nil
|
return dedupCache, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,25 +52,71 @@ func PushAlerts(alerts []types.RuntimeAlert, client *apiclient.ApiClient) error
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to transform alerts for api: %w", err)
|
return fmt.Errorf("failed to transform alerts for api: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, _, err = client.Alerts.Add(ctx, alertsToPush)
|
_, _, err = client.Alerts.Add(ctx, alertsToPush)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed sending alert to LAPI: %w", err)
|
return fmt.Errorf("failed sending alert to LAPI: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var bucketOverflows []types.Event
|
var bucketOverflows []types.Event
|
||||||
|
|
||||||
func runOutput(input chan types.Event, overflow chan types.Event, buckets *leaky.Buckets, postOverflowCTX parser.UnixParserCtx,
|
func runOutput(input chan types.Event, overflow chan types.Event, buckets *leaky.Buckets,
|
||||||
postOverflowNodes []parser.Node, client *apiclient.ApiClient) error {
|
postOverflowCTX parser.UnixParserCtx, postOverflowNodes []parser.Node,
|
||||||
var (
|
apiConfig csconfig.ApiCredentialsCfg, hub *cwhub.Hub) error {
|
||||||
cache []types.RuntimeAlert
|
|
||||||
cacheMutex sync.Mutex
|
|
||||||
)
|
|
||||||
|
|
||||||
|
var err error
|
||||||
ticker := time.NewTicker(1 * time.Second)
|
ticker := time.NewTicker(1 * time.Second)
|
||||||
|
|
||||||
|
var cache []types.RuntimeAlert
|
||||||
|
var cacheMutex sync.Mutex
|
||||||
|
|
||||||
|
scenarios, err := hub.GetInstalledItemNames(cwhub.SCENARIOS)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("loading list of installed hub scenarios: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
apiURL, err := url.Parse(apiConfig.URL)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("parsing api url ('%s'): %w", apiConfig.URL, err)
|
||||||
|
}
|
||||||
|
papiURL, err := url.Parse(apiConfig.PapiURL)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("parsing polling api url ('%s'): %w", apiConfig.PapiURL, err)
|
||||||
|
}
|
||||||
|
password := strfmt.Password(apiConfig.Password)
|
||||||
|
|
||||||
|
Client, err := apiclient.NewClient(&apiclient.Config{
|
||||||
|
MachineID: apiConfig.Login,
|
||||||
|
Password: password,
|
||||||
|
Scenarios: scenarios,
|
||||||
|
UserAgent: fmt.Sprintf("crowdsec/%s", version.String()),
|
||||||
|
URL: apiURL,
|
||||||
|
PapiURL: papiURL,
|
||||||
|
VersionPrefix: "v1",
|
||||||
|
UpdateScenario: func() ([]string, error) {return hub.GetInstalledItemNames(cwhub.SCENARIOS)},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("new client api: %w", err)
|
||||||
|
}
|
||||||
|
authResp, _, err := Client.Auth.AuthenticateWatcher(context.Background(), models.WatcherAuthRequest{
|
||||||
|
MachineID: &apiConfig.Login,
|
||||||
|
Password: &password,
|
||||||
|
Scenarios: scenarios,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("authenticate watcher (%s): %w", apiConfig.Login, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := Client.GetClient().Transport.(*apiclient.JWTTransport).Expiration.UnmarshalText([]byte(authResp.Expire)); err != nil {
|
||||||
|
return fmt.Errorf("unable to parse jwt expiration: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
Client.GetClient().Transport.(*apiclient.JWTTransport).Token = authResp.Token
|
||||||
|
|
||||||
|
//start the heartbeat service
|
||||||
|
log.Debugf("Starting HeartBeat service")
|
||||||
|
Client.HeartBeat.StartHeartBeat(context.Background(), &outputsTomb)
|
||||||
LOOP:
|
LOOP:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -79,7 +127,7 @@ LOOP:
|
||||||
newcache := make([]types.RuntimeAlert, 0)
|
newcache := make([]types.RuntimeAlert, 0)
|
||||||
cache = newcache
|
cache = newcache
|
||||||
cacheMutex.Unlock()
|
cacheMutex.Unlock()
|
||||||
if err := PushAlerts(cachecopy, client); err != nil {
|
if err := PushAlerts(cachecopy, Client); err != nil {
|
||||||
log.Errorf("while pushing to api : %s", err)
|
log.Errorf("while pushing to api : %s", err)
|
||||||
//just push back the events to the queue
|
//just push back the events to the queue
|
||||||
cacheMutex.Lock()
|
cacheMutex.Lock()
|
||||||
|
@ -92,11 +140,10 @@ LOOP:
|
||||||
cacheMutex.Lock()
|
cacheMutex.Lock()
|
||||||
cachecopy := cache
|
cachecopy := cache
|
||||||
cacheMutex.Unlock()
|
cacheMutex.Unlock()
|
||||||
if err := PushAlerts(cachecopy, client); err != nil {
|
if err := PushAlerts(cachecopy, Client); err != nil {
|
||||||
log.Errorf("while pushing leftovers to api : %s", err)
|
log.Errorf("while pushing leftovers to api : %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
break LOOP
|
break LOOP
|
||||||
case event := <-overflow:
|
case event := <-overflow:
|
||||||
/*if alert is empty and mapKey is present, the overflow is just to cleanup bucket*/
|
/*if alert is empty and mapKey is present, the overflow is just to cleanup bucket*/
|
||||||
|
@ -107,7 +154,7 @@ LOOP:
|
||||||
/* process post overflow parser nodes */
|
/* process post overflow parser nodes */
|
||||||
event, err := parser.Parse(postOverflowCTX, event, postOverflowNodes)
|
event, err := parser.Parse(postOverflowCTX, event, postOverflowNodes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("postoverflow failed: %w", err)
|
return fmt.Errorf("postoverflow failed : %s", err)
|
||||||
}
|
}
|
||||||
log.Printf("%s", *event.Overflow.Alert.Message)
|
log.Printf("%s", *event.Overflow.Alert.Message)
|
||||||
//if the Alert is nil, it's to signal bucket is ready for GC, don't track this
|
//if the Alert is nil, it's to signal bucket is ready for GC, don't track this
|
||||||
|
@ -137,6 +184,6 @@ LOOP:
|
||||||
}
|
}
|
||||||
|
|
||||||
ticker.Stop()
|
ticker.Stop()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func runParse(input chan types.Event, output chan types.Event, parserCTX parser.UnixParserCtx, nodes []parser.Node) error {
|
func runParse(input chan types.Event, output chan types.Event, parserCTX parser.UnixParserCtx, nodes []parser.Node) error {
|
||||||
|
|
||||||
LOOP:
|
LOOP:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
@ -55,6 +56,5 @@ LOOP:
|
||||||
output <- parsed
|
output <- parsed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,17 +4,15 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
|
||||||
log "github.com/sirupsen/logrus"
|
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
|
||||||
leaky "github.com/crowdsecurity/crowdsec/pkg/leakybucket"
|
leaky "github.com/crowdsecurity/crowdsec/pkg/leakybucket"
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/types"
|
"github.com/crowdsecurity/crowdsec/pkg/types"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
func runPour(input chan types.Event, holders []leaky.BucketFactory, buckets *leaky.Buckets, cConfig *csconfig.Config) error {
|
func runPour(input chan types.Event, holders []leaky.BucketFactory, buckets *leaky.Buckets, cConfig *csconfig.Config) error {
|
||||||
count := 0
|
count := 0
|
||||||
|
|
||||||
for {
|
for {
|
||||||
//bucket is now ready
|
//bucket is now ready
|
||||||
select {
|
select {
|
||||||
|
@ -23,7 +21,6 @@ func runPour(input chan types.Event, holders []leaky.BucketFactory, buckets *lea
|
||||||
return nil
|
return nil
|
||||||
case parsed := <-input:
|
case parsed := <-input:
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
|
|
||||||
count++
|
count++
|
||||||
if count%5000 == 0 {
|
if count%5000 == 0 {
|
||||||
log.Infof("%d existing buckets", leaky.LeakyRoutineCount)
|
log.Infof("%d existing buckets", leaky.LeakyRoutineCount)
|
||||||
|
@ -35,9 +32,8 @@ func runPour(input chan types.Event, holders []leaky.BucketFactory, buckets *lea
|
||||||
log.Warningf("Failed to unmarshal time from event '%s' : %s", parsed.MarshaledTime, err)
|
log.Warningf("Failed to unmarshal time from event '%s' : %s", parsed.MarshaledTime, err)
|
||||||
} else {
|
} else {
|
||||||
log.Warning("Starting buckets garbage collection ...")
|
log.Warning("Starting buckets garbage collection ...")
|
||||||
|
|
||||||
if err = leaky.GarbageCollectBuckets(*z, buckets); err != nil {
|
if err = leaky.GarbageCollectBuckets(*z, buckets); err != nil {
|
||||||
return fmt.Errorf("failed to start bucket GC : %w", err)
|
return fmt.Errorf("failed to start bucket GC : %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -49,16 +45,13 @@ func runPour(input chan types.Event, holders []leaky.BucketFactory, buckets *lea
|
||||||
log.Errorf("bucketify failed for: %v", parsed)
|
log.Errorf("bucketify failed for: %v", parsed)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
elapsed := time.Since(startTime)
|
elapsed := time.Since(startTime)
|
||||||
globalPourHistogram.With(prometheus.Labels{"type": parsed.Line.Module, "source": parsed.Line.Src}).Observe(elapsed.Seconds())
|
globalPourHistogram.With(prometheus.Labels{"type": parsed.Line.Module, "source": parsed.Line.Src}).Observe(elapsed.Seconds())
|
||||||
|
|
||||||
if poured {
|
if poured {
|
||||||
globalBucketPourOk.Inc()
|
globalBucketPourOk.Inc()
|
||||||
} else {
|
} else {
|
||||||
globalBucketPourKo.Inc()
|
globalBucketPourKo.Inc()
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(parsed.MarshaledTime) != 0 {
|
if len(parsed.MarshaledTime) != 0 {
|
||||||
if err := lastProcessedItem.UnmarshalText([]byte(parsed.MarshaledTime)); err != nil {
|
if err := lastProcessedItem.UnmarshalText([]byte(parsed.MarshaledTime)); err != nil {
|
||||||
log.Warningf("failed to unmarshal time from event : %s", err)
|
log.Warningf("failed to unmarshal time from event : %s", err)
|
||||||
|
|
|
@ -1,12 +1,14 @@
|
||||||
//go:build !windows
|
//go:build linux || freebsd || netbsd || openbsd || solaris || !windows
|
||||||
|
// +build linux freebsd netbsd openbsd solaris !windows
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime/pprof"
|
"os"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
"github.com/sirupsen/logrus/hooks/writer"
|
||||||
|
|
||||||
"github.com/crowdsecurity/go-cs-lib/trace"
|
"github.com/crowdsecurity/go-cs-lib/trace"
|
||||||
"github.com/crowdsecurity/go-cs-lib/version"
|
"github.com/crowdsecurity/go-cs-lib/version"
|
||||||
|
@ -23,9 +25,15 @@ func StartRunSvc() error {
|
||||||
|
|
||||||
defer trace.CatchPanic("crowdsec/StartRunSvc")
|
defer trace.CatchPanic("crowdsec/StartRunSvc")
|
||||||
|
|
||||||
// Always try to stop CPU profiling to avoid passing flags around
|
// Set a default logger with level=fatal on stderr,
|
||||||
// It's a noop if profiling is not enabled
|
// in addition to the one we configure afterwards
|
||||||
defer pprof.StopCPUProfile()
|
log.AddHook(&writer.Hook{
|
||||||
|
Writer: os.Stderr,
|
||||||
|
LogLevels: []log.Level{
|
||||||
|
log.PanicLevel,
|
||||||
|
log.FatalLevel,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
if cConfig, err = LoadConfig(flags.ConfigFile, flags.DisableAgent, flags.DisableAPI, false); err != nil {
|
if cConfig, err = LoadConfig(flags.ConfigFile, flags.DisableAgent, flags.DisableAPI, false); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -33,31 +41,23 @@ func StartRunSvc() error {
|
||||||
|
|
||||||
log.Infof("Crowdsec %s", version.String())
|
log.Infof("Crowdsec %s", version.String())
|
||||||
|
|
||||||
|
apiReady := make(chan bool, 1)
|
||||||
agentReady := make(chan bool, 1)
|
agentReady := make(chan bool, 1)
|
||||||
|
|
||||||
// Enable profiling early
|
// Enable profiling early
|
||||||
if cConfig.Prometheus != nil {
|
if cConfig.Prometheus != nil {
|
||||||
var dbClient *database.Client
|
var dbClient *database.Client
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
if cConfig.DbConfig != nil {
|
if cConfig.DbConfig != nil {
|
||||||
dbClient, err = database.NewClient(cConfig.DbConfig)
|
dbClient, err = database.NewClient(cConfig.DbConfig)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to create database client: %w", err)
|
return fmt.Errorf("unable to create database client: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
registerPrometheus(cConfig.Prometheus)
|
registerPrometheus(cConfig.Prometheus)
|
||||||
|
go servePrometheus(cConfig.Prometheus, dbClient, apiReady, agentReady)
|
||||||
go servePrometheus(cConfig.Prometheus, dbClient, agentReady)
|
|
||||||
} else {
|
|
||||||
// avoid leaking the channel
|
|
||||||
go func() {
|
|
||||||
<-agentReady
|
|
||||||
}()
|
|
||||||
}
|
}
|
||||||
|
return Serve(cConfig, apiReady, agentReady)
|
||||||
return Serve(cConfig, agentReady)
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime/pprof"
|
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
"golang.org/x/sys/windows/svc"
|
"golang.org/x/sys/windows/svc"
|
||||||
|
@ -20,10 +19,6 @@ func StartRunSvc() error {
|
||||||
|
|
||||||
defer trace.CatchPanic("crowdsec/StartRunSvc")
|
defer trace.CatchPanic("crowdsec/StartRunSvc")
|
||||||
|
|
||||||
// Always try to stop CPU profiling to avoid passing flags around
|
|
||||||
// It's a noop if profiling is not enabled
|
|
||||||
defer pprof.StopCPUProfile()
|
|
||||||
|
|
||||||
isRunninginService, err := svc.IsWindowsService()
|
isRunninginService, err := svc.IsWindowsService()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to determine if we are running in windows service mode: %w", err)
|
return fmt.Errorf("failed to determine if we are running in windows service mode: %w", err)
|
||||||
|
@ -73,6 +68,7 @@ func WindowsRun() error {
|
||||||
|
|
||||||
log.Infof("Crowdsec %s", version.String())
|
log.Infof("Crowdsec %s", version.String())
|
||||||
|
|
||||||
|
apiReady := make(chan bool, 1)
|
||||||
agentReady := make(chan bool, 1)
|
agentReady := make(chan bool, 1)
|
||||||
|
|
||||||
// Enable profiling early
|
// Enable profiling early
|
||||||
|
@ -84,11 +80,11 @@ func WindowsRun() error {
|
||||||
dbClient, err = database.NewClient(cConfig.DbConfig)
|
dbClient, err = database.NewClient(cConfig.DbConfig)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("unable to create database client: %w", err)
|
return fmt.Errorf("unable to create database client: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
registerPrometheus(cConfig.Prometheus)
|
registerPrometheus(cConfig.Prometheus)
|
||||||
go servePrometheus(cConfig.Prometheus, dbClient, agentReady)
|
go servePrometheus(cConfig.Prometheus, dbClient, apiReady, agentReady)
|
||||||
}
|
}
|
||||||
return Serve(cConfig, agentReady)
|
return Serve(cConfig, apiReady, agentReady)
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"runtime/pprof"
|
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -42,9 +41,7 @@ func debugHandler(sig os.Signal, cConfig *csconfig.Config) error {
|
||||||
if err := leaky.ShutdownAllBuckets(buckets); err != nil {
|
if err := leaky.ShutdownAllBuckets(buckets); err != nil {
|
||||||
log.Warningf("Failed to shut down routines : %s", err)
|
log.Warningf("Failed to shut down routines : %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Shutdown is finished, buckets are in %s", tmpFile)
|
log.Printf("Shutdown is finished, buckets are in %s", tmpFile)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -68,25 +65,24 @@ func reloadHandler(sig os.Signal) (*csconfig.Config, error) {
|
||||||
if !cConfig.DisableAPI {
|
if !cConfig.DisableAPI {
|
||||||
if flags.DisableCAPI {
|
if flags.DisableCAPI {
|
||||||
log.Warningf("Communication with CrowdSec Central API disabled from args")
|
log.Warningf("Communication with CrowdSec Central API disabled from args")
|
||||||
|
|
||||||
cConfig.API.Server.OnlineClient = nil
|
cConfig.API.Server.OnlineClient = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
apiServer, err := initAPIServer(cConfig)
|
apiServer, err := initAPIServer(cConfig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to init api server: %w", err)
|
return nil, fmt.Errorf("unable to init api server: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
serveAPIServer(apiServer)
|
apiReady := make(chan bool, 1)
|
||||||
|
serveAPIServer(apiServer, apiReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !cConfig.DisableAgent {
|
if !cConfig.DisableAgent {
|
||||||
hub, err := cwhub.NewHub(cConfig.Hub, nil, false, log.StandardLogger())
|
hub, err := cwhub.NewHub(cConfig.Hub, nil, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("while loading hub index: %w", err)
|
return nil, fmt.Errorf("while loading hub index: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
csParsers, datasources, err := initCrowdsec(cConfig, hub)
|
csParsers, err := initCrowdsec(cConfig, hub)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("unable to init crowdsec: %w", err)
|
return nil, fmt.Errorf("unable to init crowdsec: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -103,7 +99,7 @@ func reloadHandler(sig os.Signal) (*csconfig.Config, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
agentReady := make(chan bool, 1)
|
agentReady := make(chan bool, 1)
|
||||||
serveCrowdsec(csParsers, cConfig, hub, datasources, agentReady)
|
serveCrowdsec(csParsers, cConfig, hub, agentReady)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Printf("Reload is finished")
|
log.Printf("Reload is finished")
|
||||||
|
@ -113,7 +109,6 @@ func reloadHandler(sig os.Signal) (*csconfig.Config, error) {
|
||||||
log.Warningf("Failed to delete temp file (%s) : %s", tmpFile, err)
|
log.Warningf("Failed to delete temp file (%s) : %s", tmpFile, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return cConfig, nil
|
return cConfig, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,12 +116,10 @@ func ShutdownCrowdsecRoutines() error {
|
||||||
var reterr error
|
var reterr error
|
||||||
|
|
||||||
log.Debugf("Shutting down crowdsec sub-routines")
|
log.Debugf("Shutting down crowdsec sub-routines")
|
||||||
|
|
||||||
if len(dataSources) > 0 {
|
if len(dataSources) > 0 {
|
||||||
acquisTomb.Kill(nil)
|
acquisTomb.Kill(nil)
|
||||||
log.Debugf("waiting for acquisition to finish")
|
log.Debugf("waiting for acquisition to finish")
|
||||||
drainChan(inputLineChan)
|
drainChan(inputLineChan)
|
||||||
|
|
||||||
if err := acquisTomb.Wait(); err != nil {
|
if err := acquisTomb.Wait(); err != nil {
|
||||||
log.Warningf("Acquisition returned error : %s", err)
|
log.Warningf("Acquisition returned error : %s", err)
|
||||||
reterr = err
|
reterr = err
|
||||||
|
@ -136,7 +129,6 @@ func ShutdownCrowdsecRoutines() error {
|
||||||
log.Debugf("acquisition is finished, wait for parser/bucket/ouputs.")
|
log.Debugf("acquisition is finished, wait for parser/bucket/ouputs.")
|
||||||
parsersTomb.Kill(nil)
|
parsersTomb.Kill(nil)
|
||||||
drainChan(inputEventChan)
|
drainChan(inputEventChan)
|
||||||
|
|
||||||
if err := parsersTomb.Wait(); err != nil {
|
if err := parsersTomb.Wait(); err != nil {
|
||||||
log.Warningf("Parsers returned error : %s", err)
|
log.Warningf("Parsers returned error : %s", err)
|
||||||
reterr = err
|
reterr = err
|
||||||
|
@ -167,7 +159,6 @@ func ShutdownCrowdsecRoutines() error {
|
||||||
log.Warningf("Outputs returned error : %s", err)
|
log.Warningf("Outputs returned error : %s", err)
|
||||||
reterr = err
|
reterr = err
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("outputs are done")
|
log.Debugf("outputs are done")
|
||||||
case <-time.After(3 * time.Second):
|
case <-time.After(3 * time.Second):
|
||||||
// this can happen if outputs are stuck in a http retry loop
|
// this can happen if outputs are stuck in a http retry loop
|
||||||
|
@ -189,7 +180,6 @@ func shutdownAPI() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("done")
|
log.Debugf("done")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -202,7 +192,6 @@ func shutdownCrowdsec() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Debugf("done")
|
log.Debugf("done")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,10 +245,6 @@ func HandleSignals(cConfig *csconfig.Config) error {
|
||||||
|
|
||||||
exitChan := make(chan error)
|
exitChan := make(chan error)
|
||||||
|
|
||||||
// Always try to stop CPU profiling to avoid passing flags around
|
|
||||||
// It's a noop if profiling is not enabled
|
|
||||||
defer pprof.StopCPUProfile()
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer trace.CatchPanic("crowdsec/HandleSignals")
|
defer trace.CatchPanic("crowdsec/HandleSignals")
|
||||||
Loop:
|
Loop:
|
||||||
|
@ -302,11 +287,10 @@ func HandleSignals(cConfig *csconfig.Config) error {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Warning("Crowdsec service shutting down")
|
log.Warning("Crowdsec service shutting down")
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func Serve(cConfig *csconfig.Config, agentReady chan bool) error {
|
func Serve(cConfig *csconfig.Config, apiReady chan bool, agentReady chan bool) error {
|
||||||
acquisTomb = tomb.Tomb{}
|
acquisTomb = tomb.Tomb{}
|
||||||
parsersTomb = tomb.Tomb{}
|
parsersTomb = tomb.Tomb{}
|
||||||
bucketsTomb = tomb.Tomb{}
|
bucketsTomb = tomb.Tomb{}
|
||||||
|
@ -336,7 +320,6 @@ func Serve(cConfig *csconfig.Config, agentReady chan bool) error {
|
||||||
|
|
||||||
if cConfig.API.CTI != nil && *cConfig.API.CTI.Enabled {
|
if cConfig.API.CTI != nil && *cConfig.API.CTI.Enabled {
|
||||||
log.Infof("Crowdsec CTI helper enabled")
|
log.Infof("Crowdsec CTI helper enabled")
|
||||||
|
|
||||||
if err := exprhelpers.InitCrowdsecCTI(cConfig.API.CTI.Key, cConfig.API.CTI.CacheTimeout, cConfig.API.CTI.CacheSize, cConfig.API.CTI.LogLevel); err != nil {
|
if err := exprhelpers.InitCrowdsecCTI(cConfig.API.CTI.Key, cConfig.API.CTI.CacheTimeout, cConfig.API.CTI.CacheSize, cConfig.API.CTI.LogLevel); err != nil {
|
||||||
return fmt.Errorf("failed to init crowdsec cti: %w", err)
|
return fmt.Errorf("failed to init crowdsec cti: %w", err)
|
||||||
}
|
}
|
||||||
|
@ -349,7 +332,6 @@ func Serve(cConfig *csconfig.Config, agentReady chan bool) error {
|
||||||
|
|
||||||
if flags.DisableCAPI {
|
if flags.DisableCAPI {
|
||||||
log.Warningf("Communication with CrowdSec Central API disabled from args")
|
log.Warningf("Communication with CrowdSec Central API disabled from args")
|
||||||
|
|
||||||
cConfig.API.Server.OnlineClient = nil
|
cConfig.API.Server.OnlineClient = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -359,26 +341,26 @@ func Serve(cConfig *csconfig.Config, agentReady chan bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !flags.TestMode {
|
if !flags.TestMode {
|
||||||
serveAPIServer(apiServer)
|
serveAPIServer(apiServer, apiReady)
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
apiReady <- true
|
||||||
}
|
}
|
||||||
|
|
||||||
if !cConfig.DisableAgent {
|
if !cConfig.DisableAgent {
|
||||||
hub, err := cwhub.NewHub(cConfig.Hub, nil, false, log.StandardLogger())
|
hub, err := cwhub.NewHub(cConfig.Hub, nil, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("while loading hub index: %w", err)
|
return fmt.Errorf("while loading hub index: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
csParsers, datasources, err := initCrowdsec(cConfig, hub)
|
csParsers, err := initCrowdsec(cConfig, hub)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("crowdsec init: %w", err)
|
return fmt.Errorf("crowdsec init: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if it's just linting, we're done
|
// if it's just linting, we're done
|
||||||
if !flags.TestMode {
|
if !flags.TestMode {
|
||||||
serveCrowdsec(csParsers, cConfig, hub, datasources, agentReady)
|
serveCrowdsec(csParsers, cConfig, hub, agentReady)
|
||||||
} else {
|
|
||||||
agentReady <- true
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
agentReady <- true
|
agentReady <- true
|
||||||
|
@ -391,7 +373,7 @@ func Serve(cConfig *csconfig.Config, agentReady chan bool) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
if cConfig.Common != nil && cConfig.Common.Daemonize {
|
if cConfig.Common != nil && cConfig.Common.Daemonize {
|
||||||
csdaemon.Notify(csdaemon.Ready, log.StandardLogger())
|
csdaemon.NotifySystemd(log.StandardLogger())
|
||||||
// wait for signals
|
// wait for signals
|
||||||
return HandleSignals(cConfig)
|
return HandleSignals(cConfig)
|
||||||
}
|
}
|
||||||
|
@ -408,7 +390,6 @@ func Serve(cConfig *csconfig.Config, agentReady chan bool) error {
|
||||||
|
|
||||||
for _, ch := range waitChans {
|
for _, ch := range waitChans {
|
||||||
<-ch
|
<-ch
|
||||||
|
|
||||||
switch ch {
|
switch ch {
|
||||||
case apiTomb.Dead():
|
case apiTomb.Dead():
|
||||||
log.Infof("api shutdown")
|
log.Infof("api shutdown")
|
||||||
|
@ -416,6 +397,5 @@ func Serve(cConfig *csconfig.Config, agentReady chan bool) error {
|
||||||
log.Infof("crowdsec shutdown")
|
log.Infof("crowdsec shutdown")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
//go:build windows
|
//go:build windows
|
||||||
|
// +build windows
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
@ -23,7 +24,7 @@ type crowdsec_winservice struct {
|
||||||
config *csconfig.Config
|
config *csconfig.Config
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *crowdsec_winservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
|
func (m *crowdsec_winservice) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
|
||||||
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
|
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
|
||||||
changes <- svc.Status{State: svc.StartPending}
|
changes <- svc.Status{State: svc.StartPending}
|
||||||
tick := time.Tick(500 * time.Millisecond)
|
tick := time.Tick(500 * time.Millisecond)
|
||||||
|
@ -59,8 +60,7 @@ func (m *crowdsec_winservice) Execute(args []string, r <-chan svc.ChangeRequest,
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
return
|
||||||
return false, 0
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func runService(name string) error {
|
func runService(name string) error {
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
//go:build windows
|
//go:build windows
|
||||||
|
// +build windows
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
// Use of this source code is governed by a BSD-style
|
// Use of this source code is governed by a BSD-style
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
//go:build windows
|
// +build windows
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
|
|
@ -5,11 +5,10 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
|
||||||
"github.com/hashicorp/go-hclog"
|
"github.com/hashicorp/go-hclog"
|
||||||
plugin "github.com/hashicorp/go-plugin"
|
plugin "github.com/hashicorp/go-plugin"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type PluginConfig struct {
|
type PluginConfig struct {
|
||||||
|
@ -33,7 +32,6 @@ func (s *DummyPlugin) Notify(ctx context.Context, notification *protobufs.Notifi
|
||||||
if _, ok := s.PluginConfigByName[notification.Name]; !ok {
|
if _, ok := s.PluginConfigByName[notification.Name]; !ok {
|
||||||
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
|
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := s.PluginConfigByName[notification.Name]
|
cfg := s.PluginConfigByName[notification.Name]
|
||||||
|
|
||||||
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
|
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
|
||||||
|
@ -44,22 +42,19 @@ func (s *DummyPlugin) Notify(ctx context.Context, notification *protobufs.Notifi
|
||||||
logger.Debug(notification.Text)
|
logger.Debug(notification.Text)
|
||||||
|
|
||||||
if cfg.OutputFile != nil && *cfg.OutputFile != "" {
|
if cfg.OutputFile != nil && *cfg.OutputFile != "" {
|
||||||
f, err := os.OpenFile(*cfg.OutputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644)
|
f, err := os.OpenFile(*cfg.OutputFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(fmt.Sprintf("Cannot open notification file: %s", err))
|
logger.Error(fmt.Sprintf("Cannot open notification file: %s", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := f.WriteString(notification.Text + "\n"); err != nil {
|
if _, err := f.WriteString(notification.Text + "\n"); err != nil {
|
||||||
f.Close()
|
f.Close()
|
||||||
logger.Error(fmt.Sprintf("Cannot write notification to file: %s", err))
|
logger.Error(fmt.Sprintf("Cannot write notification to file: %s", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
err = f.Close()
|
err = f.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(fmt.Sprintf("Cannot close notification file: %s", err))
|
logger.Error(fmt.Sprintf("Cannot close notification file: %s", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(notification.Text)
|
fmt.Println(notification.Text)
|
||||||
|
|
||||||
return &protobufs.Empty{}, nil
|
return &protobufs.Empty{}, nil
|
||||||
|
@ -69,12 +64,11 @@ func (s *DummyPlugin) Configure(ctx context.Context, config *protobufs.Config) (
|
||||||
d := PluginConfig{}
|
d := PluginConfig{}
|
||||||
err := yaml.Unmarshal(config.Config, &d)
|
err := yaml.Unmarshal(config.Config, &d)
|
||||||
s.PluginConfigByName[d.Name] = d
|
s.PluginConfigByName[d.Name] = d
|
||||||
|
|
||||||
return &protobufs.Empty{}, err
|
return &protobufs.Empty{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
handshake := plugin.HandshakeConfig{
|
var handshake = plugin.HandshakeConfig{
|
||||||
ProtocolVersion: 1,
|
ProtocolVersion: 1,
|
||||||
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
|
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
|
||||||
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),
|
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),
|
||||||
|
|
|
@ -2,17 +2,15 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
|
||||||
"github.com/hashicorp/go-hclog"
|
"github.com/hashicorp/go-hclog"
|
||||||
plugin "github.com/hashicorp/go-plugin"
|
plugin "github.com/hashicorp/go-plugin"
|
||||||
mail "github.com/xhit/go-simple-mail/v2"
|
mail "github.com/xhit/go-simple-mail/v2"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v2"
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var baseLogger hclog.Logger = hclog.New(&hclog.LoggerOptions{
|
var baseLogger hclog.Logger = hclog.New(&hclog.LoggerOptions{
|
||||||
|
@ -74,20 +72,19 @@ func (n *EmailPlugin) Configure(ctx context.Context, config *protobufs.Config) (
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.Name == "" {
|
if d.Name == "" {
|
||||||
return nil, errors.New("name is required")
|
return nil, fmt.Errorf("name is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.SMTPHost == "" {
|
if d.SMTPHost == "" {
|
||||||
return nil, errors.New("SMTP host is not set")
|
return nil, fmt.Errorf("SMTP host is not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
if d.ReceiverEmails == nil || len(d.ReceiverEmails) == 0 {
|
if d.ReceiverEmails == nil || len(d.ReceiverEmails) == 0 {
|
||||||
return nil, errors.New("receiver emails are not set")
|
return nil, fmt.Errorf("receiver emails are not set")
|
||||||
}
|
}
|
||||||
|
|
||||||
n.ConfigByName[d.Name] = d
|
n.ConfigByName[d.Name] = d
|
||||||
baseLogger.Debug(fmt.Sprintf("Email plugin '%s' use SMTP host '%s:%d'", d.Name, d.SMTPHost, d.SMTPPort))
|
baseLogger.Debug(fmt.Sprintf("Email plugin '%s' use SMTP host '%s:%d'", d.Name, d.SMTPHost, d.SMTPPort))
|
||||||
|
|
||||||
return &protobufs.Empty{}, nil
|
return &protobufs.Empty{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,7 +92,6 @@ func (n *EmailPlugin) Notify(ctx context.Context, notification *protobufs.Notifi
|
||||||
if _, ok := n.ConfigByName[notification.Name]; !ok {
|
if _, ok := n.ConfigByName[notification.Name]; !ok {
|
||||||
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
|
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := n.ConfigByName[notification.Name]
|
cfg := n.ConfigByName[notification.Name]
|
||||||
|
|
||||||
logger := baseLogger.Named(cfg.Name)
|
logger := baseLogger.Named(cfg.Name)
|
||||||
|
@ -121,7 +117,6 @@ func (n *EmailPlugin) Notify(ctx context.Context, notification *protobufs.Notifi
|
||||||
server.ConnectTimeout, err = time.ParseDuration(cfg.ConnectTimeout)
|
server.ConnectTimeout, err = time.ParseDuration(cfg.ConnectTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn(fmt.Sprintf("invalid connect timeout '%s', using default '10s'", cfg.ConnectTimeout))
|
logger.Warn(fmt.Sprintf("invalid connect timeout '%s', using default '10s'", cfg.ConnectTimeout))
|
||||||
|
|
||||||
server.ConnectTimeout = 10 * time.Second
|
server.ConnectTimeout = 10 * time.Second
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -130,18 +125,15 @@ func (n *EmailPlugin) Notify(ctx context.Context, notification *protobufs.Notifi
|
||||||
server.SendTimeout, err = time.ParseDuration(cfg.SendTimeout)
|
server.SendTimeout, err = time.ParseDuration(cfg.SendTimeout)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warn(fmt.Sprintf("invalid send timeout '%s', using default '10s'", cfg.SendTimeout))
|
logger.Warn(fmt.Sprintf("invalid send timeout '%s', using default '10s'", cfg.SendTimeout))
|
||||||
|
|
||||||
server.SendTimeout = 10 * time.Second
|
server.SendTimeout = 10 * time.Second
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("making smtp connection")
|
logger.Debug("making smtp connection")
|
||||||
|
|
||||||
smtpClient, err := server.Connect()
|
smtpClient, err := server.Connect()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &protobufs.Empty{}, err
|
return &protobufs.Empty{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("smtp connection done")
|
logger.Debug("smtp connection done")
|
||||||
|
|
||||||
email := mail.NewMSG()
|
email := mail.NewMSG()
|
||||||
|
@ -154,14 +146,12 @@ func (n *EmailPlugin) Notify(ctx context.Context, notification *protobufs.Notifi
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return &protobufs.Empty{}, err
|
return &protobufs.Empty{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info(fmt.Sprintf("sent email to %v", cfg.ReceiverEmails))
|
logger.Info(fmt.Sprintf("sent email to %v", cfg.ReceiverEmails))
|
||||||
|
|
||||||
return &protobufs.Empty{}, nil
|
return &protobufs.Empty{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
handshake := plugin.HandshakeConfig{
|
var handshake = plugin.HandshakeConfig{
|
||||||
ProtocolVersion: 1,
|
ProtocolVersion: 1,
|
||||||
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
|
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
|
||||||
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),
|
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),
|
||||||
|
|
|
@ -4,33 +4,24 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"crypto/x509"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/hashicorp/go-hclog"
|
|
||||||
plugin "github.com/hashicorp/go-plugin"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
|
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
|
||||||
|
"github.com/hashicorp/go-hclog"
|
||||||
|
plugin "github.com/hashicorp/go-plugin"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PluginConfig struct {
|
type PluginConfig struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
URL string `yaml:"url"`
|
URL string `yaml:"url"`
|
||||||
UnixSocket string `yaml:"unix_socket"`
|
|
||||||
Headers map[string]string `yaml:"headers"`
|
Headers map[string]string `yaml:"headers"`
|
||||||
SkipTLSVerification bool `yaml:"skip_tls_verification"`
|
SkipTLSVerification bool `yaml:"skip_tls_verification"`
|
||||||
Method string `yaml:"method"`
|
Method string `yaml:"method"`
|
||||||
LogLevel *string `yaml:"log_level"`
|
LogLevel *string `yaml:"log_level"`
|
||||||
Client *http.Client `yaml:"-"`
|
|
||||||
CertPath string `yaml:"cert_path"`
|
|
||||||
KeyPath string `yaml:"key_path"`
|
|
||||||
CAPath string `yaml:"ca_cert_path"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type HTTPPlugin struct {
|
type HTTPPlugin struct {
|
||||||
|
@ -44,78 +35,10 @@ var logger hclog.Logger = hclog.New(&hclog.LoggerOptions{
|
||||||
JSONFormat: true,
|
JSONFormat: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
func getCertPool(caPath string) (*x509.CertPool, error) {
|
|
||||||
cp, err := x509.SystemCertPool()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to load system CA certificates: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if cp == nil {
|
|
||||||
cp = x509.NewCertPool()
|
|
||||||
}
|
|
||||||
|
|
||||||
if caPath == "" {
|
|
||||||
return cp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Info(fmt.Sprintf("Using CA cert '%s'", caPath))
|
|
||||||
|
|
||||||
caCert, err := os.ReadFile(caPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("unable to load CA certificate '%s': %w", caPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
cp.AppendCertsFromPEM(caCert)
|
|
||||||
|
|
||||||
return cp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func getTLSClient(c *PluginConfig) error {
|
|
||||||
caCertPool, err := getCertPool(c.CAPath)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
tlsConfig := &tls.Config{
|
|
||||||
RootCAs: caCertPool,
|
|
||||||
InsecureSkipVerify: c.SkipTLSVerification,
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.CertPath != "" && c.KeyPath != "" {
|
|
||||||
logger.Info(fmt.Sprintf("Using client certificate '%s' and key '%s'", c.CertPath, c.KeyPath))
|
|
||||||
|
|
||||||
cert, err := tls.LoadX509KeyPair(c.CertPath, c.KeyPath)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("unable to load client certificate '%s' and key '%s': %w", c.CertPath, c.KeyPath, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
tlsConfig.Certificates = []tls.Certificate{cert}
|
|
||||||
}
|
|
||||||
|
|
||||||
transport := &http.Transport{
|
|
||||||
TLSClientConfig: tlsConfig,
|
|
||||||
}
|
|
||||||
|
|
||||||
if c.UnixSocket != "" {
|
|
||||||
logger.Info(fmt.Sprintf("Using socket '%s'", c.UnixSocket))
|
|
||||||
|
|
||||||
transport.DialContext = func(_ context.Context, _, _ string) (net.Conn, error) {
|
|
||||||
return net.Dial("unix", strings.TrimSuffix(c.UnixSocket, "/"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Client = &http.Client{
|
|
||||||
Transport: transport,
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *HTTPPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
|
func (s *HTTPPlugin) Notify(ctx context.Context, notification *protobufs.Notification) (*protobufs.Empty, error) {
|
||||||
if _, ok := s.PluginConfigByName[notification.Name]; !ok {
|
if _, ok := s.PluginConfigByName[notification.Name]; !ok {
|
||||||
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
|
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := s.PluginConfigByName[notification.Name]
|
cfg := s.PluginConfigByName[notification.Name]
|
||||||
|
|
||||||
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
|
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
|
||||||
|
@ -123,20 +46,24 @@ func (s *HTTPPlugin) Notify(ctx context.Context, notification *protobufs.Notific
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info(fmt.Sprintf("received signal for %s config", notification.Name))
|
logger.Info(fmt.Sprintf("received signal for %s config", notification.Name))
|
||||||
|
client := http.Client{}
|
||||||
|
|
||||||
|
if cfg.SkipTLSVerification {
|
||||||
|
client.Transport = &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
request, err := http.NewRequest(cfg.Method, cfg.URL, bytes.NewReader([]byte(notification.Text)))
|
request, err := http.NewRequest(cfg.Method, cfg.URL, bytes.NewReader([]byte(notification.Text)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for headerName, headerValue := range cfg.Headers {
|
for headerName, headerValue := range cfg.Headers {
|
||||||
logger.Debug(fmt.Sprintf("adding header %s: %s", headerName, headerValue))
|
logger.Debug(fmt.Sprintf("adding header %s: %s", headerName, headerValue))
|
||||||
request.Header.Add(headerName, headerValue)
|
request.Header.Add(headerName, headerValue)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug(fmt.Sprintf("making HTTP %s call to %s with body %s", cfg.Method, cfg.URL, notification.Text))
|
logger.Debug(fmt.Sprintf("making HTTP %s call to %s with body %s", cfg.Method, cfg.URL, notification.Text))
|
||||||
|
resp, err := client.Do(request.WithContext(ctx))
|
||||||
resp, err := cfg.Client.Do(request.WithContext(ctx))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error(fmt.Sprintf("Failed to make HTTP request : %s", err))
|
logger.Error(fmt.Sprintf("Failed to make HTTP request : %s", err))
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -145,15 +72,13 @@ func (s *HTTPPlugin) Notify(ctx context.Context, notification *protobufs.Notific
|
||||||
|
|
||||||
respData, err := io.ReadAll(resp.Body)
|
respData, err := io.ReadAll(resp.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to read response body got error %w", err)
|
return nil, fmt.Errorf("failed to read response body got error %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug(fmt.Sprintf("got response %s", string(respData)))
|
logger.Debug(fmt.Sprintf("got response %s", string(respData)))
|
||||||
|
|
||||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||||
logger.Warn(fmt.Sprintf("HTTP server returned non 200 status code: %d", resp.StatusCode))
|
logger.Warn(fmt.Sprintf("HTTP server returned non 200 status code: %d", resp.StatusCode))
|
||||||
logger.Debug(fmt.Sprintf("HTTP server returned body: %s", string(respData)))
|
|
||||||
|
|
||||||
return &protobufs.Empty{}, nil
|
return &protobufs.Empty{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -162,25 +87,14 @@ func (s *HTTPPlugin) Notify(ctx context.Context, notification *protobufs.Notific
|
||||||
|
|
||||||
func (s *HTTPPlugin) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
|
func (s *HTTPPlugin) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
|
||||||
d := PluginConfig{}
|
d := PluginConfig{}
|
||||||
|
|
||||||
err := yaml.Unmarshal(config.Config, &d)
|
err := yaml.Unmarshal(config.Config, &d)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = getTLSClient(&d)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
s.PluginConfigByName[d.Name] = d
|
s.PluginConfigByName[d.Name] = d
|
||||||
logger.Debug(fmt.Sprintf("HTTP plugin '%s' use URL '%s'", d.Name, d.URL))
|
logger.Debug(fmt.Sprintf("HTTP plugin '%s' use URL '%s'", d.Name, d.URL))
|
||||||
|
|
||||||
return &protobufs.Empty{}, err
|
return &protobufs.Empty{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
handshake := plugin.HandshakeConfig{
|
var handshake = plugin.HandshakeConfig{
|
||||||
ProtocolVersion: 1,
|
ProtocolVersion: 1,
|
||||||
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
|
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
|
||||||
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),
|
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),
|
||||||
|
|
|
@ -5,12 +5,12 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
|
||||||
"github.com/hashicorp/go-hclog"
|
"github.com/hashicorp/go-hclog"
|
||||||
plugin "github.com/hashicorp/go-plugin"
|
plugin "github.com/hashicorp/go-plugin"
|
||||||
"github.com/slack-go/slack"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
|
|
||||||
"github.com/crowdsecurity/crowdsec/pkg/protobufs"
|
"github.com/slack-go/slack"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PluginConfig struct {
|
type PluginConfig struct {
|
||||||
|
@ -33,16 +33,13 @@ func (n *Notify) Notify(ctx context.Context, notification *protobufs.Notificatio
|
||||||
if _, ok := n.ConfigByName[notification.Name]; !ok {
|
if _, ok := n.ConfigByName[notification.Name]; !ok {
|
||||||
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
|
return nil, fmt.Errorf("invalid plugin config name %s", notification.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg := n.ConfigByName[notification.Name]
|
cfg := n.ConfigByName[notification.Name]
|
||||||
|
|
||||||
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
|
if cfg.LogLevel != nil && *cfg.LogLevel != "" {
|
||||||
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
|
logger.SetLevel(hclog.LevelFromString(*cfg.LogLevel))
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Info(fmt.Sprintf("found notify signal for %s config", notification.Name))
|
logger.Info(fmt.Sprintf("found notify signal for %s config", notification.Name))
|
||||||
logger.Debug(fmt.Sprintf("posting to %s webhook, message %s", cfg.Webhook, notification.Text))
|
logger.Debug(fmt.Sprintf("posting to %s webhook, message %s", cfg.Webhook, notification.Text))
|
||||||
|
|
||||||
err := slack.PostWebhookContext(ctx, n.ConfigByName[notification.Name].Webhook, &slack.WebhookMessage{
|
err := slack.PostWebhookContext(ctx, n.ConfigByName[notification.Name].Webhook, &slack.WebhookMessage{
|
||||||
Text: notification.Text,
|
Text: notification.Text,
|
||||||
})
|
})
|
||||||
|
@ -55,19 +52,16 @@ func (n *Notify) Notify(ctx context.Context, notification *protobufs.Notificatio
|
||||||
|
|
||||||
func (n *Notify) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
|
func (n *Notify) Configure(ctx context.Context, config *protobufs.Config) (*protobufs.Empty, error) {
|
||||||
d := PluginConfig{}
|
d := PluginConfig{}
|
||||||
|
|
||||||
if err := yaml.Unmarshal(config.Config, &d); err != nil {
|
if err := yaml.Unmarshal(config.Config, &d); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
n.ConfigByName[d.Name] = d
|
n.ConfigByName[d.Name] = d
|
||||||
logger.Debug(fmt.Sprintf("Slack plugin '%s' use URL '%s'", d.Name, d.Webhook))
|
logger.Debug(fmt.Sprintf("Slack plugin '%s' use URL '%s'", d.Name, d.Webhook))
|
||||||
|
|
||||||
return &protobufs.Empty{}, nil
|
return &protobufs.Empty{}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
handshake := plugin.HandshakeConfig{
|
var handshake = plugin.HandshakeConfig{
|
||||||
ProtocolVersion: 1,
|
ProtocolVersion: 1,
|
||||||
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
|
MagicCookieKey: "CROWDSEC_PLUGIN_KEY",
|
||||||
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),
|
MagicCookieValue: os.Getenv("CROWDSEC_PLUGIN_KEY"),
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue