Skip to content

Commit 631ed3c

Browse files
mhuinFlorent Flament
authored andcommitted
Unscoped federated user-specific commands
A federated user can authenticate with the v3unscopedsaml plugin and list the domains and projects she is allowed to scope to. This patch introduces the new commands 'federation domain list' and 'federation project list'. Note that for these commands -and plugin- to be available, the lxml library must be installed. Change-Id: I2707b624befcfb0a01b40a094e12fd68a3ee7773 Co-Authored-By: Florent Flament <florent.flament-ext@cloudwatt.com>
1 parent 2c9d263 commit 631ed3c

6 files changed

Lines changed: 231 additions & 0 deletions

File tree

doc/source/man/openstack.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ Please bear in mind that some plugins might not support all of the functionaliti
4747

4848
Additionally, it is possible to use Keystone's service token to authenticate, by setting the options :option:`--os-token` and :option:`--os-url` (or the environment variables :envvar:`OS_TOKEN` and :envvar:`OS_URL` respectively). This method takes precedence over authentication plugins.
4949

50+
.. NOTE::
51+
To use the ``v3unscopedsaml`` method, the lxml package will need to be installed.
52+
5053
OPTIONS
5154
=======
5255

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
#
13+
14+
"""Identity v3 unscoped SAML auth action implementations.
15+
16+
The first step of federated auth is to fetch an unscoped token. From there,
17+
the user can list domains and projects they are allowed to access, and request
18+
a scoped token."""
19+
20+
import logging
21+
22+
from cliff import lister
23+
24+
from openstackclient.common import exceptions
25+
from openstackclient.common import utils
26+
27+
28+
UNSCOPED_AUTH_PLUGINS = ['v3unscopedsaml', 'v3unscopedadfs']
29+
30+
31+
def auth_with_unscoped_saml(func):
32+
"""Check the unscoped federated context"""
33+
def _decorated(self, parsed_args):
34+
auth_plugin_name = self.app.client_manager.auth_plugin_name
35+
if auth_plugin_name in UNSCOPED_AUTH_PLUGINS:
36+
return func(self, parsed_args)
37+
else:
38+
msg = ('This command requires the use of an unscoped SAML '
39+
'authentication plugin. Please use argument '
40+
'--os-auth-plugin with one of the following '
41+
'plugins: ' + ', '.join(UNSCOPED_AUTH_PLUGINS))
42+
raise exceptions.CommandError(msg)
43+
return _decorated
44+
45+
46+
class ListAccessibleDomains(lister.Lister):
47+
"""List accessible domains"""
48+
49+
log = logging.getLogger(__name__ + '.ListAccessibleDomains')
50+
51+
@auth_with_unscoped_saml
52+
def take_action(self, parsed_args):
53+
self.log.debug('take_action(%s)', parsed_args)
54+
columns = ('ID', 'Enabled', 'Name', 'Description')
55+
identity_client = self.app.client_manager.identity
56+
data = identity_client.federation.domains.list()
57+
return (columns,
58+
(utils.get_item_properties(
59+
s, columns,
60+
formatters={},
61+
) for s in data))
62+
63+
64+
class ListAccessibleProjects(lister.Lister):
65+
"""List accessible projects"""
66+
67+
log = logging.getLogger(__name__ + '.ListAccessibleProjects')
68+
69+
@auth_with_unscoped_saml
70+
def take_action(self, parsed_args):
71+
self.log.debug('take_action(%s)', parsed_args)
72+
columns = ('ID', 'Domain ID', 'Enabled', 'Name')
73+
identity_client = self.app.client_manager.identity
74+
data = identity_client.federation.projects.list()
75+
return (columns,
76+
(utils.get_item_properties(
77+
s, columns,
78+
formatters={},
79+
) for s in data))

openstackclient/tests/fakes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ def __init__(self):
199199
self.network = None
200200
self.session = None
201201
self.auth_ref = None
202+
self.auth_plugin_name = None
202203

203204

204205
class FakeModule(object):

openstackclient/tests/identity/v3/fakes.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,19 @@
285285
}
286286

287287

288+
class FakeAuth(object):
289+
def __init__(self, auth_method_class=None):
290+
self._auth_method_class = auth_method_class
291+
292+
def get_token(self, *args, **kwargs):
293+
return token_id
294+
295+
296+
class FakeSession(object):
297+
def __init__(self, **kwargs):
298+
self.auth = FakeAuth()
299+
300+
288301
class FakeIdentityv3Client(object):
289302
def __init__(self, **kwargs):
290303
self.domains = mock.Mock()
@@ -320,6 +333,10 @@ def __init__(self, **kwargs):
320333
self.mappings.resource_class = fakes.FakeResource(None, {})
321334
self.protocols = mock.Mock()
322335
self.protocols.resource_class = fakes.FakeResource(None, {})
336+
self.projects = mock.Mock()
337+
self.projects.resource_class = fakes.FakeResource(None, {})
338+
self.domains = mock.Mock()
339+
self.domains.resource_class = fakes.FakeResource(None, {})
323340

324341

325342
class FakeFederatedClient(FakeIdentityv3Client):
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# Licensed under the Apache License, Version 2.0 (the "License"); you may
2+
# not use this file except in compliance with the License. You may obtain
3+
# a copy of the License at
4+
#
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
#
7+
# Unless required by applicable law or agreed to in writing, software
8+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10+
# License for the specific language governing permissions and limitations
11+
# under the License.
12+
13+
import copy
14+
15+
from openstackclient.common import exceptions
16+
from openstackclient.identity.v3 import unscoped_saml
17+
from openstackclient.tests import fakes
18+
from openstackclient.tests.identity.v3 import fakes as identity_fakes
19+
20+
21+
class TestUnscopedSAML(identity_fakes.TestFederatedIdentity):
22+
23+
def setUp(self):
24+
super(TestUnscopedSAML, self).setUp()
25+
26+
federation_lib = self.app.client_manager.identity.federation
27+
self.projects_mock = federation_lib.projects
28+
self.projects_mock.reset_mock()
29+
self.domains_mock = federation_lib.domains
30+
self.domains_mock.reset_mock()
31+
32+
33+
class TestProjectList(TestUnscopedSAML):
34+
35+
def setUp(self):
36+
super(TestProjectList, self).setUp()
37+
38+
self.projects_mock.list.return_value = [
39+
fakes.FakeResource(
40+
None,
41+
copy.deepcopy(identity_fakes.PROJECT),
42+
loaded=True,
43+
),
44+
]
45+
46+
# Get the command object to test
47+
self.cmd = unscoped_saml.ListAccessibleProjects(self.app, None)
48+
49+
def test_accessible_projects_list(self):
50+
self.app.client_manager.auth_plugin_name = 'v3unscopedsaml'
51+
arglist = []
52+
verifylist = []
53+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
54+
55+
# DisplayCommandBase.take_action() returns two tuples
56+
columns, data = self.cmd.take_action(parsed_args)
57+
58+
self.projects_mock.list.assert_called_with()
59+
60+
collist = ('ID', 'Domain ID', 'Enabled', 'Name')
61+
self.assertEqual(columns, collist)
62+
datalist = ((
63+
identity_fakes.project_id,
64+
identity_fakes.domain_id,
65+
True,
66+
identity_fakes.project_name,
67+
), )
68+
self.assertEqual(tuple(data), datalist)
69+
70+
def test_accessible_projects_list_wrong_auth(self):
71+
auth = identity_fakes.FakeAuth("wrong auth")
72+
self.app.client_manager.identity.session.auth = auth
73+
arglist = []
74+
verifylist = []
75+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
76+
77+
self.assertRaises(exceptions.CommandError,
78+
self.cmd.take_action,
79+
parsed_args)
80+
81+
82+
class TestDomainList(TestUnscopedSAML):
83+
84+
def setUp(self):
85+
super(TestDomainList, self).setUp()
86+
87+
self.domains_mock.list.return_value = [
88+
fakes.FakeResource(
89+
None,
90+
copy.deepcopy(identity_fakes.DOMAIN),
91+
loaded=True,
92+
),
93+
]
94+
95+
# Get the command object to test
96+
self.cmd = unscoped_saml.ListAccessibleDomains(self.app, None)
97+
98+
def test_accessible_domains_list(self):
99+
self.app.client_manager.auth_plugin_name = 'v3unscopedsaml'
100+
arglist = []
101+
verifylist = []
102+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
103+
104+
# DisplayCommandBase.take_action() returns two tuples
105+
columns, data = self.cmd.take_action(parsed_args)
106+
107+
self.domains_mock.list.assert_called_with()
108+
109+
collist = ('ID', 'Enabled', 'Name', 'Description')
110+
self.assertEqual(columns, collist)
111+
datalist = ((
112+
identity_fakes.domain_id,
113+
True,
114+
identity_fakes.domain_name,
115+
identity_fakes.domain_description,
116+
), )
117+
self.assertEqual(tuple(data), datalist)
118+
119+
def test_accessible_domains_list_wrong_auth(self):
120+
auth = identity_fakes.FakeAuth("wrong auth")
121+
self.app.client_manager.identity.session.auth = auth
122+
arglist = []
123+
verifylist = []
124+
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
125+
126+
self.assertRaises(exceptions.CommandError,
127+
self.cmd.take_action,
128+
parsed_args)

setup.cfg

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,9 @@ openstack.identity.v3 =
240240
federation_protocol_set = openstackclient.identity.v3.federation_protocol:SetProtocol
241241
federation_protocol_show = openstackclient.identity.v3.federation_protocol:ShowProtocol
242242

243+
federation_domain_list = openstackclient.identity.v3.unscoped_saml:ListAccessibleDomains
244+
federation_project_list = openstackclient.identity.v3.unscoped_saml:ListAccessibleProjects
245+
243246
request_token_authorize = openstackclient.identity.v3.token:AuthorizeRequestToken
244247
request_token_create = openstackclient.identity.v3.token:CreateRequestToken
245248

0 commit comments

Comments
 (0)