Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context propagation: pass context to NewAPIC() #3231

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions cmd/crowdsec-cli/clipapi/papi.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
func (cli *cliPapi) Status(ctx context.Context, out io.Writer, db *database.Client) error {
cfg := cli.cfg()

apic, err := apiserver.NewAPIC(cfg.API.Server.OnlineClient, db, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
apic, err := apiserver.NewAPIC(ctx, cfg.API.Server.OnlineClient, db, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
if err != nil {
return fmt.Errorf("unable to initialize API client: %w", err)
}
Expand Down Expand Up @@ -118,11 +118,11 @@
return cmd
}

func (cli *cliPapi) sync(out io.Writer, db *database.Client) error {
func (cli *cliPapi) sync(ctx context.Context, out io.Writer, db *database.Client) error {

Check warning on line 121 in cmd/crowdsec-cli/clipapi/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clipapi/papi.go#L121

Added line #L121 was not covered by tests
cfg := cli.cfg()
t := tomb.Tomb{}

apic, err := apiserver.NewAPIC(cfg.API.Server.OnlineClient, db, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)
apic, err := apiserver.NewAPIC(ctx, cfg.API.Server.OnlineClient, db, cfg.API.Server.ConsoleConfig, cfg.API.Server.CapiWhitelists)

Check warning on line 125 in cmd/crowdsec-cli/clipapi/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clipapi/papi.go#L125

Added line #L125 was not covered by tests
if err != nil {
return fmt.Errorf("unable to initialize API client: %w", err)
}
Expand Down Expand Up @@ -159,12 +159,14 @@
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, _ []string) error {
cfg := cli.cfg()
db, err := require.DBClient(cmd.Context(), cfg.DbConfig)
ctx := cmd.Context()

db, err := require.DBClient(ctx, cfg.DbConfig)

Check warning on line 164 in cmd/crowdsec-cli/clipapi/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clipapi/papi.go#L162-L164

Added lines #L162 - L164 were not covered by tests
if err != nil {
return err
}

return cli.sync(color.Output, db)
return cli.sync(ctx, color.Output, db)

Check warning on line 169 in cmd/crowdsec-cli/clipapi/papi.go

View check run for this annotation

Codecov / codecov/patch

cmd/crowdsec-cli/clipapi/papi.go#L169

Added line #L169 was not covered by tests
},
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/apiserver/apic.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func alertToSignal(alert *models.Alert, scenarioTrust string, shareContext bool)
return signal
}

func NewAPIC(config *csconfig.OnlineApiClientCfg, dbClient *database.Client, consoleConfig *csconfig.ConsoleConfig, apicWhitelist *csconfig.CapiWhitelist) (*apic, error) {
func NewAPIC(ctx context.Context, config *csconfig.OnlineApiClientCfg, dbClient *database.Client, consoleConfig *csconfig.ConsoleConfig, apicWhitelist *csconfig.CapiWhitelist) (*apic, error) {
var err error

ret := &apic{
Expand Down Expand Up @@ -237,7 +237,7 @@ func NewAPIC(config *csconfig.OnlineApiClientCfg, dbClient *database.Client, con
return ret, fmt.Errorf("get scenario in db: %w", err)
}

authResp, _, err := ret.apiClient.Auth.AuthenticateWatcher(context.Background(), models.WatcherAuthRequest{
authResp, _, err := ret.apiClient.Auth.AuthenticateWatcher(ctx, models.WatcherAuthRequest{
MachineID: &config.Credentials.Login,
Password: &password,
Scenarios: scenarios,
Expand Down
4 changes: 3 additions & 1 deletion pkg/apiserver/apic_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ func TestNewAPIC(t *testing.T) {
},
}

ctx := context.Background()

for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
setConfig()
Expand All @@ -246,7 +248,7 @@ func TestNewAPIC(t *testing.T) {
),
))
tc.action()
_, err := NewAPIC(testConfig, tc.args.dbClient, tc.args.consoleConfig, nil)
_, err := NewAPIC(ctx, testConfig, tc.args.dbClient, tc.args.consoleConfig, nil)
cstest.RequireErrorContains(t, err, tc.expectedErr)
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apiserver/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func NewServer(config *csconfig.LocalApiServerCfg) (*APIServer, error) {
if config.OnlineClient != nil && config.OnlineClient.Credentials != nil {
log.Printf("Loading CAPI manager")

apiClient, err = NewAPIC(config.OnlineClient, dbClient, config.ConsoleConfig, config.CapiWhitelists)
apiClient, err = NewAPIC(ctx, config.OnlineClient, dbClient, config.ConsoleConfig, config.CapiWhitelists)
if err != nil {
return nil, err
}
Expand Down