Skip to content

Commit e7634c8

Browse files
committed
fix(client): Add environmental variable KEYFACTOR_API_PATH to allow for custom API path specification.
fix(store): Inventory to parse from kf10 correctly.
1 parent 4f8f529 commit e7634c8

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

v2/api/client.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ var (
2121
EnvCommandUsername = "KEYFACTOR_USERNAME"
2222
EnvCommandPassword = "KEYFACTOR_PASSWORD"
2323
EnvCommandDomain = "KEYFACTOR_DOMAIN"
24+
EnvCommandAPI = "KEYFACTOR_API_PATH"
25+
DefaultAPIPath = "KeyfactorAPI"
2426
)
2527

2628
type Client struct {
2729
hostname string
2830
httpClient *http.Client
2931
basicAuthString string
32+
apiPath string
3033
}
3134

3235
// AuthConfig is a struct holding all necessary client configuration data
@@ -37,6 +40,7 @@ type AuthConfig struct {
3740
Username string
3841
Password string
3942
Domain string
43+
APIPath string
4044
}
4145

4246
// NewKeyfactorClient creates a new Keyfactor client instance. A configured Client is returned with methods used to
@@ -61,6 +65,14 @@ func loginToKeyfactor(auth *AuthConfig) (*Client, error) {
6165
return nil, fmt.Errorf("%s is required", EnvCommandHostname)
6266
}
6367
}
68+
if auth.APIPath == "" {
69+
envAPIPath := os.Getenv(EnvCommandAPI)
70+
if envAPIPath != "" {
71+
auth.APIPath = envAPIPath
72+
} else {
73+
auth.APIPath = DefaultAPIPath
74+
}
75+
}
6476
if auth.Username == "" {
6577
envUsername := os.Getenv(EnvCommandUsername)
6678
if envUsername != "" {
@@ -103,6 +115,7 @@ func loginToKeyfactor(auth *AuthConfig) (*Client, error) {
103115
hostname: auth.Hostname,
104116
httpClient: &http.Client{Timeout: 10 * time.Second},
105117
basicAuthString: buildBasicAuthString(auth),
118+
apiPath: auth.APIPath,
106119
}
107120

108121
_, err := c.sendRequest(keyfactorAPIStruct)
@@ -129,7 +142,7 @@ func (c *Client) sendRequest(request *request) (*http.Response, error) {
129142
if u.Scheme != "https" {
130143
u.Scheme = "https"
131144
}
132-
endpoint := "KeyfactorAPI/" + request.Endpoint
145+
endpoint := fmt.Sprintf("%s/", strings.Trim(c.apiPath, "/")) + request.Endpoint
133146
u.Path = path.Join(u.Path, endpoint) // Attach enroll endpoint
134147

135148
// Set request query

v2/api/store.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -406,9 +406,22 @@ func (c *Client) GetCertStoreInventory(storeId string) (*[]CertStoreInventory, e
406406
if !ok {
407407
params = map[string]interface{}{}
408408
}
409+
410+
certMetadata := storedCert.(map[string]interface{})
411+
// certMetadata["Certificates"] is an array of interface{}s
412+
invItemId := 0
413+
if certMetadata["Certificates"] != nil {
414+
certsList := certMetadata["Certificates"].([]interface{})
415+
certInstances := certsList[0].(map[string]interface{}) // todo: will this ever contain > 1?
416+
invItemId = int(certInstances["Id"].(float64))
417+
//certInstances := certsList["Certificates"].(map[string]interface{})
418+
//invItemId = int(certInstances["Id"].(float64))
419+
//invItemId = int(certObjs.([]interface{})[0].(map[string]interface{})["Id"].(float64))
420+
}
421+
409422
invC := CertStoreInventory{
410423
Name: storedCert.(map[string]interface{})["Name"].(string),
411-
CertStoreInventoryItemId: int(storedCert.(map[string]interface{})["CertStoreInventoryItemId"].(float64)),
424+
CertStoreInventoryItemId: invItemId,
412425
Certificates: []InventoriedCertificate{},
413426
Parameters: params,
414427
Thumbprints: map[string]bool{},

0 commit comments

Comments
 (0)