user-sessions: added an API call to cleanup sessions.

This commit is contained in:
Marcin Kuzminski 2017-01-30 11:17:42 +01:00
parent 452fb50caf
commit ad2b740b5b
3 changed files with 121 additions and 4 deletions

View file

@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2017-2017 RhodeCode GmbH
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License, version 3
# (only), as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This program is dual-licensed. If you wish to learn more about the
# RhodeCode Enterprise Edition, including its added features, Support services,
# and proprietary license terms, please see https://rhodecode.com/licenses/
import mock
import pytest
from rhodecode.lib.user_sessions import FileAuthSessions
from rhodecode.api.tests.utils import (
build_data, api_call, assert_ok, assert_error, crash)
@pytest.mark.usefixtures("testuser_api", "app")
class TestCleanupSessions(object):
def test_api_cleanup_sessions(self):
id_, params = build_data(self.apikey, 'cleanup_sessions')
response = api_call(self.app, params)
expected = {'backend': 'file sessions', 'sessions_removed': 0}
assert_ok(id_, expected, given=response.body)
@mock.patch.object(FileAuthSessions, 'clean_sessions', crash)
def test_api_cleanup_error(self):
id_, params = build_data(self.apikey, 'cleanup_sessions', )
response = api_call(self.app, params)
expected = 'Error occurred during session cleanup'
assert_error(id_, expected, given=response.body)

View file

@ -26,6 +26,8 @@ from rhodecode.api import jsonrpc_method, JSONRPCError, JSONRPCForbidden
from rhodecode.api.utils import (
Optional, OAttr, has_superadmin_permission, get_user_or_error)
from rhodecode.lib.utils import repo2db_mapper
from rhodecode.lib import system_info
from rhodecode.lib import user_sessions
from rhodecode.model.db import UserIpMap
from rhodecode.model.scm import ScmModel
@ -176,3 +178,67 @@ def rescan_repos(request, apiuser, remove_obsolete=Optional(False)):
'Error occurred during rescan repositories action'
)
@jsonrpc_method()
def cleanup_sessions(request, apiuser, older_then=Optional(60)):
"""
Triggers a session cleanup action.
If the ``older_then`` option is set, only sessions that hasn't been
accessed in the given number of days will be removed.
This command can only be run using an |authtoken| with admin rights to
the specified repository.
This command takes the following options:
:param apiuser: This is filled automatically from the |authtoken|.
:type apiuser: AuthUser
:param older_then: Deletes session that hasn't been accessed
in given number of days.
:type older_then: Optional(int)
Example output:
.. code-block:: bash
id : <id_given_in_input>
result: {
"backend": "<type of backend>",
"sessions_removed": <number_of_removed_sessions>
}
error : null
Example error output:
.. code-block:: bash
id : <id_given_in_input>
result : null
error : {
'Error occurred during session cleanup'
}
"""
if not has_superadmin_permission(apiuser):
raise JSONRPCForbidden()
older_then = Optional.extract(older_then)
older_than_seconds = 60 * 60 * 24 * older_then
config = system_info.rhodecode_config().get_value()['value']['config']
session_model = user_sessions.get_session_handler(
config.get('beaker.session.type', 'memory'))(config)
backend = session_model.SESSION_TYPE
try:
cleaned = session_model.clean_sessions(
older_than_seconds=older_than_seconds)
return {'sessions_removed': cleaned, 'backend': backend}
except user_sessions.CleanupCommand as msg:
return {'cleanup_command': msg.message, 'backend': backend}
except Exception as e:
log.exception('Failed session cleanup')
raise JSONRPCError(
'Error occurred during session cleanup'
)