
    bi2                         d dl Z d dlZd dlZddlmZ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| d      t        |      |fS )	Nz://z)Repository name cannot contain a scheme ()r   -zInvalid index name (z%). Cannot begin or end with a hyphen.)r   InvalidRepositorysplit_repo_nameresolve_index_name)	repo_name
index_nameremote_names      F/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/docker/auth.pyresolve_repository_namer      s    	&&7	{!D
 	
 .i8J!}z"~4&&":, /1 1
 	
 j);66    c                 @    t        |       } | dt         k(  rt        } | S )Nzindex.)convert_to_hostname
INDEX_NAME)r   s    r   r   r      s&    $Z0Jvj\**
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   r1   r1   E   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)r0   Nc                 T    d|vri |d<   | j                  |       || _        i | _        y Nauths)update_credstore_env_stores)selfdctr   s      r   __init__zAuthConfig.__init__L   s.    #CLC+r   c                    i }|j                         D ]  \  }}t        |t              s7t        j	                  d| d       |rt        j                  d|       i c S d|v r#t        j	                  d|        d|d   i||<   td|vrt        j	                  d| d	       i ||<   t        |d         \  }}t        j	                  d
|d|d       |||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 z is not auth configz#Invalid configuration for registry identitytokenz*Found an IdentityToken entry for registry IdentityTokenauthzAuth data for z> is absent. Client might be using a credentials store instead.zFound entry (registry=z, username=r   emailusernamepasswordrA   serveraddress)	itemsr/   dictr   r   r   InvalidConfigFiledecode_authget)clsentriesraise_on_errorconfr!   entryrC   rD   s           r   
parse_authzAuthConfig.parse_authS   s7    &}} *	OHeeT*		+H:5HI " 22=hZH  	%'		FxjQR#U?%;"X U" 		$XJ /I J "$X!,U6]!;HhII(K|1M
 %$7+!)	DNK*	V 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
        Nr6   zFound 'auths' sectionT)rM   
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_configrJ   r7   rP   pop)rK   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 r5   rJ   r:   s    r   r6   zAuthConfig.auths   s    xx$$r   c                 &    | j                  dd       S )NrR   rd   re   s    r   creds_storezAuthConfig.creds_store   s    xxd++r   c                 &    | j                  di       S )NrS   rd   re   s    r   cred_helperszAuthConfig.cred_helpers   s    xxr**r   c                 X    | j                    xr | j                   xr | j                   S r.   )r6   rg   ri   re   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)rg   ri   r1   r   r   _resolve_authconfig_credstorer   r   reprr6   rF   )r:   r!   
store_namecfgkeyrN   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)	Nrm   ServerAddressUsernameSecretr?   )rv   Passwordrn   zCredentials store error: )r   	INDEX_URLr   r   rp   _get_store_instancerJ   TOKEN_USERNAMEr7   r   CredentialsNotFound
StoreErrorr   DockerException)r:   r!   credstore_namestoredatarb   ra   s          r   ro   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)r9   r   Storer8   )r:   names     r   rz   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   ry   ri   rJ   rg   )r:   r!   s     r   r1   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.   )
r6   copyrg   rz   listkeysro   r   ri   rF   )r:   	auth_datar   kregrq   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 r5    )r:   r   r   s      r   add_authzAuthConfig.add_auth1  s    !Wcr   r.   F)__name__
__module____qualname__r<   classmethodrP   r   propertyr6   rg   ri   r   r   ro   rz   r1   r   r   r   r   r   r0   r0   K   s     9 9v -J -J^ % % , , + + 
 

!F8"C("r   r0   c                 \    t        | t              st        | |      } | j                  |      S r.   )r/   r0   r   )r2   r!   r   s      r   r   r   5  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   ;  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)r/   strencodebase64	b64decoder)   decode)r@   sloginpwds       r   rI   rI   ?  s[    ${{7#Aq!JE3<<F!333r   c                 t    t        j                  |       j                  d      }t        j                  |      S )Nr   )rV   dumpsr   r   urlsafe_b64encode)r@   	auth_jsons     r   r   r   G  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.
    )r0   rP   )rL   rM   s     r   rP   rP   L  s       .99r   c                 0    t         j                  | ||      S r.   )r0   r   )r]   r^   r   s      r   r   r   \  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   r6   rB   z4All parsing attempts failed - returning empty config)r   r   rU   	readlinesappendstripr)   r*   r   rH   rI   r   ry   	Exception)r_   r   r`   linerC   rD   ra   s          r   r[   r[   `  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   rV   loggingr   r   r   utilsr   r   ry   r{   	getLoggerr   r   r   r   r#   r   r1   rG   r0   r   r   rI   r   rP   r   r[   r   r   r   <module>r      s       ! 
ZL-	g!7,5g" g"T3O4/
: Kr   