77 * and limitations under the License.
88 */
99
10- package keyfactor
10+ package kfbackend
1111
1212import (
1313 "bytes"
@@ -18,12 +18,11 @@ import (
1818 "crypto/x509/pkix"
1919 "encoding/asn1"
2020 "encoding/base64"
21- b64 "encoding/base64"
2221 "encoding/json"
2322 "encoding/pem"
2423 "errors"
2524 "fmt"
26- "io/ioutil "
25+ "io"
2726 "net"
2827 "net/http"
2928 "net/url"
@@ -37,7 +36,7 @@ import (
3736// fetch the CA info from keyfactor
3837func fetchCAInfo (ctx context.Context , req * logical.Request , b * keyfactorBackend ) (response * logical.Response , retErr error ) {
3938 // first we see if we have previously retreived the CA or chain
40- config , err := b .config (ctx , req .Storage )
39+ config , err := b .fetchConfig (ctx , req .Storage )
4140 if err != nil {
4241 return nil , err
4342 }
@@ -151,12 +150,12 @@ func fetchCaChainInfo(ctx context.Context, req *logical.Request, b *keyfactorBac
151150}
152151
153152func getCAId (ctx context.Context , req * logical.Request , b * keyfactorBackend ) (string , error ) {
154- config , err := b .config (ctx , req .Storage )
153+ config , err := b .fetchConfig (ctx , req .Storage )
155154 if err != nil {
156155 return "" , err
157156 }
158157 if config == nil {
159- return "" , errors .New ("unable to load configuration. " )
158+ return "" , errors .New ("unable to load configuration" )
160159 }
161160
162161 if config .CertAuthority == "" {
@@ -168,12 +167,16 @@ func getCAId(ctx context.Context, req *logical.Request, b *keyfactorBackend) (st
168167
169168 // This is only needed when running as a vault extension
170169 b .Logger ().Debug ("Closing idle connections" )
171- http .DefaultClient .CloseIdleConnections ()
170+ client , err := b .getClient (ctx , req .Storage )
171+ if err != nil {
172+ b .Logger ().Error ("unable to create the http client" )
173+ }
174+ client .httpClient .CloseIdleConnections ()
172175
173176 ca_name = url .QueryEscape (ca_name )
174177
175- creds := config .Username + ":" + config .Password
176- encCreds := b64 .StdEncoding .EncodeToString ([]byte (creds ))
178+ // creds := config.Username + ":" + config.Password
179+ // encCreds := b64.StdEncoding.EncodeToString([]byte(creds))
177180
178181 // Build request
179182
@@ -183,21 +186,21 @@ func getCAId(ctx context.Context, req *logical.Request, b *keyfactorBackend) (st
183186 if err != nil {
184187 b .Logger ().Info ("Error forming request: {{err}}" , err )
185188 }
186- httpReq .Header .Add ("x-keyfactor-requested-with" , "APIClient" )
189+ // httpReq.Header.Add("x-keyfactor-requested-with", "APIClient")
187190 httpReq .Header .Add ("x-keyfactor-api-version" , "1" )
188- httpReq .Header .Add ("authorization" , "Basic " + encCreds )
191+ // httpReq.Header.Add("authorization", "Basic "+encCreds)
189192
190193 // Send request and check status
191194 b .Logger ().Debug ("About to connect to " + config .KeyfactorUrl + "for ca retrieval" )
192- res , err := http . DefaultClient .Do (httpReq )
195+ res , err := client . httpClient .Do (httpReq )
193196 if err != nil {
194197 b .Logger ().Info ("failed getting CA: {{err}}" , err )
195198 return "" , err
196199 }
197200 if res .StatusCode != 200 {
198201 b .Logger ().Error ("request failed: server returned" + fmt .Sprint (res .StatusCode ))
199202 defer res .Body .Close ()
200- body , err := ioutil .ReadAll (res .Body )
203+ body , err := io .ReadAll (res .Body )
201204 if err != nil {
202205 b .Logger ().Info ("Error reading response: {{err}}" , err )
203206 return "" , err
@@ -246,22 +249,25 @@ func (b *keyfactorBackend) generateCSR(cn string, ip_sans []string, dns_sans []s
246249}
247250
248251func fetchCertFromKeyfactor (ctx context.Context , req * logical.Request , b * keyfactorBackend , kfCertId string , includeChain bool ) (string , error ) {
249- config , err := b .config (ctx , req .Storage )
252+ config , err := b .fetchConfig (ctx , req .Storage )
250253 if err != nil {
251254 return "" , err
252255 }
253256 if config == nil {
254257 return "" , errors .New ("unable to load configuration" )
255258 }
256- creds := config .Username + ":" + config .Password
257- encCreds := b64 .StdEncoding .EncodeToString ([]byte (creds ))
258- //location, _ := time.LoadLocation("UTC")
259- //t := time.Now().In(location)
260- //time := t.Format("2006-01-02T15:04:05")
259+ // creds := config.Username + ":" + config.Password
260+ // encCreds := b64.StdEncoding.EncodeToString([]byte(creds))
261261
262+ // get the client
263+ client , err := b .getClient (ctx , req .Storage )
264+ if err != nil {
265+ b .Logger ().Error ("unable to create the http client" )
266+ }
262267 // This is only needed when running as a vault extension
263268 b .Logger ().Debug ("Closing idle connections" )
264- http .DefaultClient .CloseIdleConnections ()
269+ client .httpClient .CloseIdleConnections ()
270+
265271 include := "false"
266272 if includeChain {
267273 include = "true"
@@ -279,12 +285,11 @@ func fetchCertFromKeyfactor(ctx context.Context, req *logical.Request, b *keyfac
279285 }
280286 httpReq .Header .Add ("x-keyfactor-requested-with" , "APIClient" )
281287 httpReq .Header .Add ("content-type" , "application/json" )
282- httpReq .Header .Add ("authorization" , "Basic " + encCreds )
283288 httpReq .Header .Add ("x-certificateformat" , "PEM" )
284289
285290 // Send request and check status
286291 b .Logger ().Debug ("About to connect to " + config .KeyfactorUrl + "for cert retrieval" )
287- res , err := http . DefaultClient .Do (httpReq )
292+ res , err := client . httpClient .Do (httpReq )
288293 if err != nil {
289294 b .Logger ().Info ("failed getting cert: {{err}}" , err )
290295 return "" , err
@@ -298,7 +303,7 @@ func fetchCertFromKeyfactor(ctx context.Context, req *logical.Request, b *keyfac
298303 // Read response and return certificate and key
299304 defer res .Body .Close ()
300305
301- body , err := ioutil .ReadAll (res .Body )
306+ body , err := io .ReadAll (res .Body )
302307 if err != nil {
303308 b .Logger ().Info ("Error reading response: {{err}}" , err )
304309 return "" , err
@@ -339,7 +344,7 @@ func fetchCertBySerial(ctx context.Context, req *logical.Request, prefix, serial
339344 return nil , errutil.InternalError {Err : fmt .Sprintf ("error fetching certificate %s: %s" , serial , err )}
340345 }
341346 if certEntry != nil {
342- if certEntry . Value == nil || len (certEntry .Value ) == 0 {
347+ if len (certEntry .Value ) == 0 {
343348 return nil , errutil.InternalError {Err : fmt .Sprintf ("returned certificate bytes for serial %s were empty" , serial )}
344349 }
345350 return certEntry , nil
@@ -358,7 +363,7 @@ func fetchCertBySerial(ctx context.Context, req *logical.Request, prefix, serial
358363 if certEntry == nil {
359364 return nil , nil
360365 }
361- if certEntry . Value == nil || len (certEntry .Value ) == 0 {
366+ if len (certEntry .Value ) == 0 {
362367 return nil , errutil.InternalError {Err : fmt .Sprintf ("returned certificate bytes for serial %s were empty" , serial )}
363368 }
364369
0 commit comments