-
Notifications
You must be signed in to change notification settings - Fork 280
/
token.go
159 lines (139 loc) · 5.3 KB
/
token.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
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package k8s
import (
"encoding/json"
"fmt"
"time"
authenticationv1 "k8s.io/api/authentication/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
kubeinformers "k8s.io/client-go/informers"
storageinformers "k8s.io/client-go/informers/storage/v1"
"k8s.io/client-go/kubernetes"
storagelisters "k8s.io/client-go/listers/storage/v1"
"k8s.io/client-go/tools/cache"
"k8s.io/klog/v2"
"sigs.k8s.io/secrets-store-csi-driver/pkg/k8s/token"
)
// TokenClient is a client for Kubernetes Token API
type TokenClient struct {
driverName string
csiDriverInformer storageinformers.CSIDriverInformer
csiDriverLister storagelisters.CSIDriverLister
manager *token.Manager
}
// NewTokenClient creates a new TokenClient
// The client will be used to request a token for token requests configured in the CSIDriver.
func NewTokenClient(kubeClient kubernetes.Interface, driverName string, resyncPeriod time.Duration) *TokenClient {
kubeInformerFactory := kubeinformers.NewSharedInformerFactoryWithOptions(
kubeClient,
resyncPeriod,
kubeinformers.WithTweakListOptions(
func(options *metav1.ListOptions) {
options.FieldSelector = fmt.Sprintf("metadata.name=%s", driverName)
},
),
)
csiDriverInformer := kubeInformerFactory.Storage().V1().CSIDrivers()
csiDriverLister := csiDriverInformer.Lister()
return &TokenClient{
driverName: driverName,
csiDriverInformer: csiDriverInformer,
csiDriverLister: csiDriverLister,
manager: token.NewManager(kubeClient),
}
}
// Run initiates the sync of the informers and caches
func (c *TokenClient) Run(stopCh <-chan struct{}) error {
go c.csiDriverInformer.Informer().Run(stopCh)
if !cache.WaitForCacheSync(stopCh, c.csiDriverInformer.Informer().HasSynced) {
return fmt.Errorf("failed to sync informer caches")
}
return nil
}
// PodServiceAccountTokenAttrs returns the token for the pod service account that can be bound to the pod.
// This token will be sent to the providers and is of the format:
//
// "csi.storage.k8s.io/serviceAccount.tokens": {
// <audience>: {
// 'token': <token>,
// 'expirationTimestamp': <expiration timestamp in RFC3339 format>,
// },
// ...
// }
//
// ref: https://kubernetes-csi.github.io/docs/token-requests.html#usage
func (c *TokenClient) PodServiceAccountTokenAttrs(namespace, podName, serviceAccountName string, podUID types.UID) (map[string]string, error) {
csiDriver, err := c.csiDriverLister.Get(c.driverName)
if err != nil {
if apierrors.IsNotFound(err) {
klog.V(5).InfoS("CSIDriver not found, not adding service account token information", "driver", c.driverName)
return nil, nil
}
return nil, err
}
if len(csiDriver.Spec.TokenRequests) == 0 {
return nil, nil
}
outputs := map[string]authenticationv1.TokenRequestStatus{}
for _, tokenRequest := range csiDriver.Spec.TokenRequests {
audience := tokenRequest.Audience
audiences := []string{audience}
if audience == "" {
audiences = []string{}
}
tr := &authenticationv1.TokenRequest{
Spec: authenticationv1.TokenRequestSpec{
ExpirationSeconds: tokenRequest.ExpirationSeconds,
Audiences: audiences,
BoundObjectRef: &authenticationv1.BoundObjectReference{
APIVersion: "v1",
Kind: "Pod",
Name: podName,
UID: podUID,
},
},
}
tr, err := c.GetServiceAccountToken(namespace, serviceAccountName, tr)
if err != nil {
return nil, err
}
outputs[audience] = tr.Status
}
klog.V(4).InfoS("Fetched service account token attrs for CSIDriver", "driver", c.driverName, "podUID", podUID)
tokens, err := json.Marshal(outputs)
if err != nil {
return nil, err
}
return map[string]string{
"csi.storage.k8s.io/serviceAccount.tokens": string(tokens),
}, nil
}
// GetServiceAccountToken gets a service account token for a pod from cache or
// from the TokenRequest API. This process is as follows:
// * Check the cache for the current token request.
// * If the token exists and does not require a refresh, return the current token.
// * Attempt to refresh the token.
// * If the token is refreshed successfully, save it in the cache and return the token.
// * If refresh fails and the old token is still valid, log an error and return the old token.
// * If refresh fails and the old token is no longer valid, return an error
func (c *TokenClient) GetServiceAccountToken(namespace, name string, tr *authenticationv1.TokenRequest) (*authenticationv1.TokenRequest, error) {
return c.manager.GetServiceAccountToken(namespace, name, tr)
}
// DeleteServiceAccountToken should be invoked when pod got deleted. It simply
// clean token manager cache.
func (c *TokenClient) DeleteServiceAccountToken(podUID types.UID) {
c.manager.DeleteServiceAccountToken(podUID)
}