def test_authenticate_success(self): cs = client.Client("username", "password", "project_id", "auth_url") management_url = 'https://localhost/v2.1/443470' auth_response = utils.TestResponse({ 'status_code': 204, 'headers': { 'x-server-management-url': management_url, 'x-auth-token': '1b751d74-de0c-46ae-84f0-915744b582d1', }, }) mock_request = mock.Mock(return_value=(auth_response)) @mock.patch.object(requests, "request", mock_request) def test_auth_call(): cs.client.authenticate() headers = { 'Accept': 'application/json', 'X-Auth-User': '******', 'X-Auth-Key': 'password', 'X-Auth-Project-Id': 'project_id', 'User-Agent': cs.client.USER_AGENT } mock_request.assert_called_with( "GET", cs.client.auth_url, headers=headers, **self.TEST_REQUEST_BASE) self.assertEqual(cs.client.management_url, auth_response.headers['x-server-management-url']) self.assertEqual(cs.client.auth_token, auth_response.headers['x-auth-token']) test_auth_call()
def test_auth_manual(self): cs = client.Client("username", "password", "project_id", "auth_url") @mock.patch.object(cs.client, 'authenticate') def test_auth_call(m): cs.authenticate() m.assert_called() test_auth_call()
def test_authenticate_failure(self): cs = client.Client("username", "password", "project_id", "auth_url") auth_response = utils.TestResponse({"status_code": 401}) mock_request = mock.Mock(return_value=(auth_response)) @mock.patch.object(requests, "request", mock_request) def test_auth_call(): self.assertRaises(exceptions.Unauthorized, cs.client.authenticate) test_auth_call()
def get_manilaclient(region_name=None): ks_cfg = cfg.CONF.keystone_authtoken endpoint = ks_client.get_endpoint(region_name, 'share') auth_token = ks_client.get_token() auth_url = ks_client.get_auth_url() c = manila_client.Client(ks_cfg.admin_user, ks_cfg.admin_password, None, auth_url=auth_url) c.client.auth_token = auth_token c.client.management_url = endpoint return c
def test_auth_automatic(self): cs = client.Client("username", "password", "project_id", "auth_url") http_client = cs.client http_client.management_url = '' mock_request = mock.Mock(return_value=(None, None)) @mock.patch.object(http_client, 'request', mock_request) @mock.patch.object(http_client, 'authenticate') def test_auth_call(m): http_client.get('/') m.assert_called() mock_request.assert_called() test_auth_call()
def test_ambiguous_endpoints(self): cs = client.Client("username", "password", "project_id", "auth_url/v2.0", service_type='compute') resp = { "access": { "token": { "expires": "12345", "id": "FAKE_ID", }, "serviceCatalog": [ { "adminURL": "http://*****:*****@mock.patch.object(requests, "request", mock_request) def test_auth_call(): self.assertRaises(exceptions.AmbiguousEndpoints, cs.client.authenticate) test_auth_call()
def test_authenticate_failure(self): cs = client.Client("username", "password", "project_id", "auth_url/v2.0") resp = {"unauthorized": {"message": "Unauthorized", "code": "401"}} auth_response = utils.TestResponse({ "status_code": 401, "text": json.dumps(resp), }) mock_request = mock.Mock(return_value=(auth_response)) @mock.patch.object(requests, "request", mock_request) def test_auth_call(): self.assertRaises(exceptions.Unauthorized, cs.client.authenticate) test_auth_call()
def manilaclient(request): insecure = getattr(settings, 'OPENSTACK_SSL_NO_VERIFY', False) cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None) try: manila_url = base.url_for(request, 'share') except exceptions.ServiceCatalogException: LOG.debug('no share service configured.') return None c = manila_client.Client(request.user.username, input_auth_token=request.user.token.id, project_id=request.user.tenant_id, service_catalog_url=manila_url, insecure=insecure, cacert=cacert, http_log_debug=settings.DEBUG) c.client.auth_token = request.user.token.id c.client.management_url = manila_url return c
def test_authenticate_tenant_id(self): cs = client.Client("username", "password", auth_url="auth_url/v2.0", tenant_id='tenant_id', service_type='compute') resp = { "access": { "token": { "expires": "12345", "id": "FAKE_ID", "tenant": { "description": None, "enabled": True, "id": "tenant_id", "name": "demo" } # tenant associated with token }, "serviceCatalog": [ { "type": "compute", "endpoints": [ { "region": "RegionOne", "adminURL": "http://*****:*****@mock.patch.object(requests, "request", mock_request) def test_auth_call(): cs.client.authenticate() headers = { 'User-Agent': cs.client.USER_AGENT, 'Content-Type': 'application/json', 'Accept': 'application/json', } body = { 'auth': { 'passwordCredentials': { 'username': cs.client.user, 'password': cs.client.password, }, 'tenantId': cs.client.tenant_id, }, } token_url = cs.client.auth_url + "/tokens" mock_request.assert_called_with( "POST", token_url, headers=headers, data=json.dumps(body), allow_redirects=True, **self.TEST_REQUEST_BASE) endpoints = resp["access"]["serviceCatalog"][0]['endpoints'] public_url = endpoints[0]["publicURL"].rstrip('/') self.assertEqual(cs.client.management_url, public_url) token_id = resp["access"]["token"]["id"] self.assertEqual(cs.client.auth_token, token_id) tenant_id = resp["access"]["token"]["tenant"]["id"] self.assertEqual(cs.client.tenant_id, tenant_id) test_auth_call()
def test_auth_redirect(self): cs = client.Client("username", "password", "project_id", "auth_url/v2", service_type='compute') dict_correct_response = { "access": { "token": { "expires": "12345", "id": "FAKE_ID", }, "serviceCatalog": [ { "type": "compute", "endpoints": [ { "adminURL": "http://*****:*****@mock.patch.object(requests, "request", mock_request) def test_auth_call(): cs.client.authenticate() headers = { 'User-Agent': cs.client.USER_AGENT, 'Content-Type': 'application/json', 'Accept': 'application/json', } body = { 'auth': { 'passwordCredentials': { 'username': cs.client.user, 'password': cs.client.password, }, 'tenantName': cs.client.projectid, }, } token_url = cs.client.auth_url + "/tokens" mock_request.assert_called_with( "POST", token_url, headers=headers, data=json.dumps(body), allow_redirects=True, **self.TEST_REQUEST_BASE) resp = dict_correct_response endpoints = resp["access"]["serviceCatalog"][0]['endpoints'] public_url = endpoints[0]["publicURL"].rstrip('/') self.assertEqual(cs.client.management_url, public_url) token_id = resp["access"]["token"]["id"] self.assertEqual(cs.client.auth_token, token_id) test_auth_call()
def test_400_api_connection(self): """Simple api calls to check service is up and responding""" u.log.debug('Checking api functionality...') # This handles both keystone v2 and v3. # For keystone v2 we need a user: # - 'demo' user # - has a project 'demo' # - in the 'demo' project # - with an 'admin' role # For keystone v3 we need a user: # - 'default' domain # - 'demo' user # - 'demo' project # - 'admin' role -- to be able to delete. # manila requires a user with creator or admin role on the project # when creating a secret (which this test does). Therefore, we create # a demo user, demo project, and then get a demo manila client and do # the secret. ensure that the default domain is created. keystone_ip = self.keystone_sentry.info['public-address'] if self._keystone_version == '2': # find or create the 'demo' tenant (project) tenant = self._find_or_create( items=self.keystone.tenants.list(), key=lambda t: t.name == 'demo', create=lambda: self.keystone.tenants.create( tenant_name="demo", description="Demo for testing manila", enabled=True)) # find or create the demo user demo_user = self._find_or_create( items=self.keystone.users.list(), key=lambda u: u.name == 'demo', create=lambda: self.keystone.users.create( name='demo', password='******', tenant_id=tenant.id)) # find the admin role # already be created - if not, then this will fail later. admin_role = self._find_or_create( items=self.keystone.roles.list(), key=lambda r: r.name.lower() == 'admin', create=lambda: None) # grant the role if it isn't already created. # now grant the creator role to the demo user. self._find_or_create( items=self.keystone.roles.roles_for_user(demo_user, tenant=tenant), key=lambda r: r.name.lower() == admin_role.name.lower(), create=lambda: self.keystone.roles.add_user_role( demo_user, admin_role, tenant=tenant)) # Authenticate demo user with keystone self.demo_user_session, _ = u.get_keystone_session( keystone_ip, 'demo', 'pass', api_version=2, project_name='demo', ) else: # find or create the 'default' domain domain = self._find_or_create( items=self.keystone.domains.list(), key=lambda u: u.name == 'Default', create=lambda: self.keystone.domains.create( "Default", description="domain for manila testing", enabled=True)) # find or create the 'demo' user demo_user = self._find_or_create( items=self.keystone.users.list(domain=domain.id), key=lambda u: u.name == 'demo', create=lambda: self.keystone.users.create( 'demo', domain=domain.id, description="Demo user for manila tests", enabled=True, email="*****@*****.**", password="******")) # find or create the 'demo' project demo_project = self._find_or_create( items=self.keystone.projects.list(domain=domain.id), key=lambda x: x.name == 'demo', create=lambda: self.keystone.projects.create( 'demo', domain=domain.id, description='manila testing project', enabled=True)) # create the role for the user - needs to be admin so that the # secret can be deleted - note there is only one admin role, and it # should already be created - if not, then this will fail later. admin_role = self._find_or_create( items=self.keystone.roles.list(), key=lambda r: r.name.lower() == 'admin', create=lambda: None) # now grant the creator role to the demo user. try: self.keystone.roles.check(role=admin_role, user=demo_user, project=demo_project) except keystoneclient.exceptions.NotFound: # create it if it isn't found self.keystone.roles.grant(role=admin_role, user=demo_user, project=demo_project) self.demo_user_session, _ = u.get_keystone_session( keystone_ip, 'demo', 'pass', api_version=3, project_name='demo', user_domain_name='default', project_domain_name='default', ) # Authenticate admin with manila endpoint manila_ep = self.keystone.service_catalog.url_for( service_type='share', interface='publicURL') manila = manila_client.Client(session=self.demo_user_session, endpoint=manila_ep) # now just try a list the shares # NOTE(AJK) the 'search_opts={}' is needed to work around Bug#1707303 manila.shares.list(search_opts={}) u.log.debug('OK') u.log.debug('OK')
def test_400_api_connection(self): """Simple api calls to check service is up and responding""" u.log.debug('Checking api functionality...') # This handles both keystone v2 and v3. # For keystone v2 we need a user: # - 'demo' user # - has a project 'demo' # - in the 'demo' project # - with an 'admin' role # For keystone v3 we need a user: # - 'default' domain # - 'demo' user # - 'demo' project # - 'admin' role -- to be able to delete. # manila requires a user with creator or admin role on the project # when creating a secret (which this test does). Therefore, we create # a demo user, demo project, and then get a demo manila client and do # the secret. ensure that the default domain is created. if self._keystone_version == '2': # find or create the 'demo' tenant (project) tenant = self._find_or_create( items=self.keystone.tenants.list(), key=lambda t: t.name == 'demo', create=lambda: self.keystone.tenants.create( tenant_name="demo", description="Demo for testing manila", enabled=True)) # find or create the demo user demo_user = self._find_or_create( items=self.keystone.users.list(), key=lambda u: u.name == 'demo', create=lambda: self.keystone.users.create( name='demo', password='******', tenant_id=tenant.id)) # find the admin role # already be created - if not, then this will fail later. admin_role = self._find_or_create( items=self.keystone.roles.list(), key=lambda r: r.name.lower() == 'admin', create=lambda: None) # grant the role if it isn't already created. # now grant the creator role to the demo user. self._find_or_create( items=self.keystone.roles.roles_for_user(demo_user, tenant=tenant), key=lambda r: r.name.lower() == admin_role.name.lower(), create=lambda: self.keystone.roles.add_user_role( demo_user, admin_role, tenant=tenant)) # now we can finally get the manila client and create the secret keystone_ep = self.keystone.service_catalog.url_for( service_type='identity', endpoint_type='publicURL') auth = keystone_identity.v2.Password(username=demo_user.name, password='******', tenant_name=tenant.name, auth_url=keystone_ep) else: # find or create the 'default' domain domain = self._find_or_create( items=self.keystone.domains.list(), key=lambda u: u.name == 'default', create=lambda: self.keystone.domains.create( "default", description="domain for manila testing", enabled=True)) # find or create the 'demo' user demo_user = self._find_or_create( items=self.keystone.users.list(domain=domain.id), key=lambda u: u.name == 'demo', create=lambda: self.keystone.users.create( 'demo', domain=domain.id, description="Demo user for manila tests", enabled=True, email="*****@*****.**", password="******")) # find or create the 'demo' project demo_project = self._find_or_create( items=self.keystone.projects.list(domain=domain.id), key=lambda x: x.name == 'demo', create=lambda: self.keystone.projects.create( 'demo', domain=domain.id, description='manila testing project', enabled=True)) # create the role for the user - needs to be admin so that the # secret can be deleted - note there is only one admin role, and it # should already be created - if not, then this will fail later. admin_role = self._find_or_create( items=self.keystone.roles.list(), key=lambda r: r.name.lower() == 'admin', create=lambda: None) # now grant the creator role to the demo user. try: self.keystone.roles.check(role=admin_role, user=demo_user, project=demo_project) except keystoneclient.exceptions.NotFound: # create it if it isn't found self.keystone.roles.grant(role=admin_role, user=demo_user, project=demo_project) # now we can finally get the manila client and create the secret keystone_ep = self.keystone.service_catalog.url_for( service_type='identity', endpoint_type='publicURL') auth = keystone_identity.v3.Password( user_domain_name=domain.name, username=demo_user.name, password='******', project_domain_name=domain.name, project_name=demo_project.name, auth_url=keystone_ep) # Now we carry on with common v2 and v3 code sess = keystone_session.Session(auth=auth) # Authenticate admin with manila endpoint manila_ep = self.keystone.service_catalog.url_for( service_type='key-manager', endpoint_type='publicURL') manila = manila_client.Client(session=sess, endpoint=manila_ep) # now create the secret. my_secret = manila.secrets.create() my_secret.name = u'Random plain text password' my_secret.payload = u'password' my_secret_ref = my_secret.store() assert (my_secret_ref is not None) # and now delete the secret my_secret.delete() u.log.debug('OK')