Skip to content

Commit 4f8f529

Browse files
committed
Merge remote-tracking branch 'origin/main' into v2
# Conflicts: # api/certificate.go # api/store.go
2 parents 76c7066 + ae10aa8 commit 4f8f529

21 files changed

+925
-974
lines changed

.github/workflows/release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ jobs:
3232
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
3333
passphrase: ${{ secrets.PASSPHRASE }}
3434
- name: Run GoReleaser
35-
uses: goreleaser/goreleaser-action@v3
35+
uses: goreleaser/goreleaser-action@v4
3636
with:
37-
args: release --rm-dist
37+
args: release --clean
3838
env:
3939
# GitHub sets the GITHUB_TOKEN secret automatically.
4040
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/tests.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ jobs:
2424
- name: Build
2525
run: go build -v ./...
2626

27-
- name: Test
28-
env:
29-
# GitHub sets the GITHUB_TOKEN secret automatically.
30-
ENV_FILE: ${{ secrets.ENV_FILE }}
31-
run: echo $ENV_FILE | base64 --decode > .env && source .env && go test -v ./api...
27+
# - name: Test
28+
# env:
29+
# # GitHub sets the GITHUB_TOKEN secret automatically.
30+
# ENV_FILE: ${{ secrets.ENV_FILE }}
31+
# run: echo $ENV_FILE | base64 --decode > .env && source .env && go test -v ./api...
3232

3333

3434

api/agent.go

Lines changed: 95 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -1,218 +1,170 @@
11
package api
22

33
import (
4-
"encoding/json"
4+
"context"
55
"fmt"
6-
"log"
6+
7+
"github.com/Keyfactor/keyfactor-go-client-sdk/api/keyfactor"
78
)
89

910
// GetAgentList returns a list of orchestrators registered in the Keyfactor instance
1011
func (c *Client) GetAgentList() ([]Agent, error) {
11-
log.Println("[INFO] Getting a list of agents registered in Keyfactor")
1212

13-
// Set Keyfactor-specific headers
14-
headers := &apiHeaders{
15-
Headers: []StringTuple{
16-
{"x-keyfactor-api-version", "1"},
17-
{"x-keyfactor-requested-with", "APIClient"},
18-
},
19-
}
13+
xKeyfactorRequestedWith := "APIClient"
14+
xKeyfactorApiVersion := "1"
2015

21-
keyfactorAPIStruct := &request{
22-
Method: "GET",
23-
Endpoint: "Agents",
24-
Headers: headers,
25-
Payload: nil,
26-
}
16+
configuration := keyfactor.NewConfiguration(make(map[string]string))
17+
apiClient := keyfactor.NewAPIClient(configuration)
2718

28-
resp, err := c.sendRequest(keyfactorAPIStruct)
29-
if err != nil {
30-
return nil, err
31-
}
19+
resp, _, err := apiClient.AgentApi.AgentGetAgents(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
20+
21+
var revResp []Agent
3222

33-
var jsonResp []Agent
34-
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
3523
if err != nil {
36-
return nil, err
37-
}
38-
return jsonResp, nil
24+
return revResp, err
25+
}
26+
27+
for i := range resp {
28+
newAgent := Agent{
29+
AgentId: *resp[i].AgentId,
30+
AgentPoolId: "",
31+
ClientMachine: *resp[i].ClientMachine,
32+
Username: *resp[i].Username,
33+
AgentPlatform: int(*resp[i].AgentPlatform),
34+
Status: int(*resp[i].Status),
35+
EnableDiscover: false, //TODO
36+
EnableMonitor: false, //TODO
37+
Version: *resp[i].Version,
38+
LastSeen: resp[i].LastSeen.String(),
39+
Thumbprint: *resp[i].Thumbprint,
40+
LegacyThumbprint: *resp[i].LegacyThumbprint,
41+
}
42+
revResp = append(revResp, newAgent)
43+
}
44+
45+
return revResp, nil
3946
}
4047

4148
func (c *Client) GetAgent(id string) ([]Agent, error) {
42-
log.Println("[INFO] Getting agent by ID or name.")
4349

44-
// Set Keyfactor-specific headers
45-
headers := &apiHeaders{
46-
Headers: []StringTuple{
47-
{"x-keyfactor-api-version", "1"},
48-
{"x-keyfactor-requested-with", "APIClient"},
49-
},
50-
}
51-
query := apiQuery{
52-
Query: []StringTuple{},
53-
}
54-
query.Query = append(query.Query, StringTuple{
55-
"pq.queryString", fmt.Sprintf(`ClientMachine -eq "%s"`, id),
56-
})
57-
58-
keyfactorAPIStruct := &request{
59-
Method: "GET",
60-
Endpoint: "Agents",
61-
Headers: headers,
62-
Payload: nil,
63-
Query: &query,
64-
}
50+
xKeyfactorRequestedWith := "APIClient"
51+
xKeyfactorApiVersion := "1"
6552

66-
resp, err := c.sendRequest(keyfactorAPIStruct)
67-
if err != nil {
68-
return nil, err
69-
}
53+
configuration := keyfactor.NewConfiguration(make(map[string]string))
54+
apiClient := keyfactor.NewAPIClient(configuration)
55+
56+
resp, _, err := apiClient.AgentApi.AgentGetAgentDetail(context.Background(), id).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
57+
58+
var revResp []Agent
7059

71-
var jsonResp []Agent
72-
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
7360
if err != nil {
74-
return nil, err
75-
}
76-
if len(jsonResp) == 0 {
77-
return jsonResp, fmt.Errorf("agent %s not found", id)
61+
return revResp, err
62+
}
63+
64+
revResp = []Agent{
65+
{
66+
AgentId: resp.GetAgentId(),
67+
AgentPoolId: "",
68+
ClientMachine: resp.GetClientMachine(),
69+
Username: resp.GetUsername(),
70+
AgentPlatform: int(resp.GetAgentPlatform()),
71+
Status: int(resp.GetStatus()),
72+
EnableDiscover: false, //TODO
73+
EnableMonitor: false, //TODO
74+
Version: resp.GetVersion(),
75+
LastSeen: resp.GetLastSeen().String(),
76+
Thumbprint: resp.GetThumbprint(),
77+
LegacyThumbprint: resp.GetLegacyThumbprint(),
78+
},
7879
}
79-
return jsonResp, nil
80+
81+
return revResp, nil
8082
}
8183

8284
func (c *Client) ApproveAgent(id string) (string, error) {
83-
log.Printf("[INFO] Approving agent %s in Keyfactor.\n", id)
8485

85-
// Set Keyfactor-specific headers
86-
headers := &apiHeaders{
87-
Headers: []StringTuple{
88-
{"x-keyfactor-api-version", "1"},
89-
{"x-keyfactor-requested-with", "APIClient"},
90-
},
91-
}
86+
xKeyfactorRequestedWith := "APIClient"
87+
xKeyfactorApiVersion := "1"
9288

93-
keyfactorAPIStruct := &request{
94-
Method: "POST",
95-
Endpoint: "Agents/Approve",
96-
Headers: headers,
97-
Payload: &[]string{id},
98-
}
89+
configuration := keyfactor.NewConfiguration(make(map[string]string))
90+
apiClient := keyfactor.NewAPIClient(configuration)
9991

100-
resp, rErr := c.sendRequest(keyfactorAPIStruct)
101-
if rErr != nil {
102-
return "", rErr
103-
}
92+
var ids = []string{id}
93+
94+
resp, err := apiClient.AgentApi.AgentApprove(context.Background()).XKeyfactorRequestedWith(xKeyfactorRequestedWith).AgentIds(ids).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
10495

10596
if resp.StatusCode == 204 {
10697
return "Approve agent successful.", nil
10798
}
10899

109-
var jsonResp string
110-
err := json.NewDecoder(resp.Body).Decode(&jsonResp)
111100
if err != nil {
112101
return "", err
113102
}
114-
return jsonResp, nil
103+
104+
return resp.Status, nil
115105
}
116106

117107
func (c *Client) DisApproveAgent(id string) (string, error) {
118-
log.Printf("[INFO] Disapproving agent %s in Keyfactor.\n", id)
119108

120-
// Set Keyfactor-specific headers
121-
headers := &apiHeaders{
122-
Headers: []StringTuple{
123-
{"x-keyfactor-api-version", "1"},
124-
{"x-keyfactor-requested-with", "APIClient"},
125-
},
126-
}
109+
xKeyfactorRequestedWith := "APIClient"
110+
xKeyfactorApiVersion := "1"
127111

128-
keyfactorAPIStruct := &request{
129-
Method: "POST",
130-
Endpoint: "Agents/Disapprove",
131-
Headers: headers,
132-
Payload: &[]string{id},
133-
}
112+
configuration := keyfactor.NewConfiguration(make(map[string]string))
113+
apiClient := keyfactor.NewAPIClient(configuration)
134114

135-
resp, rErr := c.sendRequest(keyfactorAPIStruct)
136-
if rErr != nil {
137-
return "", rErr
138-
}
115+
var ids = []string{id}
116+
117+
resp, err := apiClient.AgentApi.AgentDisapprove(context.Background()).AgentIds(ids).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
139118

140119
if resp.StatusCode == 204 {
141120
return fmt.Sprintf("Disapproving %s successful.", id), nil
142121
}
143122

144-
var jsonResp string
145-
err := json.NewDecoder(resp.Body).Decode(&jsonResp)
146123
if err != nil {
147124
return "", err
148125
}
149-
return jsonResp, nil
126+
127+
return resp.Status, nil
150128
}
151129

152130
func (c *Client) ResetAgent(id string) (string, error) {
153-
log.Printf("[INFO] Resetting agent %s in Keyfactor.\n", id)
154131

155-
// Set Keyfactor-specific headers
156-
headers := &apiHeaders{
157-
Headers: []StringTuple{
158-
{"x-keyfactor-api-version", "1"},
159-
{"x-keyfactor-requested-with", "APIClient"},
160-
},
161-
}
132+
xKeyfactorRequestedWith := "APIClient"
133+
xKeyfactorApiVersion := "1"
162134

163-
keyfactorAPIStruct := &request{
164-
Method: "POST",
165-
Endpoint: fmt.Sprintf("Agents/%s/Reset", id),
166-
Headers: headers,
167-
Payload: nil,
168-
}
135+
configuration := keyfactor.NewConfiguration(make(map[string]string))
136+
apiClient := keyfactor.NewAPIClient(configuration)
169137

170-
resp, rErr := c.sendRequest(keyfactorAPIStruct)
171-
if rErr != nil {
172-
return "", rErr
173-
}
138+
resp, err := apiClient.AgentApi.AgentReset1(context.Background(), id).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
174139

175140
if resp.StatusCode == 204 {
176141
return "Reset agent successful.", nil
177142
}
178-
var jsonResp string
179-
err := json.NewDecoder(resp.Body).Decode(&jsonResp)
143+
180144
if err != nil {
181145
return "", err
182146
}
183-
return jsonResp, nil
147+
148+
return resp.Status, nil
184149
}
185150

186151
func (c *Client) FetchAgentLogs(id string) (string, error) {
187-
log.Printf("[INFO] Fetching agent logs for %s.\n", id)
188152

189-
// Set Keyfactor-specific headers
190-
headers := &apiHeaders{
191-
Headers: []StringTuple{
192-
{"x-keyfactor-api-version", "1"},
193-
{"x-keyfactor-requested-with", "APIClient"},
194-
},
195-
}
153+
xKeyfactorRequestedWith := "APIClient"
154+
xKeyfactorApiVersion := "1"
196155

197-
keyfactorAPIStruct := &request{
198-
Method: "POST",
199-
Endpoint: fmt.Sprintf("Agents/%s/FetchLogs", id),
200-
Headers: headers,
201-
Payload: nil,
202-
}
156+
configuration := keyfactor.NewConfiguration(make(map[string]string))
157+
apiClient := keyfactor.NewAPIClient(configuration)
203158

204-
resp, rErr := c.sendRequest(keyfactorAPIStruct)
205-
if rErr != nil {
206-
return "", rErr
207-
}
159+
resp, err := apiClient.AgentApi.AgentFetchLogs(context.Background(), id).XKeyfactorRequestedWith(xKeyfactorRequestedWith).XKeyfactorApiVersion(xKeyfactorApiVersion).Execute()
208160

209161
if resp.StatusCode == 204 {
210-
return "Fetch logs successful.", nil
162+
return "Reset agent successful.", nil
211163
}
212-
var jsonResp string
213-
err := json.NewDecoder(resp.Body).Decode(&jsonResp)
164+
214165
if err != nil {
215166
return "", err
216167
}
217-
return jsonResp, nil
168+
169+
return resp.Status, nil
218170
}

0 commit comments

Comments
 (0)