Skip to content

Commit 8e62bef

Browse files
committed
wip appsec tests
1 parent da75a03 commit 8e62bef

File tree

8 files changed

+41
-2
lines changed

8 files changed

+41
-2
lines changed

pkg/acquisition/acquisition.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ func DataSourceConfigure(
9090

9191
if lapiClientAware, ok := dataSrc.(types.LAPIClientAware); ok {
9292
cConfig := csconfig.GetConfig()
93+
if cConfig.API == nil {
94+
return nil, errors.New("crowdsec configuration not loaded while initializing appsec - this is a bug, plese report")
95+
}
9396
lapiClientAware.SetClientConfig(cConfig.API.Client)
9497
}
9598

pkg/acquisition/config_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
"github.com/stretchr/testify/assert"
1313
"github.com/stretchr/testify/require"
1414

15+
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
16+
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
1517
"github.com/crowdsecurity/crowdsec/pkg/metrics"
1618
)
1719

@@ -61,6 +63,13 @@ func TestParseSourceConfig(t *testing.T) {
6163
expectValid bool
6264
}
6365

66+
// load a configuration, appsec needs it
67+
_, _, err := csconfig.NewConfig("./testdata/config.yaml", false, false, true)
68+
require.NoError(t, err)
69+
70+
// load a hub, appsec needs it
71+
hub := cwhub.Hub{}
72+
6473
suites := []suite{
6574
{name: "valid", root: filepath.Join("testdata", "valid"), expectValid: true},
6675
{name: "invalid", root: filepath.Join("testdata", "invalid"), expectValid: false},
@@ -86,7 +95,7 @@ func TestParseSourceConfig(t *testing.T) {
8695

8796
if s.expectValid {
8897
require.False(t, hasWant, "valid config must not include # wantErr: directive")
89-
parsed, err := ParseSourceConfig(ctx, fileContent, metrics.AcquisitionMetricsLevelNone, nil)
98+
parsed, err := ParseSourceConfig(ctx, fileContent, metrics.AcquisitionMetricsLevelNone, &hub)
9099
require.NotNil(t, parsed)
91100
require.NoError(t, err)
92101
return
@@ -97,7 +106,7 @@ func TestParseSourceConfig(t *testing.T) {
97106
require.True(t, hasWant, "invalid config must include '# wantErr: <exact error>'")
98107
require.NotEmpty(t, wantErr, "wantErr directive found but empty")
99108

100-
parsed, err := ParseSourceConfig(ctx, fileContent, metrics.AcquisitionMetricsLevelNone, nil)
109+
parsed, err := ParseSourceConfig(ctx, fileContent, metrics.AcquisitionMetricsLevelNone, &hub)
101110
require.Nil(t, parsed)
102111
require.Error(t, err, "got no error, expected %q", wantErr)
103112
assert.Equal(t, wantErr, err.Error())

pkg/acquisition/modules/appsec/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ func (w *Source) Configure(_ context.Context, yamlConfig []byte, logger *log.Ent
128128
return fmt.Errorf("unable to parse appsec configuration: %w", err)
129129
}
130130

131+
if w.lapiClientConfig.Credentials == nil {
132+
return fmt.Errorf("missing lapi client credentials")
133+
}
134+
131135
w.lapiURL = fmt.Sprintf("%sv1/decisions/stream", w.lapiClientConfig.Credentials.URL)
132136
w.AuthCache = NewAuthCache()
133137

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# minimal configuration to satisfy appsec
2+
config_paths:
3+
data_dir: /path/to/data
4+
api:
5+
client:
6+
insecure_skip_verify: false
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# wantErr: datasource of type appsec: unable to parse appsec configuration: appsec_config or appsec_config_path must be set
2+
source: appsec
3+
labels:
4+
type: sometype
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# wantErr: datasource of type appsec: unable to parse appsec configuration: cannot parse appsec configuration: [6:6] cannot unmarshal map[string]interface {} into Go struct field Configuration.AppsecConfig of type string
2+
source: appsec
3+
labels:
4+
type: sometype
5+
appsec_config:
6+
foo: bar
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# wantErr: missing labels
2+
source: appsec
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# wantErr: datasource of type appsec: missing lapi client credentials
2+
source: appsec
3+
labels:
4+
type: sometype
5+
appsec_config: something

0 commit comments

Comments
 (0)