|
| 1 | +/* |
| 2 | +go client for the wazuh [rest api](https://documentation.wazuh.com/4.0/user-manual/api/reference.html) |
| 3 | +
|
| 4 | +it is generated from the OpenAPI 3.0 specifications. Thus it is not the most elegant API. Some effort has been put into an more go friendly interface by wrapping non successful results into errors and returning the `Data` objects instead of the raw result. |
| 5 | +
|
| 6 | +
|
| 7 | +There are a few With... option functions that can be used to customize the API client: |
| 8 | +
|
| 9 | +- `WithBaseURL` custom base url |
| 10 | +- `WithLogin` (username, password) |
| 11 | +- `WithContext` (custom Context) |
| 12 | +- `WithInsecure` allow insecure certificates |
| 13 | +- `WithUserAgent` to set custom user agent |
| 14 | +
|
| 15 | +go-wazuh supports following environment variables for easy construction of a client: |
| 16 | +
|
| 17 | +- `WAZUH_URL` |
| 18 | +- `WAZUH_USER` |
| 19 | +- `WAZUH_PASSWORD` |
| 20 | +- `WAZUH_INSECURE` |
| 21 | +
|
| 22 | +Construct a new Wazuh client, then use the various service on the client to access different parts of the wazuh API. For example, to list all agents: |
| 23 | +
|
| 24 | + c := NewAPIClient("https://localhost:55000", WithLogin("wazuh", "wazuh"), WithInsecure(true)) |
| 25 | + c.Authenticate() |
| 26 | + agents := c.AgentsController.GetAgents(&AgentsControllerGetAgentsParams{}) |
| 27 | + fmt.Printf("Get Agents TotalAffectedItems %d\n", agents.AllItemsResponse.TotalAffectedItems) |
| 28 | + for i, agent := range agents.AffectedItems { |
| 29 | + fmt.Printf(" %d: %s on %s\n", i, *agent.Id, *agent.NodeName) |
| 30 | + } |
| 31 | +
|
| 32 | +
|
| 33 | +Or use the environment to construct the client to get the server basic information: |
| 34 | +
|
| 35 | +
|
| 36 | + c, err := NewClientFromEnvironment(WithInsecure(true)) |
| 37 | + if err != nil { |
| 38 | + panic(err) |
| 39 | + } |
| 40 | + // authenticate |
| 41 | + err = c.Authenticate() |
| 42 | + if err != nil { |
| 43 | + panic(err) |
| 44 | + } |
| 45 | +
|
| 46 | + // call the DefaultInfo on the |
| 47 | + status, err := c.Default.DefaultInfo(&DefaultControllerDefaultInfoParams{}) |
| 48 | + if err != nil { |
| 49 | + panic(err) |
| 50 | + } |
| 51 | + fmt.Printf("Connected to %s on %s\n", *status.Title, *status.Hostname) |
| 52 | +
|
| 53 | +*/ |
| 54 | + |
1 | 55 | package wazuh
|
2 | 56 |
|
3 | 57 | import (
|
@@ -55,9 +109,9 @@ func WithContext(ctx context.Context) ClientOption {
|
55 | 109 | }
|
56 | 110 |
|
57 | 111 | // WithInsecure accept all certificates
|
58 |
| -func WithInsecure() ClientOption { |
| 112 | +func WithInsecure(insecure bool) ClientOption { |
59 | 113 | return func(c *Client) error {
|
60 |
| - c.insecure = true |
| 114 | + c.insecure = insecure |
61 | 115 | return nil
|
62 | 116 | }
|
63 | 117 | }
|
@@ -111,16 +165,15 @@ func NewClientFromEnvironment(opts ...ClientOption) (*APIClient, error) {
|
111 | 165 | user := os.Getenv("WAZUH_USER")
|
112 | 166 | password := os.Getenv("WAZUH_PASSWORD")
|
113 | 167 | opts = append(opts, WithLogin(user, password))
|
| 168 | + if os.Getenv("WAZUH_INSECURE") == "true" { |
| 169 | + opts = append(opts, WithInsecure(true)) |
| 170 | + } |
| 171 | + |
114 | 172 | c, err := NewAPIClient(baseURL, opts...)
|
115 | 173 | if err != nil {
|
116 | 174 | return nil, err
|
117 | 175 | }
|
118 |
| - if os.Getenv("WAZUH_INSECURE") == "true" { |
119 |
| - err := WithInsecure()(c.ClientInterface.(*Client)) |
120 |
| - if err != nil { |
121 |
| - return nil, err |
122 |
| - } |
123 |
| - } |
| 176 | + |
124 | 177 | return c, nil
|
125 | 178 | }
|
126 | 179 |
|
|
0 commit comments