audit-logs: add push/pull actions to audit logs.
This commit is contained in:
parent
9473470c8a
commit
664cbc6f52
3 changed files with 41 additions and 3 deletions
|
|
@ -22,7 +22,7 @@ import logging
|
|||
import datetime
|
||||
|
||||
from rhodecode.model import meta
|
||||
from rhodecode.model.db import User, UserLog
|
||||
from rhodecode.model.db import User, UserLog, Repository
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
|
@ -33,6 +33,8 @@ ACTIONS = {
|
|||
'user.login.failure': {},
|
||||
'user.logout': {},
|
||||
'user.password.reset_request': {},
|
||||
'user.push': {},
|
||||
'user.pull': {},
|
||||
|
||||
'repo.add': {},
|
||||
'repo.edit': {},
|
||||
|
|
@ -52,6 +54,16 @@ class UserWrap(object):
|
|||
self.ip_addr = ip_addr
|
||||
|
||||
|
||||
class RepoWrap(object):
|
||||
"""
|
||||
Fake object used to imitate RepoObject that audit logger requires
|
||||
"""
|
||||
|
||||
def __init__(self, repo_id=None, repo_name=None):
|
||||
self.repo_id = repo_id
|
||||
self.repo_name = repo_name
|
||||
|
||||
|
||||
def _store_log(action_name, action_data, user_id, username, user_data,
|
||||
ip_address, repository_id, repository_name):
|
||||
user_log = UserLog()
|
||||
|
|
@ -127,8 +139,13 @@ def store(
|
|||
'email': user.email,
|
||||
}
|
||||
|
||||
repository_id = getattr(repo, 'repo_id', None)
|
||||
repository_name = getattr(repo, 'repo_name', None)
|
||||
repository_id = getattr(repo, 'repo_id', None)
|
||||
if not repository_id:
|
||||
# maybe we have repo_name ? Try to figure repo_id from repo_name
|
||||
if repository_name:
|
||||
repository_id = getattr(
|
||||
Repository.get_by_repo_name(repository_name), 'repo_id', None)
|
||||
|
||||
user_log = _store_log(
|
||||
action_name=safe_unicode(action),
|
||||
|
|
|
|||
|
|
@ -30,6 +30,7 @@ import logging
|
|||
import rhodecode
|
||||
from rhodecode import events
|
||||
from rhodecode.lib import helpers as h
|
||||
from rhodecode.lib import audit_logger
|
||||
from rhodecode.lib.utils import action_logger
|
||||
from rhodecode.lib.utils2 import safe_str
|
||||
from rhodecode.lib.exceptions import HTTPLockedRC, UserCreationError
|
||||
|
|
@ -156,6 +157,14 @@ def post_pull(extras):
|
|||
action = 'pull'
|
||||
action_logger(user, action, extras.repository, extras.ip, commit=True)
|
||||
|
||||
audit_user = audit_logger.UserWrap(
|
||||
username=extras.username,
|
||||
ip_addr=extras.ip)
|
||||
audit_logger.store(
|
||||
action='user.pull', action_data={
|
||||
'user_agent': extras.user_agent},
|
||||
user=audit_user, commit=True)
|
||||
|
||||
# Propagate to external components.
|
||||
if not is_shadow_repo(extras):
|
||||
post_pull_extension(**extras)
|
||||
|
|
@ -192,6 +201,16 @@ def post_push(extras):
|
|||
action_logger(
|
||||
extras.username, action, extras.repository, extras.ip, commit=True)
|
||||
|
||||
audit_user = audit_logger.UserWrap(
|
||||
username=extras.username,
|
||||
ip_addr=extras.ip)
|
||||
repo = audit_logger.RepoWrap(repo_name=extras.repository)
|
||||
audit_logger.store(
|
||||
action='user.push', action_data={
|
||||
'user_agent': extras.user_agent,
|
||||
'commit_ids': commit_ids[:10000]},
|
||||
user=audit_user, repo=repo, commit=True)
|
||||
|
||||
# Propagate to external components.
|
||||
if not is_shadow_repo(extras):
|
||||
post_push_extension(
|
||||
|
|
|
|||
|
|
@ -537,7 +537,9 @@ class TestPullrequestsController:
|
|||
pull_request, ChangesetStatus.STATUS_APPROVED)
|
||||
|
||||
# Check the relevant log entries were added
|
||||
user_logs = UserLog.query().order_by('-user_log_id').limit(4)
|
||||
user_logs = UserLog.query() \
|
||||
.filter(UserLog.version == UserLog.VERSION_1) \
|
||||
.order_by('-user_log_id').limit(4)
|
||||
actions = [log.action for log in user_logs]
|
||||
pr_commit_ids = PullRequestModel()._get_commit_ids(pull_request)
|
||||
expected_actions = [
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue