Skip to content

Commit d7d7e67

Browse files
authored
cscli: handle sigint/sigterm, cancel context of ongoing http req (#3660)
* cscli: handle sigint/sigterm, cancel context of ongoing http req * lint * format error correctly from initialize()
1 parent 6a8f7c2 commit d7d7e67

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

cmd/crowdsec-cli/main.go

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package main
22

33
import (
4+
"context"
45
"fmt"
56
"os"
7+
"os/signal"
68
"path/filepath"
79
"slices"
10+
"syscall"
811
"time"
912

1013
"github.com/fatih/color"
@@ -171,6 +174,7 @@ func (cli *cliRoot) initialize() error {
171174
if csConfig.DbConfig != nil {
172175
csConfig.DbConfig.LogLevel = ptr.Of(cli.wantedLogLevel())
173176
}
177+
174178
return nil
175179
}
176180

@@ -295,7 +299,7 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
295299
cobra.OnInitialize(
296300
func() {
297301
if err := cli.initialize(); err != nil {
298-
log.Fatal(err)
302+
fatal(err)
299303
}
300304
},
301305
)
@@ -304,15 +308,27 @@ It is meant to allow you to manage bans, parsers/scenarios/etc, api and generall
304308
return cmd, nil
305309
}
306310

307-
func main() {
311+
func fatal(err error) {
312+
red := color.New(color.FgRed).SprintFunc()
313+
fmt.Fprintln(os.Stderr, red("Error:"), err)
314+
os.Exit(1)
315+
}
316+
317+
func mainWrap() error {
318+
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
319+
defer stop()
320+
308321
cmd, err := newCliRoot().NewCommand()
309322
if err != nil {
310-
log.Fatal(err)
323+
return err
311324
}
312325

313-
if err := cmd.Execute(); err != nil {
314-
red := color.New(color.FgRed).SprintFunc()
315-
fmt.Fprintln(os.Stderr, red("Error:"), err)
316-
os.Exit(1)
326+
return cmd.ExecuteContext(ctx)
327+
}
328+
329+
func main() {
330+
err := mainWrap()
331+
if err != nil {
332+
fatal(err)
317333
}
318334
}

pkg/apiclient/auth_jwt.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@ type JWTTransport struct {
3636
TokenSave TokenSave
3737
}
3838

39-
func (t *JWTTransport) refreshJwtToken() error {
39+
func (t *JWTTransport) refreshJwtToken(ctx context.Context) error {
4040
var err error
4141

42-
ctx := context.TODO()
43-
4442
if t.UpdateScenario != nil {
4543
t.Scenarios, err = t.UpdateScenario(ctx)
4644
if err != nil {
@@ -141,6 +139,7 @@ func (t *JWTTransport) refreshJwtToken() error {
141139
log.Errorf("unable to save token: %s", err)
142140
}
143141
}
142+
144143
log.Debugf("token %s will expire on %s", t.Token, t.Expiration.String())
145144

146145
return nil
@@ -160,7 +159,7 @@ func (t *JWTTransport) prepareRequest(req *http.Request) (*http.Request, error)
160159
// We bypass the refresh if we are requesting the login endpoint, as it does not require a token,
161160
// and it leads to do 2 requests instead of one (refresh + actual login request).
162161
if req.URL.Path != "/"+t.VersionPrefix+"/watchers/login" && t.needsTokenRefresh() {
163-
if err := t.refreshJwtToken(); err != nil {
162+
if err := t.refreshJwtToken(req.Context()); err != nil {
164163
return nil, err
165164
}
166165
}

0 commit comments

Comments
 (0)