Skip to content

Commit 58d0569

Browse files
Finished testing. Updated documentation.
1 parent 58c2a10 commit 58d0569

File tree

5 files changed

+28
-20
lines changed

5 files changed

+28
-20
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ Keyfactor Vault Secrets Engine Guide.docx
88
Makefile
99
sample_config.json
1010
README.md
11+
README.md

backend.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ import (
2323

2424
const (
2525
operationPrefixKeyfactor string = "keyfactor"
26+
PluginVersion = "1.4.2" // this should match the release version of the plugin
2627
)
2728

28-
const PluginVersion = "1.4.2" // this should match the release version of the plugin
29-
3029
// Factory configures and returns backend
3130
func Factory(ctx context.Context, conf *logical.BackendConfig) (logical.Backend, error) {
3231

@@ -109,7 +108,7 @@ func (b *keyfactorBackend) getClient(ctx context.Context, s logical.Storage) (*k
109108
defer b.configLock.RUnlock()
110109

111110
if b.client != nil {
112-
b.Logger().Debug("closing idle connections before returning existing client")
111+
b.Logger().Trace("returning existing client")
113112
return b.client, nil
114113
}
115114

@@ -130,5 +129,5 @@ func (b *keyfactorBackend) getClient(ctx context.Context, s logical.Storage) (*k
130129
}
131130

132131
const keyfactorHelp = `
133-
The Keyfactor backend is a pki service that issues and manages certificates.
132+
The Keyfactor backend is a pki service that issues and manages certificates via the Keyfactor Command platform.
134133
`

cert_util.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"encoding/pem"
2323
"errors"
2424
"fmt"
25+
"io"
2526
"net"
2627
"strings"
2728
"time"
@@ -128,15 +129,24 @@ func (b *keyfactorBackend) submitCSR(ctx context.Context, req *logical.Request,
128129

129130
b.Logger().Debug("setting parameters on the request.. ")
130131

131-
apiRequest := client.V1.EnrollmentApi.NewCreateEnrollmentCSRRequest(ctx).EnrollmentCSREnrollmentRequest(enrollmentRequest).XCertificateformat("PEM")
132+
apiRequest := client.V1.EnrollmentApi.NewCreateEnrollmentCSRRequest(ctx).ForceEnroll(true).EnrollmentCSREnrollmentRequest(enrollmentRequest).XCertificateformat("PEM")
132133

133134
b.Logger().Debug("about to connect to " + config.KeyfactorUrl + " with Keyfactor client for CSR submission")
134135

135136
resData, httpRes, err := apiRequest.Execute()
136137

137138
if err != nil || httpRes.StatusCode != 200 {
138-
b.Logger().Error(fmt.Sprintf("there was an error performing CSR enrollment. HttpStatusCode: %d, error: %s", httpRes.StatusCode, err))
139-
return nil, "", err
139+
body, bodyErr := io.ReadAll(httpRes.Body)
140+
errMsg := ""
141+
142+
if bodyErr != nil {
143+
b.Logger().Error(fmt.Sprintf("there was an error reading the response body: %v", bodyErr))
144+
errMsg = err.Error()
145+
} else {
146+
errMsg = string(body)
147+
}
148+
b.Logger().Error(fmt.Sprintf("there was an error performing CSR enrollment. HttpStatusCode: %d, error: %s", httpRes.StatusCode, errMsg))
149+
return nil, "", fmt.Errorf(errMsg)
140150
}
141151

142152
// Read certificates from response
@@ -389,19 +399,17 @@ func fetchCertIssuedByCA(ctx context.Context, req *logical.Request, b *keyfactor
389399

390400
//caName = strings.Replace(caName, " ", "%20", -1)
391401

392-
getCertRequest := v1.ApiGetCertificatesRequest{}
393-
getCertRequest.QueryString("CA -eq " + caName)
394-
getCertRequest.ReturnLimit(1)
395-
396402
// Send request and check status
397-
b.Logger().Debug("calling API with query string %s for cert retrieval", getCertRequest.QueryString)
398403

399-
apiRequest := client.V1.CertificateApi.NewGetCertificatesRequest(ctx)
404+
b.Logger().Debug(fmt.Sprintf("calling API with to fetch cert issued by %s", caName))
405+
406+
certs, httpResponse, err := client.V1.CertificateApi.NewGetCertificatesRequest(ctx).QueryString("CA -eq \"" + caName + "\"").ReturnLimit(1).Execute()
400407

401-
certs, httpResponse, err := apiRequest.ApiService.GetCertificatesExecute(getCertRequest)
408+
//certs, httpResponse, err := apiRequest.Execute()
402409

403410
if err != nil {
404-
b.Logger().Info(fmt.Sprintf("failed getting cert: %s", err.Error()))
411+
b.Logger().Error(fmt.Sprintf("failed to retreive cert: %s", err.Error()))
412+
b.Logger().Debug(fmt.Sprintf("http status code: %d, http response: %s", httpResponse.StatusCode, httpResponse.Body))
405413
return nil, err
406414
}
407415

@@ -411,7 +419,7 @@ func fetchCertIssuedByCA(ctx context.Context, req *logical.Request, b *keyfactor
411419
return nil, fmt.Errorf("error downloading certificate. returned status = %d\n %s", httpResponse.StatusCode, httpResponse.Body)
412420
}
413421

414-
b.Logger().Debug("response = ", certs)
422+
b.Logger().Debug(fmt.Sprintf("cert issued by CA response: %s", certs))
415423

416424
if len(certs) == 0 {
417425
return nil, fmt.Errorf("no certificates issued by CA %s found in Command. At least 1 must exist in order to retreive the CA or CA chain certificate(s)", caName)
@@ -434,8 +442,6 @@ func fetchChainAndCAForCert(ctx context.Context, req *logical.Request, b *keyfac
434442
if err != nil {
435443
b.Logger().Error("unable to create the http client")
436444
}
437-
// This is only needed when running as a vault extension
438-
b.Logger().Debug("Closing idle connections")
439445

440446
// Build request
441447

installation.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ sha256 checksum: <checksum>
2323

2424
> vault secrets enable <instance name>
2525

26-
6) test the connection by requesting the ca
27-
26+
6) test the connection by requesting the CA (the CA certificate will need to exist in the Command database)
2827
> vault read <instance name>/ca
2928

3029

readme_source.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,10 @@ instance of the plugin is named "keyfactor".
596596
### Read CA cert
597597

598598
`vault read keyfactor/ca ca=<ca name>`
599+
> Note: The certificate for the CA needs to have been imported into Command for this endpoint to return the CA Certificate
599600
600601
### Read CA chain
601602

602603
`vault read keyfactor/ca_chain ca=<ca name>`
604+
> Note: _All_ certificates in the chain need to have been imported into Command for this endpoint to return the CA Certificate Chain
605+

0 commit comments

Comments
 (0)