7
7
* and limitations under the License.
8
8
*/
9
9
10
- package keyfactor
10
+ package kfbackend
11
11
12
12
import (
13
13
"bytes"
@@ -18,12 +18,11 @@ import (
18
18
"crypto/x509/pkix"
19
19
"encoding/asn1"
20
20
"encoding/base64"
21
- b64 "encoding/base64"
22
21
"encoding/json"
23
22
"encoding/pem"
24
23
"errors"
25
24
"fmt"
26
- "io/ioutil "
25
+ "io"
27
26
"net"
28
27
"net/http"
29
28
"net/url"
@@ -37,7 +36,7 @@ import (
37
36
// fetch the CA info from keyfactor
38
37
func fetchCAInfo (ctx context.Context , req * logical.Request , b * keyfactorBackend ) (response * logical.Response , retErr error ) {
39
38
// 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 )
41
40
if err != nil {
42
41
return nil , err
43
42
}
@@ -151,12 +150,12 @@ func fetchCaChainInfo(ctx context.Context, req *logical.Request, b *keyfactorBac
151
150
}
152
151
153
152
func 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 )
155
154
if err != nil {
156
155
return "" , err
157
156
}
158
157
if config == nil {
159
- return "" , errors .New ("unable to load configuration. " )
158
+ return "" , errors .New ("unable to load configuration" )
160
159
}
161
160
162
161
if config .CertAuthority == "" {
@@ -168,12 +167,16 @@ func getCAId(ctx context.Context, req *logical.Request, b *keyfactorBackend) (st
168
167
169
168
// This is only needed when running as a vault extension
170
169
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 ()
172
175
173
176
ca_name = url .QueryEscape (ca_name )
174
177
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))
177
180
178
181
// Build request
179
182
@@ -183,21 +186,21 @@ func getCAId(ctx context.Context, req *logical.Request, b *keyfactorBackend) (st
183
186
if err != nil {
184
187
b .Logger ().Info ("Error forming request: {{err}}" , err )
185
188
}
186
- httpReq .Header .Add ("x-keyfactor-requested-with" , "APIClient" )
189
+ // httpReq.Header.Add("x-keyfactor-requested-with", "APIClient")
187
190
httpReq .Header .Add ("x-keyfactor-api-version" , "1" )
188
- httpReq .Header .Add ("authorization" , "Basic " + encCreds )
191
+ // httpReq.Header.Add("authorization", "Basic "+encCreds)
189
192
190
193
// Send request and check status
191
194
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 )
193
196
if err != nil {
194
197
b .Logger ().Info ("failed getting CA: {{err}}" , err )
195
198
return "" , err
196
199
}
197
200
if res .StatusCode != 200 {
198
201
b .Logger ().Error ("request failed: server returned" + fmt .Sprint (res .StatusCode ))
199
202
defer res .Body .Close ()
200
- body , err := ioutil .ReadAll (res .Body )
203
+ body , err := io .ReadAll (res .Body )
201
204
if err != nil {
202
205
b .Logger ().Info ("Error reading response: {{err}}" , err )
203
206
return "" , err
@@ -246,22 +249,25 @@ func (b *keyfactorBackend) generateCSR(cn string, ip_sans []string, dns_sans []s
246
249
}
247
250
248
251
func 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 )
250
253
if err != nil {
251
254
return "" , err
252
255
}
253
256
if config == nil {
254
257
return "" , errors .New ("unable to load configuration" )
255
258
}
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))
261
261
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
+ }
262
267
// This is only needed when running as a vault extension
263
268
b .Logger ().Debug ("Closing idle connections" )
264
- http .DefaultClient .CloseIdleConnections ()
269
+ client .httpClient .CloseIdleConnections ()
270
+
265
271
include := "false"
266
272
if includeChain {
267
273
include = "true"
@@ -279,12 +285,11 @@ func fetchCertFromKeyfactor(ctx context.Context, req *logical.Request, b *keyfac
279
285
}
280
286
httpReq .Header .Add ("x-keyfactor-requested-with" , "APIClient" )
281
287
httpReq .Header .Add ("content-type" , "application/json" )
282
- httpReq .Header .Add ("authorization" , "Basic " + encCreds )
283
288
httpReq .Header .Add ("x-certificateformat" , "PEM" )
284
289
285
290
// Send request and check status
286
291
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 )
288
293
if err != nil {
289
294
b .Logger ().Info ("failed getting cert: {{err}}" , err )
290
295
return "" , err
@@ -298,7 +303,7 @@ func fetchCertFromKeyfactor(ctx context.Context, req *logical.Request, b *keyfac
298
303
// Read response and return certificate and key
299
304
defer res .Body .Close ()
300
305
301
- body , err := ioutil .ReadAll (res .Body )
306
+ body , err := io .ReadAll (res .Body )
302
307
if err != nil {
303
308
b .Logger ().Info ("Error reading response: {{err}}" , err )
304
309
return "" , err
@@ -339,7 +344,7 @@ func fetchCertBySerial(ctx context.Context, req *logical.Request, prefix, serial
339
344
return nil , errutil.InternalError {Err : fmt .Sprintf ("error fetching certificate %s: %s" , serial , err )}
340
345
}
341
346
if certEntry != nil {
342
- if certEntry . Value == nil || len (certEntry .Value ) == 0 {
347
+ if len (certEntry .Value ) == 0 {
343
348
return nil , errutil.InternalError {Err : fmt .Sprintf ("returned certificate bytes for serial %s were empty" , serial )}
344
349
}
345
350
return certEntry , nil
@@ -358,7 +363,7 @@ func fetchCertBySerial(ctx context.Context, req *logical.Request, prefix, serial
358
363
if certEntry == nil {
359
364
return nil , nil
360
365
}
361
- if certEntry . Value == nil || len (certEntry .Value ) == 0 {
366
+ if len (certEntry .Value ) == 0 {
362
367
return nil , errutil.InternalError {Err : fmt .Sprintf ("returned certificate bytes for serial %s were empty" , serial )}
363
368
}
364
369
0 commit comments