Skip to content

Commit 4a82ef4

Browse files
committed
fix(client): Fixed returned error messages to be a little more friendly rather than a dump of a map[string]interface{}
feat(store_types): Added create and update functions. fix(store_types): Changed `StoreTypePropertyDefinition` to allow different `DefaultValue` by converting to interface.
1 parent c4ca48a commit 4a82ef4

File tree

4 files changed

+89
-85
lines changed

4 files changed

+89
-85
lines changed

api/client.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (c *Client) sendRequest(request *request) (*http.Response, error) {
162162
err = errors.New("401 - Unauthorized: Access is denied due to invalid credentials.")
163163
return nil, err
164164
} else {
165-
var errorMessage interface{} // Decode JSON body to handle issue
165+
var errorMessage map[string]interface{} // Decode JSON body to handle issue
166166
err = json.NewDecoder(resp.Body).Decode(&errorMessage)
167167

168168
if err != nil {
@@ -175,7 +175,12 @@ func (c *Client) sendRequest(request *request) (*http.Response, error) {
175175
}
176176

177177
log.Printf("[DEBUG] Request failed with code %d and message %v", resp.StatusCode, errorMessage)
178-
stringMessage = fmt.Sprintf("%v", errorMessage)
178+
var fOps []string
179+
for _, v := range errorMessage["FailedOperations"].([]interface{}) {
180+
fOps = append(fOps, fmt.Sprintf("%s", v.(map[string]interface{})["Reason"]))
181+
}
182+
fOpsStr := strings.Join(fOps, ", ")
183+
stringMessage = fmt.Sprintf("%s. %s", errorMessage["Message"], fOpsStr)
179184
return nil, errors.New(stringMessage)
180185
}
181186
}

api/store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ func (c *Client) GetCertStoreInventory(storeId string) (*[]CertStoreInventory, e
337337
}
338338
for _, cert := range storedCert.(map[string]interface{})["Certificates"].([]interface{}) {
339339
iCert := InventoriedCertificate{
340-
Id: int(cert.(map[string]interface{})["ID"].(float64)),
340+
Id: int(cert.(map[string]interface{})["Id"].(float64)),
341341
IssuedDN: cert.(map[string]interface{})["IssuedDN"].(string),
342342
SerialNumber: cert.(map[string]interface{})["SerialNumber"].(string),
343343
NotBefore: cert.(map[string]interface{})["NotBefore"].(string),
@@ -350,7 +350,7 @@ func (c *Client) GetCertStoreInventory(storeId string) (*[]CertStoreInventory, e
350350
invC.Certificates = append(invC.Certificates, iCert)
351351
invC.Thumbprints[cert.(map[string]interface{})["Thumbprint"].(string)] = true
352352
invC.Serials[cert.(map[string]interface{})["SerialNumber"].(string)] = true
353-
invC.Ids[int(cert.(map[string]interface{})["ID"].(float64))] = true
353+
invC.Ids[int(cert.(map[string]interface{})["Id"].(float64))] = true
354354
invResp = append(invResp, invC)
355355
}
356356
}

api/store_type.go

Lines changed: 73 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -126,80 +126,79 @@ func (c *Client) ListCertificateStoreTypes() (*[]CertificateStoreType, error) {
126126
// - StorePath : string
127127
// - Properties : []StringTuple *Note - Method converts this array of StringTuples to a JSON string if provided
128128
// - AgentId : string
129-
//
130-
// func (c *Client) CreateStoreType(ca *CertificateStoreType) (*CertificateStoreType, error) {
131-
// log.Println("[INFO] Creating new certificate store type with Keyfactor")
132-
//
133-
// // Validate that the required fields are present
134-
// //err := validateCreateStoreTypeArgs(ca)
135-
// //if err != nil {
136-
// // return nil, err
137-
// //}
138-
//
139-
// // Set Keyfactor-specific headers
140-
// headers := &apiHeaders{
141-
// Headers: []StringTuple{
142-
// {"x-keyfactor-api-version", "1"},
143-
// {"x-keyfactor-requested-with", "APIClient"},
144-
// },
145-
// }
146-
//
147-
// keyfactorAPIStruct := &request{
148-
// Method: "POST",
149-
// Endpoint: "CertificateStoreType",
150-
// Headers: headers,
151-
// Payload: &ca,
152-
// }
153-
//
154-
// resp, err := c.sendRequest(keyfactorAPIStruct)
155-
// if err != nil {
156-
// return nil, err
157-
// }
158-
//
159-
// jsonResp := &CertStoreTypeResponse{}
160-
// err = json.NewDecoder(resp.Body).Decode(&jsonResp)
161-
// if err != nil {
162-
// return nil, err
163-
// }
164-
// return jsonResp, nil
165-
// }
166-
//
167-
// func (c *Client) UpdateStoreType(ca *CertificateStoreType) (*CertificateStoreType, error) {
168-
// log.Println("[INFO] Creating new certificate store type with Keyfactor")
169-
//
170-
// // Validate that the required fields are present
171-
// //err := validateCreateStoreTypeArgs(ca)
172-
// //if err != nil {
173-
// // return nil, err
174-
// //}
175-
//
176-
// // Set Keyfactor-specific headers
177-
// headers := &apiHeaders{
178-
// Headers: []StringTuple{
179-
// {"x-keyfactor-api-version", "1"},
180-
// {"x-keyfactor-requested-with", "APIClient"},
181-
// },
182-
// }
183-
//
184-
// keyfactorAPIStruct := &request{
185-
// Method: "POST",
186-
// Endpoint: "CertificateStoreType",
187-
// Headers: headers,
188-
// Payload: &ca,
189-
// }
190-
//
191-
// resp, err := c.sendRequest(keyfactorAPIStruct)
192-
// if err != nil {
193-
// return nil, err
194-
// }
195-
//
196-
// jsonResp := &CertStoreTypeResponse{}
197-
// err = json.NewDecoder(resp.Body).Decode(&jsonResp)
198-
// if err != nil {
199-
// return nil, err
200-
// }
201-
// return jsonResp, nil
202-
// }
129+
func (c *Client) CreateStoreType(ca *CertificateStoreType) (*CertificateStoreType, error) {
130+
log.Println("[INFO] Creating new certificate store type with Keyfactor")
131+
132+
// Validate that the required fields are present
133+
//err := validateCreateStoreTypeArgs(ca)
134+
//if err != nil {
135+
// return nil, err
136+
//}
137+
138+
// Set Keyfactor-specific headers
139+
headers := &apiHeaders{
140+
Headers: []StringTuple{
141+
{"x-keyfactor-api-version", "1"},
142+
{"x-keyfactor-requested-with", "APIClient"},
143+
},
144+
}
145+
146+
keyfactorAPIStruct := &request{
147+
Method: "POST",
148+
Endpoint: "CertificateStoreTypes",
149+
Headers: headers,
150+
Payload: &ca,
151+
}
152+
153+
resp, err := c.sendRequest(keyfactorAPIStruct)
154+
if err != nil {
155+
return nil, err
156+
}
157+
158+
jsonResp := &CertificateStoreType{}
159+
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
160+
if err != nil {
161+
return nil, err
162+
}
163+
return jsonResp, nil
164+
}
165+
166+
func (c *Client) UpdateStoreType(ca *CertificateStoreType) (*CertificateStoreType, error) {
167+
log.Println("[INFO] Creating new certificate store type with Keyfactor")
168+
169+
// Validate that the required fields are present
170+
//err := validateCreateStoreTypeArgs(ca)
171+
//if err != nil {
172+
// return nil, err
173+
//}
174+
175+
// Set Keyfactor-specific headers
176+
headers := &apiHeaders{
177+
Headers: []StringTuple{
178+
{"x-keyfactor-api-version", "1"},
179+
{"x-keyfactor-requested-with", "APIClient"},
180+
},
181+
}
182+
183+
keyfactorAPIStruct := &request{
184+
Method: "POST",
185+
Endpoint: "CertificateStoreType",
186+
Headers: headers,
187+
Payload: &ca,
188+
}
189+
190+
resp, err := c.sendRequest(keyfactorAPIStruct)
191+
if err != nil {
192+
return nil, err
193+
}
194+
195+
jsonResp := &CertificateStoreType{}
196+
err = json.NewDecoder(resp.Body).Decode(&jsonResp)
197+
if err != nil {
198+
return nil, err
199+
}
200+
return jsonResp, nil
201+
}
203202
func (c *Client) DeleteCertificateStoreType(id int) (*DeleteStoreType, error) {
204203
log.Printf("[INFO] Attempting to delete certificate store type %d", id)
205204

api/store_type_models.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,13 @@ type DeleteStoreType struct {
4646
}
4747

4848
type StoreTypePropertyDefinition struct {
49-
StoreTypeID int `json:"StoreTypeId"`
50-
Name string `json:"Name"`
51-
DisplayName string `json:"DisplayName"`
52-
Type string `json:"Type"`
53-
DependsOn string `json:"DependsOn"`
54-
DefaultValue string `json:"DefaultValue"`
55-
Required bool `json:"Required"`
49+
StoreTypeID int `json:"StoreTypeId;omitempty"`
50+
Name string `json:"Name"`
51+
DisplayName string `json:"DisplayName"`
52+
Type string `json:"Type"`
53+
DependsOn string `json:"DependsOn"`
54+
DefaultValue interface{} `json:"DefaultValue"`
55+
Required bool `json:"Required"`
5656
}
5757

5858
type EntryParameter struct {

0 commit comments

Comments
 (0)