
                            S r SSKrSSKrSSKrSSKJrJr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	rS
rSrS\
R0                  4S jrS\\   4S jr SS\S\\\      S\\   4S jjrS\\   4S jr\R>                  4S\\   S\\    S\\   4S jjr!\R>                  4S\\   S\\    S\\   4S jjr"S\4S jr#S\S\4S jr$S\S\S\\   4S jr%S r&g)z6Utilities to load Google Auth credentials from gcloud.    N)IteratorListOptional)credentials)utils)gcloud_runner)bq_error)bq_gcloud_utilsz#No such file or directory: 'gcloud'z%https://www.googleapis.com/auth/drivez.https://www.googleapis.com/auth/cloud-platformreturnc                  >   [         R                  " 5       n U R                  S0 5      R                  SS5      n[        R                  " SU5        [
        R                  " U5      n[        U5      nU(       a  SO	[        5       nU(       a  [        OSn[        UUSLS9n[        R                  " UUUU[        R                  " 5       [        R                  " 5       [        R                   " 5       [
        R"                  " [$        R&                  R(                  U5      S9$ )z-Loads credentials by calling gcloud commands.coreaccount z4Loading auth credentials from gcloud for account: %sNis_service_accounthas_refresh_token)r   tokenrefresh_tokenrefresh_handler	client_idclient_secret	token_uriquota_project_id)r
   load_configgetlogginginfobq_utilsIsServiceAccount_GetAccessTokenAndPrintOutput_GetRefreshTokenAndPrintOutput_ServiceAccountRefreshHandler_GetFallbackQuotaProjectIdgoogle_oauth2Credentialsbq_auth_utilsget_client_idget_client_secretget_token_uriGetResolvedQuotaProjectIDbq_auth_flagsQUOTA_PROJECT_IDvalue)gcloud_configr   r   access_tokenr   r   fallback_quota_project_ids          ,platform/bq/auth/gcloud_credential_loader.pyLoadCredentialr2      s    !--/-fb)--i<'	,,EwO009./AB, !d&D&F  (:#t  9+%T1
 
	"	"!%++-!335++-99

(
(
.
.0I
     c                      / n [         R                  R                  (       a  [        nU R	                  U[
        /5        U $ N)bq_flagsENABLE_GDRIVEr-   _GDRIVE_SCOPEextend
_GCP_SCOPE)scopesdrive_scopes     r1   
_GetScopesr=   ;   s1    &!!K
MM;
+,	-r3   r   r;   c                     Uc
  [        5       OUnU (       a%  U(       a  [        SSSSR                  U5      /5      $ [        SS/5      $ )Nauthzprint-access-tokenz--scopes,)r=   &_GetTokenFromGcloudAndPrintOtherOutputjoinr   r;   s     r1   r    r    C   sL     ">:<v&F1	%z388F3CD  
09M0N	OOr3   c                      [        SS/5      $ )Nr?   zprint-refresh-token)rA    r3   r1   r!   r!   N   s    	/9N0O	PPr3   cmdstderrc           	          Sn[        X5       H   nU(       a  SU;  a  Un  U$ [        U5        M"     U$ ! [        R                   a  n[	        U5      R                  SS5      nSU;   a!  [        R                  " S[        5       -   5      eSU;   a!  [        R                  " S[        5       -   5      eS	U;   a   SnAg[        R                  " S
[        [	        U5      5      -  5      eSnAf[         ab  n[	        U5      R                  SS5      n[        U;   a  [        R                  " S5      e[        R                  " S
[	        U5      -  5      eSnAff = f)zGReturns a token or prints other messages from the given gcloud command.N 
r   zsecurity keyzTAccess token has expired. Did you touch the security key within the timeout window?
zRefresh token has expiredzRefresh token has expired. zdo not support refresh tokensz1Error retrieving auth credentials from gcloud: %sz'gcloud' not found but is required for authentication. To install, follow these instructions: https://cloud.google.com/sdk/docs/install)
_RunGcloudCommandprintr	   BigqueryErrorstrreplace_GetReauthMessage_UpdateReauthMessage	Exception'ERROR_TEXT_PRODUCED_IF_GCLOUD_NOT_FOUND)rF   rG   r   outputesingle_line_error_msgs         r1   rA   rA   R   s\   
(E#C0	Cv% L 	f 1 L			 FNN44..""  
 
%(=	=""
'*;*=
=  
),A	A""
= Q()  
 
FNN44.2GG""7 
 
 
 ;c!fD 
s-   !5 5 E	A/C(=+C((E5AEEc              #     #    [         R                  " XS9n/ nUR                  (       a  [        UR                  R                  S5       HP  n[        U5      R                  5       nUR                  S5      (       d  U(       a  UR                  U5        ML  Uv   MR     UR                  R                  5         UR                  5       nU(       a%  [        R                  " SR                  U5      5      eg7f)zTRuns the given gcloud command, yields the output, and returns the final status code.)rG   r   zERROR:rJ   N)r   run_gcloud_commandstdoutiterreadlinerN   strip
startswithappendclosewaitr	   rM   rB   )rF   rG   proc
error_msgsstdout_linelinereturn_codes          r1   rK   rK      s      
	)	)#	=$*	[[DKK00"5##%d		"	"j$
 6 	KK		+

 
 :!6
77 s   C3C5c                  X    S[         R                  R                  (       a  SOS-   n SU -  $ )N$ gcloud auth loginz --enable-gdrive-accessr   zTo re-authenticate, run:

%s)r6   r7   r-   )gcloud_commands    r1   rP   rP      s-    (#+#9#9#?#?R. 
*N	::r3   messagec                 t    SU ;  d  [         R                  R                  (       d  U $ U R                  SS5      $ )Nrg   z*$ gcloud auth login --enable-gdrive-access)r6   r7   r-   rO   )ri   s    r1   rQ   rQ      s4    ')1G1G1M1MN	2
 r3   r   c                 V    U (       a  g U(       d  g [         R                  R                  $ r5   )r6   
PROJECT_IDr-   r   s     r1   r#   r#      s"     					"	""r3   c                     A [        SUS9n[        R                  R                  [        R                  R                  5      [        R
                  " SS9-   nUR                  SS9nX#4$ )z1Refreshes the access token for a service account.TrC   7   )minutesN)tzinfo)r    datetimenowtimezoneutc	timedeltarO   )requestr;   r/   expirys       r1   r"   r"      sh    .f,   !2!2!6!67(:L:L; & >>>&&		r3   r5   )'__doc__rq   r   
subprocesstypingr   r   r   google.oauth2r   r$   r+   r6   r   r?   r   r&   gcloud_wrapperr   r	   r
   rS   r8   r:   r%   r2   rN   r=   boolr    r!   STDOUTintrA   rK   rP   rQ   r#   r"   rE   r3   r1   <module>r      sX   <    + + 6    ' (  !*O '7=
11 DDI  =APP&.tCy&9Pc]PQ Q '---	c-SM- c]-b -7,=,=8	c8$SM8c]8&;3 ;# # ##15#c]#r3   