예제 #1
0
def get_auth_plugin(opts):
    """Create the right keystone connection depending on the version
    for the api, if username/password and token are provided, username and
    password takes precedence.
    """
    auth_version = guess_auth_version(opts)
    if opts.os_username:
        if auth_version == '3':
            return v3.Password(auth_url=opts.os_auth_url,
                               username=opts.os_username,
                               password=opts.os_password,
                               project_name=opts.os_project_name,
                               user_domain_name=opts.os_user_domain_name,
                               project_domain_name=opts.os_project_domain_name)
        elif auth_version == '2.0':
            return v2.Password(auth_url=opts.os_auth_url,
                               username=opts.os_username,
                               password=opts.os_password,
                               tenant_name=opts.os_tenant_name)
    elif opts.os_token:
        if auth_version == '3':
            return v3.Token(auth_url=opts.os_auth_url,
                            token=opts.os_token,
                            project_name=opts.os_project_name,
                            project_domain_name=opts.os_project_domain_name)
        elif auth_version == '2.0':
            return v2.Token(auth_url=opts.os_auth_url,
                            token=opts.os_token,
                            tenant_name=opts.os_tenant_name)
    raise Exception('Unable to determine correct auth method, please provide'
                    ' either username or token')
예제 #2
0
def get_auth_plugin(opts):
    auth_version = guess_auth_version(opts)
    if opts.os_username:
        if auth_version == '3':
            return v3.Password(auth_url=opts.os_auth_url,
                               username=opts.os_username,
                               password=opts.os_password,
                               project_name=opts.os_project_name,
                               user_domain_name=opts.os_user_domain_name,
                               project_domain_name=opts.os_project_domain_name)
        elif auth_version == '2.0':
            return v2.Password(auth_url=opts.os_auth_url,
                               username=opts.os_username,
                               password=opts.os_password,
                               tenant_name=opts.os_tenant_name)
    elif opts.os_token:
        if auth_version == '3':
            return v3.Token(auth_url=opts.os_auth_url,
                            token=opts.os_token,
                            project_name=opts.os_project_name,
                            project_domain_name=opts.os_project_domain_name)
        elif auth_version == '2.0':
            return v2.Token(auth_url=opts.os_auth_url,
                            token=opts.os_token,
                            tenant_name=opts.os_tenant_name)
    raise Exception('Unable to determine correct auth method, please provide'
                    ' either username or token')
예제 #3
0
def _get_token_auth_ks_session(**kwargs):

    cacert = kwargs.pop('cacert', None)
    cert = kwargs.pop('cert', None)
    key = kwargs.pop('key', None)
    insecure = kwargs.pop('insecure', False)
    auth_url = kwargs.pop('auth_url', None)
    project_id = kwargs.pop('project_id', None)
    project_name = kwargs.pop('project_name', None)
    timeout = kwargs.get('timeout')
    token = kwargs['token']

    if insecure:
        verify = False
    else:
        verify = cacert or True

    if cert and key:
        # passing cert and key together is deprecated in favour of the
        # requests lib form of having the cert and key as a tuple
        cert = (cert, key)

    # create the keystone client session
    ks_session = session.Session(verify=verify, cert=cert, timeout=timeout)
    v2_auth_url, v3_auth_url = _discover_auth_versions(ks_session, auth_url)

    user_domain_name = kwargs.pop('user_domain_name', None)
    user_domain_id = kwargs.pop('user_domain_id', None)
    project_domain_name = kwargs.pop('project_domain_name', None)
    project_domain_id = kwargs.pop('project_domain_id', None)
    auth = None

    use_domain = (user_domain_id or user_domain_name or project_domain_id
                  or project_domain_name)
    use_v3 = v3_auth_url and (use_domain or (not v2_auth_url))
    use_v2 = v2_auth_url and not use_domain

    if use_v3:
        auth = v3_auth.Token(v3_auth_url,
                             token=token,
                             project_name=project_name,
                             project_id=project_id,
                             project_domain_name=project_domain_name,
                             project_domain_id=project_domain_id)
    elif use_v2:
        auth = v2_auth.Token(v2_auth_url,
                             token=token,
                             tenant_id=project_id,
                             tenant_name=project_name)
    else:
        raise exc.CommandError('Unable to determine the Keystone version '
                               'to authenticate with using the given '
                               'auth_url.')

    ks_session.auth = auth
    return ks_session
예제 #4
0
    def test_authenticate_with_token(self):
        self.stub_auth(json=self.TEST_RESPONSE_DICT)
        a = v2.Token(self.TEST_URL, 'foo')
        s = session.Session(a)
        s.get_token()

        req = {'auth': {'token': {'id': 'foo'}}}
        self.assertRequestBodyIs(json=req)
        self.assertRequestHeaderEqual('x-Auth-Token', 'foo')
        self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN)
예제 #5
0
    def url_for(self, **kwargs):
        def get_endpoint():
            auth_plugin = self.context.auth_plugin
            return auth_plugin.get_endpoint(self._keystone_session, **kwargs)

        # NOTE(jamielennox): use the session defined by the keystoneclient
        # options as traditionally the token was always retrieved from
        # keystoneclient.
        try:
            kwargs.setdefault('interface', kwargs.pop('endpoint_type'))
        except KeyError:
            pass

        reg = self.context.region_name or cfg.CONF.region_name_for_services
        kwargs.setdefault('region_name', reg)

        try:
            url = get_endpoint()
        except exceptions.EmptyCatalog:
            kc = self.clients.client('keystone').client

            auth_plugin = self.context.auth_plugin
            endpoint = auth_plugin.get_endpoint(None,
                                                interface=auth.AUTH_INTERFACE)
            token = auth_plugin.get_token(None)
            project_id = auth_plugin.get_project_id(None)

            if kc.version == 'v3':
                token_obj = v3.Token(endpoint, token, project_id=project_id)
                catalog_key = 'catalog'
                access_key = 'token'
            elif kc.version == 'v2.0':
                endpoint = endpoint.replace('v3', 'v2.0')
                token_obj = v2.Token(endpoint, token, tenant_id=project_id)
                catalog_key = 'serviceCatalog'
                access_key = 'access'
            else:
                raise exceptions.Error(_("Unknown Keystone version"))

            auth_ref = token_obj.get_auth_ref(self._keystone_session)

            if catalog_key in auth_ref:
                cxt = self.context.to_dict()
                access_info = cxt['auth_token_info'][access_key]
                access_info[catalog_key] = auth_ref[catalog_key]
                self.context = context.RequestContext.from_dict(cxt)
                url = get_endpoint()

        # NOTE(jamielennox): raising exception maintains compatibility with
        # older keystoneclient service catalog searching.
        if url is None:
            raise exceptions.EndpointNotFound()

        return url
예제 #6
0
    def _create_token_auth(self, project_id, token=None, url=None):
        if not token:
            token = self.data.unscoped_access_info.auth_token

        if not url:
            url = settings.OPENSTACK_KEYSTONE_URL

        return auth_v2.Token(auth_url=url,
                             token=token,
                             tenant_id=project_id,
                             reauthenticate=False)
예제 #7
0
def get_token_auth_plugin(auth_url, token, project_id=None):
    if get_keystone_version() >= 3:
        return v3_auth.Token(auth_url=auth_url,
                             token=token,
                             project_id=project_id,
                             reauthenticate=False)

    else:
        return v2_auth.Token(auth_url=auth_url,
                             token=token,
                             tenant_id=project_id,
                             reauthenticate=False)
예제 #8
0
def barbicanclient(request):
    project_id = request.user.project_id
    if keystone.get_version() < 3:
        auth = auth_v2.Token(settings.OPENSTACK_KEYSTONE_URL,
                             request.user.token.id,
                             tenant_id=project_id)
    else:
        domain_id = request.session.get('domain_context')
        auth = auth_v3.Token(settings.OPENSTACK_KEYSTONE_URL,
                             request.user.token.id,
                             project_id=project_id,
                             project_domain_id=domain_id)
    return barbican_client.Client(session=session.Session(auth=auth))
    def test_authenticate_with_token(self):
        self.stub_auth(json=self.TEST_RESPONSE_DICT)
        a = v2.Token(self.TEST_URL, 'foo')
        s = session.Session(a)
        self.assertEqual({'X-Auth-Token': self.TEST_TOKEN},
                         s.get_auth_headers())

        req = {'auth': {'token': {'id': 'foo'}}}
        self.assertRequestBodyIs(json=req)
        self.assertRequestHeaderEqual('x-Auth-Token', 'foo')
        self.assertRequestHeaderEqual('Content-Type', 'application/json')
        self.assertRequestHeaderEqual('Accept', 'application/json')
        self.assertEqual(s.auth.auth_ref.auth_token, self.TEST_TOKEN)
예제 #10
0
 def _get_keystone_v2_auth(self, v2_auth_url, **kwargs):
     auth_token = kwargs.pop('auth_token', None)
     if auth_token:
         return v2_auth.Token(v2_auth_url, auth_token,
                              tenant_id=kwargs.pop('project_id', None),
                              tenant_name=kwargs.pop('project_name', None))
     else:
         return v2_auth.Password(
             v2_auth_url,
             username=kwargs.pop('username', None),
             password=kwargs.pop('password', None),
             tenant_id=kwargs.pop('project_id', None),
             tenant_name=kwargs.pop('project_name', None))
예제 #11
0
    def get_raw_token_from_identity_service(self,
                                            auth_url,
                                            username=None,
                                            password=None,
                                            tenant_name=None,
                                            tenant_id=None,
                                            token=None,
                                            project_name=None,
                                            project_id=None,
                                            trust_id=None,
                                            **kwargs):
        """Authenticate against the v2 Identity API.

        If a token is provided it will be used in preference over username and
        password.

        :returns: access.AccessInfo if authentication was successful.
        :raises keystoneclient.exceptions.AuthorizationFailure: if unable to
            authenticate or validate the existing authorization token
        """
        try:
            if auth_url is None:
                raise ValueError(_("Cannot authenticate without an auth_url"))

            new_kwargs = {
                'trust_id': trust_id,
                'tenant_id': project_id or tenant_id,
                'tenant_name': project_name or tenant_name
            }

            if token:
                plugin = v2_auth.Token(auth_url, token, **new_kwargs)
            elif username and password:
                plugin = v2_auth.Password(auth_url, username, password,
                                          **new_kwargs)
            else:
                msg = _('A username and password or token is required.')
                raise exceptions.AuthorizationFailure(msg)

            return plugin.get_auth_ref(self.session)
        except (exceptions.AuthorizationFailure, exceptions.Unauthorized):
            _logger.debug("Authorization Failed.")
            raise
        except exceptions.EndpointNotFound:
            msg = (
                _('There was no suitable authentication url for this request'))
            raise exceptions.AuthorizationFailure(msg)
        except Exception as e:
            raise exceptions.AuthorizationFailure(
                _("Authorization Failed: %s") % e)
예제 #12
0
    def get_plugin(self, auth_url=None, token=None, project_id=None, **kwargs):
        if not all((auth_url, token)):
            return None

        if utils.get_keystone_version() >= 3:
            return v3_auth.Token(auth_url=auth_url,
                                 token=token,
                                 project_id=project_id,
                                 reauthenticate=False)

        else:
            return v2_auth.Token(auth_url=auth_url,
                                 token=token,
                                 tenant_id=project_id,
                                 reauthenticate=False)
예제 #13
0
def cueclient(request):
    cacert = getattr(settings, 'OPENSTACK_SSL_CACERT', None)
    auth_url = getattr(settings, 'OPENSTACK_KEYSTONE_URL')
    auth_version = getattr(settings, 'OPENSTACK_API_VERSIONS',
                           {}).get('identity', 2.0)
    if auth_version == 3:
        auth = v3.Token(auth_url,
                        request.user.token.id,
                        project_id=request.user.project_id,
                        project_name=request.user.project_name)
    elif auth_version == 2 or auth_version == 2.0:
        auth = v2.Token(auth_url,
                        request.user.token.id,
                        tenant_id=request.user.tenant_id,
                        tenant_name=request.user.tenant_name)

    session = ksc_session.Session(auth=auth, verify=cacert)
    return client.Client(session=session)
예제 #14
0
    def test_access_audit_id(self):
        unscoped_plugin = v2.Password(auth_url=os.environ.get('OS_AUTH_URL'),
                                      username=os.environ.get('OS_USERNAME'),
                                      password=os.environ.get('OS_PASSWORD'))

        unscoped_auth_ref = unscoped_plugin.get_access(self.session)

        self.assertIsNotNone(unscoped_auth_ref.audit_id)
        self.assertIsNone(unscoped_auth_ref.audit_chain_id)

        scoped_plugin = v2.Token(auth_url=os.environ.get('OS_AUTH_URL'),
                                 token=unscoped_auth_ref.auth_token,
                                 tenant_name=os.environ.get('OS_TENANT_NAME'))

        scoped_auth_ref = scoped_plugin.get_access(self.session)

        self.assertIsNotNone(scoped_auth_ref.audit_id)
        self.assertIsNotNone(scoped_auth_ref.audit_chain_id)

        self.assertEqual(unscoped_auth_ref.audit_id,
                         scoped_auth_ref.audit_chain_id)
예제 #15
0
def _get_keystone_session(**kwargs):
    # TODO(fabgia): the heavy lifting here should be really done by Keystone.
    # Unfortunately Keystone does not support a richer method to perform
    # discovery and return a single viable URL. A bug against Keystone has
    # been filed: https://bugs.launchpad.net/python-keystoneclient/+bug/1330677

    # first create a Keystone session
    cacert = kwargs.pop('cacert', None)
    cert = kwargs.pop('cert', None)
    key = kwargs.pop('key', None)
    insecure = kwargs.pop('insecure', False)
    auth_url = kwargs.pop('auth_url', None)
    project_id = kwargs.pop('project_id', None)
    project_name = kwargs.pop('project_name', None)
    token = kwargs['token']
    timeout = kwargs.get('timeout')

    if insecure:
        verify = False
    else:
        verify = cacert or True

    if cert and key:
        # passing cert and key together is deprecated in favour of the
        # requests lib form of having the cert and key as a tuple
        cert = (cert, key)

    # create the keystone client session
    ks_session = session.Session(verify=verify, cert=cert, timeout=timeout)
    v2_auth_url, v3_auth_url = _discover_auth_versions(ks_session, auth_url)

    username = kwargs.pop('username', None)
    user_id = kwargs.pop('user_id', None)
    user_domain_name = kwargs.pop('user_domain_name', None)
    user_domain_id = kwargs.pop('user_domain_id', None)
    project_domain_name = kwargs.pop('project_domain_name', None)
    project_domain_id = kwargs.pop('project_domain_id', None)
    auth = None

    use_domain = (user_domain_id or user_domain_name or project_domain_id
                  or project_domain_name)
    use_v3 = v3_auth_url and (use_domain or (not v2_auth_url))
    use_v2 = v2_auth_url and not use_domain

    if use_v3 and token:
        auth = v3_auth.Token(v3_auth_url,
                             token=token,
                             project_name=project_name,
                             project_id=project_id,
                             project_domain_name=project_domain_name,
                             project_domain_id=project_domain_id)
    elif use_v2 and token:
        auth = v2_auth.Token(v2_auth_url,
                             token=token,
                             tenant_id=project_id,
                             tenant_name=project_name)
    elif use_v3:
        # the auth_url as v3 specified
        # e.g. http://no.where:5000/v3
        # Keystone will return only v3 as viable option
        auth = v3_auth.Password(v3_auth_url,
                                username=username,
                                password=kwargs.pop('password', None),
                                user_id=user_id,
                                user_domain_name=user_domain_name,
                                user_domain_id=user_domain_id,
                                project_name=project_name,
                                project_id=project_id,
                                project_domain_name=project_domain_name,
                                project_domain_id=project_domain_id)
    elif use_v2:
        # the auth_url as v2 specified
        # e.g. http://no.where:5000/v2.0
        # Keystone will return only v2 as viable option
        auth = v2_auth.Password(v2_auth_url,
                                username,
                                kwargs.pop('password', None),
                                tenant_id=project_id,
                                tenant_name=project_name)

    else:
        raise exc.CommandError('Unable to determine the Keystone version '
                               'to authenticate with using the given '
                               'auth_url.')

    ks_session.auth = auth
    return ks_session
예제 #16
0
    def create_plugin(self, session, version, url, raw_status=None):
        if _discover.version_match((2, ), version):
            return v2.Token(url, self._token, **self._v2_params)

        elif _discover.version_match((3, ), version):
            return v3.Token(url, self._token, **self._v3_params)
예제 #17
0
 def __init__(self, endpoint, token, **kwargs):
     self.client = KeystoneClient(session=KeystoneClient.Session(
         auth=v2.Token(auth_url=endpoint, token=token)))
     self.images = self._get_resources('Image')
예제 #18
0
 def __init__(self, ctxt):
     auth = v2.Token(auth_url=_get_admin_auth_url(ctxt),
                     token=ctxt.auth_token,
                     tenant_name=ctxt.project_name)
     sess = session.Session(auth=auth)
     self._nc = client.Client(NOVA_API_VERSION, session=sess)
예제 #19
0
    def __init__(self, token=None, url=None, auth_url=None,
                 domain_id=None, domain_name=None,
                 project_name=None, project_id=None,
                 username=None, password=None,
                 user_domain_id=None, user_domain_name=None,
                 project_domain_id=None, project_domain_name=None,
                 region_name=None, api_version=None, verify=True,
                 trust_id=None, timing=None):
        self._token = token
        self._url = url
        self._auth_url = auth_url
        self._domain_id = domain_id
        self._domain_name = domain_name
        self._project_name = project_name
        self._project_id = project_id
        self._username = username
        self._password = password
        self._user_domain_id = user_domain_id
        self._user_domain_name = user_domain_name
        self._project_domain_id = project_domain_id
        self._project_domain_name = project_domain_name
        self._region_name = region_name
        self._api_version = api_version
        self._trust_id = trust_id
        self._service_catalog = None
        self.timing = timing

        # verify is the Requests-compatible form
        self._verify = verify
        # also store in the form used by the legacy client libs
        self._cacert = None
        if verify is True or verify is False:
            self._insecure = not verify
        else:
            self._cacert = verify
            self._insecure = False

        ver_prefix = identity_client.AUTH_VERSIONS[
            self._api_version[identity_client.API_NAME]
        ]

        # Get logging from root logger
        root_logger = logging.getLogger('')
        LOG.setLevel(root_logger.getEffectiveLevel())

        # NOTE(dtroyer): These plugins are hard-coded for the first step
        #                in using the new Keystone auth plugins.

        if self._url:
            LOG.debug('Using token auth %s', ver_prefix)
            if ver_prefix == 'v2':
                self.auth = v2_auth.Token(
                    auth_url=url,
                    token=token,
                )
            else:
                self.auth = v3_auth.Token(
                    auth_url=url,
                    token=token,
                )
        else:
            LOG.debug('Using password auth %s', ver_prefix)
            if ver_prefix == 'v2':
                self.auth = v2_auth.Password(
                    auth_url=auth_url,
                    username=username,
                    password=password,
                    trust_id=trust_id,
                    tenant_id=project_id,
                    tenant_name=project_name,
                )
            else:
                self.auth = v3_auth.Password(
                    auth_url=auth_url,
                    username=username,
                    password=password,
                    trust_id=trust_id,
                    user_domain_id=user_domain_id,
                    user_domain_name=user_domain_name,
                    domain_id=domain_id,
                    domain_name=domain_name,
                    project_id=project_id,
                    project_name=project_name,
                    project_domain_id=project_domain_id,
                    project_domain_name=project_domain_name,
                )

        self.session = session.Session(
            auth=self.auth,
            verify=verify,
        )

        self.auth_ref = None
        if not self._url:
            # Trigger the auth call
            self.auth_ref = self.session.auth.get_auth_ref(self.session)
            # Populate other password flow attributes
            self._token = self.session.auth.get_token(self.session)
            self._service_catalog = self.auth_ref.service_catalog

        return