Skip to content

Commit 174684f

Browse files
committed
Change WithInsecure behaviour
1 parent 8ee0243 commit 174684f

File tree

3 files changed

+73
-24
lines changed

3 files changed

+73
-24
lines changed

.github/workflows/go.yml

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,20 @@ name: Go
22

33
on:
44
push:
5-
branches: [ main ]
5+
branches: [main]
66
pull_request:
7-
branches: [ main ]
7+
branches: [main]
88

99
jobs:
10-
1110
build:
1211
runs-on: ubuntu-latest
1312
steps:
14-
- uses: actions/checkout@v2
15-
16-
- name: Set up Go
17-
uses: actions/setup-go@v2
18-
with:
19-
go-version: 1.15
13+
- uses: actions/checkout@v2
2014

21-
- name: Build
22-
run: go build -v ./...
15+
- name: Set up Go
16+
uses: actions/setup-go@v2
17+
with:
18+
go-version: 1.15
2319

24-
- name: Test
25-
run: go test -v ./...
20+
- name: Build
21+
run: go build -v ./...

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ go-wazuh supports following environment variables for easy construction of a cli
3434
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:
3535

3636
```
37-
c := NewAPIClient("https://localhost:55000", WithLogin("wazuh", "wazuh"), WithInsecure())
37+
c := NewAPIClient("https://localhost:55000", WithLogin("wazuh", "wazuh"), WithInsecure(true))
3838
c.Authenticate()
3939
agents := c.AgentsController.GetAgents(&AgentsControllerGetAgentsParams{})
4040
fmt.Printf("Get Agents TotalAffectedItems %d\n", agents.AllItemsResponse.TotalAffectedItems)
@@ -46,7 +46,7 @@ for i, agent := range agents.AffectedItems {
4646
Or use the environment to construct the client to get the server basic information:
4747

4848
```
49-
c, err := NewClientFromEnvironment(WithInsecure())
49+
c, err := NewClientFromEnvironment(WithInsecure(true))
5050
if err != nil {
5151
panic(err)
5252
}
@@ -67,7 +67,7 @@ fmt.Printf("Connected to %s on %s\n", *status.Title, *status.Hostname)
6767
## Testing
6868

6969
Prerequisite: <https://documentation.wazuh.com/4.0/docker/wazuh-container.html>
70-
WAZUH\_\* environment variabes must be configured.
70+
WAZUH\_\* environment variables must be configured.
7171

7272
Visual Studio Code launch configuration used for tests:
7373

wazuh.go

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
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+
155
package wazuh
256

357
import (
@@ -55,9 +109,9 @@ func WithContext(ctx context.Context) ClientOption {
55109
}
56110

57111
// WithInsecure accept all certificates
58-
func WithInsecure() ClientOption {
112+
func WithInsecure(insecure bool) ClientOption {
59113
return func(c *Client) error {
60-
c.insecure = true
114+
c.insecure = insecure
61115
return nil
62116
}
63117
}
@@ -111,16 +165,15 @@ func NewClientFromEnvironment(opts ...ClientOption) (*APIClient, error) {
111165
user := os.Getenv("WAZUH_USER")
112166
password := os.Getenv("WAZUH_PASSWORD")
113167
opts = append(opts, WithLogin(user, password))
168+
if os.Getenv("WAZUH_INSECURE") == "true" {
169+
opts = append(opts, WithInsecure(true))
170+
}
171+
114172
c, err := NewAPIClient(baseURL, opts...)
115173
if err != nil {
116174
return nil, err
117175
}
118-
if os.Getenv("WAZUH_INSECURE") == "true" {
119-
err := WithInsecure()(c.ClientInterface.(*Client))
120-
if err != nil {
121-
return nil, err
122-
}
123-
}
176+
124177
return c, nil
125178
}
126179

0 commit comments

Comments
 (0)