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

Kconfig base64 padding #87

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions config/kube_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,20 @@ def _load_oid_token(self, provider):
return

parts = provider['config']['id-token'].split('.')

if len(parts) != 3: # Not a valid JWT
# check for invalid JWT
url_chars = '=+/'
if len(parts) != 3 or any(x in url_chars for x in parts):
return None

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

if PY3:
jwt_attributes = json.loads(
base64.b64decode(parts[1]).decode('utf-8')
base64.urlsafe_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
14 changes: 11 additions & 3 deletions config/kube_config_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ def _base64(string):
return base64.encodestring(string.encode()).decode()


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

def _unpadded_base64_urlsafe(string):
return base64.urlsafe_b64encode(string.encode()).decode().rstrip('=')

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

Expand Down Expand Up @@ -82,16 +88,18 @@ def _raise_exception(st):
TEST_CLIENT_KEY = "client-key"
TEST_CLIENT_KEY_BASE64 = _base64(TEST_CLIENT_KEY)
TEST_CLIENT_CERT = "client-cert"
TEST_CLIENT_CERT_BASE64 = _base64(TEST_CLIENT_CERT)
TEST_CLIENT_CERT_BASE64 = _unpadded_base64_urlsafe(TEST_CLIENT_CERT)


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