| 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 |
|
| 16 |
import logging |
| 17 |
import os |
| 18 |
import platform |
| 19 |
import shlex |
| 20 |
import sys |
| 21 |
import tempfile |
| 22 |
import time |
| 23 |
from unittest import mock |
| 24 |
|
| 25 |
import testtools |
| 26 |
|
| 27 |
from oslo_privsep import comm |
| 28 |
from oslo_privsep import daemon |
| 29 |
from oslo_privsep import priv_context |
| 30 |
from oslo_privsep.tests import testctx |
| 31 |
|
| 32 |
LOG = logging.getLogger(__name__) |
| 33 |
|
| 34 |
|
| 35 |
@testctx.context.entrypoint |
| 36 |
def priv_getpid(): |
| 37 |
return os.getpid() |
| 38 |
|
| 39 |
|
| 40 |
@testctx.context.entrypoint |
| 41 |
def add1(arg): |
| 42 |
return arg + 1 |
| 43 |
|
| 44 |
|
| 45 |
@testctx.context.entrypoint_with_timeout(0.2) |
| 46 |
def do_some_long(long_timeout=0.4): |
| 47 |
time.sleep(long_timeout) |
| 48 |
return 42 |
| 49 |
|
| 50 |
|
| 51 |
class CustomError(Exception): |
| 52 |
def __init__(self, code, msg): |
| 53 |
super().__init__(code, msg) |
| 54 |
self.code = code |
| 55 |
self.msg = msg |
| 56 |
|
| 57 |
def __str__(self): |
| 58 |
return f'Code {self.code}: {self.msg}' |
| 59 |
|
| 60 |
|
| 61 |
@testctx.context.entrypoint |
| 62 |
def fail(custom=False): |
| 63 |
if custom: |
| 64 |
raise CustomError(42, 'omg!') |
| 65 |
else: |
| 66 |
raise RuntimeError("I can't let you do that Dave") |
| 67 |
|
| 68 |
|
| 69 |
@testtools.skipIf( |
| 70 |
platform.system() != 'Linux', 'works only on Linux platform.' |
| 71 |
) |
| 72 |
class PrivContextTest(testctx.TestContextTestCase): |
| 73 |
def test_set_client_mode(self): |
| 74 |
context = priv_context.PrivContext('test', capabilities=[]) |
| 75 |
self.assertTrue(context.client_mode) |
| 76 |
|
| 77 |
context.set_client_mode(False) |
| 78 |
self.assertFalse(context.client_mode) |
| 79 |
|
| 80 |
def test_helper_command(self): |
| 81 |
self.privsep_conf.privsep.helper_command = 'foo --bar' |
| 82 |
_, temp_path = tempfile.mkstemp() |
| 83 |
cmd = testctx.context.helper_command(temp_path) |
| 84 |
expected = [ |
| 85 |
'foo', |
| 86 |
'--bar', |
| 87 |
'--privsep_context', |
| 88 |
testctx.context.pypath, |
| 89 |
'--privsep_sock_path', |
| 90 |
temp_path, |
| 91 |
] |
| 92 |
self.assertEqual(expected, cmd) |
| 93 |
|
| 94 |
def test_helper_command_default(self): |
| 95 |
self.privsep_conf.config_file = ['/bar.conf'] |
| 96 |
_, temp_path = tempfile.mkstemp() |
| 97 |
cmd = testctx.context.helper_command(temp_path) |
| 98 |
expected = [ |
| 99 |
'sudo', |
| 100 |
'privsep-helper', |
| 101 |
'--config-file', |
| 102 |
'/bar.conf', |
| 103 |
# --config-dir arg should be skipped |
| 104 |
'--privsep_context', |
| 105 |
testctx.context.pypath, |
| 106 |
'--privsep_sock_path', |
| 107 |
temp_path, |
| 108 |
] |
| 109 |
self.assertEqual(expected, cmd) |
| 110 |
|
| 111 |
def test_helper_command_default_dirtoo(self): |
| 112 |
self.privsep_conf.config_file = ['/bar.conf', '/baz.conf'] |
| 113 |
self.privsep_conf.config_dir = ['/foo.d'] |
| 114 |
_, temp_path = tempfile.mkstemp() |
| 115 |
cmd = testctx.context.helper_command(temp_path) |
| 116 |
expected = [ |
| 117 |
'sudo', |
| 118 |
'privsep-helper', |
| 119 |
'--config-file', |
| 120 |
'/bar.conf', |
| 121 |
'--config-file', |
| 122 |
'/baz.conf', |
| 123 |
'--config-dir', |
| 124 |
'/foo.d', |
| 125 |
'--privsep_context', |
| 126 |
testctx.context.pypath, |
| 127 |
'--privsep_sock_path', |
| 128 |
temp_path, |
| 129 |
] |
| 130 |
self.assertEqual(expected, cmd) |
| 131 |
|
| 132 |
def test_init_known_contexts(self): |
| 133 |
self.assertEqual( |
| 134 |
testctx.context.helper_command('/sock')[:2], |
| 135 |
['sudo', 'privsep-helper'], |
| 136 |
) |
| 137 |
priv_context.init(root_helper=['sudo', 'rootwrap']) |
| 138 |
self.assertEqual( |
| 139 |
testctx.context.helper_command('/sock')[:3], |
| 140 |
['sudo', 'rootwrap', 'privsep-helper'], |
| 141 |
) |
| 142 |
|
| 143 |
def test_start_acquires_lock(self): |
| 144 |
context = priv_context.PrivContext('test', capabilities=[]) |
| 145 |
context.channel = "something not None" # type: ignore[assignment] |
| 146 |
context.start_lock = mock.Mock() |
| 147 |
context.start_lock.__enter__ = mock.Mock() |
| 148 |
context.start_lock.__exit__ = mock.Mock() |
| 149 |
self.assertFalse(context.start_lock.__enter__.called) |
| 150 |
context.start() |
| 151 |
self.assertTrue(context.start_lock.__enter__.called) |
| 152 |
|
| 153 |
|
| 154 |
@testtools.skipIf( |
| 155 |
platform.system() != 'Linux', 'works only on Linux platform.' |
| 156 |
) |
| 157 |
class SeparationTest(testctx.TestContextTestCase): |
| 158 |
def test_getpid(self): |
| 159 |
# Verify that priv_getpid() was executed in another process. |
| 160 |
priv_pid = priv_getpid() |
| 161 |
self.assertNotMyPid(priv_pid) |
| 162 |
|
| 163 |
def test_client_mode(self): |
| 164 |
self.assertNotMyPid(priv_getpid()) |
| 165 |
|
| 166 |
self.addCleanup(testctx.context.set_client_mode, True) |
| 167 |
|
| 168 |
testctx.context.set_client_mode(False) |
| 169 |
# priv_getpid() should now run locally (and return our pid) |
| 170 |
self.assertEqual(os.getpid(), priv_getpid()) |
| 171 |
|
| 172 |
testctx.context.set_client_mode(True) |
| 173 |
# priv_getpid() should now run remotely again |
| 174 |
self.assertNotMyPid(priv_getpid()) |
| 175 |
|
| 176 |
|
| 177 |
@testtools.skipIf( |
| 178 |
platform.system() != 'Linux', 'works only on Linux platform.' |
| 179 |
) |
| 180 |
class RootwrapTest(testctx.TestContextTestCase): |
| 181 |
def setUp(self): |
| 182 |
super().setUp() |
| 183 |
testctx.context.stop() |
| 184 |
|
| 185 |
# Generate a command that will run daemon.helper_main without |
| 186 |
# requiring it to be properly installed. |
| 187 |
cmd = [ |
| 188 |
'env', |
| 189 |
f'PYTHON_PATH={os.path.pathsep.join(sys.path)}', |
| 190 |
sys.executable, |
| 191 |
daemon.__file__, |
| 192 |
] |
| 193 |
if LOG.isEnabledFor(logging.DEBUG): |
| 194 |
cmd.append('--debug') |
| 195 |
|
| 196 |
self.privsep_conf.set_override( |
| 197 |
'helper_command', |
| 198 |
' '.join(map(shlex.quote, cmd)), |
| 199 |
group=testctx.context.cfg_section, |
| 200 |
) |
| 201 |
|
| 202 |
testctx.context.start(method=priv_context.Method.ROOTWRAP) |
| 203 |
|
| 204 |
def test_getpid(self): |
| 205 |
# Verify that priv_getpid() was executed in another process. |
| 206 |
priv_pid = priv_getpid() |
| 207 |
self.assertNotMyPid(priv_pid) |
| 208 |
|
| 209 |
def test_long_call_with_timeout(self): |
| 210 |
self.assertRaises(comm.PrivsepTimeout, do_some_long) |
| 211 |
|
| 212 |
def test_long_call_within_timeout(self): |
| 213 |
res = do_some_long(0.001) |
| 214 |
self.assertEqual(42, res) |
| 215 |
|
| 216 |
|
| 217 |
@testtools.skipIf( |
| 218 |
platform.system() != 'Linux', 'works only on Linux platform.' |
| 219 |
) |
| 220 |
class SerializationTest(testctx.TestContextTestCase): |
| 221 |
def test_basic_functionality(self): |
| 222 |
self.assertEqual(43, add1(42)) |
| 223 |
|
| 224 |
def test_raises_standard(self): |
| 225 |
self.assertRaisesRegex( |
| 226 |
RuntimeError, "I can't let you do that Dave", fail |
| 227 |
) |
| 228 |
|
| 229 |
def test_raises_custom(self): |
| 230 |
exc = self.assertRaises(CustomError, fail, custom=True) |
| 231 |
self.assertEqual(exc.code, 42) |
| 232 |
self.assertEqual(exc.msg, 'omg!') |