forked from remilapeyre/vault-acme
/
path_accounts.go
259 lines (234 loc) · 6.9 KB
/
path_accounts.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package acme
import (
"context"
"fmt"
"github.com/go-acme/lego/v4/certcrypto"
"github.com/go-acme/lego/v4/registration"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
var keyTypes = []interface{}{
"EC256",
"EC384",
"RSA2048",
"RSA4096",
"RSA8192",
}
func pathAccounts(b *backend) []*framework.Path {
return []*framework.Path{
{
Pattern: "accounts/?$",
Operations: map[logical.Operation]framework.OperationHandler{
logical.ListOperation: &framework.PathOperation{
Callback: b.accountList,
},
},
},
{
Pattern: "accounts/" + framework.GenericNameRegex("account"),
Fields: map[string]*framework.FieldSchema{
"account": {
Type: framework.TypeString,
Required: true,
},
"server_url": {
Type: framework.TypeString,
// Required is only used in the documentation for now
Required: true,
},
"terms_of_service_agreed": {
Type: framework.TypeBool,
Default: false,
},
"key_type": {
Type: framework.TypeString,
Default: "EC256",
AllowedValues: keyTypes,
},
// TODO(remi): We should have a list of those so we can request certs
// for domains registred to different providers
"provider": {
Type: framework.TypeString,
},
"provider_configuration": {
Type: framework.TypeKVPairs,
},
"enable_http_01": {
Type: framework.TypeBool,
},
"enable_tls_alpn_01": {
Type: framework.TypeBool,
},
"dns_resolvers": {
Type: framework.TypeStringSlice,
},
"ignore_dns_propagation": {
Type: framework.TypeBool,
Default: false,
},
"contact": {
Type: framework.TypeString,
Required: true,
},
},
ExistenceCheck: b.pathExistenceCheck,
Operations: map[logical.Operation]framework.OperationHandler{
logical.CreateOperation: &framework.PathOperation{
Callback: b.accountWrite,
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.accountRead,
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.accountWrite,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.accountDelete,
},
},
},
}
}
func getKeyType(t string) (certcrypto.KeyType, error) {
switch t {
case "EC256":
return certcrypto.EC256, nil
case "EC384":
return certcrypto.EC384, nil
case "RSA2048":
return certcrypto.RSA2048, nil
case "RSA4096":
return certcrypto.RSA4096, nil
case "RSA8192":
return certcrypto.RSA8192, nil
default:
return "", fmt.Errorf("%q is not a supported key type", t)
}
}
func (b *backend) accountWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
if err := data.Validate(); err != nil {
return nil, err
}
serverURL := data.Get("server_url").(string)
contact := data.Get("contact").(string)
termsOfServiceAgreed := data.Get("terms_of_service_agreed").(bool)
provider := data.Get("provider").(string)
providerConfiguration := data.Get("provider_configuration").(map[string]string)
enableHTTP01 := data.Get("enable_http_01").(bool)
enableTLSALPN01 := data.Get("enable_tls_alpn_01").(bool)
dnsResolvers := data.Get("dns_resolvers").([]string)
ignoreDNSPropagation := data.Get("ignore_dns_propagation").(bool)
var update bool
user, err := getAccount(ctx, req.Storage, req.Path)
if err != nil {
return nil, err
}
if user == nil {
b.Logger().Info("Generating key pair for new account")
keyType, err := getKeyType(data.Get("key_type").(string))
if err != nil {
return logical.ErrorResponse(err.Error()), nil
}
privateKey, err := certcrypto.GeneratePrivateKey(keyType)
if err != nil {
return nil, fmt.Errorf("failed to generate account key pair: %w", err)
}
user = &account{
ServerURL: serverURL,
KeyType: data.Get("key_type").(string),
Key: privateKey,
}
} else {
update = true
if serverURL != user.ServerURL {
return logical.ErrorResponse("Cannot update server_url"), nil
}
if data.Get("key_type").(string) != user.KeyType {
return logical.ErrorResponse("Cannot update key_type"), nil
}
}
user.Email = contact
user.Provider = provider
user.ProviderConfiguration = providerConfiguration
user.EnableHTTP01 = enableHTTP01
user.EnableTLSALPN01 = enableTLSALPN01
user.TermsOfServiceAgreed = termsOfServiceAgreed
user.DNSResolvers = dnsResolvers
user.IgnoreDNSPropagation = ignoreDNSPropagation
client, err := user.getClient()
if err != nil {
return nil, err
}
var reg *registration.Resource
options := registration.RegisterOptions{
TermsOfServiceAgreed: termsOfServiceAgreed,
}
if update {
b.Logger().Info("Updating account")
reg, err = client.Registration.UpdateRegistration(options)
} else {
b.Logger().Info("Registring new account")
reg, err = client.Registration.Register(options)
}
if err != nil {
return logical.ErrorResponse(err.Error()), nil
}
user.Registration = reg
if err != nil {
return nil, fmt.Errorf("failed to create storage entry: %w", err)
}
b.Logger().Info("Saving account")
if err = user.save(ctx, req.Storage, req.Path, serverURL); err != nil {
return nil, err
}
return b.accountRead(ctx, req, data)
}
func (b *backend) accountRead(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
a, err := getAccount(ctx, req.Storage, req.Path)
if err != nil {
return nil, err
}
if a == nil {
return logical.ErrorResponse("This account does not exists"), nil
}
return &logical.Response{
Data: map[string]interface{}{
"server_url": a.ServerURL,
"registration_uri": a.Registration.URI,
"contact": a.GetEmail(),
"terms_of_service_agreed": a.TermsOfServiceAgreed,
"key_type": a.KeyType,
"provider": a.Provider,
"provider_configuration": a.ProviderConfiguration,
"enable_http_01": a.EnableHTTP01,
"enable_tls_alpn_01": a.EnableTLSALPN01,
"dns_resolvers": a.DNSResolvers,
"ignore_dns_propagation": a.IgnoreDNSPropagation,
},
}, nil
}
func (b *backend) accountDelete(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
a, err := getAccount(ctx, req.Storage, req.Path)
if err != nil {
return nil, err
}
if a == nil {
return logical.ErrorResponse("This account does not exists"), nil
}
client, err := a.getClient()
if err != nil {
return nil, fmt.Errorf("failed to instanciate new client: %w", err)
}
if err = client.Registration.DeleteRegistration(); err != nil {
return nil, fmt.Errorf("failed to deactivate registration: %w", err)
}
err = req.Storage.Delete(ctx, req.Path)
return nil, err
}
func (b *backend) accountList(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
entries, err := req.Storage.List(ctx, "accounts/")
if err != nil {
return nil, err
}
return logical.ListResponse(entries), nil
}