
                            d Z ddlZddlZddlZddlmZmZmZ ddlm	Z
 ddlZddlZddlZddlmZ ddlmZ ddlmZ ddlmZ d	Zd
ZdZde
j0                  fdZdee   fdZ	 ddedeee      dee   fdZdee   fdZej>                  fdee   dee    dee   fdZ!ej>                  fdee   dee    dee   fdZ"defdZ#dedefdZ$dededee   fdZ%d Z&y)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                  &   t        j                         } | j                  di       j                  dd      }t        j                  d|       t        j                  |      }t        |      }|rdn	t               }|rt        nd}t        ||du      }t        j                  ||||t        j                         t        j                         t        j                          t        j"                  t$        j&                  j(                  |            S )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                  x    g } t         j                  j                  rt        }| j	                  |t
        g       | S N)bq_flagsENABLE_GDRIVEr-   _GDRIVE_SCOPEextend
_GCP_SCOPE)scopesdrive_scopes     r1   
_GetScopesr=   ;   s1    &!!K
MM;
+,	-r3   r   r;   c                 |    |
t               n|}| r |rt        ddddj                  |      g      S t        ddg      S )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                      t        ddg      S )Nr?   zprint-refresh-token)rA    r3   r1   r!   r!   N   s    	/9N0O	PPr3   cmdstderrc           	         	 d}t        | |      D ]  }|r	d|vr|} |S t        |        |S # t        j                  $ r}t	        |      j                  dd      }d|v r t        j                  dt               z         d|v r t        j                  dt               z         d	|v rY d}~yt        j                  d
t        t	        |            z        d}~wt        $ r^}t	        |      j                  dd      }t        |v rt        j                  d      t        j                  d
t	        |      z        d}~ww xY w)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-   - - E  A'C,*CE "AD;;E c              #     K   t        j                  | |      }g }|j                  rt        |j                  j                  d      D ]D  }t        |      j                         }|j                  d      s|r|j                  |       A| F |j                  j                          |j                         }|r$t        j                  dj                  |            yw)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      
	)	)#f	=$*	[[DKK00"5##%d		"j$
 6 	KK		+

 
 :!6
77 s   CC c                  N    dt         j                  j                  rdndz   } d| z  S )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                 f    d| vst         j                  j                  s| S | j                  dd      S )Nrg   z*$ gcloud auth login --enable-gdrive-access)r6   r7   r-   rO   )ri   s    r1   rQ   rQ      s4    ')1G1G1M1MN	2
 r3   r   c                 B    | ry |sy t         j                  j                  S r5   )r6   
PROJECT_IDr-   r   s     r1   r#   r#      s"     					"	""r3   c                     ~ t        d|      }t        j                  j                  t        j                  j                        t        j
                  d      z   }|j                  d      }||fS )z1Refreshes the access token for a service account.TrC   7   )minutesN)tzinfo)r    datetimenowtimezoneutc	timedeltarO   )requestr;   r/   expirys       r1   r"   r"      sj    .f,   !2!2!6!67(:L:L; & >>>&&	v	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   