-
Notifications
You must be signed in to change notification settings - Fork 157
/
bound_sa_token_test.go
304 lines (266 loc) · 11.8 KB
/
bound_sa_token_test.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
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
package e2e
import (
"bytes"
"context"
"encoding/json"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/require"
authenticationv1 "k8s.io/api/authentication/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
clientcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
configclient "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
tokenctl "github.com/openshift/cluster-kube-apiserver-operator/pkg/operator/boundsatokensignercontroller"
"github.com/openshift/cluster-kube-apiserver-operator/pkg/operator/operatorclient"
test "github.com/openshift/cluster-kube-apiserver-operator/test/library"
testlibrary "github.com/openshift/library-go/test/library"
)
const (
interval = 1 * time.Second
regularTimeout = 30 * time.Second
// Need a long time for promotion to account for the delay in
// nodes being updated to a revision of the configmap that
// contains the latest public key.
promotionTimeout = 40 * time.Minute
)
// TestBoundTokenSignerController verifies the expected behavior of the controller
// with respect to the resources it manages.
func TestBoundTokenSignerController(t *testing.T) {
kubeConfig, err := testlibrary.NewClientConfigForTest()
require.NoError(t, err)
kubeClient, err := clientcorev1.NewForConfig(kubeConfig)
require.NoError(t, err)
configClient, err := configclient.NewForConfig(kubeConfig)
require.NoError(t, err)
targetNamespace := operatorclient.TargetNamespace
operatorNamespace := operatorclient.OperatorNamespace
// Retrieve the operator secret. The values in the secret and config map in the
// operand namespace should match the values in the operator secret.
operatorSecret, err := kubeClient.Secrets(operatorNamespace).Get(context.TODO(), tokenctl.NextSigningKeySecretName, metav1.GetOptions{})
require.NoError(t, err)
operatorPublicKey := operatorSecret.Data[tokenctl.PublicKeyKey]
operatorPrivateKey := operatorSecret.Data[tokenctl.PrivateKeyKey]
// The operand secret should be recreated after deletion.
t.Run("operand-secret-deletion", func(t *testing.T) {
test.WaitForKubeAPIServerClusterOperatorAvailableNotProgressingNotDegraded(t, configClient)
err := kubeClient.Secrets(targetNamespace).Delete(context.TODO(), tokenctl.SigningKeySecretName, metav1.DeleteOptions{})
require.NoError(t, err)
checkBoundTokenOperandSecret(t, kubeClient, nil, regularTimeout, operatorSecret.Data)
})
// The operand config map should be recreated after deletion.
t.Run("configmap-deletion", func(t *testing.T) {
test.WaitForKubeAPIServerClusterOperatorAvailableNotProgressingNotDegraded(t, configClient)
err := kubeClient.ConfigMaps(targetNamespace).Delete(context.TODO(), tokenctl.PublicKeyConfigMapName, metav1.DeleteOptions{})
require.NoError(t, err)
checkCertConfigMap(t, kubeClient, map[string]string{
"service-account-001.pub": string(operatorPublicKey),
})
})
// The secret in the operator namespace should be recreated with a new keypair
// after deletion. The configmap in the operand namespace should be updated
// immediately, and the secret once the configmap is present on all nodes.
t.Run("operator-secret-deletion", func(t *testing.T) {
test.WaitForKubeAPIServerClusterOperatorAvailableNotProgressingNotDegraded(t, configClient)
// Delete the operator secret
err := kubeClient.Secrets(operatorNamespace).Delete(context.TODO(), tokenctl.NextSigningKeySecretName, metav1.DeleteOptions{})
require.NoError(t, err)
// Ensure that the cert configmap is always removed at the end of the test
// to ensure it will contain only the current public key. This property is
// essential to allowing repeated invocations of the containing test.
defer func() {
err := kubeClient.ConfigMaps(targetNamespace).Delete(context.TODO(), tokenctl.PublicKeyConfigMapName, metav1.DeleteOptions{})
require.NoError(t, err)
}()
// Wait for secret to be recreated with a new keypair
var newOperatorSecret *corev1.Secret
err = wait.PollImmediate(interval, regularTimeout, func() (done bool, err error) {
newOperatorSecret, err = kubeClient.Secrets(operatorNamespace).Get(context.TODO(), tokenctl.NextSigningKeySecretName, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
return false, nil
}
if err != nil {
t.Errorf("failed to retrieve template secret: %v", err)
return false, nil
}
return true, nil
})
require.NoError(t, err)
newOperatorPublicKey := newOperatorSecret.Data[tokenctl.PublicKeyKey]
newOperatorPrivateKey := newOperatorSecret.Data[tokenctl.PrivateKeyKey]
// Keypair should have changed
require.NotEqual(t, operatorPublicKey, newOperatorPublicKey)
require.NotEqual(t, operatorPrivateKey, newOperatorPrivateKey)
// The certs configmap should include the previous and current public keys
checkCertConfigMap(t, kubeClient, map[string]string{
"service-account-001.pub": string(operatorPublicKey),
"service-account-002.pub": string(newOperatorPublicKey),
})
// Check that the operand secret is updated within the promotion timeout
checkBoundTokenOperandSecret(t, kubeClient, func() {
t.Log("WaitForKubeAPIServerClusterOperatorAvailableNotProgressingNotDegraded")
test.WaitForKubeAPIServerClusterOperatorAvailableNotProgressingNotDegraded(t, configClient)
}, promotionTimeout, newOperatorSecret.Data)
})
}
// checkBoundTokenOperandSecret checks that the operand secret is
// populated with the expected data.
func checkBoundTokenOperandSecret(t *testing.T, kubeClient *clientcorev1.CoreV1Client, additionalCheckFn func(), timeout time.Duration, expectedData map[string][]byte) {
err := wait.PollImmediate(interval, timeout, func() (done bool, err error) {
if additionalCheckFn != nil {
additionalCheckFn()
}
secret, err := kubeClient.Secrets(operatorclient.TargetNamespace).Get(context.TODO(), tokenctl.SigningKeySecretName, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
return false, nil
}
if err != nil {
t.Errorf("failed to retrieve signing secret template: %v", err)
return false, nil
}
if !reflect.DeepEqual(secret.Data, expectedData) {
t.Log("secret data is not as expected")
return false, nil
}
return true, nil
})
require.NoError(t, err)
}
// checkCertConfigMap checks that the cert configmap is present and populated with
// the expected data.
func checkCertConfigMap(t *testing.T, kubeClient *clientcorev1.CoreV1Client, expectedData map[string]string) {
err := wait.PollImmediate(interval, regularTimeout, func() (done bool, err error) {
configMap, err := kubeClient.ConfigMaps(operatorclient.TargetNamespace).Get(context.TODO(), tokenctl.PublicKeyConfigMapName, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
return false, nil
}
if err != nil {
t.Errorf("failed to retrieve cert configmap: %v", err)
return false, nil
}
if !reflect.DeepEqual(configMap.Data, expectedData) {
t.Log("secret data is not yet as expected")
return false, nil
}
return true, nil
})
require.NoError(t, err)
}
// TestTokenRequestAndReview checks that bound sa tokens are correctly
// configured. A token is requested via the TokenRequest API and
// validated via the TokenReview API.
func TestTokenRequestAndReview(t *testing.T) {
kubeConfig, err := testlibrary.NewClientConfigForTest()
require.NoError(t, err)
kubeClient, err := kubernetes.NewForConfig(kubeConfig)
require.NoError(t, err)
corev1client := kubeClient.CoreV1()
// Create all test resources in a temp namespace that will be
// removed at the end of the test to avoid requiring explicit
// cleanup.
ns, err := corev1client.Namespaces().Create(context.TODO(), &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "e2e-token-request-",
},
}, metav1.CreateOptions{})
require.NoError(t, err)
defer func() {
err := corev1client.Namespaces().Delete(context.TODO(), ns.Name, metav1.DeleteOptions{})
require.NoError(t, err)
}()
namespace := ns.Name
sa, err := corev1client.ServiceAccounts(namespace).Create(context.TODO(), &v1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "test-service-account",
},
}, metav1.CreateOptions{})
require.NoError(t, err)
treq, err := corev1client.ServiceAccounts(sa.Namespace).CreateToken(context.TODO(),
sa.Name,
&authenticationv1.TokenRequest{
Spec: authenticationv1.TokenRequestSpec{
// Avoid specifying any audiences so that the token will be
// issued for the default audience of the issuer.
},
},
metav1.CreateOptions{})
require.NoError(t, err)
trev, err := kubeClient.AuthenticationV1().TokenReviews().Create(context.TODO(), &authenticationv1.TokenReview{
Spec: authenticationv1.TokenReviewSpec{
Token: treq.Status.Token,
},
}, metav1.CreateOptions{})
require.NoError(t, err)
require.Empty(t, trev.Status.Error)
require.True(t, trev.Status.Authenticated)
}
// TestChangeServiceAccountIssuer checks that the operator considers
// the value set for Authentication.ServiceAccountIssuer when setting
// the configuration configmap for the apiserver pods.
func TestChangeServiceAccountIssuer(t *testing.T) {
kubeConfig, err := testlibrary.NewClientConfigForTest()
require.NoError(t, err)
kubeClient, err := kubernetes.NewForConfig(kubeConfig)
require.NoError(t, err)
coreClient := kubeClient.CoreV1()
configClient, err := configclient.NewForConfig(kubeConfig)
require.NoError(t, err)
// Wait for operator readiness
test.WaitForKubeAPIServerClusterOperatorAvailableNotProgressingNotDegraded(t, configClient)
defaultIssuer := "https://kubernetes.default.svc"
// Check that the default issuer is set in the operand config
require.NoError(t, pollForOperandIssuer(t, coreClient, defaultIssuer))
nonDefaultIssuer := "https://my-custom-issuer.com"
// Update the issuer to a valid value (corner cases are unit tested)
setServiceAccountIssuer(t, configClient, nonDefaultIssuer)
// Check that the issuer has changed to the non-default value
require.NoError(t, pollForOperandIssuer(t, coreClient, nonDefaultIssuer))
// Clear the issuer
setServiceAccountIssuer(t, configClient, "")
// Check that the issuer has changed back to the default
require.NoError(t, pollForOperandIssuer(t, coreClient, defaultIssuer))
}
func pollForOperandIssuer(t *testing.T, client clientcorev1.CoreV1Interface, expectedIssuer string) error {
return wait.PollImmediate(interval, regularTimeout, func() (done bool, err error) {
configMap, err := client.ConfigMaps(operatorclient.TargetNamespace).Get(context.TODO(), "config", metav1.GetOptions{})
if err != nil {
t.Errorf("failed to retrieve apiserver config configmap: %v", err)
return false, nil
}
// key has a .yaml extension but actual format is json
rawConfig := configMap.Data["config.yaml"]
if len(rawConfig) == 0 {
t.Logf("config.yaml is empty in apiserver config configmap")
return false, nil
}
config := map[string]interface{}{}
if err := json.NewDecoder(bytes.NewBuffer([]byte(rawConfig))).Decode(&config); err != nil {
t.Errorf("error parsing config, %v", err)
return false, nil
}
issuers, found, err := unstructured.NestedStringSlice(config, "apiServerArguments", "service-account-issuer")
if !found {
t.Log("apiServerArguments.service-account-issuer not found in config")
return false, nil
}
issuer := issuers[0]
if !found || expectedIssuer != issuer {
t.Logf("expected service account issuer to be %q, got %q", expectedIssuer, issuer)
return false, nil
}
return true, nil
})
}
func setServiceAccountIssuer(t *testing.T, client configclient.ConfigV1Interface, issuer string) {
auth, err := client.Authentications().Get(context.TODO(), "cluster", metav1.GetOptions{})
require.NoError(t, err)
auth.Spec.ServiceAccountIssuer = issuer
_, err = client.Authentications().Update(context.TODO(), auth, metav1.UpdateOptions{})
require.NoError(t, err)
}