Skip to content

Commit bbc8d7f

Browse files
authored
refact apiclient.Config: remove field Scenarios (#3622)
1 parent 3664d6f commit bbc8d7f

File tree

7 files changed

+43
-32
lines changed

7 files changed

+43
-32
lines changed

cmd/crowdsec-cli/clicapi/capi.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,6 @@ func queryCAPIStatus(ctx context.Context, db *database.Client, hub *cwhub.Hub, c
173173
client, err := apiclient.NewClient(&apiclient.Config{
174174
MachineID: login,
175175
Password: passwd,
176-
Scenarios: itemsForAPI,
177176
URL: apiURL,
178177
// I don't believe papi is neede to check enrollement
179178
// PapiURL: papiURL,

cmd/crowdsec-cli/cliconsole/console.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"net/http"
1010
"net/url"
1111
"os"
12-
"strconv"
1312
"slices"
13+
"strconv"
1414
"strings"
1515

1616
"github.com/fatih/color"
@@ -85,9 +85,11 @@ func (cli *cliConsole) enroll(ctx context.Context, key string, name string, over
8585
c, _ := apiclient.NewClient(&apiclient.Config{
8686
MachineID: cli.cfg().API.Server.OnlineClient.Credentials.Login,
8787
Password: password,
88-
Scenarios: hub.GetInstalledListForAPI(),
8988
URL: apiURL,
9089
VersionPrefix: "v3",
90+
UpdateScenario: func(_ context.Context) ([]string, error) {
91+
return hub.GetInstalledListForAPI(), nil
92+
},
9193
})
9294

9395
resp, err := c.Auth.EnrollWatcher(ctx, key, name, tags, overwrite)
@@ -157,12 +159,14 @@ func optionFilterDisable(opts []string, disableOpts []string) ([]string, error)
157159
// discard all elements == opt
158160

159161
j := 0
162+
160163
for _, o := range opts {
161164
if o != opt {
162165
opts[j] = o
163166
j++
164167
}
165168
}
169+
166170
opts = opts[:j]
167171
}
168172

@@ -323,7 +327,7 @@ func (cli *cliConsole) newStatusCmd() *cobra.Command {
323327
if err != nil {
324328
return fmt.Errorf("failed to serialize configuration: %w", err)
325329
}
326-
fmt.Println(string(data))
330+
fmt.Fprintln(os.Stdout, string(data))
327331
case "raw":
328332
csvwriter := csv.NewWriter(os.Stdout)
329333
err := csvwriter.Write([]string{"option", "enabled"})

pkg/apiclient/auth_service_test.go

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package apiclient
22

33
import (
44
"bytes"
5+
"context"
56
"encoding/json"
67
"fmt"
78
"io"
@@ -72,6 +73,7 @@ func initBasicMuxMock(t *testing.T, mux *http.ServeMux, path string) {
7273
*/
7374
func TestWatcherRegister(t *testing.T) {
7475
ctx := t.Context()
76+
7577
log.SetLevel(log.DebugLevel)
7678

7779
mux, urlx, teardown := setup()
@@ -110,6 +112,7 @@ func TestWatcherRegister(t *testing.T) {
110112

111113
func TestWatcherAuth(t *testing.T) {
112114
ctx := t.Context()
115+
113116
log.SetLevel(log.DebugLevel)
114117

115118
mux, urlx, teardown := setup()
@@ -122,22 +125,29 @@ func TestWatcherAuth(t *testing.T) {
122125
apiURL, err := url.Parse(urlx + "/")
123126
require.NoError(t, err)
124127

128+
updateScenario := func(_ context.Context) ([]string, error) {
129+
return []string{"crowdsecurity/test"}, nil
130+
}
131+
125132
// ok auth
126133
clientConfig := &Config{
127-
MachineID: "test_login",
128-
Password: "test_password",
129-
URL: apiURL,
130-
VersionPrefix: "v1",
131-
Scenarios: []string{"crowdsecurity/test"},
134+
MachineID: "test_login",
135+
Password: "test_password",
136+
URL: apiURL,
137+
VersionPrefix: "v1",
138+
UpdateScenario: updateScenario,
132139
}
133140

134141
client, err := NewClient(clientConfig)
135142
require.NoError(t, err)
136143

144+
scenarios, err := clientConfig.UpdateScenario(ctx)
145+
require.NoError(t, err)
146+
137147
_, _, err = client.Auth.AuthenticateWatcher(ctx, models.WatcherAuthRequest{
138148
MachineID: &clientConfig.MachineID,
139149
Password: &clientConfig.Password,
140-
Scenarios: clientConfig.Scenarios,
150+
Scenarios: scenarios,
141151
})
142152
require.NoError(t, err)
143153

@@ -171,6 +181,7 @@ func TestWatcherAuth(t *testing.T) {
171181

172182
func TestWatcherUnregister(t *testing.T) {
173183
ctx := t.Context()
184+
174185
log.SetLevel(log.DebugLevel)
175186

176187
mux, urlx, teardown := setup()
@@ -205,12 +216,16 @@ func TestWatcherUnregister(t *testing.T) {
205216
apiURL, err := url.Parse(urlx + "/")
206217
require.NoError(t, err)
207218

219+
updateScenario := func(_ context.Context) ([]string, error) {
220+
return []string{"crowdsecurity/test"}, nil
221+
}
222+
208223
mycfg := &Config{
209-
MachineID: "test_login",
210-
Password: "test_password",
211-
URL: apiURL,
212-
VersionPrefix: "v1",
213-
Scenarios: []string{"crowdsecurity/test"},
224+
MachineID: "test_login",
225+
Password: "test_password",
226+
URL: apiURL,
227+
VersionPrefix: "v1",
228+
UpdateScenario: updateScenario,
214229
}
215230

216231
client, err := NewClient(mycfg)
@@ -224,6 +239,7 @@ func TestWatcherUnregister(t *testing.T) {
224239

225240
func TestWatcherEnroll(t *testing.T) {
226241
ctx := t.Context()
242+
227243
log.SetLevel(log.DebugLevel)
228244

229245
mux, urlx, teardown := setup()
@@ -260,12 +276,16 @@ func TestWatcherEnroll(t *testing.T) {
260276
apiURL, err := url.Parse(urlx + "/")
261277
require.NoError(t, err)
262278

279+
updateScenario := func(_ context.Context) ([]string, error) {
280+
return []string{"crowdsecurity/test"}, nil
281+
}
282+
263283
mycfg := &Config{
264-
MachineID: "test_login",
265-
Password: "test_password",
266-
URL: apiURL,
267-
VersionPrefix: "v1",
268-
Scenarios: []string{"crowdsecurity/test"},
284+
MachineID: "test_login",
285+
Password: "test_password",
286+
URL: apiURL,
287+
VersionPrefix: "v1",
288+
UpdateScenario: updateScenario,
269289
}
270290

271291
client, err := NewClient(mycfg)

pkg/apiclient/client.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ func InitLAPIClient(ctx context.Context, apiUrl string, papiUrl string, login st
8787
client, err := NewClient(&Config{
8888
MachineID: login,
8989
Password: pwd,
90-
Scenarios: scenarios,
9190
URL: apiURL,
9291
PapiURL: papiURL,
9392
VersionPrefix: "v1",
@@ -138,7 +137,6 @@ func NewClient(config *Config) (*ApiClient, error) {
138137
t := &JWTTransport{
139138
MachineID: &config.MachineID,
140139
Password: &config.Password,
141-
Scenarios: config.Scenarios,
142140
UserAgent: userAgent,
143141
VersionPrefix: config.VersionPrefix,
144142
UpdateScenario: config.UpdateScenario,

pkg/apiclient/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
type Config struct {
1111
MachineID string
1212
Password strfmt.Password
13-
Scenarios []string
1413
URL *url.URL
1514
PapiURL *url.URL
1615
VersionPrefix string

pkg/apiserver/apic.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ type apic struct {
6767
metricsTomb tomb.Tomb
6868
startup bool
6969
credentials *csconfig.ApiCredentialsCfg
70-
scenarioList []string
7170
consoleConfig *csconfig.ConsoleConfig
7271
isPulling chan bool
7372
whitelists *csconfig.CapiWhitelist
@@ -196,7 +195,6 @@ func NewAPIC(ctx context.Context, config *csconfig.OnlineApiClientCfg, dbClient
196195
pullTomb: tomb.Tomb{},
197196
pushTomb: tomb.Tomb{},
198197
metricsTomb: tomb.Tomb{},
199-
scenarioList: make([]string, 0),
200198
consoleConfig: consoleConfig,
201199
pullInterval: pullIntervalDefault,
202200
pullIntervalFirst: randomDuration(pullIntervalDefault, pullIntervalDelta),
@@ -223,18 +221,12 @@ func NewAPIC(ctx context.Context, config *csconfig.OnlineApiClientCfg, dbClient
223221
return nil, fmt.Errorf("while parsing '%s': %w", config.Credentials.PapiURL, err)
224222
}
225223

226-
ret.scenarioList, err = ret.FetchScenariosListFromDB(ctx)
227-
if err != nil {
228-
return nil, fmt.Errorf("while fetching scenarios from db: %w", err)
229-
}
230-
231224
ret.apiClient, err = apiclient.NewClient(&apiclient.Config{
232225
MachineID: config.Credentials.Login,
233226
Password: strfmt.Password(config.Credentials.Password),
234227
URL: apiURL,
235228
PapiURL: papiURL,
236229
VersionPrefix: "v3",
237-
Scenarios: ret.scenarioList,
238230
UpdateScenario: ret.FetchScenariosListFromDB,
239231
})
240232
if err != nil {

pkg/apiserver/apic_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ func getAPIC(t *testing.T, ctx context.Context) *apic {
6363
pullTomb: tomb.Tomb{},
6464
pushTomb: tomb.Tomb{},
6565
metricsTomb: tomb.Tomb{},
66-
scenarioList: make([]string, 0),
6766
consoleConfig: &csconfig.ConsoleConfig{
6867
ShareManualDecisions: ptr.Of(false),
6968
ShareTaintedScenarios: ptr.Of(false),

0 commit comments

Comments
 (0)