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

Method register

cmd/crowdsec-cli/clicapi/capi.go:56–126  ·  view source on GitHub ↗
(ctx context.Context, capiUserPrefix string, outputFile string)

Source from the content-addressed store, hash-verified

54}
55
56func (cli *cliCapi) register(ctx context.Context, capiUserPrefix string, outputFile string) error {
57 cfg := cli.cfg()
58
59 capiUser, err := idgen.GenerateMachineID(capiUserPrefix)
60 if err != nil {
61 return fmt.Errorf("unable to generate machine id: %w", err)
62 }
63
64 pstr, err := idgen.GeneratePassword(idgen.PasswordLength)
65 if err != nil {
66 return err
67 }
68
69 password := strfmt.Password(pstr)
70
71 apiurl, err := url.Parse(CAPIBaseURL)
72 if err != nil {
73 return fmt.Errorf("unable to parse api url %s: %w", CAPIBaseURL, err)
74 }
75
76 _, err = apiclient.RegisterClient(ctx, &apiclient.Config{
77 MachineID: capiUser,
78 Password: password,
79 URL: apiurl,
80 VersionPrefix: "v3",
81 }, nil)
82 if err != nil {
83 return fmt.Errorf("api client register ('%s'): %w", CAPIBaseURL, err)
84 }
85
86 log.Infof("Successfully registered to Central API (CAPI)")
87
88 var dumpFile string
89
90 switch {
91 case outputFile != "":
92 dumpFile = outputFile
93 case cfg.API.Server.OnlineClient.CredentialsFilePath != "":
94 dumpFile = cfg.API.Server.OnlineClient.CredentialsFilePath
95 default:
96 dumpFile = ""
97 }
98
99 apiCfg := csconfig.ApiCredentialsCfg{
100 Login: capiUser,
101 Password: password.String(),
102 URL: CAPIBaseURL,
103 }
104
105 apiConfigDump, err := yaml.Marshal(apiCfg)
106 if err != nil {
107 return fmt.Errorf("unable to serialize api credentials: %w", err)
108 }
109
110 if dumpFile != "" {
111 err = os.WriteFile(dumpFile, apiConfigDump, 0o600)
112 if err != nil {
113 return fmt.Errorf("write api credentials in '%s' failed: %w", dumpFile, err)

Callers 1

newRegisterCmdMethod · 0.95

Calls 8

GenerateMachineIDFunction · 0.92
GeneratePasswordFunction · 0.92
RegisterClientFunction · 0.92
UserMessageFunction · 0.92
cfgMethod · 0.80
PasswordMethod · 0.80
ParseMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected