add name and alias in cscli console enroll (#950)
* add name and alias in cscli console enroll
This commit is contained in:
parent
0d46890d6b
commit
cf57c89177
|
@ -31,6 +31,9 @@ func NewConsoleCmd() *cobra.Command {
|
|||
},
|
||||
}
|
||||
|
||||
name := ""
|
||||
tags := []string{}
|
||||
|
||||
cmdEnroll := &cobra.Command{
|
||||
Use: "enroll [enroll-key]",
|
||||
Short: "Enroll this instance to https://app.crowdsec.net [requires local API]",
|
||||
|
@ -39,7 +42,10 @@ Enroll this instance to https://app.crowdsec.net
|
|||
|
||||
You can get your enrollment key by creating an account on https://app.crowdsec.net.
|
||||
After running this command your will need to validate the enrollment in the webapp.`,
|
||||
Example: "cscli console enroll YOUR-ENROLL-KEY",
|
||||
Example: `cscli console enroll YOUR-ENROLL-KEY
|
||||
cscli console enroll --name [instance_name] YOUR-ENROLL-KEY
|
||||
cscli console enroll --name [instance_name] --tags [tag_1] --tags [tag_2] YOUR-ENROLL-KEY
|
||||
`,
|
||||
Args: cobra.ExactArgs(1),
|
||||
DisableAutoGenTag: true,
|
||||
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
@ -87,14 +93,15 @@ 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])
|
||||
_, err = c.Auth.EnrollWatcher(context.Background(), args[0], name, tags)
|
||||
if err != nil {
|
||||
log.Fatalf("Could not enroll instance: %s", err)
|
||||
}
|
||||
log.Infof("Watcher successfully enrolled. Visit https://app.crowdsec.net to accept it.")
|
||||
},
|
||||
}
|
||||
|
||||
cmdEnroll.Flags().StringVarP(&name, "name", "n", "", "Name to display in the console")
|
||||
cmdEnroll.Flags().StringSliceVarP(&tags, "tags", "t", tags, "Tags to display in the console")
|
||||
cmdConsole.AddCommand(cmdEnroll)
|
||||
return cmdConsole
|
||||
}
|
||||
|
|
|
@ -14,6 +14,8 @@ type AuthService service
|
|||
// Don't add it to the models, as they are used with LAPI, but the enroll endpoint is specific to CAPI
|
||||
type enrollRequest struct {
|
||||
EnrollKey string `json:"attachment_key"`
|
||||
Name string `json:"name"`
|
||||
Tags []string `json:"tags"`
|
||||
}
|
||||
|
||||
func (s *AuthService) UnregisterWatcher(ctx context.Context) (*Response, error) {
|
||||
|
@ -61,9 +63,9 @@ func (s *AuthService) AuthenticateWatcher(ctx context.Context, auth models.Watch
|
|||
return resp, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) EnrollWatcher(ctx context.Context, enrollKey string) (*Response, error) {
|
||||
func (s *AuthService) EnrollWatcher(ctx context.Context, enrollKey string, name string, tags []string) (*Response, error) {
|
||||
u := fmt.Sprintf("%s/watchers/enroll", s.client.URLPrefix)
|
||||
req, err := s.client.NewRequest("POST", u, &enrollRequest{EnrollKey: enrollKey})
|
||||
req, err := s.client.NewRequest("POST", u, &enrollRequest{EnrollKey: enrollKey, Name: name, Tags: tags})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -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"}
|
||||
if newStr == `{"attachment_key":"goodkey","name":"","tags":[]}
|
||||
` {
|
||||
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")
|
||||
_, err = client.Auth.EnrollWatcher(context.Background(), "goodkey", "", []string{})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpect auth err: %s", err)
|
||||
t.Fatalf("unexpect enroll err: %s", err)
|
||||
}
|
||||
|
||||
_, err = client.Auth.EnrollWatcher(context.Background(), "badkey")
|
||||
_, err = client.Auth.EnrollWatcher(context.Background(), "badkey", "", []string{})
|
||||
assert.Contains(t, err.Error(), "the attachment key provided is not valid")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue