-
Notifications
You must be signed in to change notification settings - Fork 20
/
path_venafi_cert_revoke.go
198 lines (150 loc) · 4.58 KB
/
path_venafi_cert_revoke.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package pki
import (
"context"
"fmt"
"github.com/Venafi/vcert/v4/pkg/certificate"
"github.com/Venafi/vcert/v4/pkg/endpoint"
"github.com/Venafi/vcert/v4/pkg/policy"
"github.com/Venafi/vcert/v4/pkg/util"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
"strings"
)
func pathVenafiCertRevoke(b *backend) *framework.Path {
return &framework.Path{
Pattern: "revoke/" + framework.GenericNameRegex("role"),
Fields: map[string]*framework.FieldSchema{
"role": {
Type: framework.TypeString,
Description: `The desired role with configuration for this request`,
},
"certificate_uid": {
Type: framework.TypeString,
Description: "Common name for created certificate",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.venafiCertRevoke,
},
},
HelpSynopsis: pathConfigRootHelpSyn,
HelpDescription: pathConfigRootHelpDesc,
}
}
func (b *backend) venafiCertRevoke(ctx context.Context, req *logical.Request, d *framework.FieldData) (*logical.Response, error) {
op := req.Operation
if op == logical.RevokeOperation {
return nil, nil
}
b.Logger().Debug("Getting the role\n")
roleName := d.Get("role").(string)
// Get the role
role, err := b.getRole(ctx, req.Storage, roleName)
if err != nil {
return nil, err
}
if role == nil {
return logical.ErrorResponse(fmt.Sprintf("unknown role: %s", roleName)), nil
}
if role.KeyType == "any" {
return logical.ErrorResponse("role key type \"any\" not allowed for issuing certificates, only signing"), nil
}
id := d.Get("certificate_uid").(string)
if exists, err := isCertificateStored(ctx, req, id, role); !exists {
if err != nil {
return logical.ErrorResponse(err.Error()), err
}
return logical.ErrorResponse("the certificate is not stored"), fmt.Errorf("the certificate is not stored")
}
b.Logger().Debug("Creating Venafi client:")
cl, _, err := b.ClientVenafi(ctx, req.Storage, d, req, roleName)
if err != nil {
return logical.ErrorResponse(err.Error()), nil
}
dn, err := getDn(b, &cl, ctx, req, roleName, id, role.StoreByCN || strings.ToLower(role.StoreBy) == "cn")
if err != nil {
return logical.ErrorResponse(err.Error()), nil
}
revReq := certificate.RevocationRequest{}
revReq.CertificateDN = dn
err = cl.RevokeCertificate(&revReq)
if err != nil {
return logical.ErrorResponse("Failed to revoke certificate: %s", err), nil
}
err = deleteCertificateEntry(ctx, req, id, role)
if err != nil {
return logical.ErrorResponse("Certificate was revoked, but failed to remove it from vault: %s", err), nil
}
return nil, nil
}
func deleteCertificateEntry(ctx context.Context, req *logical.Request, id string, role *roleEntry) error {
if !role.StoreByCN {
id = strings.ReplaceAll(id, ":", "-")
}
path := "certs/" + id
//remove the certificate from vault.
err := req.Storage.Delete(ctx, path)
if err != nil {
return err
}
return nil
}
func isCertificateStored(ctx context.Context, req *logical.Request, id string, role *roleEntry) (bool, error) {
//there is nothing to remove.
if role.NoStore {
return false, fmt.Errorf("certificate is not stored")
}
if !role.StoreByCN {
if strings.Contains(id, ":") {
id = strings.ReplaceAll(id, ":", "-")
}
if strings.Contains(id, "-") {
id = strings.ReplaceAll(id, "-", "-")
}
}
path := "certs/" + id
entry, err := req.Storage.Get(ctx, path)
if err != nil {
return false, err
}
if entry == nil {
return false, nil
}
return true, nil
}
func getDn(b *backend, c *endpoint.Connector, ctx context.Context, req *logical.Request, rn, id string, storeByCN bool) (string, error) {
if !storeByCN {
return getDnFromSerial(c, id)
}
dn := id
if (*c).GetType() == endpoint.ConnectorTypeTPP {
cfg, err := b.getConfig(ctx, req, rn, false)
if err != nil {
return "", err
}
zone := cfg.Zone
if !strings.HasPrefix(zone, util.PathSeparator) {
zone = util.PathSeparator + zone
}
if !strings.HasPrefix(zone, policy.RootPath) {
zone = policy.RootPath + zone
}
if !strings.HasPrefix(dn, zone) {
dn = fmt.Sprintf("%s\\%s", zone, id)
}
}
return dn, nil
}
func getDnFromSerial(c *endpoint.Connector, serial string) (string, error) {
var reqS certificate.SearchRequest
//removing dash from serial since TPP does not contain them
tppSerial := strings.ReplaceAll(serial, "-", "")
reqS = append(reqS, fmt.Sprintf("serial=%s", tppSerial))
data, err := (*c).SearchCertificates(&reqS)
if err != nil {
return "", err
}
dn := data.Certificates[0].CertificateRequestId
return dn, nil
}