2015-07-15 21:25:41 +00:00
|
|
|
import json
|
2017-02-15 20:44:08 +00:00
|
|
|
import logging
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
from calendar import timegm
|
2018-04-06 17:48:01 +00:00
|
|
|
from peewee import JOIN, fn, PeeweeException
|
2016-08-12 20:53:17 +00:00
|
|
|
from datetime import datetime, timedelta
|
2015-08-05 21:36:17 +00:00
|
|
|
from cachetools import lru_cache
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2017-07-06 18:50:30 +00:00
|
|
|
import data
|
2019-01-03 18:50:43 +00:00
|
|
|
from data.database import (LogEntry, LogEntryKind, User, RepositoryActionCount, db, LogEntry3,
|
|
|
|
LogEntry3)
|
2016-08-12 20:53:17 +00:00
|
|
|
from data.model import config, user, DataModelException
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2017-02-15 20:44:08 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2017-04-07 15:39:54 +00:00
|
|
|
ACTIONS_ALLOWED_WITHOUT_AUDIT_LOGGING = ['pull_repo']
|
|
|
|
|
2017-07-10 13:46:02 +00:00
|
|
|
|
2018-11-27 16:28:32 +00:00
|
|
|
def _logs_query(selections, start_time=None, end_time=None, performer=None, repository=None,
|
2019-01-03 18:50:43 +00:00
|
|
|
namespace=None, ignore=None, model=LogEntry3, id_range=None):
|
2018-05-18 16:54:38 +00:00
|
|
|
""" Returns a query for selecting logs from the table, with various options and filters. """
|
2019-01-03 18:50:43 +00:00
|
|
|
# TODO(LogMigrate): Remove the branch once we're back on a single table.
|
2018-11-27 16:28:32 +00:00
|
|
|
assert (start_time is not None and end_time is not None) or (id_range is not None)
|
|
|
|
joined = (model.select(*selections).switch(model))
|
|
|
|
|
|
|
|
if id_range is not None:
|
|
|
|
joined = joined.where(model.id >= id_range[0], model.id <= id_range[1])
|
|
|
|
else:
|
|
|
|
joined = joined.where(model.datetime >= start_time, model.datetime < end_time)
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
if repository:
|
2018-05-18 16:54:38 +00:00
|
|
|
joined = joined.where(model.repository == repository)
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
if performer:
|
2018-05-18 16:54:38 +00:00
|
|
|
joined = joined.where(model.performer == performer)
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2018-05-14 15:41:49 +00:00
|
|
|
if namespace and not repository:
|
2016-08-12 20:53:17 +00:00
|
|
|
namespace_user = user.get_user_or_org(namespace)
|
|
|
|
if namespace_user is None:
|
|
|
|
raise DataModelException('Invalid namespace requested')
|
|
|
|
|
2018-05-18 16:54:38 +00:00
|
|
|
joined = joined.where(model.account == namespace_user.id)
|
2015-07-31 17:38:02 +00:00
|
|
|
|
2016-04-26 19:16:55 +00:00
|
|
|
if ignore:
|
|
|
|
kind_map = get_log_entry_kinds()
|
|
|
|
ignore_ids = [kind_map[kind_name] for kind_name in ignore]
|
2018-05-18 16:54:38 +00:00
|
|
|
joined = joined.where(~(model.kind << ignore_ids))
|
2016-04-26 19:16:55 +00:00
|
|
|
|
2015-07-31 17:38:02 +00:00
|
|
|
return joined
|
|
|
|
|
|
|
|
|
2015-08-05 21:36:17 +00:00
|
|
|
@lru_cache(maxsize=1)
|
|
|
|
def get_log_entry_kinds():
|
|
|
|
kind_map = {}
|
|
|
|
for kind in LogEntryKind.select():
|
|
|
|
kind_map[kind.id] = kind.name
|
2016-04-26 19:16:55 +00:00
|
|
|
kind_map[kind.name] = kind.id
|
2015-08-05 21:36:17 +00:00
|
|
|
|
|
|
|
return kind_map
|
|
|
|
|
|
|
|
|
2016-05-13 19:20:55 +00:00
|
|
|
def _get_log_entry_kind(name):
|
|
|
|
kinds = get_log_entry_kinds()
|
|
|
|
return kinds[name]
|
|
|
|
|
|
|
|
|
2016-04-26 19:16:55 +00:00
|
|
|
def get_aggregated_logs(start_time, end_time, performer=None, repository=None, namespace=None,
|
2019-01-03 18:50:43 +00:00
|
|
|
ignore=None, model=LogEntry3):
|
2018-05-18 16:54:38 +00:00
|
|
|
""" Returns the count of logs, by kind and day, for the logs matching the given filters. """
|
2019-01-03 18:50:43 +00:00
|
|
|
# TODO(LogMigrate): Remove the branch once we're back on a single table.
|
2018-05-18 16:54:38 +00:00
|
|
|
date = db.extract_date('day', model.datetime)
|
|
|
|
selections = [model.kind, date.alias('day'), fn.Count(model.id).alias('count')]
|
|
|
|
query = _logs_query(selections, start_time, end_time, performer, repository, namespace, ignore,
|
|
|
|
model=model)
|
|
|
|
return query.group_by(date, model.kind)
|
2015-07-31 17:38:02 +00:00
|
|
|
|
|
|
|
|
2018-11-27 16:28:32 +00:00
|
|
|
def get_logs_query(start_time=None, end_time=None, performer=None, repository=None, namespace=None,
|
2019-01-03 18:50:43 +00:00
|
|
|
ignore=None, model=LogEntry3, id_range=None):
|
2018-05-18 16:54:38 +00:00
|
|
|
""" Returns the logs matching the given filters. """
|
2019-01-03 18:50:43 +00:00
|
|
|
# TODO(LogMigrate): Remove the branch once we're back on a single table.
|
2015-07-31 17:38:02 +00:00
|
|
|
Performer = User.alias()
|
2017-02-14 19:55:24 +00:00
|
|
|
Account = User.alias()
|
2018-05-18 16:54:38 +00:00
|
|
|
selections = [model, Performer]
|
2015-07-31 17:38:02 +00:00
|
|
|
|
2017-02-14 19:55:24 +00:00
|
|
|
if namespace is None and repository is None:
|
|
|
|
selections.append(Account)
|
|
|
|
|
2018-05-18 16:54:38 +00:00
|
|
|
query = _logs_query(selections, start_time, end_time, performer, repository, namespace, ignore,
|
2018-11-27 16:28:32 +00:00
|
|
|
model=model, id_range=id_range)
|
2018-04-06 17:48:01 +00:00
|
|
|
query = (query.switch(model).join(Performer, JOIN.LEFT_OUTER,
|
2018-11-27 16:28:32 +00:00
|
|
|
on=(model.performer == Performer.id).alias('performer')))
|
2015-07-31 17:38:02 +00:00
|
|
|
|
2017-02-14 19:55:24 +00:00
|
|
|
if namespace is None and repository is None:
|
2018-04-06 17:48:01 +00:00
|
|
|
query = (query.switch(model).join(Account, JOIN.LEFT_OUTER,
|
2018-11-27 16:28:32 +00:00
|
|
|
on=(model.account == Account.id).alias('account')))
|
2017-02-14 19:55:24 +00:00
|
|
|
|
2015-12-22 14:05:17 +00:00
|
|
|
return query
|
2015-07-15 21:25:41 +00:00
|
|
|
|
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
def _json_serialize(obj):
|
|
|
|
if isinstance(obj, datetime):
|
|
|
|
return timegm(obj.utctimetuple())
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
2017-07-10 13:46:02 +00:00
|
|
|
def log_action(kind_name, user_or_organization_name, performer=None, repository=None, ip=None,
|
|
|
|
metadata={}, timestamp=None):
|
2018-07-27 21:46:46 +00:00
|
|
|
""" Logs an entry in the LogEntry table. """
|
2015-07-15 21:25:41 +00:00
|
|
|
if not timestamp:
|
|
|
|
timestamp = datetime.today()
|
|
|
|
|
2016-04-01 17:55:29 +00:00
|
|
|
account = None
|
|
|
|
if user_or_organization_name is not None:
|
|
|
|
account = User.get(User.username == user_or_organization_name).id
|
2016-04-26 19:16:55 +00:00
|
|
|
else:
|
|
|
|
account = config.app_config.get('SERVICE_LOG_ACCOUNT_ID')
|
|
|
|
if account is None:
|
2019-01-21 21:33:32 +00:00
|
|
|
account = user.get_minimum_user_id()
|
2016-04-01 17:55:29 +00:00
|
|
|
|
2016-08-05 20:37:04 +00:00
|
|
|
if performer is not None:
|
|
|
|
performer = performer.id
|
|
|
|
|
|
|
|
if repository is not None:
|
2017-07-10 18:45:23 +00:00
|
|
|
repository = repository.id
|
2016-08-05 20:37:04 +00:00
|
|
|
|
2016-05-13 19:20:55 +00:00
|
|
|
kind = _get_log_entry_kind(kind_name)
|
2016-04-01 17:55:29 +00:00
|
|
|
metadata_json = json.dumps(metadata, default=_json_serialize)
|
2017-03-13 14:52:36 +00:00
|
|
|
log_data = {
|
|
|
|
'kind': kind,
|
|
|
|
'account': account,
|
|
|
|
'performer': performer,
|
|
|
|
'repository': repository,
|
|
|
|
'ip': ip,
|
|
|
|
'metadata_json': metadata_json,
|
|
|
|
'datetime': timestamp
|
|
|
|
}
|
2017-04-07 15:39:54 +00:00
|
|
|
|
2017-02-15 20:44:08 +00:00
|
|
|
try:
|
2019-01-03 18:50:43 +00:00
|
|
|
LogEntry3.create(**log_data)
|
2017-02-28 04:57:40 +00:00
|
|
|
except PeeweeException as ex:
|
2017-04-07 15:39:54 +00:00
|
|
|
strict_logging_disabled = config.app_config.get('ALLOW_PULLS_WITHOUT_STRICT_LOGGING')
|
|
|
|
if strict_logging_disabled and kind_name in ACTIONS_ALLOWED_WITHOUT_AUDIT_LOGGING:
|
2017-03-13 14:52:36 +00:00
|
|
|
logger.exception('log_action failed', extra=({'exception': ex}).update(log_data))
|
2017-02-15 20:44:08 +00:00
|
|
|
else:
|
|
|
|
raise
|
|
|
|
|
2015-07-15 21:25:41 +00:00
|
|
|
|
2018-05-18 16:54:38 +00:00
|
|
|
def get_stale_logs_start_id(model):
|
2015-10-09 19:41:56 +00:00
|
|
|
""" Gets the oldest log entry. """
|
2019-01-03 18:50:43 +00:00
|
|
|
# TODO(LogMigrate): Remove the branch once we're back on a single table.
|
2015-10-09 19:41:56 +00:00
|
|
|
try:
|
2018-05-18 16:54:38 +00:00
|
|
|
return (model.select(model.id).order_by(model.id).limit(1).tuples())[0][0]
|
2015-10-09 19:41:56 +00:00
|
|
|
except IndexError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-05-18 16:54:38 +00:00
|
|
|
def get_stale_logs_cutoff_id(cutoff_date, model):
|
2015-10-09 19:41:56 +00:00
|
|
|
""" Gets the most recent ID created before the cutoff_date. """
|
2019-01-03 18:50:43 +00:00
|
|
|
# TODO(LogMigrate): Remove the branch once we're back on a single table.
|
2015-10-09 19:41:56 +00:00
|
|
|
try:
|
2018-05-18 16:54:38 +00:00
|
|
|
return (model.select(fn.Max(model.id)).where(model.datetime <= cutoff_date)
|
2015-10-09 19:41:56 +00:00
|
|
|
.tuples())[0][0]
|
|
|
|
except IndexError:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-05-18 16:54:38 +00:00
|
|
|
def get_stale_logs(start_id, end_id, model):
|
2015-10-09 19:41:56 +00:00
|
|
|
""" Returns all the logs with IDs between start_id and end_id inclusively. """
|
2019-01-03 18:50:43 +00:00
|
|
|
# TODO(LogMigrate): Remove the branch once we're back on a single table.
|
2018-05-18 16:54:38 +00:00
|
|
|
return model.select().where((model.id >= start_id), (model.id <= end_id))
|
2015-10-09 19:41:56 +00:00
|
|
|
|
|
|
|
|
2018-05-18 16:54:38 +00:00
|
|
|
def delete_stale_logs(start_id, end_id, model):
|
2015-10-09 19:41:56 +00:00
|
|
|
""" Deletes all the logs with IDs between start_id and end_id. """
|
2019-01-03 18:50:43 +00:00
|
|
|
# TODO(LogMigrate): Remove the branch once we're back on a single table.
|
2018-05-18 16:54:38 +00:00
|
|
|
model.delete().where((model.id >= start_id), (model.id <= end_id)).execute()
|
2016-06-22 18:50:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_repository_action_counts(repo, start_date):
|
2018-05-18 16:54:38 +00:00
|
|
|
""" Returns the daily aggregated action counts for the given repository, starting at the given
|
|
|
|
start date.
|
|
|
|
"""
|
2016-06-22 18:50:59 +00:00
|
|
|
return RepositoryActionCount.select().where(RepositoryActionCount.repository == repo,
|
|
|
|
RepositoryActionCount.date >= start_date)
|
|
|
|
|
|
|
|
|
|
|
|
def get_repositories_action_sums(repository_ids):
|
2018-05-18 16:54:38 +00:00
|
|
|
""" Returns a map from repository ID to total actions within that repository in the last week. """
|
2016-06-22 18:50:59 +00:00
|
|
|
if not repository_ids:
|
|
|
|
return {}
|
|
|
|
|
|
|
|
# Filter the join to recent entries only.
|
|
|
|
last_week = datetime.now() - timedelta(weeks=1)
|
2017-07-10 13:46:02 +00:00
|
|
|
tuples = (RepositoryActionCount.select(RepositoryActionCount.repository,
|
|
|
|
fn.Sum(RepositoryActionCount.count))
|
2016-06-22 18:50:59 +00:00
|
|
|
.where(RepositoryActionCount.repository << repository_ids)
|
|
|
|
.where(RepositoryActionCount.date >= last_week)
|
2017-07-10 13:46:02 +00:00
|
|
|
.group_by(RepositoryActionCount.repository).tuples())
|
2016-06-22 18:50:59 +00:00
|
|
|
|
|
|
|
action_count_map = {}
|
|
|
|
for record in tuples:
|
|
|
|
action_count_map[record[0]] = record[1]
|
|
|
|
|
|
|
|
return action_count_map
|
2018-11-27 16:28:32 +00:00
|
|
|
|
|
|
|
|
2019-01-03 18:50:43 +00:00
|
|
|
def get_minimum_id_for_logs(start_time, repository_id=None, namespace_id=None, model=LogEntry3):
|
2018-11-27 16:28:32 +00:00
|
|
|
""" Returns the minimum ID for logs matching the given repository or namespace in
|
|
|
|
the logs table, starting at the given start time.
|
|
|
|
"""
|
|
|
|
# First try bounded by a day. Most repositories will meet this criteria, and therefore
|
|
|
|
# can make a much faster query.
|
|
|
|
day_after = start_time + timedelta(days=1)
|
2019-01-02 20:57:55 +00:00
|
|
|
result = _get_bounded_id(fn.Min, model.datetime >= start_time,
|
|
|
|
repository_id, namespace_id, model.datetime < day_after, model=model)
|
2018-11-27 16:28:32 +00:00
|
|
|
if result is not None:
|
|
|
|
return result
|
|
|
|
|
2019-01-02 20:57:55 +00:00
|
|
|
return _get_bounded_id(fn.Min, model.datetime >= start_time, repository_id, namespace_id,
|
|
|
|
model=model)
|
2018-11-27 16:28:32 +00:00
|
|
|
|
|
|
|
|
2019-01-03 18:50:43 +00:00
|
|
|
def get_maximum_id_for_logs(end_time, repository_id=None, namespace_id=None, model=LogEntry3):
|
2018-11-27 16:28:32 +00:00
|
|
|
""" Returns the maximum ID for logs matching the given repository or namespace in
|
|
|
|
the logs table, ending at the given end time.
|
|
|
|
"""
|
|
|
|
# First try bounded by a day. Most repositories will meet this criteria, and therefore
|
|
|
|
# can make a much faster query.
|
|
|
|
day_before = end_time - timedelta(days=1)
|
2019-01-02 20:57:55 +00:00
|
|
|
result = _get_bounded_id(fn.Max, model.datetime <= end_time,
|
|
|
|
repository_id, namespace_id, model.datetime > day_before, model=model)
|
2018-11-27 16:28:32 +00:00
|
|
|
if result is not None:
|
|
|
|
return result
|
|
|
|
|
2019-01-02 20:57:55 +00:00
|
|
|
return _get_bounded_id(fn.Max, model.datetime <= end_time, repository_id, namespace_id,
|
|
|
|
model=model)
|
2018-11-27 16:28:32 +00:00
|
|
|
|
|
|
|
|
2019-01-02 20:57:55 +00:00
|
|
|
def _get_bounded_id(fn, filter_clause, repository_id, namespace_id, reduction_clause=None,
|
2019-01-03 18:50:43 +00:00
|
|
|
model=LogEntry3):
|
2018-11-27 16:28:32 +00:00
|
|
|
assert (namespace_id is not None) or (repository_id is not None)
|
2019-01-02 20:57:55 +00:00
|
|
|
query = (model
|
|
|
|
.select(fn(model.id))
|
2018-11-27 16:28:32 +00:00
|
|
|
.where(filter_clause))
|
|
|
|
|
|
|
|
if reduction_clause is not None:
|
|
|
|
query = query.where(reduction_clause)
|
|
|
|
|
|
|
|
if repository_id is not None:
|
2019-01-02 20:57:55 +00:00
|
|
|
query = query.where(model.repository == repository_id)
|
2018-11-27 16:28:32 +00:00
|
|
|
else:
|
2019-01-02 20:57:55 +00:00
|
|
|
query = query.where(model.account == namespace_id)
|
2018-11-27 16:28:32 +00:00
|
|
|
|
|
|
|
row = query.tuples()[0]
|
|
|
|
if not row:
|
|
|
|
return None
|
|
|
|
|
|
|
|
return row[0]
|