-
Notifications
You must be signed in to change notification settings - Fork 20
/
path_venafi_cert_read.go
81 lines (70 loc) · 2.3 KB
/
path_venafi_cert_read.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package pki
import (
"context"
"fmt"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func pathVenafiCertRead(b *backend) *framework.Path {
return &framework.Path{
Pattern: "cert/" + framework.GenericNameRegex("certificate_uid"),
Fields: map[string]*framework.FieldSchema{
"certificate_uid": {
Type: framework.TypeString,
Description: "Common name or serial number of desired certificate",
},
"key_password": {
Type: framework.TypeString,
Description: "Password for encrypting private key",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.ReadOperation: b.pathVenafiCertRead,
//todo: maybe add delete operation to delete certificate entry from storage
},
HelpSynopsis: pathConfigRootHelpSyn,
HelpDescription: pathConfigRootHelpDesc,
}
}
func (b *backend) pathVenafiCertRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
b.Logger().Debug("Trying to read certificate")
certUID := data.Get("certificate_uid").(string)
if len(certUID) == 0 {
return logical.ErrorResponse("no common name specified on certificate"), nil
}
path := "certs/" + certUID
entry, err := req.Storage.Get(ctx, path)
if err != nil {
return nil, fmt.Errorf("failed to read Venafi certificate: %s", err)
}
if entry == nil {
return nil, fmt.Errorf("no entry found in path %s", path)
}
var cert VenafiCert
b.Logger().Debug("Getting venafi certificate")
if err := entry.DecodeJSON(&cert); err != nil {
b.Logger().Error("error reading venafi configuration: %s", err)
return nil, err
}
b.Logger().Debug("certificate is:" + cert.Certificate)
b.Logger().Debug("chain is:" + cert.CertificateChain)
respData := map[string]interface{}{
"certificate_uid": certUID,
"serial_number": cert.SerialNumber,
"certificate_chain": cert.CertificateChain,
"certificate": cert.Certificate,
"private_key": cert.PrivateKey,
}
keyPassword := data.Get("key_password").(string)
if keyPassword != "" {
encryptedPrivateKeyPem, err := encryptPrivateKey(cert.PrivateKey, keyPassword)
if err != nil {
return nil, err
}
respData["private_key"] = encryptedPrivateKeyPem
}
return &logical.Response{
//Data: structs.New(cert).Map(),
Data: respData,
}, nil
}