Example #1
0
    def test_inequality(self):
        cc1 = cloud_config.CloudConfig("test1", "region-al", fake_config_dict)

        cc2 = cloud_config.CloudConfig("test2", "region-al", fake_config_dict)
        self.assertNotEqual(cc1, cc2)

        cc2 = cloud_config.CloudConfig("test1", "region-xx", fake_config_dict)
        self.assertNotEqual(cc1, cc2)

        cc2 = cloud_config.CloudConfig("test1", "region-al", {})
        self.assertNotEqual(cc1, cc2)
Example #2
0
    def test_verify_cacert(self):
        config_dict = copy.deepcopy(fake_config_dict)
        config_dict['cacert'] = "certfile"

        config_dict['verify'] = False
        cc = cloud_config.CloudConfig("test1", "region-xx", config_dict)
        (verify, cert) = cc.get_requests_verify_args()
        self.assertFalse(verify)

        config_dict['verify'] = True
        cc = cloud_config.CloudConfig("test1", "region-xx", config_dict)
        (verify, cert) = cc.get_requests_verify_args()
        self.assertEqual("certfile", verify)
Example #3
0
    def test_getters(self):
        cc = cloud_config.CloudConfig("test1", "region-al", fake_services_dict)

        self.assertEqual(['compute', 'identity', 'image', 'volume'],
                         sorted(cc.get_services()))
        self.assertEqual({
            'password': '******',
            'username': '******'
        }, cc.get_auth_args())
        self.assertEqual('public', cc.get_interface())
        self.assertEqual('public', cc.get_interface('compute'))
        self.assertEqual('admin', cc.get_interface('identity'))
        self.assertEqual('region-al', cc.get_region_name())
        self.assertEqual('region-al', cc.get_region_name('image'))
        self.assertEqual('region-bl', cc.get_region_name('compute'))
        self.assertIsNone(cc.get_api_version('image'))
        self.assertEqual('2', cc.get_api_version('compute'))
        self.assertEqual('mage', cc.get_service_type('image'))
        self.assertEqual('compute', cc.get_service_type('compute'))
        self.assertEqual('1', cc.get_api_version('volume'))
        self.assertEqual('volume', cc.get_service_type('volume'))
        self.assertEqual('http://compute.example.com',
                         cc.get_endpoint('compute'))
        self.assertIsNone(cc.get_endpoint('image'))
        self.assertIsNone(cc.get_service_name('compute'))
        self.assertEqual('locks', cc.get_service_name('identity'))
Example #4
0
 def test_legacy_client_object_store_password_v2(self,
                                                 mock_get_session_endpoint,
                                                 mock_get_auth_args):
     mock_client = mock.Mock()
     mock_get_session_endpoint.return_value = 'http://swift.example.com'
     mock_get_auth_args.return_value = dict(
         username='******',
         password='******',
         project_name='testproject',
         auth_url='http://example.com',
     )
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_legacy_client('object-store', mock_client)
     mock_client.assert_called_with(session=mock.ANY,
                                    os_options={
                                        'region_name': 'region-al',
                                        'service_type': 'object-store',
                                        'object_storage_url': None,
                                        'endpoint_type': 'public',
                                    })
Example #5
0
    def test_client_manager_k2k_auth_setup(self):
        loader = loading.get_plugin_loader('password')
        auth_plugin = loader.load_from_options(**AUTH_DICT)
        cli_options = defaults.get_defaults()
        cli_options.update({
            'auth_type': 'password',
            'auth': AUTH_DICT,
            'interface': fakes.INTERFACE,
            'region_name': fakes.REGION_NAME,
            'service_provider': fakes.SERVICE_PROVIDER_ID,
            'remote_project_id': fakes.PROJECT_ID
        })
        client_manager = self._clientmanager_class()(
            cli_options=cloud_config.CloudConfig(
                name='t1',
                region='1',
                config=cli_options,
                auth_plugin=auth_plugin,
            ),
            api_version={
                'identity': '3',
            },
        )

        self.assertFalse(client_manager._auth_setup_completed)
        client_manager.setup_auth()
        # Note(knikolla): Make sure that the auth object is of the correct
        # type and that the service_provider is correctly set.
        self.assertIsInstance(client_manager.auth, k2k.Keystone2Keystone)
        self.assertEqual(client_manager.auth._sp_id, fakes.SERVICE_PROVIDER_ID)
        self.assertEqual(client_manager.auth.project_id, fakes.PROJECT_ID)
        self.assertTrue(client_manager._auth_setup_completed)
Example #6
0
    def test_client_manager_auth_setup_once(self, check_authn_options_func):
        loader = loading.get_plugin_loader('password')
        auth_plugin = loader.load_from_options(**AUTH_DICT)
        cli_options = defaults.get_defaults()
        cli_options.update({
            'auth_type': 'password',
            'auth': AUTH_DICT,
            'interface': fakes.INTERFACE,
            'region_name': fakes.REGION_NAME,
        })
        client_manager = self._clientmanager_class()(
            cli_options=cloud_config.CloudConfig(
                name='t1',
                region='1',
                config=cli_options,
                auth_plugin=auth_plugin,
            ),
            api_version={
                'identity': '2.0',
            },
        )
        self.assertFalse(client_manager._auth_setup_completed)
        client_manager.setup_auth()
        self.assertTrue(check_authn_options_func.called)
        self.assertTrue(client_manager._auth_setup_completed)

        # now make sure we don't do auth setup the second time around
        # by checking whether check_valid_auth_options() gets called again
        check_authn_options_func.reset_mock()
        client_manager.auth_ref
        check_authn_options_func.assert_not_called()
Example #7
0
 def test_session_endpoint_not_found(self, mock_get_session):
     exc_to_raise = ksa_exceptions.catalog.EndpointNotFound
     mock_get_session.return_value.get_endpoint.side_effect = exc_to_raise
     cc = cloud_config.CloudConfig("test1",
                                   "region-al", {},
                                   auth_plugin=mock.Mock())
     self.assertIsNone(cc.get_session_endpoint('notfound'))
Example #8
0
 def test_override_session_endpoint(self, mock_session):
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     self.assertEqual(cc.get_session_endpoint('telemetry'),
                      fake_services_dict['telemetry_endpoint'])
Example #9
0
    def _make_clientmanager(
        self,
        auth_args=None,
        config_args=None,
        identity_api_version=None,
        auth_plugin_name=None,
        auth_required=None,
    ):

        if identity_api_version is None:
            identity_api_version = '2.0'
        if auth_plugin_name is None:
            auth_plugin_name = 'password'

        if auth_plugin_name.endswith('password'):
            auth_dict = copy.deepcopy(self.default_password_auth)
        elif auth_plugin_name.endswith('token'):
            auth_dict = copy.deepcopy(self.default_token_auth)
        else:
            auth_dict = {}

        if auth_args is not None:
            auth_dict = auth_args

        cli_options = defaults.get_defaults()
        cli_options.update({
            'auth_type': auth_plugin_name,
            'auth': auth_dict,
            'interface': fakes.INTERFACE,
            'region_name': fakes.REGION_NAME,
            # 'workflow_api_version': '2',
        })
        if config_args is not None:
            cli_options.update(config_args)

        loader = loading.get_plugin_loader(auth_plugin_name)
        auth_plugin = loader.load_from_options(**auth_dict)
        client_manager = self._clientmanager_class()(
            cli_options=cloud_config.CloudConfig(
                name='t1',
                region='1',
                config=cli_options,
                auth_plugin=auth_plugin,
            ),
            api_version={
                'identity': identity_api_version,
            },
        )
        client_manager._auth_required = auth_required is True
        client_manager.setup_auth()
        client_manager.auth_ref

        self.assertEqual(
            auth_plugin_name,
            client_manager.auth_plugin_name,
        )
        return client_manager
Example #10
0
    def test_cert_with_key(self):
        config_dict = copy.deepcopy(fake_config_dict)
        config_dict['cacert'] = None
        config_dict['verify'] = False

        config_dict['cert'] = 'cert'
        config_dict['key'] = 'key'

        cc = cloud_config.CloudConfig("test1", "region-xx", config_dict)
        (verify, cert) = cc.get_requests_verify_args()
        self.assertEqual(("cert", "key"), cert)
Example #11
0
 def test_session_endpoint(self, mock_get_session):
     mock_session = mock.Mock()
     mock_get_session.return_value = mock_session
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_session_endpoint('orchestration')
     mock_session.get_endpoint.assert_called_with(
         interface='public',
         service_name=None,
         region_name='region-al',
         service_type='orchestration')
Example #12
0
 def test_get_session(self, mock_session):
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     fake_session = mock.Mock()
     fake_session.additional_user_agent = []
     mock_session.return_value = fake_session
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_session()
     mock_session.assert_called_with(auth=mock.ANY,
                                     verify=True,
                                     cert=None,
                                     timeout=None)
     self.assertEqual(fake_session.additional_user_agent,
                      [('openstacksdk', '1.2.3')])
Example #13
0
 def test_legacy_client_network(self, mock_get_session_endpoint):
     mock_client = mock.Mock()
     mock_get_session_endpoint.return_value = 'http://example.com/v2'
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_legacy_client('network', mock_client)
     mock_client.assert_called_with(api_version='2.0',
                                    endpoint_type='public',
                                    endpoint_override=None,
                                    region_name='region-al',
                                    service_type='network',
                                    session=mock.ANY,
                                    service_name=None)
Example #14
0
 def test_legacy_client_image(self, mock_get_session_endpoint):
     mock_client = mock.Mock()
     mock_get_session_endpoint.return_value = 'http://example.com/v2'
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_legacy_client('image', mock_client)
     mock_client.assert_called_with(
         version=2.0,
         service_name=None,
         endpoint_override='http://example.com',
         region_name='region-al',
         interface='public',
         session=mock.ANY,
         # Not a typo - the config dict above overrides this
         service_type='mage')
Example #15
0
 def test_legacy_client_identity_v3(self, mock_get_session_endpoint):
     mock_client = mock.Mock()
     mock_get_session_endpoint.return_value = 'http://example.com'
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     config_dict['identity_api_version'] = '3'
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_legacy_client('identity', mock_client)
     mock_client.assert_called_with(version='3',
                                    endpoint='http://example.com',
                                    interface='admin',
                                    endpoint_override=None,
                                    region_name='region-al',
                                    service_type='identity',
                                    session=mock.ANY,
                                    service_name='locks')
Example #16
0
 def test_legacy_client_object_store_endpoint(self, mock_get_auth_args):
     mock_client = mock.Mock()
     mock_get_auth_args.return_value = {}
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     config_dict['object_store_endpoint'] = 'http://example.com/swift'
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   config_dict,
                                   auth_plugin=mock.Mock())
     cc.get_legacy_client('object-store', mock_client)
     mock_client.assert_called_with(session=mock.ANY,
                                    os_options={
                                        'region_name': 'region-al',
                                        'service_type': 'object-store',
                                        'object_storage_url':
                                        'http://example.com/swift',
                                        'endpoint_type': 'public',
                                    })
Example #17
0
    def test_arbitrary_attributes(self):
        cc = cloud_config.CloudConfig("test1", "region-al", fake_config_dict)
        self.assertEqual("test1", cc.name)
        self.assertEqual("region-al", cc.region)

        # Look up straight value
        self.assertEqual(1, cc.a)

        # Look up prefixed attribute, fail - returns None
        self.assertIsNone(cc.os_b)

        # Look up straight value, then prefixed value
        self.assertEqual(3, cc.c)
        self.assertEqual(3, cc.os_c)

        # Lookup mystery attribute
        self.assertIsNone(cc.x)

        # Test default ipv6
        self.assertFalse(cc.force_ipv4)
Example #18
0
 def test_get_session_no_auth(self):
     config_dict = defaults.get_defaults()
     config_dict.update(fake_services_dict)
     cc = cloud_config.CloudConfig("test1", "region-al", config_dict)
     self.assertRaises(exceptions.OpenStackConfigException, cc.get_session)
Example #19
0
 def test_workflow_override_v2(self):
     cc = cloud_config.CloudConfig("test1", "region-al", fake_services_dict)
     cc.config['workflow_api_version'] = '2'
     self.assertEqual('workflowv2', cc.get_service_type('workflow'))
    def get_one_cloud_osc(self,
                          cloud=None,
                          validate=True,
                          argparse=None,
                          **kwargs):
        """Retrieve a single cloud configuration and merge additional options

        :param string cloud:
            The name of the configuration to load from clouds.yaml
        :param boolean validate:
            Validate the config. Setting this to False causes no auth plugin
            to be created. It's really only useful for testing.
        :param Namespace argparse:
            An argparse Namespace object; allows direct passing in of
            argparse options to be added to the cloud config.  Values
            of None and '' will be removed.
        :param region_name: Name of the region of the cloud.
        :param kwargs: Additional configuration options

        :raises: keystoneauth1.exceptions.MissingRequiredOptions
            on missing required auth parameters
        """

        args = self._fix_args(kwargs, argparse=argparse)

        if cloud is None:
            if 'cloud' in args:
                cloud = args['cloud']
            else:
                cloud = self.default_cloud

        config = self._get_base_cloud_config(cloud)

        # Get region specific settings
        if 'region_name' not in args:
            args['region_name'] = ''
        region = self._get_region(cloud=cloud, region_name=args['region_name'])
        args['region_name'] = region['name']
        region_args = copy.deepcopy(region['values'])

        # Regions is a list that we can use to create a list of cloud/region
        # objects. It does not belong in the single-cloud dict
        config.pop('regions', None)

        # Can't just do update, because None values take over
        for arg_list in region_args, args:
            for (key, val) in iter(arg_list.items()):
                if val is not None:
                    if key == 'auth' and config[key] is not None:
                        config[key] = _auth_update(config[key], val)
                    else:
                        config[key] = val

        config = self.magic_fixes(config)

        # NOTE(dtroyer): OSC needs a hook into the auth args before the
        #                plugin is loaded in order to maintain backward-
        #                compatible behaviour
        config = self.auth_config_hook(config)

        if validate:
            loader = self._get_auth_loader(config)
            config = self._validate_auth_correctly(config, loader)
            auth_plugin = loader.load_from_options(**config['auth'])
        else:
            auth_plugin = None

        # If any of the defaults reference other values, we need to expand
        for (key, value) in config.items():
            if hasattr(value, 'format'):
                config[key] = value.format(**config)

        force_ipv4 = config.pop('force_ipv4', self.force_ipv4)
        prefer_ipv6 = config.pop('prefer_ipv6', True)
        if not prefer_ipv6:
            force_ipv4 = True

        if cloud is None:
            cloud_name = ''
        else:
            cloud_name = str(cloud)
        return cloud_config.CloudConfig(
            name=cloud_name,
            region=config['region_name'],
            config=self._normalize_keys(config),
            force_ipv4=force_ipv4,
            auth_plugin=auth_plugin,
            openstack_config=self,
        )
Example #21
0
 def test_ipv6(self):
     cc = cloud_config.CloudConfig("test1",
                                   "region-al",
                                   fake_config_dict,
                                   force_ipv4=True)
     self.assertTrue(cc.force_ipv4)
Example #22
0
 def test_volume_override_v3(self):
     cc = cloud_config.CloudConfig("test1", "region-al", fake_services_dict)
     cc.config['volume_api_version'] = '3'
     self.assertEqual('volumev3', cc.get_service_type('volume'))
Example #23
0
 def test_iteration(self):
     cc = cloud_config.CloudConfig("test1", "region-al", fake_config_dict)
     self.assertTrue('a' in cc)
     self.assertFalse('x' in cc)
Example #24
0
 def test_no_override(self):
     """Test no override happens when defaults are not configured"""
     cc = cloud_config.CloudConfig("test1", "region-al", fake_services_dict)
     self.assertEqual('volume', cc.get_service_type('volume'))
     self.assertEqual('workflow', cc.get_service_type('workflow'))
     self.assertEqual('not-exist', cc.get_service_type('not-exist'))