Skip to content

Commit 43ddea4

Browse files
author
ci bot
committed
Merge branch 'jwt-key' into 'enterprise'
feat(auth): The JWT signing token is now configurable See merge request dkinternal/testgen/dataops-testgen!208
2 parents 110c480 + c893444 commit 43ddea4

4 files changed

Lines changed: 29 additions & 3 deletions

File tree

deploy/charts/testgen-app/templates/_environment.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
secretKeyRef:
1010
name: {{ .Values.testgen.authSecrets.name | quote }}
1111
key: "decrypt-password"
12+
- name: TG_JWT_HASHING_KEY
13+
valueFrom:
14+
secretKeyRef:
15+
name: {{ .Values.testgen.authSecrets.name | quote }}
16+
key: "jwt-hashing-key"
1217
- name: TG_METADATA_DB_HOST
1318
value: {{ .Values.testgen.databaseHost | quote }}
1419
- name: TG_METADATA_DB_NAME

deploy/charts/testgen-app/templates/secrets.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ type: Opaque
1212
data:
1313
decrypt-salt: {{ randAlphaNum 32 | b64enc | quote }}
1414
decrypt-password: {{ randAlphaNum 32 | b64enc | quote }}
15+
jwt-hashing-key: {{ randBytes 32 | b64enc | quote }}
1516
{{- end }}

testgen/settings.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,3 +484,8 @@
484484
"""
485485
Disables sending usage data when set to any value except "true" and "yes". Defaults to "yes"
486486
"""
487+
488+
JWT_HASHING_KEY_B64: str = os.getenv("TG_JWT_HASHING_KEY")
489+
"""
490+
Random key used to sign/verify the authentication token
491+
"""

testgen/ui/services/user_session_service.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
import datetime
23
import logging
34
import typing
@@ -6,19 +7,29 @@
67
import jwt
78
import streamlit as st
89

10+
from testgen import settings
911
from testgen.ui.queries import user_queries
1012
from testgen.ui.session import session
1113

1214
RoleType = typing.Literal["admin", "data_quality", "analyst", "business", "catalog"]
1315

14-
JWT_HASHING_KEY = "dk_signature_key"
1516
AUTH_TOKEN_COOKIE_NAME = "dk_cookie_name" # noqa: S105
1617
AUTH_TOKEN_EXPIRATION_DAYS = 1
1718
DISABLED_ACTION_TEXT = "You do not have permissions to perform this action. Contact your administrator."
1819

1920
LOG = logging.getLogger("testgen")
2021

2122

23+
def _get_jwt_hashing_key() -> bytes:
24+
try:
25+
return base64.b64decode(settings.JWT_HASHING_KEY_B64.encode("ascii"))
26+
except Exception as e:
27+
raise ValueError(
28+
"Error reading the JWT signing key from settings. Make sure you have a valid base 64 "
29+
"string assigned to the TG_JWT_HASHING_KEY environment variable."
30+
) from e
31+
32+
2233
def load_user_session() -> None:
2334
# Replacing this with st.context.cookies does not work
2435
# Because it does not update when cookies are deleted on logout
@@ -29,7 +40,7 @@ def load_user_session() -> None:
2940
token = cookies.get(AUTH_TOKEN_COOKIE_NAME)
3041
if token is not None:
3142
try:
32-
token = jwt.decode(token, JWT_HASHING_KEY, algorithms=["HS256"])
43+
token = jwt.decode(token, _get_jwt_hashing_key(), algorithms=["HS256"])
3344
if token["exp_date"] > datetime.datetime.utcnow().timestamp():
3445
start_user_session(token["name"], token["username"])
3546
except Exception:
@@ -77,7 +88,11 @@ def get_auth_data():
7788

7889
return {
7990
"credentials": {"usernames": usernames},
80-
"cookie": {"expiry_days": AUTH_TOKEN_EXPIRATION_DAYS, "key": JWT_HASHING_KEY, "name": AUTH_TOKEN_COOKIE_NAME},
91+
"cookie": {
92+
"expiry_days": AUTH_TOKEN_EXPIRATION_DAYS,
93+
"key": _get_jwt_hashing_key(),
94+
"name": AUTH_TOKEN_COOKIE_NAME,
95+
},
8196
"preauthorized": {"emails": preauthorized_list},
8297
}
8398

0 commit comments

Comments
 (0)