
    2.a3                         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Zde dZ	d	Z
 ej                  e      Zd
 Zd Zd Zd Zd Z G d de      ZddZd Zd Zd ZddZddZd Zy)    N   )credentials)errors)configz	docker.iozhttps://index.z/v1/z<token>c                     d| v rt        j                  d|  d      t        |       \  }}|d   dk(  s|d   dk(  r$t        j                  dj                  |            t	        |      |fS )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      -/usr/lib/python3/dist-packages/docker/auth.pyresolve_repository_namer      s    	&&7	{!D
 	
 .i8J!}z"~4&&vj)
 	
 j);66    c                 @    t        |       } | dt        z   k(  rt        } | S )Nzindex.)convert_to_hostname
INDEX_NAME)r   s    r   r   r      s$    $Z0JX
**
r   c                    t         j                  d       | j                  r| j                  j                  r0t         j                  d       t	        | j
                        | _        t        | j                  || j
                        }|r t         j                  d       t        |      S t         j                  d       y )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 hf6J6JG 		%& W%%II$%r   c                     | j                  dd      }t        |      dk(  sd|d   vrd|d   vr|d   dk7  rt        | fS t        |      S )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                 Z    t        | t              st        |       } | j                  |      S N)
isinstance
AuthConfigget_credential_store)
authconfigr"   s     r   r2   r2   F   s'    j*-
+
**844r   c                       e Zd ZddZedd       Zedd       Zed        Zed        Z	ed        Z
ed        Zdd	Zd
 Zd Zd Zd Zd Zy)r1   Nc                 T    d|vri |d<   | j                  |       || _        i | _        y Nauths)update_credstore_env_stores)selfdctr   s      r   __init__zAuthConfig.__init__M   s.    #CLC+r   c           	      ~   i }|j                         D ]&  \  }}t        |t              sNt        j	                  dj                  |             |r$t        j                  dj                  |            i c S d|v r/t        j	                  dj                  |             d|d   i||<   d|vr*t        j	                  dj                  |             i ||<   t        |d         \  }}t        j	                  dj                  t        |      t        |                   |||j                  d	      |d
||<   ) |S )a   
        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passwordrB   serveraddress)itemsr0   dictr   r   r   r   InvalidConfigFiledecode_authreprget)clsentriesraise_on_errorconfr"   entryrD   rE   s           r   
parse_authzAuthConfig.parse_authT   sN    &}} 3	OHeeT*		@GG  " 22?FF$ 
 	%'		BII  $U?%;"X U" 		1171A "$X!,U6]!;HhII8XX7 %$7+!)	DN]3	h r   c                    |sJt        j                  |      }|s	 | i |      S 	 t        |      5 }t        j                  |      }ddd       i }|j                  d      rHt        j                  d       |j                  d| j                  |j                  d      d      i       |j                  d      r7t        j                  d       |j                  d|j                  d      i       |j                  d      r7t        j                  d	       |j                  d|j                  d      i       |r	 | ||      S t        j                  d
        | d| j                  |      i|      S # 1 sw Y   /xY w# t
        t        t        f$ r1}t        j                  |        | t        |      |      cY d}~S d}~ww xY w)a8  
        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
        Nr7   zFound 'auths' sectionT)rO   
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_configrL   r8   rR   pop)rM   config_pathconfig_dictr   config_fileferess           r   r   z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sM**		 	
 GS^^K89=II=/ /Xz2 L 		!.{;]KKLs4   E? E2E? 2E<7E? ?G&F?9G?Gc                 &    | j                  di       S r6   rL   r;   s    r   r7   zAuthConfig.auths   s    xx$$r   c                 &    | j                  dd       S )NrT   rf   rg   s    r   creds_storezAuthConfig.creds_store   s    xxd++r   c                 &    | j                  di       S )NrU   rf   rg   s    r   cred_helperszAuthConfig.cred_helpers   s    xxr**r   c                 X    | j                    xr | j                   xr | j                   S r/   )r7   ri   rk   rg   s    r   r   zAuthConfig.is_empty   s.     

NM4#3#33MD<M<M8M	
r   c                    | j                   s| j                  rW| j                  |      }|Dt        j	                  d| d       | j                  ||      }||S t        j	                  d       |rt        |      nt        }t        j	                  dt        |              || j                  v r0t        j	                  dt        |              | j                  |   S | j                  j                         D ]8  \  }}t        |      |k(  st        j	                  dt        |              |c S  t        j	                  d       y)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)ri   rk   r2   r   r   _resolve_authconfig_credstorer   r   rK   r7   rG   )r;   r"   
store_namecfgkeyrP   s         r   r   zAuthConfig.resolve_authconfig   s&    t00228<J%		/
|1= 88:N?J		KL 4<%h/		/X/?@Atzz!IItH~./0::h''))+ 	IC!#&(2		F49+./	
 			"#r   c                    |r	|t         k(  rt        }t        j                  dt	        |              | j                  |      }	 |j                  |      }d|i}|d   t        k(  r
|d   |d<   |S |j                  |d   |d   d       |S # t        j                  $ r t        j                  d       Y y t        j                  $ r&}t        j                  dt	        |             d }~ww xY w)	Nro   ServerAddressUsernameSecretr@   )rw   Passwordrp   zCredentials store error: )r   	INDEX_URLr   r   rK   _get_store_instancerL   TOKEN_USERNAMEr8   r   CredentialsNotFound
StoreErrorr   DockerException)r;   r"   credstore_namestoredatard   rc   s          r   rq   z(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 0B (C.6C.!C))C.c                     || j                   vr.t        j                  || j                        | j                   |<   | j                   |   S )N)environment)r:   r   Storer9   )r;   names     r   r{   zAuthConfig._get_store_instance  sC    t||#!,!2!2$"5"5"DLL ||D!!r   c                 v    |r	|t         k(  rt        }| j                  j                  |      xs | j                  S r/   )r   rz   rk   rL   ri   )r;   r"   s     r   r2   zAuthConfig.get_credential_store!  s3    8z1 H  $$X.B$2B2BBr   c                    | j                   j                         }| j                  rn| j                  | j                        }|j	                         j                         D ]2  }| j                  || j                        ||<   ||   |t        |      <   4 | j                  j                         D ]+  \  }}| j                  ||      ||<   ||   |t        |      <   - |S r/   )
r7   copyri   r{   listkeysrq   r   rk   rG   )r;   	auth_datar   kregrr   s         r   get_all_credentialszAuthConfig.get_all_credentials'  s    JJOO%	,,T-=-=>EZZ\&&( A#AAt'' 	! 5>aL	-a01	A  $00668 	AOC!??ZIcN 3<C.I)#./		A r   c                     || d   |<   y r6    )r;   r   r   s      r   add_authzAuthConfig.add_auth;  s    !Wcr   r/   F)__name__
__module____qualname__r=   classmethodrR   r   propertyr7   ri   rk   r   r   rq   r{   r2   r   r   r   r   r   r1   r1   L   s     B BH -J -J^ % % , , + + 
 

!F8"C("r   r1   c                 \    t        | t              st        | |      } | j                  |      S r/   )r0   r1   r   )r3   r"   r   s      r   r   r   ?  s)    j*-
M:
((22r   c                 l    | j                  dd      j                  dd      j                  dd      d   S )Nzhttp:// zhttps://r&   r   r   )replacer*   )urls    r   r   r   E  s2    ;;y"%--j"=CCCKANNr   c                     t        | t              r| j                  d      } t        j                  |       }|j                  dd      \  }}|j                  d      |j                  d      fS )Nascii   :r   utf8)r0   strencodebase64	b64decoder*   decode)rA   sloginpwds       r   rJ   rJ   I  s[    ${{7#Aq!JE3<<F!333r   c                 t    t        j                  |       j                  d      }t        j                  |      S )Nr   )rX   dumpsr   r   urlsafe_b64encode)rA   	auth_jsons     r   r    r    Q  s,    

4 ''0I##I..r   c                 .    t         j                  | |      S )a   
    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.
    )r1   rR   )rN   rO   s     r   rR   rR   V  s       .99r   c                 0    t         j                  | ||      S r/   )r1   r   )r_   r`   r   s      r   r   r   f  s    !!+{MJJr   c                    t         j                  d       	 g }t        |       5 }|j                         D ]3  }|j	                  |j                         j                  d      d          5 t        |      dk  rt        j                  d      	 d d d        t        |d         \  }}dt        |||d   t        diiS # 1 sw Y   0xY w# t        $ r}t         j                  |       Y d }~nd }~ww xY wt         j                  d	       i S )
Nz+Attempting to parse legacy auth file formatz = r      z$Invalid or empty configuration file!r   r7   rC   z4All parsing attempts failed - returning empty config)r   r   rW   	readlinesappendstripr*   r+   r   rI   rJ   r   rz   	Exception)ra   r   rb   linerD   rE   rc   s          r   r]   r]   j  s   II;<+ 	! :DJJL..u5a89:4y1}..:  	 )a1($$a!*	
  		 	$  		! IIDEIs/   C
 A*B>.C
 >CC
 
	C2C--C2)NNr   )NNN)r   rX   loggingr   r   r   utilsr   r   rz   r|   	getLoggerr   r   r   r   r$   r   r2   rH   r1   r   r   rJ   r    rR   r   r]   r   r   r   <module>r      s         
ZL-	g!7,5p" p"f3O4/
: Kr   