Add last_accessed information to User and expose for robot accounts
Fixes https://jira.coreos.com/browse/QUAY-848
This commit is contained in:
parent
f1da3c452f
commit
2ea13e86a0
13 changed files with 143 additions and 67 deletions
|
@ -1,11 +1,17 @@
|
|||
from peewee import fn
|
||||
import logging
|
||||
|
||||
from peewee import fn, PeeweeException
|
||||
from cachetools import lru_cache
|
||||
|
||||
from data.model import DataModelException
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from data.model import DataModelException, config
|
||||
from data.database import (Repository, User, Team, TeamMember, RepositoryPermission, TeamRole,
|
||||
Namespace, Visibility, ImageStorage, Image, RepositoryKind,
|
||||
db_for_update)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
def reduce_as_tree(queries_to_reduce):
|
||||
""" This method will split a list of queries into halves recursively until we reach individual
|
||||
queries, at which point it will start unioning the queries, or the already unioned subqueries.
|
||||
|
@ -164,3 +170,36 @@ def calculate_image_aggregate_size(ancestors_str, image_size, parent_image):
|
|||
return None
|
||||
|
||||
return ancestor_size + image_size
|
||||
|
||||
|
||||
def update_last_accessed(token_or_user):
|
||||
""" Updates the `last_accessed` field on the given token or user. If the existing field's value
|
||||
is within the configured threshold, the update is skipped. """
|
||||
threshold = timedelta(seconds=config.app_config.get('LAST_ACCESSED_UPDATE_THRESHOLD_S', 120))
|
||||
if (token_or_user.last_accessed is not None and
|
||||
datetime.utcnow() - token_or_user.last_accessed < threshold):
|
||||
# Skip updating, as we don't want to put undue pressure on the database.
|
||||
return
|
||||
|
||||
model_class = token_or_user.__class__
|
||||
last_accessed = datetime.utcnow()
|
||||
|
||||
try:
|
||||
(model_class
|
||||
.update(last_accessed=last_accessed)
|
||||
.where(model_class.id == token_or_user.id)
|
||||
.execute())
|
||||
token_or_user.last_accessed = last_accessed
|
||||
except PeeweeException as ex:
|
||||
# If there is any form of DB exception, only fail if strict logging is enabled.
|
||||
strict_logging_disabled = config.app_config.get('ALLOW_PULLS_WITHOUT_STRICT_LOGGING')
|
||||
if strict_logging_disabled:
|
||||
data = {
|
||||
'exception': ex,
|
||||
'token_or_user': token_or_user.id,
|
||||
'class': str(model_class),
|
||||
}
|
||||
|
||||
logger.exception('update last_accessed for token/user failed', extra=data)
|
||||
else:
|
||||
raise
|
||||
|
|
|
@ -3,10 +3,10 @@ import logging
|
|||
from datetime import datetime
|
||||
|
||||
from cachetools import lru_cache
|
||||
from peewee import PeeweeException
|
||||
|
||||
from data.database import AppSpecificAuthToken, User, db_transaction
|
||||
from data.model import config
|
||||
from data.model._basequery import update_last_accessed
|
||||
from util.timedeltastring import convert_to_timedelta
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
@ -104,23 +104,7 @@ def access_valid_token(token_code):
|
|||
((AppSpecificAuthToken.expiration > datetime.now()) |
|
||||
(AppSpecificAuthToken.expiration >> None)))
|
||||
.get())
|
||||
update_last_accessed(token)
|
||||
return token
|
||||
except AppSpecificAuthToken.DoesNotExist:
|
||||
return None
|
||||
|
||||
token.last_accessed = datetime.now()
|
||||
|
||||
try:
|
||||
token.save()
|
||||
except PeeweeException as ex:
|
||||
strict_logging_disabled = config.app_config.get('ALLOW_PULLS_WITHOUT_STRICT_LOGGING')
|
||||
if strict_logging_disabled:
|
||||
data = {
|
||||
'exception': ex,
|
||||
'token': token.id,
|
||||
}
|
||||
|
||||
logger.exception('update last_accessed for token failed', extra=data)
|
||||
else:
|
||||
raise
|
||||
|
||||
return token
|
||||
|
|
|
@ -173,7 +173,7 @@ def change_username(user_id, new_username):
|
|||
user = db_for_update(User.select().where(User.id == user_id)).get()
|
||||
|
||||
# Rename the robots
|
||||
for robot in db_for_update(_list_entity_robots(user.username)):
|
||||
for robot in db_for_update(_list_entity_robots(user.username, include_metadata=False)):
|
||||
_, robot_shortname = parse_robot_username(robot.username)
|
||||
new_robot_name = format_robot_username(new_username, robot_shortname)
|
||||
robot.username = new_robot_name
|
||||
|
@ -264,15 +264,10 @@ def create_robot(robot_shortname, parent, description='', unstructured_metadata=
|
|||
|
||||
|
||||
def get_or_create_robot_metadata(robot):
|
||||
try:
|
||||
return RobotAccountMetadata.get(robot_account=robot)
|
||||
except RobotAccountMetadata.DoesNotExist:
|
||||
try:
|
||||
return RobotAccountMetadata.create(robot_account=robot, description='',
|
||||
unstructured_json='{}')
|
||||
except IntegrityError:
|
||||
return RobotAccountMetadata.get(robot_account=robot)
|
||||
|
||||
defaults = dict(description='', unstructured_json='{}')
|
||||
metadata, _ = RobotAccountMetadata.get_or_create(robot_account=robot, defaults=defaults)
|
||||
return metadata
|
||||
|
||||
|
||||
def update_robot_metadata(robot, description='', unstructured_json=None):
|
||||
""" Updates the description and user-specified unstructured metadata associated
|
||||
|
@ -281,13 +276,7 @@ def update_robot_metadata(robot, description='', unstructured_json=None):
|
|||
metadata.description = description
|
||||
metadata.unstructured_json = unstructured_json or metadata.unstructured_json or {}
|
||||
metadata.save()
|
||||
|
||||
|
||||
def get_robot(robot_shortname, parent):
|
||||
robot_username = format_robot_username(parent.username, robot_shortname)
|
||||
robot = lookup_robot(robot_username)
|
||||
return robot, robot.email
|
||||
|
||||
|
||||
|
||||
def get_robot_and_metadata(robot_shortname, parent):
|
||||
robot_username = format_robot_username(parent.username, robot_shortname)
|
||||
|
@ -360,6 +349,9 @@ def verify_robot(robot_username, password):
|
|||
if not owner.enabled:
|
||||
raise InvalidRobotException('This user has been disabled. Please contact your administrator.')
|
||||
|
||||
# Mark that the robot was accessed.
|
||||
_basequery.update_last_accessed(robot)
|
||||
|
||||
return robot
|
||||
|
||||
def regenerate_robot_token(robot_shortname, parent):
|
||||
|
@ -394,22 +386,27 @@ def list_namespace_robots(namespace):
|
|||
return _list_entity_robots(namespace)
|
||||
|
||||
|
||||
def _list_entity_robots(entity_name):
|
||||
def _list_entity_robots(entity_name, include_metadata=True):
|
||||
""" Return the list of robots for the specified entity. This MUST return a query, not a
|
||||
materialized list so that callers can use db_for_update.
|
||||
"""
|
||||
return (User
|
||||
.select(User, FederatedLogin, RobotAccountMetadata)
|
||||
.join(FederatedLogin)
|
||||
.switch(User)
|
||||
.join(RobotAccountMetadata, JOIN_LEFT_OUTER)
|
||||
.where(User.robot == True, User.username ** (entity_name + '+%')))
|
||||
"""
|
||||
query = (User
|
||||
.select(User, FederatedLogin)
|
||||
.join(FederatedLogin)
|
||||
.where(User.robot == True, User.username ** (entity_name + '+%')))
|
||||
|
||||
if include_metadata:
|
||||
query = (query.switch(User)
|
||||
.join(RobotAccountMetadata, JOIN_LEFT_OUTER)
|
||||
.select(User, FederatedLogin, RobotAccountMetadata))
|
||||
|
||||
return query
|
||||
|
||||
|
||||
def list_entity_robot_permission_teams(entity_name, include_permissions=False):
|
||||
query = (_list_entity_robots(entity_name))
|
||||
|
||||
fields = [User.username, User.creation_date, FederatedLogin.service_ident,
|
||||
fields = [User.username, User.creation_date, User.last_accessed, FederatedLogin.service_ident,
|
||||
RobotAccountMetadata.description, RobotAccountMetadata.unstructured_json]
|
||||
if include_permissions:
|
||||
query = (query
|
||||
|
@ -484,6 +481,10 @@ def verify_federated_login(service_id, service_ident):
|
|||
.switch(FederatedLogin).join(User)
|
||||
.where(FederatedLogin.service_ident == service_ident, LoginService.name == service_id)
|
||||
.get())
|
||||
|
||||
# Mark that the user was accessed.
|
||||
_basequery.update_last_accessed(found.user)
|
||||
|
||||
return found.user
|
||||
except FederatedLogin.DoesNotExist:
|
||||
return None
|
||||
|
@ -749,6 +750,9 @@ def verify_user(username_or_email, password):
|
|||
.where(User.id == fetched.id)
|
||||
.execute())
|
||||
|
||||
# Mark that the user was accessed.
|
||||
_basequery.update_last_accessed(fetched)
|
||||
|
||||
# Return the valid user.
|
||||
return fetched
|
||||
|
||||
|
@ -990,10 +994,10 @@ def get_pull_credentials(robotname):
|
|||
return None
|
||||
|
||||
return {
|
||||
'username': robot.username,
|
||||
'password': login_info.service_ident,
|
||||
'registry': '%s://%s/v1/' % (config.app_config['PREFERRED_URL_SCHEME'],
|
||||
config.app_config['SERVER_HOSTNAME']),
|
||||
'username': robot.username,
|
||||
'password': login_info.service_ident,
|
||||
'registry': '%s://%s/v1/' % (config.app_config['PREFERRED_URL_SCHEME'],
|
||||
config.app_config['SERVER_HOSTNAME']),
|
||||
}
|
||||
|
||||
def get_region_locations(user):
|
||||
|
|
Reference in a new issue