Authentication Plugins¶
Introduction¶
Authentication plugins provide a generic means by which to extend the authentication mechanisms known to OpenStack clients.
In the vast majority of cases the authentication plugins used will be those written for use with the OpenStack Identity Service (Keystone), however this is not the only possible case, and the mechanisms by which authentication plugins are used and implemented should be generic enough to cover completely customized authentication solutions.
The subset of authentication plugins intended for use with an OpenStack Identity server (such as Keystone) are called Identity Plugins.
V2 Identity Plugins¶
Standard V2 identity plugins are defined in the module:
keystoneauth1.identity.v2
They include:
Password: Authenticate against a V2 identity service using a username and password.Token: Authenticate against a V2 identity service using an existing token.
V2 identity plugins must use an auth_url that points to the root of a V2
identity server URL, i.e.: http://hostname:5000/v2.0.
V3 Identity Plugins¶
V3 Identity plugins are slightly different from their V2 counterparts as a V3 authentication request can contain multiple authentication methods. Users can compose different auth methods to enable use cases like multi-factor authentication.
V3 Identity plugins must use an auth_url that points to the root of a V3
identity server URL, i.e.: http://hostname:5000/v3.
Standard Plugins¶
The following plugins are provided as helpers for authenticating with a single method.
Password: Authenticate against a V3 identity service using a username and password. Uses thePasswordMethodmethod.Token: Authenticate against a V3 identity service using an existing token. Uses theTokenMethodmethod.TOTP: Authenticate against a V3 identity service using a Time-Based One-Time Password (TOTP). Uses theTOTPMethodmethod.ApplicationCredential: Authenticate against a V3 identity service using an application credential. Uses theApplicationCredentialMethodmethod. See Application Credentials for more details.
The following plugins are also available but require extra packages.
Kerberos: Authenticate against a V3 identity service using Kerberos. Uses theKerberosMethodmethod. See Kerberos for more details.V3OAuth1: Authenticate against a V3 identity service using OAuth 1.0a. Uses theV3OAuth1Methodmethod. See OAuth 1.0a for more details.
For example, to use the password plugin:
>>> from keystoneauth1 import session
>>> from keystoneauth1.identity import v3
>>> auth = v3.Password(
... auth_url='http://my.keystone.com:5000/v3',
... username='username',
... password='password',
... project_id='projectid',
... user_domain_name='default',
... )
>>> sess = session.Session(auth=auth)
These plugins are provided for convenience. You can also construct a plugin
directly by passing AuthMethod instances
to Auth. For example, the following is
equivalent to using the password plugin above:
>>> from keystoneauth1 import session
>>> from keystoneauth1.identity import v3
>>> password = v3.PasswordMethod(
... username='username',
... password='password',
... user_domain_name='default',
... )
>>> auth = v3.Auth(
... auth_url='http://my.keystone.com:5000/v3',
... auth_methods=[password],
... project_id='projectid',
... )
>>> sess = session.Session(auth=auth)
To combine authentication methods, use the
MultiFactor plugin as described below.
Multi-Factor Authentication¶
The MultiFactor plugin lets you supply
all the needed authentication methods up front, using their plugin names:
>>> from keystoneauth1 import session
>>> from keystoneauth1.identity import v3
>>> auth = v3.MultiFactor(
... auth_url='http://my.keystone.com:5000/v3',
... auth_methods=['v3password', 'v3totp'],
... username='user',
... password='password',
... passcode='123456',
... user_domain_id='default',
... project_id='projectid',
... )
>>> sess = session.Session(auth=auth)
Note
The identity service must support the chosen combination of authentication methods; client-side composability does not guarantee that every combination will be accepted.
You can also construct a plugin directly by passing
AuthMethod instances to
Auth. The following is equivalent to
using MultiFactor above:
>>> from keystoneauth1 import session
>>> from keystoneauth1.identity import v3
>>> auth = v3.Auth(
... auth_url='http://my.keystone.com:5000/v3',
... auth_methods=[
... v3.PasswordMethod(
... username='user',
... password='password',
... user_domain_id="default",
... ),
... v3.TOTPMethod(
... username='user',
... passcode='123456',
... user_domain_id="default",
... ),
... ],
... project_id='projectid',
... )
>>> sess = session.Session(auth=auth)
You can also add methods to an existing plugin after it has been created. For example, to add TOTP authentication to a password plugin:
>>> auth = v3.Password(
... auth_url='http://my.keystone.com:5000/v3',
... username='user',
... password='password',
... user_domain_id='default',
... project_id='projectid',
... )
>>> totp = v3.TOTPMethod(
... username='user',
... passcode='123456',
... user_domain_id='default',
... )
>>> auth.add_method(totp)
>>> sess = session.Session(auth=auth)
Some authentication methods cannot be used on their own.
ReceiptMethod uses an existing
auth-receipt and must be combined with at least one other method.
Note
The MultiFactor helper does not
support auth receipts as an option in auth_methods, but one can be added
with add_method().
When you supply just one method when multiple are needed, a
MissingAuthMethods error will
be raised. This can be caught, and you can infer based on the error what
the missing methods were, and from it extract the receipt to continue
authentication:
>>> from keystoneauth1 import exceptions
>>> auth = v3.Password(
... auth_url='http://my.keystone.com:5000/v3',
... username='username',
... password='password',
... project_id='projectid',
... user_domain_id='default',
... )
>>> sess = session.Session(auth=auth)
>>> try:
... sess.get_token()
... except exceptions.MissingAuthMethods as e:
... receipt = e.receipt
... methods = e.methods
... required_methods = e.required_auth_methods
Once you know what auth methods are needed to continue, you can extend the existing auth plugin with additional methods:
>>> auth.add_method(
... v3.TOTPMethod(
... username='user',
... passcode='123456',
... user_domain_id='default',
... )
... )
>>> sess.get_token()
Or if you do not have the existing auth method, but have the receipt you can continue as well:
>>> auth = v3.TOTP(
... auth_url='http://my.keystone.com:5000/v3',
... username='user',
... passcode='123456',
... user_domain_id='default',
... project_id='projectid',
... )
>>> auth.add_method(v3.ReceiptMethod(receipt=receipt))
>>> sess = session.Session(auth=auth)
>>> sess.get_token()
Application Credentials¶
There is a specific authentication method for interacting with Identity servers that support application credential authentication. Since application credentials are associated to a user on a specific project, some parameters are not required as they would be with traditional password authentication. The following plugin can be used to authenticate for a token using an application credential:
ApplicationCredential:
The following example shows the plugin usage with a session:
>>> from keystoneauth1 import session
>>> from keystoneauth1.identity import v3
>>> auth = v3.ApplicationCredential(
... auth_url='http://my.keystone.com:5000/v3',
... application_credential_secret='application_credential_secret',
... application_credential_id='c2872b920853478292623be94b657090',
... )
>>> sess = session.Session(auth=auth)
Kerberos¶
The Kerberos plugin requires the
keystoneauth1[kerberos] extra and a valid Kerberos ticket (for example,
obtained using kinit).
The following example shows the plugin usage with a session:
>>> from keystoneauth1 import session
>>> from keystoneauth1.extras import kerberos
>>> auth = kerberos.Kerberos(
... auth_url='http://my.keystone.com:5000/v3',
... project_id='projectid',
... )
>>> sess = session.Session(auth=auth)
OAuth 1.0a¶
The V3OAuth1 plugin requires the
keystoneauth1[oauth1] extra. It also requires the OAuth consumer’s key and
secret, as well as the OAuth access token’s key and secret.
The following example shows the plugin usage with a session:
>>> from keystoneauth1.extras import oauth1
>>> from keystoneauth1 import session
>>> auth = oauth1.V3OAuth1(
... 'http://my.keystone.com:5000/v3',
... consumer_key=consumer_id,
... consumer_secret=consumer_secret,
... access_key=access_token_key,
... access_secret=access_token_secret,
... )
>>> sess = session.Session(auth=auth)
Tokenless Authentication¶
A plugin for tokenless authentication also exists. It provides a means to authorize client operations within the Identity server by using an X.509 TLS client certificate without having to issue a token. We provide a tokenless authentication plugin at:
TokenlessAuth
It is mostly used by service clients for token validation and here is an example of how this plugin would be used in practice:
>>> from keystoneauth1 import session
>>> from keystoneauth1.identity import v3
>>> auth = v3.TokenlessAuth(
... auth_url='https://keystone:5000/v3',
... domain_name='my_service_domain',
... )
>>> sess = session.Session(
... auth=auth,
... cert=('/opt/service_client.crt', '/opt/service_client.key'),
... verify='/opt/ca.crt',
... )
Federation¶
The following V3 plugins are provided to support federation:
MappedKerberos: Federated (mapped) Kerberos.Password: ADFS SAML2 password authentication.Password: SAML2 password authentication.OidcAccessToken: Plugin to reuse an existing OpenID Connect access token.OidcAuthorizationCode: OpenID Connect Authorization Code grant type.OidcClientCredentials: OpenID Connect Client Credentials grant type.OidcDeviceAuthorization: OpenID Connect / OAuth 2.0 Device Authorization Grant.OidcPassword: OpenID Connect Resource Owner Password Credentials grant type.WebSSO: Browser-based Web Single Sign On. See WebSSO.Keystone2Keystone: Keystone to Keystone Federation.
The Keystone2Keystone plugin is special as it takes a Password auth for
one keystone instance acting as an Identity Provider as input in order to
create a session on the keystone acting as a Service Provider, for example:
>>> from keystoneauth1 import session
>>> from keystoneauth1.identity import v3
>>> from keystoneauth1.identity.v3 import k2k
>>> pwauth = v3.Password(
... auth_url='http://my.keystone.com:5000/v3',
... username='username',
... password='password',
... project_id='projectid',
... user_domain_name='Default',
... )
>>> k2kauth = k2k.Keystone2Keystone(
... pwauth, 'mysp', project_id='federated_projectid',
... )
>>> k2ksession = session.Session(auth=k2kauth)
The OidcPassword plugin also supports
OTP. This option is required in cases when the Identity Provider requires more
than a password to authenticate the user. As the OTP usually is a short-lived
code that continually changes, then, when this option is active, the user will
be requested to input the OTP code when executing the authentication process.
To enable this option, the user will need to export the environment variable
OS_IDP_OTP_KEY with the OTP key used by the Identity Provider’s
authentication API. For example, if the Identity Provider’s authentication API
requires some JSON like:
{
"username": "user1",
"password": "passwd",
"totp": "763907"
}
Then, you will use the totp value in your OS_IDP_OTP_KEY, e.g.
export OS_IDP_OTP_KEY=totp.
After the configuration of the OS_IDP_OTP_KEY environment variable,
every time that you will log in through a client, a prompt will be displayed
requesting you to input your OTP code.
OAuth 2.0 Client Credentials¶
Warning
The access token must be only added for the requests using HTTPS according to RFC6749.
There is a specific authentication method for interacting with Identity
servers that support OAuth 2.0 Client Credential Grant. The notable difference
from the other authentication method is that, after passing the
authentication, the session will add Authorization header with an
OAuth 2.0 access token to send subsequent requests. The following plugin can be
used to authenticate for a token using OAuth 2.0 client credentials:
OAuth2ClientCredential:
The following example shows the method usage with a session:
>>> from keystoneauth1 import session
>>> from keystoneauth1.identity import v3
>>> auth = v3.OAuth2ClientCredential(
... auth_url='https://keystone.host:5000/v3',
... oauth2_endpoint='https://keystone.host/identity/v3/OS-OAUTH2/token',
... oauth2_client_id='f96a2fec117141a6b5fbaa0485632244',
... oauth2_client_secret='client_credential_secret',
... )
>>> sess = session.Session(auth=auth)
OAuth 2.0 Mutual-TLS Client Credentials¶
Warning
The access token must be only added for the requests using mutual TLS according to RFC8705.
There is a specific authentication method for interacting with Identity
servers that support OAuth 2.0 Mutual-TLS Client Authentication. The notable
difference from the other authentication method is that, after passing the
authentication, the session will add Authorization header with an
OAuth 2.0 Certificate-Bound Access Tokens to send subsequent requests. The
following method can be used to authenticate for a token using OAuth 2.0
Mutual-TLS client credentials:
OAuth2mTlsClientCredential:
The following example shows the method usage with a session:
>>> from keystoneauth1 import session
>>> from keystoneauth1.identity import v3
>>> auth = v3.OAuth2mTlsClientCredential(
... auth_url='https://keystone.host:5000/v3',
... oauth2_endpoint='https://keystone.host/identity/v3/OS-OAUTH2/token',
... oauth2_client_id='f96a2fec117141a6b5fbaa0485632244',
... )
>>> sess = session.Session(
... auth=auth,
... cert=('/opt/service_client.crt', '/opt/service_client.key'),
... verify='/opt/ca.crt',
... )
Version Independent Identity Plugins¶
Standard version independent identity plugins are defined in the module
keystoneauth1.identity.generic.
For the cases of plugins that exist under both the identity V2 and V3 APIs there is an abstraction to allow the plugin to determine which of the V2 and V3 APIs are supported by the server and use the most appropriate API.
These plugins are:
Password: Authenticate using a user/password against either v2 or v3 API.Token: Authenticate using an existing token against either v2 or v3 API.
These plugins work by first querying the identity server to determine available
versions and so the auth_url used with the plugins should point to the base
URL of the identity server to use. If the auth_url points to either a V2 or
V3 endpoint it will restrict the plugin to only working with that version of
the API.
Simple Plugins¶
In addition to the Identity plugins a simple plugin that will always use the same provided token and endpoint is available. This is useful for situations where you have a token and want to bypass authentication to obtain a new token for subsequent requests. Testing, proxies, and service-to-service authentication on behalf of a user are good examples use cases for this authentication plugin.
It can be found at keystoneauth1.token_endpoint.Token.
For example:
>>> from keystoneauth1 import session
>>> from keystoneauth1 import token_endpoint
>>> auth = token_endpoint.Token(
... 'http://my.keystone.com:5000/v3',
... token=token,
... )
>>> sess = session.Session(auth=auth)
Standalone Plugins¶
Services can be deployed in a standalone environment where there is no integration with an identity service. The following plugins are provided to support standalone services:
HTTPBasicAuth: HTTP Basic authenticationNoAuth: No authentication
Standalone plugins must be given an endpoint that points to the URL of the one
service being used, since there is no service catalog to look up endpoints:
>>> from keystoneauth1 import noauth
>>> from keystoneauth1 import session
>>> auth = noauth.NoAuth(endpoint='http://hostname:6385/')
>>> sess = session.Session(auth=auth)
HTTPBasicAuth also requires a username
and password:
>>> from keystoneauth1 import http_basic
>>> from keystoneauth1 import session
>>> auth = http_basic.HTTPBasicAuth(
... endpoint='http://hostname:6385/',
... username='myUser',
... password='myPassword',
... )
>>> sess = session.Session(auth=auth)
Loading Plugins by Name¶
In auth_token middleware and for some service to service communication it is
possible to specify a plugin to load via name. The authentication options that
are available are then specific to the plugin that you specified. Currently the
authentication plugins that are available in keystoneauth are:
none:keystoneauth1.noauth.NoAuthhttp_basic:keystoneauth1.http_basic.HTTPBasicAuthadmin_token:keystoneauth1.token_endpoint.Tokenpassword:keystoneauth1.identity.generic.Passwordtoken:keystoneauth1.identity.generic.Tokenv2password:keystoneauth1.identity.v2.Passwordv2token:keystoneauth1.identity.v2.Tokenv3adfspassword:keystoneauth1.extras._saml2.v3.adfs.Passwordv3applicationcredential:keystoneauth1.identity.v3.ApplicationCredentialv3fedkerb:keystoneauth1.extras.kerberos.MappedKerberosv3kerberos:keystoneauth1.extras.kerberos.Kerberosv3multifactor:keystoneauth1.identity.v3.MultiFactorv3oauth1:keystoneauth1.extras.oauth1.v3.OAuth1v3oauth2clientcredential:keystoneauth1.identity.v3.OAuth2ClientCredentialv3oauth2mtlsclientcredential:keystoneauth1.identity.v3.OAuth2mTlsClientCredentialv3oidcaccesstoken:keystoneauth1.identity.v3.OidcAccessTokenv3oidcauthcode:keystoneauth1.identity.v3.OidcAuthorizationCodev3oidcclientcredentials:keystoneauth1.identity.v3.OidcClientCredentialsv3oidcdeviceauthz:keystoneauth1.identity.v3.oidc.OidcDeviceAuthorizationv3oidcpassword:keystoneauth1.identity.v3.OidcPasswordv3password:keystoneauth1.identity.v3.Passwordv3samlpassword:keystoneauth1.extras._saml2.v3.saml2.Passwordv3token:keystoneauth1.identity.v3.Tokenv3tokenlessauth:keystoneauth1.identity.v3.TokenlessAuthv3totp:keystoneauth1.identity.v3.TOTPv3websso:keystoneauth1.identity.v3.WebSSO
Creating Authentication Plugins¶
Creating an Identity Plugin¶
If you have implemented a new authentication mechanism into the Identity service then you will be able to reuse a lot of the infrastructure available for the existing Identity mechanisms. As the V2 identity API has been removed, it is expected that new plugins are for the V3 API.
To implement a new V3 plugin that can be combined with others you should
implement the base keystoneauth1.identity.v3.AuthMethod class
and implement the
get_auth_data() function.
If your Plugin cannot be used in conjunction with existing
keystoneauth1.identity.v3.AuthMethod then you should just
override keystoneauth1.identity.v3.Auth directly.
The new AuthMethod should take all
the required parameters via
__init__() and return from
get_auth_data() a tuple
with the unique identifier of this plugin (e.g. password) and a dictionary
containing the payload of values to send to the authentication server. The
session, calling auth object and request headers are also passed to this
function so that the plugin may use or manipulate them.
You should also provide a class that inherits from
keystoneauth1.identity.v3.Auth with an instance of your new
AuthMethod as the auth_methods
parameter to keystoneauth1.identity.v3.Auth.
By convention (and like above) these are named PluginType and
PluginTypeMethod (for example
Password and
PasswordMethod).
Creating a Custom Plugin¶
To implement an entirely new plugin you should implement the base class
keystoneauth1.plugin.BaseAuthPlugin and provide the
get_endpoint(),
get_token() and
invalidate() methods.
get_token() is called to retrieve
the string token from a plugin. It is intended that a plugin will cache a
received token and so if the token is still valid then it should be reused
rather than fetching a new one. A session object is provided with which the
plugin can contact its server. (Note: use authenticated=False when making
those requests or it will end up being called recursively). The return value
should be the token as a string.
get_endpoint() is called to
determine a base URL for a particular service’s requests. The keyword arguments
provided to the function are those that are given by the endpoint_filter
variable in keystoneauth1.session.Session.request(). A session object
is also provided so that the plugin may contact an external source to determine
the endpoint. Again this will be generally be called once per request and so
it is up to the plugin to cache these responses if appropriate. The return
value should be the base URL to communicate with.
invalidate() should also be
implemented to clear the current user credentials so that on the next
get_token() call a new token can
be retrieved.
The most simple example of a plugin is the
keystoneauth1.token_endpoint.Token plugin.