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
package vaultlayout
import (
"crypto/sha256"
"encoding/hex"
"strings"
)
const (
NodesKVMount = "socialweb-nodes"
NodeJWTPolicyPrefix = "node-"
NodeJWTMountPrefix = "jwt-"
NodeJWTMountHashHex = 12
NodeJWTTokenTTL = "5m"
NodeJWTUserClaim = "wid_sub"
NodeJWTRoleType = "jwt"
)
func NodeKVDataPath(node string) string {
return NodesKVMount + "/data/" + node
}
func NodeKVMetadataPath(node string) string {
return NodesKVMount + "/metadata/" + node
}
func NodeJWTPolicyName(node string) string {
return NodeJWTPolicyPrefix + node
}
func NodeFromJWTPolicy(name string) (string, bool) {
node, ok := strings.CutPrefix(name, NodeJWTPolicyPrefix)
if !ok || node == "" {
return "", false
}
return node, true
}
func NodeJWTPolicyHCL(node string) string {
return readPolicy(NodeKVDataPath(node))
}
func NodeJWTRoleName(node string) string {
return node
}
func NodeJWTMount(issuer string) string {
sum := sha256.Sum256([]byte(issuer))
return NodeJWTMountPrefix + hex.EncodeToString(sum[:])[:NodeJWTMountHashHex]
}
func IsNodeJWTMount(path string) bool {
name := strings.Trim(path, "/")
rest, ok := strings.CutPrefix(name, NodeJWTMountPrefix)
if !ok || len(rest) != NodeJWTMountHashHex {
return false
}
_, err := hex.DecodeString(rest)
return err == nil
}