From 6a5a2565092e26769ebd490a8e3bbe18d82fade9 Mon Sep 17 00:00:00 2001 From: Stephen Newey Date: Tue, 30 Jun 2015 13:23:42 +0100 Subject: [PATCH] Add support for user on exec_create. --- docker/client.py | 11 +++++++++-- tests/integration_test.py | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/docker/client.py b/docker/client.py index 998ebacb..274359bf 100644 --- a/docker/client.py +++ b/docker/client.py @@ -521,19 +521,26 @@ class Client(requests.Session): return self.exec_start(create_res, detach, tty, stream) def exec_create(self, container, cmd, stdout=True, stderr=True, tty=False, - privileged=False): + user=None, privileged=False): if utils.compare_version('1.15', self._version) < 0: raise errors.InvalidVersion('Exec is not supported in API < 1.15') if privileged and utils.compare_version('1.19', self._version) < 0: raise errors.InvalidVersion( 'Privileged exec is not supported in API < 1.19' ) + if user and utils.compare_version('1.19', self._version) < 0: + raise errors.InvalidVersion( + 'User-specific exec is not supported in API < 1.19' + ) if isinstance(cmd, six.string_types): cmd = shlex.split(str(cmd)) + if user is None: + user = '' + data = { 'Container': container, - 'User': '', + 'User': user, 'Privileged': privileged, 'Tty': tty, 'AttachStdin': False, diff --git a/tests/integration_test.py b/tests/integration_test.py index 4b9869e2..f0064fa2 100644 --- a/tests/integration_test.py +++ b/tests/integration_test.py @@ -829,6 +829,23 @@ class TestExecuteCommandString(BaseTestCase): self.assertEqual(exec_log, expected) +@unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native') +class TestExecuteCommandStringAsUser(BaseTestCase): + def runTest(self): + container = self.client.create_container('busybox', 'cat', + detach=True, stdin_open=True) + id = container['Id'] + self.client.start(id) + self.tmp_containers.append(id) + + res = self.client.exec_create(id, 'whoami', user='default') + self.assertIn('Id', res) + + exec_log = self.client.exec_start(res) + expected = b'default' if six.PY3 else 'default\n' + self.assertEqual(exec_log, expected) + + @unittest.skipIf(not EXEC_DRIVER_IS_NATIVE, 'Exec driver not native') class TestExecuteCommandStreaming(BaseTestCase): def runTest(self):