This commit is contained in:
Laurent Gourvenec 2025-04-18 16:23:34 +02:00
parent 10e326b4b1
commit d199bd97ba
3 changed files with 46 additions and 5 deletions

View File

@ -11,9 +11,13 @@ import (
func main() {
cfg, err := config.NewConfig()
if err != nil {
log.Print(err)
os.Exit(1)
}
log.Print(cfg.BaseURL)
// Faire l'appel
err = wazuh.AddAgent(cfg)
if err != nil {
log.Print(err)
os.Exit(2)
}
}

View File

@ -7,12 +7,13 @@ import (
type Config struct {
BaseURL string `env:"WAZUH_MANAGER_BASE_URL,required,notEmpty"`
User string `env:"WAZUH_MANAGER_USER,required,notEmpty"`
Passwd string `env:"WAZUH_MANAGER_PASSWD,required,notEmpty"`
}
func NewConfig() (*Config, error) {
cfg := &Config{}
if err := env.Parse(cfg); err != nil {
log.Print(err)
return nil, err
}

View File

@ -8,9 +8,43 @@ import (
"forge.cadoles.com/cadoles/wazuh-agent-k8s-autoadd/internal/config"
)
func AddAgent(cfg Config) (error) {
// Craft jwt
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
@ -23,4 +57,6 @@ func AddAgent(cfg Config) (error) {
default:
return false, fmt.Errorf("Bad status: %d", resp.StatusCode)
}
return nil
}