|
| 1 | +/* |
| 2 | +Copyright 2021 The cert-manager Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package util |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | + |
| 23 | + "k8s.io/client-go/rest" |
| 24 | + |
| 25 | + "github.com/cert-manager/csi-lib/metadata" |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | +) |
| 28 | + |
| 29 | +func Test_restConfigForMetadataTokenRequestEmptyAud(t *testing.T) { |
| 30 | + var ( |
| 31 | + baseRestConfig = &rest.Config{ |
| 32 | + Host: "my-host", |
| 33 | + TLSClientConfig: rest.TLSClientConfig{ |
| 34 | + ServerName: "my-server", |
| 35 | + }, |
| 36 | + BearerToken: "my-token", |
| 37 | + UserAgent: "csi.cert-manager.io/unit-tests", |
| 38 | + Timeout: time.Millisecond, |
| 39 | + } |
| 40 | + ) |
| 41 | + |
| 42 | + tests := map[string]struct { |
| 43 | + volumeContext map[string]string |
| 44 | + expRestConfig *rest.Config |
| 45 | + expErr bool |
| 46 | + }{ |
| 47 | + "volume context doesn't contain any token requests should error": { |
| 48 | + volumeContext: map[string]string{}, |
| 49 | + expRestConfig: nil, |
| 50 | + expErr: true, |
| 51 | + }, |
| 52 | + "volume context contains token request entry but json is garbage should error": { |
| 53 | + volumeContext: map[string]string{ |
| 54 | + "csi.storage.k8s.io/serviceAccount.tokens": "garbage-data", |
| 55 | + }, |
| 56 | + expRestConfig: nil, |
| 57 | + expErr: true, |
| 58 | + }, |
| 59 | + "volume context contains token requests, but not an empty audience should error": { |
| 60 | + volumeContext: map[string]string{ |
| 61 | + "csi.storage.k8s.io/serviceAccount.tokens": ` |
| 62 | + { |
| 63 | + "vault": { |
| 64 | + "token": "vault-token", |
| 65 | + "expiry": "Wed, 11 Aug 2021 09:03:03 GMT" |
| 66 | + }, |
| 67 | + "kubernetes.io": { |
| 68 | + "token": "kube-token", |
| 69 | + "expiry": "Wed, 11 Aug 2021 09:03:03 GMT" |
| 70 | + } |
| 71 | + } |
| 72 | + `, |
| 73 | + }, |
| 74 | + expRestConfig: nil, |
| 75 | + expErr: true, |
| 76 | + }, |
| 77 | + "volume context contains only an empty audience token should return a rest config with token": { |
| 78 | + volumeContext: map[string]string{ |
| 79 | + "csi.storage.k8s.io/serviceAccount.tokens": ` |
| 80 | + { |
| 81 | + "": { |
| 82 | + "token": "empty-aud-token", |
| 83 | + "expiry": "Wed, 11 Aug 2021 09:03:03 GMT" |
| 84 | + } |
| 85 | + } |
| 86 | + `, |
| 87 | + }, |
| 88 | + expRestConfig: &rest.Config{ |
| 89 | + Host: "my-host", |
| 90 | + TLSClientConfig: rest.TLSClientConfig{ |
| 91 | + ServerName: "my-server", |
| 92 | + }, |
| 93 | + UserAgent: "csi.cert-manager.io/unit-tests", |
| 94 | + Timeout: time.Millisecond, |
| 95 | + BearerToken: "empty-aud-token", |
| 96 | + }, |
| 97 | + expErr: false, |
| 98 | + }, |
| 99 | + "volume context contains multiple request tokens including the empty audience should return a rest config with empty audience token": { |
| 100 | + volumeContext: map[string]string{ |
| 101 | + "csi.storage.k8s.io/serviceAccount.tokens": ` |
| 102 | + { |
| 103 | + "vault": { |
| 104 | + "token": "vault-token", |
| 105 | + "expiry": "Wed, 11 Aug 2021 09:03:03 GMT" |
| 106 | + }, |
| 107 | + "": { |
| 108 | + "token": "another-empty-aud-token", |
| 109 | + "expiry": "Wed, 11 Aug 2021 09:03:03 GMT" |
| 110 | + }, |
| 111 | + "kubernetes.io": { |
| 112 | + "token": "kube-token", |
| 113 | + "expiry": "Wed, 11 Aug 2021 09:03:03 GMT" |
| 114 | + } |
| 115 | + } |
| 116 | + `, |
| 117 | + }, |
| 118 | + expRestConfig: &rest.Config{ |
| 119 | + Host: "my-host", |
| 120 | + TLSClientConfig: rest.TLSClientConfig{ |
| 121 | + ServerName: "my-server", |
| 122 | + }, |
| 123 | + UserAgent: "csi.cert-manager.io/unit-tests", |
| 124 | + Timeout: time.Millisecond, |
| 125 | + BearerToken: "another-empty-aud-token", |
| 126 | + }, |
| 127 | + expErr: false, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + for name, test := range tests { |
| 132 | + t.Run(name, func(t *testing.T) { |
| 133 | + restConfig, gotErr := restConfigForMetadataTokenRequestEmptyAud(baseRestConfig)(metadata.Metadata{VolumeContext: test.volumeContext}) |
| 134 | + assert.Equal(t, test.expErr, gotErr != nil, "%v", gotErr) |
| 135 | + assert.Equal(t, test.expRestConfig, restConfig) |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
0 commit comments