Skip to content

Commit

Permalink
context propagation: pass context to NewAPIC() (#3231)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmetc authored Sep 19, 2024
1 parent 7c5d4d8 commit be391a3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
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) NewCommand() *cobra.Command {
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 @@ func (cli *cliPapi) newStatusCmd() *cobra.Command {
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 {
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)
if err != nil {
return fmt.Errorf("unable to initialize API client: %w", err)
}
Expand Down Expand Up @@ -159,12 +159,14 @@ func (cli *cliPapi) newSyncCmd() *cobra.Command {
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)
if err != nil {
return err
}

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

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

0 comments on commit be391a3

Please sign in to comment.