Skip to content

Commit 8f3e7d6

Browse files
Merge pull request #10 from Keyfactor/double-encoding-fix
fixed double-encoding when storing the cert.
2 parents 832e316 + 2613add commit 8f3e7d6

File tree

2 files changed

+19
-38
lines changed

2 files changed

+19
-38
lines changed

backend.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func (b *keyfactorBackend) submitCSR(ctx context.Context, req *logical.Request,
106106
return nil, "", err
107107
}
108108
if config == nil {
109-
return nil, "", errors.New("configuration is empty.")
109+
return nil, "", errors.New("configuration is empty")
110110
}
111111

112112
ca := config.CertAuthority
@@ -154,10 +154,11 @@ func (b *keyfactorBackend) submitCSR(ctx context.Context, req *logical.Request,
154154
}
155155

156156
// Read response and return certificate and key
157+
157158
defer res.Body.Close()
158159
body, err := ioutil.ReadAll(res.Body)
159160
if err != nil {
160-
b.Logger().Info("Error reading response: {{err}}", err)
161+
b.Logger().Error("Error reading response: {{err}}", err)
161162
return nil, "", err
162163
}
163164

@@ -177,6 +178,8 @@ func (b *keyfactorBackend) submitCSR(ctx context.Context, req *logical.Request,
177178
serial := inner["SerialNumber"].(string)
178179
kfId := inner["KeyfactorID"].(float64)
179180

181+
b.Logger().Debug("parsed response: ", certI...)
182+
180183
if err != nil {
181184
b.Logger().Error("unable to parse ca_chain response", fmt.Sprint(err))
182185
}
@@ -190,20 +193,26 @@ func (b *keyfactorBackend) submitCSR(ctx context.Context, req *logical.Request,
190193
b.Logger().Error("error storing the ca_chain locally", err)
191194
}
192195

193-
err = req.Storage.Put(ctx, &logical.StorageEntry{
194-
Key: "certs/" + normalizeSerial(serial),
196+
key := "certs/" + normalizeSerial(serial)
197+
198+
entry := &logical.StorageEntry{
199+
Key: key,
195200
Value: []byte(certs[0]),
196-
})
201+
}
202+
203+
b.Logger().Debug("cert entry.Value = ", string(entry.Value))
204+
205+
err = req.Storage.Put(ctx, entry)
197206
if err != nil {
198207
return nil, "", errwrap.Wrapf("unable to store certificate locally: {{err}}", err)
199208
}
200209

201-
entry, err := logical.StorageEntryJSON("kfId/"+normalizeSerial(serial), kfId)
210+
kfIdEntry, err := logical.StorageEntryJSON("kfId/"+normalizeSerial(serial), kfId)
202211
if err != nil {
203212
return nil, "", err
204213
}
205214

206-
err = req.Storage.Put(ctx, entry)
215+
err = req.Storage.Put(ctx, kfIdEntry)
207216
if err != nil {
208217
return nil, "", errwrap.Wrapf("unable to store the keyfactor ID for the certificate locally: {{err}}", err)
209218
}

path_certs.go

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/base64"
66
b64 "encoding/base64"
77
"encoding/json"
8-
"encoding/pem"
98
"fmt"
109
"io"
1110
"net/http"
@@ -117,8 +116,7 @@ func (b *keyfactorBackend) pathFetchCert(ctx context.Context, req *logical.Reque
117116
var serial, contentType string
118117
var certEntry, revokedEntry *logical.StorageEntry
119118
var funcErr error
120-
var certificate []byte
121-
var block pem.Block
119+
var certificate string
122120
var revocationTime int64
123121
response = &logical.Response{
124122
Data: map[string]interface{}{},
@@ -131,7 +129,6 @@ func (b *keyfactorBackend) pathFetchCert(ctx context.Context, req *logical.Reque
131129
b.Logger().Debug("fetching cert, path = " + req.Path)
132130

133131
serial = data.Get("serial").(string)
134-
pemType := "CERTIFICATE"
135132

136133
if len(serial) == 0 {
137134
response = logical.ErrorResponse("The serial number must be provided")
@@ -156,13 +153,9 @@ func (b *keyfactorBackend) pathFetchCert(ctx context.Context, req *logical.Reque
156153
goto reply
157154
}
158155

159-
block = pem.Block{
160-
Type: pemType,
161-
Bytes: certEntry.Value,
162-
}
163-
164-
certificate = []byte(strings.TrimSpace(string(pem.EncodeToMemory(&block))))
156+
b.Logger().Debug("fetched certEntry.Value = ", certEntry.Value)
165157

158+
certificate = string(certEntry.Value)
166159
revokedEntry, funcErr = fetchCertBySerial(ctx, req, "revoked/", serial)
167160
if funcErr != nil {
168161
switch funcErr.(type) {
@@ -509,19 +502,6 @@ func revokeCert(ctx context.Context, b *keyfactorBackend, req *logical.Request,
509502
}
510503
b.Logger().Info("certEntry key = " + certEntry.Key)
511504
b.Logger().Info("certEntry value = " + string(certEntry.Value))
512-
// cert, err := x509.ParseCertificate(certEntry.Value)
513-
// if err != nil {
514-
// return nil, errwrap.Wrapf("error parsing certificate: {{err}}", err)
515-
// }
516-
// if cert == nil {
517-
// return nil, fmt.Errorf("got a nil certificate")
518-
// }
519-
520-
// Add a little wiggle room because leases are stored with a second
521-
// granularity
522-
// if cert.NotAfter.Before(time.Now().Add(2 * time.Second)) {
523-
// return nil, nil
524-
// }
525505

526506
currTime := time.Now()
527507
revInfo.CertificateBytes = certEntry.Value
@@ -540,14 +520,6 @@ func revokeCert(ctx context.Context, b *keyfactorBackend, req *logical.Request,
540520

541521
}
542522

543-
// crlErr := buildCRL(ctx, b, req, false)
544-
// switch crlErr.(type) {
545-
// case errutil.UserError:
546-
// return logical.ErrorResponse(fmt.Sprintf("Error during CRL building: %s", crlErr)), nil
547-
// case errutil.InternalError:
548-
// return nil, errwrap.Wrapf("error encountered during CRL building: {{err}}", crlErr)
549-
// }
550-
551523
resp := &logical.Response{
552524
Data: map[string]interface{}{
553525
"revocation_time": revInfo.RevocationTime,

0 commit comments

Comments
 (0)