| 1 |
# Copyright 2015 Rackspace Inc. |
| 2 |
# |
| 3 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 |
# not use this file except in compliance with the License. You may obtain |
| 5 |
# a copy of the License at |
| 6 |
# |
| 7 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
# |
| 9 |
# Unless required by applicable law or agreed to in writing, software |
| 10 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 |
# License for the specific language governing permissions and limitations |
| 13 |
# under the License. |
| 14 |
|
| 15 |
import copy |
| 16 |
import eventlet |
| 17 |
import fixtures |
| 18 |
import functools |
| 19 |
import logging as pylogging |
| 20 |
import platform |
| 21 |
import sys |
| 22 |
import time |
| 23 |
from unittest import mock |
| 24 |
|
| 25 |
from oslo_log import formatters |
| 26 |
from oslo_log import log as logging |
| 27 |
from oslotest import base |
| 28 |
import testtools |
| 29 |
|
| 30 |
from oslo_privsep import capabilities |
| 31 |
from oslo_privsep import comm |
| 32 |
from oslo_privsep import daemon |
| 33 |
from oslo_privsep.tests import testctx |
| 34 |
|
| 35 |
|
| 36 |
LOG = logging.getLogger(__name__) |
| 37 |
|
| 38 |
|
| 39 |
def undecorated(): |
| 40 |
pass |
| 41 |
|
| 42 |
|
| 43 |
class TestException(Exception): |
| 44 |
pass |
| 45 |
|
| 46 |
|
| 47 |
def get_fake_context(conf_attrs=None, **context_attrs): |
| 48 |
conf_attrs = conf_attrs or {} |
| 49 |
context = mock.NonCallableMock() |
| 50 |
context.conf.user = 42 |
| 51 |
context.conf.group = 84 |
| 52 |
context.conf.thread_pool_size = 10 |
| 53 |
context.conf.capabilities = [ |
| 54 |
capabilities.CAP_SYS_ADMIN, |
| 55 |
capabilities.CAP_NET_ADMIN, |
| 56 |
] |
| 57 |
context.conf.logger_name = 'oslo_privsep.daemon' |
| 58 |
vars(context).update(context_attrs) # type: ignore[attr-defined] |
| 59 |
vars(context.conf).update(conf_attrs) |
| 60 |
return context |
| 61 |
|
| 62 |
|
| 63 |
@testctx.context.entrypoint |
| 64 |
def logme(level, msg, exc_info=False): |
| 65 |
# We want to make sure we log everything from the priv side for |
| 66 |
# the purposes of this test, so force loglevel. |
| 67 |
LOG.logger.setLevel(logging.DEBUG) |
| 68 |
if exc_info: |
| 69 |
try: |
| 70 |
raise TestException('with arg') |
| 71 |
except TestException: |
| 72 |
LOG.log(level, msg, exc_info=True) |
| 73 |
else: |
| 74 |
LOG.log(level, msg) |
| 75 |
|
| 76 |
|
| 77 |
@testctx.context.entrypoint |
| 78 |
def raise_runtimeerror(): |
| 79 |
raise RuntimeError() |
| 80 |
|
| 81 |
|
| 82 |
class LogRecorder(pylogging.Formatter): |
| 83 |
def __init__(self, logs, *args, **kwargs): |
| 84 |
kwargs['validate'] = False |
| 85 |
super().__init__(*args, **kwargs) |
| 86 |
self.logs = logs |
| 87 |
|
| 88 |
def format(self, record): |
| 89 |
self.logs.append(copy.deepcopy(record)) |
| 90 |
return super().format(record) |
| 91 |
|
| 92 |
|
| 93 |
@testtools.skipIf( |
| 94 |
platform.system() != 'Linux', 'works only on Linux platform.' |
| 95 |
) |
| 96 |
class LogTest(testctx.TestContextTestCase): |
| 97 |
def test_priv_loglevel(self): |
| 98 |
logger = self.useFixture(fixtures.FakeLogger(level=logging.INFO)) |
| 99 |
|
| 100 |
# These write to the log on the priv side |
| 101 |
logme(logging.DEBUG, 'test@DEBUG') |
| 102 |
logme(logging.WARNING, 'test@WARN') |
| 103 |
|
| 104 |
time.sleep(0.1) # Hack to give logging thread a chance to run |
| 105 |
|
| 106 |
# logger.output is the resulting log on the unpriv side. |
| 107 |
# This should have been filtered based on (unpriv) loglevel. |
| 108 |
self.assertNotIn('test@DEBUG', logger.output) |
| 109 |
self.assertIn('test@WARN', logger.output) |
| 110 |
|
| 111 |
def test_record_data(self): |
| 112 |
logs: list[pylogging.LogRecord] = [] |
| 113 |
# fixtures.FakeLogger accepts only a formatter class/function, not an |
| 114 |
# instance :( |
| 115 |
fmt = functools.partial(LogRecorder, logs) |
| 116 |
|
| 117 |
self.useFixture( |
| 118 |
fixtures.FakeLogger( |
| 119 |
level=logging.INFO, |
| 120 |
format='dummy', |
| 121 |
formatter=fmt, # type: ignore[arg-type] |
| 122 |
) |
| 123 |
) |
| 124 |
|
| 125 |
try: |
| 126 |
logme(logging.WARNING, 'test with exc', exc_info=True) |
| 127 |
except Exception: # noqa: S110 |
| 128 |
pass |
| 129 |
|
| 130 |
time.sleep(0.1) # Hack to give logging thread a chance to run |
| 131 |
|
| 132 |
self.assertEqual(1, len(logs)) |
| 133 |
|
| 134 |
record = logs[0] |
| 135 |
self.assertIn('test with exc', record.getMessage()) |
| 136 |
self.assertIsNone(record.exc_info) |
| 137 |
self.assertIn('TestException: with arg', record.exc_text) |
| 138 |
self.assertEqual( |
| 139 |
'PrivContext(cfg_section=privsep)', record.processName |
| 140 |
) |
| 141 |
self.assertIn('test_daemon.py', record.exc_text) |
| 142 |
self.assertEqual(logging.WARNING, record.levelno) |
| 143 |
self.assertEqual('logme', record.funcName) |
| 144 |
|
| 145 |
def test_format_record(self): |
| 146 |
logs: list[pylogging.LogRecord] = [] |
| 147 |
# fixtures.FakeLogger accepts only a formatter class/function, not an |
| 148 |
# instance :( |
| 149 |
fmt = functools.partial(LogRecorder, logs) |
| 150 |
|
| 151 |
self.useFixture( |
| 152 |
fixtures.FakeLogger( |
| 153 |
level=logging.INFO, |
| 154 |
format='dummy', |
| 155 |
formatter=fmt, # type: ignore[arg-type] |
| 156 |
) |
| 157 |
) |
| 158 |
|
| 159 |
logme(logging.WARNING, 'test with exc', exc_info=True) |
| 160 |
|
| 161 |
time.sleep(0.1) # Hack to give logging thread a chance to run |
| 162 |
|
| 163 |
self.assertEqual(1, len(logs)) |
| 164 |
|
| 165 |
record = logs[0] |
| 166 |
# Verify the log record can be formatted by ContextFormatter |
| 167 |
fake_config = mock.Mock( |
| 168 |
logging_default_format_string="NOCTXT: %(message)s" |
| 169 |
) |
| 170 |
formatter = formatters.ContextFormatter(config=fake_config) |
| 171 |
formatter.format(record) |
| 172 |
|
| 173 |
|
| 174 |
@testtools.skipIf( |
| 175 |
platform.system() != 'Linux', 'works only on Linux platform.' |
| 176 |
) |
| 177 |
class LogTestDaemonTraceback(testctx.TestContextTestCase): |
| 178 |
def setUp(self): |
| 179 |
self.config_override = {'log_daemon_traceback': True} |
| 180 |
super().setUp() |
| 181 |
|
| 182 |
def test_record_daemon_traceback(self): |
| 183 |
self.privsep_conf.set_override( |
| 184 |
'log_daemon_traceback', True, group='privsep' |
| 185 |
) |
| 186 |
logs: list[pylogging.LogRecord] = [] |
| 187 |
# fixtures.FakeLogger accepts only a formatter class/function, not an |
| 188 |
# instance :( |
| 189 |
fmt = functools.partial(LogRecorder, logs) |
| 190 |
|
| 191 |
self.useFixture( |
| 192 |
fixtures.FakeLogger( |
| 193 |
level=logging.INFO, |
| 194 |
format='dummy', |
| 195 |
formatter=fmt, # type: ignore[arg-type] |
| 196 |
) |
| 197 |
) |
| 198 |
|
| 199 |
self.assertRaises(RuntimeError, raise_runtimeerror) |
| 200 |
time.sleep(0.1) # Hack to give logging thread a chance to run |
| 201 |
|
| 202 |
self.assertEqual(1, len(logs)) |
| 203 |
record = logs[0] |
| 204 |
self.assertIn('Privsep daemon traceback: ', record.getMessage()) |
| 205 |
self.assertIsNone(record.exc_info) |
| 206 |
self.assertEqual('MainProcess', record.processName) |
| 207 |
self.assertEqual(logging.WARNING, record.levelno) |
| 208 |
|
| 209 |
|
| 210 |
@testtools.skipIf( |
| 211 |
platform.system() != 'Linux', 'works only on Linux platform.' |
| 212 |
) |
| 213 |
class DaemonTest(base.BaseTestCase): |
| 214 |
@mock.patch('os.setuid') |
| 215 |
@mock.patch('os.setgid') |
| 216 |
@mock.patch('os.setgroups') |
| 217 |
@mock.patch('oslo_privsep.capabilities.set_keepcaps') |
| 218 |
@mock.patch('oslo_privsep.capabilities.drop_all_caps_except') |
| 219 |
def test_drop_privs( |
| 220 |
self, |
| 221 |
mock_dropcaps, |
| 222 |
mock_keepcaps, |
| 223 |
mock_setgroups, |
| 224 |
mock_setgid, |
| 225 |
mock_setuid, |
| 226 |
): |
| 227 |
channel = mock.NonCallableMock() |
| 228 |
context = get_fake_context() |
| 229 |
|
| 230 |
manager = mock.Mock() |
| 231 |
manager.attach_mock(mock_setuid, "setuid") |
| 232 |
manager.attach_mock(mock_setgid, "setgid") |
| 233 |
expected_calls = [mock.call.setgid(84), mock.call.setuid(42)] |
| 234 |
|
| 235 |
d = daemon.Daemon(channel, context) |
| 236 |
d._drop_privs() |
| 237 |
|
| 238 |
mock_setuid.assert_called_once_with(42) |
| 239 |
mock_setgid.assert_called_once_with(84) |
| 240 |
mock_setgroups.assert_called_once_with([]) |
| 241 |
|
| 242 |
self.assertEqual(expected_calls, manager.mock_calls) |
| 243 |
|
| 244 |
self.assertCountEqual( |
| 245 |
[mock.call(True), mock.call(False)], mock_keepcaps.mock_calls |
| 246 |
) |
| 247 |
|
| 248 |
mock_dropcaps.assert_called_once_with( |
| 249 |
{capabilities.CAP_SYS_ADMIN, capabilities.CAP_NET_ADMIN}, |
| 250 |
{capabilities.CAP_SYS_ADMIN, capabilities.CAP_NET_ADMIN}, |
| 251 |
[], |
| 252 |
) |
| 253 |
|
| 254 |
|
| 255 |
@testtools.skipIf( |
| 256 |
platform.system() != 'Linux', 'works only on Linux platform.' |
| 257 |
) |
| 258 |
class WithContextTest(testctx.TestContextTestCase): |
| 259 |
def test_unexported(self): |
| 260 |
self.assertRaisesRegex( |
| 261 |
NameError, |
| 262 |
'undecorated not exported', |
| 263 |
testctx.context._wrap, |
| 264 |
undecorated, |
| 265 |
) |
| 266 |
|
| 267 |
|
| 268 |
class ClientChannelTestCase(base.BaseTestCase): |
| 269 |
DICT = { |
| 270 |
'string_1': ('tuple_1', b'tuple_2'), |
| 271 |
b'byte_1': ['list_1', 'list_2'], |
| 272 |
} |
| 273 |
|
| 274 |
EXPECTED = { |
| 275 |
'string_1': ('tuple_1', b'tuple_2'), |
| 276 |
'byte_1': ['list_1', 'list_2'], |
| 277 |
} |
| 278 |
|
| 279 |
def setUp(self): |
| 280 |
super().setUp() |
| 281 |
context = get_fake_context() |
| 282 |
with ( |
| 283 |
mock.patch.object(comm.ClientChannel, '__init__'), |
| 284 |
mock.patch.object(daemon._ClientChannel, 'exchange_ping'), |
| 285 |
): |
| 286 |
self.client_channel = daemon._ClientChannel(mock.ANY, context) |
| 287 |
|
| 288 |
@mock.patch.object(daemon.LOG.logger, 'handle') |
| 289 |
def test_out_of_band_log_message(self, handle_mock): |
| 290 |
message = [comm.Message.LOG, self.DICT] |
| 291 |
self.assertEqual(self.client_channel.log, daemon.LOG) |
| 292 |
with ( |
| 293 |
mock.patch.object(pylogging, 'makeLogRecord') as mock_make_log, |
| 294 |
mock.patch.object( |
| 295 |
daemon.LOG, 'isEnabledFor', return_value=True |
| 296 |
) as mock_enabled, |
| 297 |
): |
| 298 |
self.client_channel.out_of_band(message) |
| 299 |
mock_make_log.assert_called_once_with(self.EXPECTED) |
| 300 |
handle_mock.assert_called_once_with(mock_make_log.return_value) |
| 301 |
mock_enabled.assert_called_once_with( |
| 302 |
mock_make_log.return_value.levelno |
| 303 |
) |
| 304 |
|
| 305 |
def test_out_of_band_not_log_message(self): |
| 306 |
with mock.patch.object(daemon.LOG, 'warning') as mock_warning: |
| 307 |
self.client_channel.out_of_band([comm.Message.PING]) |
| 308 |
mock_warning.assert_called_once() |
| 309 |
|
| 310 |
@mock.patch.object(daemon.logging, 'getLogger') # type: ignore[attr-defined] |
| 311 |
@mock.patch.object(pylogging, 'makeLogRecord') |
| 312 |
def test_out_of_band_log_message_context_logger( |
| 313 |
self, make_log_mock, get_logger_mock |
| 314 |
): |
| 315 |
logger_name = 'os_brick.privileged' |
| 316 |
context = get_fake_context(conf_attrs={'logger_name': logger_name}) |
| 317 |
with ( |
| 318 |
mock.patch.object(comm.ClientChannel, '__init__'), |
| 319 |
mock.patch.object(daemon._ClientChannel, 'exchange_ping'), |
| 320 |
): |
| 321 |
channel = daemon._ClientChannel(mock.ANY, context) |
| 322 |
|
| 323 |
get_logger_mock.assert_called_once_with(logger_name) |
| 324 |
self.assertEqual(get_logger_mock.return_value, channel.log) |
| 325 |
|
| 326 |
message = [comm.Message.LOG, self.DICT] |
| 327 |
channel.out_of_band(message) |
| 328 |
|
| 329 |
make_log_mock.assert_called_once_with(self.EXPECTED) |
| 330 |
channel.log.isEnabledFor.assert_called_once_with( # type: ignore[attr-defined] |
| 331 |
make_log_mock.return_value.levelno |
| 332 |
) |
| 333 |
channel.log.logger.handle.assert_called_once_with( # type: ignore[attr-defined] |
| 334 |
make_log_mock.return_value |
| 335 |
) |
| 336 |
|
| 337 |
|
| 338 |
class UnMonkeyPatch(base.BaseTestCase): |
| 339 |
def test_un_monkey_patch(self): |
| 340 |
self.assertFalse( |
| 341 |
any( |
| 342 |
eventlet.patcher.is_monkey_patched(eventlet_mod_name) |
| 343 |
for eventlet_mod_name in daemon.EVENTLET_MODULES |
| 344 |
) |
| 345 |
) |
| 346 |
|
| 347 |
eventlet.monkey_patch() |
| 348 |
self.assertTrue( |
| 349 |
any( |
| 350 |
eventlet.patcher.is_monkey_patched(eventlet_mod_name) |
| 351 |
for eventlet_mod_name in daemon.EVENTLET_MODULES |
| 352 |
) |
| 353 |
) |
| 354 |
|
| 355 |
daemon.un_monkey_patch() |
| 356 |
for eventlet_mod_name, func_modules in daemon.EVENTLET_LIBRARIES: |
| 357 |
if not eventlet.patcher.is_monkey_patched(eventlet_mod_name): |
| 358 |
continue |
| 359 |
|
| 360 |
for name, green_mod in func_modules(): |
| 361 |
orig_mod = eventlet.patcher.original(name) |
| 362 |
patched_mod = sys.modules.get(name) |
| 363 |
for attr_name in green_mod.__patched__: |
| 364 |
un_monkey_patched_attr = getattr( |
| 365 |
patched_mod, attr_name, None |
| 366 |
) |
| 367 |
original_attr = getattr(orig_mod, attr_name, None) |
| 368 |
self.assertEqual(un_monkey_patched_attr, original_attr) |