Skip to content

Commit 551bf00

Browse files
committed
feat(agent): Agent's can be looked up by GUID or ClientMachine name.
feat(store): Certificate stores can be looked up using a combination of ClientMachine name and StorePath. feat(model): Adding `DisplayName` to `GetCertificateStoreResponse` fix(model): `CreateStoreResponse` now uses correct JSON element `CertStoreType` feat(model): Added the following to accommodate updating Command reserved store parameters via PUT: `SpecialPropertiesSecretValue`,`SecretParamValue`, `SpecialPropertiesValue`
1 parent 5e78ed0 commit 551bf00

File tree

4 files changed

+123
-8
lines changed

4 files changed

+123
-8
lines changed

main.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import (
99
func main() {
1010
var showVersion bool
1111

12-
version := os.Getenv("GITHUB_REF_NAME")
13-
if version == "" {
14-
version = "1.0.0"
15-
}
12+
version := "2.0.0"
1613
flag.BoolVar(&showVersion, "version", false, "Print version information.")
1714
flag.BoolVar(&showVersion, "v", false, "Print version information.")
1815
flag.Parse()

v2/api/agent.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"log"
7+
"regexp"
78
)
89

910
// GetAgentList returns a list of orchestrators registered in the Keyfactor instance
@@ -51,9 +52,16 @@ func (c *Client) GetAgent(id string) ([]Agent, error) {
5152
query := apiQuery{
5253
Query: []StringTuple{},
5354
}
54-
query.Query = append(query.Query, StringTuple{
55-
"pq.queryString", fmt.Sprintf(`ClientMachine -eq "%s"`, id),
56-
})
55+
56+
if isGUID(id) {
57+
query.Query = append(query.Query, StringTuple{
58+
"pq.queryString", fmt.Sprintf("AgentId -eq \"%s\"", id),
59+
})
60+
} else {
61+
query.Query = append(query.Query, StringTuple{
62+
"pq.queryString", fmt.Sprintf("ClientMachine -eq \"%s\"", id),
63+
})
64+
}
5765

5866
keyfactorAPIStruct := &request{
5967
Method: "GET",
@@ -216,3 +224,9 @@ func (c *Client) FetchAgentLogs(id string) (string, error) {
216224
}
217225
return jsonResp, nil
218226
}
227+
228+
func isGUID(input string) bool {
229+
guidPattern := `^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$`
230+
match, _ := regexp.MatchString(guidPattern, input)
231+
return match
232+
}

v2/api/store.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,97 @@ func (c *Client) GetCertificateStoreByContainerID(containerID interface{}) (*[]G
302302
return jsonResp, nil
303303
}
304304

305+
func (c *Client) GetCertificateStoreByClientAndStorePath(clientMachine string, storePath, containerID interface{}) (*[]GetCertificateStoreResponse, error) {
306+
307+
query := apiQuery{
308+
Query: []StringTuple{},
309+
}
310+
311+
fullQueryString := ""
312+
switch containerID.(type) {
313+
case int, int64:
314+
contIdInt := int(containerID.(int64))
315+
if contIdInt > 0 {
316+
//query.Query = append(query.Query, StringTuple{
317+
// "certificateStoreQuery.queryString", fmt.Sprintf(`ContainerId -eq "%d"`, containerID),
318+
//})
319+
//append to fullQueryString
320+
fullQueryString = fmt.Sprintf(`ContainerId -eq "%d"`, contIdInt)
321+
}
322+
case string:
323+
//ct, ctErr := c.GetStoreContainer(containerID.(string))
324+
//if ctErr != nil {
325+
// return nil, ctErr
326+
//}
327+
//query.Query = append(query.Query, StringTuple{
328+
// "certificateStoreQuery.queryString", fmt.Sprintf(`ContainerId -eq %d`, *ct.Id),
329+
//})
330+
//append to fullQueryString
331+
fullQueryString = fmt.Sprintf(`ContainerId -eq "%s"`, containerID)
332+
}
333+
334+
if storePath != nil {
335+
if fullQueryString != "" {
336+
fullQueryString = fmt.Sprintf(`%s AND StorePath -eq "%s"`, fullQueryString, storePath)
337+
} else {
338+
fullQueryString = fmt.Sprintf(`StorePath -eq "%s"`, storePath)
339+
}
340+
}
341+
342+
if clientMachine != "" {
343+
if fullQueryString != "" {
344+
fullQueryString = fmt.Sprintf(`%s AND ClientMachine -eq "%s"`, fullQueryString, clientMachine)
345+
} else {
346+
fullQueryString = fmt.Sprintf(`ClientMachine -eq "%s"`, clientMachine)
347+
}
348+
}
349+
350+
if fullQueryString != "" {
351+
query.Query = append(query.Query, StringTuple{
352+
"certificateStoreQuery.queryString", fullQueryString,
353+
})
354+
}
355+
356+
// Set Keyfactor-specific headers
357+
headers := &apiHeaders{
358+
Headers: []StringTuple{
359+
{"x-keyfactor-api-version", "1"},
360+
{"x-keyfactor-requested-with", "APIClient"},
361+
},
362+
}
363+
364+
endpoint := "CertificateStores"
365+
366+
var keyfactorAPIStruct *request
367+
if query.Query != nil {
368+
keyfactorAPIStruct = &request{
369+
Method: "GET",
370+
Endpoint: endpoint,
371+
Headers: headers,
372+
Query: &query,
373+
}
374+
} else {
375+
keyfactorAPIStruct = &request{
376+
Method: "GET",
377+
Endpoint: endpoint,
378+
Headers: headers,
379+
}
380+
}
381+
382+
resp, err := c.sendRequest(keyfactorAPIStruct)
383+
if err != nil {
384+
return nil, err
385+
}
386+
387+
jsonResp := &[]GetCertificateStoreResponse{}
388+
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
389+
if err != nil {
390+
return nil, err
391+
}
392+
//jsonResp.Properties = unmarshalPropertiesString(jsonResp.PropertiesString)
393+
return jsonResp, nil
394+
}
395+
305396
// AddCertificateToStores takes argument for a AddCertificateToStore structure and is used to remove a configured certificate
306397
// from one or more certificate stores.
307398
func (c *Client) AddCertificateToStores(config *AddCertificateToStore) ([]string, error) {

v2/api/store_models.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ type GetCertificateStoreResponse struct {
163163
ReenrollmentStatus ReEnrollmnentConfig `json:"ReenrollmentStatus,omitempty"`
164164
SetNewPasswordAllowed bool `json:"SetNewPasswordAllowed,omitempty"`
165165
Password StorePasswordConfig `json:"Password,omitempty"`
166+
DisplayName string `json:"DisplayName,omitempty"`
166167
}
167168

168169
// PropertyDefinition defines property fields associated with a certificate store type, and is returned by the
@@ -184,7 +185,7 @@ type CreateStoreResponse struct {
184185
ClientMachine string `json:"ClientMachine"`
185186
Storepath string `json:"Storepath"`
186187
CertStoreInventoryJobId string `json:"CertStoreInventoryJobId"`
187-
CertStoreType int `json:"cert_store_type"`
188+
CertStoreType int `json:"CertStoreType"`
188189
Approved bool `json:"Approved"`
189190
CreateIfMissing bool `json:"CreateIfMissing"`
190191
PropertiesString string `json:"Properties"`
@@ -296,3 +297,15 @@ type EntryPassword struct {
296297
// An integer that identifies the PAM provider used to store the password.
297298
Provider int `json:"Provider,omitempty"`
298299
}
300+
301+
type SpecialPropertiesSecretValue struct {
302+
Value SecretParamValue `json:"value"`
303+
}
304+
305+
type SpecialPropertiesValue struct {
306+
Value interface{} `json:"value"`
307+
}
308+
309+
type SecretParamValue struct {
310+
SecretValue string `json:"SecretValue"`
311+
}

0 commit comments

Comments
 (0)