o
    .&ßa?  ã                   @   sÊ   d dl 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 e d¡Zdd	„ Zd
d„ ZG dd„ deƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    N)Ú	SigV4Auth)Ú
AWSRequest)Úurlsplit)ÚBasicCommand)ÚNonTranslatedStdoutzbotocore.credentialsc                 C   s   |   dt¡ dS )z3
    The entry point for the credential helper
    z!building-command-table.codecommitN)ÚregisterÚinject_commands)Úcli© r
   úB/usr/lib/python3/dist-packages/awscli/customizations/codecommit.pyÚ
initialize   ó   r   c                 K   s   t |ƒ| d< dS )z>
    Injects new commands into the codecommit subcommand.
    úcredential-helperN)ÚCodeCommitCommand)Úcommand_tableÚsessionÚkwargsr
   r
   r   r   %   r   r   c                   @   ó(   e Zd ZdZdZdZdZdZdd„ ZdS )	ÚCodeCommitNoOpStoreCommandÚstorezAThis operation does nothing, credentials are calculated each timez&aws codecommit credential-helper storeÚ Tc                 C   ó   dS ©Nr   r
   ©ÚselfÚargsÚparsed_globalsr
   r
   r   Ú	_run_main4   ó   z$CodeCommitNoOpStoreCommand._run_mainN©	Ú__name__Ú
__module__Ú__qualname__ÚNAMEÚDESCRIPTIONÚSYNOPSISÚEXAMPLESÚ_UNDOCUMENTEDr   r
   r
   r
   r   r   ,   ó    r   c                   @   r   )	ÚCodeCommitNoOpEraseCommandÚerasez;This operation does nothing, no credentials are ever storedz&aws codecommit credential-helper eraser   Tc                 C   r   r   r
   r   r
   r
   r   r   @   r   z$CodeCommitNoOpEraseCommand._run_mainNr   r
   r
   r
   r   r)   8   r(   r)   c                       sn   e Zd ZdZdZdZdZdddddd	œgZ‡ fd
d„Zdd„ Z	dd„ Z
dd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚCodeCommitGetCommandÚgetz¿get a username SigV4 credential pair based on protocol, host and path provided from standard in. This is primarily called by git to generate credentials to authenticate against AWS CodeCommitz$aws codecommit credential-helper getz…echo -e "protocol=https\\npath=/v1/repos/myrepo\\nhost=git-codecommit.us-east-1.amazonaws.com" | aws codecommit credential-helper getzignore-host-checkÚ
store_trueFzTOptional. Generate credentials regardless of whether the domain is an Amazon domain.)ÚnameÚactionÚdefaultÚ
group_nameÚ	help_textc                    s   t t| ƒ |¡ d S )N)Úsuperr+   Ú__init__)r   r   ©Ú	__class__r
   r   r4   ]   s   zCodeCommitGetCommand.__init__c                 C   sV   |   ¡ }d|d v sd|d v s|jr)|  |¡}|  ||¡}|  ||¡}|  |¡ dS )Nz
amazon.comÚhostzamazonaws.comr   )Úread_git_parametersÚignore_host_checkÚextract_urlÚextract_regionÚsign_requestÚwrite_git_parameters)r   r   r   Úgit_parametersÚtheUrlÚregionÚ	signaturer
   r
   r   r   `   s   ÿ

zCodeCommitGetCommand._run_mainc                 C   sœ   | j  ¡ j}| j  ¡ jd ur|d| j  ¡ j 7 }tƒ �)}| d |¡¡ t d|¡ | d |¡¡ | 	¡  t d|¡ W d   ƒ d S 1 sGw   Y  d S )Nú%zusername={0}
zusername
%szpassword={0}
zsignature
%s)
Ú_sessionÚget_credentialsÚ
access_keyÚtokenr   ÚwriteÚformatÚloggerÚdebugÚflush)r   rA   ÚusernameÚbinary_stdoutr
   r
   r   r=   k   s   "ùz)CodeCommitGetCommand.write_git_parametersc                 C   s8   i }t jD ]}| ¡ }|r| dd¡\}}|||< q|S )Nú=é   )ÚsysÚstdinÚstripÚsplit)r   ÚparsedÚlineÚkeyÚvaluer
   r
   r   r8   z   s   
€z(CodeCommitGetCommand.read_git_parametersc                 C   s   d  |d |d |d ¡}|S )Nz{0}://{1}/{2}Úprotocolr7   Úpath)rH   )r   Ú
parametersÚurlr
   r
   r   r:   ƒ   s
   
þz CodeCommitGetCommand.extract_urlc                 C   s>   t  d|d ¡}|d ur| d¡S |jd ur|jS | j d¡S )NzD(vpce-.+\.)?git-codecommit(-fips)?\.([^.]+)\.(vpce\.)?amazonaws\.comr7   é   r@   )ÚreÚmatchÚgroupr@   rC   Úget_config_variable)r   rZ   r   r^   r
   r
   r   r;   ‰   s   ÿ

z#CodeCommitGetCommand.extract_regionc                 C   sÆ   | j  ¡ }t|d|ƒ}tƒ }||_d|_tj ¡ }| d¡|j	d< t
|jƒ}|j d¡d }d |j|j|¡}	t d¡ t d	|	¡ | ||	¡}
t d
|
¡ | |
|¡}t d|¡ d |j	d |¡S )NÚ
codecommitÚGITz%Y%m%dT%H%M%SÚ	timestampú:r   z{0}
{1}

host:{2}

host
z$Calculating signature using v4 auth.zCanonicalRequest:
%szStringToSign:
%szSignature:
%sz{0}Z{1})rC   rD   r   r   r[   ÚmethodÚdatetimeÚutcnowÚstrftimeÚcontextr   ÚnetlocrS   rH   rY   rI   rJ   Ústring_to_signrA   )r   r@   Úurl_to_signÚcredentialsÚsignerÚrequestÚnowrS   ÚhostnameÚcanonical_requestrk   rA   r
   r
   r   r<   “   s*   


ý
z!CodeCommitGetCommand.sign_request)r    r!   r"   r#   r$   r%   r&   Ú	ARG_TABLEr4   r   r=   r8   r:   r;   r<   Ú__classcell__r
   r
   r5   r   r+   D   s&    úÿ	
r+   c                   @   s@   e Zd ZdZdZdZdedœdedœdedœgZ	dZ
d	d
„ ZdS )r   r   z aws codecommit credential-helperr   r,   )r.   Úcommand_classr   r*   a#  Provide a SigV4 compatible user name and password for git smart HTTP  These commands are consumed by git and should not used directly. Erase and Store are no-ops. Get is operation to generate credentials to authenticate AWS CodeCommit. Run "aws codecommit credential-helper help" for detailsc                 C   s   t dƒ‚)Nziusage: aws [options] codecommit credential-helper <subcommand> [parameters]
aws: error: too few arguments)Ú
ValueErrorr   r
   r
   r   r   ¾   s   zCodeCommitCommand._run_mainN)r    r!   r"   r#   r%   r&   r+   r   r)   ÚSUBCOMMANDSr$   r   r
   r
   r
   r   r   «   s    ý	r   )Úosr]   rP   ÚloggingÚ	fileinputrf   Úbotocore.authr   Úbotocore.awsrequestr   Úbotocore.compatr   Úawscli.customizations.commandsr   Úawscli.compatr   Ú	getLoggerrI   r   r   r   r)   r+   r   r
   r
   r
   r   Ú<module>   s$   
g