Esempio n. 1
0
    def test_run_user(self):
        """Test check user in docker run
        """
        users = [(123, 456)]
        plugin = plugins.DockerRunUserPlugin()

        # pylint: disable=protected-access
        plugins._IMAGE_USER = {}
        # no user name
        (allow, msg) = plugin.run_req('POST',
                                      '/v1.26/containers/create',
                                      {'Image': 'foo'},
                                      users=users)
        self.assertFalse(allow)

        # wrong user name
        (allow, msg) = plugin.run_req('POST',
                                      '/v1.26/containers/create', {
                                          'User': '******',
                                          'Image': 'foo'
                                      },
                                      users=users)
        self.assertFalse(allow)

        # correct user name
        (allow, msg) = plugin.run_req('POST',
                                      '/v1.26/containers/create', {
                                          'User': '******',
                                          'Image': 'foo'
                                      },
                                      users=users)
        self.assertTrue(allow)

        plugins._IMAGE_USER = {'foo': '5:5'}
        (allow, msg) = plugin.run_req('POST',
                                      '/v1.26/containers/create',
                                      {'Image': 'foo'},
                                      users=users)
        self.assertTrue(allow)
    def test_run_user(self):
        """Test check user in docker run
        """
        plugin = plugins.DockerRunUserPlugin()

        # pylint: disable=protected-access
        # no user name
        (allow, msg) = plugin.run_req(
            'POST',
            '/v1.26/containers/create',
            {'Image': 'foo'},
        )
        self.assertTrue(allow)

        # correct user name
        (allow, msg) = plugin.run_req(
            'POST',
            '/v1.26/containers/create',
            {
                'User': '******',
                'Image': 'foo'
            },
        )
        self.assertTrue(allow)
Esempio n. 3
0
class API:
    """API of docker authz plugin
    """

    _plugins = [
        plugins.DockerRunUserPlugin(),
        plugins.DockerRunPrivilegePlugin(),
        plugins.DockerExecUserPlugin(),
    ]

    def __init__(self, **_kwargs):

        # TODO: add schema validation
        def authzreq(data):
            """implement AuthZPlugin.AuthZReq
            """
            if 'RequestBody' in data:
                request_body = base64.b64decode(data['RequestBody'])
                _LOGGER.debug('request body: %s', request_body)
                request_obj = json.loads(request_body.decode())
            else:
                request_obj = {}

            for plugin in self._plugins:
                (allow, msg) = plugin.run_req(
                    data['RequestMethod'],
                    data['RequestUri'],
                    request_obj,
                )
                _LOGGER.debug(
                    'Request %s: %s',
                    plugin.__class__,
                    ('Authorized' if allow else 'Not authorized'),
                )
                if not allow:
                    break
            else:
                allow = True

            _LOGGER.info(
                'Request: %s %s %s (%s)',
                data['RequestMethod'],
                data['RequestUri'],
                ('Authorized' if allow else 'Not authorized'),
                msg,
            )

            return (allow, msg)

        # TODO: add schema validation
        def authzres(data):
            """implement AuthZPlugin.AuthZReq
            """
            if 'RequestBody' in data:
                request_body = base64.b64decode(data['RequestBody'])
                request_obj = json.loads(request_body.decode())
            else:
                request_obj = {}

            if 'ResponseBody' in data:
                response_body = base64.b64decode(data['ResponseBody'])
                _LOGGER.debug('response body: %s', response_body)
                response_obj = json.loads(response_body.decode())
            else:
                response_obj = {}

            for plugin in self._plugins:
                (allow, msg) = plugin.run_res(
                    data['RequestMethod'],
                    data['RequestUri'],
                    request_obj,
                    response_obj,
                )
                _LOGGER.debug(
                    'Response %s: %s',
                    plugin.__class__,
                    ('Authorized' if allow else 'Not authorized'),
                )

                if not allow:
                    break
            else:
                allow = True

            _LOGGER.info(
                'Response: %s %s %s %s',
                data['RequestMethod'],
                data['RequestUri'],
                data.get('ResponseStatusCode', None),
                ('Authorized' if allow else 'Not authorized'),
            )

            return (allow, msg)

        # TODO: add schema validation
        def activate():
            """Implement Plugin.Activate
            """
            return {'Implements': [_PLUGIN_NAME]}

        self.authzreq = authzreq
        self.authzres = authzres
        self.activate = activate