Skip to content
This repository was archived by the owner on Mar 13, 2022. It is now read-only.

Commit 2202bb5

Browse files
bpicoloroycaihw
authored andcommitted
Add tests for updated pieces
1 parent ba8b9e0 commit 2202bb5

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

config/kube_config.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -264,18 +264,18 @@ def _load_oid_token(self, provider):
264264

265265
if any(char in token for char in reserved_characters):
266266
# Invalid jwt, as it contains url-unsafe chars
267-
return None
267+
return
268268

269269
parts = token.split('.')
270270
if len(parts) != 3: # Not a valid JWT
271-
return None
271+
return
272272

273273
padding = (4 - len(parts[1]) % 4) * '='
274274
if len(padding) == 3:
275275
# According to spec, 3 padding characters cannot occur
276276
# in a valid jwt
277277
# https://tools.ietf.org/html/rfc7515#appendix-C
278-
return None
278+
return
279279

280280
if PY3:
281281
jwt_attributes = json.loads(

config/kube_config_test.py

+79
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ def _raise_exception(st):
114114
TEST_OIDC_EXP_BASE,
115115
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT)
116116
])
117+
TEST_OIDC_CONTAINS_RESERVED_CHARACTERS = ".".join([
118+
_urlsafe_unpadded_b64encode(TEST_OIDC_TOKEN),
119+
_urlsafe_unpadded_b64encode(TEST_OIDC_INFO).replace("a", "+"),
120+
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT)
121+
])
122+
TEST_OIDC_INVALID_PADDING_LENGTH = ".".join([
123+
_urlsafe_unpadded_b64encode(TEST_OIDC_TOKEN),
124+
"aaaaa",
125+
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT)
126+
])
127+
117128
TEST_OIDC_CA = _base64(TEST_CERTIFICATE_AUTH)
118129

119130

@@ -420,6 +431,22 @@ class TestKubeConfigLoader(BaseTestCase):
420431
"user": "expired_oidc_nocert"
421432
}
422433
},
434+
{
435+
"name": "oidc_contains_reserved_character",
436+
"context": {
437+
"cluster": "default",
438+
"user": "oidc_contains_reserved_character"
439+
440+
}
441+
},
442+
{
443+
"name": "oidc_invalid_padding_length",
444+
"context": {
445+
"cluster": "default",
446+
"user": "oidc_invalid_padding_length"
447+
448+
}
449+
},
423450
{
424451
"name": "user_pass",
425452
"context": {
@@ -606,6 +633,38 @@ class TestKubeConfigLoader(BaseTestCase):
606633
}
607634
}
608635
},
636+
{
637+
"name": "oidc_contains_reserved_character",
638+
"user": {
639+
"auth-provider": {
640+
"name": "oidc",
641+
"config": {
642+
"client-id": "tectonic-kubectl",
643+
"client-secret": "FAKE_SECRET",
644+
"id-token": TEST_OIDC_CONTAINS_RESERVED_CHARACTERS,
645+
"idp-issuer-url": "https://example.org/identity",
646+
"refresh-token":
647+
"lucWJjEhlxZW01cXI3YmVlcYnpxNGhzk"
648+
}
649+
}
650+
}
651+
},
652+
{
653+
"name": "oidc_invalid_padding_length",
654+
"user": {
655+
"auth-provider": {
656+
"name": "oidc",
657+
"config": {
658+
"client-id": "tectonic-kubectl",
659+
"client-secret": "FAKE_SECRET",
660+
"id-token": TEST_OIDC_INVALID_PADDING_LENGTH,
661+
"idp-issuer-url": "https://example.org/identity",
662+
"refresh-token":
663+
"lucWJjEhlxZW01cXI3YmVlcYnpxNGhzk"
664+
}
665+
}
666+
}
667+
},
609668
{
610669
"name": "user_pass",
611670
"user": {
@@ -804,6 +863,26 @@ def test_oidc_with_refresh_nocert(
804863
self.assertTrue(loader._load_auth_provider_token())
805864
self.assertEqual("Bearer abc123", loader.token)
806865

866+
def test_oidc_fails_if_contains_reserved_chars(self):
867+
loader = KubeConfigLoader(
868+
config_dict=self.TEST_KUBE_CONFIG,
869+
active_context="oidc_contains_reserved_character",
870+
)
871+
self.assertEqual(
872+
loader._load_oid_token("oidc_contains_reserved_character"),
873+
None,
874+
)
875+
876+
def test_oidc_fails_if_invalid_padding_length(self):
877+
loader = KubeConfigLoader(
878+
config_dict=self.TEST_KUBE_CONFIG,
879+
active_context="oidc_invalid_padding_length",
880+
)
881+
self.assertEqual(
882+
loader._load_oid_token("oidc_invalid_padding_length"),
883+
None,
884+
)
885+
807886
def test_user_pass(self):
808887
expected = FakeConfig(host=TEST_HOST, token=TEST_BASIC_TOKEN)
809888
actual = FakeConfig()

0 commit comments

Comments
 (0)