U
    Ø–]<4  ã                   @   sÚ   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Z	d 
e	¡ZdZe e¡Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ ZG dd„ deƒZd$dd„Zdd„ Zdd„ Zdd„ Zd%dd„Zd&d d!„Zd"d#„ ZdS )'é    Né   )Úcredentials)Úerrors)Úconfigz	docker.iozhttps://index.{0}/v1/z<token>c                 C   sX   d| krt  d | ¡¡‚t| ƒ\}}|d dks<|d dkrLt  d |¡¡‚t|ƒ|fS )Nz://z-Repository name cannot contain a scheme ({0})r   ú-éÿÿÿÿz<Invalid index name ({0}). Cannot begin or end with a hyphen.)r   ZInvalidRepositoryÚformatÚsplit_repo_nameÚresolve_index_name)Ú	repo_nameÚ
index_nameZremote_name© r   ú-/usr/lib/python3/dist-packages/docker/auth.pyÚresolve_repository_name   s    ÿÿÿr   c                 C   s   t | ƒ} | dt krt} | S )Nzindex.)Úconvert_to_hostnameÚ
INDEX_NAME)r   r   r   r   r
   !   s    r
   c                 C   sf   t  d¡ | jr| jjr0t  d¡ t| jd| _t| j|| jd}|rXt  d¡ t|ƒS t  d¡ d S )NzLooking for auth configz2No auth config in memory - loading from filesystem)Úcredstore_envzFound auth configzNo auth config found)ÚlogÚdebugZ_auth_configsÚis_emptyÚload_configr   Úresolve_authconfigÚencode_header)ZclientÚregistryZauthcfgr   r   r   Úget_config_header(   s     
ÿ  ÿ

r   c                 C   sL   |   dd¡}t|ƒdks<d|d krDd|d krD|d dkrDt| fS t|ƒS )Nú/r   Ú.r   ú:Z	localhost)ÚsplitÚlenr   Útuple)r   Úpartsr   r   r   r	   >   s    
ÿ
ÿ
ÿr	   c                 C   s   t | tƒst| ƒ} |  |¡S ©N)Ú
isinstanceÚ
AuthConfigÚget_credential_store)Ú
authconfigr   r   r   r   r%   H   s    
r%   c                   @   s”   e Zd Zddd„Zeddd„ƒZeddd„ƒZed	d
„ ƒZedd„ ƒZ	edd„ ƒZ
edd„ ƒZd dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )!r$   Nc                 C   s*   d|kri |d< |   |¡ || _i | _d S ©NÚauths)ÚupdateÚ_credstore_envÚ_stores)ÚselfZdctr   r   r   r   Ú__init__O   s
    
zAuthConfig.__init__Fc                 C   sà   i }t  |¡D ]Ì\}}t|tƒsLt d |¡¡ |rDt d |¡¡‚i   S d|krvt d |¡¡ d|d i||< qd|kr˜t d |¡¡ i ||< qt	|d ƒ\}}t d t
|ƒt
|ƒ¡¡ ||| d	¡|d
œ||< q|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 {0} is not auth configz&Invalid configuration for registry {0}Zidentitytokenz-Found an IdentityToken entry for registry {0}ÚIdentityTokenÚauthzOAuth data for {0} is absent. Client might be using a credentials store instead.z(Found entry (registry={0}, username={1})Úemail©ÚusernameÚpasswordr0   Zserveraddress)ÚsixÚ	iteritemsr#   Údictr   r   r   r   ÚInvalidConfigFileÚdecode_authÚreprÚget)ÚclsÚentriesÚraise_on_errorÚconfr   Úentryr2   r3   r   r   r   Ú
parse_authV   s\    
ÿÿ	ÿÿÿÿ ÿÿÿ ÿÿüzAuthConfig.parse_authc              
   C   s:  |s„t  |¡}|s| i |ƒS z"t|ƒ}t |¡}W 5 Q R X W nD tttfk
r‚ } z t 	|¡ | t
|ƒ|ƒ W Y ¢S d}~X Y nX i }| d¡rºt 	d¡ | d| j| d¡ddi¡ | d¡rât 	d¡ | d| d¡i¡ | d¡rt 	d	¡ | d| d¡i¡ |r| ||ƒS t 	d
¡ | d|  |¡i|ƒS )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
        Nr(   zFound 'auths' sectionT)r=   Ú
credsStorezFound 'credsStore' sectionÚcredHelperszFound 'credHelpers' sectionzNCouldn't find auth-related section ; attempting to interpret as auth-only file)r   Zfind_config_fileÚopenÚjsonÚloadÚIOErrorÚKeyErrorÚ
ValueErrorr   r   Ú_load_legacy_configr:   r)   r@   Úpop)r;   Úconfig_pathÚconfig_dictr   Úconfig_fileÚfÚeÚresr   r   r   r   ›   s@    




$

  ÿÿ



ÿzAuthConfig.load_configc                 C   s   |   di ¡S r'   ©r:   ©r,   r   r   r   r(   Ë   s    zAuthConfig.authsc                 C   s   |   dd ¡S )NrA   rQ   rR   r   r   r   Úcreds_storeÏ   s    zAuthConfig.creds_storec                 C   s   |   di ¡S )NrB   rQ   rR   r   r   r   Úcred_helpersÓ   s    zAuthConfig.cred_helpersc                 C   s   | j  o| j o| j S r"   )r(   rS   rT   rR   r   r   r   r   ×   s    ÿzAuthConfig.is_emptyc                 C   sè   | j s| jrP|  |¡}|dk	rPt d |¡¡ |  ||¡}|dk	rF|S t d¡ |r\t|ƒnt}t d t	|ƒ¡¡ || j
krœt d t	|ƒ¡¡ | j
| S t | j
¡D ]0\}}t|ƒ|kr¨t d t	|ƒ¡¡ |  S q¨t d¡ dS )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 "{0}"z/No entry in credstore - fetching from auth dictúLooking for auth entry for {0}z	Found {0}úNo entry found)rS   rT   r%   r   r   r   Ú_resolve_authconfig_credstorer
   r   r9   r(   r4   r5   )r,   r   Ú
store_nameZcfgÚkeyr>   r   r   r   r   Ý   s*    
ÿ




zAuthConfig.resolve_authconfigc              
   C   sÖ   |r|t krt}t d t|ƒ¡¡ |  |¡}zJ| |¡}d|i}|d tkr\|d |d< n| 	|d |d dœ¡ |W S  t
jk
rš   t d¡ Y d S  t
jk
rÐ } zt d t|ƒ¡¡‚W 5 d }~X Y nX d S )	NrU   ZServerAddressÚUsernameZSecretr.   )rZ   ZPasswordrV   zCredentials store error: {0})r   Ú	INDEX_URLr   r   r   r9   Ú_get_store_instancer:   ÚTOKEN_USERNAMEr)   r   ZCredentialsNotFoundZ
StoreErrorr   ZDockerException)r,   r   Zcredstore_nameÚstoreÚdatarP   rO   r   r   r   rW      s.    

 ÿþ
ÿz(AuthConfig._resolve_authconfig_credstorec                 C   s*   || j kr tj|| jd| j |< | j | S )N)Zenvironment)r+   r   ZStorer*   )r,   Únamer   r   r   r\     s    
 ÿzAuthConfig._get_store_instancec                 C   s"   |r|t krt}| j |¡p | jS r"   )r   r[   rT   r:   rS   )r,   r   r   r   r   r%   #  s    zAuthConfig.get_credential_storec                 C   sˆ   | j  ¡ }| jrP|  | j¡}| ¡  ¡ D ]&}|  || j¡||< || |t|ƒ< q(| j 	¡ D ](\}}|  ||¡||< || |t|ƒ< qZ|S r"   )
r(   ÚcopyrS   r\   ÚlistÚkeysrW   r   rT   Úitems)r,   Z	auth_datar^   ÚkÚregrX   r   r   r   Úget_all_credentials)  s     
 ÿ ÿzAuthConfig.get_all_credentialsc                 C   s   || d |< d S r'   r   )r,   rf   r_   r   r   r   Úadd_auth=  s    zAuthConfig.add_auth)N)F)N)N)Ú__name__Ú
__module__Ú__qualname__r-   Úclassmethodr@   r   Úpropertyr(   rS   rT   r   r   rW   r\   r%   rg   rh   r   r   r   r   r$   N   s&   
D/




#r$   c                 C   s   t | tƒst| |ƒ} |  |¡S r"   )r#   r$   r   )r&   r   r   r   r   r   r   A  s    

r   c                 C   s    |   dd¡  dd¡ dd¡d S )Nzhttp://Ú zhttps://r   r   r   )Úreplacer   )Zurlr   r   r   r   G  s    r   c                 C   sD   t | tjƒr|  d¡} t | ¡}| dd¡\}}| d¡| d¡fS )NÚasciió   :r   Úutf8)r#   r4   Zstring_typesÚencodeÚbase64Z	b64decoder   Údecode)r/   ÚsZloginÚpwdr   r   r   r8   K  s
    

r8   c                 C   s   t  | ¡ d¡}t |¡S )Nrp   )rD   Údumpsrs   rt   Zurlsafe_b64encode)r/   Z	auth_jsonr   r   r   r   S  s    r   Fc                 C   s   t  | |¡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.
    )r$   r@   )r<   r=   r   r   r   r@   X  s    r@   c                 C   s   t  | ||¡S r"   )r$   r   )rK   rL   r   r   r   r   r   h  s    r   c              
   C   sÆ   t  d¡ z€g }t| ƒB}| ¡ D ]}| | ¡  d¡d ¡ q"t|ƒdk rVt 	d¡‚W 5 Q R X t
|d ƒ\}}dt|||d tdœiiW S  tk
r¶ } zt  |¡ W 5 d }~X Y nX t  d	¡ i S )
Nz+Attempting to parse legacy auth file formatz = r   é   z$Invalid or empty configuration file!r   r(   r1   z4All parsing attempts failed - returning empty config)r   r   rC   Ú	readlinesÚappendÚstripr   r   r   r7   r8   r   r[   Ú	Exception)rM   r_   rN   Úliner2   r3   rO   r   r   r   rI   l  s0    

ÿüÿ

rI   )NN)F)NNN)rt   rD   Zloggingr4   rn   r   r   Zutilsr   r   r   r[   r]   Z	getLoggerri   r   r   r
   r   r	   r%   r6   r$   r   r   r8   r   r@   r   rI   r   r   r   r   Ú<module>   s0   


 t


