| 1 |
# |
| 2 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 |
# not use this file except in compliance with the License. You may obtain |
| 4 |
# a copy of the License at |
| 5 |
# |
| 6 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
# |
| 8 |
# Unless required by applicable law or agreed to in writing, software |
| 9 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 |
# License for the specific language governing permissions and limitations |
| 12 |
# under the License. |
| 13 |
|
| 14 |
import os |
| 15 |
|
| 16 |
from keystoneauth1 import loading |
| 17 |
from keystoneauth1 import plugin |
| 18 |
|
| 19 |
|
| 20 |
class AodhNoAuthPlugin(plugin.BaseAuthPlugin): |
| 21 |
"""No authentication plugin for Aodh |
| 22 |
|
| 23 |
This is a keystoneauth plugin that instead of |
| 24 |
doing authentication, it just fill the 'x-user-id' |
| 25 |
and 'x-project-id' headers with the user provided one. |
| 26 |
""" |
| 27 |
def __init__(self, user_id, project_id, roles, endpoint): |
| 28 |
self._user_id = user_id |
| 29 |
self._project_id = project_id |
| 30 |
self._endpoint = endpoint |
| 31 |
self._roles = roles |
| 32 |
|
| 33 |
def get_token(self, session, **kwargs): |
| 34 |
return '<no-token-needed>' |
| 35 |
|
| 36 |
def get_headers(self, session, **kwargs): |
| 37 |
return {'x-user-id': self._user_id, |
| 38 |
'x-project-id': self._project_id, |
| 39 |
'x-roles': self._roles} |
| 40 |
|
| 41 |
def get_user_id(self, session, **kwargs): |
| 42 |
return self._user_id |
| 43 |
|
| 44 |
def get_project_id(self, session, **kwargs): |
| 45 |
return self._project_id |
| 46 |
|
| 47 |
def get_endpoint(self, session, **kwargs): |
| 48 |
return self._endpoint |
| 49 |
|
| 50 |
|
| 51 |
class AodhOpt(loading.Opt): |
| 52 |
@property |
| 53 |
def argparse_args(self): |
| 54 |
return ['--%s' % o.name for o in self._all_opts] |
| 55 |
|
| 56 |
@property |
| 57 |
def argparse_default(self): |
| 58 |
# select the first ENV that is not false-y or return None |
| 59 |
for o in self._all_opts: |
| 60 |
v = os.environ.get('AODH_%s' % o.name.replace('-', '_').upper()) |
| 61 |
if v: |
| 62 |
return v |
| 63 |
return self.default |
| 64 |
|
| 65 |
|
| 66 |
class AodhNoAuthLoader(loading.BaseLoader): |
| 67 |
plugin_class = AodhNoAuthPlugin |
| 68 |
|
| 69 |
def get_options(self): |
| 70 |
options = super().get_options() |
| 71 |
options.extend([ |
| 72 |
AodhOpt('user-id', help='User ID', required=True), |
| 73 |
AodhOpt('project-id', help='Project ID', required=True), |
| 74 |
AodhOpt('roles', help='Roles', default="admin"), |
| 75 |
AodhOpt('aodh-endpoint', help='Aodh endpoint', |
| 76 |
dest="endpoint", required=True), |
| 77 |
]) |
| 78 |
return options |