Skip to content

Commit b3ddbd9

Browse files
committed
Add tests for updated pieces
1 parent 4750aa9 commit b3ddbd9

File tree

2 files changed

+82
-3
lines changed

2 files changed

+82
-3
lines changed

config/kube_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,18 +257,18 @@ def _load_oid_token(self, provider):
257257

258258
if any(char in token for char in reserved_characters):
259259
# Invalid jwt, as it contains url-unsafe chars
260-
return None
260+
return
261261

262262
parts = token.split('.')
263263
if len(parts) != 3: # Not a valid JWT
264-
return None
264+
return
265265

266266
padding = (4 - len(parts[1]) % 4) * '='
267267
if len(padding) == 3:
268268
# According to spec, 3 padding characters cannot occur
269269
# in a valid jwt
270270
# https://tools.ietf.org/html/rfc7515#appendix-C
271-
return None
271+
return
272272

273273
if PY3:
274274
jwt_attributes = json.loads(

config/kube_config_test.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,17 @@ def _raise_exception(st):
107107
TEST_OIDC_EXP_BASE,
108108
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT)
109109
])
110+
TEST_OIDC_CONTAINS_RESERVED_CHARACTERS = ".".join([
111+
_urlsafe_unpadded_b64encode(TEST_OIDC_TOKEN),
112+
_urlsafe_unpadded_b64encode(TEST_OIDC_INFO).replace("a", "+"),
113+
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT)
114+
])
115+
TEST_OIDC_INVALID_PADDING_LENGTH = ".".join([
116+
_urlsafe_unpadded_b64encode(TEST_OIDC_TOKEN),
117+
"aaaaa",
118+
_urlsafe_unpadded_b64encode(TEST_CLIENT_CERT)
119+
])
120+
110121
TEST_OIDC_CA = _base64(TEST_CERTIFICATE_AUTH)
111122

112123

@@ -394,6 +405,22 @@ class TestKubeConfigLoader(BaseTestCase):
394405
"user": "expired_oidc_nocert"
395406
}
396407
},
408+
{
409+
"name": "oidc_contains_reserved_character",
410+
"context": {
411+
"cluster": "default",
412+
"user": "oidc_contains_reserved_character"
413+
414+
}
415+
},
416+
{
417+
"name": "oidc_invalid_padding_length",
418+
"context": {
419+
"cluster": "default",
420+
"user": "oidc_invalid_padding_length"
421+
422+
}
423+
},
397424
{
398425
"name": "user_pass",
399426
"context": {
@@ -556,6 +583,38 @@ class TestKubeConfigLoader(BaseTestCase):
556583
}
557584
}
558585
},
586+
{
587+
"name": "oidc_contains_reserved_character",
588+
"user": {
589+
"auth-provider": {
590+
"name": "oidc",
591+
"config": {
592+
"client-id": "tectonic-kubectl",
593+
"client-secret": "FAKE_SECRET",
594+
"id-token": TEST_OIDC_CONTAINS_RESERVED_CHARACTERS,
595+
"idp-issuer-url": "https://example.org/identity",
596+
"refresh-token":
597+
"lucWJjEhlxZW01cXI3YmVlcYnpxNGhzk"
598+
}
599+
}
600+
}
601+
},
602+
{
603+
"name": "oidc_invalid_padding_length",
604+
"user": {
605+
"auth-provider": {
606+
"name": "oidc",
607+
"config": {
608+
"client-id": "tectonic-kubectl",
609+
"client-secret": "FAKE_SECRET",
610+
"id-token": TEST_OIDC_INVALID_PADDING_LENGTH,
611+
"idp-issuer-url": "https://example.org/identity",
612+
"refresh-token":
613+
"lucWJjEhlxZW01cXI3YmVlcYnpxNGhzk"
614+
}
615+
}
616+
}
617+
},
559618
{
560619
"name": "user_pass",
561620
"user": {
@@ -712,6 +771,26 @@ def test_oidc_with_refresh_nocert(
712771
self.assertTrue(loader._load_auth_provider_token())
713772
self.assertEqual("Bearer abc123", loader.token)
714773

774+
def test_oidc_fails_if_contains_reserved_chars(self):
775+
loader = KubeConfigLoader(
776+
config_dict=self.TEST_KUBE_CONFIG,
777+
active_context="oidc_contains_reserved_character",
778+
)
779+
self.assertEqual(
780+
loader._load_oid_token("oidc_contains_reserved_character"),
781+
None,
782+
)
783+
784+
def test_oidc_fails_if_invalid_padding_length(self):
785+
loader = KubeConfigLoader(
786+
config_dict=self.TEST_KUBE_CONFIG,
787+
active_context="oidc_invalid_padding_length",
788+
)
789+
self.assertEqual(
790+
loader._load_oid_token("oidc_invalid_padding_length"),
791+
None,
792+
)
793+
715794
def test_user_pass(self):
716795
expected = FakeConfig(host=TEST_HOST, token=TEST_BASIC_TOKEN)
717796
actual = FakeConfig()

0 commit comments

Comments
 (0)