Skip to content

Commit

Permalink
wip: expose worker options (#30)
Browse files Browse the repository at this point in the history
* wip: expose worker options

* wip: deps update

* wip: add additional config options

* fix: update deps

* fix: fix len append

* fix: fix namespace code for worker currently a hardcoded string which isnt great

* fix: another hard coded string which was missed

* fix: nil pointer and a basic test to ensure it doesnt happen again, there not alot of tests so hard to find anything out
  • Loading branch information
LaurenceJJones authored Jun 18, 2024
1 parent fd7e210 commit b36bc38
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 76 deletions.
10 changes: 5 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ func getConfigFromPath(configPath string) (*cfg.BouncerConfig, error) {
return conf, nil
}

func CloudflareManagersFromConfig(ctx context.Context, accountConfigs []cfg.AccountConfig) ([]*cf.CloudflareAccountManager, error) {
cfManagers := make([]*cf.CloudflareAccountManager, 0)
for _, accountCfg := range accountConfigs {
func CloudflareManagersFromConfig(ctx context.Context, config cfg.CloudflareConfig) ([]*cf.CloudflareAccountManager, error) {
cfManagers := make([]*cf.CloudflareAccountManager, 0, len(config.Accounts))
for _, accountCfg := range config.Accounts {
cfg := accountCfg
manager, err := cf.NewCloudflareManager(ctx, cfg)
manager, err := cf.NewCloudflareManager(ctx, cfg, &config.Worker)
if err != nil {
return nil, fmt.Errorf("unable to create cloudflare manager: %w", err)
}
Expand Down Expand Up @@ -165,7 +165,7 @@ func Execute(configTokens *string, configOutputPath *string, configPath *string,

rootCtx := context.Background()
g, ctx := errgroup.WithContext(rootCtx)
cfManagers, err := CloudflareManagersFromConfig(ctx, conf.CloudflareConfig.Accounts)
cfManagers, err := CloudflareManagersFromConfig(ctx, conf.CloudflareConfig)
if err != nil {
return err
}
Expand Down
14 changes: 7 additions & 7 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func runInfraTest(t *testing.T, m *cf.CloudflareAccountManager) error {
if err != nil {
return err
}
_, err = api.GetWorker(m.Ctx, cloudflare.AccountIdentifier(m.AccountCfg.ID), cf.ScriptName)
_, err = api.GetWorker(m.Ctx, cloudflare.AccountIdentifier(m.AccountCfg.ID), m.Worker.ScriptName)
if err != nil {
return err
}
Expand All @@ -43,7 +43,7 @@ func runInfraTest(t *testing.T, m *cf.CloudflareAccountManager) error {
}
foundRoute := false
for _, route := range routeResp.Routes {
if route.ScriptName == cf.ScriptName {
if route.ScriptName == m.Worker.ScriptName {
foundRoute = true
}
}
Expand All @@ -59,7 +59,7 @@ func runInfraTest(t *testing.T, m *cf.CloudflareAccountManager) error {

foundKVNamespace := false
for _, kvNamespace := range kvNamespaces {
if kvNamespace.Title != cf.KVNsName {
if kvNamespace.Title != m.Worker.KVNameSpaceName {
continue
}
foundKVNamespace = true
Expand Down Expand Up @@ -128,7 +128,7 @@ func runCleanUpTest(t *testing.T, m *cf.CloudflareAccountManager) error {
return err
}
err = api.DeleteWorker(m.Ctx, cloudflare.AccountIdentifier(m.AccountCfg.ID), cloudflare.DeleteWorkerParams{
ScriptName: cf.ScriptName,
ScriptName: m.Worker.ScriptName,
})
if err == nil || !strings.Contains(err.Error(), "workers.api.error.script_not_found") {
return fmt.Errorf("worker should not exist")
Expand All @@ -150,7 +150,7 @@ func runCleanUpTest(t *testing.T, m *cf.CloudflareAccountManager) error {
return err
}
for _, route := range routeResp.Routes {
if route.ScriptName == cf.ScriptName {
if route.ScriptName == m.Worker.ScriptName {
return fmt.Errorf("route should not exist")
}
}
Expand All @@ -162,7 +162,7 @@ func runCleanUpTest(t *testing.T, m *cf.CloudflareAccountManager) error {
}

for _, kvNamespace := range kvNamespaces {
if kvNamespace.Title == cf.KVNsName {
if kvNamespace.Title == m.Worker.KVNameSpaceName {
return fmt.Errorf("kv namespace should not exist")
}
}
Expand Down Expand Up @@ -318,7 +318,7 @@ func TestBouncer(t *testing.T) {
}

// test setup
managers, err := CloudflareManagersFromConfig(context.Background(), cfg.CloudflareConfig.Accounts)
managers, err := CloudflareManagersFromConfig(context.Background(), cfg.CloudflareConfig)
if err != nil {
t.Fatal(err)
}
Expand Down
60 changes: 32 additions & 28 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,35 @@ module github.com/crowdsecurity/crowdsec-cloudflare-worker-bouncer
go 1.21

require (
github.com/crowdsecurity/crowdsec v1.5.5
github.com/crowdsecurity/crowdsec v1.6.1
github.com/crowdsecurity/go-cs-bouncer v0.0.13
github.com/prometheus/client_golang v1.17.0
github.com/prometheus/client_golang v1.19.0
github.com/sirupsen/logrus v1.9.3
github.com/whuang8/redactrus v1.0.2
golang.org/x/sync v0.3.0
golang.org/x/sync v0.7.0
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

require (
github.com/antonmedv/expr v1.15.3 // indirect
github.com/antonmedv/expr v1.15.5 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/fatih/color v1.15.0 // indirect
github.com/go-openapi/analysis v0.21.4 // indirect
github.com/go-openapi/errors v0.20.4 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/loads v0.21.2 // indirect
github.com/go-openapi/spec v0.20.9 // indirect
github.com/go-openapi/strfmt v0.21.7 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-openapi/validate v0.22.1 // indirect
github.com/goccy/go-yaml v1.11.2 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/expr-lang/expr v1.16.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/go-openapi/analysis v0.23.0 // indirect
github.com/go-openapi/errors v0.22.0 // indirect
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/loads v0.22.0 // indirect
github.com/go-openapi/spec v0.21.0 // indirect
github.com/go-openapi/strfmt v0.23.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
github.com/go-openapi/validate v0.24.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-yaml v1.11.3 // indirect
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
Expand All @@ -36,25 +40,25 @@ require (
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
go.mongodb.org/mongo-driver v1.12.1 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.53.0 // indirect
github.com/prometheus/procfs v0.14.0 // indirect
go.mongodb.org/mongo-driver v1.15.0 // indirect
golang.org/x/xerrors v0.0.0-20231012003039-104605ab7028 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/tomb.v2 v2.0.0-20161208151619-d5d1b5820637 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

require (
github.com/cloudflare/cloudflare-go v0.70.0
github.com/crowdsecurity/go-cs-lib v0.0.5
github.com/cloudflare/cloudflare-go v0.94.0
github.com/crowdsecurity/go-cs-lib v0.0.10
github.com/google/go-querystring v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.4 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
gopkg.in/yaml.v3 v3.0.1
)
Loading

0 comments on commit b36bc38

Please sign in to comment.