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

Commit

Permalink
Fix base64 padding for kube config
Browse files Browse the repository at this point in the history
  • Loading branch information
bpicolo committed Jul 30, 2018
1 parent 24a0ff2 commit dadda3d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 4 additions & 2 deletions config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,15 @@ def _load_oid_token(self, provider):
if len(parts) != 3: # Not a valid JWT
return None

padding = (4 - len(parts[1]) % 4) * '='

if PY3:
jwt_attributes = json.loads(
base64.b64decode(parts[1]).decode('utf-8')
base64.b64decode(parts[1] + padding).decode('utf-8')
)
else:
jwt_attributes = json.loads(
base64.b64decode(parts[1] + "==")
base64.b64decode(parts[1] + padding)
)

expire = jwt_attributes.get('exp')
Expand Down
10 changes: 8 additions & 2 deletions config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def _base64(string):
return base64.encodestring(string.encode()).decode()


def _unpadded_base64(string):
return base64.b64encode(string.encode()).decode()


def _format_expiry_datetime(dt):
return dt.strftime(EXPIRY_DATETIME_FORMAT)

Expand Down Expand Up @@ -87,11 +91,13 @@ def _raise_exception(st):

TEST_OIDC_TOKEN = "test-oidc-token"
TEST_OIDC_INFO = "{\"name\": \"test\"}"
TEST_OIDC_BASE = _base64(TEST_OIDC_TOKEN) + "." + _base64(TEST_OIDC_INFO)
TEST_OIDC_BASE = _unpadded_base64(
TEST_OIDC_TOKEN) + "." + _unpadded_base64(TEST_OIDC_INFO)
TEST_OIDC_LOGIN = TEST_OIDC_BASE + "." + TEST_CLIENT_CERT_BASE64
TEST_OIDC_TOKEN = "Bearer %s" % TEST_OIDC_LOGIN
TEST_OIDC_EXP = "{\"name\": \"test\",\"exp\": 536457600}"
TEST_OIDC_EXP_BASE = _base64(TEST_OIDC_TOKEN) + "." + _base64(TEST_OIDC_EXP)
TEST_OIDC_EXP_BASE = _unpadded_base64(
TEST_OIDC_TOKEN) + "." + _unpadded_base64(TEST_OIDC_EXP)
TEST_OIDC_EXPIRED_LOGIN = TEST_OIDC_EXP_BASE + "." + TEST_CLIENT_CERT_BASE64
TEST_OIDC_CA = _base64(TEST_CERTIFICATE_AUTH)

Expand Down

0 comments on commit dadda3d

Please sign in to comment.