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

Commit b013697

Browse files
bpicoloroycaihw
authored andcommitted
Add tests for updated pieces
1 parent 8f0ccdb commit b013697

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
@@ -273,18 +273,18 @@ def _load_oid_token(self, provider):
273273

274274
if any(char in token for char in reserved_characters):
275275
# Invalid jwt, as it contains url-unsafe chars
276-
return None
276+
return
277277

278278
parts = token.split('.')
279279
if len(parts) != 3: # Not a valid JWT
280-
return None
280+
return
281281

282282
padding = (4 - len(parts[1]) % 4) * '='
283283
if len(padding) == 3:
284284
# According to spec, 3 padding characters cannot occur
285285
# in a valid jwt
286286
# https://tools.ietf.org/html/rfc7515#appendix-C
287-
return None
287+
return
288288

289289
if PY3:
290290
jwt_attributes = json.loads(

config/kube_config_test.py

+79
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ def _raise_exception(st):
116116
TEST_OIDC_EXP_BASE,
117117
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT)
118118
])
119+
TEST_OIDC_CONTAINS_RESERVED_CHARACTERS = ".".join([
120+
_urlsafe_unpadded_b64encode(TEST_OIDC_TOKEN),
121+
_urlsafe_unpadded_b64encode(TEST_OIDC_INFO).replace("a", "+"),
122+
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT)
123+
])
124+
TEST_OIDC_INVALID_PADDING_LENGTH = ".".join([
125+
_urlsafe_unpadded_b64encode(TEST_OIDC_TOKEN),
126+
"aaaaa",
127+
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT)
128+
])
129+
119130
TEST_OIDC_CA = _base64(TEST_CERTIFICATE_AUTH)
120131

121132

@@ -422,6 +433,22 @@ class TestKubeConfigLoader(BaseTestCase):
422433
"user": "expired_oidc_nocert"
423434
}
424435
},
436+
{
437+
"name": "oidc_contains_reserved_character",
438+
"context": {
439+
"cluster": "default",
440+
"user": "oidc_contains_reserved_character"
441+
442+
}
443+
},
444+
{
445+
"name": "oidc_invalid_padding_length",
446+
"context": {
447+
"cluster": "default",
448+
"user": "oidc_invalid_padding_length"
449+
450+
}
451+
},
425452
{
426453
"name": "user_pass",
427454
"context": {
@@ -608,6 +635,38 @@ class TestKubeConfigLoader(BaseTestCase):
608635
}
609636
}
610637
},
638+
{
639+
"name": "oidc_contains_reserved_character",
640+
"user": {
641+
"auth-provider": {
642+
"name": "oidc",
643+
"config": {
644+
"client-id": "tectonic-kubectl",
645+
"client-secret": "FAKE_SECRET",
646+
"id-token": TEST_OIDC_CONTAINS_RESERVED_CHARACTERS,
647+
"idp-issuer-url": "https://example.org/identity",
648+
"refresh-token":
649+
"lucWJjEhlxZW01cXI3YmVlcYnpxNGhzk"
650+
}
651+
}
652+
}
653+
},
654+
{
655+
"name": "oidc_invalid_padding_length",
656+
"user": {
657+
"auth-provider": {
658+
"name": "oidc",
659+
"config": {
660+
"client-id": "tectonic-kubectl",
661+
"client-secret": "FAKE_SECRET",
662+
"id-token": TEST_OIDC_INVALID_PADDING_LENGTH,
663+
"idp-issuer-url": "https://example.org/identity",
664+
"refresh-token":
665+
"lucWJjEhlxZW01cXI3YmVlcYnpxNGhzk"
666+
}
667+
}
668+
}
669+
},
611670
{
612671
"name": "user_pass",
613672
"user": {
@@ -806,6 +865,26 @@ def test_oidc_with_refresh_nocert(
806865
self.assertTrue(loader._load_auth_provider_token())
807866
self.assertEqual("Bearer abc123", loader.token)
808867

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

0 commit comments

Comments
 (0)