Skip to content

Commit 529a72a

Browse files
committed
Fix base64 padding for kube config
1 parent 24a0ff2 commit 529a72a

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

config/kube_config.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,15 @@ def _load_oid_token(self, provider):
257257
if len(parts) != 3: # Not a valid JWT
258258
return None
259259

260+
padding = (4 - len(parts[1]) % 4) * '='
261+
260262
if PY3:
261263
jwt_attributes = json.loads(
262-
base64.b64decode(parts[1]).decode('utf-8')
264+
base64.b64decode(parts[1] + padding).decode('utf-8')
263265
)
264266
else:
265267
jwt_attributes = json.loads(
266-
base64.b64decode(parts[1] + "==")
268+
base64.b64decode(parts[1] + padding)
267269
)
268270

269271
expire = jwt_attributes.get('exp')

config/kube_config_test.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def _base64(string):
4343
return base64.encodestring(string.encode()).decode()
4444

4545

46+
def _unpadded_base64(string):
47+
return base64.b64encode(string.encode()).decode().rstrip('=')
48+
49+
4650
def _format_expiry_datetime(dt):
4751
return dt.strftime(EXPIRY_DATETIME_FORMAT)
4852

@@ -87,11 +91,13 @@ def _raise_exception(st):
8791

8892
TEST_OIDC_TOKEN = "test-oidc-token"
8993
TEST_OIDC_INFO = "{\"name\": \"test\"}"
90-
TEST_OIDC_BASE = _base64(TEST_OIDC_TOKEN) + "." + _base64(TEST_OIDC_INFO)
94+
TEST_OIDC_BASE = _unpadded_base64(
95+
TEST_OIDC_TOKEN) + "." + _unpadded_base64(TEST_OIDC_INFO)
9196
TEST_OIDC_LOGIN = TEST_OIDC_BASE + "." + TEST_CLIENT_CERT_BASE64
9297
TEST_OIDC_TOKEN = "Bearer %s" % TEST_OIDC_LOGIN
9398
TEST_OIDC_EXP = "{\"name\": \"test\",\"exp\": 536457600}"
94-
TEST_OIDC_EXP_BASE = _base64(TEST_OIDC_TOKEN) + "." + _base64(TEST_OIDC_EXP)
99+
TEST_OIDC_EXP_BASE = _unpadded_base64(
100+
TEST_OIDC_TOKEN) + "." + _unpadded_base64(TEST_OIDC_EXP)
95101
TEST_OIDC_EXPIRED_LOGIN = TEST_OIDC_EXP_BASE + "." + TEST_CLIENT_CERT_BASE64
96102
TEST_OIDC_CA = _base64(TEST_CERTIFICATE_AUTH)
97103

0 commit comments

Comments
 (0)