
    ~!                        d Z ddlmZ ddlmZ ddlmZ ddlZddl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d	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlZ ej>                  d      Z  ej>                  d      Z!ejD                  ejF                   G d dejH                                      Z%y)a  A git credential helper that provides Google git repository passwords.

Reads a session from stdin that looks a lot like:
  protocol=https
  host=code.google.com
And writes out a session to stdout that looks a lot like:
  username=me
  password=secret

Errors will be reported on stderr.

Note that spaces may be part of key names so, for example, "protocol" must not
be proceeded by leading spaces.
    )absolute_import)division)unicode_literalsN)
exceptions)base)log)
properties)creds)store)files)	platforms)clientz	(.+)=(.*)z^ *$c                       e Zd ZdZdZdZeegZdZed        Z	 e
j                  ej                  ej                        d        Zd Zd Zy	)
	GitHelperzEA git credential helper to provide access to Google git repositories.getr   zgooglesource.comc                 R    | j                  dd       | j                  ddd       y )Nmethodz!The git credential helper method.)helpz--ignore-unknown
store_truezTProduce no output and exit with 0 when given an unknown method (e.g. store) or host.)actionr   )add_argument)parsers    lib/surface/auth/git_helper.pyArgszGitHelper.ArgsG   s;    
@  B
*+H  J    c           
      2	   t         j                  j                  j                  j	                  d       |j
                  t        j                  vrZ|j                  ryt        j                  dj                  |j
                  dj                  t        j                                    | j                         }dt        j                  gddd	d
t        j                  z   gt         j                  j                  j                   j#                         }|r j%                  |j'                  d             |j)                  d      }fd} ||      s2|j                  s%t        j                  dj                  |            y|j
                  t        j*                  k(  rt         j                  j                  j,                  j#                         }	 t/        j0                  |d      }t/        j2                  |       | jG                          |t        j                  k(  s"|jI                  d
t        j                  z         rd}	n|}	tK        jL                  |      r|jN                  }
n|jP                  }
t8        jR                  j=                  t?        j@                  d      j                  |	|
             y|j
                  t        jT                  k(  rtV        jX                  j[                         tV        jX                  j\                  k(  rt_        j`                  d       	 dj                  |j)                  d      |j)                  d            }t_        j`                  d|       tc        jd                  ddgtb        jf                  tb        jf                  tb        jf                        }|ji                  |      \  }}|jj                  rt_        j`                  d||       yyyy# t4        j6                  $ r_}t8        j:                  j=                  t?        j@                  dj                  tC        jD                  |                         Y d}~yd}~ww xY w# tl        $ r!}t_        j`                  d d!       Y d}~yd}~ww xY w)"zRun the helper command.FNz8Unexpected method [{meth}]. One of [{methods}] expected.z, )methmethodssource.developers.google.comz.sourcemanager.devz.blueoryx.devz.developerconnect.dev.,hostc                 D    | v ryD ]  }| j                  |      s y y)NTF)endswith)r"   suffixcredentialed_domainscredentialed_domains_suffixs     r   _ValidateHostz$GitHelper.Run.<locals>._ValidateHostn   s-    	%	%/&==  0 r   zUnknown host [{host}].)r"   T)use_google_authzV            ERROR: {error}
            Run 'gcloud auth login' to log in.
            )errorzgit-accountzF          username={username}
          password={password}
          )usernamepasswordzClearing OSX credential cache.z!protocol={protocol}
host={host}

protocol)r-   r"   zCalling erase with input:
%szgit-credential-osxkeychainerase)stdinstdoutstderrz7Failed to clear OSX keychain:
stdout: {%s}
stderr: {%s}zFailed to clear OSX keychain)exc_info)7r	   VALUESauth#service_account_use_self_signed_jwtSetr   r   METHODSignore_unknownauth_exceptionsGitCredentialHelperErrorformatjoin_ParseInputGOOGLESOURCEcore credentialed_hosted_repo_domainsGetextendsplitr   GETaccountc_storeLoadRefreshcreds_exceptionsErrorsysr1   writetextwrapdedentsix	text_type_CheckNetrcr$   c_credsIsOauth2ClientCredentialsaccess_tokentokenr0   STOREr   OperatingSystemCurrentMACOSXr   debug
subprocessPopenPIPEcommunicate
returncode	Exception)selfargsinfoextrar"   r(   rE   credesent_accountrT   input_stringpouterrr&   r'   s                  @@r   RunzGitHelper.RunP   s   
 >>BB5I{{)+++			44
D6t{{DIIi6G6G,H6IK K D&
 	I"""	# ""CCGGIE!!%++c"2388FD   66$+++68 	8{{imm#!!&&..224g||GT:  )((
(]]3y5556$		*	*4	0((zz	jj
//   v|lvC	E
 
		' 
#
#
+
+
-

#
#
*
*+		23	C?FFxx
+$((62B G D,
))3\
B <gF%/__&0oo&0oo7! }}\2*3\\IIKS + 
(7 ## 

 * S]]1-.0 	1 	l  	C
))2T
B
B	Cs2   ,O7 0CQ, 7Q)
AQ$$Q),	R5RRc                 6   i }t         j                  D ]  }t        j                  |      rt        j                  |      }|s4t        j                  dj                  |j                  d                  |j                         \  }}|j                         ||<    d|vrt        j                  d      d|vrt        j                  d      |j                  d      dk7  r4t        j                  d	j                  |j                  d      
            |S )zgParse the fields from stdin.

    Returns:
      {str: str}, The parsed parameters given on stdin.
    z&Invalid input line format: [{format}].
)r;   r-   z Required key "protocol" missing.r"   zRequired key "host" missing.httpsz*Invalid protocol [{p}].  "https" expected.)ri   )rK   r/   _BLANK_LINE_REmatch
_KEYVAL_REr9   r:   r;   rstripgroupsstripr   )ra   rc   linerq   keyvals         r   r=   zGitHelper._ParseInput   s    D					d	#t$e664V4;;t,V-/ 	/ hc3))+d3i  44
,. . T44
(* * xx
w&44
66DHHZ(6)+ + Kr   c                     d } |t        j                  t        j                  j	                  dd                    |t        j                  t        j                  j	                  dd                   y)z:Warn on stderr if ~/.netrc contains redundant credentials.c                    t         j                  j                  |       sy	 t        j                  |       }d|v rCt
        j                  j                  t        j                  dj                  |                    yy# t        $ r Y yw xY w)z9Warn about other credential helpers that will be ignored.Nr   zYou have credentials for your Google repository in [{path}]. This repository's
git credential helper is set correctly, so the credentials in [{path}] will not
be used, but you may want to remove them to avoid confusion.
)path)osr{   existsr   ReadFileContentsrK   r1   rL   rM   rN   r;   r`   )ri   datas     r   Checkz$GitHelper._CheckNetrc.<locals>.Check   sz    WW^^A
%%a()T1
**

8?? , FFN	  2  s   AA? ?	B
B~z.netrc_netrcN)r   ExpandHomeDirr|   r{   r<   )ra   r   s     r   rQ   zGitHelper._CheckNetrc   sM     
%

bggll39
:;	%

bggll39
:;r   N)__name__
__module____qualname____doc__rD   rV   r7   r>   staticmethodr   c_excRaiseErrorInsteadOfr9   AuthenticationErrorr   rJ   rl   r=   rQ    r   r   r   r   <   sr     N#
%%L'#,J J 5_@@&,,OgC PgCR@<r   r   )&r   
__future__r   r   r   r|   rer[   rK   rM   googlecloudsdk.api_lib.authr   r9   googlecloudsdk.callioper   r   googlecloudsdk.corer   r	   googlecloudsdk.core.credentialsr
   rR   rI   r   rF   googlecloudsdk.core.utilr   r   oauth2clientr   rO   compilerr   rp   HiddenDefaultUniverseOnlyCommandr   r   r   r   <module>r      s     '  ' 	 	  
  E ( 7 # * < J < * .  
 RZZ%
G$ o< o<  o<r   