Skip to content

Commit 922c5cd

Browse files
authored
update lint version, action and configuration; propagate context (#115)
1 parent 5f3770c commit 922c5cd

File tree

6 files changed

+41
-25
lines changed

6 files changed

+41
-25
lines changed

.github/workflows/lint.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ jobs:
3636
make build
3737
3838
- name: golangci-lint
39-
uses: golangci/golangci-lint-action@v7
39+
uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0
4040
with:
41-
version: v2.0
41+
version: v2.5
4242
args: --issues-exit-code=1 --timeout 10m
4343
only-new-issues: false
4444

.golangci.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ linters:
2424
- whitespace
2525
- wrapcheck
2626
- wsl
27+
- embeddedstructfieldcheck
28+
- noinlineerr
29+
- wsl_v5
30+
2731
settings:
2832

2933
depguard:
@@ -32,6 +36,13 @@ linters:
3236
deny:
3337
- pkg: github.com/pkg/errors
3438
desc: errors.Wrap() is deprecated in favor of fmt.Errorf()
39+
40+
gocritic:
41+
enable-all: true
42+
disabled-checks:
43+
- importShadow
44+
- sloppyReassign
45+
3546
govet:
3647
disable:
3748
- fieldalignment

cmd/root.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,29 @@ func HandleSignals(ctx context.Context) error {
5757
return nil
5858
}
5959

60-
func deleteDecisions(custom *custom.CustomBouncer, decisions []*models.Decision) {
60+
func deleteDecisions(ctx context.Context, custom *custom.CustomBouncer, decisions []*models.Decision) {
6161
if len(decisions) == 1 {
62-
log.Infof("deleting 1 decision")
62+
log.Info("deleting 1 decision")
6363
} else {
6464
log.Infof("deleting %d decisions", len(decisions))
6565
}
6666
for _, d := range decisions {
67-
if err := custom.Delete(d); err != nil {
67+
if err := custom.Delete(ctx, d); err != nil {
6868
log.Errorf("unable to delete decision for '%s': %s", *d.Value, err)
6969
continue
7070
}
7171
log.Debugf("deleted '%s'", *d.Value)
7272
}
7373
}
7474

75-
func addDecisions(custom *custom.CustomBouncer, decisions []*models.Decision) {
75+
func addDecisions(ctx context.Context, custom *custom.CustomBouncer, decisions []*models.Decision) {
7676
if len(decisions) == 1 {
7777
log.Info("adding 1 decision")
7878
} else {
7979
log.Infof("adding %d decisions", len(decisions))
8080
}
8181
for _, d := range decisions {
82-
if err := custom.Add(d); err != nil {
82+
if err := custom.Add(ctx, d); err != nil {
8383
log.Errorf("unable to insert decision for '%s': %s", *d.Value, err)
8484
continue
8585
}
@@ -236,13 +236,13 @@ func Execute() error {
236236
}
237237

238238
g.Go(func() error {
239-
log.Infof("Processing new and deleted decisions . . .")
239+
log.Info("Processing new and deleted decisions . . .")
240240
for {
241241
select {
242242
case <-ctx.Done():
243-
log.Infoln("terminating bouncer process")
243+
log.Info("terminating bouncer process")
244244
if config.PrometheusConfig.Enabled {
245-
log.Infoln("terminating prometheus server")
245+
log.Info("terminating prometheus server")
246246
if err := promServer.Shutdown(context.Background()); err != nil {
247247
log.Errorf("unable to shutdown prometheus server: %s", err)
248248
}
@@ -252,8 +252,8 @@ func Execute() error {
252252
if decisions == nil {
253253
continue
254254
}
255-
deleteDecisions(custom, decisions.Deleted)
256-
addDecisions(custom, decisions.New)
255+
deleteDecisions(ctx, custom, decisions.Deleted)
256+
addDecisions(ctx, custom, decisions.New)
257257
case <-cacheResetTicker.C:
258258
custom.ResetCache()
259259
}

pkg/cfg/config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,29 +52,29 @@ func NewConfig(reader io.Reader) (*BouncerConfig, error) {
5252

5353
fcontent, err := io.ReadAll(reader)
5454
if err != nil {
55-
return &BouncerConfig{}, err
55+
return nil, err
5656
}
5757

5858
err = yaml.Unmarshal(fcontent, &config)
5959
if err != nil {
60-
return &BouncerConfig{}, fmt.Errorf("failed to unmarshal: %w", err)
60+
return nil, fmt.Errorf("failed to unmarshal: %w", err)
6161
}
6262

6363
if err = config.Logging.setup("crowdsec-custom-bouncer.log"); err != nil {
64-
return &BouncerConfig{}, err
64+
return nil, err
6565
}
6666

6767
if config.BinPath == "" {
68-
return &BouncerConfig{}, errors.New("bin_path is not set")
68+
return nil, errors.New("bin_path is not set")
6969
}
7070

7171
_, err = os.Stat(config.BinPath)
7272
if os.IsNotExist(err) {
73-
return &BouncerConfig{}, fmt.Errorf("binary '%s' doesn't exist", config.BinPath)
73+
return nil, fmt.Errorf("binary '%s' doesn't exist", config.BinPath)
7474
}
7575

7676
if config.CacheRetentionDuration == 0 {
77-
log.Infof("cache_retention_duration defaults to 10 seconds")
77+
log.Info("cache_retention_duration defaults to 10 seconds")
7878
config.CacheRetentionDuration = 10 * time.Second
7979
}
8080

pkg/custom/custom.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package custom
22

33
import (
4+
"context"
45
"encoding/json"
56
"fmt"
67
"io"
@@ -56,7 +57,7 @@ func (c *CustomBouncer) Init() error {
5657
return nil
5758
}
5859

59-
func (c *CustomBouncer) Add(decision *models.Decision) error {
60+
func (c *CustomBouncer) Add(ctx context.Context, decision *models.Decision) error {
6061
if _, exists := c.newDecisionValueSet[decisionToDecisionKey(decision)]; exists {
6162
return nil
6263
}
@@ -79,15 +80,15 @@ func (c *CustomBouncer) Add(decision *models.Decision) error {
7980
c.newDecisionValueSet[decisionToDecisionKey(decision)] = struct{}{}
8081
return nil
8182
}
82-
cmd := exec.Command(c.Path, "add", *decision.Value, strconv.Itoa(int(banDuration.Seconds())), *decision.Scenario, str)
83+
cmd := exec.CommandContext(ctx, c.Path, "add", *decision.Value, strconv.Itoa(int(banDuration.Seconds())), *decision.Scenario, str)
8384
if out, err := cmd.CombinedOutput(); err != nil {
8485
log.Errorf("Error in 'add' command (%s): %v --> %s", cmd.String(), err, string(out))
8586
}
8687
c.newDecisionValueSet[decisionToDecisionKey(decision)] = struct{}{}
8788
return nil
8889
}
8990

90-
func (c *CustomBouncer) Delete(decision *models.Decision) error {
91+
func (c *CustomBouncer) Delete(ctx context.Context, decision *models.Decision) error {
9192
if _, exists := c.expiredDecisionValueSet[decisionToDecisionKey(decision)]; exists {
9293
return nil
9394
}
@@ -110,7 +111,7 @@ func (c *CustomBouncer) Delete(decision *models.Decision) error {
110111
log.Warningf("serialize: %s", err)
111112
}
112113
log.Debugf("custom [%s] : del ban on %s for %s sec (%s)", c.Path, *decision.Value, strconv.Itoa(int(banDuration.Seconds())), *decision.Scenario)
113-
cmd := exec.Command(c.Path, "del", *decision.Value, strconv.Itoa(int(banDuration.Seconds())), *decision.Scenario, str)
114+
cmd := exec.CommandContext(ctx, c.Path, "del", *decision.Value, strconv.Itoa(int(banDuration.Seconds())), *decision.Scenario, str)
114115
if out, err := cmd.CombinedOutput(); err != nil {
115116
log.Errorf("Error in 'del' command (%s): %v --> %s", cmd.String(), err, string(out))
116117
}

pkg/custom/custom_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func parseFile(path string) []parsedLine {
4040
panic(err)
4141
}
4242
for _, line := range strings.Split(string(dat), "\n") {
43-
if len(line) == 0 {
43+
if line == "" {
4444
continue
4545
}
4646

@@ -68,6 +68,8 @@ func cleanup() {
6868
}
6969

7070
func Test_CustomBouncer_Add(t *testing.T) {
71+
ctx := t.Context()
72+
7173
type args struct {
7274
Decisions []*models.Decision
7375
}
@@ -166,7 +168,7 @@ func Test_CustomBouncer_Add(t *testing.T) {
166168
}
167169
c.ResetCache()
168170
for _, decision := range tt.args.Decisions {
169-
err := c.Add(decision)
171+
err := c.Add(ctx, decision)
170172
if err != nil {
171173
t.Error(err)
172174
}
@@ -180,6 +182,8 @@ func Test_CustomBouncer_Add(t *testing.T) {
180182
}
181183

182184
func Test_CustomBouncer_Delete(t *testing.T) {
185+
ctx := t.Context()
186+
183187
type args struct {
184188
Decisions []*models.Decision
185189
}
@@ -278,7 +282,7 @@ func Test_CustomBouncer_Delete(t *testing.T) {
278282
}
279283
c.ResetCache()
280284
for _, decision := range tt.args.Decisions {
281-
err := c.Delete(decision)
285+
err := c.Delete(ctx, decision)
282286
if err != nil {
283287
t.Error(err)
284288
}

0 commit comments

Comments
 (0)