diff --git a/cmd/crowdsec-cli/console.go b/cmd/crowdsec-cli/console.go index 342fbd68b..c18fded9e 100644 --- a/cmd/crowdsec-cli/console.go +++ b/cmd/crowdsec-cli/console.go @@ -53,6 +53,7 @@ func NewConsoleCmd() *cobra.Command { } name := "" + overwrite := false tags := []string{} cmdEnroll := &cobra.Command{ @@ -102,10 +103,14 @@ After running this command your will need to validate the enrollment in the weba URL: apiURL, VersionPrefix: "v2", }) - _, err = c.Auth.EnrollWatcher(context.Background(), args[0], name, tags) + resp, err := c.Auth.EnrollWatcher(context.Background(), args[0], name, tags, overwrite) if err != nil { log.Fatalf("Could not enroll instance: %s", err) } + if resp.Response.StatusCode == 200 && !overwrite { + log.Warningf("Instance already enrolled. You can use '--overwrite' to force enroll") + return + } SetConsoleOpts(csconfig.CONSOLE_CONFIGS, true) if err := csConfig.API.Server.DumpConsoleConfig(); err != nil { @@ -117,6 +122,7 @@ After running this command your will need to validate the enrollment in the weba }, } cmdEnroll.Flags().StringVarP(&name, "name", "n", "", "Name to display in the console") + cmdEnroll.Flags().BoolVarP(&overwrite, "overwrite", "", false, "Force enroll the instance") cmdEnroll.Flags().StringSliceVarP(&tags, "tags", "t", tags, "Tags to display in the console") cmdConsole.AddCommand(cmdEnroll) diff --git a/pkg/apiclient/auth_service.go b/pkg/apiclient/auth_service.go index 6809aba30..9b007ee8d 100644 --- a/pkg/apiclient/auth_service.go +++ b/pkg/apiclient/auth_service.go @@ -16,6 +16,7 @@ type enrollRequest struct { EnrollKey string `json:"attachment_key"` Name string `json:"name"` Tags []string `json:"tags"` + Overwrite bool `json:"overwrite"` } func (s *AuthService) UnregisterWatcher(ctx context.Context) (*Response, error) { @@ -63,9 +64,9 @@ func (s *AuthService) AuthenticateWatcher(ctx context.Context, auth models.Watch return resp, nil } -func (s *AuthService) EnrollWatcher(ctx context.Context, enrollKey string, name string, tags []string) (*Response, error) { +func (s *AuthService) EnrollWatcher(ctx context.Context, enrollKey string, name string, tags []string, overwrite bool) (*Response, error) { u := fmt.Sprintf("%s/watchers/enroll", s.client.URLPrefix) - req, err := s.client.NewRequest("POST", u, &enrollRequest{EnrollKey: enrollKey, Name: name, Tags: tags}) + req, err := s.client.NewRequest("POST", u, &enrollRequest{EnrollKey: enrollKey, Name: name, Tags: tags, Overwrite: overwrite}) if err != nil { return nil, err } diff --git a/pkg/apiclient/auth_service_test.go b/pkg/apiclient/auth_service_test.go index 460d662a1..107e3d685 100644 --- a/pkg/apiclient/auth_service_test.go +++ b/pkg/apiclient/auth_service_test.go @@ -192,7 +192,7 @@ func TestWatcherEnroll(t *testing.T) { _, _ = buf.ReadFrom(r.Body) newStr := buf.String() log.Debugf("body -> %s", newStr) - if newStr == `{"attachment_key":"goodkey","name":"","tags":[]} + if newStr == `{"attachment_key":"goodkey","name":"","tags":[],"overwrite":false} ` { log.Print("good key") w.WriteHeader(http.StatusOK) @@ -228,11 +228,11 @@ func TestWatcherEnroll(t *testing.T) { log.Fatalf("new api client: %s", err.Error()) } - _, err = client.Auth.EnrollWatcher(context.Background(), "goodkey", "", []string{}) + _, err = client.Auth.EnrollWatcher(context.Background(), "goodkey", "", []string{}, false) if err != nil { t.Fatalf("unexpect enroll err: %s", err) } - _, err = client.Auth.EnrollWatcher(context.Background(), "badkey", "", []string{}) + _, err = client.Auth.EnrollWatcher(context.Background(), "badkey", "", []string{}, false) assert.Contains(t, err.Error(), "the attachment key provided is not valid") }