2015-05-14 21:15:39 +00:00
|
|
|
""" Access usage logs for organizations or repositories. """
|
2015-05-14 20:47:38 +00:00
|
|
|
|
2014-03-14 20:02:13 +00:00
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
|
|
from endpoints.api import (resource, nickname, ApiResource, query_param, parse_args,
|
2014-03-17 17:10:12 +00:00
|
|
|
RepositoryParamResource, require_repo_admin, related_user_resource,
|
2016-04-11 20:20:11 +00:00
|
|
|
format_date, require_user_admin, path_param, require_scope, page_support)
|
2017-07-18 15:58:42 +00:00
|
|
|
from endpoints.api.logs_models_pre_oci import pre_oci_model as model
|
2016-04-11 20:20:11 +00:00
|
|
|
from endpoints.exception import Unauthorized, NotFound
|
2017-02-09 00:52:17 +00:00
|
|
|
from auth.permissions import AdministerOrganizationPermission
|
2014-03-14 20:02:13 +00:00
|
|
|
from auth.auth_context import get_authenticated_user
|
2014-08-19 23:21:41 +00:00
|
|
|
from auth import scopes
|
2014-03-14 20:02:13 +00:00
|
|
|
|
2015-12-22 14:05:17 +00:00
|
|
|
LOGS_PER_PAGE = 20
|
2016-04-26 19:16:55 +00:00
|
|
|
SERVICE_LEVEL_LOG_KINDS = set(['service_key_create', 'service_key_approve', 'service_key_delete',
|
|
|
|
'service_key_modify', 'service_key_extend', 'service_key_rotate'])
|
2014-03-14 20:02:13 +00:00
|
|
|
|
|
|
|
|
2017-07-18 15:58:42 +00:00
|
|
|
def _validate_logs_arguments(start_time, end_time):
|
2014-03-14 20:02:13 +00:00
|
|
|
if start_time:
|
|
|
|
try:
|
|
|
|
start_time = datetime.strptime(start_time + ' UTC', '%m/%d/%Y %Z')
|
|
|
|
except ValueError:
|
|
|
|
start_time = None
|
|
|
|
|
2014-11-24 21:07:38 +00:00
|
|
|
if not start_time:
|
2017-07-18 15:58:42 +00:00
|
|
|
start_time = datetime.today() - timedelta(7) # One week
|
2014-03-14 20:02:13 +00:00
|
|
|
|
|
|
|
if end_time:
|
|
|
|
try:
|
|
|
|
end_time = datetime.strptime(end_time + ' UTC', '%m/%d/%Y %Z')
|
|
|
|
end_time = end_time + timedelta(days=1)
|
|
|
|
except ValueError:
|
|
|
|
end_time = None
|
2014-11-24 21:07:38 +00:00
|
|
|
|
|
|
|
if not end_time:
|
2014-03-14 20:02:13 +00:00
|
|
|
end_time = datetime.today()
|
|
|
|
|
2017-07-18 15:58:42 +00:00
|
|
|
return start_time, end_time
|
2015-07-31 17:38:02 +00:00
|
|
|
|
|
|
|
|
2017-07-18 15:58:42 +00:00
|
|
|
def get_logs(start_time, end_time, performer_name=None, repository_name=None, namespace_name=None,
|
2016-04-26 19:16:55 +00:00
|
|
|
page_token=None, ignore=None):
|
2017-07-18 15:58:42 +00:00
|
|
|
(start_time, end_time) = _validate_logs_arguments(start_time, end_time)
|
|
|
|
|
|
|
|
kinds = model.get_log_entry_kinds()
|
|
|
|
log_entry_page = model.get_logs_query(start_time, end_time, performer_name, repository_name,
|
|
|
|
namespace_name, ignore, page_token)
|
2015-12-22 14:05:17 +00:00
|
|
|
|
2017-07-18 15:58:42 +00:00
|
|
|
include_namespace = namespace_name is None and repository_name is None
|
2015-07-31 17:38:02 +00:00
|
|
|
|
2014-03-14 20:02:13 +00:00
|
|
|
return {
|
2017-07-18 15:58:42 +00:00
|
|
|
'start_time': format_date(start_time),
|
|
|
|
'end_time': format_date(end_time),
|
|
|
|
'logs': [log.to_dict(kinds, include_namespace) for log in log_entry_page.logs],
|
|
|
|
}, log_entry_page.next_page_token
|
2015-12-22 14:05:17 +00:00
|
|
|
|
2015-07-31 17:38:02 +00:00
|
|
|
|
2016-04-26 19:16:55 +00:00
|
|
|
def get_aggregate_logs(start_time, end_time, performer_name=None, repository=None, namespace=None,
|
|
|
|
ignore=None):
|
2017-07-18 15:58:42 +00:00
|
|
|
(start_time, end_time) = _validate_logs_arguments(start_time, end_time)
|
2015-07-31 17:38:02 +00:00
|
|
|
|
2017-07-18 15:58:42 +00:00
|
|
|
kinds = model.get_log_entry_kinds()
|
|
|
|
aggregated_logs = model.get_aggregated_logs(start_time, end_time, performer_name=performer_name,
|
|
|
|
repository_name=repository, namespace_name=namespace,
|
|
|
|
ignore=ignore)
|
2015-07-31 17:38:02 +00:00
|
|
|
|
|
|
|
return {
|
2017-07-18 15:58:42 +00:00
|
|
|
'aggregated': [log.to_dict(kinds, start_time) for log in aggregated_logs]
|
2014-03-14 20:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/logs')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
2014-03-14 20:02:13 +00:00
|
|
|
class RepositoryLogs(RepositoryParamResource):
|
|
|
|
""" Resource for fetching logs for the specific repository. """
|
2017-07-18 15:58:42 +00:00
|
|
|
|
2014-03-14 20:02:13 +00:00
|
|
|
@require_repo_admin
|
|
|
|
@nickname('listRepoLogs')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2014-03-14 20:02:13 +00:00
|
|
|
@query_param('starttime', 'Earliest time from which to get logs (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('endtime', 'Latest time to which to get logs (%m/%d/%Y %Z)', type=str)
|
2015-07-31 17:38:02 +00:00
|
|
|
@query_param('page', 'The page number for the logs', type=int, default=1)
|
2016-01-26 21:27:36 +00:00
|
|
|
@page_support()
|
|
|
|
def get(self, namespace, repository, page_token, parsed_args):
|
2014-03-14 20:02:13 +00:00
|
|
|
""" List the logs for the specified repository. """
|
2017-07-18 15:58:42 +00:00
|
|
|
if model.repo_exists(namespace, repository) is False:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|
2014-03-14 20:02:13 +00:00
|
|
|
|
2016-01-26 21:27:36 +00:00
|
|
|
start_time = parsed_args['starttime']
|
|
|
|
end_time = parsed_args['endtime']
|
2017-07-18 15:58:42 +00:00
|
|
|
return get_logs(start_time, end_time, repository_name=repository, page_token=page_token, namespace_name=namespace,
|
2016-04-26 19:16:55 +00:00
|
|
|
ignore=SERVICE_LEVEL_LOG_KINDS)
|
2014-03-14 20:02:13 +00:00
|
|
|
|
|
|
|
|
2014-03-14 21:37:57 +00:00
|
|
|
@resource('/v1/user/logs')
|
|
|
|
class UserLogs(ApiResource):
|
|
|
|
""" Resource for fetching logs for the current user. """
|
2017-07-18 15:58:42 +00:00
|
|
|
|
2014-03-18 23:21:27 +00:00
|
|
|
@require_user_admin
|
2014-03-14 21:37:57 +00:00
|
|
|
@nickname('listUserLogs')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2014-03-14 21:37:57 +00:00
|
|
|
@query_param('starttime', 'Earliest time from which to get logs. (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('endtime', 'Latest time to which to get logs. (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('performer', 'Username for which to filter logs.', type=str)
|
2016-01-26 21:27:36 +00:00
|
|
|
@page_support()
|
|
|
|
def get(self, parsed_args, page_token):
|
2014-03-14 21:37:57 +00:00
|
|
|
""" List the logs for the current user. """
|
2016-01-26 21:27:36 +00:00
|
|
|
performer_name = parsed_args['performer']
|
|
|
|
start_time = parsed_args['starttime']
|
|
|
|
end_time = parsed_args['endtime']
|
2014-03-14 21:37:57 +00:00
|
|
|
|
2014-03-18 19:58:37 +00:00
|
|
|
user = get_authenticated_user()
|
2017-07-18 15:58:42 +00:00
|
|
|
return get_logs(start_time, end_time, performer_name=performer_name, namespace_name=user.username,
|
2016-04-26 19:16:55 +00:00
|
|
|
page_token=page_token, ignore=SERVICE_LEVEL_LOG_KINDS)
|
2014-03-14 21:37:57 +00:00
|
|
|
|
|
|
|
|
2014-03-14 20:02:13 +00:00
|
|
|
@resource('/v1/organization/<orgname>/logs')
|
2014-08-19 23:05:28 +00:00
|
|
|
@path_param('orgname', 'The name of the organization')
|
2014-03-14 21:35:52 +00:00
|
|
|
@related_user_resource(UserLogs)
|
2014-03-14 20:02:13 +00:00
|
|
|
class OrgLogs(ApiResource):
|
|
|
|
""" Resource for fetching logs for the entire organization. """
|
2017-07-18 15:58:42 +00:00
|
|
|
|
2014-03-14 20:02:13 +00:00
|
|
|
@nickname('listOrgLogs')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2014-03-14 20:02:13 +00:00
|
|
|
@query_param('starttime', 'Earliest time from which to get logs. (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('endtime', 'Latest time to which to get logs. (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('performer', 'Username for which to filter logs.', type=str)
|
2015-07-31 17:38:02 +00:00
|
|
|
@query_param('page', 'The page number for the logs', type=int, default=1)
|
2016-01-26 21:27:36 +00:00
|
|
|
@page_support()
|
2014-08-19 23:21:41 +00:00
|
|
|
@require_scope(scopes.ORG_ADMIN)
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, orgname, page_token, parsed_args):
|
2014-03-14 20:02:13 +00:00
|
|
|
""" List the logs for the specified organization. """
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
2016-01-26 21:27:36 +00:00
|
|
|
performer_name = parsed_args['performer']
|
|
|
|
start_time = parsed_args['starttime']
|
|
|
|
end_time = parsed_args['endtime']
|
2014-03-14 20:02:13 +00:00
|
|
|
|
2017-07-18 15:58:42 +00:00
|
|
|
return get_logs(start_time, end_time, namespace_name=orgname, performer_name=performer_name,
|
2016-04-26 19:16:55 +00:00
|
|
|
page_token=page_token, ignore=SERVICE_LEVEL_LOG_KINDS)
|
2015-07-31 17:38:02 +00:00
|
|
|
|
|
|
|
raise Unauthorized()
|
|
|
|
|
|
|
|
|
2016-01-21 20:40:51 +00:00
|
|
|
@resource('/v1/repository/<apirepopath:repository>/aggregatelogs')
|
2015-07-31 17:38:02 +00:00
|
|
|
@path_param('repository', 'The full path of the repository. e.g. namespace/name')
|
|
|
|
class RepositoryAggregateLogs(RepositoryParamResource):
|
|
|
|
""" Resource for fetching aggregated logs for the specific repository. """
|
2017-07-18 15:58:42 +00:00
|
|
|
|
2015-07-31 17:38:02 +00:00
|
|
|
@require_repo_admin
|
|
|
|
@nickname('getAggregateRepoLogs')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2015-07-31 17:38:02 +00:00
|
|
|
@query_param('starttime', 'Earliest time from which to get logs (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('endtime', 'Latest time to which to get logs (%m/%d/%Y %Z)', type=str)
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, namespace, repository, parsed_args):
|
2015-07-31 17:38:02 +00:00
|
|
|
""" Returns the aggregated logs for the specified repository. """
|
2017-07-18 15:58:42 +00:00
|
|
|
if model.repo_exists(namespace, repository) is False:
|
2015-07-31 17:38:02 +00:00
|
|
|
raise NotFound()
|
|
|
|
|
2016-01-26 21:27:36 +00:00
|
|
|
start_time = parsed_args['starttime']
|
|
|
|
end_time = parsed_args['endtime']
|
2017-07-18 15:58:42 +00:00
|
|
|
return get_aggregate_logs(start_time, end_time, repository=repository, namespace=namespace,
|
2016-04-26 19:16:55 +00:00
|
|
|
ignore=SERVICE_LEVEL_LOG_KINDS)
|
2015-07-31 17:38:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/user/aggregatelogs')
|
|
|
|
class UserAggregateLogs(ApiResource):
|
|
|
|
""" Resource for fetching aggregated logs for the current user. """
|
2017-07-18 15:58:42 +00:00
|
|
|
|
2015-07-31 17:38:02 +00:00
|
|
|
@require_user_admin
|
|
|
|
@nickname('getAggregateUserLogs')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2015-07-31 17:38:02 +00:00
|
|
|
@query_param('starttime', 'Earliest time from which to get logs. (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('endtime', 'Latest time to which to get logs. (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('performer', 'Username for which to filter logs.', type=str)
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, parsed_args):
|
2015-07-31 17:38:02 +00:00
|
|
|
""" Returns the aggregated logs for the current user. """
|
2016-01-26 21:27:36 +00:00
|
|
|
performer_name = parsed_args['performer']
|
|
|
|
start_time = parsed_args['starttime']
|
|
|
|
end_time = parsed_args['endtime']
|
2015-07-31 17:38:02 +00:00
|
|
|
|
|
|
|
user = get_authenticated_user()
|
|
|
|
return get_aggregate_logs(start_time, end_time, performer_name=performer_name,
|
2016-05-04 19:20:27 +00:00
|
|
|
namespace=user.username, ignore=SERVICE_LEVEL_LOG_KINDS)
|
2015-07-31 17:38:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
@resource('/v1/organization/<orgname>/aggregatelogs')
|
|
|
|
@path_param('orgname', 'The name of the organization')
|
|
|
|
@related_user_resource(UserLogs)
|
|
|
|
class OrgAggregateLogs(ApiResource):
|
|
|
|
""" Resource for fetching aggregate logs for the entire organization. """
|
2017-07-18 15:58:42 +00:00
|
|
|
|
2015-07-31 17:38:02 +00:00
|
|
|
@nickname('getAggregateOrgLogs')
|
2016-01-26 21:27:36 +00:00
|
|
|
@parse_args()
|
2015-07-31 17:38:02 +00:00
|
|
|
@query_param('starttime', 'Earliest time from which to get logs. (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('endtime', 'Latest time to which to get logs. (%m/%d/%Y %Z)', type=str)
|
|
|
|
@query_param('performer', 'Username for which to filter logs.', type=str)
|
|
|
|
@require_scope(scopes.ORG_ADMIN)
|
2016-01-26 21:27:36 +00:00
|
|
|
def get(self, orgname, parsed_args):
|
2015-07-31 17:38:02 +00:00
|
|
|
""" Gets the aggregated logs for the specified organization. """
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
2016-01-26 21:27:36 +00:00
|
|
|
performer_name = parsed_args['performer']
|
|
|
|
start_time = parsed_args['starttime']
|
|
|
|
end_time = parsed_args['endtime']
|
2015-07-31 17:38:02 +00:00
|
|
|
|
|
|
|
return get_aggregate_logs(start_time, end_time, namespace=orgname,
|
2016-04-26 19:16:55 +00:00
|
|
|
performer_name=performer_name, ignore=SERVICE_LEVEL_LOG_KINDS)
|
2014-03-14 20:02:13 +00:00
|
|
|
|
2014-04-10 04:26:55 +00:00
|
|
|
raise Unauthorized()
|