def test_auth_endpoint_validation(post, get_endpoint, auth_response):
    post.return_value = MagicMock(json=MagicMock(return_value=auth_response))

    assert Lava('username', 'region', api_key='apikey',
                endpoint='v2/tenant').endpoint == 'v2/tenant'
    assert Lava('username',
                'region',
                api_key='apikey',
                endpoint='v2',
                tenant_id='tenant').endpoint == 'v2/tenant'
    assert Lava('username',
                'region',
                api_key='apikey',
                endpoint='v2/tenant',
                tenant_id='tenant').endpoint == 'v2/tenant'

    pytest.raises(error.InvalidError,
                  Lava,
                  'username',
                  'region',
                  api_key='apikey',
                  endpoint='foo')
    pytest.raises(error.InvalidError,
                  Lava,
                  'username',
                  'region',
                  api_key='apikey',
                  endpoint='foo',
                  tenant_id='tenant')
def mock_client(request):
    client = Lava(
        'username',
        endpoint='http://dfw.bigdata.api.rackspacecloud.com/v2',
        token='token',
        tenant_id=12345,
        _cli_args=Mock(headless=True))

    client._request = MagicMock()

    def init_logging(*args):
        initialize_logging(MagicMock(debug=True))

    def exit(*args):
        raise Exits(args)

    patchers = [
        patch('lavaclient.cli.initialize_logging', init_logging),
        patch('sys.exit', exit),
        patch('lavaclient.cli.create_client',
              MagicMock(return_value=client)),
    ]

    for patcher in patchers:
        patcher.start()
        request.addfinalizer(patcher.stop)

    return client
Exemple #3
0
def mock_client(request):
    client = Lava(
        'username',
        endpoint='http://dfw.bigdata.api.rackspacecloud.com/v2',
        token='token',
        tenant_id=12345,
        _cli_args=Mock(headless=True))

    client._request = MagicMock()

    def init_logging(*args):
        initialize_logging(MagicMock(debug=True))

    def exit(*args):
        raise Exits(args)

    patchers = [
        patch('lavaclient.cli.initialize_logging', init_logging),
        patch('sys.exit', exit),
        patch('lavaclient.cli.create_client',
              MagicMock(return_value=client)),
    ]

    for patcher in patchers:
        patcher.start()
        request.addfinalizer(patcher.stop)

    return client
def test_auth_client(post, auth_response):
    post.return_value = MagicMock(json=MagicMock(return_value=auth_response))
    client = Lava('username', 'region', api_key='apikey', tenant_id='tenantId')

    assert post.call_count == 1
    assert client.token == 'ab48a9efdfedb23ty3494'
    assert client.endpoint == 'publicURL/v2/tenantId'
def create_client(args):
    """
    Create instance of Lava from CLI args
    """
    resource = args.resource
    if resource == 'shell':
        args.enable_cli = False

    apikey = first_exists(args.lava_api_key, os.environ.get('LAVA_API_KEY'),
                          os.environ.get('OS_API_KEY'))
    token = first_exists(args.token, os.environ.get('LAVA_AUTH_TOKEN'),
                         os.environ.get('AUTH_TOKEN'))
    user = first_exists(args.user, os.environ.get('LAVA_USERNAME'),
                        os.environ.get('OS_USERNAME'), getpass.getuser())
    password = first_exists(args.password, os.environ.get('LAVA_PASSWORD'),
                            os.environ.get('OS_PASSWORD'))

    if not any((apikey, token, password)):
        if args.headless:
            six.print_('Error: no API key, token, or password specified',
                       file=sys.stderr)
            sys.exit(1)
        else:
            password = getpass.getpass('Password for {0}: '.format(user))

    try:
        return Lava(user,
                    api_key=apikey,
                    token=token,
                    password=password,
                    tenant_id=first_exists(args.tenant,
                                           os.environ.get('LAVA_TENANT_NAME'),
                                           os.environ.get('OS_TENANT_NAME')),
                    region=first_exists(args.region,
                                        os.environ.get('LAVA_REGION_NAME'),
                                        os.environ.get('OS_REGION_NAME')),
                    auth_url=first_exists(args.auth_url,
                                          os.environ.get('LAVA_AUTH_URL'),
                                          os.environ.get('OS_AUTH_URL')),
                    endpoint=first_exists(args.endpoint,
                                          os.environ.get('LAVA2_API_URL'),
                                          os.environ.get('LAVA_API_URL')),
                    verify_ssl=args.verify_ssl,
                    timeout=first_exists(args.request_timeout,
                                         os.environ.get('LAVA_TIMEOUT')),
                    max_retries=first_exists(args.retries,
                                             os.environ.get('LAVA_RETRIES')),
                    retry_backoff=first_exists(
                        args.retry_backoff,
                        os.environ.get('LAVA_RETRY_BACKOFF')),
                    _cli_args=args)
    except LavaError as exc:
        six.print_('Error during authentication: {0}'.format(exc),
                   file=sys.stderr)
        if args.debug:
            raise

        sys.exit(1)
def test_auth_endpoint(post, get_endpoint, auth_response):
    post.return_value = MagicMock(json=MagicMock(return_value=auth_response))

    client1 = Lava('username',
                   'region',
                   api_key='apikey',
                   endpoint='v2/tenant')

    assert get_endpoint.call_count == 0
    assert post.call_count == 1
    assert client1.token == 'ab48a9efdfedb23ty3494'
    assert client1.endpoint == 'v2/tenant'
Exemple #7
0
 def _create(self):
     """Create an authenticated CBD client."""
     region = cfg.CONF.region_name_for_services.lower()
     if self.context.region_name:
         region = self.context.region_name.lower()
     LOG.info(_LI("CBD client authenticating username %s in region %s"),
              self.context.username, region)
     tenant = self.context.tenant_id
     username = self.context.username
     endpoint_uri = ("https://{region}.bigdata.api.rackspacecloud.com:443/"
                     "v2/{tenant}".format(region=region, tenant=tenant))
     try:
         return Lava(username=username,
                     tenant_id=self.context.tenant_id,
                     auth_url=self.context.auth_url,
                     api_key=None,
                     token=self.context.auth_token,
                     region=region,
                     endpoint=endpoint_uri,
                     verify_ssl=False)
     except LavaError as exc:
         LOG.warn(_LW("CBD client authentication failed: %s."), exc)
         raise exception.AuthorizationFailure()
     LOG.info(_LI("CBD user %s authenticated successfully."), username)