forked from spiffe/spire
/
utils.go
165 lines (140 loc) · 4.28 KB
/
utils.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
package k8s
import (
"errors"
"fmt"
"net/url"
"path"
"strings"
"gopkg.in/square/go-jose.v2/jwt"
authv1 "k8s.io/api/authentication/v1"
)
const (
k8sPodNameKey = "authentication.kubernetes.io/pod-name"
k8sPodUIDKey = "authentication.kubernetes.io/pod-uid"
)
// SATClaims represents claims in a service account token, for example:
//
// {
// "iss": "kubernetes/serviceaccount",
// "kubernetes.io/serviceaccount/namespace": "spire",
// "kubernetes.io/serviceaccount/secret.name": "spire-agent-token-zjr8v",
// "kubernetes.io/serviceaccount/service-account.name": "spire-agent",
// "kubernetes.io/serviceaccount/service-account.uid": "1881e84f-b612-11e8-a543-0800272c6e42",
// "sub": "system:serviceaccount:spire:spire-agent"
// }
type SATClaims struct {
jwt.Claims
Namespace string `json:"kubernetes.io/serviceaccount/namespace"`
ServiceAccountName string `json:"kubernetes.io/serviceaccount/service-account.name"`
// This struct is included in case that a projected service account token is
// parsed as a regular service account token
K8s struct {
Namespace string `json:"namespace"`
ServiceAccount struct {
Name string `json:"name"`
} `json:"serviceaccount"`
} `json:"kubernetes.io"`
}
// PSATClaims represents claims in a projected service account token, for example:
//
// {
// "aud": [
// "spire-server"
// ],
// "exp": 1550850854,
// "iat": 1550843654,
// "iss": "api",
// "kubernetes.io": {
// "namespace": "spire",
// "pod": {
// "name": "spire-agent-5d84p",
// "uid": "56857f33-36a9-11e9-860c-080027b25557"
// },
// "serviceaccount": {
// "name": "spire-agent",
// "uid": "ca29bd95-36a8-11e9-b8af-080027b25557"
// }
// },
// "nbf": 1550843654,
// "sub": "system:serviceaccount:spire:spire-agent"
// }
type PSATClaims struct {
jwt.Claims
K8s struct {
Namespace string `json:"namespace"`
Pod struct {
Name string `json:"name"`
UID string `json:"uid"`
} `json:"pod"`
ServiceAccount struct {
Name string `json:"name"`
UID string `json:"uid"`
} `json:"serviceaccount"`
} `json:"kubernetes.io"`
}
type SATAttestationData struct {
Cluster string `json:"cluster"`
Token string `json:"token"`
}
type PSATAttestationData struct {
Cluster string `json:"cluster"`
Token string `json:"token"`
}
func AgentID(pluginName, trustDomain, cluster, uuid string) string {
u := url.URL{
Scheme: "spiffe",
Host: trustDomain,
Path: path.Join("spire", "agent", pluginName, cluster, uuid),
}
return u.String()
}
func MakeSelectorValue(kind string, values ...string) string {
return fmt.Sprintf("%s:%s", kind, strings.Join(values, ":"))
}
// GetNamesFromTokenStatus parses a fully qualified k8s username like: 'system:serviceaccount:spire:spire-agent'
// from tokenStatus. The string is split and the last two names are returned: namespace and service account name
func GetNamesFromTokenStatus(tokenStatus *authv1.TokenReviewStatus) (string, string, error) {
username := tokenStatus.User.Username
if username == "" {
return "", "", errors.New("empty username")
}
names := strings.Split(username, ":")
if len(names) != 4 {
return "", "", fmt.Errorf("unexpected username format: %v", username)
}
if names[2] == "" {
return "", "", fmt.Errorf("missing namespace")
}
if names[3] == "" {
return "", "", fmt.Errorf("missing service account name")
}
return names[2], names[3], nil
}
// GetPodNameFromTokenStatus extracts pod name from a tokenReviewStatus type
func GetPodNameFromTokenStatus(tokenStatus *authv1.TokenReviewStatus) (string, error) {
podName, ok := tokenStatus.User.Extra[k8sPodNameKey]
if !ok {
return "", errors.New("missing pod name")
}
if len(podName) != 1 {
return "", fmt.Errorf("expected 1 name but got: %d", len(podName))
}
if podName[0] == "" {
return "", errors.New("pod name is empty")
}
return podName[0], nil
}
// GetPodUIDFromTokenStatus extracts pod UID from a tokenReviewStatus type
func GetPodUIDFromTokenStatus(tokenStatus *authv1.TokenReviewStatus) (string, error) {
podUID, ok := tokenStatus.User.Extra[k8sPodUIDKey]
if !ok {
return "", errors.New("missing pod UID")
}
if len(podUID) != 1 {
return "", fmt.Errorf("expected 1 UID but got: %d", len(podUID))
}
if podUID[0] == "" {
return "", errors.New("pod UID is empty")
}
return podUID[0], nil
}