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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package vaultlayout
import (
"errors"
"fmt"
"strings"
"time"
"github.com/johnandersen777/socialweb-computer-kcp/internal/identitybundle"
)
type VaultName string
const (
Root VaultName = "root"
Cluster VaultName = "cluster"
)
type MountKind string
const (
MountKV MountKind = "kv-v2"
MountPKI MountKind = "pki"
)
const (
MountKVRoot = "secret/"
MountPKIRoot = "pki/"
MountKVCluster = "secret/"
)
const (
PathKCPAdmin = "secret/data/socialweb/kcp/admin"
PathKCPAdminTokenStore = "secret/data/socialweb/kcp/admin-token-store"
PathKCPClientCA = "secret/data/socialweb/kcp/client-ca"
PathContainerTLS = "secret/data/socialweb/container/tls"
PathClusterIdentity = "secret/data/k3s/identity"
PathClusterInstanceToken = "secret/data/k3s/instance-token"
PathClusterPostgres = "secret/data/k3s/postgres"
PathClusterSSH = "secret/data/k3s/ssh"
PathDoorClaimsPrefix = "secret/data/socialweb-door/claims/"
)
const DoorClaimSecretsIngressKey = "secrets-ingress"
func DoorClaimSecretsIngressPath(claim string) string {
return PathDoorClaimsPrefix + claim + "/" + DoorClaimSecretsIngressKey
}
const (
RootServerName = "root-vault"
RootClient = "gateway"
PolicyRootWriter = "root-writer"
)
const (
PolicyKCPRuntime = "kcp-runtime"
PolicyKCPAdminTokenStore = "kcp-admin-token-store"
PolicyKCPCA = "kcp-ca"
PolicyContainerTLS = "container-tls"
PolicyKCPClientIssue = "kcp-client-issue"
PolicyIdentityBundle = "identity-bundle"
PolicyInstanceToken = "instance-token"
PolicyPostgres = "postgres"
PolicySSH = "ssh"
)
const (
RoleGateway = "gateway"
RoleClusterProvider = "clusterprovider"
RoleContainerTLS = "container-tls"
RoleClusterIdentity = "cluster-identity"
RoleClusterData = "cluster-data"
RoleClusterNode = "cluster-node"
RoleKCPClient = "kcp-client"
)
type SecretClassName string
const (
ClassKCPAdminKubeconfig SecretClassName = "kcp-admin-kubeconfig"
ClassKCPAdminTokenStore SecretClassName = "kcp-admin-token-store"
ClassKCPClientCA SecretClassName = "kcp-client-ca"
ClassContainerTLS SecretClassName = "container-tls-certificate"
ClassIdentityBundle SecretClassName = "identity-bundle"
ClassInstanceToken SecretClassName = "instance-token"
ClassPostgresCredential SecretClassName = "postgres-credential"
ClassPerClusterSSHKey SecretClassName = "per-cluster-ssh-key"
)
type Mount struct {
Vault VaultName
Path string
Kind MountKind
MaxLeaseTTL time.Duration
}
type Policy struct {
Vault VaultName
Name string
HCL string
}
type AppRole struct {
Vault VaultName
Name string
Policies []string
TokenTTL time.Duration
TokenMaxTTL time.Duration
SecretIDTTL time.Duration
}
type PKIRole struct {
Vault VaultName
Mount string
Name string
AllowedDomains []string
AllowSubdomains bool
AllowBareDomains bool
AllowIPSANs bool
EnforceHostnames bool
RequireCN bool
ClientAuth bool
ServerAuth bool
KeyUsage []string
KeyType string
KeyBits int
TTL time.Duration
MaxTTL time.Duration
}
type SecretClass struct {
Name SecretClassName
Vault VaultName
}
type Secret struct {
Vault VaultName
Path string
Class SecretClassName
Consumer string
Keys []string
}
type Layout struct {
Mounts []Mount
Policies []Policy
AppRoles []AppRole
PKIRoles []PKIRole
Secrets []Secret
SecretClasses []SecretClass
}
func readPolicy(paths ...string) string {
var b strings.Builder
for _, p := range paths {
fmt.Fprintf(&b, "path \"%s\" {\n capabilities = [\"read\"]\n}\n", p)
}
return b.String()
}
func issuePolicy(paths ...string) string {
var b strings.Builder
for _, p := range paths {
fmt.Fprintf(&b, "path \"%s\" {\n capabilities = [\"create\", \"update\"]\n}\n", p)
}
return b.String()
}
func Default() Layout {
return Layout{
Mounts: []Mount{
{Vault: Root, Path: MountKVRoot, Kind: MountKV},
{Vault: Root, Path: MountPKIRoot, Kind: MountPKI, MaxLeaseTTL: 8760 * time.Hour},
{Vault: Cluster, Path: MountKVCluster, Kind: MountKV},
},
Policies: []Policy{
{Vault: Root, Name: PolicyKCPRuntime, HCL: readPolicy(PathKCPAdmin)},
{Vault: Root, Name: PolicyKCPAdminTokenStore, HCL: readPolicy(PathKCPAdminTokenStore)},
{Vault: Root, Name: PolicyKCPCA, HCL: readPolicy(PathKCPClientCA)},
{Vault: Root, Name: PolicyContainerTLS, HCL: readPolicy(PathContainerTLS)},
{Vault: Root, Name: PolicyKCPClientIssue, HCL: issuePolicy(MountPKIRoot + "issue/" + RoleKCPClient)},
{Vault: Cluster, Name: PolicyIdentityBundle, HCL: readPolicy(PathClusterIdentity)},
{Vault: Cluster, Name: PolicyInstanceToken, HCL: readPolicy(PathClusterInstanceToken)},
{Vault: Cluster, Name: PolicyPostgres, HCL: readPolicy(PathClusterPostgres)},
{Vault: Cluster, Name: PolicySSH, HCL: readPolicy(PathClusterSSH)},
},
AppRoles: []AppRole{
{
Vault: Root,
Name: RoleGateway,
Policies: []string{PolicyKCPRuntime, PolicyKCPClientIssue},
TokenTTL: time.Hour,
TokenMaxTTL: 4 * time.Hour,
SecretIDTTL: 30 * time.Minute,
},
{
Vault: Root,
Name: RoleClusterProvider,
Policies: []string{PolicyKCPAdminTokenStore, PolicyKCPCA},
TokenTTL: time.Hour,
TokenMaxTTL: 4 * time.Hour,
SecretIDTTL: 30 * time.Minute,
},
{
Vault: Root,
Name: RoleContainerTLS,
Policies: []string{PolicyContainerTLS},
TokenTTL: time.Hour,
TokenMaxTTL: 4 * time.Hour,
SecretIDTTL: 30 * time.Minute,
},
{
Vault: Cluster,
Name: RoleClusterIdentity,
Policies: []string{PolicyIdentityBundle, PolicyInstanceToken},
TokenTTL: time.Hour,
TokenMaxTTL: 4 * time.Hour,
SecretIDTTL: 30 * time.Minute,
},
{
Vault: Cluster,
Name: RoleClusterData,
Policies: []string{PolicyPostgres},
TokenTTL: time.Hour,
TokenMaxTTL: 4 * time.Hour,
SecretIDTTL: 30 * time.Minute,
},
{
Vault: Cluster,
Name: RoleClusterNode,
Policies: []string{PolicySSH},
TokenTTL: time.Hour,
TokenMaxTTL: 4 * time.Hour,
SecretIDTTL: 30 * time.Minute,
},
},
PKIRoles: []PKIRole{
{
Vault: Root,
Mount: MountPKIRoot,
Name: RoleKCPClient,
AllowedDomains: []string{"socialweb.local"},
AllowSubdomains: true,
AllowBareDomains: false,
AllowIPSANs: false,
EnforceHostnames: true,
RequireCN: true,
ClientAuth: true,
ServerAuth: false,
KeyUsage: []string{"DigitalSignature", "KeyEncipherment"},
KeyType: "ecdsa",
KeyBits: 256,
TTL: 168 * time.Hour,
MaxTTL: 720 * time.Hour,
},
},
Secrets: []Secret{
{
Vault: Root,
Path: PathKCPAdmin,
Class: ClassKCPAdminKubeconfig,
Consumer: RoleGateway,
Keys: []string{"kubeconfig"},
},
{
Vault: Root,
Path: PathKCPAdminTokenStore,
Class: ClassKCPAdminTokenStore,
Consumer: RoleClusterProvider,
Keys: []string{"token-hash"},
},
{
Vault: Root,
Path: PathKCPClientCA,
Class: ClassKCPClientCA,
Consumer: RoleClusterProvider,
Keys: []string{"ca.crt"},
},
{
Vault: Root,
Path: PathContainerTLS,
Class: ClassContainerTLS,
Consumer: RoleContainerTLS,
Keys: []string{"tls.crt", "tls.key"},
},
{
Vault: Cluster,
Path: PathClusterIdentity,
Class: ClassIdentityBundle,
Consumer: RoleClusterIdentity,
Keys: identitybundle.Paths,
},
{
Vault: Cluster,
Path: PathClusterInstanceToken,
Class: ClassInstanceToken,
Consumer: RoleClusterIdentity,
Keys: []string{"token"},
},
{
Vault: Cluster,
Path: PathClusterPostgres,
Class: ClassPostgresCredential,
Consumer: RoleClusterData,
Keys: []string{"username", "password", "database"},
},
{
Vault: Cluster,
Path: PathClusterSSH,
Class: ClassPerClusterSSHKey,
Consumer: RoleClusterNode,
Keys: []string{"id_ed25519", "id_ed25519.pub"},
},
},
SecretClasses: []SecretClass{
{Name: ClassKCPAdminKubeconfig, Vault: Root},
{Name: ClassKCPAdminTokenStore, Vault: Root},
{Name: ClassKCPClientCA, Vault: Root},
{Name: ClassContainerTLS, Vault: Root},
{Name: ClassIdentityBundle, Vault: Cluster},
{Name: ClassInstanceToken, Vault: Cluster},
{Name: ClassPostgresCredential, Vault: Cluster},
{Name: ClassPerClusterSSHKey, Vault: Cluster},
},
}
}
func policyPaths(hcl string) []string {
var out []string
for line := range strings.SplitSeq(hcl, "\n") {
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "path \"") {
continue
}
rest := strings.TrimPrefix(line, "path \"")
if before, _, ok := strings.Cut(rest, "\""); ok {
out = append(out, before)
}
}
return out
}
func providedBy(mounts map[string]Mount, path string) bool {
for _, m := range mounts {
if strings.HasPrefix(path, m.Path) {
return true
}
}
return false
}
func Validate(l Layout) error {
var errs []error
mounts := map[VaultName]map[string]Mount{}
for _, m := range l.Mounts {
if mounts[m.Vault] == nil {
mounts[m.Vault] = map[string]Mount{}
}
mounts[m.Vault][m.Path] = m
}
policies := map[VaultName]map[string]Policy{}
for _, p := range l.Policies {
if policies[p.Vault] == nil {
policies[p.Vault] = map[string]Policy{}
}
if _, dup := policies[p.Vault][p.Name]; dup {
errs = append(errs, fmt.Errorf("policy %s/%s is declared twice", p.Vault, p.Name))
}
policies[p.Vault][p.Name] = p
for _, path := range policyPaths(p.HCL) {
if !providedBy(mounts[p.Vault], path) {
errs = append(errs, fmt.Errorf(
"policy %s/%s references path %q, which no mount in the %s vault provides",
p.Vault, p.Name, path, p.Vault))
}
}
}
roles := map[VaultName]map[string]AppRole{}
for _, r := range l.AppRoles {
if roles[r.Vault] == nil {
roles[r.Vault] = map[string]AppRole{}
}
if _, dup := roles[r.Vault][r.Name]; dup {
errs = append(errs, fmt.Errorf("role %s/%s is declared twice", r.Vault, r.Name))
}
roles[r.Vault][r.Name] = r
if len(r.Policies) == 0 {
errs = append(errs, fmt.Errorf("role %s/%s names no policy", r.Vault, r.Name))
}
for _, name := range r.Policies {
if _, ok := policies[r.Vault][name]; !ok {
errs = append(errs, fmt.Errorf(
"role %s/%s references policy %q, which does not exist in the %s vault",
r.Vault, r.Name, name, r.Vault))
}
}
}
for _, r := range l.PKIRoles {
m, ok := mounts[r.Vault][r.Mount]
if !ok {
errs = append(errs, fmt.Errorf(
"PKI role %s/%s names mount %q, which the %s vault does not have",
r.Vault, r.Name, r.Mount, r.Vault))
continue
}
if m.Kind != MountPKI {
errs = append(errs, fmt.Errorf(
"PKI role %s/%s names mount %q, which is %s and not pki",
r.Vault, r.Name, r.Mount, m.Kind))
}
if r.MaxTTL > m.MaxLeaseTTL {
errs = append(errs, fmt.Errorf(
"PKI role %s/%s has max TTL %s, above its mount %q max lease TTL %s",
r.Vault, r.Name, r.MaxTTL, r.Mount, m.MaxLeaseTTL))
}
if !r.ClientAuth && !r.ServerAuth {
errs = append(errs, fmt.Errorf(
"PKI role %s/%s grants neither client nor server auth", r.Vault, r.Name))
}
}
declared := map[SecretClassName]SecretClass{}
for _, c := range l.SecretClasses {
if _, dup := declared[c.Name]; dup {
errs = append(errs, fmt.Errorf("secret class %q is declared twice", c.Name))
}
declared[c.Name] = c
}
counts := map[SecretClassName]int{}
for _, s := range l.Secrets {
c, ok := declared[s.Class]
if !ok {
errs = append(errs, fmt.Errorf(
"secret %s/%s has class %q, which is not declared", s.Vault, s.Path, s.Class))
continue
}
counts[s.Class]++
if c.Vault != s.Vault {
errs = append(errs, fmt.Errorf(
"secret %s/%s has class %q, whose home is the %s vault, not the %s vault",
s.Vault, s.Path, s.Class, c.Vault, s.Vault))
}
if _, ok := roles[s.Vault][s.Consumer]; !ok {
errs = append(errs, fmt.Errorf(
"secret %s/%s names consumer %q, which is not a role in the %s vault",
s.Vault, s.Path, s.Consumer, s.Vault))
}
if !providedBy(mounts[s.Vault], s.Path) {
errs = append(errs, fmt.Errorf(
"secret %s/%s is not under any mount in the %s vault", s.Vault, s.Path, s.Vault))
}
if s.Class == ClassIdentityBundle && len(s.Keys) != len(identitybundle.Paths) {
errs = append(errs, fmt.Errorf(
"secret %s/%s is the identity bundle and holds %d keys, want %d",
s.Vault, s.Path, len(s.Keys), len(identitybundle.Paths)))
}
}
for _, c := range l.SecretClasses {
switch {
case counts[c.Name] == 0:
errs = append(errs, fmt.Errorf(
"secret class %q is assigned to no secret, so it has no vault", c.Name))
case counts[c.Name] > 1:
errs = append(errs, fmt.Errorf(
"secret class %q is assigned to %d secrets, so it has more than one vault",
c.Name, counts[c.Name]))
}
}
return errors.Join(errs...)
}