|
@@ -96,7 +96,9 @@ func (ctx *ApiCtx) LoadConfig(cfg string) error {
|
|
log.Warningf("!API paths must not be prefixed by /")
|
|
log.Warningf("!API paths must not be prefixed by /")
|
|
}
|
|
}
|
|
|
|
|
|
- ctx.Http = sling.New().Base(ctx.BaseURL+"/"+ctx.ApiVersion+"/").Set("User-Agent", fmt.Sprintf("Crowdsec/%s", cwversion.VersionStr()))
|
|
|
|
|
|
+ httpClient := &http.Client{Timeout: 20 * time.Second}
|
|
|
|
+
|
|
|
|
+ ctx.Http = sling.New().Client(httpClient).Base(ctx.BaseURL+"/"+ctx.ApiVersion+"/").Set("User-Agent", fmt.Sprintf("Crowdsec/%s", cwversion.VersionStr()))
|
|
log.Printf("api load configuration: configuration loaded successfully (base:%s)", ctx.BaseURL+"/"+ctx.ApiVersion+"/")
|
|
log.Printf("api load configuration: configuration loaded successfully (base:%s)", ctx.BaseURL+"/"+ctx.ApiVersion+"/")
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
@@ -139,17 +141,13 @@ func (ctx *ApiCtx) Signin() error {
|
|
if ctx.Creds.User == "" || ctx.Creds.Password == "" {
|
|
if ctx.Creds.User == "" || ctx.Creds.Password == "" {
|
|
return fmt.Errorf("api signin: missing credentials in api.yaml")
|
|
return fmt.Errorf("api signin: missing credentials in api.yaml")
|
|
}
|
|
}
|
|
|
|
+ jsonResp := &ApiResp{}
|
|
|
|
|
|
- req, err := ctx.Http.New().Post(ctx.SigninPath).BodyJSON(ctx.Creds).Request()
|
|
|
|
|
|
+ resp, err := ctx.Http.Post(ctx.SigninPath).BodyJSON(ctx.Creds).ReceiveSuccess(jsonResp)
|
|
if err != nil {
|
|
if err != nil {
|
|
return fmt.Errorf("api signin: HTTP request creation failed: %s", err)
|
|
return fmt.Errorf("api signin: HTTP request creation failed: %s", err)
|
|
}
|
|
}
|
|
- log.Debugf("api signin: URL: '%s'", req.URL)
|
|
|
|
- httpClient := http.Client{Timeout: 20 * time.Second}
|
|
|
|
- resp, err := httpClient.Do(req)
|
|
|
|
- if err != nil {
|
|
|
|
- return fmt.Errorf("api signin: API call failed : %s", err)
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
defer resp.Body.Close()
|
|
defer resp.Body.Close()
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
if err != nil {
|
|
@@ -159,12 +157,10 @@ func (ctx *ApiCtx) Signin() error {
|
|
if resp.StatusCode != 200 {
|
|
if resp.StatusCode != 200 {
|
|
return fmt.Errorf("api signin: return bad HTTP code (%d): %s", resp.StatusCode, string(body))
|
|
return fmt.Errorf("api signin: return bad HTTP code (%d): %s", resp.StatusCode, string(body))
|
|
}
|
|
}
|
|
-
|
|
|
|
- jsonResp := ApiResp{}
|
|
|
|
- err = json.Unmarshal(body, &jsonResp)
|
|
|
|
- if err != nil {
|
|
|
|
- return fmt.Errorf("api signin: unable to unmarshall api response '%s': %s", string(body), err.Error())
|
|
|
|
|
|
+ if jsonResp.Message == "" || jsonResp.StatusCode != 200 {
|
|
|
|
+ return fmt.Errorf("api signin failed. http response: %s", body)
|
|
}
|
|
}
|
|
|
|
+
|
|
ctx.Http = ctx.Http.Set("Authorization", jsonResp.Message)
|
|
ctx.Http = ctx.Http.Set("Authorization", jsonResp.Message)
|
|
log.Printf("api signin: signed in successfuly")
|
|
log.Printf("api signin: signed in successfuly")
|
|
return nil
|
|
return nil
|