|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | +) |
| 8 | + |
| 9 | +// ListPAMProviders returns all PAM providers according to the provided filter and output parameters |
| 10 | +func (c *Client) ListPAMProviders(query *GetPAMProviderQuery) (*[]ProviderResponseLegacy, error) { |
| 11 | + log.Println("[INFO] Listing all PAM providers") |
| 12 | + |
| 13 | + headers := &apiHeaders{ |
| 14 | + Headers: []StringTuple{ |
| 15 | + {"x-keyfactor-api-version", "1"}, |
| 16 | + {"x-keyfactor-requested-with", "APIClient"}, |
| 17 | + }, |
| 18 | + } |
| 19 | + |
| 20 | + endpoint := "PamProviders" |
| 21 | + if query != nil { |
| 22 | + queryParams := query.toQueryString() |
| 23 | + if queryParams != "" { |
| 24 | + endpoint = fmt.Sprintf("%s?%s", endpoint, queryParams) |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + keyfactorAPIStruct := &request{ |
| 29 | + Method: "GET", |
| 30 | + Endpoint: endpoint, |
| 31 | + Headers: headers, |
| 32 | + Payload: nil, |
| 33 | + } |
| 34 | + |
| 35 | + resp, err := c.sendRequest(keyfactorAPIStruct) |
| 36 | + if err != nil { |
| 37 | + return nil, err |
| 38 | + } |
| 39 | + |
| 40 | + var jsonResp []ProviderResponseLegacy |
| 41 | + err = json.NewDecoder(resp.Body).Decode(&jsonResp) |
| 42 | + if err != nil { |
| 43 | + return nil, err |
| 44 | + } |
| 45 | + return &jsonResp, nil |
| 46 | +} |
| 47 | + |
| 48 | +// GetPAMProvider returns a specific PAM provider by ID |
| 49 | +func (c *Client) GetPAMProvider(id int) (*ProviderResponseLegacy, error) { |
| 50 | + log.Printf("[INFO] Getting PAM provider with ID: %d", id) |
| 51 | + |
| 52 | + headers := &apiHeaders{ |
| 53 | + Headers: []StringTuple{ |
| 54 | + {"x-keyfactor-api-version", "1"}, |
| 55 | + {"x-keyfactor-requested-with", "APIClient"}, |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + endpoint := fmt.Sprintf("PamProviders/%d", id) |
| 60 | + keyfactorAPIStruct := &request{ |
| 61 | + Method: "GET", |
| 62 | + Endpoint: endpoint, |
| 63 | + Headers: headers, |
| 64 | + Payload: nil, |
| 65 | + } |
| 66 | + |
| 67 | + resp, err := c.sendRequest(keyfactorAPIStruct) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + |
| 72 | + var jsonResp ProviderResponseLegacy |
| 73 | + err = json.NewDecoder(resp.Body).Decode(&jsonResp) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + return &jsonResp, nil |
| 78 | +} |
| 79 | + |
| 80 | +// CreatePAMProvider creates a new PAM provider with the associated properties |
| 81 | +func (c *Client) CreatePAMProvider(provider *ProviderCreateRequest) (*ProviderResponseLegacy, error) { |
| 82 | + log.Printf("[INFO] Creating new PAM provider: %s", provider.Name) |
| 83 | + |
| 84 | + headers := &apiHeaders{ |
| 85 | + Headers: []StringTuple{ |
| 86 | + {"x-keyfactor-api-version", "1"}, |
| 87 | + {"x-keyfactor-requested-with", "APIClient"}, |
| 88 | + {"Content-Type", "application/json"}, |
| 89 | + }, |
| 90 | + } |
| 91 | + |
| 92 | + keyfactorAPIStruct := &request{ |
| 93 | + Method: "POST", |
| 94 | + Endpoint: "PamProviders", |
| 95 | + Headers: headers, |
| 96 | + Payload: provider, |
| 97 | + } |
| 98 | + |
| 99 | + resp, err := c.sendRequest(keyfactorAPIStruct) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + var jsonResp ProviderResponseLegacy |
| 105 | + err = json.NewDecoder(resp.Body).Decode(&jsonResp) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + return &jsonResp, nil |
| 110 | +} |
| 111 | + |
| 112 | +// UpdatePAMProvider updates an existing PAM provider |
| 113 | +func (c *Client) UpdatePAMProvider(provider *ProviderUpdateRequestLegacy) (*ProviderResponseLegacy, error) { |
| 114 | + log.Printf("[INFO] Updating PAM provider with ID: %d", provider.Id) |
| 115 | + |
| 116 | + headers := &apiHeaders{ |
| 117 | + Headers: []StringTuple{ |
| 118 | + {"x-keyfactor-api-version", "1"}, |
| 119 | + {"x-keyfactor-requested-with", "APIClient"}, |
| 120 | + {"Content-Type", "application/json"}, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + keyfactorAPIStruct := &request{ |
| 125 | + Method: "PUT", |
| 126 | + Endpoint: "PamProviders", |
| 127 | + Headers: headers, |
| 128 | + Payload: provider, |
| 129 | + } |
| 130 | + |
| 131 | + resp, err := c.sendRequest(keyfactorAPIStruct) |
| 132 | + if err != nil { |
| 133 | + return nil, err |
| 134 | + } |
| 135 | + |
| 136 | + var jsonResp ProviderResponseLegacy |
| 137 | + err = json.NewDecoder(resp.Body).Decode(&jsonResp) |
| 138 | + if err != nil { |
| 139 | + return nil, err |
| 140 | + } |
| 141 | + return &jsonResp, nil |
| 142 | +} |
| 143 | + |
| 144 | +// DeletePAMProvider deletes a PAM provider by ID |
| 145 | +func (c *Client) DeletePAMProvider(id int) error { |
| 146 | + log.Printf("[INFO] Deleting PAM provider with ID: %d", id) |
| 147 | + |
| 148 | + headers := &apiHeaders{ |
| 149 | + Headers: []StringTuple{ |
| 150 | + {"x-keyfactor-api-version", "1"}, |
| 151 | + {"x-keyfactor-requested-with", "APIClient"}, |
| 152 | + }, |
| 153 | + } |
| 154 | + |
| 155 | + endpoint := fmt.Sprintf("PamProviders/%d", id) |
| 156 | + keyfactorAPIStruct := &request{ |
| 157 | + Method: "DELETE", |
| 158 | + Endpoint: endpoint, |
| 159 | + Headers: headers, |
| 160 | + Payload: nil, |
| 161 | + } |
| 162 | + |
| 163 | + _, err := c.sendRequest(keyfactorAPIStruct) |
| 164 | + if err != nil { |
| 165 | + return err |
| 166 | + } |
| 167 | + |
| 168 | + return nil |
| 169 | +} |
| 170 | + |
| 171 | +// ListLocalPAMEntries returns local PAM entries for the given PAM provider according to the provided filter |
| 172 | +func (c *Client) ListLocalPAMEntries(providerId int, query *GetPAMProviderQuery) (*[]LocalPAMEntryResponse, error) { |
| 173 | + log.Printf("[INFO] Listing local PAM entries for provider ID: %d", providerId) |
| 174 | + |
| 175 | + headers := &apiHeaders{ |
| 176 | + Headers: []StringTuple{ |
| 177 | + {"x-keyfactor-api-version", "1"}, |
| 178 | + {"x-keyfactor-requested-with", "APIClient"}, |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + endpoint := fmt.Sprintf("PamProviders/Local/%d/Entries", providerId) |
| 183 | + if query != nil { |
| 184 | + queryParams := query.toQueryString() |
| 185 | + if queryParams != "" { |
| 186 | + endpoint = fmt.Sprintf("%s?%s", endpoint, queryParams) |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + keyfactorAPIStruct := &request{ |
| 191 | + Method: "GET", |
| 192 | + Endpoint: endpoint, |
| 193 | + Headers: headers, |
| 194 | + Payload: nil, |
| 195 | + } |
| 196 | + |
| 197 | + resp, err := c.sendRequest(keyfactorAPIStruct) |
| 198 | + if err != nil { |
| 199 | + return nil, err |
| 200 | + } |
| 201 | + |
| 202 | + var jsonResp []LocalPAMEntryResponse |
| 203 | + err = json.NewDecoder(resp.Body).Decode(&jsonResp) |
| 204 | + if err != nil { |
| 205 | + return nil, err |
| 206 | + } |
| 207 | + return &jsonResp, nil |
| 208 | +} |
| 209 | + |
| 210 | +// CreateLocalPAMEntry creates a new local PAM entry for the given PAM provider |
| 211 | +func (c *Client) CreateLocalPAMEntry(providerId int, entry *LocalPAMEntryCreateRequest) ( |
| 212 | + *LocalPAMEntryResponse, |
| 213 | + error, |
| 214 | +) { |
| 215 | + log.Printf("[INFO] Creating local PAM entry for provider ID: %d", providerId) |
| 216 | + |
| 217 | + headers := &apiHeaders{ |
| 218 | + Headers: []StringTuple{ |
| 219 | + {"x-keyfactor-api-version", "1"}, |
| 220 | + {"x-keyfactor-requested-with", "APIClient"}, |
| 221 | + {"Content-Type", "application/json"}, |
| 222 | + }, |
| 223 | + } |
| 224 | + |
| 225 | + endpoint := fmt.Sprintf("PamProviders/Local/%d/Entries", providerId) |
| 226 | + keyfactorAPIStruct := &request{ |
| 227 | + Method: "POST", |
| 228 | + Endpoint: endpoint, |
| 229 | + Headers: headers, |
| 230 | + Payload: entry, |
| 231 | + } |
| 232 | + |
| 233 | + resp, err := c.sendRequest(keyfactorAPIStruct) |
| 234 | + if err != nil { |
| 235 | + return nil, err |
| 236 | + } |
| 237 | + |
| 238 | + var jsonResp LocalPAMEntryResponse |
| 239 | + err = json.NewDecoder(resp.Body).Decode(&jsonResp) |
| 240 | + if err != nil { |
| 241 | + return nil, err |
| 242 | + } |
| 243 | + return &jsonResp, nil |
| 244 | +} |
| 245 | + |
| 246 | +// UpdateLocalPAMEntry updates an existing local PAM entry for the given PAM provider |
| 247 | +func (c *Client) UpdateLocalPAMEntry( |
| 248 | + providerId int, |
| 249 | + secretName string, |
| 250 | + entry *LocalPAMEntryUpdateRequest, |
| 251 | +) (*LocalPAMEntryResponse, error) { |
| 252 | + log.Printf("[INFO] Updating local PAM entry '%s' for provider ID: %d", secretName, providerId) |
| 253 | + |
| 254 | + headers := &apiHeaders{ |
| 255 | + Headers: []StringTuple{ |
| 256 | + {"x-keyfactor-api-version", "1"}, |
| 257 | + {"x-keyfactor-requested-with", "APIClient"}, |
| 258 | + {"Content-Type", "application/json"}, |
| 259 | + }, |
| 260 | + } |
| 261 | + |
| 262 | + endpoint := fmt.Sprintf("PamProviders/Local/%d/Entries/%s", providerId, secretName) |
| 263 | + keyfactorAPIStruct := &request{ |
| 264 | + Method: "PUT", |
| 265 | + Endpoint: endpoint, |
| 266 | + Headers: headers, |
| 267 | + Payload: entry, |
| 268 | + } |
| 269 | + |
| 270 | + resp, err := c.sendRequest(keyfactorAPIStruct) |
| 271 | + if err != nil { |
| 272 | + return nil, err |
| 273 | + } |
| 274 | + |
| 275 | + var jsonResp LocalPAMEntryResponse |
| 276 | + err = json.NewDecoder(resp.Body).Decode(&jsonResp) |
| 277 | + if err != nil { |
| 278 | + return nil, err |
| 279 | + } |
| 280 | + return &jsonResp, nil |
| 281 | +} |
| 282 | + |
| 283 | +// DeleteLocalPAMEntry deletes a local PAM entry for the given PAM provider |
| 284 | +func (c *Client) DeleteLocalPAMEntry(providerId int, secretName string) error { |
| 285 | + log.Printf("[INFO] Deleting local PAM entry '%s' for provider ID: %d", secretName, providerId) |
| 286 | + |
| 287 | + headers := &apiHeaders{ |
| 288 | + Headers: []StringTuple{ |
| 289 | + {"x-keyfactor-api-version", "1"}, |
| 290 | + {"x-keyfactor-requested-with", "APIClient"}, |
| 291 | + }, |
| 292 | + } |
| 293 | + |
| 294 | + endpoint := fmt.Sprintf("PamProviders/Local/%d/Entries/%s", providerId, secretName) |
| 295 | + keyfactorAPIStruct := &request{ |
| 296 | + Method: "DELETE", |
| 297 | + Endpoint: endpoint, |
| 298 | + Headers: headers, |
| 299 | + Payload: nil, |
| 300 | + } |
| 301 | + |
| 302 | + _, err := c.sendRequest(keyfactorAPIStruct) |
| 303 | + if err != nil { |
| 304 | + return err |
| 305 | + } |
| 306 | + |
| 307 | + return nil |
| 308 | +} |
| 309 | + |
| 310 | +// GetPAMProviderQuery represents query parameters for PAM provider listing |
| 311 | +type GetPAMProviderQuery struct { |
| 312 | + QueryString string |
| 313 | + PageReturned int |
| 314 | + ReturnLimit int |
| 315 | + SortField string |
| 316 | + SortAscending int |
| 317 | +} |
| 318 | + |
| 319 | +// toQueryString converts query parameters to URL query string |
| 320 | +func (q *GetPAMProviderQuery) toQueryString() string { |
| 321 | + if q == nil { |
| 322 | + return "" |
| 323 | + } |
| 324 | + |
| 325 | + params := "" |
| 326 | + if q.QueryString != "" { |
| 327 | + params += fmt.Sprintf("QueryString=%s&", q.QueryString) |
| 328 | + } |
| 329 | + if q.PageReturned > 0 { |
| 330 | + params += fmt.Sprintf("PageReturned=%d&", q.PageReturned) |
| 331 | + } |
| 332 | + if q.ReturnLimit > 0 { |
| 333 | + params += fmt.Sprintf("ReturnLimit=%d&", q.ReturnLimit) |
| 334 | + } |
| 335 | + if q.SortField != "" { |
| 336 | + params += fmt.Sprintf("SortField=%s&", q.SortField) |
| 337 | + // Only add SortAscending if SortField is provided |
| 338 | + params += fmt.Sprintf("SortAscending=%d&", q.SortAscending) |
| 339 | + } |
| 340 | + |
| 341 | + // Remove trailing '&' |
| 342 | + if len(params) > 0 && params[len(params)-1] == '&' { |
| 343 | + params = params[:len(params)-1] |
| 344 | + } |
| 345 | + |
| 346 | + return params |
| 347 | +} |
0 commit comments