19
19
import shutil
20
20
import tempfile
21
21
import unittest
22
+ from collections import namedtuple
22
23
23
24
import mock
24
25
import yaml
27
28
from kubernetes .client import Configuration
28
29
29
30
from .config_exception import ConfigException
30
- from .kube_config import (ENV_KUBECONFIG_PATH_SEPARATOR , ConfigNode ,
31
- FileOrData , KubeConfigLoader , KubeConfigMerger ,
32
- _cleanup_temp_files , _create_temp_file_with_content ,
31
+ from .dateutil import parse_rfc3339
32
+ from .kube_config import (ENV_KUBECONFIG_PATH_SEPARATOR , CommandTokenSource ,
33
+ ConfigNode , FileOrData , KubeConfigLoader ,
34
+ KubeConfigMerger , _cleanup_temp_files ,
35
+ _create_temp_file_with_content ,
33
36
list_kube_config_contexts , load_kube_config ,
34
37
new_client_from_config )
35
38
@@ -550,6 +553,27 @@ class TestKubeConfigLoader(BaseTestCase):
550
553
"user" : "exec_cred_user"
551
554
}
552
555
},
556
+ {
557
+ "name" : "contexttestcmdpath" ,
558
+ "context" : {
559
+ "cluster" : "clustertestcmdpath" ,
560
+ "user" : "usertestcmdpath"
561
+ }
562
+ },
563
+ {
564
+ "name" : "contexttestcmdpathempty" ,
565
+ "context" : {
566
+ "cluster" : "clustertestcmdpath" ,
567
+ "user" : "usertestcmdpathempty"
568
+ }
569
+ },
570
+ {
571
+ "name" : "contexttestcmdpathscope" ,
572
+ "context" : {
573
+ "cluster" : "clustertestcmdpath" ,
574
+ "user" : "usertestcmdpathscope"
575
+ }
576
+ }
553
577
],
554
578
"clusters" : [
555
579
{
@@ -588,6 +612,10 @@ class TestKubeConfigLoader(BaseTestCase):
588
612
"insecure-skip-tls-verify" : True ,
589
613
}
590
614
},
615
+ {
616
+ "name" : "clustertestcmdpath" ,
617
+ "cluster" : {}
618
+ }
591
619
],
592
620
"users" : [
593
621
{
@@ -661,7 +689,8 @@ class TestKubeConfigLoader(BaseTestCase):
661
689
"auth-provider" : {
662
690
"config" : {
663
691
"access-token" : TEST_AZURE_TOKEN ,
664
- "apiserver-id" : "00000002-0000-0000-c000-000000000000" ,
692
+ "apiserver-id" : "00000002-0000-0000-c000-"
693
+ "000000000000" ,
665
694
"environment" : "AzurePublicCloud" ,
666
695
"refresh-token" : "refreshToken" ,
667
696
"tenant-id" : "9d2ac018-e843-4e14-9e2b-4e0ddac75433"
@@ -676,7 +705,8 @@ class TestKubeConfigLoader(BaseTestCase):
676
705
"auth-provider" : {
677
706
"config" : {
678
707
"access-token" : TEST_AZURE_TOKEN ,
679
- "apiserver-id" : "00000002-0000-0000-c000-000000000000" ,
708
+ "apiserver-id" : "00000002-0000-0000-c000-"
709
+ "000000000000" ,
680
710
"environment" : "AzurePublicCloud" ,
681
711
"expires-in" : "0" ,
682
712
"expires-on" : "156207275" ,
@@ -693,7 +723,8 @@ class TestKubeConfigLoader(BaseTestCase):
693
723
"auth-provider" : {
694
724
"config" : {
695
725
"access-token" : TEST_AZURE_TOKEN ,
696
- "apiserver-id" : "00000002-0000-0000-c000-000000000000" ,
726
+ "apiserver-id" : "00000002-0000-0000-c000-"
727
+ "000000000000" ,
697
728
"environment" : "AzurePublicCloud" ,
698
729
"expires-in" : "0" ,
699
730
"expires-on" : "2018-10-18 00:52:29.044727" ,
@@ -710,7 +741,8 @@ class TestKubeConfigLoader(BaseTestCase):
710
741
"auth-provider" : {
711
742
"config" : {
712
743
"access-token" : TEST_AZURE_TOKEN ,
713
- "apiserver-id" : "00000002-0000-0000-c000-000000000000" ,
744
+ "apiserver-id" : "00000002-0000-0000-c000-"
745
+ "000000000000" ,
714
746
"environment" : "AzurePublicCloud" ,
715
747
"expires-in" : "0" ,
716
748
"expires-on" : "2018-10-18 00:52" ,
@@ -727,7 +759,8 @@ class TestKubeConfigLoader(BaseTestCase):
727
759
"auth-provider" : {
728
760
"config" : {
729
761
"access-token" : TEST_AZURE_TOKEN ,
730
- "apiserver-id" : "00000002-0000-0000-c000-000000000000" ,
762
+ "apiserver-id" : "00000002-0000-0000-c000-"
763
+ "000000000000" ,
731
764
"environment" : "AzurePublicCloud" ,
732
765
"expires-in" : "0" ,
733
766
"expires-on" : "-1" ,
@@ -877,6 +910,40 @@ class TestKubeConfigLoader(BaseTestCase):
877
910
}
878
911
}
879
912
},
913
+ {
914
+ "name" : "usertestcmdpath" ,
915
+ "user" : {
916
+ "auth-provider" : {
917
+ "name" : "gcp" ,
918
+ "config" : {
919
+ "cmd-path" : "cmdtorun"
920
+ }
921
+ }
922
+ }
923
+ },
924
+ {
925
+ "name" : "usertestcmdpathempty" ,
926
+ "user" : {
927
+ "auth-provider" : {
928
+ "name" : "gcp" ,
929
+ "config" : {
930
+ "cmd-path" : ""
931
+ }
932
+ }
933
+ }
934
+ },
935
+ {
936
+ "name" : "usertestcmdpathscope" ,
937
+ "user" : {
938
+ "auth-provider" : {
939
+ "name" : "gcp" ,
940
+ "config" : {
941
+ "cmd-path" : "cmd" ,
942
+ "scopes" : "scope"
943
+ }
944
+ }
945
+ }
946
+ }
880
947
]
881
948
}
882
949
@@ -1279,6 +1346,48 @@ def test_user_exec_auth(self, mock):
1279
1346
active_context = "exec_cred_user" ).load_and_set (actual )
1280
1347
self .assertEqual (expected , actual )
1281
1348
1349
+ def test_user_cmd_path (self ):
1350
+ A = namedtuple ('A' , ['token' , 'expiry' ])
1351
+ token = "dummy"
1352
+ return_value = A (token , parse_rfc3339 (datetime .datetime .now ()))
1353
+ CommandTokenSource .token = mock .Mock (return_value = return_value )
1354
+ expected = FakeConfig (api_key = {
1355
+ "authorization" : BEARER_TOKEN_FORMAT % token })
1356
+ actual = FakeConfig ()
1357
+ KubeConfigLoader (
1358
+ config_dict = self .TEST_KUBE_CONFIG ,
1359
+ active_context = "contexttestcmdpath" ).load_and_set (actual )
1360
+ expected .get_api_key_with_prefix = actual .get_api_key_with_prefix
1361
+ self .assertEqual (expected , actual )
1362
+
1363
+ def test_user_cmd_path_empty (self ):
1364
+ A = namedtuple ('A' , ['token' , 'expiry' ])
1365
+ token = "dummy"
1366
+ return_value = A (token , parse_rfc3339 (datetime .datetime .now ()))
1367
+ CommandTokenSource .token = mock .Mock (return_value = return_value )
1368
+ expected = FakeConfig (api_key = {
1369
+ "authorization" : BEARER_TOKEN_FORMAT % token })
1370
+ actual = FakeConfig ()
1371
+ self .expect_exception (lambda : KubeConfigLoader (
1372
+ config_dict = self .TEST_KUBE_CONFIG ,
1373
+ active_context = "contexttestcmdpathempty" ).load_and_set (actual ),
1374
+ "missing access token cmd "
1375
+ "(cmd-path is an empty string in your kubeconfig file)" )
1376
+
1377
+ def test_user_cmd_path_with_scope (self ):
1378
+ A = namedtuple ('A' , ['token' , 'expiry' ])
1379
+ token = "dummy"
1380
+ return_value = A (token , parse_rfc3339 (datetime .datetime .now ()))
1381
+ CommandTokenSource .token = mock .Mock (return_value = return_value )
1382
+ expected = FakeConfig (api_key = {
1383
+ "authorization" : BEARER_TOKEN_FORMAT % token })
1384
+ actual = FakeConfig ()
1385
+ self .expect_exception (lambda : KubeConfigLoader (
1386
+ config_dict = self .TEST_KUBE_CONFIG ,
1387
+ active_context = "contexttestcmdpathscope" ).load_and_set (actual ),
1388
+ "scopes can only be used when kubectl is using "
1389
+ "a gcp service account key" )
1390
+
1282
1391
1283
1392
class TestKubernetesClientConfiguration (BaseTestCase ):
1284
1393
# Verifies properties of kubernetes.client.Configuration.
@@ -1421,14 +1530,46 @@ class TestKubeConfigMerger(BaseTestCase):
1421
1530
TEST_KUBE_CONFIG_PART4 = {
1422
1531
"current-context" : "no_user" ,
1423
1532
}
1533
+ # Config with user having cmd-path
1534
+ TEST_KUBE_CONFIG_PART5 = {
1535
+ "contexts" : [
1536
+ {
1537
+ "name" : "contexttestcmdpath" ,
1538
+ "context" : {
1539
+ "cluster" : "clustertestcmdpath" ,
1540
+ "user" : "usertestcmdpath"
1541
+ }
1542
+ }
1543
+ ],
1544
+ "clusters" : [
1545
+ {
1546
+ "name" : "clustertestcmdpath" ,
1547
+ "cluster" : {}
1548
+ }
1549
+ ],
1550
+ "users" : [
1551
+ {
1552
+ "name" : "usertestcmdpath" ,
1553
+ "user" : {
1554
+ "auth-provider" : {
1555
+ "name" : "gcp" ,
1556
+ "config" : {
1557
+ "cmd-path" : "cmdtorun"
1558
+ }
1559
+ }
1560
+ }
1561
+ }
1562
+ ]
1563
+ }
1424
1564
1425
1565
def _create_multi_config (self ):
1426
1566
files = []
1427
1567
for part in (
1428
1568
self .TEST_KUBE_CONFIG_PART1 ,
1429
1569
self .TEST_KUBE_CONFIG_PART2 ,
1430
1570
self .TEST_KUBE_CONFIG_PART3 ,
1431
- self .TEST_KUBE_CONFIG_PART4 ):
1571
+ self .TEST_KUBE_CONFIG_PART4 ,
1572
+ self .TEST_KUBE_CONFIG_PART5 ):
1432
1573
files .append (self ._create_temp_file (yaml .safe_dump (part )))
1433
1574
return ENV_KUBECONFIG_PATH_SEPARATOR .join (files )
1434
1575
@@ -1439,7 +1580,11 @@ def test_list_kube_config_contexts(self):
1439
1580
{'context' : {'cluster' : 'ssl' , 'user' : 'ssl' }, 'name' : 'ssl' },
1440
1581
{'context' : {'cluster' : 'default' , 'user' : 'simple_token' },
1441
1582
'name' : 'simple_token' },
1442
- {'context' : {'cluster' : 'default' , 'user' : 'expired_oidc' }, 'name' : 'expired_oidc' }]
1583
+ {'context' : {'cluster' : 'default' , 'user' : 'expired_oidc' },
1584
+ 'name' : 'expired_oidc' },
1585
+ {'context' : {'cluster' : 'clustertestcmdpath' ,
1586
+ 'user' : 'usertestcmdpath' },
1587
+ 'name' : 'contexttestcmdpath' }]
1443
1588
1444
1589
contexts , active_context = list_kube_config_contexts (
1445
1590
config_file = kubeconfigs )
0 commit comments