|
1 | 1 | package api |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "encoding/json" |
| 4 | + "context" |
5 | 5 | "fmt" |
6 | | - "log" |
| 6 | + |
| 7 | + "github.com/Keyfactor/keyfactor-go-client-sdk/api/keyfactor" |
7 | 8 | ) |
8 | 9 |
|
9 | 10 | // GetAgentList returns a list of orchestrators registered in the Keyfactor instance |
10 | 11 | func (c *Client) GetAgentList() ([]Agent, error) { |
11 | | - log.Println("[INFO] Getting a list of agents registered in Keyfactor") |
12 | 12 |
|
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" |
20 | 15 |
|
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) |
27 | 18 |
|
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 |
32 | 22 |
|
33 | | - var jsonResp []Agent |
34 | | - err = json.NewDecoder(resp.Body).Decode(&jsonResp) |
35 | 23 | 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 |
39 | 46 | } |
40 | 47 |
|
41 | 48 | func (c *Client) GetAgent(id string) ([]Agent, error) { |
42 | | - log.Println("[INFO] Getting agent by ID or name.") |
43 | 49 |
|
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" |
65 | 52 |
|
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 |
70 | 59 |
|
71 | | - var jsonResp []Agent |
72 | | - err = json.NewDecoder(resp.Body).Decode(&jsonResp) |
73 | 60 | 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 | + }, |
78 | 79 | } |
79 | | - return jsonResp, nil |
| 80 | + |
| 81 | + return revResp, nil |
80 | 82 | } |
81 | 83 |
|
82 | 84 | func (c *Client) ApproveAgent(id string) (string, error) { |
83 | | - log.Printf("[INFO] Approving agent %s in Keyfactor.\n", id) |
84 | 85 |
|
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" |
92 | 88 |
|
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) |
99 | 91 |
|
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() |
104 | 95 |
|
105 | 96 | if resp.StatusCode == 204 { |
106 | 97 | return "Approve agent successful.", nil |
107 | 98 | } |
108 | 99 |
|
109 | | - var jsonResp string |
110 | | - err := json.NewDecoder(resp.Body).Decode(&jsonResp) |
111 | 100 | if err != nil { |
112 | 101 | return "", err |
113 | 102 | } |
114 | | - return jsonResp, nil |
| 103 | + |
| 104 | + return resp.Status, nil |
115 | 105 | } |
116 | 106 |
|
117 | 107 | func (c *Client) DisApproveAgent(id string) (string, error) { |
118 | | - log.Printf("[INFO] Disapproving agent %s in Keyfactor.\n", id) |
119 | 108 |
|
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" |
127 | 111 |
|
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) |
134 | 114 |
|
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() |
139 | 118 |
|
140 | 119 | if resp.StatusCode == 204 { |
141 | 120 | return fmt.Sprintf("Disapproving %s successful.", id), nil |
142 | 121 | } |
143 | 122 |
|
144 | | - var jsonResp string |
145 | | - err := json.NewDecoder(resp.Body).Decode(&jsonResp) |
146 | 123 | if err != nil { |
147 | 124 | return "", err |
148 | 125 | } |
149 | | - return jsonResp, nil |
| 126 | + |
| 127 | + return resp.Status, nil |
150 | 128 | } |
151 | 129 |
|
152 | 130 | func (c *Client) ResetAgent(id string) (string, error) { |
153 | | - log.Printf("[INFO] Resetting agent %s in Keyfactor.\n", id) |
154 | 131 |
|
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" |
162 | 134 |
|
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) |
169 | 137 |
|
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() |
174 | 139 |
|
175 | 140 | if resp.StatusCode == 204 { |
176 | 141 | return "Reset agent successful.", nil |
177 | 142 | } |
178 | | - var jsonResp string |
179 | | - err := json.NewDecoder(resp.Body).Decode(&jsonResp) |
| 143 | + |
180 | 144 | if err != nil { |
181 | 145 | return "", err |
182 | 146 | } |
183 | | - return jsonResp, nil |
| 147 | + |
| 148 | + return resp.Status, nil |
184 | 149 | } |
185 | 150 |
|
186 | 151 | func (c *Client) FetchAgentLogs(id string) (string, error) { |
187 | | - log.Printf("[INFO] Fetching agent logs for %s.\n", id) |
188 | 152 |
|
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" |
196 | 155 |
|
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) |
203 | 158 |
|
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() |
208 | 160 |
|
209 | 161 | if resp.StatusCode == 204 { |
210 | | - return "Fetch logs successful.", nil |
| 162 | + return "Reset agent successful.", nil |
211 | 163 | } |
212 | | - var jsonResp string |
213 | | - err := json.NewDecoder(resp.Body).Decode(&jsonResp) |
| 164 | + |
214 | 165 | if err != nil { |
215 | 166 | return "", err |
216 | 167 | } |
217 | | - return jsonResp, nil |
| 168 | + |
| 169 | + return resp.Status, nil |
218 | 170 | } |
0 commit comments