-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes: https://tracker.ceph.com/issues/36193 Enable API auditing with 'ceph dashboard set-audit-api-enabled true' (default is false). If you do not want to log the request payload, then disable it via 'set-audit-api-log-payload false' (default is true). Example output: 2018-10-08 10:25:21.850994 mgr.x [INF] [AUDIT] from='https://[::1]:44410' path='/api/auth' method='POST' user='None' params='{"username": "admin", "password": "admin", "stay_signed_in": false}' Signed-off-by: Volker Theile <vtheile@suse.com>
- Loading branch information
Showing
3 changed files
with
119 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# -*- coding: utf-8 -*- | ||
# pylint: disable=dangerous-default-value,too-many-public-methods | ||
from __future__ import absolute_import | ||
|
||
import json | ||
import re | ||
import mock | ||
|
||
from .helper import ControllerTestCase | ||
from ..controllers import RESTController, Controller | ||
from .. import mgr | ||
|
||
|
||
@Controller('/foo', secure=False) | ||
class Foo(RESTController): | ||
def create(self, data): | ||
pass | ||
|
||
def get(self, key): | ||
pass | ||
|
||
def delete(self, key): | ||
pass | ||
|
||
def set(self, key, x): | ||
pass | ||
|
||
|
||
class ApiAuditingTest(ControllerTestCase): | ||
settings = {} | ||
|
||
@classmethod | ||
def mock_set_config(cls, key, val): | ||
cls.settings[key] = val | ||
|
||
@classmethod | ||
def mock_get_config(cls, key, default): | ||
return cls.settings.get(key, default) | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
mgr.get_config.side_effect = cls.mock_get_config | ||
mgr.set_config.side_effect = cls.mock_set_config | ||
|
||
@classmethod | ||
def setup_server(cls): | ||
cls.setup_controllers([Foo]) | ||
|
||
def setUp(self): | ||
mgr.cluster_log = mock.Mock() | ||
mgr.set_config('AUDIT_API_ENABLED', True) | ||
mgr.set_config('AUDIT_API_LOG_PAYLOAD', True) | ||
|
||
def test_audit(self): | ||
self._put('/foo/test1', {'x': 0}) | ||
mgr.cluster_log.assert_called_once() | ||
_, _, msg = mgr.cluster_log.call_args_list[0][0] | ||
pattern = r'^\[AUDIT\] from=\'(.+)\' path=\'(.+)\' ' \ | ||
'method=\'(.+)\' user=\'(.+)\' params=\'(.+)\'$' | ||
m = re.match(pattern, msg) | ||
self.assertEqual(m.group(2), '/foo/test1') | ||
self.assertEqual(m.group(3), 'PUT') | ||
self.assertEqual(m.group(4), 'None') | ||
params = json.loads(m.group(5)) | ||
self.assertEqual(params['key'], 'test1') | ||
self.assertEqual(params['x'], 0) | ||
|
||
def test_no_audit(self): | ||
mgr.set_config('AUDIT_API_ENABLED', False) | ||
self._delete('/foo/test1') | ||
mgr.cluster_log.assert_not_called() | ||
|
||
def test_no_payload(self): | ||
mgr.set_config('AUDIT_API_LOG_PAYLOAD', False) | ||
self._delete('/foo/test1') | ||
_, _, msg = mgr.cluster_log.call_args_list[0][0] | ||
self.assertNotIn('params=', msg) | ||
|
||
def test_no_audit_get(self): | ||
self._get('/foo/test1') | ||
mgr.cluster_log.assert_not_called() | ||
|
||
def test_audit_put(self): | ||
self._put('/foo/test1', {'x': 0}) | ||
mgr.cluster_log.assert_called_once() | ||
|
||
def test_audit_post(self): | ||
self._post('/foo', {'data': {'id': 'test1', 'x': 0}}) | ||
mgr.cluster_log.assert_called_once() | ||
|
||
def test_audit_delete(self): | ||
self._delete('/foo/test1') | ||
mgr.cluster_log.assert_called_once() |