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

Function queryCAPIStatus

cmd/crowdsec-cli/clicapi/capi.go:168–214  ·  view source on GitHub ↗

queryCAPIStatus checks if the Central API is reachable, and if the credentials are correct. It then checks if the instance is enrolled in the console.

(ctx context.Context, db *database.Client, hub *cwhub.Hub, credURL string, login string, password string)

Source from the content-addressed store, hash-verified

166
167// queryCAPIStatus checks if the Central API is reachable, and if the credentials are correct. It then checks if the instance is enrolled in the console.
168func queryCAPIStatus(ctx context.Context, db *database.Client, hub *cwhub.Hub, credURL string, login string, password string) (capiStatus, error) {
169 apiURL, err := url.Parse(credURL)
170 if err != nil {
171 return capiStatus{}, err
172 }
173
174 itemsForAPI := hub.GetInstalledListForAPI()
175
176 passwd := strfmt.Password(password)
177
178 client := apiclient.NewClient(&apiclient.Config{
179 MachineID: login,
180 Password: passwd,
181 URL: apiURL,
182 // I don't believe papi is needed to check enrollement
183 // PapiURL: papiURL,
184 VersionPrefix: "v3",
185 UpdateScenario: func(_ context.Context) ([]string, error) {
186 return itemsForAPI, nil
187 },
188 })
189
190 pw := strfmt.Password(password)
191
192 t := models.WatcherAuthRequest{
193 MachineID: &login,
194 Password: &pw,
195 Scenarios: itemsForAPI,
196 }
197
198 authResp, _, err := client.Auth.AuthenticateWatcher(ctx, t)
199 if err != nil {
200 return capiStatus{}, err
201 }
202
203 if err := db.SaveAPICToken(ctx, authResp.Token); err != nil {
204 return capiStatus{}, err
205 }
206
207 client.GetClient().Transport.(*apiclient.JWTTransport).Token = authResp.Token
208
209 if client.IsEnrolled() {
210 return capiStatus{true, true, client.GetSubscriptionType()}, nil
211 }
212
213 return capiStatus{true, false, ""}, nil
214}
215
216func (cli *cliCapi) Status(ctx context.Context, db *database.Client, out io.Writer, hub *cwhub.Hub) error {
217 cfg := cli.cfg()

Callers 1

StatusMethod · 0.85

Calls 9

NewClientFunction · 0.92
PasswordMethod · 0.80
AuthenticateWatcherMethod · 0.80
SaveAPICTokenMethod · 0.80
GetClientMethod · 0.80
IsEnrolledMethod · 0.80
GetSubscriptionTypeMethod · 0.80
ParseMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…