Skip to content

Commit 7e058b9

Browse files
committed
up
1 parent 59c99af commit 7e058b9

3 files changed

Lines changed: 19 additions & 5 deletions

File tree

cmd/crowdsec-cli/cliconsole/console.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"slices"
1313
"strconv"
1414
"strings"
15+
"time"
1516

1617
"github.com/fatih/color"
1718
"github.com/go-openapi/strfmt"
@@ -107,6 +108,9 @@ func (cli *cliConsole) enroll(ctx context.Context, key string, name string, over
107108

108109
if autoEnroll {
109110
log.Infof("Please visit the following URL to enroll your instance: %s", autoResp.Url)
111+
log.Infof("This link is valid for the next %s.", time.Until(time.UnixMilli(autoResp.ExpiresAt)).Round(time.Minute))
112+
log.Info("Please restart crowdsec after accepting the enrollment.")
113+
110114
} else {
111115
log.Info("Watcher successfully enrolled. Visit https://app.crowdsec.net to accept it.")
112116
log.Info("Please restart crowdsec after accepting the enrollment.")

pkg/apiclient/auth_service.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@ type AuthService service
1212

1313
// Don't add it to the models, as they are used with LAPI, but the enroll endpoint is specific to CAPI
1414
type enrollRequest struct {
15-
EnrollKey string `json:"attachment_key"`
15+
EnrollKey string `json:"attachment_key,omitempty"`
1616
Name string `json:"name"`
1717
Tags []string `json:"tags"`
18-
Overwrite bool `json:"overwrite"`
18+
Overwrite bool `json:"overwrite,omitempty"`
1919
AutoEnroll bool `json:"autoenroll"`
2020
}
2121

2222
type autoEnrollResponse struct {
23-
Url string `json:"url"`
23+
Url string `json:"url"`
24+
ExpiresAt int64 `json:"expire_at"`
2425
}
2526

2627
func (s *AuthService) UnregisterWatcher(ctx context.Context) (*Response, error) {
@@ -76,7 +77,16 @@ func (s *AuthService) AuthenticateWatcher(ctx context.Context, auth models.Watch
7677
func (s *AuthService) EnrollWatcher(ctx context.Context, enrollKey string, name string, tags []string, overwrite bool, autoEnroll bool) (autoEnrollResponse, *Response, error) {
7778
u := fmt.Sprintf("%s/watchers/enroll", s.client.URLPrefix)
7879

79-
req, err := s.client.PrepareRequest(ctx, http.MethodPost, u, &enrollRequest{EnrollKey: enrollKey, Name: name, Tags: tags, Overwrite: overwrite, AutoEnroll: autoEnroll})
80+
b := &enrollRequest{EnrollKey: enrollKey, Name: name, Tags: tags, AutoEnroll: autoEnroll}
81+
// Console refuses enroll requests if overwrite or key is set even to falsy values with autoenroll, so we only set them if they are needed
82+
if overwrite {
83+
b.Overwrite = true
84+
}
85+
if enrollKey != "" {
86+
b.EnrollKey = enrollKey
87+
}
88+
89+
req, err := s.client.PrepareRequest(ctx, http.MethodPost, u, b)
8090
if err != nil {
8191
return autoEnrollResponse{}, nil, err
8292
}

pkg/apiclient/auth_service_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ func TestWatcherEnroll(t *testing.T) {
256256
log.Print("good key")
257257
w.WriteHeader(http.StatusOK)
258258
fmt.Fprint(w, `{"statusCode": 200, "message": "OK"}`)
259-
} else if parsedBody["attachment_key"] == "" && parsedBody["autoenroll"].(bool) {
259+
} else if parsedBody["attachment_key"] == nil && parsedBody["autoenroll"].(bool) {
260260
log.Print("autoenroll")
261261
w.WriteHeader(http.StatusOK)
262262
fmt.Fprint(w, `{"url": "https://example.com/enroll"}`)

0 commit comments

Comments
 (0)