@@ -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
0 commit comments