22
22
23
23
import mock
24
24
import yaml
25
- from six import PY3
25
+ from six import PY3 , next
26
26
27
27
from .config_exception import ConfigException
28
- from .dateutil import parse_rfc3339
29
28
from .kube_config import (ConfigNode , FileOrData , KubeConfigLoader ,
30
29
_cleanup_temp_files , _create_temp_file_with_content ,
31
30
list_kube_config_contexts , load_kube_config ,
32
31
new_client_from_config )
33
32
34
33
BEARER_TOKEN_FORMAT = "Bearer %s"
35
34
35
+ EXPIRY_DATETIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
36
+ # should be less than kube_config.EXPIRY_SKEW_PREVENTION_DELAY
37
+ EXPIRY_TIMEDELTA = 2
38
+
36
39
NON_EXISTING_FILE = "zz_non_existing_file_472398324"
37
40
38
41
39
42
def _base64 (string ):
40
43
return base64 .encodestring (string .encode ()).decode ()
41
44
42
45
46
+ def _format_expiry_datetime (dt ):
47
+ return dt .strftime (EXPIRY_DATETIME_FORMAT )
48
+
49
+
50
+ def _get_expiry (loader ):
51
+ expired_gcp_conf = (item for item in loader ._config .value .get ("users" )
52
+ if item .get ("name" ) == "expired_gcp" )
53
+ return next (expired_gcp_conf ).get ("user" ).get ("auth-provider" ) \
54
+ .get ("config" ).get ("expiry" )
55
+
56
+
43
57
def _raise_exception (st ):
44
58
raise Exception (st )
45
59
@@ -59,6 +73,8 @@ def _raise_exception(st):
59
73
TEST_PASSWORD = "pass"
60
74
# token for me:pass
61
75
TEST_BASIC_TOKEN = "Basic bWU6cGFzcw=="
76
+ TEST_TOKEN_EXPIRY = _format_expiry_datetime (
77
+ datetime .datetime .utcnow () - datetime .timedelta (minutes = EXPIRY_TIMEDELTA ))
62
78
63
79
TEST_SSL_HOST = "https://test-host"
64
80
TEST_CERTIFICATE_AUTH = "cert-auth"
@@ -194,10 +210,12 @@ class TestConfigNode(BaseTestCase):
194
210
{"name" : "test_name2" ,
195
211
"value" : {"key1" , "test" }},
196
212
{"name" : "test_name3" , "value" : [1 , 2 , 3 ]}],
197
- "with_names_dup" : [{"name" : "test_name" , "value" : "test_value" },
198
- {"name" : "test_name" ,
199
- "value" : {"key1" , "test" }},
200
- {"name" : "test_name3" , "value" : [1 , 2 , 3 ]}]}
213
+ "with_names_dup" : [
214
+ {"name" : "test_name" , "value" : "test_value" },
215
+ {"name" : "test_name" ,
216
+ "value" : {"key1" , "test" }},
217
+ {"name" : "test_name3" , "value" : [1 , 2 , 3 ]}
218
+ ]}
201
219
202
220
def setUp (self ):
203
221
super (TestConfigNode , self ).setUp ()
@@ -213,7 +231,8 @@ def test_normal_map_array_operations(self):
213
231
self .assertEqual (3 , len (self .node ['key2' ]))
214
232
215
233
self .assertEqual ("test_obj/key3" , self .node ['key3' ].name )
216
- self .assertEqual ({"inner_key" : "inner_value" }, self .node ['key3' ].value )
234
+ self .assertEqual ({"inner_key" : "inner_value" },
235
+ self .node ['key3' ].value )
217
236
self .assertEqual ("inner_value" , self .node ['key3' ]["inner_key" ])
218
237
self .assertEqual (1 , len (self .node ['key3' ]))
219
238
@@ -255,7 +274,8 @@ def test_get_with_name_on_name_does_not_exists(self):
255
274
def test_get_with_name_on_duplicate_name (self ):
256
275
self .expect_exception (
257
276
lambda : self .node ['with_names_dup' ].get_with_name ('test_name' ),
258
- "Expected only one object with name test_name in test_obj/with_names_dup list" )
277
+ "Expected only one object with name test_name in "
278
+ "test_obj/with_names_dup list" )
259
279
260
280
261
281
class FakeConfig :
@@ -421,7 +441,8 @@ class TestKubeConfigLoader(BaseTestCase):
421
441
"name" : "ssl" ,
422
442
"cluster" : {
423
443
"server" : TEST_SSL_HOST ,
424
- "certificate-authority-data" : TEST_CERTIFICATE_AUTH_BASE64 ,
444
+ "certificate-authority-data" :
445
+ TEST_CERTIFICATE_AUTH_BASE64 ,
425
446
}
426
447
},
427
448
{
@@ -462,7 +483,7 @@ class TestKubeConfigLoader(BaseTestCase):
462
483
"name" : "gcp" ,
463
484
"config" : {
464
485
"access-token" : TEST_DATA_BASE64 ,
465
- "expiry" : "2000-01-01T12:00:00Z" , # always in past
486
+ "expiry" : TEST_TOKEN_EXPIRY , # always in past
466
487
}
467
488
},
468
489
"token" : TEST_DATA_BASE64 , # should be ignored
@@ -492,7 +513,8 @@ class TestKubeConfigLoader(BaseTestCase):
492
513
"id-token" : TEST_OIDC_EXPIRED_LOGIN ,
493
514
"idp-certificate-authority-data" : TEST_OIDC_CA ,
494
515
"idp-issuer-url" : "https://example.org/identity" ,
495
- "refresh-token" : "lucWJjEhlxZW01cXI3YmVlcYnpxNGhzk"
516
+ "refresh-token" :
517
+ "lucWJjEhlxZW01cXI3YmVlcYnpxNGhzk"
496
518
}
497
519
}
498
520
}
@@ -578,7 +600,6 @@ def test_load_gcp_token_no_refresh(self):
578
600
loader .token )
579
601
580
602
def test_load_gcp_token_with_refresh (self ):
581
-
582
603
def cred (): return None
583
604
cred .token = TEST_ANOTHER_DATA_BASE64
584
605
cred .expiry = datetime .datetime .now ()
@@ -587,7 +608,11 @@ def cred(): return None
587
608
config_dict = self .TEST_KUBE_CONFIG ,
588
609
active_context = "expired_gcp" ,
589
610
get_google_credentials = lambda : cred )
611
+ original_expiry = _get_expiry (loader )
590
612
self .assertTrue (loader ._load_gcp_token ())
613
+ new_expiry = _get_expiry (loader )
614
+ # assert that the configs expiry actually updates
615
+ self .assertTrue (new_expiry > original_expiry )
591
616
self .assertEqual (BEARER_TOKEN_FORMAT % TEST_ANOTHER_DATA_BASE64 ,
592
617
loader .token )
593
618
0 commit comments