| 1 |
/* |
| 2 |
Copyright 2017 The Kubernetes 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 encryptionconfig |
| 18 |
|
| 19 |
import ( |
| 20 |
"bytes" |
| 21 |
"context" |
| 22 |
"encoding/base64" |
| 23 |
"errors" |
| 24 |
"fmt" |
| 25 |
"io" |
| 26 |
"reflect" |
| 27 |
"strings" |
| 28 |
"sync" |
| 29 |
"testing" |
| 30 |
"time" |
| 31 |
|
| 32 |
"github.com/google/go-cmp/cmp" |
| 33 |
|
| 34 |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 35 |
"k8s.io/apimachinery/pkg/runtime/schema" |
| 36 |
"k8s.io/apimachinery/pkg/util/sets" |
| 37 |
"k8s.io/apiserver/pkg/apis/apiserver" |
| 38 |
"k8s.io/apiserver/pkg/features" |
| 39 |
"k8s.io/apiserver/pkg/storage/value" |
| 40 |
"k8s.io/apiserver/pkg/storage/value/encrypt/envelope" |
| 41 |
envelopekmsv2 "k8s.io/apiserver/pkg/storage/value/encrypt/envelope/kmsv2" |
| 42 |
kmstypes "k8s.io/apiserver/pkg/storage/value/encrypt/envelope/kmsv2/v2" |
| 43 |
"k8s.io/apiserver/pkg/storage/value/encrypt/envelope/metrics" |
| 44 |
utilfeature "k8s.io/apiserver/pkg/util/feature" |
| 45 |
featuregatetesting "k8s.io/component-base/featuregate/testing" |
| 46 |
"k8s.io/component-base/metrics/legacyregistry" |
| 47 |
"k8s.io/component-base/metrics/testutil" |
| 48 |
"k8s.io/klog/v2" |
| 49 |
kmsservice "k8s.io/kms/pkg/service" |
| 50 |
"k8s.io/utils/pointer" |
| 51 |
) |
| 52 |
|
| 53 |
const ( |
| 54 |
sampleText = "abcdefghijklmnopqrstuvwxyz" |
| 55 |
sampleContextText = "0123456789" |
| 56 |
) |
| 57 |
|
| 58 |
var ( |
| 59 |
sampleInvalidKeyID = string(make([]byte, envelopekmsv2.KeyIDMaxSize+1)) |
| 60 |
) |
| 61 |
|
| 62 |
// testEnvelopeService is a mock envelope service which can be used to simulate remote Envelope services |
| 63 |
// for testing of the envelope transformer with other transformers. |
| 64 |
type testEnvelopeService struct { |
| 65 |
err error |
| 66 |
} |
| 67 |
|
| 68 |
func (t *testEnvelopeService) Decrypt(data []byte) ([]byte, error) { |
| 69 |
if t.err != nil { |
| 70 |
return nil, t.err |
| 71 |
} |
| 72 |
return base64.StdEncoding.DecodeString(string(data)) |
| 73 |
} |
| 74 |
|
| 75 |
func (t *testEnvelopeService) Encrypt(data []byte) ([]byte, error) { |
| 76 |
if t.err != nil { |
| 77 |
return nil, t.err |
| 78 |
} |
| 79 |
return []byte(base64.StdEncoding.EncodeToString(data)), nil |
| 80 |
} |
| 81 |
|
| 82 |
// testKMSv2EnvelopeService is a mock kmsv2 envelope service which can be used to simulate remote Envelope v2 services |
| 83 |
// for testing of the envelope transformer with other transformers. |
| 84 |
type testKMSv2EnvelopeService struct { |
| 85 |
err error |
| 86 |
keyID string |
| 87 |
encryptCalls int |
| 88 |
encryptAnnotations map[string][]byte |
| 89 |
} |
| 90 |
|
| 91 |
func (t *testKMSv2EnvelopeService) Decrypt(ctx context.Context, uid string, req *kmsservice.DecryptRequest) ([]byte, error) { |
| 92 |
if t.err != nil { |
| 93 |
return nil, t.err |
| 94 |
} |
| 95 |
return base64.StdEncoding.DecodeString(string(req.Ciphertext)) |
| 96 |
} |
| 97 |
|
| 98 |
func (t *testKMSv2EnvelopeService) Encrypt(ctx context.Context, uid string, data []byte) (*kmsservice.EncryptResponse, error) { |
| 99 |
t.encryptCalls++ |
| 100 |
if t.err != nil { |
| 101 |
return nil, t.err |
| 102 |
} |
| 103 |
return &kmsservice.EncryptResponse{ |
| 104 |
Ciphertext: []byte(base64.StdEncoding.EncodeToString(data)), |
| 105 |
KeyID: t.keyID, |
| 106 |
Annotations: t.encryptAnnotations, |
| 107 |
}, nil |
| 108 |
} |
| 109 |
|
| 110 |
func (t *testKMSv2EnvelopeService) Status(ctx context.Context) (*kmsservice.StatusResponse, error) { |
| 111 |
if t.err != nil { |
| 112 |
return nil, t.err |
| 113 |
} |
| 114 |
return &kmsservice.StatusResponse{Healthz: "ok", KeyID: t.keyID, Version: "v2beta1"}, nil |
| 115 |
} |
| 116 |
|
| 117 |
// The factory method to create mock envelope service. |
| 118 |
func newMockEnvelopeService(ctx context.Context, endpoint string, timeout time.Duration) (envelope.Service, error) { |
| 119 |
return &testEnvelopeService{nil}, nil |
| 120 |
} |
| 121 |
|
| 122 |
// The factory method to create mock envelope service which always returns error. |
| 123 |
func newMockErrorEnvelopeService(endpoint string, timeout time.Duration) (envelope.Service, error) { |
| 124 |
return &testEnvelopeService{errors.New("test")}, nil |
| 125 |
} |
| 126 |
|
| 127 |
// The factory method to create mock envelope kmsv2 service. |
| 128 |
func newMockEnvelopeKMSv2Service(ctx context.Context, endpoint, providerName string, timeout time.Duration) (kmsservice.Service, error) { |
| 129 |
return &testKMSv2EnvelopeService{nil, "1", 0, nil}, nil |
| 130 |
} |
| 131 |
|
| 132 |
// The factory method to create mock envelope kmsv2 service which always returns error. |
| 133 |
func newMockErrorEnvelopeKMSv2Service(endpoint string, timeout time.Duration) (kmsservice.Service, error) { |
| 134 |
return &testKMSv2EnvelopeService{errors.New("test"), "1", 0, nil}, nil |
| 135 |
} |
| 136 |
|
| 137 |
// The factory method to create mock envelope kmsv2 service that always returns invalid keyID. |
| 138 |
func newMockInvalidKeyIDEnvelopeKMSv2Service(ctx context.Context, endpoint string, timeout time.Duration, keyID string) (kmsservice.Service, error) { |
| 139 |
return &testKMSv2EnvelopeService{nil, keyID, 0, nil}, nil |
| 140 |
} |
| 141 |
|
| 142 |
func TestLegacyConfig(t *testing.T) { |
| 143 |
legacyV1Config := "testdata/valid-configs/legacy.yaml" |
| 144 |
legacyConfigObject, _, err := loadConfig(legacyV1Config, false) |
| 145 |
cacheSize := int32(10) |
| 146 |
if err != nil { |
| 147 |
t.Fatalf("error while parsing configuration file: %s.\nThe file was:\n%s", err, legacyV1Config) |
| 148 |
} |
| 149 |
|
| 150 |
expected := &apiserver.EncryptionConfiguration{ |
| 151 |
Resources: []apiserver.ResourceConfiguration{ |
| 152 |
{ |
| 153 |
Resources: []string{"secrets", "namespaces"}, |
| 154 |
Providers: []apiserver.ProviderConfiguration{ |
| 155 |
{Identity: &apiserver.IdentityConfiguration{}}, |
| 156 |
{AESGCM: &apiserver.AESConfiguration{ |
| 157 |
Keys: []apiserver.Key{ |
| 158 |
{Name: "key1", Secret: "c2VjcmV0IGlzIHNlY3VyZQ=="}, |
| 159 |
{Name: "key2", Secret: "dGhpcyBpcyBwYXNzd29yZA=="}, |
| 160 |
}, |
| 161 |
}}, |
| 162 |
{KMS: &apiserver.KMSConfiguration{ |
| 163 |
APIVersion: "v1", |
| 164 |
Name: "testprovider", |
| 165 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 166 |
CacheSize: &cacheSize, |
| 167 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 168 |
}}, |
| 169 |
{AESCBC: &apiserver.AESConfiguration{ |
| 170 |
Keys: []apiserver.Key{ |
| 171 |
{Name: "key1", Secret: "c2VjcmV0IGlzIHNlY3VyZQ=="}, |
| 172 |
{Name: "key2", Secret: "dGhpcyBpcyBwYXNzd29yZA=="}, |
| 173 |
}, |
| 174 |
}}, |
| 175 |
{Secretbox: &apiserver.SecretboxConfiguration{ |
| 176 |
Keys: []apiserver.Key{ |
| 177 |
{Name: "key1", Secret: "YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoxMjM0NTY="}, |
| 178 |
}, |
| 179 |
}}, |
| 180 |
}, |
| 181 |
}, |
| 182 |
}, |
| 183 |
} |
| 184 |
if d := cmp.Diff(expected, legacyConfigObject); d != "" { |
| 185 |
t.Fatalf("EncryptionConfig mismatch (-want +got):\n%s", d) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func TestEncryptionProviderConfigCorrect(t *testing.T) { |
| 190 |
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KMSv1, true) |
| 191 |
|
| 192 |
// Set factory for mock envelope service |
| 193 |
factory := envelopeServiceFactory |
| 194 |
factoryKMSv2 := EnvelopeKMSv2ServiceFactory |
| 195 |
envelopeServiceFactory = newMockEnvelopeService |
| 196 |
EnvelopeKMSv2ServiceFactory = newMockEnvelopeKMSv2Service |
| 197 |
defer func() { |
| 198 |
envelopeServiceFactory = factory |
| 199 |
EnvelopeKMSv2ServiceFactory = factoryKMSv2 |
| 200 |
}() |
| 201 |
|
| 202 |
ctx := testContext(t) |
| 203 |
|
| 204 |
// Creates compound/prefix transformers with different ordering of available transformers. |
| 205 |
// Transforms data using one of them, and tries to untransform using the others. |
| 206 |
// Repeats this for all possible combinations. |
| 207 |
// Math for GracePeriod is explained at - https://github.com/kubernetes/kubernetes/blob/c9ed04762f94a319d7b1fb718dc345491a32bea6/staging/src/k8s.io/apiserver/pkg/server/options/encryptionconfig/config.go#L159-L163 |
| 208 |
expectedKMSCloseGracePeriod := 46 * time.Second |
| 209 |
correctConfigWithIdentityFirst := "testdata/valid-configs/identity-first.yaml" |
| 210 |
identityFirstEncryptionConfiguration, err := LoadEncryptionConfig(ctx, correctConfigWithIdentityFirst, false, "") |
| 211 |
if err != nil { |
| 212 |
t.Fatalf("error while parsing configuration file: %s.\nThe file was:\n%s", err, correctConfigWithIdentityFirst) |
| 213 |
} |
| 214 |
if identityFirstEncryptionConfiguration.KMSCloseGracePeriod != expectedKMSCloseGracePeriod { |
| 215 |
t.Fatalf("KMSCloseGracePeriod mismatch (-want +got):\n%s", cmp.Diff(expectedKMSCloseGracePeriod, identityFirstEncryptionConfiguration.KMSCloseGracePeriod)) |
| 216 |
} |
| 217 |
|
| 218 |
// Math for GracePeriod is explained at - https://github.com/kubernetes/kubernetes/blob/c9ed04762f94a319d7b1fb718dc345491a32bea6/staging/src/k8s.io/apiserver/pkg/server/options/encryptionconfig/config.go#L159-L163 |
| 219 |
expectedKMSCloseGracePeriod = 32 * time.Second |
| 220 |
correctConfigWithAesGcmFirst := "testdata/valid-configs/aes-gcm-first.yaml" |
| 221 |
aesGcmFirstEncryptionConfiguration, err := LoadEncryptionConfig(ctx, correctConfigWithAesGcmFirst, false, "") |
| 222 |
if err != nil { |
| 223 |
t.Fatalf("error while parsing configuration file: %s.\nThe file was:\n%s", err, correctConfigWithAesGcmFirst) |
| 224 |
} |
| 225 |
if aesGcmFirstEncryptionConfiguration.KMSCloseGracePeriod != expectedKMSCloseGracePeriod { |
| 226 |
t.Fatalf("KMSCloseGracePeriod mismatch (-want +got):\n%s", cmp.Diff(expectedKMSCloseGracePeriod, aesGcmFirstEncryptionConfiguration.KMSCloseGracePeriod)) |
| 227 |
} |
| 228 |
|
| 229 |
invalidConfigWithAesGcm := "testdata/invalid-configs/invalid-aes-gcm.yaml" |
| 230 |
_, err = LoadEncryptionConfig(ctx, invalidConfigWithAesGcm, false, "") |
| 231 |
if !strings.Contains(errString(err), "error while parsing file") { |
| 232 |
t.Fatalf("should result in error while parsing configuration file: %s.\nThe file was:\n%s", err, invalidConfigWithAesGcm) |
| 233 |
} |
| 234 |
|
| 235 |
invalidConfigWithTypo := "testdata/invalid-configs/invalid-typo.yaml" |
| 236 |
_, err = LoadEncryptionConfig(ctx, invalidConfigWithTypo, false, "") |
| 237 |
if got, wantSubString := errString(err), `strict decoding error: unknown field "resources[0].providers[3].kms.pandas"`; !strings.Contains(got, wantSubString) { |
| 238 |
t.Fatalf("should result in strict decode error while parsing configuration file %q:\ngot: %q\nwant substring: %q", invalidConfigWithTypo, got, wantSubString) |
| 239 |
} |
| 240 |
|
| 241 |
// Math for GracePeriod is explained at - https://github.com/kubernetes/kubernetes/blob/c9ed04762f94a319d7b1fb718dc345491a32bea6/staging/src/k8s.io/apiserver/pkg/server/options/encryptionconfig/config.go#L159-L163 |
| 242 |
expectedKMSCloseGracePeriod = 26 * time.Second |
| 243 |
correctConfigWithAesCbcFirst := "testdata/valid-configs/aes-cbc-first.yaml" |
| 244 |
aesCbcFirstEncryptionConfiguration, err := LoadEncryptionConfig(ctx, correctConfigWithAesCbcFirst, false, "") |
| 245 |
if err != nil { |
| 246 |
t.Fatalf("error while parsing configuration file: %s.\nThe file was:\n%s", err, correctConfigWithAesCbcFirst) |
| 247 |
} |
| 248 |
if aesCbcFirstEncryptionConfiguration.KMSCloseGracePeriod != expectedKMSCloseGracePeriod { |
| 249 |
t.Fatalf("KMSCloseGracePeriod mismatch (-want +got):\n%s", cmp.Diff(expectedKMSCloseGracePeriod, aesCbcFirstEncryptionConfiguration.KMSCloseGracePeriod)) |
| 250 |
} |
| 251 |
|
| 252 |
// Math for GracePeriod is explained at - https://github.com/kubernetes/kubernetes/blob/c9ed04762f94a319d7b1fb718dc345491a32bea6/staging/src/k8s.io/apiserver/pkg/server/options/encryptionconfig/config.go#L159-L163 |
| 253 |
expectedKMSCloseGracePeriod = 14 * time.Second |
| 254 |
correctConfigWithSecretboxFirst := "testdata/valid-configs/secret-box-first.yaml" |
| 255 |
secretboxFirstEncryptionConfiguration, err := LoadEncryptionConfig(ctx, correctConfigWithSecretboxFirst, false, "") |
| 256 |
if err != nil { |
| 257 |
t.Fatalf("error while parsing configuration file: %s.\nThe file was:\n%s", err, correctConfigWithSecretboxFirst) |
| 258 |
} |
| 259 |
if secretboxFirstEncryptionConfiguration.KMSCloseGracePeriod != expectedKMSCloseGracePeriod { |
| 260 |
t.Fatalf("KMSCloseGracePeriod mismatch (-want +got):\n%s", cmp.Diff(expectedKMSCloseGracePeriod, secretboxFirstEncryptionConfiguration.KMSCloseGracePeriod)) |
| 261 |
} |
| 262 |
|
| 263 |
// Math for GracePeriod is explained at - https://github.com/kubernetes/kubernetes/blob/c9ed04762f94a319d7b1fb718dc345491a32bea6/staging/src/k8s.io/apiserver/pkg/server/options/encryptionconfig/config.go#L159-L163 |
| 264 |
expectedKMSCloseGracePeriod = 34 * time.Second |
| 265 |
correctConfigWithKMSFirst := "testdata/valid-configs/kms-first.yaml" |
| 266 |
kmsFirstEncryptionConfiguration, err := LoadEncryptionConfig(ctx, correctConfigWithKMSFirst, false, "") |
| 267 |
if err != nil { |
| 268 |
t.Fatalf("error while parsing configuration file: %s.\nThe file was:\n%s", err, correctConfigWithKMSFirst) |
| 269 |
} |
| 270 |
if kmsFirstEncryptionConfiguration.KMSCloseGracePeriod != expectedKMSCloseGracePeriod { |
| 271 |
t.Fatalf("KMSCloseGracePeriod mismatch (-want +got):\n%s", cmp.Diff(expectedKMSCloseGracePeriod, kmsFirstEncryptionConfiguration.KMSCloseGracePeriod)) |
| 272 |
} |
| 273 |
|
| 274 |
// Math for GracePeriod is explained at - https://github.com/kubernetes/kubernetes/blob/c9ed04762f94a319d7b1fb718dc345491a32bea6/staging/src/k8s.io/apiserver/pkg/server/options/encryptionconfig/config.go#L159-L163 |
| 275 |
expectedKMSCloseGracePeriod = 42 * time.Second |
| 276 |
correctConfigWithKMSv2First := "testdata/valid-configs/kmsv2-first.yaml" |
| 277 |
kmsv2FirstEncryptionConfiguration, err := LoadEncryptionConfig(ctx, correctConfigWithKMSv2First, false, "") |
| 278 |
if err != nil { |
| 279 |
t.Fatalf("error while parsing configuration file: %s.\nThe file was:\n%s", err, correctConfigWithKMSv2First) |
| 280 |
} |
| 281 |
if kmsv2FirstEncryptionConfiguration.KMSCloseGracePeriod != expectedKMSCloseGracePeriod { |
| 282 |
t.Fatalf("KMSCloseGracePeriod mismatch (-want +got):\n%s", cmp.Diff(expectedKMSCloseGracePeriod, kmsv2FirstEncryptionConfiguration.KMSCloseGracePeriod)) |
| 283 |
} |
| 284 |
|
| 285 |
// Pick the transformer for any of the returned resources. |
| 286 |
identityFirstTransformer := identityFirstEncryptionConfiguration.Transformers[schema.ParseGroupResource("secrets")] |
| 287 |
aesGcmFirstTransformer := aesGcmFirstEncryptionConfiguration.Transformers[schema.ParseGroupResource("secrets")] |
| 288 |
aesCbcFirstTransformer := aesCbcFirstEncryptionConfiguration.Transformers[schema.ParseGroupResource("secrets")] |
| 289 |
secretboxFirstTransformer := secretboxFirstEncryptionConfiguration.Transformers[schema.ParseGroupResource("secrets")] |
| 290 |
kmsFirstTransformer := kmsFirstEncryptionConfiguration.Transformers[schema.ParseGroupResource("secrets")] |
| 291 |
kmsv2FirstTransformer := kmsv2FirstEncryptionConfiguration.Transformers[schema.ParseGroupResource("secrets")] |
| 292 |
|
| 293 |
dataCtx := value.DefaultContext(sampleContextText) |
| 294 |
originalText := []byte(sampleText) |
| 295 |
|
| 296 |
transformers := []struct { |
| 297 |
Transformer value.Transformer |
| 298 |
Name string |
| 299 |
}{ |
| 300 |
{aesGcmFirstTransformer, "aesGcmFirst"}, |
| 301 |
{aesCbcFirstTransformer, "aesCbcFirst"}, |
| 302 |
{secretboxFirstTransformer, "secretboxFirst"}, |
| 303 |
{identityFirstTransformer, "identityFirst"}, |
| 304 |
{kmsFirstTransformer, "kmsFirst"}, |
| 305 |
{kmsv2FirstTransformer, "kmvs2First"}, |
| 306 |
} |
| 307 |
|
| 308 |
for _, testCase := range transformers { |
| 309 |
transformedData, err := testCase.Transformer.TransformToStorage(ctx, originalText, dataCtx) |
| 310 |
if err != nil { |
| 311 |
t.Fatalf("%s: error while transforming data to storage: %s", testCase.Name, err) |
| 312 |
} |
| 313 |
|
| 314 |
for _, transformer := range transformers { |
| 315 |
untransformedData, stale, err := transformer.Transformer.TransformFromStorage(ctx, transformedData, dataCtx) |
| 316 |
if err != nil { |
| 317 |
t.Fatalf("%s: error while reading using %s transformer: %s", testCase.Name, transformer.Name, err) |
| 318 |
} |
| 319 |
if stale != (transformer.Name != testCase.Name) { |
| 320 |
t.Fatalf("%s: wrong stale information on reading using %s transformer, should be %v", testCase.Name, transformer.Name, testCase.Name == transformer.Name) |
| 321 |
} |
| 322 |
if !bytes.Equal(untransformedData, originalText) { |
| 323 |
t.Fatalf("%s: %s transformer transformed data incorrectly. Expected: %v, got %v", testCase.Name, transformer.Name, originalText, untransformedData) |
| 324 |
} |
| 325 |
} |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
func TestKMSv1Deprecation(t *testing.T) { |
| 330 |
testCases := []struct { |
| 331 |
name string |
| 332 |
kmsv1Enabled bool |
| 333 |
expectedErr string |
| 334 |
}{ |
| 335 |
{ |
| 336 |
name: "config with kmsv1, KMSv1=false", |
| 337 |
kmsv1Enabled: false, |
| 338 |
expectedErr: "KMSv1 is deprecated and will only receive security updates going forward. Use KMSv2 instead. Set --feature-gates=KMSv1=true to use the deprecated KMSv1 feature.", |
| 339 |
}, |
| 340 |
{ |
| 341 |
name: "config with kmsv1, KMSv1=true", |
| 342 |
kmsv1Enabled: true, |
| 343 |
expectedErr: "", |
| 344 |
}, |
| 345 |
} |
| 346 |
|
| 347 |
for _, testCase := range testCases { |
| 348 |
t.Run(testCase.name, func(t *testing.T) { |
| 349 |
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KMSv1, testCase.kmsv1Enabled) |
| 350 |
|
| 351 |
kmsv1Config := "testdata/valid-configs/kms/multiple-providers.yaml" |
| 352 |
_, err := LoadEncryptionConfig(testContext(t), kmsv1Config, false, "") |
| 353 |
if !strings.Contains(errString(err), testCase.expectedErr) { |
| 354 |
t.Fatalf("expected error %q, got %q", testCase.expectedErr, errString(err)) |
| 355 |
} |
| 356 |
}) |
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
func TestKMSvsEnablement(t *testing.T) { |
| 361 |
testCases := []struct { |
| 362 |
name string |
| 363 |
filePath string |
| 364 |
expectedErr string |
| 365 |
}{ |
| 366 |
{ |
| 367 |
name: "config with kmsv2 and kmsv1, KMSv2=true, KMSv1=false, should fail when feature is disabled", |
| 368 |
filePath: "testdata/valid-configs/kms/multiple-providers-mixed.yaml", |
| 369 |
expectedErr: "KMSv1 is deprecated and will only receive security updates going forward. Use KMSv2 instead", |
| 370 |
}, |
| 371 |
{ |
| 372 |
name: "config with kmsv2, KMSv2=true, KMSv1=false", |
| 373 |
filePath: "testdata/valid-configs/kms/multiple-providers-kmsv2.yaml", |
| 374 |
expectedErr: "", |
| 375 |
}, |
| 376 |
} |
| 377 |
|
| 378 |
for _, testCase := range testCases { |
| 379 |
t.Run(testCase.name, func(t *testing.T) { |
| 380 |
// only the KMSv2 feature flag is enabled |
| 381 |
_, err := LoadEncryptionConfig(testContext(t), testCase.filePath, false, "") |
| 382 |
|
| 383 |
if len(testCase.expectedErr) > 0 && !strings.Contains(errString(err), testCase.expectedErr) { |
| 384 |
t.Fatalf("expected error %q, got %q", testCase.expectedErr, errString(err)) |
| 385 |
} |
| 386 |
if len(testCase.expectedErr) == 0 && err != nil { |
| 387 |
t.Fatalf("unexpected error %q", errString(err)) |
| 388 |
} |
| 389 |
|
| 390 |
}) |
| 391 |
} |
| 392 |
tts := []struct { |
| 393 |
name string |
| 394 |
expectedErr string |
| 395 |
expectedTimeout time.Duration |
| 396 |
config apiserver.EncryptionConfiguration |
| 397 |
wantV2Used bool |
| 398 |
}{ |
| 399 |
{ |
| 400 |
name: "with kmsv1 and kmsv2, KMSv2=true", |
| 401 |
config: apiserver.EncryptionConfiguration{ |
| 402 |
Resources: []apiserver.ResourceConfiguration{ |
| 403 |
{ |
| 404 |
Resources: []string{"secrets"}, |
| 405 |
Providers: []apiserver.ProviderConfiguration{ |
| 406 |
{ |
| 407 |
KMS: &apiserver.KMSConfiguration{ |
| 408 |
Name: "kms", |
| 409 |
APIVersion: "v1", |
| 410 |
Timeout: &metav1.Duration{ |
| 411 |
Duration: 1 * time.Second, |
| 412 |
}, |
| 413 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 414 |
CacheSize: pointer.Int32(1000), |
| 415 |
}, |
| 416 |
}, |
| 417 |
{ |
| 418 |
KMS: &apiserver.KMSConfiguration{ |
| 419 |
Name: "another-kms", |
| 420 |
APIVersion: "v2", |
| 421 |
Timeout: &metav1.Duration{ |
| 422 |
Duration: 1 * time.Second, |
| 423 |
}, |
| 424 |
Endpoint: "unix:///tmp/anothertestprovider.sock", |
| 425 |
CacheSize: pointer.Int32(1000), |
| 426 |
}, |
| 427 |
}, |
| 428 |
}, |
| 429 |
}, |
| 430 |
}, |
| 431 |
}, |
| 432 |
expectedErr: "", |
| 433 |
wantV2Used: true, |
| 434 |
}, |
| 435 |
} |
| 436 |
|
| 437 |
for _, tt := range tts { |
| 438 |
t.Run(tt.name, func(t *testing.T) { |
| 439 |
// Just testing KMSv2 feature flag |
| 440 |
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KMSv1, true) |
| 441 |
|
| 442 |
ctx, cancel := context.WithCancel(context.Background()) |
| 443 |
cancel() // cancel this upfront so the kms v2 checks do not block |
| 444 |
|
| 445 |
_, _, kmsUsed, err := getTransformerOverridesAndKMSPluginHealthzCheckers(ctx, &tt.config, "") |
| 446 |
if err == nil { |
| 447 |
if kmsUsed == nil || kmsUsed.v2Used != tt.wantV2Used { |
| 448 |
t.Fatalf("unexpected kmsUsed value, expected: %v, got: %v", tt.wantV2Used, kmsUsed) |
| 449 |
} |
| 450 |
|
| 451 |
} |
| 452 |
if !strings.Contains(errString(err), tt.expectedErr) { |
| 453 |
t.Fatalf("expecting error calling prefixTransformersAndProbes, expected: %s, got: %s", tt.expectedErr, errString(err)) |
| 454 |
} |
| 455 |
}) |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
func TestKMSMaxTimeout(t *testing.T) { |
| 460 |
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KMSv1, true) |
| 461 |
|
| 462 |
testCases := []struct { |
| 463 |
name string |
| 464 |
expectedErr string |
| 465 |
expectedTimeout time.Duration |
| 466 |
config apiserver.EncryptionConfiguration |
| 467 |
}{ |
| 468 |
{ |
| 469 |
name: "config with bad provider", |
| 470 |
config: apiserver.EncryptionConfiguration{ |
| 471 |
Resources: []apiserver.ResourceConfiguration{ |
| 472 |
{ |
| 473 |
Resources: []string{"secrets"}, |
| 474 |
Providers: []apiserver.ProviderConfiguration{ |
| 475 |
{ |
| 476 |
KMS: nil, |
| 477 |
}, |
| 478 |
}, |
| 479 |
}, |
| 480 |
}, |
| 481 |
}, |
| 482 |
expectedErr: "provider does not contain any of the expected providers: KMS, AESGCM, AESCBC, Secretbox, Identity", |
| 483 |
expectedTimeout: 6 * time.Second, |
| 484 |
}, |
| 485 |
{ |
| 486 |
name: "default timeout", |
| 487 |
config: apiserver.EncryptionConfiguration{ |
| 488 |
Resources: []apiserver.ResourceConfiguration{ |
| 489 |
{ |
| 490 |
Resources: []string{"secrets"}, |
| 491 |
Providers: []apiserver.ProviderConfiguration{ |
| 492 |
{ |
| 493 |
KMS: &apiserver.KMSConfiguration{ |
| 494 |
Name: "kms", |
| 495 |
APIVersion: "v1", |
| 496 |
Timeout: &metav1.Duration{ |
| 497 |
// default timeout is 3s |
| 498 |
// this will be set automatically if not provided in config file |
| 499 |
Duration: 3 * time.Second, |
| 500 |
}, |
| 501 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 502 |
}, |
| 503 |
}, |
| 504 |
}, |
| 505 |
}, |
| 506 |
}, |
| 507 |
}, |
| 508 |
expectedErr: "", |
| 509 |
expectedTimeout: 6 * time.Second, |
| 510 |
}, |
| 511 |
{ |
| 512 |
name: "with v1 provider", |
| 513 |
config: apiserver.EncryptionConfiguration{ |
| 514 |
Resources: []apiserver.ResourceConfiguration{ |
| 515 |
{ |
| 516 |
Resources: []string{"secrets"}, |
| 517 |
Providers: []apiserver.ProviderConfiguration{ |
| 518 |
{ |
| 519 |
KMS: &apiserver.KMSConfiguration{ |
| 520 |
Name: "kms", |
| 521 |
APIVersion: "v1", |
| 522 |
Timeout: &metav1.Duration{ |
| 523 |
// default timeout is 3s |
| 524 |
// this will be set automatically if not provided in config file |
| 525 |
Duration: 3 * time.Second, |
| 526 |
}, |
| 527 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 528 |
}, |
| 529 |
}, |
| 530 |
}, |
| 531 |
}, |
| 532 |
{ |
| 533 |
Resources: []string{"configmaps"}, |
| 534 |
Providers: []apiserver.ProviderConfiguration{ |
| 535 |
{ |
| 536 |
KMS: &apiserver.KMSConfiguration{ |
| 537 |
Name: "kms", |
| 538 |
APIVersion: "v1", |
| 539 |
Timeout: &metav1.Duration{ |
| 540 |
// default timeout is 3s |
| 541 |
// this will be set automatically if not provided in config file |
| 542 |
Duration: 3 * time.Second, |
| 543 |
}, |
| 544 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 545 |
}, |
| 546 |
}, |
| 547 |
}, |
| 548 |
}, |
| 549 |
}, |
| 550 |
}, |
| 551 |
expectedErr: "", |
| 552 |
expectedTimeout: 12 * time.Second, |
| 553 |
}, |
| 554 |
{ |
| 555 |
name: "with v2 provider", |
| 556 |
config: apiserver.EncryptionConfiguration{ |
| 557 |
Resources: []apiserver.ResourceConfiguration{ |
| 558 |
{ |
| 559 |
Resources: []string{"secrets"}, |
| 560 |
Providers: []apiserver.ProviderConfiguration{ |
| 561 |
{ |
| 562 |
KMS: &apiserver.KMSConfiguration{ |
| 563 |
Name: "kms", |
| 564 |
APIVersion: "v2", |
| 565 |
Timeout: &metav1.Duration{ |
| 566 |
Duration: 15 * time.Second, |
| 567 |
}, |
| 568 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 569 |
}, |
| 570 |
}, |
| 571 |
{ |
| 572 |
KMS: &apiserver.KMSConfiguration{ |
| 573 |
Name: "new-kms", |
| 574 |
APIVersion: "v2", |
| 575 |
Timeout: &metav1.Duration{ |
| 576 |
Duration: 5 * time.Second, |
| 577 |
}, |
| 578 |
Endpoint: "unix:///tmp/anothertestprovider.sock", |
| 579 |
}, |
| 580 |
}, |
| 581 |
}, |
| 582 |
}, |
| 583 |
{ |
| 584 |
Resources: []string{"configmaps"}, |
| 585 |
Providers: []apiserver.ProviderConfiguration{ |
| 586 |
{ |
| 587 |
KMS: &apiserver.KMSConfiguration{ |
| 588 |
Name: "another-kms", |
| 589 |
APIVersion: "v2", |
| 590 |
Timeout: &metav1.Duration{ |
| 591 |
Duration: 10 * time.Second, |
| 592 |
}, |
| 593 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 594 |
}, |
| 595 |
}, |
| 596 |
{ |
| 597 |
KMS: &apiserver.KMSConfiguration{ |
| 598 |
Name: "yet-another-kms", |
| 599 |
APIVersion: "v2", |
| 600 |
Timeout: &metav1.Duration{ |
| 601 |
Duration: 2 * time.Second, |
| 602 |
}, |
| 603 |
Endpoint: "unix:///tmp/anothertestprovider.sock", |
| 604 |
}, |
| 605 |
}, |
| 606 |
}, |
| 607 |
}, |
| 608 |
}, |
| 609 |
}, |
| 610 |
expectedErr: "", |
| 611 |
expectedTimeout: 32 * time.Second, |
| 612 |
}, |
| 613 |
{ |
| 614 |
name: "with v1 and v2 provider", |
| 615 |
config: apiserver.EncryptionConfiguration{ |
| 616 |
Resources: []apiserver.ResourceConfiguration{ |
| 617 |
{ |
| 618 |
Resources: []string{"secrets"}, |
| 619 |
Providers: []apiserver.ProviderConfiguration{ |
| 620 |
{ |
| 621 |
KMS: &apiserver.KMSConfiguration{ |
| 622 |
Name: "kms", |
| 623 |
APIVersion: "v1", |
| 624 |
Timeout: &metav1.Duration{ |
| 625 |
Duration: 1 * time.Second, |
| 626 |
}, |
| 627 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 628 |
}, |
| 629 |
}, |
| 630 |
{ |
| 631 |
KMS: &apiserver.KMSConfiguration{ |
| 632 |
Name: "another-kms", |
| 633 |
APIVersion: "v2", |
| 634 |
Timeout: &metav1.Duration{ |
| 635 |
Duration: 1 * time.Second, |
| 636 |
}, |
| 637 |
Endpoint: "unix:///tmp/anothertestprovider.sock", |
| 638 |
}, |
| 639 |
}, |
| 640 |
}, |
| 641 |
}, |
| 642 |
{ |
| 643 |
Resources: []string{"configmaps"}, |
| 644 |
Providers: []apiserver.ProviderConfiguration{ |
| 645 |
{ |
| 646 |
KMS: &apiserver.KMSConfiguration{ |
| 647 |
Name: "kms", |
| 648 |
APIVersion: "v1", |
| 649 |
Timeout: &metav1.Duration{ |
| 650 |
Duration: 4 * time.Second, |
| 651 |
}, |
| 652 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 653 |
}, |
| 654 |
}, |
| 655 |
{ |
| 656 |
KMS: &apiserver.KMSConfiguration{ |
| 657 |
Name: "yet-another-kms", |
| 658 |
APIVersion: "v1", |
| 659 |
Timeout: &metav1.Duration{ |
| 660 |
Duration: 2 * time.Second, |
| 661 |
}, |
| 662 |
Endpoint: "unix:///tmp/anothertestprovider.sock", |
| 663 |
}, |
| 664 |
}, |
| 665 |
}, |
| 666 |
}, |
| 667 |
}, |
| 668 |
}, |
| 669 |
expectedErr: "", |
| 670 |
expectedTimeout: 15 * time.Second, |
| 671 |
}, |
| 672 |
} |
| 673 |
|
| 674 |
for _, testCase := range testCases { |
| 675 |
t.Run(testCase.name, func(t *testing.T) { |
| 676 |
cacheSize := int32(1000) |
| 677 |
for _, resource := range testCase.config.Resources { |
| 678 |
for _, provider := range resource.Providers { |
| 679 |
if provider.KMS != nil { |
| 680 |
provider.KMS.CacheSize = &cacheSize |
| 681 |
} |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
ctx, cancel := context.WithCancel(context.Background()) |
| 686 |
cancel() // cancel this upfront so the kms v2 checks do not block |
| 687 |
|
| 688 |
_, _, kmsUsed, err := getTransformerOverridesAndKMSPluginHealthzCheckers(ctx, &testCase.config, "") |
| 689 |
|
| 690 |
if !strings.Contains(errString(err), testCase.expectedErr) { |
| 691 |
t.Fatalf("expecting error calling prefixTransformersAndProbes, expected: %s, got: %s", testCase.expectedErr, errString(err)) |
| 692 |
} |
| 693 |
if len(testCase.expectedErr) == 0 { |
| 694 |
if kmsUsed == nil { |
| 695 |
t.Fatal("kmsUsed should not be nil") |
| 696 |
} |
| 697 |
|
| 698 |
if kmsUsed.kmsTimeoutSum != testCase.expectedTimeout { |
| 699 |
t.Fatalf("expected timeout %v, got %v", testCase.expectedTimeout, kmsUsed.kmsTimeoutSum) |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
}) |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
func TestKMSPluginHealthz(t *testing.T) { |
| 708 |
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KMSv1, true) |
| 709 |
|
| 710 |
kmsv2Probe := &kmsv2PluginProbe{ |
| 711 |
name: "foo", |
| 712 |
ttl: 3 * time.Second, |
| 713 |
apiServerID: "", |
| 714 |
} |
| 715 |
keyID := "1" |
| 716 |
kmsv2Probe.state.Store(&envelopekmsv2.State{EncryptedObject: kmstypes.EncryptedObject{KeyID: keyID}}) |
| 717 |
|
| 718 |
testCases := []struct { |
| 719 |
desc string |
| 720 |
config string |
| 721 |
want []healthChecker |
| 722 |
wantErr string |
| 723 |
kmsv2 bool |
| 724 |
kmsv1 bool |
| 725 |
}{ |
| 726 |
{ |
| 727 |
desc: "Invalid config file path", |
| 728 |
config: "invalid/path", |
| 729 |
want: nil, |
| 730 |
wantErr: `error reading encryption provider configuration file "invalid/path"`, |
| 731 |
}, |
| 732 |
{ |
| 733 |
desc: "Empty config file content", |
| 734 |
config: "testdata/invalid-configs/kms/invalid-content.yaml", |
| 735 |
want: nil, |
| 736 |
wantErr: `encryption provider configuration file "testdata/invalid-configs/kms/invalid-content.yaml" is empty`, |
| 737 |
}, |
| 738 |
{ |
| 739 |
desc: "Unable to decode", |
| 740 |
config: "testdata/invalid-configs/kms/invalid-gvk.yaml", |
| 741 |
want: nil, |
| 742 |
wantErr: `error decoding encryption provider configuration file`, |
| 743 |
}, |
| 744 |
{ |
| 745 |
desc: "Unexpected config type", |
| 746 |
config: "testdata/invalid-configs/kms/invalid-config-type.yaml", |
| 747 |
want: nil, |
| 748 |
wantErr: `no kind "EncryptionConfigurations" is registered for version "apiserver.config.k8s.io/v1"`, |
| 749 |
}, |
| 750 |
{ |
| 751 |
desc: "Install Healthz", |
| 752 |
config: "testdata/valid-configs/kms/default-timeout.yaml", |
| 753 |
want: []healthChecker{ |
| 754 |
&kmsPluginProbe{ |
| 755 |
name: "foo", |
| 756 |
ttl: 3 * time.Second, |
| 757 |
}, |
| 758 |
}, |
| 759 |
kmsv1: true, |
| 760 |
}, |
| 761 |
{ |
| 762 |
desc: "Install multiple healthz", |
| 763 |
config: "testdata/valid-configs/kms/multiple-providers.yaml", |
| 764 |
want: []healthChecker{ |
| 765 |
&kmsPluginProbe{ |
| 766 |
name: "foo", |
| 767 |
ttl: 3 * time.Second, |
| 768 |
}, |
| 769 |
&kmsPluginProbe{ |
| 770 |
name: "bar", |
| 771 |
ttl: 3 * time.Second, |
| 772 |
}, |
| 773 |
}, |
| 774 |
kmsv1: true, |
| 775 |
}, |
| 776 |
{ |
| 777 |
desc: "No KMS Providers", |
| 778 |
config: "testdata/valid-configs/aes/aes-gcm.yaml", |
| 779 |
}, |
| 780 |
{ |
| 781 |
desc: "Install multiple healthz with v1 and v2", |
| 782 |
config: "testdata/valid-configs/kms/multiple-providers-mixed.yaml", |
| 783 |
want: []healthChecker{ |
| 784 |
kmsv2Probe, |
| 785 |
&kmsPluginProbe{ |
| 786 |
name: "bar", |
| 787 |
ttl: 3 * time.Second, |
| 788 |
}, |
| 789 |
}, |
| 790 |
kmsv2: true, |
| 791 |
kmsv1: true, |
| 792 |
}, |
| 793 |
{ |
| 794 |
desc: "Invalid API version", |
| 795 |
config: "testdata/invalid-configs/kms/invalid-apiversion.yaml", |
| 796 |
want: nil, |
| 797 |
wantErr: `resources[0].providers[0].kms.apiVersion: Invalid value: "v3": unsupported apiVersion apiVersion for KMS provider, only v1 and v2 are supported`, |
| 798 |
}, |
| 799 |
} |
| 800 |
|
| 801 |
for _, tt := range testCases { |
| 802 |
t.Run(tt.desc, func(t *testing.T) { |
| 803 |
config, _, err := loadConfig(tt.config, false) |
| 804 |
if errStr := errString(err); !strings.Contains(errStr, tt.wantErr) { |
| 805 |
t.Fatalf("unexpected error state got=%s want=%s", errStr, tt.wantErr) |
| 806 |
} |
| 807 |
if len(tt.wantErr) > 0 { |
| 808 |
return |
| 809 |
} |
| 810 |
|
| 811 |
ctx, cancel := context.WithCancel(context.Background()) |
| 812 |
cancel() // cancel this upfront so the kms v2 healthz check poll does not run |
| 813 |
_, got, kmsUsed, err := getTransformerOverridesAndKMSPluginProbes(ctx, config, "") |
| 814 |
if err != nil { |
| 815 |
t.Fatal(err) |
| 816 |
} |
| 817 |
|
| 818 |
// unset fields that are not relevant to the test |
| 819 |
for i := range got { |
| 820 |
checker := got[i] |
| 821 |
switch p := checker.(type) { |
| 822 |
case *kmsPluginProbe: |
| 823 |
p.service = nil |
| 824 |
p.l = nil |
| 825 |
p.lastResponse = nil |
| 826 |
case *kmsv2PluginProbe: |
| 827 |
p.service = nil |
| 828 |
p.l = nil |
| 829 |
p.lastResponse = nil |
| 830 |
p.state.Store(kmsv2Probe.state.Load()) |
| 831 |
default: |
| 832 |
t.Fatalf("unexpected probe type %T", p) |
| 833 |
} |
| 834 |
} |
| 835 |
|
| 836 |
if tt.kmsv2 != kmsUsed.v2Used { |
| 837 |
t.Errorf("incorrect kms v2 detection: want=%v got=%v", tt.kmsv2, kmsUsed.v2Used) |
| 838 |
} |
| 839 |
if tt.kmsv1 != kmsUsed.v1Used { |
| 840 |
t.Errorf("incorrect kms v1 detection: want=%v got=%v", tt.kmsv1, kmsUsed.v1Used) |
| 841 |
} |
| 842 |
|
| 843 |
if d := cmp.Diff(tt.want, got, |
| 844 |
cmp.Comparer(func(a, b *kmsPluginProbe) bool { |
| 845 |
return *a == *b |
| 846 |
}), |
| 847 |
cmp.Comparer(func(a, b *kmsv2PluginProbe) bool { |
| 848 |
return *a == *b |
| 849 |
}), |
| 850 |
); d != "" { |
| 851 |
t.Fatalf("HealthzConfig mismatch (-want +got):\n%s", d) |
| 852 |
} |
| 853 |
}) |
| 854 |
} |
| 855 |
} |
| 856 |
|
| 857 |
// tests for masking rules |
| 858 |
func TestWildcardMasking(t *testing.T) { |
| 859 |
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KMSv1, true) |
| 860 |
|
| 861 |
testCases := []struct { |
| 862 |
desc string |
| 863 |
config *apiserver.EncryptionConfiguration |
| 864 |
expectedError string |
| 865 |
}{ |
| 866 |
{ |
| 867 |
desc: "resources masked by *. group", |
| 868 |
config: &apiserver.EncryptionConfiguration{ |
| 869 |
Resources: []apiserver.ResourceConfiguration{ |
| 870 |
{ |
| 871 |
Resources: []string{ |
| 872 |
"configmaps", |
| 873 |
"*.", |
| 874 |
"secrets", |
| 875 |
}, |
| 876 |
Providers: []apiserver.ProviderConfiguration{ |
| 877 |
{ |
| 878 |
KMS: &apiserver.KMSConfiguration{ |
| 879 |
Name: "kms", |
| 880 |
APIVersion: "v1", |
| 881 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 882 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 883 |
CacheSize: pointer.Int32(10), |
| 884 |
}, |
| 885 |
}, |
| 886 |
}, |
| 887 |
}, |
| 888 |
}, |
| 889 |
}, |
| 890 |
expectedError: "resource \"secrets\" is masked by earlier rule \"*.\"", |
| 891 |
}, |
| 892 |
{ |
| 893 |
desc: "*. masked by *. group", |
| 894 |
config: &apiserver.EncryptionConfiguration{ |
| 895 |
Resources: []apiserver.ResourceConfiguration{ |
| 896 |
{ |
| 897 |
Resources: []string{ |
| 898 |
"*.", |
| 899 |
}, |
| 900 |
Providers: []apiserver.ProviderConfiguration{ |
| 901 |
{ |
| 902 |
KMS: &apiserver.KMSConfiguration{ |
| 903 |
Name: "kms", |
| 904 |
APIVersion: "v1", |
| 905 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 906 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 907 |
CacheSize: pointer.Int32(10), |
| 908 |
}, |
| 909 |
}, |
| 910 |
}, |
| 911 |
}, |
| 912 |
{ |
| 913 |
Resources: []string{ |
| 914 |
"*.", |
| 915 |
}, |
| 916 |
Providers: []apiserver.ProviderConfiguration{ |
| 917 |
{ |
| 918 |
KMS: &apiserver.KMSConfiguration{ |
| 919 |
Name: "kms2", |
| 920 |
APIVersion: "v1", |
| 921 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 922 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 923 |
CacheSize: pointer.Int32(10), |
| 924 |
}, |
| 925 |
}, |
| 926 |
}, |
| 927 |
}, |
| 928 |
}, |
| 929 |
}, |
| 930 |
expectedError: "resource \"*.\" is masked by earlier rule \"*.\"", |
| 931 |
}, |
| 932 |
{ |
| 933 |
desc: "*.foo masked by *.foo", |
| 934 |
config: &apiserver.EncryptionConfiguration{ |
| 935 |
Resources: []apiserver.ResourceConfiguration{ |
| 936 |
{ |
| 937 |
Resources: []string{ |
| 938 |
"*.foo", |
| 939 |
}, |
| 940 |
Providers: []apiserver.ProviderConfiguration{ |
| 941 |
{ |
| 942 |
KMS: &apiserver.KMSConfiguration{ |
| 943 |
Name: "kms", |
| 944 |
APIVersion: "v1", |
| 945 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 946 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 947 |
CacheSize: pointer.Int32(10), |
| 948 |
}, |
| 949 |
}, |
| 950 |
}, |
| 951 |
}, |
| 952 |
{ |
| 953 |
Resources: []string{ |
| 954 |
"*.foo", |
| 955 |
}, |
| 956 |
Providers: []apiserver.ProviderConfiguration{ |
| 957 |
{ |
| 958 |
KMS: &apiserver.KMSConfiguration{ |
| 959 |
Name: "kms2", |
| 960 |
APIVersion: "v1", |
| 961 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 962 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 963 |
CacheSize: pointer.Int32(10), |
| 964 |
}, |
| 965 |
}, |
| 966 |
}, |
| 967 |
}, |
| 968 |
}, |
| 969 |
}, |
| 970 |
expectedError: "resource \"*.foo\" is masked by earlier rule \"*.foo\"", |
| 971 |
}, |
| 972 |
{ |
| 973 |
desc: "*.* masked by *.*", |
| 974 |
config: &apiserver.EncryptionConfiguration{ |
| 975 |
Resources: []apiserver.ResourceConfiguration{ |
| 976 |
{ |
| 977 |
Resources: []string{ |
| 978 |
"*.*", |
| 979 |
}, |
| 980 |
Providers: []apiserver.ProviderConfiguration{ |
| 981 |
{ |
| 982 |
KMS: &apiserver.KMSConfiguration{ |
| 983 |
Name: "kms", |
| 984 |
APIVersion: "v1", |
| 985 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 986 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 987 |
CacheSize: pointer.Int32(10), |
| 988 |
}, |
| 989 |
}, |
| 990 |
}, |
| 991 |
}, |
| 992 |
{ |
| 993 |
Resources: []string{ |
| 994 |
"*.*", |
| 995 |
}, |
| 996 |
Providers: []apiserver.ProviderConfiguration{ |
| 997 |
{ |
| 998 |
KMS: &apiserver.KMSConfiguration{ |
| 999 |
Name: "kms2", |
| 1000 |
APIVersion: "v1", |
| 1001 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1002 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1003 |
CacheSize: pointer.Int32(10), |
| 1004 |
}, |
| 1005 |
}, |
| 1006 |
}, |
| 1007 |
}, |
| 1008 |
}, |
| 1009 |
}, |
| 1010 |
expectedError: "resource \"*.*\" is masked by earlier rule \"*.*\"", |
| 1011 |
}, |
| 1012 |
{ |
| 1013 |
desc: "resources masked by *. group in multiple configurations", |
| 1014 |
config: &apiserver.EncryptionConfiguration{ |
| 1015 |
Resources: []apiserver.ResourceConfiguration{ |
| 1016 |
{ |
| 1017 |
Resources: []string{ |
| 1018 |
"configmaps", |
| 1019 |
}, |
| 1020 |
Providers: []apiserver.ProviderConfiguration{ |
| 1021 |
{ |
| 1022 |
KMS: &apiserver.KMSConfiguration{ |
| 1023 |
Name: "kms", |
| 1024 |
APIVersion: "v1", |
| 1025 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1026 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1027 |
CacheSize: pointer.Int32(10), |
| 1028 |
}, |
| 1029 |
}, |
| 1030 |
}, |
| 1031 |
}, |
| 1032 |
{ |
| 1033 |
Resources: []string{ |
| 1034 |
"*.", |
| 1035 |
"secrets", |
| 1036 |
}, |
| 1037 |
Providers: []apiserver.ProviderConfiguration{ |
| 1038 |
{ |
| 1039 |
KMS: &apiserver.KMSConfiguration{ |
| 1040 |
Name: "another-kms", |
| 1041 |
APIVersion: "v1", |
| 1042 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1043 |
Endpoint: "unix:///tmp/another-testprovider.sock", |
| 1044 |
CacheSize: pointer.Int32(10), |
| 1045 |
}, |
| 1046 |
}, |
| 1047 |
}, |
| 1048 |
}, |
| 1049 |
}, |
| 1050 |
}, |
| 1051 |
expectedError: "resource \"secrets\" is masked by earlier rule \"*.\"", |
| 1052 |
}, |
| 1053 |
{ |
| 1054 |
desc: "resources masked by *.*", |
| 1055 |
config: &apiserver.EncryptionConfiguration{ |
| 1056 |
Resources: []apiserver.ResourceConfiguration{ |
| 1057 |
{ |
| 1058 |
Resources: []string{ |
| 1059 |
"configmaps", |
| 1060 |
"*.*", |
| 1061 |
"secrets", |
| 1062 |
}, |
| 1063 |
Providers: []apiserver.ProviderConfiguration{ |
| 1064 |
{ |
| 1065 |
KMS: &apiserver.KMSConfiguration{ |
| 1066 |
Name: "kms", |
| 1067 |
APIVersion: "v1", |
| 1068 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1069 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1070 |
CacheSize: pointer.Int32(10), |
| 1071 |
}, |
| 1072 |
}, |
| 1073 |
}, |
| 1074 |
}, |
| 1075 |
}, |
| 1076 |
}, |
| 1077 |
expectedError: "resource \"secrets\" is masked by earlier rule \"*.*\"", |
| 1078 |
}, |
| 1079 |
{ |
| 1080 |
desc: "resources masked by *.* in multiple configurations", |
| 1081 |
config: &apiserver.EncryptionConfiguration{ |
| 1082 |
Resources: []apiserver.ResourceConfiguration{ |
| 1083 |
{ |
| 1084 |
Resources: []string{ |
| 1085 |
"configmaps", |
| 1086 |
}, |
| 1087 |
Providers: []apiserver.ProviderConfiguration{ |
| 1088 |
{ |
| 1089 |
KMS: &apiserver.KMSConfiguration{ |
| 1090 |
Name: "kms", |
| 1091 |
APIVersion: "v1", |
| 1092 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1093 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1094 |
CacheSize: pointer.Int32(10), |
| 1095 |
}, |
| 1096 |
}, |
| 1097 |
}, |
| 1098 |
}, |
| 1099 |
{ |
| 1100 |
Resources: []string{ |
| 1101 |
"*.*", |
| 1102 |
"secrets", |
| 1103 |
}, |
| 1104 |
Providers: []apiserver.ProviderConfiguration{ |
| 1105 |
{ |
| 1106 |
KMS: &apiserver.KMSConfiguration{ |
| 1107 |
Name: "another-kms", |
| 1108 |
APIVersion: "v1", |
| 1109 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1110 |
Endpoint: "unix:///tmp/another-testprovider.sock", |
| 1111 |
CacheSize: pointer.Int32(10), |
| 1112 |
}, |
| 1113 |
}, |
| 1114 |
}, |
| 1115 |
}, |
| 1116 |
}, |
| 1117 |
}, |
| 1118 |
expectedError: "resource \"secrets\" is masked by earlier rule \"*.*\"", |
| 1119 |
}, |
| 1120 |
{ |
| 1121 |
desc: "resources *. masked by *.*", |
| 1122 |
config: &apiserver.EncryptionConfiguration{ |
| 1123 |
Resources: []apiserver.ResourceConfiguration{ |
| 1124 |
{ |
| 1125 |
Resources: []string{ |
| 1126 |
"configmaps", |
| 1127 |
"*.*", |
| 1128 |
"*.", |
| 1129 |
}, |
| 1130 |
Providers: []apiserver.ProviderConfiguration{ |
| 1131 |
{ |
| 1132 |
KMS: &apiserver.KMSConfiguration{ |
| 1133 |
Name: "kms", |
| 1134 |
APIVersion: "v1", |
| 1135 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1136 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1137 |
CacheSize: pointer.Int32(10), |
| 1138 |
}, |
| 1139 |
}, |
| 1140 |
}, |
| 1141 |
}, |
| 1142 |
}, |
| 1143 |
}, |
| 1144 |
expectedError: "resource \"*.\" is masked by earlier rule \"*.*\"", |
| 1145 |
}, |
| 1146 |
{ |
| 1147 |
desc: "resources *. masked by *.* in multiple configurations", |
| 1148 |
config: &apiserver.EncryptionConfiguration{ |
| 1149 |
Resources: []apiserver.ResourceConfiguration{ |
| 1150 |
{ |
| 1151 |
Resources: []string{ |
| 1152 |
"configmaps", |
| 1153 |
"*.*", |
| 1154 |
}, |
| 1155 |
Providers: []apiserver.ProviderConfiguration{ |
| 1156 |
{ |
| 1157 |
KMS: &apiserver.KMSConfiguration{ |
| 1158 |
Name: "kms", |
| 1159 |
APIVersion: "v1", |
| 1160 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1161 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1162 |
CacheSize: pointer.Int32(10), |
| 1163 |
}, |
| 1164 |
}, |
| 1165 |
}, |
| 1166 |
}, |
| 1167 |
{ |
| 1168 |
Resources: []string{ |
| 1169 |
"*.", |
| 1170 |
}, |
| 1171 |
Providers: []apiserver.ProviderConfiguration{ |
| 1172 |
{ |
| 1173 |
KMS: &apiserver.KMSConfiguration{ |
| 1174 |
Name: "another-kms", |
| 1175 |
APIVersion: "v1", |
| 1176 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1177 |
Endpoint: "unix:///tmp/another-testprovider.sock", |
| 1178 |
CacheSize: pointer.Int32(10), |
| 1179 |
}, |
| 1180 |
}, |
| 1181 |
}, |
| 1182 |
}, |
| 1183 |
}, |
| 1184 |
}, |
| 1185 |
expectedError: "resource \"*.\" is masked by earlier rule \"*.*\"", |
| 1186 |
}, |
| 1187 |
{ |
| 1188 |
desc: "resources not masked by any rule", |
| 1189 |
config: &apiserver.EncryptionConfiguration{ |
| 1190 |
Resources: []apiserver.ResourceConfiguration{ |
| 1191 |
{ |
| 1192 |
Resources: []string{ |
| 1193 |
"configmaps", |
| 1194 |
"secrets", |
| 1195 |
"*.*", |
| 1196 |
}, |
| 1197 |
Providers: []apiserver.ProviderConfiguration{ |
| 1198 |
{ |
| 1199 |
KMS: &apiserver.KMSConfiguration{ |
| 1200 |
Name: "kms", |
| 1201 |
APIVersion: "v1", |
| 1202 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1203 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1204 |
CacheSize: pointer.Int32(10), |
| 1205 |
}, |
| 1206 |
}, |
| 1207 |
}, |
| 1208 |
}, |
| 1209 |
}, |
| 1210 |
}, |
| 1211 |
}, |
| 1212 |
{ |
| 1213 |
desc: "resources not masked by any rule in multiple configurations", |
| 1214 |
config: &apiserver.EncryptionConfiguration{ |
| 1215 |
Resources: []apiserver.ResourceConfiguration{ |
| 1216 |
{ |
| 1217 |
Resources: []string{ |
| 1218 |
"configmaps", |
| 1219 |
"secrets", |
| 1220 |
}, |
| 1221 |
Providers: []apiserver.ProviderConfiguration{ |
| 1222 |
{ |
| 1223 |
KMS: &apiserver.KMSConfiguration{ |
| 1224 |
Name: "kms", |
| 1225 |
APIVersion: "v1", |
| 1226 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1227 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1228 |
CacheSize: pointer.Int32(10), |
| 1229 |
}, |
| 1230 |
}, |
| 1231 |
}, |
| 1232 |
}, |
| 1233 |
{ |
| 1234 |
Resources: []string{ |
| 1235 |
"*.*", |
| 1236 |
}, |
| 1237 |
Providers: []apiserver.ProviderConfiguration{ |
| 1238 |
{ |
| 1239 |
KMS: &apiserver.KMSConfiguration{ |
| 1240 |
Name: "another-kms", |
| 1241 |
APIVersion: "v1", |
| 1242 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1243 |
Endpoint: "unix:///tmp/another-testprovider.sock", |
| 1244 |
CacheSize: pointer.Int32(10), |
| 1245 |
}, |
| 1246 |
}, |
| 1247 |
}, |
| 1248 |
}, |
| 1249 |
}, |
| 1250 |
}, |
| 1251 |
}, |
| 1252 |
} |
| 1253 |
|
| 1254 |
for _, tc := range testCases { |
| 1255 |
t.Run(tc.desc, func(t *testing.T) { |
| 1256 |
ctx, cancel := context.WithCancel(context.Background()) |
| 1257 |
t.Cleanup(cancel) |
| 1258 |
|
| 1259 |
_, _, _, err := getTransformerOverridesAndKMSPluginProbes(ctx, tc.config, "") |
| 1260 |
if errString(err) != tc.expectedError { |
| 1261 |
t.Errorf("expected error %s but got %s", tc.expectedError, errString(err)) |
| 1262 |
} |
| 1263 |
}) |
| 1264 |
} |
| 1265 |
} |
| 1266 |
|
| 1267 |
func TestWildcardStructure(t *testing.T) { |
| 1268 |
featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.KMSv1, true) |
| 1269 |
testCases := []struct { |
| 1270 |
desc string |
| 1271 |
expectedResourceTransformers map[string]string |
| 1272 |
config *apiserver.EncryptionConfiguration |
| 1273 |
errorValue string |
| 1274 |
}{ |
| 1275 |
{ |
| 1276 |
desc: "should not result in error", |
| 1277 |
expectedResourceTransformers: map[string]string{ |
| 1278 |
"configmaps": "k8s:enc:kms:v1:kms:", |
| 1279 |
"secrets": "k8s:enc:kms:v1:another-kms:", |
| 1280 |
"events": "k8s:enc:kms:v1:fancy:", |
| 1281 |
"deployments.apps": "k8s:enc:kms:v1:kms:", |
| 1282 |
"pods": "k8s:enc:kms:v1:fancy:", |
| 1283 |
"pandas": "k8s:enc:kms:v1:fancy:", |
| 1284 |
"pandas.bears": "k8s:enc:kms:v1:yet-another-provider:", |
| 1285 |
"jobs.apps": "k8s:enc:kms:v1:kms:", |
| 1286 |
}, |
| 1287 |
|
| 1288 |
errorValue: "", |
| 1289 |
config: &apiserver.EncryptionConfiguration{ |
| 1290 |
Resources: []apiserver.ResourceConfiguration{ |
| 1291 |
{ |
| 1292 |
Resources: []string{ |
| 1293 |
"configmaps", |
| 1294 |
"*.apps", |
| 1295 |
}, |
| 1296 |
Providers: []apiserver.ProviderConfiguration{ |
| 1297 |
{ |
| 1298 |
KMS: &apiserver.KMSConfiguration{ |
| 1299 |
Name: "kms", |
| 1300 |
APIVersion: "v1", |
| 1301 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1302 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1303 |
CacheSize: pointer.Int32(10), |
| 1304 |
}, |
| 1305 |
}, |
| 1306 |
}, |
| 1307 |
}, |
| 1308 |
{ |
| 1309 |
Resources: []string{ |
| 1310 |
"secrets", |
| 1311 |
}, |
| 1312 |
Providers: []apiserver.ProviderConfiguration{ |
| 1313 |
{ |
| 1314 |
KMS: &apiserver.KMSConfiguration{ |
| 1315 |
Name: "another-kms", |
| 1316 |
APIVersion: "v1", |
| 1317 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1318 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1319 |
CacheSize: pointer.Int32(10), |
| 1320 |
}, |
| 1321 |
}, |
| 1322 |
{ |
| 1323 |
Identity: &apiserver.IdentityConfiguration{}, |
| 1324 |
}, |
| 1325 |
}, |
| 1326 |
}, |
| 1327 |
{ |
| 1328 |
Resources: []string{ |
| 1329 |
"*.", |
| 1330 |
}, |
| 1331 |
Providers: []apiserver.ProviderConfiguration{ |
| 1332 |
{ |
| 1333 |
KMS: &apiserver.KMSConfiguration{ |
| 1334 |
Name: "fancy", |
| 1335 |
APIVersion: "v1", |
| 1336 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1337 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1338 |
CacheSize: pointer.Int32(10), |
| 1339 |
}, |
| 1340 |
}, |
| 1341 |
}, |
| 1342 |
}, |
| 1343 |
{ |
| 1344 |
Resources: []string{ |
| 1345 |
"*.*", |
| 1346 |
}, |
| 1347 |
Providers: []apiserver.ProviderConfiguration{ |
| 1348 |
{ |
| 1349 |
KMS: &apiserver.KMSConfiguration{ |
| 1350 |
Name: "yet-another-provider", |
| 1351 |
APIVersion: "v1", |
| 1352 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1353 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1354 |
CacheSize: pointer.Int32(10), |
| 1355 |
}, |
| 1356 |
}, |
| 1357 |
}, |
| 1358 |
}, |
| 1359 |
}, |
| 1360 |
}, |
| 1361 |
}, |
| 1362 |
{ |
| 1363 |
desc: "should result in error", |
| 1364 |
errorValue: "resource \"secrets\" is masked by earlier rule \"*.\"", |
| 1365 |
config: &apiserver.EncryptionConfiguration{ |
| 1366 |
Resources: []apiserver.ResourceConfiguration{ |
| 1367 |
{ |
| 1368 |
Resources: []string{ |
| 1369 |
"configmaps", |
| 1370 |
"*.", |
| 1371 |
}, |
| 1372 |
Providers: []apiserver.ProviderConfiguration{ |
| 1373 |
{ |
| 1374 |
KMS: &apiserver.KMSConfiguration{ |
| 1375 |
Name: "kms", |
| 1376 |
APIVersion: "v1", |
| 1377 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1378 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1379 |
CacheSize: pointer.Int32(10), |
| 1380 |
}, |
| 1381 |
}, |
| 1382 |
}, |
| 1383 |
}, |
| 1384 |
{ |
| 1385 |
Resources: []string{ |
| 1386 |
"*.*", |
| 1387 |
"secrets", |
| 1388 |
}, |
| 1389 |
Providers: []apiserver.ProviderConfiguration{ |
| 1390 |
{ |
| 1391 |
KMS: &apiserver.KMSConfiguration{ |
| 1392 |
Name: "kms", |
| 1393 |
APIVersion: "v1", |
| 1394 |
Timeout: &metav1.Duration{Duration: 3 * time.Second}, |
| 1395 |
Endpoint: "unix:///tmp/testprovider.sock", |
| 1396 |
CacheSize: pointer.Int32(10), |
| 1397 |
}, |
| 1398 |
}, |
| 1399 |
{ |
| 1400 |
Identity: &apiserver.IdentityConfiguration{}, |
| 1401 |
}, |
| 1402 |
}, |
| 1403 |
}, |
| 1404 |
}, |
| 1405 |
}, |
| 1406 |
}, |
| 1407 |
} |
| 1408 |
|
| 1409 |
for _, tc := range testCases { |
| 1410 |
t.Run(tc.desc, func(t *testing.T) { |
| 1411 |
ctx, cancel := context.WithCancel(context.Background()) |
| 1412 |
t.Cleanup(cancel) |
| 1413 |
|
| 1414 |
transformers, _, _, err := getTransformerOverridesAndKMSPluginProbes(ctx, tc.config, "") |
| 1415 |
if errString(err) != tc.errorValue { |
| 1416 |
t.Errorf("expected error %s but got %s", tc.errorValue, errString(err)) |
| 1417 |
} |
| 1418 |
|
| 1419 |
if len(tc.errorValue) > 0 { |
| 1420 |
return |
| 1421 |
} |
| 1422 |
|
| 1423 |
// check if expectedResourceTransformers are present |
| 1424 |
for resource, expectedTransformerName := range tc.expectedResourceTransformers { |
| 1425 |
transformer := transformerFromOverrides(transformers, schema.ParseGroupResource(resource)) |
| 1426 |
transformerName := string( |
| 1427 |
reflect.ValueOf(transformer). |
| 1428 |
Elem(). |
| 1429 |
FieldByName("delegate"). |
| 1430 |
Elem(). |
| 1431 |
Elem(). |
| 1432 |
FieldByName("transformers"). |
| 1433 |
Index(0). |
| 1434 |
FieldByName("Prefix"). |
| 1435 |
Bytes(), |
| 1436 |
) |
| 1437 |
|
| 1438 |
if transformerName != expectedTransformerName { |
| 1439 |
t.Errorf("resource %s: expected same transformer name but got %v", resource, cmp.Diff(transformerName, expectedTransformerName)) |
| 1440 |
} |
| 1441 |
} |
| 1442 |
}) |
| 1443 |
} |
| 1444 |
} |
| 1445 |
|
| 1446 |
func TestKMSPluginHealthzTTL(t *testing.T) { |
| 1447 |
ctx := testContext(t) |
| 1448 |
|
| 1449 |
service, _ := newMockEnvelopeService(ctx, "unix:///tmp/testprovider.sock", 3*time.Second) |
| 1450 |
errService, _ := newMockErrorEnvelopeService("unix:///tmp/testprovider.sock", 3*time.Second) |
| 1451 |
|
| 1452 |
testCases := []struct { |
| 1453 |
desc string |
| 1454 |
probe *kmsPluginProbe |
| 1455 |
wantTTL time.Duration |
| 1456 |
}{ |
| 1457 |
{ |
| 1458 |
desc: "kms provider in good state", |
| 1459 |
probe: &kmsPluginProbe{ |
| 1460 |
name: "test", |
| 1461 |
ttl: kmsPluginHealthzNegativeTTL, |
| 1462 |
service: service, |
| 1463 |
l: &sync.Mutex{}, |
| 1464 |
lastResponse: &kmsPluginHealthzResponse{}, |
| 1465 |
}, |
| 1466 |
wantTTL: kmsPluginHealthzPositiveTTL, |
| 1467 |
}, |
| 1468 |
{ |
| 1469 |
desc: "kms provider in bad state", |
| 1470 |
probe: &kmsPluginProbe{ |
| 1471 |
name: "test", |
| 1472 |
ttl: kmsPluginHealthzPositiveTTL, |
| 1473 |
service: errService, |
| 1474 |
l: &sync.Mutex{}, |
| 1475 |
lastResponse: &kmsPluginHealthzResponse{}, |
| 1476 |
}, |
| 1477 |
wantTTL: kmsPluginHealthzNegativeTTL, |
| 1478 |
}, |
| 1479 |
} |
| 1480 |
|
| 1481 |
for _, tt := range testCases { |
| 1482 |
t.Run(tt.desc, func(t *testing.T) { |
| 1483 |
_ = tt.probe.check() |
| 1484 |
if tt.probe.ttl != tt.wantTTL { |
| 1485 |
t.Fatalf("want ttl %v, got ttl %v", tt.wantTTL, tt.probe.ttl) |
| 1486 |
} |
| 1487 |
}) |
| 1488 |
} |
| 1489 |
} |
| 1490 |
|
| 1491 |
func TestKMSv2PluginHealthzTTL(t *testing.T) { |
| 1492 |
ctx := testContext(t) |
| 1493 |
|
| 1494 |
service, _ := newMockEnvelopeKMSv2Service(ctx, "unix:///tmp/testprovider.sock", "providerName", 3*time.Second) |
| 1495 |
errService, _ := newMockErrorEnvelopeKMSv2Service("unix:///tmp/testprovider.sock", 3*time.Second) |
| 1496 |
|
| 1497 |
testCases := []struct { |
| 1498 |
desc string |
| 1499 |
probe *kmsv2PluginProbe |
| 1500 |
wantTTL time.Duration |
| 1501 |
}{ |
| 1502 |
{ |
| 1503 |
desc: "kmsv2 provider in good state", |
| 1504 |
probe: &kmsv2PluginProbe{ |
| 1505 |
name: "test", |
| 1506 |
ttl: kmsPluginHealthzNegativeTTL, |
| 1507 |
service: service, |
| 1508 |
l: &sync.Mutex{}, |
| 1509 |
lastResponse: &kmsPluginHealthzResponse{}, |
| 1510 |
}, |
| 1511 |
wantTTL: kmsPluginHealthzPositiveTTL, |
| 1512 |
}, |
| 1513 |
{ |
| 1514 |
desc: "kmsv2 provider in bad state", |
| 1515 |
probe: &kmsv2PluginProbe{ |
| 1516 |
name: "test", |
| 1517 |
ttl: kmsPluginHealthzPositiveTTL, |
| 1518 |
service: errService, |
| 1519 |
l: &sync.Mutex{}, |
| 1520 |
lastResponse: &kmsPluginHealthzResponse{}, |
| 1521 |
}, |
| 1522 |
wantTTL: kmsPluginHealthzNegativeTTL, |
| 1523 |
}, |
| 1524 |
} |
| 1525 |
|
| 1526 |
for _, tt := range testCases { |
| 1527 |
t.Run(tt.desc, func(t *testing.T) { |
| 1528 |
tt.probe.state.Store(&envelopekmsv2.State{}) |
| 1529 |
_ = tt.probe.check(ctx) |
| 1530 |
if tt.probe.ttl != tt.wantTTL { |
| 1531 |
t.Fatalf("want ttl %v, got ttl %v", tt.wantTTL, tt.probe.ttl) |
| 1532 |
} |
| 1533 |
}) |
| 1534 |
} |
| 1535 |
} |
| 1536 |
|
| 1537 |
func TestKMSv2InvalidKeyID(t *testing.T) { |
| 1538 |
ctx := testContext(t) |
| 1539 |
invalidKeyIDService, _ := newMockInvalidKeyIDEnvelopeKMSv2Service(ctx, "unix:///tmp/testprovider.sock", 3*time.Second, "") |
| 1540 |
invalidLongKeyIDService, _ := newMockInvalidKeyIDEnvelopeKMSv2Service(ctx, "unix:///tmp/testprovider.sock", 3*time.Second, sampleInvalidKeyID) |
| 1541 |
service, _ := newMockInvalidKeyIDEnvelopeKMSv2Service(ctx, "unix:///tmp/testprovider.sock", 3*time.Second, "1") |
| 1542 |
|
| 1543 |
testCases := []struct { |
| 1544 |
desc string |
| 1545 |
probe *kmsv2PluginProbe |
| 1546 |
metrics []string |
| 1547 |
want string |
| 1548 |
}{ |
| 1549 |
{ |
| 1550 |
desc: "kmsv2 provider returns an invalid empty keyID", |
| 1551 |
probe: &kmsv2PluginProbe{ |
| 1552 |
name: "test", |
| 1553 |
ttl: kmsPluginHealthzNegativeTTL, |
| 1554 |
service: invalidKeyIDService, |
| 1555 |
l: &sync.Mutex{}, |
| 1556 |
lastResponse: &kmsPluginHealthzResponse{}, |
| 1557 |
}, |
| 1558 |
metrics: []string{ |
| 1559 |
"apiserver_envelope_encryption_invalid_key_id_from_status_total", |
| 1560 |
}, |
| 1561 |
want: ` |
| 1562 |
# HELP apiserver_envelope_encryption_invalid_key_id_from_status_total [ALPHA] Number of times an invalid keyID is returned by the Status RPC call split by error. |
| 1563 |
# TYPE apiserver_envelope_encryption_invalid_key_id_from_status_total counter |
| 1564 |
apiserver_envelope_encryption_invalid_key_id_from_status_total{error="empty",provider_name="test"} 1 |
| 1565 |
`, |
| 1566 |
}, |
| 1567 |
{ |
| 1568 |
desc: "kmsv2 provider returns a valid keyID", |
| 1569 |
probe: &kmsv2PluginProbe{ |
| 1570 |
name: "test", |
| 1571 |
ttl: kmsPluginHealthzNegativeTTL, |
| 1572 |
service: service, |
| 1573 |
l: &sync.Mutex{}, |
| 1574 |
lastResponse: &kmsPluginHealthzResponse{}, |
| 1575 |
}, |
| 1576 |
metrics: []string{ |
| 1577 |
"apiserver_envelope_encryption_invalid_key_id_from_status_total", |
| 1578 |
}, |
| 1579 |
want: ``, |
| 1580 |
}, |
| 1581 |
{ |
| 1582 |
desc: "kmsv2 provider returns an invalid long keyID", |
| 1583 |
probe: &kmsv2PluginProbe{ |
| 1584 |
name: "test", |
| 1585 |
ttl: kmsPluginHealthzNegativeTTL, |
| 1586 |
service: invalidLongKeyIDService, |
| 1587 |
l: &sync.Mutex{}, |
| 1588 |
lastResponse: &kmsPluginHealthzResponse{}, |
| 1589 |
}, |
| 1590 |
metrics: []string{ |
| 1591 |
"apiserver_envelope_encryption_invalid_key_id_from_status_total", |
| 1592 |
}, |
| 1593 |
want: ` |
| 1594 |
# HELP apiserver_envelope_encryption_invalid_key_id_from_status_total [ALPHA] Number of times an invalid keyID is returned by the Status RPC call split by error. |
| 1595 |
# TYPE apiserver_envelope_encryption_invalid_key_id_from_status_total counter |
| 1596 |
apiserver_envelope_encryption_invalid_key_id_from_status_total{error="too_long",provider_name="test"} 1 |
| 1597 |
`, |
| 1598 |
}, |
| 1599 |
} |
| 1600 |
|
| 1601 |
metrics.InvalidKeyIDFromStatusTotal.Reset() |
| 1602 |
metrics.RegisterMetrics() |
| 1603 |
|
| 1604 |
for _, tt := range testCases { |
| 1605 |
t.Run(tt.desc, func(t *testing.T) { |
| 1606 |
defer metrics.InvalidKeyIDFromStatusTotal.Reset() |
| 1607 |
tt.probe.state.Store(&envelopekmsv2.State{}) |
| 1608 |
_ = tt.probe.check(ctx) |
| 1609 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tt.want), tt.metrics...); err != nil { |
| 1610 |
t.Fatal(err) |
| 1611 |
} |
| 1612 |
}) |
| 1613 |
} |
| 1614 |
} |
| 1615 |
|
| 1616 |
func TestCBCKeyRotationWithOverlappingProviders(t *testing.T) { |
| 1617 |
testCBCKeyRotationWithProviders( |
| 1618 |
t, |
| 1619 |
"testdata/valid-configs/aes/aes-cbc-multiple-providers.json", |
| 1620 |
"k8s:enc:aescbc:v1:1:", |
| 1621 |
"testdata/valid-configs/aes/aes-cbc-multiple-providers-reversed.json", |
| 1622 |
"k8s:enc:aescbc:v1:2:", |
| 1623 |
) |
| 1624 |
} |
| 1625 |
|
| 1626 |
func TestCBCKeyRotationWithoutOverlappingProviders(t *testing.T) { |
| 1627 |
testCBCKeyRotationWithProviders( |
| 1628 |
t, |
| 1629 |
"testdata/valid-configs/aes/aes-cbc-multiple-keys.json", |
| 1630 |
"k8s:enc:aescbc:v1:A:", |
| 1631 |
"testdata/valid-configs/aes/aes-cbc-multiple-keys-reversed.json", |
| 1632 |
"k8s:enc:aescbc:v1:B:", |
| 1633 |
) |
| 1634 |
} |
| 1635 |
|
| 1636 |
func testCBCKeyRotationWithProviders(t *testing.T, firstEncryptionConfig, firstPrefix, secondEncryptionConfig, secondPrefix string) { |
| 1637 |
p := getTransformerFromEncryptionConfig(t, firstEncryptionConfig) |
| 1638 |
|
| 1639 |
ctx := testContext(t) |
| 1640 |
dataCtx := value.DefaultContext("authenticated_data") |
| 1641 |
|
| 1642 |
out, err := p.TransformToStorage(ctx, []byte("firstvalue"), dataCtx) |
| 1643 |
if err != nil { |
| 1644 |
t.Fatal(err) |
| 1645 |
} |
| 1646 |
if !bytes.HasPrefix(out, []byte(firstPrefix)) { |
| 1647 |
t.Fatalf("unexpected prefix: %q", out) |
| 1648 |
} |
| 1649 |
from, stale, err := p.TransformFromStorage(ctx, out, dataCtx) |
| 1650 |
if err != nil { |
| 1651 |
t.Fatal(err) |
| 1652 |
} |
| 1653 |
if stale || !bytes.Equal([]byte("firstvalue"), from) { |
| 1654 |
t.Fatalf("unexpected data: %t %q", stale, from) |
| 1655 |
} |
| 1656 |
|
| 1657 |
// verify changing the context fails storage |
| 1658 |
_, _, err = p.TransformFromStorage(ctx, out, value.DefaultContext("incorrect_context")) |
| 1659 |
if err != nil { |
| 1660 |
t.Fatalf("CBC mode does not support authentication: %v", err) |
| 1661 |
} |
| 1662 |
|
| 1663 |
// reverse the order, use the second key |
| 1664 |
p = getTransformerFromEncryptionConfig(t, secondEncryptionConfig) |
| 1665 |
from, stale, err = p.TransformFromStorage(ctx, out, dataCtx) |
| 1666 |
if err != nil { |
| 1667 |
t.Fatal(err) |
| 1668 |
} |
| 1669 |
if !stale || !bytes.Equal([]byte("firstvalue"), from) { |
| 1670 |
t.Fatalf("unexpected data: %t %q", stale, from) |
| 1671 |
} |
| 1672 |
|
| 1673 |
out, err = p.TransformToStorage(ctx, []byte("firstvalue"), dataCtx) |
| 1674 |
if err != nil { |
| 1675 |
t.Fatal(err) |
| 1676 |
} |
| 1677 |
if !bytes.HasPrefix(out, []byte(secondPrefix)) { |
| 1678 |
t.Fatalf("unexpected prefix: %q", out) |
| 1679 |
} |
| 1680 |
from, stale, err = p.TransformFromStorage(ctx, out, dataCtx) |
| 1681 |
if err != nil { |
| 1682 |
t.Fatal(err) |
| 1683 |
} |
| 1684 |
if stale || !bytes.Equal([]byte("firstvalue"), from) { |
| 1685 |
t.Fatalf("unexpected data: %t %q", stale, from) |
| 1686 |
} |
| 1687 |
} |
| 1688 |
|
| 1689 |
func getTransformerFromEncryptionConfig(t *testing.T, encryptionConfigPath string) value.Transformer { |
| 1690 |
ctx := testContext(t) |
| 1691 |
|
| 1692 |
t.Helper() |
| 1693 |
encryptionConfiguration, err := LoadEncryptionConfig(ctx, encryptionConfigPath, false, "") |
| 1694 |
if err != nil { |
| 1695 |
t.Fatal(err) |
| 1696 |
} |
| 1697 |
if len(encryptionConfiguration.Transformers) != 1 { |
| 1698 |
t.Fatalf("input config does not have exactly one resource: %s", encryptionConfigPath) |
| 1699 |
} |
| 1700 |
for _, transformer := range encryptionConfiguration.Transformers { |
| 1701 |
return transformer |
| 1702 |
} |
| 1703 |
panic("unreachable") |
| 1704 |
} |
| 1705 |
|
| 1706 |
func TestIsKMSv2ProviderHealthyError(t *testing.T) { |
| 1707 |
probe := &kmsv2PluginProbe{name: "testplugin"} |
| 1708 |
|
| 1709 |
testCases := []struct { |
| 1710 |
desc string |
| 1711 |
expectedErr string |
| 1712 |
wantMetrics string |
| 1713 |
statusResponse *kmsservice.StatusResponse |
| 1714 |
}{ |
| 1715 |
{ |
| 1716 |
desc: "healthz status is not ok", |
| 1717 |
statusResponse: &kmsservice.StatusResponse{ |
| 1718 |
Healthz: "unhealthy", |
| 1719 |
}, |
| 1720 |
expectedErr: "got unexpected healthz status: unhealthy, expected KMSv2 API version v2, got , got invalid KMSv2 KeyID ", |
| 1721 |
wantMetrics: ` |
| 1722 |
# HELP apiserver_envelope_encryption_invalid_key_id_from_status_total [ALPHA] Number of times an invalid keyID is returned by the Status RPC call split by error. |
| 1723 |
# TYPE apiserver_envelope_encryption_invalid_key_id_from_status_total counter |
| 1724 |
apiserver_envelope_encryption_invalid_key_id_from_status_total{error="empty",provider_name="testplugin"} 1 |
| 1725 |
`, |
| 1726 |
}, |
| 1727 |
{ |
| 1728 |
desc: "version is not v2", |
| 1729 |
statusResponse: &kmsservice.StatusResponse{ |
| 1730 |
Version: "v1beta1", |
| 1731 |
}, |
| 1732 |
expectedErr: "got unexpected healthz status: , expected KMSv2 API version v2, got v1beta1, got invalid KMSv2 KeyID ", |
| 1733 |
wantMetrics: ` |
| 1734 |
# HELP apiserver_envelope_encryption_invalid_key_id_from_status_total [ALPHA] Number of times an invalid keyID is returned by the Status RPC call split by error. |
| 1735 |
# TYPE apiserver_envelope_encryption_invalid_key_id_from_status_total counter |
| 1736 |
apiserver_envelope_encryption_invalid_key_id_from_status_total{error="empty",provider_name="testplugin"} 1 |
| 1737 |
`, |
| 1738 |
}, |
| 1739 |
{ |
| 1740 |
desc: "missing keyID", |
| 1741 |
statusResponse: &kmsservice.StatusResponse{ |
| 1742 |
Healthz: "ok", |
| 1743 |
Version: "v2beta1", |
| 1744 |
}, |
| 1745 |
expectedErr: "got invalid KMSv2 KeyID ", |
| 1746 |
wantMetrics: ` |
| 1747 |
# HELP apiserver_envelope_encryption_invalid_key_id_from_status_total [ALPHA] Number of times an invalid keyID is returned by the Status RPC call split by error. |
| 1748 |
# TYPE apiserver_envelope_encryption_invalid_key_id_from_status_total counter |
| 1749 |
apiserver_envelope_encryption_invalid_key_id_from_status_total{error="empty",provider_name="testplugin"} 1 |
| 1750 |
`, |
| 1751 |
}, |
| 1752 |
{ |
| 1753 |
desc: "invalid long keyID", |
| 1754 |
statusResponse: &kmsservice.StatusResponse{ |
| 1755 |
Healthz: "ok", |
| 1756 |
Version: "v2", |
| 1757 |
KeyID: sampleInvalidKeyID, |
| 1758 |
}, |
| 1759 |
expectedErr: "got invalid KMSv2 KeyID ", |
| 1760 |
wantMetrics: ` |
| 1761 |
# HELP apiserver_envelope_encryption_invalid_key_id_from_status_total [ALPHA] Number of times an invalid keyID is returned by the Status RPC call split by error. |
| 1762 |
# TYPE apiserver_envelope_encryption_invalid_key_id_from_status_total counter |
| 1763 |
apiserver_envelope_encryption_invalid_key_id_from_status_total{error="too_long",provider_name="testplugin"} 1 |
| 1764 |
`, |
| 1765 |
}, |
| 1766 |
} |
| 1767 |
|
| 1768 |
for _, tt := range testCases { |
| 1769 |
t.Run(tt.desc, func(t *testing.T) { |
| 1770 |
metrics.InvalidKeyIDFromStatusTotal.Reset() |
| 1771 |
err := probe.isKMSv2ProviderHealthyAndMaybeRotateDEK(testContext(t), tt.statusResponse) |
| 1772 |
if !strings.Contains(errString(err), tt.expectedErr) { |
| 1773 |
t.Errorf("expected err %q, got %q", tt.expectedErr, errString(err)) |
| 1774 |
} |
| 1775 |
if err := testutil.GatherAndCompare(legacyregistry.DefaultGatherer, strings.NewReader(tt.wantMetrics), |
| 1776 |
"apiserver_envelope_encryption_invalid_key_id_from_status_total", |
| 1777 |
); err != nil { |
| 1778 |
t.Fatal(err) |
| 1779 |
} |
| 1780 |
}) |
| 1781 |
} |
| 1782 |
} |
| 1783 |
|
| 1784 |
// test to ensure KMSv2 API version is not changed after the first status response |
| 1785 |
func TestKMSv2SameVersionFromStatus(t *testing.T) { |
| 1786 |
probe := &kmsv2PluginProbe{name: "testplugin"} |
| 1787 |
service, _ := newMockEnvelopeKMSv2Service(testContext(t), "unix:///tmp/testprovider.sock", "providerName", 3*time.Second) |
| 1788 |
probe.l = &sync.Mutex{} |
| 1789 |
probe.state.Store(&envelopekmsv2.State{}) |
| 1790 |
probe.service = service |
| 1791 |
|
| 1792 |
testCases := []struct { |
| 1793 |
desc string |
| 1794 |
expectedErr string |
| 1795 |
newVersion string |
| 1796 |
}{ |
| 1797 |
{ |
| 1798 |
desc: "version changed", |
| 1799 |
newVersion: "v2", |
| 1800 |
expectedErr: "KMSv2 API version should not change", |
| 1801 |
}, |
| 1802 |
{ |
| 1803 |
desc: "version unchanged", |
| 1804 |
newVersion: "v2beta1", |
| 1805 |
expectedErr: "", |
| 1806 |
}, |
| 1807 |
} |
| 1808 |
for _, tt := range testCases { |
| 1809 |
t.Run(tt.desc, func(t *testing.T) { |
| 1810 |
statusResponse := &kmsservice.StatusResponse{ |
| 1811 |
Healthz: "ok", |
| 1812 |
Version: "v2beta1", |
| 1813 |
KeyID: "1", |
| 1814 |
} |
| 1815 |
if err := probe.isKMSv2ProviderHealthyAndMaybeRotateDEK(testContext(t), statusResponse); err != nil { |
| 1816 |
t.Fatal(err) |
| 1817 |
} |
| 1818 |
statusResponse.Version = tt.newVersion |
| 1819 |
err := probe.isKMSv2ProviderHealthyAndMaybeRotateDEK(testContext(t), statusResponse) |
| 1820 |
if len(tt.expectedErr) > 0 && !strings.Contains(errString(err), tt.expectedErr) { |
| 1821 |
t.Errorf("expected err %q, got %q", tt.expectedErr, errString(err)) |
| 1822 |
} |
| 1823 |
if len(tt.expectedErr) == 0 && err != nil { |
| 1824 |
t.Fatal(err) |
| 1825 |
} |
| 1826 |
}) |
| 1827 |
} |
| 1828 |
} |
| 1829 |
|
| 1830 |
func testContext(t *testing.T) context.Context { |
| 1831 |
ctx, cancel := context.WithCancel(context.Background()) |
| 1832 |
t.Cleanup(cancel) |
| 1833 |
return ctx |
| 1834 |
} |
| 1835 |
|
| 1836 |
func errString(err error) string { |
| 1837 |
if err == nil { |
| 1838 |
return "" |
| 1839 |
} |
| 1840 |
|
| 1841 |
return err.Error() |
| 1842 |
} |
| 1843 |
|
| 1844 |
func TestComputeEncryptionConfigHash(t *testing.T) { |
| 1845 |
// hash the empty string to be sure that sha256 is being used |
| 1846 |
expect := "k8s:enc:unstable:1:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" |
| 1847 |
sum := computeEncryptionConfigHash([]byte("")) |
| 1848 |
if expect != sum { |
| 1849 |
t.Errorf("expected hash %q but got %q", expect, sum) |
| 1850 |
} |
| 1851 |
} |
| 1852 |
|
| 1853 |
func Test_kmsv2PluginProbe_rotateDEKOnKeyIDChange(t *testing.T) { |
| 1854 |
defaultUseSeed := GetKDF("") |
| 1855 |
|
| 1856 |
origNowFunc := envelopekmsv2.NowFunc |
| 1857 |
now := origNowFunc() // freeze time |
| 1858 |
t.Cleanup(func() { envelopekmsv2.NowFunc = origNowFunc }) |
| 1859 |
envelopekmsv2.NowFunc = func() time.Time { return now } |
| 1860 |
|
| 1861 |
klog.LogToStderr(false) |
| 1862 |
var level klog.Level |
| 1863 |
if err := level.Set("6"); err != nil { |
| 1864 |
t.Fatal(err) |
| 1865 |
} |
| 1866 |
t.Cleanup(func() { |
| 1867 |
klog.LogToStderr(true) |
| 1868 |
if err := level.Set("0"); err != nil { |
| 1869 |
t.Fatal(err) |
| 1870 |
} |
| 1871 |
klog.SetOutput(io.Discard) |
| 1872 |
}) |
| 1873 |
|
| 1874 |
tests := []struct { |
| 1875 |
name string |
| 1876 |
service *testKMSv2EnvelopeService |
| 1877 |
state envelopekmsv2.State |
| 1878 |
useSeed bool |
| 1879 |
statusKeyID string |
| 1880 |
wantState envelopekmsv2.State |
| 1881 |
wantEncryptCalls int |
| 1882 |
wantLogs []string |
| 1883 |
wantErr string |
| 1884 |
}{ |
| 1885 |
{ |
| 1886 |
name: "happy path, no previous state", |
| 1887 |
service: &testKMSv2EnvelopeService{keyID: "1"}, |
| 1888 |
state: envelopekmsv2.State{}, |
| 1889 |
statusKeyID: "1", |
| 1890 |
wantState: envelopekmsv2.State{ |
| 1891 |
EncryptedObject: kmstypes.EncryptedObject{KeyID: "1"}, |
| 1892 |
ExpirationTimestamp: now.Add(3 * time.Minute), |
| 1893 |
}, |
| 1894 |
wantEncryptCalls: 1, |
| 1895 |
wantLogs: []string{ |
| 1896 |
`"encrypting content using envelope service" uid="panda"`, |
| 1897 |
fmt.Sprintf(`"successfully rotated DEK" uid="panda" useSeed=false newKeyIDHash="sha256:6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b" oldKeyIDHash="" expirationTimestamp="%s"`, |
| 1898 |
now.Add(3*time.Minute).Format(time.RFC3339)), |
| 1899 |
}, |
| 1900 |
wantErr: "", |
| 1901 |
}, |
| 1902 |
{ |
| 1903 |
name: "happy path, with previous state", |
| 1904 |
service: &testKMSv2EnvelopeService{err: fmt.Errorf("broken")}, // not called |
| 1905 |
state: validState(t, "2", now, false), |
| 1906 |
statusKeyID: "2", |
| 1907 |
wantState: envelopekmsv2.State{ |
| 1908 |
EncryptedObject: kmstypes.EncryptedObject{KeyID: "2"}, |
| 1909 |
ExpirationTimestamp: now.Add(3 * time.Minute), |
| 1910 |
}, |
| 1911 |
wantEncryptCalls: 0, |
| 1912 |
wantLogs: nil, |
| 1913 |
wantErr: "", |
| 1914 |
}, |
| 1915 |
{ |
| 1916 |
name: "happy path, with previous state, useSeed=true", |
| 1917 |
service: &testKMSv2EnvelopeService{keyID: "2"}, |
| 1918 |
state: validState(t, "2", now, false), |
| 1919 |
useSeed: true, |
| 1920 |
statusKeyID: "2", |
| 1921 |
wantState: envelopekmsv2.State{ |
| 1922 |
EncryptedObject: kmstypes.EncryptedObject{KeyID: "2", EncryptedDEKSourceType: kmstypes.EncryptedDEKSourceType_HKDF_SHA256_XNONCE_AES_GCM_SEED}, |
| 1923 |
ExpirationTimestamp: now.Add(3 * time.Minute), |
| 1924 |
}, |
| 1925 |
wantEncryptCalls: 1, |
| 1926 |
wantLogs: []string{ |
| 1927 |
`"encrypting content using envelope service" uid="panda"`, |
| 1928 |
fmt.Sprintf(`"successfully rotated DEK" uid="panda" useSeed=true newKeyIDHash="sha256:d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35" oldKeyIDHash="sha256:d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35" expirationTimestamp="%s"`, |
| 1929 |
now.Add(3*time.Minute).Format(time.RFC3339)), |
| 1930 |
}, |
| 1931 |
wantErr: "", |
| 1932 |
}, |
| 1933 |
{ |
| 1934 |
name: "happy path, with previous useSeed=true state", |
| 1935 |
service: &testKMSv2EnvelopeService{keyID: "2"}, |
| 1936 |
state: validState(t, "2", now, true), |
| 1937 |
statusKeyID: "2", |
| 1938 |
wantState: envelopekmsv2.State{ |
| 1939 |
EncryptedObject: kmstypes.EncryptedObject{KeyID: "2"}, |
| 1940 |
ExpirationTimestamp: now.Add(3 * time.Minute), |
| 1941 |
}, |
| 1942 |
wantEncryptCalls: 1, |
| 1943 |
wantLogs: []string{ |
| 1944 |
`"encrypting content using envelope service" uid="panda"`, |
| 1945 |
fmt.Sprintf(`"successfully rotated DEK" uid="panda" useSeed=false newKeyIDHash="sha256:d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35" oldKeyIDHash="sha256:d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35" expirationTimestamp="%s"`, |
| 1946 |
now.Add(3*time.Minute).Format(time.RFC3339)), |
| 1947 |
}, |
| 1948 |
wantErr: "", |
| 1949 |
}, |
| 1950 |
{ |
| 1951 |
name: "happy path, with previous useSeed=true state, useSeed=true", |
| 1952 |
service: &testKMSv2EnvelopeService{keyID: "2"}, |
| 1953 |
state: validState(t, "2", now, true), |
| 1954 |
useSeed: true, |
| 1955 |
statusKeyID: "2", |
| 1956 |
wantState: envelopekmsv2.State{ |
| 1957 |
EncryptedObject: kmstypes.EncryptedObject{KeyID: "2", EncryptedDEKSourceType: kmstypes.EncryptedDEKSourceType_HKDF_SHA256_XNONCE_AES_GCM_SEED}, |
| 1958 |
ExpirationTimestamp: now.Add(3 * time.Minute), |
| 1959 |
}, |
| 1960 |
wantEncryptCalls: 0, |
| 1961 |
wantLogs: nil, |
| 1962 |
wantErr: "", |
| 1963 |
}, |
| 1964 |
{ |
| 1965 |
name: "happy path, with previous state, useSeed=default", |
| 1966 |
service: &testKMSv2EnvelopeService{keyID: "2"}, |
| 1967 |
state: validState(t, "2", now, false), |
| 1968 |
useSeed: defaultUseSeed, |
| 1969 |
statusKeyID: "2", |
| 1970 |
wantState: envelopekmsv2.State{ |
| 1971 |
EncryptedObject: kmstypes.EncryptedObject{KeyID: "2", EncryptedDEKSourceType: kmstypes.EncryptedDEKSourceType_HKDF_SHA256_XNONCE_AES_GCM_SEED}, |
| 1972 |
ExpirationTimestamp: now.Add(3 * time.Minute), |
| 1973 |
}, |
| 1974 |
wantEncryptCalls: 1, |
| 1975 |
wantLogs: []string{ |
| 1976 |
`"encrypting content using envelope service" uid="panda"`, |
| 1977 |
fmt.Sprintf(`"successfully rotated DEK" uid="panda" useSeed=true newKeyIDHash="sha256:d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35" oldKeyIDHash="sha256:d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35" expirationTimestamp="%s"`, |
| 1978 |
now.Add(3*time.Minute).Format(time.RFC3339)), |
| 1979 |
}, |
| 1980 |
wantErr: "", |
| 1981 |
}, |
| 1982 |
{ |
| 1983 |
name: "happy path, with previous useSeed=true state, useSeed=default", |
| 1984 |
service: &testKMSv2EnvelopeService{keyID: "2"}, |
| 1985 |
state: validState(t, "2", now, true), |
| 1986 |
useSeed: defaultUseSeed, |
| 1987 |
statusKeyID: "2", |
| 1988 |
wantState: envelopekmsv2.State{ |
| 1989 |
EncryptedObject: kmstypes.EncryptedObject{KeyID: "2", EncryptedDEKSourceType: kmstypes.EncryptedDEKSourceType_HKDF_SHA256_XNONCE_AES_GCM_SEED}, |
| 1990 |
ExpirationTimestamp: now.Add(3 * time.Minute), |
| 1991 |
}, |
| 1992 |
wantEncryptCalls: 0, |
| 1993 |
wantLogs: nil, |
| 1994 |
wantErr: "", |
| 1995 |
}, |
| 1996 |
{ |
| 1997 |
name: "previous state expired but key ID matches", |
| 1998 |
service: &testKMSv2EnvelopeService{err: fmt.Errorf("broken")}, // not called |
| 1999 |
state: validState(t, "3", now.Add(-time.Hour), false), |
| 2000 |
statusKeyID: "3", |
| 2001 |
wantState: envelopekmsv2.State{ |
| 2002 |
EncryptedObject: kmstypes.EncryptedObject{KeyID: "3"}, |
| 2003 |
ExpirationTimestamp: now.Add(3 * time.Minute), |
| 2004 |
}, |
| 2005 |
wantEncryptCalls: 0, |
| 2006 |
wantLogs: nil, |
| 2007 |
wantErr: "", |
| 2008 |
}, |
| 2009 |
{ |
| 2010 |
name: "previous state expired but key ID does not match", |
| 2011 |
service: &testKMSv2EnvelopeService{keyID: "4"}, |
| 2012 |
state: validState(t, "3", now.Add(-time.Hour), false), |
| 2013 |
statusKeyID: "4", |
| 2014 |
wantState: envelopekmsv2.State{ |
| 2015 |
EncryptedObject: kmstypes.EncryptedObject{KeyID: "4"}, |
| 2016 |
ExpirationTimestamp: now.Add(3 * time.Minute), |
| 2017 |
}, |
| 2018 |
wantEncryptCalls: 1, |
| 2019 |
wantLogs: []string{ |
| 2020 |
`"encrypting content using envelope service" uid="panda"`, |
| 2021 |
fmt.Sprintf(`"successfully rotated DEK" uid="panda" useSeed=false newKeyIDHash="sha256:4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a" oldKeyIDHash="sha256:4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce" expirationTimestamp="%s"`, |
| 2022 |
now.Add(3*time.Minute).Format(time.RFC3339)), |
| 2023 |
}, |
| 2024 |
wantErr: "", |
| 2025 |
}, |
| 2026 |
{ |
| 2027 |
name: "service down but key ID does not match", |
| 2028 |
service: &testKMSv2EnvelopeService{err: fmt.Errorf("broken")}, |
| 2029 |
state: validState(t, "4", now.Add(7*time.Minute), false), |
| 2030 |
statusKeyID: "5", |
| 2031 |
wantState: envelopekmsv2.State{ |
| 2032 |
EncryptedObject: kmstypes.EncryptedObject{KeyID: "4"}, |
| 2033 |
ExpirationTimestamp: now.Add(7 * time.Minute), |
| 2034 |
}, |
| 2035 |
wantEncryptCalls: 1, |
| 2036 |
wantLogs: []string{ |
| 2037 |
`"encrypting content using envelope service" uid="panda"`, |
| 2038 |
}, |
| 2039 |
wantErr: `failed to rotate DEK uid="panda", useSeed=false, ` + |
| 2040 |
`errState=<nil>, errGen=failed to encrypt DEK, error: broken, statusKeyIDHash="sha256:ef2d127de37b942baad06145e54b0c619a1f22327b2ebbcfbec78f5564afe39d", ` + |
| 2041 |
`encryptKeyIDHash="", stateKeyIDHash="sha256:4b227777d4dd1fc61c6f884f48641d02b4d121d3fd328cb08b5531fcacdabf8a", expirationTimestamp=` + now.Add(7*time.Minute).Format(time.RFC3339), |
| 2042 |
}, |
| 2043 |
{ |
| 2044 |
name: "invalid service response, no previous state", |
| 2045 |
service: &testKMSv2EnvelopeService{keyID: "1", encryptAnnotations: map[string][]byte{"panda": nil}}, |
| 2046 |
state: envelopekmsv2.State{}, |
| 2047 |
statusKeyID: "1", |
| 2048 |
wantState: envelopekmsv2.State{}, |
| 2049 |
wantEncryptCalls: 1, |
| 2050 |
wantLogs: []string{ |
| 2051 |
`"encrypting content using envelope service" uid="panda"`, |
| 2052 |
}, |
| 2053 |
wantErr: `failed to rotate DEK uid="panda", useSeed=false, ` + |
| 2054 |
`errState=got unexpected nil transformer, errGen=failed to validate annotations: annotations: Invalid value: "panda": ` + |
| 2055 |
`should be a domain with at least two segments separated by dots, statusKeyIDHash="sha256:6b86b273ff34fce19d6b804eff5a3f5747ada4eaa22f1d49c01e52ddb7875b4b", ` + |
| 2056 |
`encryptKeyIDHash="", stateKeyIDHash="", expirationTimestamp=` + (time.Time{}).Format(time.RFC3339), |
| 2057 |
}, |
| 2058 |
{ |
| 2059 |
name: "invalid service response, with previous state", |
| 2060 |
service: &testKMSv2EnvelopeService{keyID: "3", encryptAnnotations: map[string][]byte{"panda": nil}}, |
| 2061 |
state: validState(t, "2", now, false), |
| 2062 |
statusKeyID: "3", |
| 2063 |
wantState: envelopekmsv2.State{ |
| 2064 |
EncryptedObject: kmstypes.EncryptedObject{KeyID: "2"}, |
| 2065 |
ExpirationTimestamp: now, |
| 2066 |
}, |
| 2067 |
wantEncryptCalls: 1, |
| 2068 |
wantLogs: []string{ |
| 2069 |
`"encrypting content using envelope service" uid="panda"`, |
| 2070 |
}, |
| 2071 |
wantErr: `failed to rotate DEK uid="panda", useSeed=false, ` + |
| 2072 |
`errState=<nil>, errGen=failed to validate annotations: annotations: Invalid value: "panda": ` + |
| 2073 |
`should be a domain with at least two segments separated by dots, statusKeyIDHash="sha256:4e07408562bedb8b60ce05c1decfe3ad16b72230967de01f640b7e4729b49fce", ` + |
| 2074 |
`encryptKeyIDHash="", stateKeyIDHash="sha256:d4735e3a265e16eee03f59718b9b5d03019c07d8b6c51f90da3a666eec13ab35", expirationTimestamp=` + now.Format(time.RFC3339), |
| 2075 |
}, |
| 2076 |
} |
| 2077 |
for i, tt := range tests { |
| 2078 |
t.Run(tt.name, func(t *testing.T) { |
| 2079 |
kmsName := fmt.Sprintf("panda-%d", i) |
| 2080 |
defer SetKDFForTests(kmsName, tt.useSeed)() |
| 2081 |
|
| 2082 |
var buf bytes.Buffer |
| 2083 |
klog.SetOutput(&buf) |
| 2084 |
|
| 2085 |
ctx := testContext(t) |
| 2086 |
|
| 2087 |
h := &kmsv2PluginProbe{ |
| 2088 |
name: kmsName, |
| 2089 |
service: tt.service, |
| 2090 |
} |
| 2091 |
h.state.Store(&tt.state) |
| 2092 |
|
| 2093 |
err := h.rotateDEKOnKeyIDChange(ctx, tt.statusKeyID, "panda") |
| 2094 |
|
| 2095 |
klog.Flush() |
| 2096 |
klog.SetOutput(io.Discard) // prevent further writes into buf |
| 2097 |
|
| 2098 |
if diff := cmp.Diff(tt.wantLogs, logLines(buf.String())); len(diff) > 0 { |
| 2099 |
t.Errorf("log mismatch (-want +got):\n%s", diff) |
| 2100 |
} |
| 2101 |
|
| 2102 |
ignoredFields := sets.NewString("Transformer", "EncryptedObject.EncryptedDEKSource", "UID", "CacheKey") |
| 2103 |
|
| 2104 |
gotState := *h.state.Load() |
| 2105 |
|
| 2106 |
if diff := cmp.Diff(tt.wantState, gotState, |
| 2107 |
cmp.FilterPath(func(path cmp.Path) bool { return ignoredFields.Has(path.String()) }, cmp.Ignore()), |
| 2108 |
); len(diff) > 0 { |
| 2109 |
t.Errorf("state mismatch (-want +got):\n%s", diff) |
| 2110 |
} |
| 2111 |
|
| 2112 |
if len(cmp.Diff(tt.wantState, gotState)) > 0 { // we only need to run this check when the state changes |
| 2113 |
validCiphertext := len(gotState.EncryptedObject.EncryptedDEKSource) > 0 |
| 2114 |
if tt.useSeed { |
| 2115 |
validCiphertext = validCiphertext && gotState.EncryptedObject.EncryptedDEKSourceType == kmstypes.EncryptedDEKSourceType_HKDF_SHA256_XNONCE_AES_GCM_SEED |
| 2116 |
} else { |
| 2117 |
validCiphertext = validCiphertext && gotState.EncryptedObject.EncryptedDEKSourceType == kmstypes.EncryptedDEKSourceType_AES_GCM_KEY |
| 2118 |
} |
| 2119 |
if !validCiphertext { |
| 2120 |
t.Errorf("invalid ciphertext with useSeed=%v, encryptedDEKSourceLen=%d, encryptedDEKSourceType=%d", tt.useSeed, |
| 2121 |
len(gotState.EncryptedObject.EncryptedDEKSource), gotState.EncryptedObject.EncryptedDEKSourceType) |
| 2122 |
} |
| 2123 |
} |
| 2124 |
|
| 2125 |
if tt.wantEncryptCalls != tt.service.encryptCalls { |
| 2126 |
t.Errorf("want %d encryptCalls, got %d", tt.wantEncryptCalls, tt.service.encryptCalls) |
| 2127 |
} |
| 2128 |
|
| 2129 |
if errString(err) != tt.wantErr { |
| 2130 |
t.Errorf("rotateDEKOnKeyIDChange() error = %v, wantErr %v", err, tt.wantErr) |
| 2131 |
} |
| 2132 |
|
| 2133 |
// if the old or new state is valid, we should be able to use it |
| 2134 |
if _, stateErr := h.getCurrentState(); stateErr == nil || err == nil { |
| 2135 |
transformer := envelopekmsv2.NewEnvelopeTransformer( |
| 2136 |
&testKMSv2EnvelopeService{err: fmt.Errorf("broken")}, // not called |
| 2137 |
kmsName, |
| 2138 |
h.getCurrentState, |
| 2139 |
"", |
| 2140 |
) |
| 2141 |
|
| 2142 |
dataCtx := value.DefaultContext(sampleContextText) |
| 2143 |
originalText := []byte(sampleText) |
| 2144 |
|
| 2145 |
transformedData, err := transformer.TransformToStorage(ctx, originalText, dataCtx) |
| 2146 |
if err != nil { |
| 2147 |
t.Fatal(err) |
| 2148 |
} |
| 2149 |
|
| 2150 |
untransformedData, stale, err := transformer.TransformFromStorage(ctx, transformedData, dataCtx) |
| 2151 |
if err != nil { |
| 2152 |
t.Fatal(err) |
| 2153 |
} |
| 2154 |
if stale { |
| 2155 |
t.Error("unexpected stale data") |
| 2156 |
} |
| 2157 |
if !bytes.Equal(untransformedData, originalText) { |
| 2158 |
t.Fatalf("incorrect transformation, want: %v, got: %v", originalText, untransformedData) |
| 2159 |
} |
| 2160 |
} |
| 2161 |
}) |
| 2162 |
} |
| 2163 |
} |
| 2164 |
|
| 2165 |
func validState(t *testing.T, keyID string, exp time.Time, useSeed bool) envelopekmsv2.State { |
| 2166 |
t.Helper() |
| 2167 |
|
| 2168 |
transformer, encObject, cacheKey, err := envelopekmsv2.GenerateTransformer(testContext(t), "", &testKMSv2EnvelopeService{keyID: keyID}, useSeed) |
| 2169 |
if err != nil { |
| 2170 |
t.Fatal(err) |
| 2171 |
} |
| 2172 |
return envelopekmsv2.State{ |
| 2173 |
Transformer: transformer, |
| 2174 |
EncryptedObject: *encObject, |
| 2175 |
ExpirationTimestamp: exp, |
| 2176 |
CacheKey: cacheKey, |
| 2177 |
} |
| 2178 |
} |
| 2179 |
|
| 2180 |
func logLines(logs string) []string { |
| 2181 |
if len(logs) == 0 { |
| 2182 |
return nil |
| 2183 |
} |
| 2184 |
|
| 2185 |
lines := strings.Split(strings.TrimSpace(logs), "\n") |
| 2186 |
for i, line := range lines { |
| 2187 |
lines[i] = strings.SplitN(line, "] ", 2)[1] |
| 2188 |
} |
| 2189 |
return lines |
| 2190 |
} |
| 2191 |
|
| 2192 |
func TestGetEncryptionConfigHash(t *testing.T) { |
| 2193 |
t.Parallel() |
| 2194 |
|
| 2195 |
tests := []struct { |
| 2196 |
name string |
| 2197 |
filepath string |
| 2198 |
wantHash string |
| 2199 |
wantErr string |
| 2200 |
}{ |
| 2201 |
{ |
| 2202 |
name: "empty config file content", |
| 2203 |
filepath: "testdata/invalid-configs/kms/invalid-content.yaml", |
| 2204 |
wantHash: "", |
| 2205 |
wantErr: `encryption provider configuration file "testdata/invalid-configs/kms/invalid-content.yaml" is empty`, |
| 2206 |
}, |
| 2207 |
{ |
| 2208 |
name: "missing file", |
| 2209 |
filepath: "testdata/invalid-configs/kms/file-that-does-not-exist.yaml", |
| 2210 |
wantHash: "", |
| 2211 |
wantErr: `error reading encryption provider configuration file "testdata/invalid-configs/kms/file-that-does-not-exist.yaml": open testdata/invalid-configs/kms/file-that-does-not-exist.yaml: no such file or directory`, |
| 2212 |
}, |
| 2213 |
{ |
| 2214 |
name: "valid file", |
| 2215 |
filepath: "testdata/valid-configs/secret-box-first.yaml", |
| 2216 |
wantHash: "k8s:enc:unstable:1:c638c0327dbc3276dd1fcf3e67895d19ebca16b91ae0d19af24ef0759b8e0f66", |
| 2217 |
wantErr: ``, |
| 2218 |
}, |
| 2219 |
} |
| 2220 |
for _, tt := range tests { |
| 2221 |
tt := tt |
| 2222 |
t.Run(tt.name, func(t *testing.T) { |
| 2223 |
t.Parallel() |
| 2224 |
|
| 2225 |
got, err := GetEncryptionConfigHash(tt.filepath) |
| 2226 |
if errString(err) != tt.wantErr { |
| 2227 |
t.Errorf("GetEncryptionConfigHash() error = %v, wantErr %v", err, tt.wantErr) |
| 2228 |
return |
| 2229 |
} |
| 2230 |
if got != tt.wantHash { |
| 2231 |
t.Errorf("GetEncryptionConfigHash() got = %v, want %v", got, tt.wantHash) |
| 2232 |
} |
| 2233 |
}) |
| 2234 |
} |
| 2235 |
} |