|
| 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 | +"""OpenStackConfig subclass for argument compatibility""" |
| 15 | + |
| 16 | +import logging |
| 17 | + |
| 18 | +from os_client_config.config import OpenStackConfig |
| 19 | + |
| 20 | + |
| 21 | +LOG = logging.getLogger(__name__) |
| 22 | + |
| 23 | + |
| 24 | +# Sublcass OpenStackConfig in order to munge config values |
| 25 | +# before auth plugins are loaded |
| 26 | +class OSC_Config(OpenStackConfig): |
| 27 | + |
| 28 | + def _auth_select_default_plugin(self, config): |
| 29 | + """Select a default plugin based on supplied arguments |
| 30 | +
|
| 31 | + Migrated from auth.select_auth_plugin() |
| 32 | + """ |
| 33 | + |
| 34 | + identity_version = config.get('identity_api_version', '') |
| 35 | + |
| 36 | + if config.get('username', None) and not config.get('auth_type', None): |
| 37 | + if identity_version == '3': |
| 38 | + config['auth_type'] = 'v3password' |
| 39 | + elif identity_version.startswith('2'): |
| 40 | + config['auth_type'] = 'v2password' |
| 41 | + else: |
| 42 | + # let keystoneauth figure it out itself |
| 43 | + config['auth_type'] = 'password' |
| 44 | + elif config.get('token', None) and not config.get('auth_type', None): |
| 45 | + if identity_version == '3': |
| 46 | + config['auth_type'] = 'v3token' |
| 47 | + elif identity_version.startswith('2'): |
| 48 | + config['auth_type'] = 'v2token' |
| 49 | + else: |
| 50 | + # let keystoneauth figure it out itself |
| 51 | + config['auth_type'] = 'token' |
| 52 | + else: |
| 53 | + # The ultimate default is similar to the original behaviour, |
| 54 | + # but this time with version discovery |
| 55 | + if not config.get('auth_type', None): |
| 56 | + config['auth_type'] = 'password' |
| 57 | + |
| 58 | + LOG.debug("Auth plugin %s selected" % config['auth_type']) |
| 59 | + return config |
| 60 | + |
| 61 | + def _auth_v2_arguments(self, config): |
| 62 | + """Set up v2-required arguments from v3 info |
| 63 | +
|
| 64 | + Migrated from auth.build_auth_params() |
| 65 | + """ |
| 66 | + |
| 67 | + if ('auth_type' in config and config['auth_type'].startswith("v2")): |
| 68 | + if 'project_id' in config['auth']: |
| 69 | + config['auth']['tenant_id'] = config['auth']['project_id'] |
| 70 | + if 'project_name' in config['auth']: |
| 71 | + config['auth']['tenant_name'] = config['auth']['project_name'] |
| 72 | + return config |
| 73 | + |
| 74 | + def _auth_v2_ignore_v3(self, config): |
| 75 | + """Remove v3 arguemnts if present for v2 plugin |
| 76 | +
|
| 77 | + Migrated from clientmanager.setup_auth() |
| 78 | + """ |
| 79 | + |
| 80 | + # NOTE(hieulq): If USER_DOMAIN_NAME, USER_DOMAIN_ID, PROJECT_DOMAIN_ID |
| 81 | + # or PROJECT_DOMAIN_NAME is present and API_VERSION is 2.0, then |
| 82 | + # ignore all domain related configs. |
| 83 | + if (config.get('identity_api_version', '').startswith('2') and |
| 84 | + config.get('auth_type', None).endswith('password')): |
| 85 | + domain_props = [ |
| 86 | + 'project_domain_id', |
| 87 | + 'project_domain_name', |
| 88 | + 'user_domain_id', |
| 89 | + 'user_domain_name', |
| 90 | + ] |
| 91 | + for prop in domain_props: |
| 92 | + if config['auth'].pop(prop, None) is not None: |
| 93 | + LOG.warning("Ignoring domain related config " + |
| 94 | + prop + " because identity API version is 2.0") |
| 95 | + return config |
| 96 | + |
| 97 | + def _auth_default_domain(self, config): |
| 98 | + """Set a default domain from available arguments |
| 99 | +
|
| 100 | + Migrated from clientmanager.setup_auth() |
| 101 | + """ |
| 102 | + |
| 103 | + identity_version = config.get('identity_api_version', '') |
| 104 | + auth_type = config.get('auth_type', None) |
| 105 | + |
| 106 | + # TODO(mordred): This is a usability improvement that's broadly useful |
| 107 | + # We should port it back up into os-client-config. |
| 108 | + default_domain = config.get('default_domain', None) |
| 109 | + if (identity_version == '3' and |
| 110 | + not auth_type.startswith('v2') and |
| 111 | + default_domain): |
| 112 | + |
| 113 | + # NOTE(stevemar): If PROJECT_DOMAIN_ID or PROJECT_DOMAIN_NAME is |
| 114 | + # present, then do not change the behaviour. Otherwise, set the |
| 115 | + # PROJECT_DOMAIN_ID to 'OS_DEFAULT_DOMAIN' for better usability. |
| 116 | + if ( |
| 117 | + not config['auth'].get('project_domain_id') and |
| 118 | + not config['auth'].get('project_domain_name') |
| 119 | + ): |
| 120 | + config['auth']['project_domain_id'] = default_domain |
| 121 | + |
| 122 | + # NOTE(stevemar): If USER_DOMAIN_ID or USER_DOMAIN_NAME is present, |
| 123 | + # then do not change the behaviour. Otherwise, set the |
| 124 | + # USER_DOMAIN_ID to 'OS_DEFAULT_DOMAIN' for better usability. |
| 125 | + # NOTE(aloga): this should only be set if there is a username. |
| 126 | + # TODO(dtroyer): Move this to os-client-config after the plugin has |
| 127 | + # been loaded so we can check directly if the options are accepted. |
| 128 | + if ( |
| 129 | + auth_type in ("password", "v3password", "v3totp") and |
| 130 | + not config['auth'].get('user_domain_id') and |
| 131 | + not config['auth'].get('user_domain_name') |
| 132 | + ): |
| 133 | + config['auth']['user_domain_id'] = default_domain |
| 134 | + return config |
| 135 | + |
| 136 | + def auth_config_hook(self, config): |
| 137 | + """Allow examination of config values before loading auth plugin |
| 138 | +
|
| 139 | + OpenStackClient will override this to perform additional chacks |
| 140 | + on auth_type. |
| 141 | + """ |
| 142 | + |
| 143 | + config = self._auth_select_default_plugin(config) |
| 144 | + config = self._auth_v2_arguments(config) |
| 145 | + config = self._auth_v2_ignore_v3(config) |
| 146 | + config = self._auth_default_domain(config) |
| 147 | + |
| 148 | + LOG.debug("auth_config_hook(): %s" % config) |
| 149 | + return config |
| 150 | + |
| 151 | + def _validate_auth_ksc(self, config, cloud): |
| 152 | + """Old compatibility hack for OSC, no longer needed/wanted""" |
| 153 | + return config |
| 154 | + |
| 155 | + def _validate_auth(self, config, loader): |
| 156 | + """Validate auth plugin arguments""" |
| 157 | + # May throw a keystoneauth1.exceptions.NoMatchingPlugin |
| 158 | + |
| 159 | + plugin_options = loader.get_options() |
| 160 | + |
| 161 | + for p_opt in plugin_options: |
| 162 | + # if it's in config, win, move it and kill it from config dict |
| 163 | + # if it's in config.auth but not in config we're good |
| 164 | + # deprecated loses to current |
| 165 | + # provided beats default, deprecated or not |
| 166 | + winning_value = self._find_winning_auth_value(p_opt, config) |
| 167 | + if not winning_value: |
| 168 | + winning_value = self._find_winning_auth_value( |
| 169 | + p_opt, config['auth']) |
| 170 | + |
| 171 | + # Clean up after ourselves |
| 172 | + for opt in [p_opt.name] + [o.name for o in p_opt.deprecated]: |
| 173 | + opt = opt.replace('-', '_') |
| 174 | + config.pop(opt, None) |
| 175 | + config['auth'].pop(opt, None) |
| 176 | + |
| 177 | + if winning_value: |
| 178 | + # Prefer the plugin configuration dest value if the value's key |
| 179 | + # is marked as depreciated. |
| 180 | + if p_opt.dest is None: |
| 181 | + config['auth'][p_opt.name.replace('-', '_')] = ( |
| 182 | + winning_value) |
| 183 | + else: |
| 184 | + config['auth'][p_opt.dest] = winning_value |
| 185 | + |
| 186 | + return config |
0 commit comments