
    k6                        S r SSKJr  SSKJr  SSKJr  SSKrSSKrSSKrSSKJ	r	  SSK
Jr  SSKJr  SS	KJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJ r   SSKJ!r!  SSK"J#r#  SSK"J$r$  SSK%J&r'  Sr( " S S\RR                  5      r* " S S\RR                  5      r+S r,S r-S r.S r/S r0S0S jr1S r2S  r3S! r4S" r5S# r6S$ r7S% r8S& r9S' r:S( r;S) r<S* r=S0S+ jr>S, r?S- r@ S1S. jrAS/ rBg)2z%Support library for the auth command.    )absolute_import)division)unicode_literalsN)jwt)
exceptions)projects_api)util)config)log)
properties)
console_io)creds)files)	platforms)client)service_account)gcezserviceusage.services.usec                       \ rS rSrSrSrg)$MissingPermissionOnQuotaProjectError/   zCAn error when ADC does not have permission to bill a quota project. N__name__
__module____qualname____firstlineno____doc____static_attributes__r       0lib/googlecloudsdk/command_lib/auth/auth_util.pyr   r   /   s    Kr   r   c                       \ rS rSrSrSrg)AddQuotaProjectError3   zGAn error when quota project ID is added to creds that don't support it.r   Nr   r   r   r    r"   r"   3   s    Or   r"   c                     SSK Js  Jn  [        U [        R
                  5      =(       d    [        XR                  R                  5      $ )zHChecks if the credential is a Compute Engine service account credential.r   N)google.auth.compute_engineauthcompute_engine
isinstanceoauth2client_gceAppAssertionCredentialscredentialsCredentials)credgoogle_auth_gces     r    IsGceAccountCredentialsr/   7   s;    
 76
T+CC
D D
T66BB
CEr   c                     SSK Js  Jn  [        U [        R                  5      =(       d    [        XR
                  5      $ )z9Checks if the credential is a service account credential.r   N)google.oauth2.service_accountoauth2r   r(   ServiceAccountCredentialsr,   )r-   google_auth_service_accounts     r    IsServiceAccountCredentialr5   B   s5    
 FE
T?DD
E D
TBB
CEr   c                 @    [         R                  R                  U 5      $ )zGChecks if the credential is an impersonated service account credential.)impersonation_util ImpersonationAccessTokenProviderIsImpersonationCredentialr-   s    r    r9   r9   M   s    

*
*+D+DT+JLr   c                 `    [        U 5      =(       d    [        U 5      =(       d    [        U 5      $ N)r9   r5   r/   r:   s    r    ValidIdTokenCredentialr=   S   s)    
#D
) (
$T
*(
!$
')r   c                      [         R                  " 5       n U (       a]  [        R                  " SR	                  [
        R                  U [         R                  " 5       S95      n[        R                  " USSS9  gg)z/Warns users if ADC environment variable is set.a5  
          The environment variable [{envvar}] is set to:
            [{override_file}]
          Credentials will still be generated to the default location:
            [{default_file}]
          To use these credentials, unset this environment variable before
          running your application.
          )envvaroverride_filedefault_fileT)messagethrow_if_unattendedcancel_on_noN)
r
   ADCEnvVariabletextwrapdedentformatr   GOOGLE_APPLICATION_CREDENTIALSADCFilePathr   PromptContinue)r@   rB   s     r    PromptIfADCEnvVarIsSetrL   Y   sl    '')-oo  f::)!--/  1
2G TF r   c                 z   [         R                  " U 5      (       d2  [         R                  " U 5      (       d  [        R                  " S5        g[         R                  " U 5      (       a  U(       a  [        S5      e[        5         U(       a  [        U 5        g[         R                  " U 5      R                  5         g)z8Writes gclouds's credential from auth login to ADC json.zyCredentials cannot be written to application default credentials because it is not a user or external account credential.NzdThe application default credentials are external account credentials, quota project cannot be added.)
c_credsIsUserAccountCredentialsIsExternalAccountCredentialsr   warningr"   rL   DumpADCOptionalQuotaProjectADCDumpADCToFile)r   add_quota_projects     r    WriteGcloudCredentialsToADCrV   l   s     
*
*5
1
1

.
.u
5
5KK   ))%005F
	)* * &KK$$&r   c                  &   [         R                  R                  [        R                  " 5       5      (       d  g[
        R                  " [        R                  " 5       5       n [        R                  " U 5      sSSS5        $ ! , (       d  f       g= f)z5Reads ADC from disk and converts it to a json object.N)	ospathisfiler
   rJ   r   
FileReaderjsonload)fs    r    GetADCAsJsonr_      sQ    	**,	-	-**,-99Q< .--s   "B
Bc                  N    [        5       n  U S   $ ! [        [        4 a     gf = f)z<Reads the quota project ID from ADC json file and return it.quota_project_idN)r_   KeyError	TypeError)adc_jsons    r    GetQuotaProjectFromADCre      s1    ^(&''
I	 s    $$c                      [         R                  " 5       n [        R                  R	                  U 5      (       d  [
        R                  " S5      eg )NzuApplication default credentials have not been set up. Run $ gcloud auth application-default login to set it up first.)r
   rJ   rX   rY   rZ   c_excBadFileExceptionadc_paths    r    AssertADCExistsrk      sC    !(		!	!

 
 	JK K 
"r   c                      [         R                  " 5       n [        R                  " 5       R	                  U 5      u  p[        R
                  " U5      =(       d    [        R                  " U5      $ )zHReturns whether the ADC credentials correspond to a user account or not.)r
   rJ   rN   GetGoogleAuthDefaultload_credentials_from_filerO    IsExternalAccountUserCredentials)	cred_filer   _s      r    ADCIsUserAccountrr      sT      ")))+FF(%

*
*5
1 :

2
25
9;r   c                 X    [        5         [        R                  " U 5      n[        X!5      $ r<   )rk   project_utilParseProject%_AdcHasGivenPermissionOnProjectHelper)
project_idpermissionsproject_refs      r    AdcHasGivenPermissionOnProjectrz      s#    ))*5+	.{	HHr   c                 8   [         R                  R                  R                  R	                  5       n [         R                  R                  R                  R                  [        R                  " 5       5        [        R                  " X5      R                  n[        U5      [        U5      :H  [         R                  R                  R                  R                  U5        $ ! [         R                  R                  R                  R                  U5        f = fr<   )r   VALUESr&   credential_file_overrideGetSetr
   rJ   r   TestIamPermissionsrx   set)ry   rx   cred_file_override_oldgranted_permissionss       r    rv   rv      s    %,,11JJNNPP33778J8J8LM&99""-+ {s#67733778NOJ33778NOs   A<C$ $5Dc                 @    [         R                  R                  U 5      $ r<   )rX   rY   realpathri   s    r    GetAdcRealPathr      s    			(	##r   c                    U n[         R                  R                  5       [         R                  R                  :X  a  [	        U 5      n[
        R                  R                  SR                  U5      5        [
        R                  R                  S5        X:w  a&  [
        R                  " SR                  U5      5        gg)z?Prints the confirmation when ADC file was successfully written.z 
Credentials saved to file: [{}]zc
These credentials will be used by any library that requests Application Default Credentials (ADC).a  You may be running gcloud with a python interpreter installed from Microsoft Store which is not supported by this command. Run `gcloud topic startup` for instructions to select a different python interpreter. Otherwise, you have to set the environment variable `GOOGLE_APPLICATION_CREDENTIALS` to the file path `{}`. See https://cloud.google.com/docs/authentication/getting-started#setting_the_environment_variable for more information.N)
r   OperatingSystemCurrentWINDOWSr   r   statusPrintrH   rQ   )rj   	real_paths     r    LogADCIsWrittenr      s    )&&(I,E,E,M,MMx(I**6==iHI**/0 KK ( )/y(9; r   c                 `    [         R                  R                  SR                  U 5      5        g )Nz
Quota project "{}" was added to ADC which can be used by Google client libraries for billing and quota. Note that some services may still bill the project owning the resource.)r   r   r   rH   quota_projects    r    LogQuotaProjectAddedr      s$    **44:F=4IKr   c                  0    [         R                  " S5        g )Nz
Cannot find a quota project to add to ADC. You might receive a "quota exceeded" or "API not enabled" error. Run $ gcloud auth application-default set-quota-project to add a quota project.r   rQ   r   r   r    LogQuotaProjectNotFoundr      s    ++ ! "r   c                 X    [         R                  " SR                  U [        5      5        g )Na  
Cannot add the project "{}" to ADC as the quota project because the account in ADC does not have the "{}" permission on this project. You might receive a "quota_exceeded" or "API not enabled" error. Run $ gcloud auth application-default set-quota-project to add a quota project.)r   rQ   rH   SERVICEUSAGE_PERMISSIONr   s    r    "LogMissingPermissionOnQuotaProjectr      s#    ++ '>?Ar   c                  0    [         R                  " S5        g )Nz
Quota project is disabled. You might receive a "quota exceeded" or "API not enabled" error. Run $ gcloud auth application-default set-quota-project to add a quota project.r   r   r   r    LogQuotaProjectDisabledr      s    ++23r   c                     [         R                  " U 5      R                  5       n[        U5        U(       a  [	        5         gg)zDumps the given credentials to ADC file.

Args:
   credentials: a credentials from oauth2client or google-auth libraries, the
     credentials to dump.
   quota_project_disabled: bool, If quota project is explicitly disabled by
     users using flags.
N)rN   rS   rT   r   r   )r+   quota_project_disabledrj   s      r    DumpADCr      s1     [[%335(( r   c                 L   [         R                  " U 5      R                  5       n[        U5        [         R                  " U SS9nU(       d  [        5         g[        U[        /S9(       a/  [         R                  " U 5      R                  US9  [        U5        g[        U5        g)aq  Dumps the given credentials to ADC file with an optional quota project.

Loads quota project from gcloud's context and writes it to application default
credentials file if the credentials has the "serviceusage.services.use"
permission on the quota project..

Args:
   credentials: a credentials from oauth2client or google-auth libraries, the
     credentials to dump.
T)force_resource_quotarx   r   N)rN   rS   rT   r   GetQuotaProjectr   rz   r   DumpExtendedADCToFiler   r   )r+   rj   r   s      r    rR   rR      s|     [[%335(()).-	%"9!:<KK222O'&}5r   c                 R   [        5         [        5       (       d  [        R                  " S5      e[        R
                  " 5       R                  [        R                  " 5       5      u  pUR                  n[        R                  " U5      R                  5         [        U [        /S9(       dB  [        R                  " U5      R                  US9  [        SR!                  U [        5      5      e[        R                  " U5      R                  U S9n[#        U5        [%        U 5        g)a|  Adds the quota project to the existing ADC file.

Quota project is only added to ADC when the credentials have the
"serviceusage.services.use" permission on the project.

Args:
  quota_project: str, The project id of a valid GCP project to add to ADC.

Raises:
  MissingPermissionOnQuotaProjectError: If the credentials do not have the
    "serviceusage.services.use" permission.
z\The application default credentials are not user credentials, quota project cannot be added.r   r   zCannot add the project "{}" to application default credentials (ADC) as a quota project because the account in ADC does not have the "{}" permission on this project.N)rk   rr   rg   rh   rN   rm   rn   r
   rJ   ra   rS   rT   rz   r   r   r   rH   r   r   )r   r+   rq   previous_quota_projectrj   s        r    AddQuotaProjectToADCr     s    			

 
 	# 
 //1LL.+ '77 
++k((*	'"9!:
 KK22, 3  /	++162,
  [[%;;! < ( (}%r   c                 d    [         R                  " XX#5      R                  5       n[        U5        g r<   )rN   rS   rT   r   )r+   target_principal	delegatesscopesrj   s        r    #DumpImpersonatedServiceAccountToADCr   Q  s)     [[",,9MO (r   c                     [         R                  " UR                  SS9nUS   nU (       aE  U R                  5       UR                  5       :w  a#  [        R
                  " SR                  XS95      eU$ )z=Extracts account from creds and validates it against account.F)verifyemailzYou attempted to log in as account [{account}] but the received credentials were for account [{web_flow_account}].

Please check that your browser is logged in as account [{account}] and that you are using the correct browser profile.)accountweb_flow_account)r   decodeid_tokenlowerauth_exceptionsWrongAccountErrorrH   )r   r   decoded_id_tokenr   s       r    ExtractAndValidateAccountr   Z  sr    ZZu=%g.$4$:$:$<<

+
+	> ?Ef ?E ?
	  
r   )Fr<   )Cr   
__future__r   r   r   r\   rX   rF   google.authr   googlecloudsdk.api_lib.authr   r   +googlecloudsdk.api_lib.cloudresourcemanagerr   %googlecloudsdk.api_lib.iamcredentialsr	   r7   googlecloudsdk.callioperg   #googlecloudsdk.command_lib.projectsrt   googlecloudsdk.corer
   r   r   googlecloudsdk.core.consoler   googlecloudsdk.core.credentialsr   rN   googlecloudsdk.core.utilr   r   oauth2clientr   r   oauth2client.contribr   r)   r   ADCErrorr   r"   r/   r5   r9   r=   rL   rV   r_   re   rk   rr   rz   rv   r   r   r   r   r   r   r   rR   r   r   r   r   r   r    <module>r      s     , &  '  	   E D L 7 D & # * 2 < * .  ( 8 6 L7+;+; LP7++ PEEL)F&',K;IP$;0K"A3645&v 04r   