@@ -187,7 +187,7 @@ func TestLegacyConfig(t *testing.T) {
187
187
}
188
188
189
189
func TestEncryptionProviderConfigCorrect (t * testing.T ) {
190
- defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .KMSv2 , true )()
190
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .KMSv1 , true )()
191
191
192
192
// Set factory for mock envelope service
193
193
factory := envelopeServiceFactory
@@ -353,42 +353,33 @@ func TestKMSv1Deprecation(t *testing.T) {
353
353
354
354
func TestKMSvsEnablement (t * testing.T ) {
355
355
testCases := []struct {
356
- name string
357
- kmsv2Enabled bool
358
- filePath string
359
- expectedErr string
356
+ name string
357
+ filePath string
358
+ expectedErr string
360
359
}{
361
360
{
362
- name : "config with kmsv2 and kmsv1, KMSv2=false" ,
363
- kmsv2Enabled : false ,
364
- filePath : "testdata/valid-configs/kms/multiple-providers-kmsv2.yaml" ,
365
- expectedErr : "KMSv2 feature is not enabled" ,
366
- },
367
- {
368
- name : "config with kmsv2 and kmsv1, KMSv2=true" ,
369
- kmsv2Enabled : true ,
370
- filePath : "testdata/valid-configs/kms/multiple-providers-kmsv2.yaml" ,
371
- expectedErr : "" ,
361
+ name : "config with kmsv2 and kmsv1, KMSv2=true, KMSv1=false, should fail when feature is disabled" ,
362
+ filePath : "testdata/valid-configs/kms/multiple-providers-mixed.yaml" ,
363
+ expectedErr : "KMSv1 is deprecated and will only receive security updates going forward. Use KMSv2 instead" ,
372
364
},
373
365
{
374
- name : "config with kmsv1, KMSv2=false" ,
375
- kmsv2Enabled : false ,
376
- filePath : "testdata/valid-configs/kms/multiple-providers.yaml" ,
377
- expectedErr : "" ,
366
+ name : "config with kmsv2, KMSv2=true, KMSv1=false" ,
367
+ filePath : "testdata/valid-configs/kms/multiple-providers-kmsv2.yaml" ,
368
+ expectedErr : "" ,
378
369
},
379
370
}
380
371
381
372
for _ , testCase := range testCases {
382
373
t .Run (testCase .name , func (t * testing.T ) {
383
- // Just testing KMSv2 feature flag
384
- defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .KMSv1 , true )()
385
-
386
- defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .KMSv2 , testCase .kmsv2Enabled )()
374
+ // only the KMSv2 feature flag is enabled
387
375
_ , err := LoadEncryptionConfig (testContext (t ), testCase .filePath , false , "" )
388
376
389
- if ! strings .Contains (errString (err ), testCase .expectedErr ) {
377
+ if len ( testCase . expectedErr ) > 0 && ! strings .Contains (errString (err ), testCase .expectedErr ) {
390
378
t .Fatalf ("expected error %q, got %q" , testCase .expectedErr , errString (err ))
391
379
}
380
+ if len (testCase .expectedErr ) == 0 && err != nil {
381
+ t .Fatalf ("unexpected error %q" , errString (err ))
382
+ }
392
383
393
384
})
394
385
}
@@ -400,43 +391,6 @@ func TestKMSvsEnablement(t *testing.T) {
400
391
config apiserverconfig.EncryptionConfiguration
401
392
wantV2Used bool
402
393
}{
403
- {
404
- name : "with kmsv1 and kmsv2, KMSv2=false" ,
405
- kmsv2Enabled : false ,
406
- config : apiserverconfig.EncryptionConfiguration {
407
- Resources : []apiserverconfig.ResourceConfiguration {
408
- {
409
- Resources : []string {"secrets" },
410
- Providers : []apiserverconfig.ProviderConfiguration {
411
- {
412
- KMS : & apiserverconfig.KMSConfiguration {
413
- Name : "kms" ,
414
- APIVersion : "v1" ,
415
- Timeout : & metav1.Duration {
416
- Duration : 1 * time .Second ,
417
- },
418
- Endpoint : "unix:///tmp/testprovider.sock" ,
419
- CacheSize : pointer .Int32 (1000 ),
420
- },
421
- },
422
- {
423
- KMS : & apiserverconfig.KMSConfiguration {
424
- Name : "another-kms" ,
425
- APIVersion : "v2" ,
426
- Timeout : & metav1.Duration {
427
- Duration : 1 * time .Second ,
428
- },
429
- Endpoint : "unix:///tmp/anothertestprovider.sock" ,
430
- CacheSize : pointer .Int32 (1000 ),
431
- },
432
- },
433
- },
434
- },
435
- },
436
- },
437
- expectedErr : "KMSv2 feature is not enabled" ,
438
- wantV2Used : false ,
439
- },
440
394
{
441
395
name : "with kmsv1 and kmsv2, KMSv2=true" ,
442
396
kmsv2Enabled : true ,
@@ -501,7 +455,7 @@ func TestKMSvsEnablement(t *testing.T) {
501
455
}
502
456
503
457
func TestKMSMaxTimeout (t * testing.T ) {
504
- defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .KMSv2 , true )()
458
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .KMSv1 , true )()
505
459
506
460
testCases := []struct {
507
461
name string
@@ -749,7 +703,7 @@ func TestKMSMaxTimeout(t *testing.T) {
749
703
}
750
704
751
705
func TestKMSPluginHealthz (t * testing.T ) {
752
- defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .KMSv2 , true )()
706
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .KMSv1 , true )()
753
707
754
708
kmsv2Probe := & kmsv2PluginProbe {
755
709
name : "foo" ,
@@ -823,7 +777,7 @@ func TestKMSPluginHealthz(t *testing.T) {
823
777
},
824
778
{
825
779
desc : "Install multiple healthz with v1 and v2" ,
826
- config : "testdata/valid-configs/kms/multiple-providers-kmsv2 .yaml" ,
780
+ config : "testdata/valid-configs/kms/multiple-providers-mixed .yaml" ,
827
781
want : []healthChecker {
828
782
kmsv2Probe ,
829
783
& kmsPluginProbe {
@@ -900,6 +854,7 @@ func TestKMSPluginHealthz(t *testing.T) {
900
854
901
855
// tests for masking rules
902
856
func TestWildcardMasking (t * testing.T ) {
857
+ defer featuregatetesting .SetFeatureGateDuringTest (t , utilfeature .DefaultFeatureGate , features .KMSv1 , true )()
903
858
904
859
testCases := []struct {
905
860
desc string
@@ -1308,7 +1263,7 @@ func TestWildcardMasking(t *testing.T) {
1308
1263
}
1309
1264
1310
1265
func TestWildcardStructure (t * testing.T ) {
1311
-
1266
+ defer featuregatetesting . SetFeatureGateDuringTest ( t , utilfeature . DefaultFeatureGate , features . KMSv1 , true )()
1312
1267
testCases := []struct {
1313
1268
desc string
1314
1269
expectedResourceTransformers map [string ]string
@@ -1752,19 +1707,19 @@ func TestIsKMSv2ProviderHealthyError(t *testing.T) {
1752
1707
statusResponse : & kmsservice.StatusResponse {
1753
1708
Healthz : "unhealthy" ,
1754
1709
},
1755
- expectedErr : "got unexpected healthz status: unhealthy, expected KMSv2 API version v2beta1 , got , got invalid KMSv2 KeyID " ,
1710
+ expectedErr : "got unexpected healthz status: unhealthy, expected KMSv2 API version v2 , got , got invalid KMSv2 KeyID " ,
1756
1711
wantMetrics : `
1757
1712
# 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.
1758
1713
# TYPE apiserver_envelope_encryption_invalid_key_id_from_status_total counter
1759
1714
apiserver_envelope_encryption_invalid_key_id_from_status_total{error="empty",provider_name="testplugin"} 1
1760
1715
` ,
1761
1716
},
1762
1717
{
1763
- desc : "version is not v2beta1 " ,
1718
+ desc : "version is not v2 " ,
1764
1719
statusResponse : & kmsservice.StatusResponse {
1765
1720
Version : "v1beta1" ,
1766
1721
},
1767
- expectedErr : "got unexpected healthz status: , expected KMSv2 API version v2beta1 , got v1beta1, got invalid KMSv2 KeyID " ,
1722
+ expectedErr : "got unexpected healthz status: , expected KMSv2 API version v2 , got v1beta1, got invalid KMSv2 KeyID " ,
1768
1723
wantMetrics : `
1769
1724
# 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.
1770
1725
# TYPE apiserver_envelope_encryption_invalid_key_id_from_status_total counter
@@ -1788,7 +1743,7 @@ func TestIsKMSv2ProviderHealthyError(t *testing.T) {
1788
1743
desc : "invalid long keyID" ,
1789
1744
statusResponse : & kmsservice.StatusResponse {
1790
1745
Healthz : "ok" ,
1791
- Version : "v2beta1 " ,
1746
+ Version : "v2 " ,
1792
1747
KeyID : sampleInvalidKeyID ,
1793
1748
},
1794
1749
expectedErr : "got invalid KMSv2 KeyID " ,
@@ -1816,6 +1771,52 @@ func TestIsKMSv2ProviderHealthyError(t *testing.T) {
1816
1771
}
1817
1772
}
1818
1773
1774
+ // test to ensure KMSv2 API version is not changed after the first status response
1775
+ func TestKMSv2SameVersionFromStatus (t * testing.T ) {
1776
+ probe := & kmsv2PluginProbe {name : "testplugin" }
1777
+ service , _ := newMockEnvelopeKMSv2Service (testContext (t ), "unix:///tmp/testprovider.sock" , "providerName" , 3 * time .Second )
1778
+ probe .l = & sync.Mutex {}
1779
+ probe .state .Store (& envelopekmsv2.State {})
1780
+ probe .service = service
1781
+
1782
+ testCases := []struct {
1783
+ desc string
1784
+ expectedErr string
1785
+ newVersion string
1786
+ }{
1787
+ {
1788
+ desc : "version changed" ,
1789
+ newVersion : "v2" ,
1790
+ expectedErr : "KMSv2 API version should not change" ,
1791
+ },
1792
+ {
1793
+ desc : "version unchanged" ,
1794
+ newVersion : "v2beta1" ,
1795
+ expectedErr : "" ,
1796
+ },
1797
+ }
1798
+ for _ , tt := range testCases {
1799
+ t .Run (tt .desc , func (t * testing.T ) {
1800
+ statusResponse := & kmsservice.StatusResponse {
1801
+ Healthz : "ok" ,
1802
+ Version : "v2beta1" ,
1803
+ KeyID : "1" ,
1804
+ }
1805
+ if err := probe .isKMSv2ProviderHealthyAndMaybeRotateDEK (testContext (t ), statusResponse ); err != nil {
1806
+ t .Fatal (err )
1807
+ }
1808
+ statusResponse .Version = tt .newVersion
1809
+ err := probe .isKMSv2ProviderHealthyAndMaybeRotateDEK (testContext (t ), statusResponse )
1810
+ if len (tt .expectedErr ) > 0 && ! strings .Contains (errString (err ), tt .expectedErr ) {
1811
+ t .Errorf ("expected err %q, got %q" , tt .expectedErr , errString (err ))
1812
+ }
1813
+ if len (tt .expectedErr ) == 0 && err != nil {
1814
+ t .Fatal (err )
1815
+ }
1816
+ })
1817
+ }
1818
+ }
1819
+
1819
1820
func testContext (t * testing.T ) context.Context {
1820
1821
ctx , cancel := context .WithCancel (context .Background ())
1821
1822
t .Cleanup (cancel )
@@ -1840,7 +1841,7 @@ func TestComputeEncryptionConfigHash(t *testing.T) {
1840
1841
}
1841
1842
1842
1843
func Test_kmsv2PluginProbe_rotateDEKOnKeyIDChange (t * testing.T ) {
1843
- defaultUseSeed := utilfeature . DefaultFeatureGate . Enabled ( features . KMSv2KDF )
1844
+ defaultUseSeed := GetKDF ( )
1844
1845
1845
1846
origNowFunc := envelopekmsv2 .NowFunc
1846
1847
now := origNowFunc () // freeze time
@@ -2065,7 +2066,7 @@ func Test_kmsv2PluginProbe_rotateDEKOnKeyIDChange(t *testing.T) {
2065
2066
}
2066
2067
for _ , tt := range tests {
2067
2068
t .Run (tt .name , func (t * testing.T ) {
2068
- defer featuregatetesting . SetFeatureGateDuringTest ( t , utilfeature . DefaultFeatureGate , features . KMSv2KDF , tt .useSeed )()
2069
+ defer SetKDFForTests ( tt .useSeed )()
2069
2070
2070
2071
var buf bytes.Buffer
2071
2072
klog .SetOutput (& buf )
0 commit comments