o
    !d?                     @   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
   Dusr/lib/python3.10/site-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)Z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EXAMPLESZ_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getzget 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 getzecho -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defaultZ
group_nameZ	help_textc                    s   t t| | d S )N)superr)   __init__)r   r   	__class__r
   r   r0   ]   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_parametersZignore_host_checkextract_urlextract_regionsign_requestwrite_git_parameters)r   r   r   Zgit_parametersZ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_credentialsZ
access_keytokenr   writeformatloggerdebugflush)r   r:   usernameZbinary_stdoutr
   r
   r   r8   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   r4   z   s   
z(CodeCommitGetCommand.read_git_parametersc                 C   s   d |d |d |d }|S )Nz{0}://{1}/{2}Zprotocolr3   path)r@   )r   
parametersurlr
   r
   r   r5      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\.comr3      r9   )rematchgroupr9   r<   Zget_config_variable)r   rP   r   rT   r
   r
   r   r6      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 )NZ
codecommitZ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})r<   r=   r   r   rQ   methoddatetimeutcnowstrftimecontextr   netlocrJ   r@   rO   rA   rB   string_to_signr:   )r   r9   Zurl_to_signZcredentialsZsignerZrequestnowrJ   hostnameZcanonical_requestr^   r:   r
   r
   r   r7      s*   



z!CodeCommitGetCommand.sign_request)r   r    r!   r"   r#   r$   r%   Z	ARG_TABLEr0   r   r8   r4   r5   r6   r7   __classcell__r
   r
   r1   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,   Z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'   ZSUBCOMMANDSr#   r   r
   r
   r
   r   r      s    	r   )osrS   rG   loggingZ	fileinputrY   Zbotocore.authr   Zbotocore.awsrequestr   Zbotocore.compatr   Zawscli.customizations.commandsr   Zawscli.compatr   	getLoggerrA   r   r   r   r'   r)   r   r
   r
   r
   r   <module>   s$   
g