Skip to content

Commit 0f46930

Browse files
authored
make: support CROWDSEC_API_DEV_ENV (#3931)
* make: support CROWDSEC_API_DEV_ENV * lint
1 parent 48bb5bf commit 0f46930

File tree

7 files changed

+51
-36
lines changed

7 files changed

+51
-36
lines changed

Makefile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,16 @@ ifneq (,$(DOCKER_BUILD))
7777
LD_OPTS_VARS += -X 'github.com/crowdsecurity/go-cs-lib/version.System=docker'
7878
endif
7979

80+
# If CROWDSEC_API_DEV_ENV is set to a truthy value, connect to dev.crowdsec.net
81+
ifeq ($(call bool,$(CROWDSEC_API_DEV_ENV)),1)
82+
LD_OPTS_VARS += \
83+
-X '$(GO_MODULE_NAME)/pkg/csconfig.PAPIBaseURl=https://papi.dev.crowdsec.net/' \
84+
-X '$(GO_MODULE_NAME)/cmd/crowdsec-cli/clicapi.CAPIBaseURL=https://api.dev.crowdsec.net/'
85+
$(info envvar CROWDSEC_API_DEV_ENV=$(CROWDSEC_API_DEV_ENV): If you are not working on CAPI/PAPI, this is probably a mistake.)
86+
$(info Note that the DEV environment is slow, unreliable and may fail at any time.)
87+
$(info )
88+
endif
89+
8090
#expr_debug tag is required to enable the debug mode in expr
8191
GO_TAGS := netgo,osusergo,sqlite_omit_load_extension,expr_debug
8292

cmd/crowdsec-cli/clicapi/capi.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ import (
2222
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
2323
"github.com/crowdsecurity/crowdsec/pkg/database"
2424
"github.com/crowdsecurity/crowdsec/pkg/models"
25-
"github.com/crowdsecurity/crowdsec/pkg/types"
2625
)
2726

27+
var CAPIBaseURL = "https://api.crowdsec.net/"
28+
2829
type configGetter = func() *csconfig.Config
2930

3031
type cliCapi struct {
@@ -65,9 +66,9 @@ func (cli *cliCapi) register(ctx context.Context, capiUserPrefix string, outputF
6566

6667
password := strfmt.Password(pstr)
6768

68-
apiurl, err := url.Parse(types.CAPIBaseURL)
69+
apiurl, err := url.Parse(CAPIBaseURL)
6970
if err != nil {
70-
return fmt.Errorf("unable to parse api url %s: %w", types.CAPIBaseURL, err)
71+
return fmt.Errorf("unable to parse api url %s: %w", CAPIBaseURL, err)
7172
}
7273

7374
_, err = apiclient.RegisterClient(ctx, &apiclient.Config{
@@ -77,7 +78,7 @@ func (cli *cliCapi) register(ctx context.Context, capiUserPrefix string, outputF
7778
VersionPrefix: "v3",
7879
}, nil)
7980
if err != nil {
80-
return fmt.Errorf("api client register ('%s'): %w", types.CAPIBaseURL, err)
81+
return fmt.Errorf("api client register ('%s'): %w", CAPIBaseURL, err)
8182
}
8283

8384
log.Infof("Successfully registered to Central API (CAPI)")
@@ -96,7 +97,7 @@ func (cli *cliCapi) register(ctx context.Context, capiUserPrefix string, outputF
9697
apiCfg := csconfig.ApiCredentialsCfg{
9798
Login: capiUser,
9899
Password: password.String(),
99-
URL: types.CAPIBaseURL,
100+
URL: CAPIBaseURL,
100101
}
101102

102103
apiConfigDump, err := yaml.Marshal(apiCfg)

cmd/crowdsec-cli/cliconsole/console.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
2828
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
2929
"github.com/crowdsecurity/crowdsec/pkg/csconfig"
30-
"github.com/crowdsecurity/crowdsec/pkg/types"
3130
)
3231

3332
type configGetter func() *csconfig.Config
@@ -52,6 +51,7 @@ func (cli *cliConsole) NewCommand() *cobra.Command {
5251
if err := require.LAPI(cfg); err != nil {
5352
return err
5453
}
54+
5555
if err := require.CAPI(cfg); err != nil {
5656
return err
5757
}
@@ -240,14 +240,17 @@ Enable given information push to the central API. Allows to empower the console`
240240
if err := cli.setConsoleOpts(csconfig.CONSOLE_CONFIGS, true); err != nil {
241241
return err
242242
}
243+
243244
log.Infof("All features have been enabled successfully")
244245
} else {
245246
if len(args) == 0 {
246247
return errors.New("you must specify at least one feature to enable")
247248
}
249+
248250
if err := cli.setConsoleOpts(args, true); err != nil {
249251
return err
250252
}
253+
251254
log.Infof("%v have been enabled", args)
252255
}
253256

@@ -279,14 +282,17 @@ Disable given information push to the central API.`,
279282
if err := cli.setConsoleOpts(csconfig.CONSOLE_CONFIGS, false); err != nil {
280283
return err
281284
}
285+
282286
log.Infof("All features have been disabled")
283287
} else {
284288
if len(args) == 0 {
285289
return errors.New("you must specify at least one feature to disable")
286290
}
291+
287292
if err := cli.setConsoleOpts(args, false); err != nil {
288293
return err
289294
}
295+
290296
log.Infof("%v have been disabled", args)
291297
}
292298

@@ -312,6 +318,7 @@ func (cli *cliConsole) newStatusCmd() *cobra.Command {
312318
RunE: func(_ *cobra.Command, _ []string) error {
313319
cfg := cli.cfg()
314320
consoleCfg := cfg.API.Server.ConsoleConfig
321+
315322
switch cfg.Cscli.Output {
316323
case "human":
317324
cmdConsoleStatusTable(color.Output, cfg.Cscli.Color, *consoleCfg)
@@ -323,13 +330,16 @@ func (cli *cliConsole) newStatusCmd() *cobra.Command {
323330
csconfig.SEND_CONTEXT: consoleCfg.ShareContext,
324331
csconfig.CONSOLE_MANAGEMENT: consoleCfg.ConsoleManagement,
325332
}
333+
326334
data, err := json.MarshalIndent(out, "", " ")
327335
if err != nil {
328336
return fmt.Errorf("failed to serialize configuration: %w", err)
329337
}
338+
330339
fmt.Fprintln(os.Stdout, string(data))
331340
case "raw":
332341
csvwriter := csv.NewWriter(os.Stdout)
342+
333343
err := csvwriter.Write([]string{"option", "enabled"})
334344
if err != nil {
335345
return err
@@ -348,6 +358,7 @@ func (cli *cliConsole) newStatusCmd() *cobra.Command {
348358
return err
349359
}
350360
}
361+
351362
csvwriter.Flush()
352363
}
353364

@@ -385,7 +396,7 @@ func (cli *cliConsole) setConsoleOpts(args []string, wanted bool) error {
385396
for _, arg := range args {
386397
switch arg {
387398
case csconfig.CONSOLE_MANAGEMENT:
388-
/*for each flag check if it's already set before setting it*/
399+
// for each flag check if it's already set before setting it
389400
if consoleCfg.ConsoleManagement != nil && *consoleCfg.ConsoleManagement == wanted {
390401
log.Debugf("%s already set to %t", csconfig.CONSOLE_MANAGEMENT, wanted)
391402
} else {
@@ -397,7 +408,7 @@ func (cli *cliConsole) setConsoleOpts(args []string, wanted bool) error {
397408
changed := false
398409
if wanted && cfg.API.Server.OnlineClient.Credentials.PapiURL == "" {
399410
changed = true
400-
cfg.API.Server.OnlineClient.Credentials.PapiURL = types.PAPIBaseURL
411+
cfg.API.Server.OnlineClient.Credentials.PapiURL = csconfig.PAPIBaseURL
401412
} else if !wanted && cfg.API.Server.OnlineClient.Credentials.PapiURL != "" {
402413
changed = true
403414
cfg.API.Server.OnlineClient.Credentials.PapiURL = ""
@@ -418,31 +429,31 @@ func (cli *cliConsole) setConsoleOpts(args []string, wanted bool) error {
418429
}
419430
}
420431
case csconfig.SEND_CUSTOM_SCENARIOS:
421-
/*for each flag check if it's already set before setting it*/
432+
// for each flag check if it's already set before setting it
422433
if consoleCfg.ShareCustomScenarios != nil && *consoleCfg.ShareCustomScenarios == wanted {
423434
log.Debugf("%s already set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
424435
} else {
425436
log.Infof("%s set to %t", csconfig.SEND_CUSTOM_SCENARIOS, wanted)
426437
consoleCfg.ShareCustomScenarios = ptr.Of(wanted)
427438
}
428439
case csconfig.SEND_TAINTED_SCENARIOS:
429-
/*for each flag check if it's already set before setting it*/
440+
// for each flag check if it's already set before setting it
430441
if consoleCfg.ShareTaintedScenarios != nil && *consoleCfg.ShareTaintedScenarios == wanted {
431442
log.Debugf("%s already set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
432443
} else {
433444
log.Infof("%s set to %t", csconfig.SEND_TAINTED_SCENARIOS, wanted)
434445
consoleCfg.ShareTaintedScenarios = ptr.Of(wanted)
435446
}
436447
case csconfig.SEND_MANUAL_SCENARIOS:
437-
/*for each flag check if it's already set before setting it*/
448+
// for each flag check if it's already set before setting it
438449
if consoleCfg.ShareManualDecisions != nil && *consoleCfg.ShareManualDecisions == wanted {
439450
log.Debugf("%s already set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
440451
} else {
441452
log.Infof("%s set to %t", csconfig.SEND_MANUAL_SCENARIOS, wanted)
442453
consoleCfg.ShareManualDecisions = ptr.Of(wanted)
443454
}
444455
case csconfig.SEND_CONTEXT:
445-
/*for each flag check if it's already set before setting it*/
456+
// for each flag check if it's already set before setting it
446457
if consoleCfg.ShareContext != nil && *consoleCfg.ShareContext == wanted {
447458
log.Debugf("%s already set to %t", csconfig.SEND_CONTEXT, wanted)
448459
} else {

pkg/apiserver/papi.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ import (
2323
)
2424

2525
const (
26-
SyncInterval = time.Second * 10
27-
PapiPullKey = "papi:last_pull"
26+
PAPIVersion = "v1"
27+
PAPIPollURL = "/decisions/stream/poll"
28+
PAPIPermissionsURL = "/permissions"
29+
SyncInterval = time.Second * 10
30+
PapiPullKey = "papi:last_pull"
2831
)
2932

3033
var operationMap = map[string]func(*Message, *Papi, bool) error{
@@ -89,11 +92,11 @@ func NewPAPI(apic *apic, dbClient *database.Client, consoleConfig *csconfig.Cons
8992
return &Papi{}, fmt.Errorf("creating papi logger: %w", err)
9093
}
9194

92-
papiUrl := *apic.apiClient.PapiURL
93-
papiUrl.Path = fmt.Sprintf("%s%s", types.PAPIVersion, types.PAPIPollUrl)
95+
papiURL := *apic.apiClient.PapiURL
96+
papiURL.Path = fmt.Sprintf("%s%s", PAPIVersion, PAPIPollURL)
9497

9598
longPollClient, err := longpollclient.NewLongPollClient(longpollclient.LongPollClientConfig{
96-
Url: papiUrl,
99+
Url: papiURL,
97100
Logger: logger,
98101
HttpClient: apic.apiClient.GetClient(),
99102
})
@@ -159,9 +162,9 @@ func (p *Papi) handleEvent(event longpollclient.Event, sync bool) error {
159162

160163
func (p *Papi) GetPermissions(ctx context.Context) (PapiPermCheckSuccess, error) {
161164
httpClient := p.apiClient.GetClient()
162-
papiCheckUrl := fmt.Sprintf("%s%s%s", p.URL, types.PAPIVersion, types.PAPIPermissionsUrl)
165+
papiCheckURL := fmt.Sprintf("%s%s%s", p.URL, PAPIVersion, PAPIPermissionsURL)
163166

164-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, papiCheckUrl, http.NoBody)
167+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, papiCheckURL, http.NoBody)
165168
if err != nil {
166169
return PapiPermCheckSuccess{}, fmt.Errorf("failed to create request: %w", err)
167170
}
@@ -230,9 +233,10 @@ func (p *Papi) PullOnce(ctx context.Context, since time.Time, sync bool) error {
230233
return nil
231234
}
232235

233-
// PullPAPI is the long polling client for real-time decisions from PAPI
236+
// Pull is the long polling client for real-time decisions from PAPI
234237
func (p *Papi) Pull(ctx context.Context) error {
235238
defer trace.CatchPanic("lapi/PullPAPI")
239+
236240
p.Logger.Infof("Starting Polling API Pull")
237241

238242
lastTimestamp := time.Time{}
@@ -322,7 +326,6 @@ func (p *Papi) Pull(ctx context.Context) error {
322326
cancel()
323327
}
324328
}
325-
326329
}
327330

328331
func (p *Papi) SyncDecisions(ctx context.Context) error {

pkg/csconfig/api.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ import (
2121
"github.com/crowdsecurity/go-cs-lib/ptr"
2222

2323
"github.com/crowdsecurity/crowdsec/pkg/apiclient"
24-
"github.com/crowdsecurity/crowdsec/pkg/types"
2524
)
2625

26+
var PAPIBaseURL = "https://papi.api.crowdsec.net/"
27+
2728
type APICfg struct {
2829
Client *LocalApiClientCfg `yaml:"client"`
2930
Server *LocalApiServerCfg `yaml:"server"`
@@ -127,7 +128,7 @@ func (o *OnlineApiClientCfg) Load() error {
127128
}
128129

129130
if o.Credentials != nil && o.Credentials.PapiURL == "" {
130-
o.Credentials.PapiURL = types.PAPIBaseURL
131+
o.Credentials.PapiURL = PAPIBaseURL
131132
}
132133

133134
return nil

pkg/csconfig/api_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import (
1313

1414
"github.com/crowdsecurity/go-cs-lib/cstest"
1515
"github.com/crowdsecurity/go-cs-lib/ptr"
16-
17-
"github.com/crowdsecurity/crowdsec/pkg/types"
1816
)
1917

2018
func TestLoadLocalApiClientCfg(t *testing.T) {
@@ -95,7 +93,7 @@ func TestLoadOnlineApiClientCfg(t *testing.T) {
9593
URL: "http://crowdsec.api",
9694
Login: "test",
9795
Password: "testpassword",
98-
PapiURL: types.PAPIBaseURL,
96+
PapiURL: PAPIBaseURL,
9997
},
10098
},
10199
{
@@ -214,7 +212,7 @@ func TestLoadAPIServer(t *testing.T) {
214212
URL: "http://crowdsec.api",
215213
Login: "test",
216214
Password: "testpassword",
217-
PapiURL: types.PAPIBaseURL,
215+
PapiURL: PAPIBaseURL,
218216
},
219217
Sharing: ptr.Of(true),
220218
PullConfig: CapiPullConfig{

pkg/types/constants.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ const (
66
PasswordAuthType = "password"
77
)
88

9-
const (
10-
PAPIBaseURL = "https://papi.api.crowdsec.net/"
11-
PAPIVersion = "v1"
12-
PAPIPollUrl = "/decisions/stream/poll"
13-
PAPIPermissionsUrl = "/permissions"
14-
)
15-
16-
const CAPIBaseURL = "https://api.crowdsec.net/"
17-
189
const (
1910
CscliOrigin = "cscli"
2011
CrowdSecOrigin = "crowdsec"

0 commit comments

Comments
 (0)