2014-03-14 20:02:13 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
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,
|
2014-03-19 16:09:07 +00:00
|
|
|
format_date, Unauthorized, NotFound, require_user_admin,
|
|
|
|
internal_only)
|
2014-03-14 20:02:13 +00:00
|
|
|
from auth.permissions import AdministerOrganizationPermission, AdministerOrganizationPermission
|
|
|
|
from auth.auth_context import get_authenticated_user
|
|
|
|
from data import model
|
|
|
|
|
|
|
|
|
|
|
|
def log_view(log):
|
|
|
|
view = {
|
|
|
|
'kind': log.kind.name,
|
|
|
|
'metadata': json.loads(log.metadata_json),
|
|
|
|
'ip': log.ip,
|
2014-03-17 17:10:12 +00:00
|
|
|
'datetime': format_date(log.datetime),
|
2014-03-14 20:02:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if log.performer:
|
|
|
|
view['performer'] = {
|
|
|
|
'kind': 'user',
|
|
|
|
'name': log.performer.username,
|
|
|
|
'is_robot': log.performer.robot,
|
|
|
|
}
|
|
|
|
|
|
|
|
return view
|
|
|
|
|
|
|
|
|
2014-04-10 04:26:55 +00:00
|
|
|
def get_logs(start_time, end_time, performer_name=None, repository=None, namespace=None):
|
2014-03-14 20:02:13 +00:00
|
|
|
performer = None
|
|
|
|
if performer_name:
|
|
|
|
performer = model.get_user(performer_name)
|
|
|
|
|
|
|
|
if start_time:
|
|
|
|
try:
|
|
|
|
start_time = datetime.strptime(start_time + ' UTC', '%m/%d/%Y %Z')
|
|
|
|
except ValueError:
|
|
|
|
start_time = None
|
|
|
|
|
|
|
|
if not start_time:
|
|
|
|
start_time = datetime.today() - timedelta(7) # One week
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if not end_time:
|
|
|
|
end_time = datetime.today()
|
|
|
|
|
2014-04-10 04:26:55 +00:00
|
|
|
logs = model.list_logs(start_time, end_time, performer=performer, repository=repository,
|
|
|
|
namespace=namespace)
|
2014-03-14 20:02:13 +00:00
|
|
|
return {
|
2014-03-17 17:10:12 +00:00
|
|
|
'start_time': format_date(start_time),
|
|
|
|
'end_time': format_date(end_time),
|
2014-03-14 20:02:13 +00:00
|
|
|
'logs': [log_view(log) for log in logs]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-19 19:39:44 +00:00
|
|
|
@resource('/v1/repository/<repopath:repository>/logs')
|
2014-03-19 16:09:07 +00:00
|
|
|
@internal_only
|
2014-03-14 20:02:13 +00:00
|
|
|
class RepositoryLogs(RepositoryParamResource):
|
|
|
|
""" Resource for fetching logs for the specific repository. """
|
|
|
|
@require_repo_admin
|
|
|
|
@nickname('listRepoLogs')
|
|
|
|
@parse_args
|
|
|
|
@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)
|
|
|
|
def get(self, args, namespace, repository):
|
|
|
|
""" List the logs for the specified repository. """
|
|
|
|
repo = model.get_repository(namespace, repository)
|
|
|
|
if not repo:
|
2014-03-17 20:57:35 +00:00
|
|
|
raise NotFound()
|
2014-03-14 20:02:13 +00:00
|
|
|
|
|
|
|
start_time = args['starttime']
|
|
|
|
end_time = args['endtime']
|
2014-04-10 04:26:55 +00:00
|
|
|
return get_logs(start_time, end_time, repository=repo, namespace=namespace)
|
2014-03-14 20:02:13 +00:00
|
|
|
|
|
|
|
|
2014-03-14 21:37:57 +00:00
|
|
|
@resource('/v1/user/logs')
|
2014-03-19 16:09:07 +00:00
|
|
|
@internal_only
|
2014-03-14 21:37:57 +00:00
|
|
|
class UserLogs(ApiResource):
|
|
|
|
""" Resource for fetching logs for the current user. """
|
2014-03-18 23:21:27 +00:00
|
|
|
@require_user_admin
|
2014-03-14 21:37:57 +00:00
|
|
|
@nickname('listUserLogs')
|
|
|
|
@parse_args
|
|
|
|
@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)
|
|
|
|
def get(self, args):
|
|
|
|
""" List the logs for the current user. """
|
|
|
|
performer_name = args['performer']
|
|
|
|
start_time = args['starttime']
|
|
|
|
end_time = args['endtime']
|
|
|
|
|
2014-03-18 19:58:37 +00:00
|
|
|
user = get_authenticated_user()
|
2014-04-10 04:26:55 +00:00
|
|
|
return get_logs(start_time, end_time, performer_name=performer_name, namespace=user.username)
|
2014-03-14 21:37:57 +00:00
|
|
|
|
|
|
|
|
2014-03-14 20:02:13 +00:00
|
|
|
@resource('/v1/organization/<orgname>/logs')
|
2014-03-19 16:09:07 +00:00
|
|
|
@internal_only
|
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. """
|
|
|
|
@nickname('listOrgLogs')
|
|
|
|
@parse_args
|
|
|
|
@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)
|
|
|
|
def get(self, args, orgname):
|
|
|
|
""" List the logs for the specified organization. """
|
|
|
|
permission = AdministerOrganizationPermission(orgname)
|
|
|
|
if permission.can():
|
|
|
|
performer_name = args['performer']
|
|
|
|
start_time = args['starttime']
|
|
|
|
end_time = args['endtime']
|
|
|
|
|
2014-04-10 04:26:55 +00:00
|
|
|
return get_logs(start_time, end_time, namespace=orgname, performer_name=performer_name)
|
2014-03-14 20:02:13 +00:00
|
|
|
|
2014-04-10 04:26:55 +00:00
|
|
|
raise Unauthorized()
|