blob: c527c3b67cb2aa682ef0dd009eb49e2c72666d22 [file] [log] [blame]
Felipe Monteiro7bc35dc2017-04-19 21:11:46 +01001========
2Overview
3========
DavidPurcell663aedf2017-01-03 10:01:14 -05004
Felipe Monteiro7bc35dc2017-04-19 21:11:46 +01005Patrole is a tool for verifying that Role-Based Access Control is being
6correctly enforced.
DavidPurcell663aedf2017-01-03 10:01:14 -05007
Felipe Monteiro7bc35dc2017-04-19 21:11:46 +01008Patrole allows users to run API tests using specified RBAC roles. This allows
DavidPurcell663aedf2017-01-03 10:01:14 -05009deployments to verify that only intended roles have access to those APIs.
10This is critical to ensure security, especially in large deployments with
11custom roles.
12
13* Free software: Apache license
14* Documentation: http://docs.openstack.org/developer/patrole
15* Source: http://git.openstack.org/cgit/openstack/patrole
16* Bugs: http://bugs.launchpad.net/patrole
17
18Features
Felipe Monteiro7bc35dc2017-04-19 21:11:46 +010019========
20Patrole offers RBAC testing for various OpenStack RBAC policies. It includes
DavidPurcell45bc1a62017-01-19 17:21:27 -050021a decorator that wraps around tests which verifies that when the test calls the
Felipe Monteiro7bc35dc2017-04-19 21:11:46 +010022corresponding API endpoint, access is only granted for correct roles.
23
24Currently, Patrole supports policies contained in code and in policy.json files.
25If both exist, the policy actions in the policy.json are prioritized.
26
27.. _test-flows:
28
29Test Flows
30----------
DavidPurcell45bc1a62017-01-19 17:21:27 -050031
32There are several possible test flows.
33
Felipe Monteiro7bc35dc2017-04-19 21:11:46 +010034If the ``rbac_test_role`` is allowed to access the endpoint:
DavidPurcell45bc1a62017-01-19 17:21:27 -050035
Felipe Monteiro7bc35dc2017-04-19 21:11:46 +010036* The test passes if no 403 ``Forbidden`` or ``RbacActionFailed`` exception is raised.
37
38If the ``rbac_test_role`` is not allowed to access the endpoint:
39
40* If the endpoint returns a 403 `Forbidden` exception the test will pass.
41* If the endpoint returns successfully, then the test will fail with an
42 ``RbacOverPermission`` exception.
43* If the endpoint returns something other than a 403 ``Forbidden`` to indicate
44 that the role is not allowed, the test will raise an ``RbacActionFailed`` exception.
45
46.. note::
47
48 Certain services like Neutron *intentionally* raise a 404 instead of a 403
49 for security concerns. Patrole accomodates this behavior by anticipating
50 a 404 instead of a 403, if specified through a special argument. For more
51 information about Neutron's policy enforcement, see:
52 `<https://docs.openstack.org/developer/neutron/devref/policy.html#request-authorization>`__.
53
54How It Works
55============
56Patrole leverages oslo_policy (OpenStack's policy enforcement engine) to
57determine whether a given role is allowed to perform a policy action given a
58specific rule and OpenStack service. This is done before test execution inside
59the ``rbac_rule_validation.action`` decorator. Then, inside the test, the API
60that does policy enforcement for the same rule is called. The outcome is
61compared against the result from oslo_policy and a pass or fail is determined
62as outlined above: :ref:`test-flows`.
63
64.. note::
65
66 Currently, Patrole does not support checking multiple rules against a single
67 API call. Even though some APIs enforce multiple rules (some indirectly),
68 it is increasingly difficult to maintain the tests if multiple policy
69 actions are expected to be called.
70
71Test Execution Workflow
72-----------------------
73
74The workflow is as follows:
75
76#. Each test uses the ``rbac_rule_validation.action`` decorator, like below: ::
77
78 @rbac_rule_validation.action(
79 service="nova",
80 rule="os_compute_api:servers:stop")
81 @decorators.idempotent_id('ab4a17d2-166f-4a6d-9944-f17baa576cf2')
82 def test_stop_server(self):
83 # Set the primary credential's role to "rbac_test_role".
84 self.rbac_utils.switch_role(self, toggle_rbac_role=True)
85 # Call the API that enforces the policy action specified by "rule".
86 self._test_stop_server()
87
88 The ``service`` attribute accepts an OpenStack service and the ``rule`` attribute
89 accepts a valid OpenStack policy action, like "os_compute_api:servers:stop".
90
91#. The ``rbac_rule_validation.action`` decorator passes these attributes,
92 along with user_id and project_id information derived from the primary
93 Tempest credential (``self.os.credentials.user_id`` and ``self.os.credentials.project_id``),
94 to the ``rbac_policy_parser``.
95
96#. The logic in ``rbac_policy_parser`` then passes all this information along
97 and the role in ``CONF.rbac.rbac_test_role`` to oslo_policy to determine whether
98 the ``rbac_test_role`` is authorized to perform the policy action for the given
99 service.
100
101#. After all of the logic above has executed inside the rbac decorator, the
102 test is executed. The test then sets up test-level resources, if necessary,
103 with **admin** credentials implicitly. This is accomplished through
104 ``rbac_utils.switch_role(toggle_rbac_role=False)``: ::
105
106 @classmethod
107 def setup_clients(cls):
108 super(BaseV2ComputeRbacTest, cls).setup_clients()
109 cls.auth_provider = cls.os.auth_provider
110 cls.rbac_utils = rbac_utils()
111 cls.rbac_utils.switch_role(cls, toggle_rbac_role=False)
112
113 This code has *already* executed when the test class is instantiated, because
114 it is located in the base rbac test class. Whenever ``cls.rbac_utils.switch_role``
115 is called, one of two behaviors are possible:
116
117 #. The primary credential's role is changed to admin if ``toggle_rbac_role=False``
118 #. The primary credential's role is changed to ``rbac_test_role`` if
119 ``toggle_rbac_role=True``
120
121 Thus, at the *beginning* of every test and during ``resource_setup`` and
122 ``resource_cleanup``, the primary credential has the admin role.
123
124#. After preliminary test-level setup is performed, like creating a server, a
125 second call to ``self.rbac_utils.switch_role`` is done: ::
126
127 self.rbac_utils.switch_role(cls, toggle_rbac_role=True)
128
129 Now the primary credential has the role specified by ``rbac_test_role``.
130
131#. The API endpoint in which policy enforcement of "os_compute_api:servers:stop"
132 is performed can now be called.
133
134 .. note:
135
136 To determine whether a policy action is enforced, refer to the relevant
137 controller code to make sure that the policy action is indeed enforced.
138
139#. Now that a call is made to "stop_server" with the primary credentials having
140 the role specified by ``rbac_test_role``, either the nova contoller will allow
141 or disallow the action to be performed. Since the "stop_server" policy action in
142 nova is defined as "base.RULE_ADMIN_OR_OWNER", the API will most likely
143 return a successful status code. For more information about this policy action,
144 see `<https://github.com/openstack/nova/blob/master/nova/policies/servers.py>`__.
145
146#. As mentioned above, the result from the API call and the result from oslo_policy
147 are compared for consistency.
148
149#. Finally, after the test has executed, but before ``tearDown`` or ``resource_cleanup``
150 is called, ``self.rbac_utils.switch_role(cls, toggle_rbac_role=False)`` is
151 called, so that the primary credential yet again has admin permissions for
152 test clean up. This call is always performed in the "finally" block inside
153 the ``rbac_rule_validation`` decorator.
154
155.. warning::
156
157 Failure to call ``self.rbac_utils.switch_role(cls, toggle_rbac_role=True)``
158 inside a test with the ``rbac_rule_validation`` decorator applied results
159 in a ``RbacResourceSetupFailed`` being raised, causing the test to fail.