63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package wazuh
|
|
|
|
import (
|
|
"encoding/json"
|
|
"log"
|
|
"net/http"
|
|
|
|
"forge.cadoles.com/cadoles/wazuh-agent-k8s-autoadd/internal/config"
|
|
)
|
|
|
|
const APIAuthentication = "/security/user/authenticate"
|
|
|
|
func getJWT(cfg Config) (string, error) {
|
|
req, err := http.NewRequest(http.MethodPost, cfg.BaseURL+APIAuthentication, http.NoBody)
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot create request for %v : %+v", cfg.BaseURL+APIAuthentication, err)
|
|
}
|
|
req.SetBasicAuth(cfg.User, cfg.Passwd)
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot request for %v : %+v", cfg.BaseURL+APIAuthentication, err)
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
if res.StatusCode != http.StatusOK {
|
|
return nil, "Bad status on %v: %d", cfg.BaseURL + APIAuthentication, res.StatusCode
|
|
}
|
|
|
|
// Faux, mapper sur du json
|
|
token, err := io.ReadAll(res.Body)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
return token, nil
|
|
}
|
|
|
|
func AddAgent(cfg Config) error {
|
|
resp, err := getJWT(cfg)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
print(resp)
|
|
resp, err := http.DefaultClient.Post(cfg.BaseURL + "/agents")
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
switch resp.StatusCode {
|
|
case http.StatusOK:
|
|
return nil
|
|
default:
|
|
return false, fmt.Errorf("Bad status: %d", resp.StatusCode)
|
|
}
|
|
|
|
return nil
|
|
}
|