Skip to content

[batch/auth] Making session timeout duration configurable #14810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions auth/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ spec:
key: cloud
- name: HAIL_SHA
value: "{{ code.sha }}"
- name: SESSION_MAX_AGE_SECS
valueFrom:
secretKeyRef:
name: auth-config
key: session_max_age_secs
optional: true
{% if scope != "test" %}
- name: HAIL_SHOULD_PROFILE
value: "1"
Expand Down
12 changes: 12 additions & 0 deletions gear/gear/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from hailtop.config import get_deploy_config
from hailtop.utils import retry_transient_errors

from .database import Database
from .time_limited_max_size_cache import TimeLimitedMaxSizeCache

log = logging.getLogger('gear.auth')
Expand Down Expand Up @@ -108,6 +109,17 @@ async def _fetch_userdata(self, request: web.Request) -> Optional[UserData]:
if session_id is None:
return None

# Refresh the session timeout window
db: Database = request.app['db']
# await db.just_execute("UPDATE sessions SET created = NOW() WHERE session_id = %s", (session_id,))
db_name = await db.select_and_fetchone("SELECT DATABASE()")
log.info("Currently connected to database: %s", db_name)
async for row in db.select_and_fetchall("SHOW TABLES"):
log.info("Table: %s", row)
log.info(
"_fetch_userdata return value: %s",
self._userdata_cache.lookup((session_id, request.app[CommonAiohttpAppKeys.CLIENT_SESSION])),
)
return await self._userdata_cache.lookup((session_id, request.app[CommonAiohttpAppKeys.CLIENT_SESSION]))

@staticmethod
Expand Down
10 changes: 8 additions & 2 deletions gear/gear/auth_utils.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import os
import secrets
from typing import Optional

from hailtop.auth import session_id_encode_to_str

from .database import Database

# Default value: 1800 seconds (30 minutes)
try:
MAX_AGE_SECS: int = int(os.environ.get("SESSION_MAX_AGE_SECS", "1800"))
except Exception as exc:
raise ValueError("Unable to interpret SESSION_MAX_AGE_SECS as an integer.") from exc


async def insert_user(db, spec):
assert all(k in spec for k in ('state', 'username'))
Expand All @@ -18,8 +25,7 @@ async def insert_user(db, spec):
)


# 2592000s = 30d
async def create_session(db: Database, user_id: int, max_age_secs: Optional[int] = 2592000) -> str:
async def create_session(db: Database, user_id: int, max_age_secs: Optional[int] = MAX_AGE_SECS) -> str:
session_id = session_id_encode_to_str(secrets.token_bytes(32))
await db.just_execute(
'INSERT INTO sessions (session_id, user_id, max_age_secs) VALUES (%s, %s, %s);',
Expand Down
11 changes: 9 additions & 2 deletions gear/gear/session.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import os

import aiohttp_session
import aiohttp_session.cookie_storage

from hailtop.config import get_deploy_config

from .cloud_config import get_global_config

# Default value: 1800 seconds (30 minutes)
try:
MAX_AGE_SECS: int = int(os.environ.get("SESSION_MAX_AGE_SECS", "1800"))
except Exception as exc:
raise ValueError("Unable to interpret SESSION_MAX_AGE_SECS as an integer.") from exc


def setup_aiohttp_session(app):
deploy_config = get_deploy_config()
Expand All @@ -21,7 +29,6 @@ def setup_aiohttp_session(app):
samesite='Lax',
domain=deploy_config._domain,
path=deploy_config._base_path or '/',
# 2592000s = 30d
max_age=2592000,
max_age=MAX_AGE_SECS,
),
)