@@ -12,7 +12,6 @@ import (
1212 "sync"
1313 "time"
1414
15- //"github.com/Keyfactor/keyfactor-go-client/api"
1615 "github.com/hashicorp/errwrap"
1716 "github.com/hashicorp/vault/sdk/framework"
1817 "github.com/hashicorp/vault/sdk/logical"
@@ -35,7 +34,7 @@ type keyfactorBackend struct {
3534 * framework.Backend
3635 lock sync.RWMutex
3736 cachedConfig * keyfactorConfig
38- // client *api.Client
37+ client * keyfactorClient
3938}
4039
4140// keyfactorBackend defines the target API keyfactorBackend
@@ -66,21 +65,39 @@ func backend() *keyfactorBackend {
6665 return & b
6766}
6867
69- // func (b *keyfactorBackend) initialize(ctx context.Context, req *logical.InitializationRequest) error {
70- // err := req.Storage.Delete(ctx, "/ca")
71-
72- // if err != nil {
73- // b.Logger().Error("Error removing previous stored ca values on init")
74- // return err
75- // }
76- // //confPath := os.Getenv("KF_CONF_PATH")
77- // //file, _ := ioutil.ReadFile(confPath)
78- // //config = make(map[string]string)
79- // //jsonutil.DecodeJSON(file, &config)
80- // //b.Logger().Debug("INITIALIZE: KF_CONF_PATH = " + confPath)
81- // //b.Logger().Debug("config file contents = ", config)
82- // return nil
83- // }
68+ // reset clears any client configuration for a new
69+ // backend to be configured
70+ func (b * keyfactorBackend ) reset () {
71+ b .lock .Lock ()
72+ defer b .lock .Unlock ()
73+ b .client = nil
74+ }
75+
76+ // invalidate clears an existing client configuration in
77+ // the backend
78+ func (b * keyfactorBackend ) invalidate (ctx context.Context , key string ) {
79+ if key == "config" {
80+ b .reset ()
81+ }
82+ }
83+
84+ // getClient locks the backend as it configures and creates a
85+ // a new client for the target API
86+ func (b * keyfactorBackend ) getClient (ctx context.Context , s logical.Storage ) (* keyfactorClient , error ) {
87+ b .lock .RLock ()
88+ unlockFunc := b .lock .RUnlock
89+ defer func () { unlockFunc () }()
90+
91+ if b .client != nil {
92+ return b .client , nil
93+ }
94+
95+ b .lock .RUnlock ()
96+ b .lock .Lock ()
97+ unlockFunc = b .lock .Unlock
98+
99+ return nil , fmt .Errorf ("need to return client" )
100+ }
84101
85102// Handle interface with Keyfactor API to enroll a certificate with given content
86103func (b * keyfactorBackend ) submitCSR (ctx context.Context , req * logical.Request , csr string , caName string , templateName string ) ([]string , string , error ) {
@@ -92,11 +109,6 @@ func (b *keyfactorBackend) submitCSR(ctx context.Context, req *logical.Request,
92109 return nil , "" , errors .New ("configuration is empty." )
93110 }
94111
95- // host := config["host"]
96- // template := config["template"]
97- // ca := config["CA"]
98- // creds := config["creds"]
99-
100112 ca := config .CertAuthority
101113 template := config .CertTemplate
102114
@@ -130,14 +142,14 @@ func (b *keyfactorBackend) submitCSR(ctx context.Context, req *logical.Request,
130142 b .Logger ().Debug ("About to connect to " + config .KeyfactorUrl + "for csr submission" )
131143 res , err := http .DefaultClient .Do (httpReq )
132144 if err != nil {
133- b .Logger ().Info ("CSR Enrollment failed: {{err}}" , err )
145+ b .Logger ().Info ("CSR Enrollment failed: {{err}}" , err . Error () )
134146 return nil , "" , err
135147 }
136148 if res .StatusCode != 200 {
137149 b .Logger ().Error ("CSR Enrollment failed: server returned" + fmt .Sprint (res .StatusCode ))
138150 defer res .Body .Close ()
139151 body , _ := ioutil .ReadAll (res .Body )
140- b .Logger ().Error ("Error response: " + fmt . Sprint (body ))
152+ b .Logger ().Error ("Error response: " + string (body [:] ))
141153 return nil , "" , fmt .Errorf ("enrollment failed: server returned %d\n " , res .StatusCode )
142154 }
143155
@@ -166,7 +178,7 @@ func (b *keyfactorBackend) submitCSR(ctx context.Context, req *logical.Request,
166178 kfId := inner ["KeyfactorID" ].(float64 )
167179
168180 if err != nil {
169- b .Logger ().Error ("unable to parse ca_chain response" , err )
181+ b .Logger ().Error ("unable to parse ca_chain response" , fmt . Sprint ( err ) )
170182 }
171183 caEntry , err := logical .StorageEntryJSON ("ca_chain/" , certs [1 :])
172184 if err != nil {
@@ -199,40 +211,6 @@ func (b *keyfactorBackend) submitCSR(ctx context.Context, req *logical.Request,
199211 return certs , serial , nil
200212}
201213
202- // reset clears any client configuration for a new
203- // backend to be configured
204- func (b * keyfactorBackend ) reset () {
205- b .lock .Lock ()
206- defer b .lock .Unlock ()
207- //b.client = nil
208- }
209-
210- // invalidate clears an existing client configuration in
211- // the backend
212- func (b * keyfactorBackend ) invalidate (ctx context.Context , key string ) {
213- if key == "config" {
214- b .reset ()
215- }
216- }
217-
218- // getClient locks the backend as it configures and creates a
219- // a new client for the target API
220- // func (b *keyfactorBackend) getClient(ctx context.Context, s logical.Storage) (*hashiCupsClient, error) {
221- // b.lock.RLock()
222- // unlockFunc := b.lock.RUnlock
223- // defer func() { unlockFunc() }()
224-
225- // // if b.client != nil {
226- // // return b.client, nil
227- // // }
228-
229- // b.lock.RUnlock()
230- // b.lock.Lock()
231- // unlockFunc = b.lock.Unlock
232-
233- // return nil, fmt.Errorf("need to return client")
234- // }
235-
236214const keyfactorHelp = `
237215The Keyfactor backend is a pki service that issues and manages certificates.
238216`
0 commit comments