Skip to content

Commit dc188f5

Browse files
authored
config: apply variable expansion to all keys (#102)
* config: apply variable expansion to all keys * lint
1 parent 049abe5 commit dc188f5

File tree

3 files changed

+20
-18
lines changed

3 files changed

+20
-18
lines changed

cmd/root.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
package cmd
22

33
import (
4-
"bytes"
54
"context"
5+
"errors"
66
"flag"
77
"fmt"
88
"net"
99
"net/http"
1010
"os"
1111
"os/exec"
1212
"os/signal"
13+
"strings"
1314
"syscall"
1415
"time"
1516

@@ -21,6 +22,7 @@ import (
2122

2223
"github.com/crowdsecurity/crowdsec/pkg/models"
2324
csbouncer "github.com/crowdsecurity/go-cs-bouncer"
25+
"github.com/crowdsecurity/go-cs-lib/csstring"
2426
"github.com/crowdsecurity/go-cs-lib/version"
2527

2628
"github.com/crowdsecurity/cs-custom-bouncer/pkg/cfg"
@@ -44,9 +46,9 @@ func HandleSignals(ctx context.Context) error {
4446
case s := <-signalChan:
4547
switch s {
4648
case syscall.SIGTERM:
47-
return fmt.Errorf("received SIGTERM")
49+
return errors.New("received SIGTERM")
4850
case os.Interrupt: // cross-platform SIGINT
49-
return fmt.Errorf("received interrupt")
51+
return errors.New("received interrupt")
5052
}
5153
case <-ctx.Done():
5254
return ctx.Err()
@@ -111,7 +113,7 @@ func feedViaStdin(ctx context.Context, custom *custom.CustomBouncer, config *cfg
111113
log.Errorf("Binary exited (retry %d/%d): %s", i, config.TotalRetries, err)
112114
}
113115
}
114-
return fmt.Errorf("maximum retries exceeded for binary. Exiting")
116+
return errors.New("maximum retries exceeded for binary. Exiting")
115117
}
116118

117119
func Execute() error {
@@ -130,20 +132,22 @@ func Execute() error {
130132
}
131133

132134
if configPath == nil || *configPath == "" {
133-
return fmt.Errorf("configuration file is required")
135+
return errors.New("configuration file is required")
134136
}
135137

136-
configBytes, err := cfg.MergedConfig(*configPath)
138+
configMerged, err := cfg.MergedConfig(*configPath)
137139
if err != nil {
138140
return fmt.Errorf("unable to read config file: %w", err)
139141
}
140142

141143
if *showConfig {
142-
fmt.Println(string(configBytes))
144+
fmt.Println(string(configMerged))
143145
return nil
144146
}
145147

146-
config, err := cfg.NewConfig(bytes.NewReader(configBytes))
148+
configExpanded := csstring.StrictExpand(string(configMerged), os.LookupEnv)
149+
150+
config, err := cfg.NewConfig(strings.NewReader(configExpanded))
147151
if err != nil {
148152
return fmt.Errorf("unable to load configuration: %w", err)
149153
}
@@ -173,7 +177,7 @@ func Execute() error {
173177
bouncer := &csbouncer.StreamBouncer{}
174178
bouncer.UserAgent = fmt.Sprintf("%s/%s", name, version.String())
175179

176-
err = bouncer.ConfigReader(bytes.NewReader(configBytes))
180+
err = bouncer.ConfigReader(strings.NewReader(configExpanded))
177181
if err != nil {
178182
return fmt.Errorf("unable to configure bouncer: %w", err)
179183
}
@@ -187,7 +191,7 @@ func Execute() error {
187191

188192
g.Go(func() error {
189193
bouncer.Run(ctx)
190-
return fmt.Errorf("bouncer stream halted")
194+
return errors.New("bouncer stream halted")
191195
})
192196

193197
if config.PrometheusConfig.Enabled {

pkg/cfg/config.go

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

33
import (
4+
"errors"
45
"fmt"
56
"io"
67
"os"
@@ -9,7 +10,6 @@ import (
910
log "github.com/sirupsen/logrus"
1011
"gopkg.in/yaml.v2"
1112

12-
"github.com/crowdsecurity/go-cs-lib/csstring"
1313
"github.com/crowdsecurity/go-cs-lib/yamlpatch"
1414
)
1515

@@ -55,19 +55,17 @@ func NewConfig(reader io.Reader) (*BouncerConfig, error) {
5555
return &BouncerConfig{}, err
5656
}
5757

58-
configBuff := csstring.StrictExpand(string(fcontent), os.LookupEnv)
59-
60-
err = yaml.Unmarshal([]byte(configBuff), &config)
58+
err = yaml.Unmarshal(fcontent, &config)
6159
if err != nil {
6260
return &BouncerConfig{}, fmt.Errorf("failed to unmarshal: %w", err)
6361
}
6462

65-
if err := config.Logging.setup("crowdsec-custom-bouncer.log"); err != nil {
63+
if err = config.Logging.setup("crowdsec-custom-bouncer.log"); err != nil {
6664
return &BouncerConfig{}, err
6765
}
6866

6967
if config.BinPath == "" {
70-
return &BouncerConfig{}, fmt.Errorf("bin_path is not set")
68+
return &BouncerConfig{}, errors.New("bin_path is not set")
7169
}
7270

7371
_, err = os.Stat(config.BinPath)

pkg/cfg/logging.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cfg
22

33
import (
4-
"fmt"
4+
"errors"
55
"io"
66
"os"
77
"path/filepath"
@@ -75,7 +75,7 @@ func (c *LoggingConfig) setDefaults() {
7575

7676
func (c *LoggingConfig) validate() error {
7777
if c.LogMode != "stdout" && c.LogMode != "file" {
78-
return fmt.Errorf("log_mode should be either 'stdout' or 'file'")
78+
return errors.New("log_mode should be either 'stdout' or 'file'")
7979
}
8080
return nil
8181
}

0 commit comments

Comments
 (0)