Skip to content

Commit 910a36b

Browse files
authored
lint update+formatting (#407)
1 parent 2f74b68 commit 910a36b

File tree

11 files changed

+105
-44
lines changed

11 files changed

+105
-44
lines changed

.golangci.yml

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ linters:
4545
enable-all: true
4646
disabled-checks:
4747
- appendCombine
48-
- commentFormatting
4948
- paramTypeCombine
5049
- sloppyReassign
5150
- unnamedResult
@@ -78,7 +77,9 @@ linters:
7877
- name: add-constant
7978
disabled: true
8079
- name: cognitive-complexity
81-
disabled: true
80+
arguments:
81+
# lower this after refactoring
82+
- 49
8283
- name: comment-spacings
8384
disabled: true
8485
- name: confusing-results
@@ -87,12 +88,13 @@ linters:
8788
arguments:
8889
# lower this after refactoring
8990
- 28
90-
- name: empty-lines
91-
disabled: true
9291
- name: flag-parameter
9392
disabled: true
9493
- name: function-length
95-
disabled: true
94+
arguments:
95+
# lower this after refactoring
96+
- 74
97+
- 149
9698
- name: import-alias-naming
9799
disabled: true
98100
- name: import-shadowing

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ func Execute() error {
235235
}
236236

237237
prometheus.MustRegister(csbouncer.TotalLAPICalls, csbouncer.TotalLAPIError)
238+
238239
if config.PrometheusConfig.Enabled {
239240
go func() {
240241
http.Handle("/metrics", mHandler.ComputeMetricsHandler(promhttp.Handler()))

pkg/dryrun/dryrun.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import (
99
"github.com/crowdsecurity/cs-firewall-bouncer/pkg/types"
1010
)
1111

12-
type dryRun struct {
13-
}
12+
type dryRun struct{}
1413

1514
func NewDryRun(_ *cfg.BouncerConfig) (types.Backend, error) {
1615
return &dryRun{}, nil

pkg/ipsetcmd/ipset.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func NewIPSet(setName string) (*IPSet, error) {
3030
if err != nil {
3131
return nil, errors.New("unable to find ipset")
3232
}
33+
3334
return &IPSet{
3435
binaryPath: ipsetBin,
3536
setName: setName,
@@ -72,6 +73,7 @@ func (i *IPSet) Add(entry string) error {
7273
cmd := exec.Command(i.binaryPath, "add", i.setName, entry)
7374

7475
log.Debugf("ipset add command: %v", cmd.String())
76+
7577
out, err := cmd.CombinedOutput()
7678
if err != nil {
7779
return fmt.Errorf("error creating ipset: %s", out)
@@ -84,6 +86,7 @@ func (i *IPSet) DeleteEntry(entry string) error {
8486
cmd := exec.Command(i.binaryPath, "del", i.setName, entry)
8587

8688
log.Debugf("ipset delete entry command: %v", cmd.String())
89+
8790
out, err := cmd.CombinedOutput()
8891
if err != nil {
8992
return fmt.Errorf("error creating ipset: %s", out)
@@ -96,6 +99,7 @@ func (i *IPSet) List() ([]string, error) {
9699
cmd := exec.Command(i.binaryPath, "list", i.setName)
97100

98101
log.Debugf("ipset list command: %v", cmd.String())
102+
99103
out, err := cmd.CombinedOutput()
100104
if err != nil {
101105
return nil, fmt.Errorf("error listing ipset: %s", out)
@@ -108,6 +112,7 @@ func (i *IPSet) Flush() error {
108112
cmd := exec.Command(i.binaryPath, "flush", i.setName)
109113

110114
log.Debugf("ipset flush command: %v", cmd.String())
115+
111116
out, err := cmd.CombinedOutput()
112117
if err != nil {
113118
return fmt.Errorf("error flushing ipset: %s", out)
@@ -120,6 +125,7 @@ func (i *IPSet) Destroy() error {
120125
cmd := exec.Command(i.binaryPath, "destroy", i.setName)
121126

122127
log.Debugf("ipset destroy command: %v", cmd.String())
128+
123129
out, err := cmd.CombinedOutput()
124130
if err != nil {
125131
return fmt.Errorf("error destroying ipset: %s", out)
@@ -132,6 +138,7 @@ func (i *IPSet) Rename(toSetName string) error {
132138
cmd := exec.Command(i.binaryPath, "rename", i.setName, toSetName)
133139

134140
log.Debugf("ipset rename command: %v", cmd.String())
141+
135142
out, err := cmd.CombinedOutput()
136143
if err != nil {
137144
return fmt.Errorf("error renaming ipset: %s", out)
@@ -146,6 +153,7 @@ func (i *IPSet) Test(entry string) error {
146153
cmd := exec.Command(i.binaryPath, "test", i.setName, entry)
147154

148155
log.Debugf("ipset test command: %v", cmd.String())
156+
149157
out, err := cmd.CombinedOutput()
150158
if err != nil {
151159
return fmt.Errorf("error testing ipset: %s", out)
@@ -158,17 +166,20 @@ func (i *IPSet) Save() ([]string, error) {
158166
cmd := exec.Command(i.binaryPath, "save", i.setName)
159167

160168
log.Debugf("ipset save command: %v", cmd.String())
169+
161170
out, err := cmd.CombinedOutput()
162171
if err != nil {
163172
return nil, fmt.Errorf("error saving ipset: %s", out)
164173
}
174+
165175
return strings.Split(string(out), "\n"), nil
166176
}
167177

168178
func (i *IPSet) Restore(filename string) error {
169179
cmd := exec.Command(i.binaryPath, "restore", "-file", filename)
170180

171181
log.Debugf("ipset restore command: %v", cmd.String())
182+
172183
out, err := cmd.CombinedOutput()
173184
if err != nil {
174185
return fmt.Errorf("error restoring ipset: %s", out)
@@ -181,6 +192,7 @@ func (i *IPSet) Swap(toSetName string) error {
181192
cmd := exec.Command(i.binaryPath, "swap", i.setName, toSetName)
182193

183194
log.Debugf("ipset swap command: %v", cmd.String())
195+
184196
out, err := cmd.CombinedOutput()
185197
if err != nil {
186198
return fmt.Errorf("error swapping ipset: %s", out)
@@ -207,6 +219,7 @@ func (i *IPSet) Len() int {
207219
cmd := exec.Command(i.binaryPath, "list", i.setName)
208220

209221
log.Debugf("ipset list command: %v", cmd.String())
222+
210223
out, err := cmd.CombinedOutput()
211224
if err != nil {
212225
return 0
@@ -238,6 +251,7 @@ func GetSetsStartingWith(name string) (map[string]*IPSet, error) {
238251
cmd := exec.Command(ipsetBinary, "list", "-name")
239252

240253
log.Debugf("ipset list command: %v", cmd.String())
254+
241255
out, err := cmd.CombinedOutput()
242256
if err != nil {
243257
return nil, fmt.Errorf("error listing ipset: %s", out)

pkg/iptables/iptables.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ type iptables struct {
3030

3131
func NewIPTables(config *cfg.BouncerConfig) (types.Backend, error) {
3232
var err error
33+
3334
ret := &iptables{}
3435

3536
defaultSet, err := ipsetcmd.NewIPSet("")
@@ -87,10 +88,12 @@ func NewIPTables(config *cfg.BouncerConfig) (types.Backend, error) {
8788

8889
if config.Mode == cfg.IpsetMode {
8990
ipv4Ctx.ipsetContentOnly = true
91+
9092
set, err := ipsetcmd.NewIPSet(config.BlacklistsIpv4)
9193
if err != nil {
9294
return nil, err
9395
}
96+
9497
v4Sets["ipset"] = set
9598
} else {
9699
ipv4Ctx.iptablesBin, err = exec.LookPath("iptables")
@@ -108,6 +111,7 @@ func NewIPTables(config *cfg.BouncerConfig) (types.Backend, error) {
108111

109112
ipv4Ctx.ipsets = v4Sets
110113
ret.v4 = ipv4Ctx
114+
111115
if config.DisableIPV6 {
112116
return ret, nil
113117
}
@@ -119,10 +123,12 @@ func NewIPTables(config *cfg.BouncerConfig) (types.Backend, error) {
119123

120124
if config.Mode == cfg.IpsetMode {
121125
ipv6Ctx.ipsetContentOnly = true
126+
122127
set, err := ipsetcmd.NewIPSet(config.BlacklistsIpv6)
123128
if err != nil {
124129
return nil, err
125130
}
131+
126132
v6Sets["ipset"] = set
127133
} else {
128134
ipv6Ctx.iptablesBin, err = exec.LookPath("ip6tables")
@@ -198,10 +204,12 @@ func (ipt *iptables) Add(decision *models.Decision) error {
198204
log.Debugf("not adding '%s' because ipv6 is disabled", *decision.Value)
199205
return nil
200206
}
207+
201208
ipt.v6.add(decision)
202209
} else {
203210
ipt.v4.add(decision)
204211
}
212+
205213
return nil
206214
}
207215

pkg/iptables/iptables_context.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
log "github.com/sirupsen/logrus"
1515

1616
"github.com/crowdsecurity/crowdsec/pkg/models"
17+
1718
"github.com/crowdsecurity/cs-firewall-bouncer/pkg/ipsetcmd"
1819
)
1920

@@ -67,7 +68,6 @@ func (ctx *ipTablesContext) setupChain() {
6768
}
6869

6970
for _, chain := range ctx.Chains {
70-
7171
cmd = []string{"-I", chain, "-j", chainName}
7272

7373
c = exec.Command(ctx.iptablesBin, cmd...)
@@ -120,7 +120,6 @@ func (ctx *ipTablesContext) setupChain() {
120120

121121
func (ctx *ipTablesContext) deleteChain() {
122122
for _, chain := range ctx.Chains {
123-
124123
cmd := []string{"-D", chain, "-j", chainName}
125124

126125
c := exec.Command(ctx.iptablesBin, cmd...)
@@ -212,9 +211,10 @@ func (ctx *ipTablesContext) commit() error {
212211
}()
213212

214213
for _, decision := range ctx.toDel {
215-
216-
var set *ipsetcmd.IPSet
217-
var ok bool
214+
var (
215+
set *ipsetcmd.IPSet
216+
ok bool
217+
)
218218

219219
// Decisions coming from lists will have "lists" as origin, and the scenario will be the list name
220220
// We use those to build a custom origin because we want to track metrics per list
@@ -252,8 +252,10 @@ func (ctx *ipTablesContext) commit() error {
252252
continue
253253
}
254254

255-
var set *ipsetcmd.IPSet
256-
var ok bool
255+
var (
256+
set *ipsetcmd.IPSet
257+
ok bool
258+
)
257259

258260
if banDuration.Seconds() > maxBanSeconds {
259261
log.Warnf("Ban duration too long (%d seconds), maximum for ipset is %d, setting duration to %d", int(banDuration.Seconds()), maxBanSeconds, maxBanSeconds-1)
@@ -272,7 +274,6 @@ func (ctx *ipTablesContext) commit() error {
272274
set, ok = ctx.ipsets[origin]
273275

274276
if !ok {
275-
276277
idx := slices.Index(ctx.originSetMapping, origin)
277278

278279
if idx == -1 {

0 commit comments

Comments
 (0)