
    3                         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rS\ S3r	S	r
\R                  " \5      rS
 rS rS rS rS r " S S\5      rSS jrS rS rS rSS jrSS jrS rg)    N   )credentials)errors)configz	docker.iozhttps://index.z/v1/z<token>c                     SU ;   a  [         R                  " SU  S35      e[        U 5      u  pUS   S:X  d	  US   S:X  a%  [         R                  " SR                  U5      5      e[	        U5      U4$ )Nz://z)Repository name cannot contain a scheme ()r   -z;Invalid index name ({}). Cannot begin or end with a hyphen.)r   InvalidRepositorysplit_repo_nameformatresolve_index_name)	repo_name
index_nameremote_names      lib/third_party/docker/auth.pyresolve_repository_namer      s    	&&7	{!D
 	
 .i8J!}z"~4&&vj)
 	
 j);66    c                 B    [        U 5      n U S[        -   :X  a  [        n U $ )Nzindex.)convert_to_hostname
INDEX_NAME)r   s    r   r   r      s$    $Z0JX
**
r   c                    [         R                  S5        U R                  (       a  U R                  R                  (       a-  [         R                  S5        [	        U R
                  S9U l        [        U R                  XR
                  S9nU(       a   [         R                  S5        [        U5      $ [         R                  S5        g )NzLooking for auth configz2No auth config in memory - loading from filesystem)credstore_envzFound auth configzNo auth config found)logdebug_auth_configsis_emptyload_configr   resolve_authconfigencode_header)clientregistryauthcfgs      r   get_config_headerr$   &   s    II'(6#7#7#@#@		@	
  +9M9MN h6J6JG 		%& W%%II$%r   c                     U R                  SS5      n[        U5      S:X  d  SUS   ;  a  SUS   ;  a  US   S:w  a  [        U 4$ [        U5      $ )N/r   .r   :	localhost)splitlenr   tuple)r   partss     r   r   r   <   sV    OOC#E
5zQ5858 3aK8O 9$$<r   c                 d    [        U [        5      (       d  [        U 5      n U R                  U5      $ N)
isinstance
AuthConfigget_credential_store)
authconfigr"   s     r   r2   r2   F   s*    j*--
+
**844r   c                       \ rS rSrSS jr\SS j5       r\SS j5       r\S 5       r	\S 5       r
\S 5       r\S	 5       rSS
 jrS rS rS rS rS rSrg)r1   L   Nc                 V    SU;  a  0 US'   U R                  U5        X l        0 U l        g Nauths)update_credstore_env_stores)selfdctr   s      r   __init__AuthConfig.__init__M   s,    #CLC+r   c           	         0 nUR                  5        GH3  u  pE[        U[        5      (       dT  [        R	                  SR                  U5      5        U(       a%  [        R                  " SR                  U5      5      e0 s  $ SU;   a/  [        R	                  SR                  U5      5        SUS   0X4'   M  SU;  a*  [        R	                  SR                  U5      5        0 X4'   M  [        US   5      u  pg[        R	                  SR                  [        U5      [        U5      5      5        UUUR                  S	5      US
.X4'   GM6     U$ )
Parses authentication entries

Args:
  entries:        Dict of authentication entries.
  raise_on_error: If set to true, an invalid format will raise
                  InvalidConfigFile

Returns:
  Authentication registry.
z*Config entry for key {} is not auth configz%Invalid configuration for registry {}identitytokenz,Found an IdentityToken entry for registry {}IdentityTokenauthzNAuth data for {} is absent. Client might be using a credentials store instead.z&Found entry (registry={}, username={})emailusernamepasswordrE   serveraddress)itemsr0   dictr   r   r   r   InvalidConfigFiledecode_authreprget)clsentriesraise_on_errorconfr"   entryrG   rH   s           r   
parse_authAuthConfig.parse_authT   sB    &}}OHeT**		@GG  " 22?FF$ 
 	%'		BII  $U?%;" U" 		1171A "$!,U6]!;HII8XX7 %$7+!)	DN]  /h r   c                    U(       dQ  [         R                  " U5      nU(       d	  U " 0 U5      $  [        U5       n[        R                  " U5      nSSS5        0 nUR                  S5      (       aE  [        R                  S5        UR                  SU R                  UR                  S5      SS905        UR                  S5      (       a7  [        R                  S5        UR                  SUR                  S5      05        UR                  S5      (       a7  [        R                  S	5        UR                  SUR                  S5      05        U(       a  U " Xs5      $ [        R                  S
5        U " SU R                  U5      0U5      $ ! , (       d  f       GND= f! [
        [        [        4 a1  n[        R                  U5        U " [        U5      U5      s SnA$ SnAff = f)a  
Loads authentication data from a Docker configuration file in the given
root directory or if config_path is passed use given path.
Lookup priority:
    explicit config_path parameter > DOCKER_CONFIG environment
    variable > ~/.docker/config.json > ~/.dockercfg
Nr8   zFound 'auths' sectionT)rR   
credsStorezFound 'credsStore' sectioncredHelperszFound 'credHelpers' sectionzNCouldn't find auth-related section ; attempting to interpret as auth-only file)r   find_config_fileopenjsonloadOSErrorKeyError
ValueErrorr   r   _load_legacy_configrO   r9   rU   pop)rP   config_pathconfig_dictr   config_fileferess           r   r   AuthConfig.load_config   s     11+>K2}--L+&!"&))A,K ' ??7##II-.JJOOG,T (  
 ??<((II23JJkool&CDE??=))II34JJ{}'EFGs**		 	
 GS^^K89=II= '&Xz2 L 		!.{;]KKLs:   F  FF  
FF  F   G&5&G!G&!G&c                 &    U R                  S0 5      $ r7   rO   r<   s    r   r8   AuthConfig.auths   s    xx$$r   c                 &    U R                  SS 5      $ )NrX   rk   rl   s    r   creds_storeAuthConfig.creds_store   s    xxd++r   c                 &    U R                  S0 5      $ )NrY   rk   rl   s    r   cred_helpersAuthConfig.cred_helpers   s    xxr**r   c                     U R                   (       + =(       a)    U R                  (       + =(       a    U R                  (       + $ r/   )r8   ro   rr   rl   s    r   r   AuthConfig.is_empty   s.     

NM4#3#33MD<M<M8M	
r   c                    U R                   (       d  U R                  (       aX  U R                  U5      nUbD  [        R	                  SU S35        U R                  X5      nUb  U$ [        R	                  S5        U(       a  [        U5      O[        n[        R	                  S[        U5       35        XR                  ;   a0  [        R	                  S[        U5       35        U R                  U   $ U R                  R                  5        H9  u  pE[        U5      U:X  d  M  [        R	                  S[        U5       35        Us  $    [        R	                  S5        g)a  
Returns the authentication data from the given auth configuration for a
specific registry. As with the Docker client, legacy entries in the
config with full URLs are stripped down to hostnames before checking
for a match. Returns None if no match was found.
NzUsing credentials store ""z/No entry in credstore - fetching from auth dictLooking for auth entry for zFound No entry found)ro   rr   r2   r   r   _resolve_authconfig_credstorer   r   rN   r8   rJ   )r<   r"   
store_namecfgkeyrS   s         r   r   AuthConfig.resolve_authconfig   s    t00228<J%		/
|1= 88N?J		KL 4<%h/		/X/?@Azz!IItH~./0::h''))+IC!#&(2		F49+./ ,
 			"#r   c                    U(       a
  U[         :X  a  [        n[        R                  S[	        U5       35        U R                  U5      n UR                  U5      nSU0nUS   [        :X  a
  US   US'   U$ UR                  US   US   S.5        U$ ! [        R                   a    [        R                  S5         g [        R                   a'  n[        R                  " S[	        U5       35      eS nAff = f)	Nrx   ServerAddressUsernameSecretrC   )r   Passwordry   zCredentials store error: )r   	INDEX_URLr   r   rN   _get_store_instancerO   TOKEN_USERNAMEr9   r   CredentialsNotFound
StoreErrorr   DockerException)r<   r"   credstore_namestoredatarh   rg   s          r   rz   (AuthConfig._resolve_authconfig_credstore   s    8z1 !H		/X/?@A((8	99X&DC J>1'+H~O$ J	 

 $Z 0 $X  J.. 	II&'%% 	((+DG95 	s$   +B 7B )C8>C8"C33C8c                     XR                   ;  a+  [        R                  " XR                  S9U R                   U'   U R                   U   $ )N)environment)r;   r   Storer:   )r<   names     r   r   AuthConfig._get_store_instance  s?    ||#!,!2!2"5"5"DLL ||D!!r   c                     U(       a
  U[         :X  a  [        nU R                  R                  U5      =(       d    U R                  $ r/   )r   r   rr   rO   ro   )r<   r"   s     r   r2   AuthConfig.get_credential_store!  s3    8z1 H  $$X.B$2B2BBr   c                    U R                   R                  5       nU R                  (       am  U R                  U R                  5      nUR	                  5       R                  5        H0  nU R                  X0R                  5      X'   X   U[        U5      '   M2     U R                  R                  5        H(  u  pEU R                  XE5      X'   X   U[        U5      '   M*     U$ r/   )
r8   copyro   r   listkeysrz   r   rr   rJ   )r<   	auth_datar   kregr{   s         r   get_all_credentialsAuthConfig.get_all_credentials'  s    JJOO%	,,T-=-=>EZZ\&&(#AA'' 	 5>L	-a01	 )  $00668OC!??IN 3<.I)#./	  9 r   c                     X S   U'   g r7    )r<   r   r   s      r   add_authAuthConfig.add_auth;  s    !Wcr   )r:   r;   r/   F)__name__
__module____qualname____firstlineno__r>   classmethodrU   r   propertyr8   ro   rr   r   r   rz   r   r2   r   r   __static_attributes__r   r   r   r1   r1   L   s     B BH -J -J^ % % , , + + 
 

!F8"C("r   r1   c                 d    [        U [        5      (       d  [        X5      n U R                  U5      $ r/   )r0   r1   r   )r3   r"   r   s      r   r   r   ?  s*    j*--
:
((22r   c                 l    U R                  SS5      R                  SS5      R                  SS5      S   $ )Nzhttp:// zhttps://r&   r   r   )replacer*   )urls    r   r   r   E  s2    ;;y"%--j"=CCCKANNr   c                     [        U [        5      (       a  U R                  S5      n [        R                  " U 5      nUR                  SS5      u  p#UR                  S5      UR                  S5      4$ )Nascii   :r   utf8)r0   strencodebase64	b64decoder*   decode)rD   sloginpwds       r   rM   rM   I  s\    ${{7#Aq!JE<<F!333r   c                 x    [         R                  " U 5      R                  S5      n[        R                  " U5      $ )Nr   )r\   dumpsr   r   urlsafe_b64encode)rD   	auth_jsons     r   r    r    Q  s,    

4 ''0I##I..r   c                 ,    [         R                  X5      $ )rA   )r1   rU   )rQ   rR   s     r   rU   rU   V  s       99r   c                 .    [         R                  XU5      $ r/   )r1   r   )rc   rd   r   s      r   r   r   f  s    !!+MJJr   c                 *   [         R                  S5         / n[        U 5       nUR                  5        H4  nUR	                  UR                  5       R                  S5      S   5        M6     [        U5      S:  a  [        R                  " S5      e S S S 5        [        US   5      u  pES[        UUUS   [        S.00$ ! , (       d  f       N4= f! [         a  n[         R                  U5         S nAOS nAff = f[         R                  S	5        0 $ )
Nz+Attempting to parse legacy auth file formatz = r      z$Invalid or empty configuration file!r   r8   rF   z4All parsing attempts failed - returning empty config)r   r   r[   	readlinesappendstripr*   r+   r   rL   rM   r   r   	Exception)re   r   rf   linerG   rH   rg   s          r   ra   ra   j  s    II;<+!DJJL..u5a89 &4y1}..:    )a1$$a!*	
  	 $  		! IIDEIs/   C A.C-C 
CC 
C;C66C;)NNr   )NNN)r   r\   loggingr   r   r   utilsr   r   r   r   	getLoggerr   r   r   r   r$   r   r2   rK   r1   r   r   rM   r    rU   r   ra   r   r   r   <module>r      s         
ZL-	!7,5p" p"f3O4/
: Kr   