<s> from keystoneclient . auth . identity import v2 as v2_auth <EOL> from keystoneclient . auth . identity import v3 as v3_auth <EOL> from keystoneclient import discover <EOL> from keystoneclient import exceptions as ks_exc <EOL> from keystoneclient import session <EOL> from oslo_utils import strutils <EOL> import six . moves . urllib . parse as urlparse <EOL> from cloudkittyclient . common import utils <EOL> from cloudkittyclient import exc <EOL> from cloudkittyclient . openstack . common . apiclient import auth <EOL> from cloudkittyclient . openstack . common . apiclient import exceptions <EOL> def _discover_auth_versions ( session , auth_url ) : <EOL> v2_auth_url = None <EOL> v3_auth_url = None <EOL> try : <EOL> ks_discover = discover . Discover ( session = session , auth_url = auth_url ) <EOL> v2_auth_url = ks_discover . url_for ( '<STR_LIT>' ) <EOL> v3_auth_url = ks_discover . url_for ( '<STR_LIT>' ) <EOL> except ks_exc . DiscoveryFailure : <EOL> raise <EOL> except exceptions . ClientException : <EOL> url_parts = urlparse . urlparse ( auth_url ) <EOL> ( scheme , netloc , path , params , query , fragment ) = url_parts <EOL> path = path . lower ( ) <EOL> if path . startswith ( '<STR_LIT>' ) : <EOL> v3_auth_url = auth_url <EOL> elif path . startswith ( '<STR_LIT>' ) : <EOL> v2_auth_url = auth_url <EOL> else : <EOL> raise exc . CommandError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> return v2_auth_url , v3_auth_url <EOL> def _get_keystone_session ( ** kwargs ) : <EOL> cacert = kwargs . pop ( '<STR_LIT>' , None ) <EOL> cert = kwargs . pop ( '<STR_LIT>' , None ) <EOL> key = kwargs . pop ( '<STR_LIT:key>' , None ) <EOL> insecure = kwargs . pop ( '<STR_LIT>' , False ) <EOL> auth_url = kwargs . pop ( '<STR_LIT>' , None ) <EOL> project_id = kwargs . pop ( '<STR_LIT>' , None ) <EOL> project_name = kwargs . pop ( '<STR_LIT>' , None ) <EOL> if insecure : <EOL> verify = False <EOL> else : <EOL> verify = cacert or True <EOL> if cert and key : <EOL> cert = ( cert , key ) <EOL> ks_session = session . Session ( verify = verify , cert = cert ) <EOL> v2_auth_url , v3_auth_url = _discover_auth_versions ( ks_session , auth_url ) <EOL> username = kwargs . pop ( '<STR_LIT:username>' , None ) <EOL> user_id = kwargs . pop ( '<STR_LIT>' , None ) <EOL> user_domain_name = kwargs . pop ( '<STR_LIT>' , None ) <EOL> user_domain_id = kwargs . pop ( '<STR_LIT>' , None ) <EOL> project_domain_name = kwargs . pop ( '<STR_LIT>' , None ) <EOL> project_domain_id = kwargs . pop ( '<STR_LIT>' , None ) <EOL> auth = None <EOL> use_domain = ( user_domain_id or user_domain_name or <EOL> project_domain_id or project_domain_name ) <EOL> use_v3 = v3_auth_url and ( use_domain or ( not v2_auth_url ) ) <EOL> use_v2 = v2_auth_url and not use_domain <EOL> if use_v3 : <EOL> auth = v3_auth . Password ( <EOL> v3_auth_url , <EOL> username = username , <EOL> password = kwargs . pop ( '<STR_LIT:password>' , None ) , <EOL> user_id = user_id , <EOL> user_domain_name = user_domain_name , <EOL> user_domain_id = user_domain_id , <EOL> project_name = project_name , <EOL> project_id = project_id , <EOL> project_domain_name = project_domain_name , <EOL> project_domain_id = project_domain_id ) <EOL> elif use_v2 : <EOL> auth = v2_auth . Password ( <EOL> v2_auth_url , <EOL> username , <EOL> kwargs . pop ( '<STR_LIT:password>' , None ) , <EOL> tenant_id = project_id , <EOL> tenant_name = project_name ) <EOL> else : <EOL> raise exc . CommandError ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> ks_session . auth = auth <EOL> return ks_session <EOL> def _get_endpoint ( ks_session , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> endpoint_type = kwargs . get ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> service_type = kwargs . get ( '<STR_LIT>' ) or '<STR_LIT>' <EOL> endpoint = ks_session . get_endpoint ( service_type = service_type , <EOL> interface = endpoint_type , <EOL> region_name = kwargs . get ( '<STR_LIT>' ) ) <EOL> return endpoint <EOL> class AuthPlugin ( auth . BaseAuthPlugin ) : <EOL> opt_names = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:key>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:password>' , '<STR_LIT:username>' , '<STR_LIT>' ] <EOL> def __init__ ( self , auth_system = None , ** kwargs ) : <EOL> self . opt_names . extend ( self . common_opt_names ) <EOL> super ( AuthPlugin , self ) . __init__ ( auth_system , ** kwargs ) <EOL> def _do_authenticate ( self , http_client ) : <EOL> token = self . opts . get ( '<STR_LIT>' ) or self . opts . get ( '<STR_LIT>' ) <EOL> endpoint = self . opts . get ( '<STR_LIT>' ) <EOL> if not ( token and endpoint ) : <EOL> project_id = ( self . opts . get ( '<STR_LIT>' ) or <EOL> self . opts . get ( '<STR_LIT>' ) ) <EOL> project_name = ( self . opts . get ( '<STR_LIT>' ) or <EOL> self . opts . get ( '<STR_LIT>' ) ) <EOL> ks_kwargs = { <EOL> '<STR_LIT:username>' : self . opts . get ( '<STR_LIT:username>' ) , <EOL> '<STR_LIT:password>' : self . opts . get ( '<STR_LIT:password>' ) , <EOL> '<STR_LIT>' : self . opts . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . opts . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . opts . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : project_id , <EOL> '<STR_LIT>' : project_name , <EOL> '<STR_LIT>' : self . opts . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . opts . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . opts . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . opts . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : self . opts . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:key>' : self . opts . get ( '<STR_LIT:key>' ) , <EOL> '<STR_LIT>' : strutils . bool_from_string ( <EOL> self . opts . get ( '<STR_LIT>' ) ) , <EOL> '<STR_LIT>' : self . opts . get ( '<STR_LIT>' ) , <EOL> } <EOL> ks_session = _get_keystone_session ( ** ks_kwargs ) <EOL> token = lambda : ks_session . get_token ( ) <EOL> endpoint = ( self . opts . get ( '<STR_LIT>' ) or <EOL> _get_endpoint ( ks_session , ** ks_kwargs ) ) <EOL> self . opts [ '<STR_LIT>' ] = token <EOL> self . opts [ '<STR_LIT>' ] = endpoint <EOL> def token_and_endpoint ( self , endpoint_type , service_type ) : <EOL> token = self . opts . get ( '<STR_LIT>' ) <EOL> if callable ( token ) : <EOL> token = token ( ) <EOL> return token , self . opts . get ( '<STR_LIT>' ) <EOL> def sufficient_options ( self ) : <EOL> """<STR_LIT>""" <EOL> has_token = self . opts . get ( '<STR_LIT>' ) or self . opts . get ( '<STR_LIT>' ) <EOL> no_auth = has_token and self . opts . get ( '<STR_LIT>' ) <EOL> has_tenant = self . opts . get ( '<STR_LIT>' ) or self . opts . get ( '<STR_LIT>' ) <EOL> has_credential = ( self . opts . get ( '<STR_LIT:username>' ) and has_tenant <EOL> and self . opts . get ( '<STR_LIT:password>' ) <EOL> and self . opts . get ( '<STR_LIT>' ) ) <EOL> missing = not ( no_auth or has_credential ) <EOL> if missing : <EOL> missing_opts = [ ] <EOL> opts = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:username>' , '<STR_LIT:password>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> for opt in opts : <EOL> if not self . opts . get ( opt ) : <EOL> missing_opts . append ( opt ) <EOL> raise exceptions . AuthPluginOptionsMissing ( missing_opts ) <EOL> def Client ( version , * args , ** kwargs ) : <EOL> module = utils . import_versioned_module ( version , '<STR_LIT>' ) <EOL> client_class = getattr ( module , '<STR_LIT>' ) <EOL> kwargs [ '<STR_LIT>' ] = kwargs . get ( '<STR_LIT>' ) or kwargs . get ( '<STR_LIT>' ) <EOL> return client_class ( * args , ** kwargs ) <EOL> def _adjust_params ( kwargs ) : <EOL> timeout = kwargs . get ( '<STR_LIT>' ) <EOL> if timeout is not None : <EOL> timeout = int ( timeout ) <EOL> if timeout <= <NUM_LIT:0> : <EOL> timeout = None <EOL> insecure = strutils . bool_from_string ( kwargs . get ( '<STR_LIT>' ) ) <EOL> verify = kwargs . get ( '<STR_LIT>' ) <EOL> if verify is None : <EOL> if insecure : <EOL> verify = False <EOL> else : <EOL> verify = kwargs . get ( '<STR_LIT>' ) or True <EOL> cert = kwargs . get ( '<STR_LIT>' ) <EOL> key = kwargs . get ( '<STR_LIT>' ) <EOL> if cert and key : <EOL> cert = cert , key <EOL> return { '<STR_LIT>' : verify , '<STR_LIT>' : cert , '<STR_LIT>' : timeout } <EOL> def get_client ( version , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> endpoint = kwargs . get ( '<STR_LIT>' ) <EOL> cli_kwargs = { <EOL> '<STR_LIT:username>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT:password>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) or kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : kwargs . get ( '<STR_LIT>' ) , <EOL> } <EOL> cli_kwargs . update ( kwargs ) <EOL> cli_kwargs . update ( _adjust_params ( cli_kwargs ) ) <EOL> return Client ( version , endpoint , ** cli_kwargs ) <EOL> def get_auth_plugin ( endpoint , ** kwargs ) : <EOL> auth_plugin = AuthPlugin ( <EOL> auth_url = kwargs . get ( '<STR_LIT>' ) , <EOL> service_type = kwargs . get ( '<STR_LIT>' ) , <EOL> token = kwargs . get ( '<STR_LIT>' ) , <EOL> endpoint_type = kwargs . get ( '<STR_LIT>' ) , <EOL> cacert = kwargs . get ( '<STR_LIT>' ) , <EOL> tenant_id = kwargs . get ( '<STR_LIT>' ) or kwargs . get ( '<STR_LIT>' ) , <EOL> endpoint = endpoint , <EOL> username = kwargs . get ( '<STR_LIT:username>' ) , <EOL> password = kwargs . get ( '<STR_LIT:password>' ) , <EOL> tenant_name = kwargs . get ( '<STR_LIT>' ) , <EOL> user_domain_name = kwargs . get ( '<STR_LIT>' ) , <EOL> user_domain_id = kwargs . get ( '<STR_LIT>' ) , <EOL> project_domain_name = kwargs . get ( '<STR_LIT>' ) , <EOL> project_domain_id = kwargs . get ( '<STR_LIT>' ) <EOL> ) <EOL> return auth_plugin </s>
