forked from Oogy/vault-plugin-secrets-nebula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_ca.go
275 lines (225 loc) · 7.27 KB
/
path_ca.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package nebula
import (
"context"
"crypto/rand"
"fmt"
"strings"
"time"
"github.com/openbao/openbao/sdk/v2/framework"
"github.com/openbao/openbao/sdk/v2/helper/errutil"
"github.com/openbao/openbao/sdk/v2/logical"
"github.com/slackhq/nebula/cert"
"golang.org/x/crypto/ed25519"
)
func buildPathGenerateCA(b *backend) *framework.Path {
return &framework.Path{
Pattern: "generate/ca",
Fields: map[string]*framework.FieldSchema{
"name": {
Type: framework.TypeString,
Description: `Required: name of the certificate authority`,
},
"duration": {
Type: framework.TypeString,
Description: `Optional: amount of time the certificate should be valid for. Valid time units are seconds: "s", minutes: "m", hours: "h" (default 8760h0m0s)`,
Default: "8760h",
},
"groups": {
Type: framework.TypeString,
Description: `Optional: list of groups. This will limit which groups subordinate certs can use.`,
Default: "",
},
"ips": {
Type: framework.TypeString,
Description: `Optional: list of ip and network in CIDR notation. This will limit which ip addresses and networks subordinate certs can use.`,
Default: "",
},
"subnets": {
Type: framework.TypeString,
Description: `Optional: list of ip and network in CIDR notation. This will limit which subnet addresses and networks subordinate certs can use.`,
Default: "",
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathGenerateCA,
Summary: "",
},
},
}
}
func pathConfigCA(b *backend) *framework.Path {
return &framework.Path{
Pattern: "config/ca",
Fields: map[string]*framework.FieldSchema{
"pem_bundle": {
Type: framework.TypeString,
Description: `PEM-format, unencrypted secret key and cert`,
},
},
Operations: map[logical.Operation]framework.OperationHandler{
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathConfigCAUpdate,
Summary: "",
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathConfigCADelete,
Summary: "",
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathConfigCARead,
Summary: "",
},
},
}
}
func (b *backend) pathGenerateCA(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
name := data.Get("name").(string)
if name == "" {
return nil, fmt.Errorf("nebula CA Name may not be empty")
}
nebulaCACertEntry, err := req.Storage.Get(ctx, "ca")
if nebulaCACertEntry != nil && err == nil {
return nil, fmt.Errorf("CA already present")
}
groups := data.Get("groups").(string)
_groups := parseGroups(groups)
duration := data.Get("duration").(string)
var _duration time.Duration
_duration, err = time.ParseDuration(duration)
if err != nil {
return nil, fmt.Errorf("invalid time format: %s", err)
}
ips := data.Get("ips").(string)
_ips, err := parseCIDRList(ips)
if err != nil {
return nil, fmt.Errorf("invalid ip definition: %s", err)
}
subnets := data.Get("subnets").(string)
_subnets, err := parseCIDRList(subnets)
if err != nil {
return nil, fmt.Errorf("invalid subnet definition: %s", err)
}
publicKey, privateKey, err := ed25519.GenerateKey(rand.Reader)
if err != nil {
return nil, err
}
nc := cert.NebulaCertificate{
Details: cert.NebulaCertificateDetails{
Name: name,
Groups: _groups,
Ips: _ips,
Subnets: _subnets,
NotBefore: time.Now(),
NotAfter: time.Now().Add(_duration),
PublicKey: publicKey,
IsCA: true,
},
}
nc.Sign(privateKey)
err = saveCertificateEntry(ctx, req, "ca", nc)
if err != nil {
return nil, err
}
err = saveCertificateEntry(ctx, req, "ca_key", privateKey)
if err != nil {
return nil, err
}
pemCert, err := nc.MarshalToPEM()
if err != nil {
return nil, err
}
fingerprint, err := nc.Sha256Sum()
var formattedIPs []string
for _, ipNet := range nc.Details.Ips {
formattedIPs = append(formattedIPs, ipNet.String()) // Add the CIDR string representation to the new slice
}
var formattedSubnets []string
for _, subnet := range nc.Details.Subnets {
formattedSubnets = append(formattedSubnets, subnet.String()) // Add the CIDR string representation to the new slice
}
resp := &logical.Response{
Data: map[string]interface{}{
"name": nc.Details.Name,
"fingerprint": formatFingerprint(fingerprint),
"groups": strings.Join(nc.Details.Groups, ", "),
"ips": strings.Join(formattedIPs, ", "),
"subnets": strings.Join(formattedSubnets, ", "),
"notBefore": nc.Details.NotBefore.Format("2006-01-02 15:04:05"),
"notAfter": nc.Details.NotAfter.Format("2006-01-02 15:04:05"),
"cert": string(pemCert),
},
}
return resp, err
}
func (b *backend) pathConfigCAUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
rawPemBundle, ok := data.GetOk("pem_bundle")
nebulaCACertEntry, err := req.Storage.Get(ctx, "ca")
if nebulaCACertEntry != nil && err == nil {
return nil, fmt.Errorf("CA already present")
}
if !ok {
return logical.ErrorResponse("'pem_bundle' not provided"), nil
}
pemBundle := rawPemBundle.(string)
if len(pemBundle) == 0 {
return logical.ErrorResponse("'pem_bundle' is empty"), nil
}
if len(pemBundle) < 200 {
return logical.ErrorResponse("provided data for import was too short; perhaps a path was passed to the API rather than the contents of a PEM file"), nil
}
var privateKey ed25519.PrivateKey
privateKey, rest, err := cert.UnmarshalEd25519PrivateKey([]byte(pemBundle))
if err != nil {
return nil, errutil.InternalError{Err: fmt.Sprintf("unable to decode Certificate Key: %v", err)}
}
// save private key
err = saveCertificateEntry(ctx, req, "ca_key", privateKey)
if err != nil {
return nil, err
}
nc, _, err := cert.UnmarshalNebulaCertificateFromPEM(rest)
if err != nil {
return nil, errutil.InternalError{Err: fmt.Sprintf("unable to decode Certificate: %v", err)}
}
if !nc.Details.IsCA {
return nil, errutil.InternalError{Err: "Certificate is not a Nebula CA"}
}
err = saveCertificateEntry(ctx, req, "ca", nc)
if err != nil {
return nil, err
}
pemCert, err := nc.MarshalToPEM()
if err != nil {
return nil, err
}
resp := &logical.Response{
Data: map[string]interface{}{
"name": nc.Details.Name,
"cert": string(pemCert),
},
}
return resp, err
}
func (b *backend) pathConfigCARead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
nebulaCACertEntry, err := req.Storage.Get(ctx, "ca")
if err != nil {
return nil, errutil.InternalError{Err: fmt.Sprintf("unable to fetch nebula ca: %v", err)}
}
var nc cert.NebulaCertificate
if err := nebulaCACertEntry.DecodeJSON(&nc); err != nil {
return nil, errutil.InternalError{Err: fmt.Sprintf("unable to decode Nebula Certificate: %v", err)}
}
certDetails := nc.Details
pemCert, _ := nc.MarshalToPEM()
resp := &logical.Response{
Data: map[string]interface{}{
"name": certDetails.Name,
"public_key": string(pemCert),
},
}
return resp, nil
}
func (b *backend) pathConfigCADelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
return nil, nil
}