Skip to content
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

[batch/auth] Set accounts to "inactive" after extended inactivity #14789

Open
wants to merge 15 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
14 changes: 13 additions & 1 deletion auth/auth/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ async def get_userinfo_from_hail_session_id(request: web.Request, session_id: st
SELECT users.*
FROM users
INNER JOIN sessions ON users.id = sessions.user_id
WHERE users.state = 'active' AND sessions.session_id = %s AND (ISNULL(sessions.max_age_secs) OR (NOW() < TIMESTAMPADD(SECOND, sessions.max_age_secs, sessions.created)));
WHERE (users.state = 'active' OR users.state = 'inactive') AND sessions.session_id = %s AND (ISNULL(sessions.max_age_secs) OR (NOW() < TIMESTAMPADD(SECOND, sessions.max_age_secs, sessions.created)));
""",
session_id,
'get_userinfo',
Expand All @@ -830,6 +830,18 @@ async def get_userinfo_from_hail_session_id(request: web.Request, session_id: st

if len(users) != 1:
return None

if users[0]['state'] == 'active':
current_uid = users[0]['id']
await db.execute_update(
"""
UPDATE users
SET last_activated = UTC_TIMESTAMP(3)
WHERE id = %s;
""",
current_uid,
)

return typing.cast(UserData, users[0])


Expand Down
4 changes: 3 additions & 1 deletion auth/sql/estimated-current.sql
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
CREATE TABLE `users` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`state` VARCHAR(100) NOT NULL,
-- creating, active, deleting, deleted
-- creating, active, deleting, deleted, inactive
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As Patrick mentioned, estimated-current is documentation, not something that actively runs. You'll want to add a migration, something like this (and referenced by build.yaml like this)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this in line with the migration you linked?

`username` varchar(255) NOT NULL COLLATE utf8mb4_0900_as_cs,
`login_id` varchar(255) DEFAULT NULL COLLATE utf8mb4_0900_as_cs,
`display_name` varchar(255) DEFAULT NULL,
Expand All @@ -16,6 +16,8 @@ CREATE TABLE `users` (
-- namespace, for developers
`namespace_name` varchar(255) DEFAULT NULL,
`trial_bp_name` varchar(300) DEFAULT NULL,
-- "last activated" tracking
`last_activated` DATETIME NOT NULL DEFAULT NOW(3),
PRIMARY KEY (`id`),
UNIQUE KEY `email` (`email`),
UNIQUE KEY `username` (`username`)
Expand Down
9 changes: 9 additions & 0 deletions batch/batch/driver/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,14 @@ async def scheduling_cancelling_bump(app):
app['cancel_running_state_changed'].set()


async def update_inactive_users(db: Database):
await db.execute_update("""
UPDATE users
SET users.state = 'inactive'
WHERE (users.state = 'active') AND (users.last_activated IS NOT NULL) AND (DATEDIFF(CURRENT_DATE(), users.last_activated) > 60);
""")


Resource = namedtuple('Resource', ['resource_id', 'deduped_resource_id'])


Expand Down Expand Up @@ -1754,6 +1762,7 @@ async def close_and_wait():
task_manager.ensure_future(periodically_call(60, compact_agg_billing_project_users_by_date_table, app, db))
task_manager.ensure_future(periodically_call(60, delete_committed_job_groups_inst_coll_staging_records, db))
task_manager.ensure_future(periodically_call(60, delete_prev_cancelled_job_group_cancellable_resources_records, db))
task_manager.ensure_future(periodically_call(86400, update_inactive_users, db)) # 86400 seconds = 1 day


async def on_cleanup(app):
Expand Down
1 change: 1 addition & 0 deletions batch/sql/add-users-last-active.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users ADD COLUMN last_activated DATETIME NOT NULL DEFAULT NOW(3), ALGORITHM=INSTANT;

Check failure on line 1 in batch/sql/add-users-last-active.sql

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

batch/sql/add-users-last-active.sql#L1

syntax error at or near "ALGORITHM"
3 changes: 3 additions & 0 deletions build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2347,6 +2347,9 @@ steps:
- name: fix-mark-job-complete-deadlocks
script: /io/sql/fix-mark-job-complete-deadlocks.sql
online: true
- name: add-users-last-active
script: /io/sql/add-users-last-active.sql
online: true
- name: jobs-add-n-max-attempts
script: /io/sql/jobs-add-n-max-attempts.sql
online: true
Expand Down
5 changes: 5 additions & 0 deletions gear/gear/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ async def wrapped(request: web.Request) -> web.StreamResponse:
if redirect or (redirect is None and '/api/' not in request.path):
raise login_redirect(request)
raise web.HTTPUnauthorized()
elif userdata['state'] == 'inactive':
raise web.HTTPUnauthorized(
text="Account is inactive. Please contact a Hail administrator to reactivate."
)

request['userdata'] = userdata
return await fun(request, userdata)

Expand Down