Skip to content

Commit b031ded

Browse files
committed
refact pkg/acquisition: http configuration
1 parent d88be56 commit b031ded

File tree

2 files changed

+47
-33
lines changed

2 files changed

+47
-33
lines changed

pkg/acquisition/modules/http/config.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
"os"
1111
"time"
1212

13+
yaml "github.com/goccy/go-yaml"
1314
log "github.com/sirupsen/logrus"
14-
"gopkg.in/yaml.v3"
1515

1616
"github.com/crowdsecurity/crowdsec/pkg/acquisition/configuration"
1717
"github.com/crowdsecurity/crowdsec/pkg/metrics"
@@ -34,6 +34,23 @@ type Configuration struct {
3434
configuration.DataSourceCommonCfg `yaml:",inline"`
3535
}
3636

37+
func ConfigurationFromYAML(y []byte) (Configuration, error) {
38+
var cfg Configuration
39+
40+
if err := yaml.UnmarshalWithOptions(y, &cfg, yaml.Strict()); err != nil {
41+
return cfg, fmt.Errorf("cannot parse: %s", yaml.FormatError(err, false, false))
42+
}
43+
44+
cfg.SetDefaults()
45+
46+
err := cfg.Validate()
47+
if err != nil {
48+
return cfg, err
49+
}
50+
51+
return cfg, nil
52+
}
53+
3754
type BasicAuthConfig struct {
3855
Username string `yaml:"username"`
3956
Password string `yaml:"password"`
@@ -46,18 +63,24 @@ type TLSConfig struct {
4663
CaCert string `yaml:"ca_cert"`
4764
}
4865

49-
func (s *Source) UnmarshalConfig(yamlConfig []byte) error {
50-
s.Config = Configuration{}
66+
func (c *Configuration) SetDefaults() {
67+
if c.Mode == "" {
68+
c.Mode = configuration.TAIL_MODE
69+
}
5170

52-
err := yaml.Unmarshal(yamlConfig, &s.Config)
53-
if err != nil {
54-
return fmt.Errorf("cannot parse %s datasource configuration: %w", s.GetName(), err)
71+
if c.Path == "" {
72+
c.Path = "/"
5573
}
74+
}
5675

57-
if s.Config.Mode == "" {
58-
s.Config.Mode = configuration.TAIL_MODE
76+
func (s *Source) UnmarshalConfig(yamlConfig []byte) error {
77+
cfg, err := ConfigurationFromYAML(yamlConfig)
78+
if err != nil {
79+
return err
5980
}
6081

82+
s.Config = cfg
83+
6184
return nil
6285
}
6386

@@ -66,10 +89,6 @@ func (c *Configuration) Validate() error {
6689
return errors.New("listen_addr or listen_socket is required")
6790
}
6891

69-
if c.Path == "" {
70-
c.Path = "/"
71-
}
72-
7392
if c.Path[0] != '/' {
7493
return errors.New("path must start with /")
7594
}
@@ -139,10 +158,6 @@ func (s *Source) Configure(_ context.Context, yamlConfig []byte, logger *log.Ent
139158
return err
140159
}
141160

142-
if err := s.Config.Validate(); err != nil {
143-
return fmt.Errorf("invalid configuration: %w", err)
144-
}
145-
146161
return nil
147162
}
148163

@@ -182,4 +197,3 @@ func (c *Configuration) NewTLSConfig() (*tls.Config, error) {
182197

183198
return &tlsConfig, nil
184199
}
185-

pkg/acquisition/modules/http/http_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,31 +42,31 @@ func TestConfigure(t *testing.T) {
4242
}{
4343
{
4444
config: `
45-
foobar: bla`,
46-
expectedErr: "invalid configuration: listen_addr or listen_socket is required",
45+
timeout: 1m`,
46+
expectedErr: "listen_addr or listen_socket is required",
4747
},
4848
{
4949
config: `
5050
source: http
5151
listen_addr: 127.0.0.1:8080
5252
path: wrongpath`,
53-
expectedErr: "invalid configuration: path must start with /",
53+
expectedErr: "path must start with /",
5454
},
5555
{
5656
config: `
5757
source: http
5858
listen_addr: 127.0.0.1:8080
5959
path: /test
6060
auth_type: basic_auth`,
61-
expectedErr: "invalid configuration: basic_auth is selected, but basic_auth is not provided",
61+
expectedErr: "basic_auth is selected, but basic_auth is not provided",
6262
},
6363
{
6464
config: `
6565
source: http
6666
listen_addr: 127.0.0.1:8080
6767
path: /test
6868
auth_type: headers`,
69-
expectedErr: "invalid configuration: headers is selected, but headers is not provided",
69+
expectedErr: "headers is selected, but headers is not provided",
7070
},
7171
{
7272
config: `
@@ -76,7 +76,7 @@ path: /test
7676
auth_type: basic_auth
7777
basic_auth:
7878
username: 132`,
79-
expectedErr: "invalid configuration: basic_auth is selected, but password is not provided",
79+
expectedErr: "basic_auth is selected, but password is not provided",
8080
},
8181
{
8282
config: `
@@ -86,7 +86,7 @@ path: /test
8686
auth_type: basic_auth
8787
basic_auth:
8888
password: 132`,
89-
expectedErr: "invalid configuration: basic_auth is selected, but username is not provided",
89+
expectedErr: "basic_auth is selected, but username is not provided",
9090
},
9191
{
9292
config: `
@@ -95,15 +95,15 @@ listen_addr: 127.0.0.1:8080
9595
path: /test
9696
auth_type: headers
9797
headers:`,
98-
expectedErr: "invalid configuration: headers is selected, but headers is not provided",
98+
expectedErr: "headers is selected, but headers is not provided",
9999
},
100100
{
101101
config: `
102102
source: http
103103
listen_addr: 127.0.0.1:8080
104104
path: /test
105105
auth_type: toto`,
106-
expectedErr: "invalid configuration: invalid auth_type: must be one of basic_auth, headers, mtls",
106+
expectedErr: "invalid auth_type: must be one of basic_auth, headers, mtls",
107107
},
108108
{
109109
config: `
@@ -115,7 +115,7 @@ headers:
115115
key: value
116116
tls:
117117
server_key: key`,
118-
expectedErr: "invalid configuration: server_cert is required",
118+
expectedErr: "server_cert is required",
119119
},
120120
{
121121
config: `
@@ -127,7 +127,7 @@ headers:
127127
key: value
128128
tls:
129129
server_cert: cert`,
130-
expectedErr: "invalid configuration: server_key is required",
130+
expectedErr: "server_key is required",
131131
},
132132
{
133133
config: `
@@ -138,7 +138,7 @@ auth_type: mtls
138138
tls:
139139
server_cert: cert
140140
server_key: key`,
141-
expectedErr: "invalid configuration: mtls is selected, but ca_cert is not provided",
141+
expectedErr: "mtls is selected, but ca_cert is not provided",
142142
},
143143
{
144144
config: `
@@ -149,7 +149,7 @@ auth_type: headers
149149
headers:
150150
key: value
151151
max_body_size: 0`,
152-
expectedErr: "invalid configuration: max_body_size must be positive",
152+
expectedErr: "max_body_size must be positive",
153153
},
154154
{
155155
config: `
@@ -160,7 +160,7 @@ auth_type: headers
160160
headers:
161161
key: value
162162
timeout: toto`,
163-
expectedErr: "cannot parse http datasource configuration: yaml: unmarshal errors:\n line 8: cannot unmarshal !!str `toto` into time.Duration",
163+
expectedErr: `cannot parse: time: invalid duration "toto"`,
164164
},
165165
{
166166
config: `
@@ -171,7 +171,7 @@ auth_type: headers
171171
headers:
172172
key: value
173173
custom_status_code: 999`,
174-
expectedErr: "invalid configuration: invalid HTTP status code",
174+
expectedErr: "invalid HTTP status code",
175175
},
176176
}
177177

@@ -199,7 +199,7 @@ source: http
199199
listen_addr: 127.0.0.1:8080
200200
path: 15
201201
auth_type: headers`))
202-
cstest.AssertErrorMessage(t, err, "cannot parse http datasource configuration: yaml: line 4: found a tab character that violates indentation")
202+
cstest.AssertErrorMessage(t, err, "cannot parse: [5:1] found character '\t' that cannot start any token")
203203
}
204204

205205
func TestGetMode(t *testing.T) {

0 commit comments

Comments
 (0)