Skip to content

Commit e041887

Browse files
committed
Merge branch 'master' into releases/1.6.x
2 parents 83d54e9 + d7d7e67 commit e041887

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+766
-612
lines changed

.github/workflows/go-tests.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,18 @@ jobs:
181181
- name: Unit tests
182182
run: |
183183
go install gotest.tools/[email protected]
184+
# make the repo read-only, with the exception of coverage output
185+
touch coverage.out
186+
chmod -R a-w .
187+
chmod u+w coverage.out
184188
make testcover
189+
# ignore changes to codecov.yml
190+
if [[ $(git status --porcelain -- . ":(exclude).github/codecov.yml") ]]; then
191+
echo "Error: Unit tests should not create or alter files inside the repository. Please use the appropriate testing helpers or otherwise temporary locations."
192+
git diff --name-only
193+
exit 1
194+
fi
195+
chmod -R u+w .
185196
186197
# check if some component stubs are missing
187198
- name: "Build profile: minimal"

.golangci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ linters:
194194
min-complexity: 16
195195

196196
nlreturn:
197-
block-size: 5
197+
block-size: 6
198198

199199
nolintlint:
200200
require-explanation: false # don't require an explanation for nolint directives

cmd/crowdsec-cli/main.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"os"
7+
"os/signal"
68
"path/filepath"
79
"slices"
10+
"syscall"
811
"time"
912

1013
"github.com/fatih/color"
@@ -171,6 +174,7 @@ func (cli *cliRoot) initialize() error {
171174
if csConfig.DbConfig != nil {
172175
csConfig.DbConfig.LogLevel = ptr.Of(cli.wantedLogLevel())
173176
}
177+
174178
return nil
175179
}
176180

@@ -295,7 +299,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
295299
cobra.OnInitialize(
296300
func() {
297301
if err := cli.initialize(); err != nil {
298-
log.Fatal(err)
302+
fatal(err)
299303
}
300304
},
301305
)
@@ -304,15 +308,27 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
304308
return cmd, nil
305309
}
306310

307-
func main() {
311+
func fatal(err error) {
312+
red := color.New(color.FgRed).SprintFunc()
313+
fmt.Fprintln(os.Stderr, red("Error:"), err)
314+
os.Exit(1)
315+
}
316+
317+
func mainWrap() error {
318+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
319+
defer stop()
320+
308321
cmd, err := newCliRoot().NewCommand()
309322
if err != nil {
310-
log.Fatal(err)
323+
return err
311324
}
312325

313-
if err := cmd.Execute(); err != nil {
314-
red := color.New(color.FgRed).SprintFunc()
315-
fmt.Fprintln(os.Stderr, red("Error:"), err)
316-
os.Exit(1)
326+
return cmd.ExecuteContext(ctx)
327+
}
328+
329+
func main() {
330+
err := mainWrap()
331+
if err != nil {
332+
fatal(err)
317333
}
318334
}

cmd/crowdsec/main.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (l *labelsMap) String() string {
144144
}
145145

146146
func (l *labelsMap) Set(label string) error {
147-
for _, pair := range strings.Split(label, ",") {
147+
for pair := range strings.SplitSeq(label, ",") {
148148
split := strings.Split(pair, ":")
149149
if len(split) != 2 {
150150
return fmt.Errorf("invalid format for label '%s', must be key:value", pair)
@@ -233,7 +233,9 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
233233
if err := trace.Init(filepath.Join(cConfig.ConfigPaths.DataDir, "trace")); err != nil {
234234
return nil, fmt.Errorf("while setting up trace directory: %w", err)
235235
}
236+
236237
var logLevelViaFlag bool
238+
237239
cConfig.Common.LogLevel, logLevelViaFlag = newLogLevel(cConfig.Common.LogLevel, flags)
238240

239241
if dumpFolder != "" {
@@ -301,24 +303,14 @@ func LoadConfig(configFile string, disableAgent bool, disableAPI bool, quiet boo
301303
if cConfig.API != nil && cConfig.API.Server != nil {
302304
cConfig.API.Server.OnlineClient = nil
303305
}
304-
// if the api is disabled as well, just read file and exit, don't daemonize
305-
if cConfig.DisableAPI {
306-
cConfig.Common.Daemonize = false
307-
}
308306

309-
log.Infof("single file mode : log_media=%s daemonize=%t", cConfig.Common.LogMedia, cConfig.Common.Daemonize)
307+
log.Infof("single file mode : log_media=%s", cConfig.Common.LogMedia)
310308
}
311309

312310
if cConfig.Common.PidDir != "" {
313311
log.Warn("Deprecation warning: the pid_dir config can be safely removed and is not required")
314312
}
315313

316-
if cConfig.Common.Daemonize && runtime.GOOS == "windows" {
317-
log.Debug("Daemonization is not supported on Windows, disabling")
318-
319-
cConfig.Common.Daemonize = false
320-
}
321-
322314
// recap of the enabled feature flags, because logging
323315
// was not enabled when we set them from envvars
324316
if fflist := csconfig.ListFeatureFlags(); fflist != "" {

cmd/crowdsec/serve.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"github.com/crowdsecurity/go-cs-lib/csdaemon"
1616
"github.com/crowdsecurity/go-cs-lib/trace"
1717

18+
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
1819
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
1920
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
2021
"github.com/crowdsecurity/crowdsec/pkg/database"
@@ -322,6 +323,18 @@ func HandleSignals(cConfig *csconfig.Config) error {
322323
if err == nil {
323324
log.Warning("Crowdsec service shutting down")
324325
}
326+
if cConfig.API != nil && cConfig.API.Client != nil && cConfig.API.Client.UnregisterOnExit {
327+
log.Warning("Unregistering watcher")
328+
lapiClient, err := apiclient.GetLAPIClient()
329+
if err != nil {
330+
return err
331+
}
332+
_, err = lapiClient.Auth.UnregisterWatcher(context.TODO())
333+
if err != nil {
334+
return fmt.Errorf("failed to unregister watcher: %w", err)
335+
}
336+
log.Warning("Watcher unregistered")
337+
}
325338

326339
return err
327340
}
@@ -418,7 +431,7 @@ func Serve(cConfig *csconfig.Config, agentReady chan bool) error {
418431
return nil
419432
}
420433

421-
if cConfig.Common != nil && cConfig.Common.Daemonize {
434+
if cConfig.Common != nil && !flags.haveTimeMachine() {
422435
_ = csdaemon.Notify(csdaemon.Ready, log.StandardLogger())
423436
// wait for signals
424437
return HandleSignals(cConfig)

config/config_win.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
common:
2-
daemonize: false
32
log_media: file
43
log_level: info
54
log_dir: C:\ProgramData\CrowdSec\log\

config/config_win_no_lapi.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
common:
2-
daemonize: true
32
log_media: file
43
log_level: info
54
log_dir: C:\ProgramData\CrowdSec\log\

config/dev.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
common:
2-
daemonize: true
32
log_media: stdout
43
log_level: info
54
config_paths:

config/user.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
common:
2-
daemonize: false
32
log_media: stdout
43
log_level: info
54
log_dir: /var/log/

docker/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ config.yaml) each time the container is run.
289289
| __Agent__ | | (these don't work with DISABLE_AGENT) |
290290
| `TYPE` | | [`Labels.type`](https://docs.crowdsec.net/Crowdsec/v1/references/acquisition/) for file in time-machine: `-e TYPE="<type>"` |
291291
| `DSN` | | Process a single source in time-machine: `-e DSN="file:///var/log/toto.log"` or `-e DSN="cloudwatch:///your/group/path:stream_name?profile=dev&backlog=16h"` or `-e DSN="journalctl://filters=_SYSTEMD_UNIT=ssh.service"` |
292+
| `UNREGISTER_ON_EXIT` | | Remove the agent from the LAPI when its container is stopped. |
292293
| | | |
293294
| __Bouncers__ | | |
294295
| `BOUNCER_KEY_<name>` | | Register a bouncer with the name `<name>` and a key equal to the value of the environment variable. |

0 commit comments

Comments
 (0)