def get_configured_credentials(credential_type, fill_in=True, identity_version=None): identity_version = identity_version or CONF.identity.auth_version if identity_version not in ('v2', 'v3'): raise exceptions.InvalidConfiguration('Unsupported auth version: %s' % identity_version) if credential_type not in CREDENTIAL_TYPES: raise exceptions.InvalidCredentials() conf_attributes = ['username', 'password', 'tenant_name'] if identity_version == 'v3': conf_attributes.append('domain_name') # Read the parts of credentials from config params = DEFAULT_PARAMS.copy() section, prefix = CREDENTIAL_TYPES[credential_type] for attr in conf_attributes: _section = getattr(CONF, section) if prefix is None: params[attr] = getattr(_section, attr) else: params[attr] = getattr(_section, prefix + "_" + attr) # Build and validate credentials. We are reading configured credentials, # so validate them even if fill_in is False credentials = get_credentials(fill_in=fill_in, identity_version=identity_version, **params) if not fill_in: if not credentials.is_valid(): msg = ("The %s credentials are incorrectly set in the config file." " Double check that all required values are assigned" % credential_type) raise exceptions.InvalidConfiguration(msg) return credentials
def get_credentials(credential_type=None, fill_in=True, **kwargs): """ Builds a credentials object based on the configured auth_version :param credential_type (string): requests credentials from tempest configuration file. Valid values are defined in Credentials.TYPE. :param kwargs (dict): take into account only if credential_type is not specified or None. Dict of credential key/value pairs Examples: Returns credentials from the provided parameters: >>> get_credentials(username='******', password='******') Returns credentials from tempest configuration: >>> get_credentials(credential_type='user') """ if CONF.identity.auth_version == 'v2': credential_class = KeystoneV2Credentials auth_provider_class = KeystoneV2AuthProvider elif CONF.identity.auth_version == 'v3': credential_class = KeystoneV3Credentials auth_provider_class = KeystoneV3AuthProvider else: raise exceptions.InvalidConfiguration('Unsupported auth version') if credential_type is not None: creds = credential_class.get_default(credential_type) else: creds = credential_class(**kwargs) # Fill in the credentials fields that were not specified if fill_in: auth_provider = auth_provider_class(creds) creds = auth_provider.fill_credentials() return creds
def get_default(cls, credentials_type): if credentials_type not in cls.TYPES: raise exceptions.InvalidCredentials() creds = cls._get_default(credentials_type) if not creds.is_valid(): raise exceptions.InvalidConfiguration() return creds
def get_default(cls, credentials_type): if credentials_type not in cls.TYPES: raise exceptions.InvalidCredentials() creds = cls._get_default(credentials_type) if not creds.is_valid(): msg = ("The %s credentials are incorrectly set in the config file." " Double check that all required values are assigned" % credentials_type) raise exceptions.InvalidConfiguration(msg) return creds