MCPcopy
hub / github.com/crowdsecurity/crowdsec / register

Method register

cmd/crowdsec-cli/clilapi/register.go:19–96  ·  view source on GitHub ↗
(ctx context.Context, apiURL string, outputFile string, machine string, token string)

Source from the content-addressed store, hash-verified

17)
18
19func (cli *cliLapi) register(ctx context.Context, apiURL string, outputFile string, machine string, token string) error {
20 var err error
21
22 lapiUser := machine
23 cfg := cli.cfg()
24
25 if lapiUser == "" {
26 lapiUser, err = idgen.GenerateMachineID("")
27 if err != nil {
28 return fmt.Errorf("unable to generate machine id: %w", err)
29 }
30 }
31
32 pstr, err := idgen.GeneratePassword(idgen.PasswordLength)
33 if err != nil {
34 return err
35 }
36
37 password := strfmt.Password(pstr)
38
39 apiurl, err := prepareAPIURL(cfg.API.Client, apiURL)
40 if err != nil {
41 return fmt.Errorf("parsing api url: %w", err)
42 }
43
44 _, err = apiclient.RegisterClient(ctx, &apiclient.Config{
45 MachineID: lapiUser,
46 Password: password,
47 RegistrationToken: token,
48 URL: apiurl,
49 VersionPrefix: LAPIURLPrefix,
50 }, nil)
51 if err != nil {
52 return fmt.Errorf("api client register: %w", err)
53 }
54
55 log.Info("Successfully registered to Local API (LAPI)")
56
57 var dumpFile string
58
59 if outputFile != "" {
60 dumpFile = outputFile
61 } else if cfg.API.Client.CredentialsFilePath != "" {
62 dumpFile = cfg.API.Client.CredentialsFilePath
63 } else {
64 dumpFile = ""
65 }
66
67 apiCfg := cfg.API.Client.Credentials
68 apiCfg.Login = lapiUser
69 apiCfg.Password = password.String()
70
71 if apiURL != "" {
72 apiCfg.URL = apiURL
73 }
74
75 apiConfigDump, err := yaml.Marshal(apiCfg)
76 if err != nil {

Callers 1

newRegisterCmdMethod · 0.95

Calls 9

GenerateMachineIDFunction · 0.92
GeneratePasswordFunction · 0.92
RegisterClientFunction · 0.92
UserMessageFunction · 0.92
prepareAPIURLFunction · 0.85
cfgMethod · 0.80
PasswordMethod · 0.80
InfoMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected