522 lines
17 KiB

# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import datetime
import uuid
from keystoneauth1 import _utils
from keystoneauth1.fixture import exception
class _Service(dict):
"""One of the services that exist in the catalog.
You use this by adding a service to a token which returns an instance of
this object and then you can add_endpoints to the service.
def add_endpoint(self, interface, url, region=None, id=None):
data = {'id': id or uuid.uuid4().hex,
'interface': interface,
'url': url,
'region': region,
'region_id': region}
self.setdefault('endpoints', []).append(data)
return data
def add_standard_endpoints(self, public=None, admin=None, internal=None,
ret = []
if public:
ret.append(self.add_endpoint('public', public, region=region))
if admin:
ret.append(self.add_endpoint('admin', admin, region=region))
if internal:
ret.append(self.add_endpoint('internal', internal, region=region))
return ret
class Token(dict):
"""A V3 Keystone token that can be used for testing.
This object is designed to allow clients to generate a correct V3 token for
use in there test code. It should prevent clients from having to know the
correct token format and allow them to test the portions of token handling
that matter to them and not copy and paste sample.
def __init__(self, expires=None, issued=None, user_id=None, user_name=None,
user_domain_id=None, user_domain_name=None, methods=None,
project_id=None, project_name=None, project_domain_id=None,
project_domain_name=None, domain_id=None, domain_name=None,
trust_id=None, trust_impersonation=None, trustee_user_id=None,
trustor_user_id=None, application_credential_id=None,
oauth_access_token_id=None, oauth_consumer_id=None,
audit_id=None, audit_chain_id=None,
is_admin_project=None, project_is_domain=None):
super(Token, self).__init__()
self.user_id = user_id or uuid.uuid4().hex
self.user_name = user_name or uuid.uuid4().hex
self.user_domain_id = user_domain_id or uuid.uuid4().hex
self.user_domain_name = user_domain_name or uuid.uuid4().hex
self.audit_id = audit_id or uuid.uuid4().hex
if not methods:
methods = ['password']
if not issued:
issued = _utils.before_utcnow(minutes=2)
self.issued = issued
except (TypeError, AttributeError):
# issued should be able to be passed as a string so ignore
self.issued_str = issued
if not expires:
expires = self.issued + datetime.timedelta(hours=1)
self.expires = expires
except (TypeError, AttributeError):
# expires should be able to be passed as a string so ignore
self.expires_str = expires
if (project_id or project_name or
project_domain_id or project_domain_name):
if domain_id or domain_name:
self.set_domain_scope(id=domain_id, name=domain_name)
if (trust_id or (trust_impersonation is not None) or
trustee_user_id or trustor_user_id):
if application_credential_id:
if oauth_access_token_id or oauth_consumer_id:
if audit_chain_id:
self.audit_chain_id = audit_chain_id
if is_admin_project is not None:
self.is_admin_project = is_admin_project
def root(self):
return self.setdefault('token', {})
def expires_str(self):
return self.root.get('expires_at')
def expires_str(self, value):
self.root['expires_at'] = value
def expires(self):
return _utils.parse_isotime(self.expires_str)
def expires(self, value):
self.expires_str = value.isoformat()
def issued_str(self):
return self.root.get('issued_at')
def issued_str(self, value):
self.root['issued_at'] = value
def issued(self):
return _utils.parse_isotime(self.issued_str)
def issued(self, value):
self.issued_str = value.isoformat()
def _user(self):
return self.root.setdefault('user', {})
def user_id(self):
return self._user.get('id')
def user_id(self, value):
self._user['id'] = value
def user_name(self):
return self._user.get('name')
def user_name(self, value):
self._user['name'] = value
def _user_domain(self):
return self._user.setdefault('domain', {})
def _user_domain(self, domain):
self._user['domain'] = domain
def user_domain_id(self):
return self._user_domain.get('id')
def user_domain_id(self, value):
self._user_domain['id'] = value
def user_domain_name(self):
return self._user_domain.get('name')
def user_domain_name(self, value):
self._user_domain['name'] = value
def methods(self):
return self.root.setdefault('methods', [])
def project_id(self):
return self.root.get('project', {}).get('id')
def project_id(self, value):
self.root.setdefault('project', {})['id'] = value
def project_is_domain(self):
return self.root.get('is_domain')
def project_is_domain(self, value):
self.root['is_domain'] = value
def project_name(self):
return self.root.get('project', {}).get('name')
def project_name(self, value):
self.root.setdefault('project', {})['name'] = value
def project_domain_id(self):
return self.root.get('project', {}).get('domain', {}).get('id')
def project_domain_id(self, value):
project = self.root.setdefault('project', {})
project.setdefault('domain', {})['id'] = value
def project_domain_name(self):
return self.root.get('project', {}).get('domain', {}).get('name')
def project_domain_name(self, value):
project = self.root.setdefault('project', {})
project.setdefault('domain', {})['name'] = value
def domain_id(self):
return self.root.get('domain', {}).get('id')
def domain_id(self, value):
self.root.setdefault('domain', {})['id'] = value
def domain_name(self):
return self.root.get('domain', {}).get('name')
def domain_name(self, value):
self.root.setdefault('domain', {})['name'] = value
def system(self):
return self.root.get('system', {})
def system(self, value):
return self.root.setdefault('system', value)
def trust_id(self):
return self.root.get('OS-TRUST:trust', {}).get('id')
def trust_id(self, value):
self.root.setdefault('OS-TRUST:trust', {})['id'] = value
def trust_impersonation(self):
return self.root.get('OS-TRUST:trust', {}).get('impersonation')
def trust_impersonation(self, value):
self.root.setdefault('OS-TRUST:trust', {})['impersonation'] = value
def trustee_user_id(self):
trust = self.root.get('OS-TRUST:trust', {})
return trust.get('trustee_user', {}).get('id')
def trustee_user_id(self, value):
trust = self.root.setdefault('OS-TRUST:trust', {})
trust.setdefault('trustee_user', {})['id'] = value
def trustor_user_id(self):
trust = self.root.get('OS-TRUST:trust', {})
return trust.get('trustor_user', {}).get('id')
def trustor_user_id(self, value):
trust = self.root.setdefault('OS-TRUST:trust', {})
trust.setdefault('trustor_user', {})['id'] = value
def application_credential_id(self):
return self.root.get('application_credential', {}).get('id')
def application_credential_id(self, value):
application_credential = self.root.setdefault(
'application_credential', {})
application_credential.setdefault('id', value)
def application_credential_access_rules(self):
return self.root.get('application_credential', {}).get('access_rules')
def application_credential_access_rules(self, value):
application_credential = self.root.setdefault(
'application_credential', {})
application_credential.setdefault('access_rules', value)
def oauth_access_token_id(self):
return self.root.get('OS-OAUTH1', {}).get('access_token_id')
def oauth_access_token_id(self, value):
self.root.setdefault('OS-OAUTH1', {})['access_token_id'] = value
def oauth_consumer_id(self):
return self.root.get('OS-OAUTH1', {}).get('consumer_id')
def oauth_consumer_id(self, value):
self.root.setdefault('OS-OAUTH1', {})['consumer_id'] = value
def audit_id(self):
return self.root.get('audit_ids', [])[0]
except IndexError:
return None
def audit_id(self, value):
audit_chain_id = self.audit_chain_id
lval = [value] if audit_chain_id else [value, audit_chain_id]
self.root['audit_ids'] = lval
def audit_chain_id(self):
return self.root.get('audit_ids', [])[1]
except IndexError:
return None
def audit_chain_id(self, value):
self.root['audit_ids'] = [self.audit_id, value]
def role_ids(self):
return [r['id'] for r in self.root.get('roles', [])]
def role_names(self):
return [r['name'] for r in self.root.get('roles', [])]
def is_admin_project(self):
return self.root.get('is_admin_project')
def is_admin_project(self, value):
self.root['is_admin_project'] = value
def is_admin_project(self):
self.root.pop('is_admin_project', None)
def validate(self):
project = self.root.get('project')
domain = self.root.get('domain')
system = self.root.get('system')
trust = self.root.get('OS-TRUST:trust')
catalog = self.root.get('catalog')
roles = self.root.get('roles')
scoped = project or domain or trust
if sum((bool(project), bool(domain), bool(trust), bool(system))) > 1:
msg = 'You cannot scope to multiple targets'
raise exception.FixtureValidationError(msg)
if catalog and not scoped:
msg = 'You cannot have a service catalog on an unscoped token'
raise exception.FixtureValidationError(msg)
if scoped and not self.user.get('roles'):
msg = 'You must have roles on a token to scope it'
raise exception.FixtureValidationError(msg)
if bool(scoped) != bool(roles):
msg = 'You must be scoped to have roles and vice-versa'
raise exception.FixtureValidationError(msg)
def add_role(self, name=None, id=None):
roles = self.root.setdefault('roles', [])
data = {'id': id or uuid.uuid4().hex,
'name': name or uuid.uuid4().hex}
return data
def add_service(self, type, name=None, id=None):
service = _Service(type=type, id=id or uuid.uuid4().hex)
if name:
service['name'] = name
self.root.setdefault('catalog', []).append(service)
return service
def remove_service(self, type):
self.root.setdefault('catalog', [])
self.root['catalog'] = [
f for f in self.root.setdefault('catalog', [])
if f['type'] != type]
def set_project_scope(self, id=None, name=None, domain_id=None,
domain_name=None, is_domain=None):
self.project_id = id or uuid.uuid4().hex
self.project_name = name or uuid.uuid4().hex
self.project_domain_id = domain_id or uuid.uuid4().hex
self.project_domain_name = domain_name or uuid.uuid4().hex
if is_domain is not None:
self.project_is_domain = is_domain
def set_domain_scope(self, id=None, name=None):
self.domain_id = id or uuid.uuid4().hex
self.domain_name = name or uuid.uuid4().hex
def set_system_scope(self):
# NOTE(lbragstad): In the future it might be possible to scope a token
# to a subset of the entire system (e.g. a specific service, region, or
# service within a region). Until then, the only system scope is the
# entire system.
self.system = {'all': True}
def set_trust_scope(self, id=None, impersonation=False,
trustee_user_id=None, trustor_user_id=None):
self.trust_id = id or uuid.uuid4().hex
self.trust_impersonation = impersonation
self.trustee_user_id = trustee_user_id or uuid.uuid4().hex
self.trustor_user_id = trustor_user_id or uuid.uuid4().hex
def set_oauth(self, access_token_id=None, consumer_id=None):
self.oauth_access_token_id = access_token_id or uuid.uuid4().hex
self.oauth_consumer_id = consumer_id or uuid.uuid4().hex
def set_application_credential(self, application_credential_id,
self.application_credential_id = application_credential_id
if access_rules is not None:
self.application_credential_access_rules = access_rules
def service_providers(self):
return self.root.get('service_providers')
def add_service_provider(self, sp_id, sp_auth_url, sp_url):
_service_providers = self.root.setdefault('service_providers', [])
sp = {'id': sp_id, 'auth_url': sp_auth_url, 'sp_url': sp_url}
return sp
def set_bind(self, name, data):
self.root.setdefault('bind', {})[name] = data
class V3FederationToken(Token):
"""A V3 Keystone Federation token that can be used for testing.
Similar to V3Token, this object is designed to allow clients to generate
a correct V3 federation token for use in test code.
def __init__(self, methods=None, identity_provider=None, protocol=None,
methods = methods or ['saml2']
super(V3FederationToken, self).__init__(methods=methods)
self._user_domain = {'id': V3FederationToken.FEDERATED_DOMAIN_ID}
self.add_federation_info_to_user(identity_provider, protocol, groups)
def add_federation_info_to_user(self, identity_provider=None,
protocol=None, groups=None):
data = {
"identity_provider": identity_provider or uuid.uuid4().hex,
"protocol": protocol or uuid.uuid4().hex,
"groups": groups or [{"id": uuid.uuid4().hex}]
return data