
    5dyH                     H   d 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
 ddlmZ dd	lmZ dd
lmZmZmZmZmZmZ ddZd ZdZdZdZdZdZdZdZdZdZ ee z   ez   ez   Z!eez   ez   Z"eez   Z#e$dk    r.ddl%Z% ee%j&        dd                   Z' e%j(        e'           dS dS )z
This module provides the CLI interface for conda-content-trust.
This is intended to provide a command-line signing and metadata update
interface.
    )ArgumentParser)deepcopy)dumps   )__version__)authentication)root_signing)signing)	CCT_Error
PrivateKeyis_gpg_fingerprint
is_hex_keyload_metadata_from_filewrite_metadata_to_fileNc                    t          dd          }|                    dddddt          z  	           |                    d
d          }|                    dd          }|                    dd           |                    dd           |                    dd          }|                    dd           |                    dd           |                    dd          }|                    dd           d}t
          j        sd}|                    d |d!z             }|                    d"d#           |                    d$|d%z             }|                    d"d#           |                    d&d'           |                    |           } | j        d$k    rZd	                    | j
                                                                                  }	t          j        | j        |	           d S | j        dk    rt          | j                  5 }
|
                                                                                                }d d d            n# 1 swxY w Y   t'          |          st)          d(           d S t+          j        | j        | j                   d S | j        d k    rsd	                    | j
                                                                                  }	t          j        |	          }t)          d)t5          |          z              d S | j        dk    r%t7          | j                  }t;          |           d S | j        dk    rt7          | j                  }t7          | j                  }|d*         d+         }|d,k    rO	 tA          j!        ||           t)          d-           d.S # tD          $ r}d/}t5          |          }Y d }~nYd }~ww xY w	 tA          j#        |||0           t)          d1           d.S # tD          $ r}d2}t5          |          }Y d }~nd }~ww xY wt)          d3|z   d4z   |z   d5z              |S |$                                 d S )6Nz(Signing and verification tools for Condaresolve)descriptionconflict_handlerz-Vz	--versionversionz5Show the conda-content-trust version number and exit.zconda-content-trust %s)actionhelpr   subcommandssubcommand_name)titledestzsign-artifactszGiven a repodata.json file, produce signatures over the metadata for each artifact listed, and update the repodata.json file with their individual signatures.)r   repodata_fnamez^the filename of a repodata.json file from which to retrieve metadata for individual artifacts.private_key_fnamezthe filename of a file containing a hex string representation of an ed25519 private key to be used to sign each artifact's metadatazverify-metadataa]  Uses the first (trusted) metadata file to verify the second (not yet trusted) metadata file.  For example, "conda-content-trust verify-metadata 4.root.json 5.root.json" to verify version 5 of root based on version 4 of root, or "conda-content-trust verify-metadata 4.root.json key_mgr.json" to verify key manager metadata based on version 4 of root.trusted_metadata_filenamezothe filename of the already-trusted metadata file that sets the rules for verifying the untrusted metadata fileuntrusted_metadata_filenamez7the filename of the (untrusted) metadata file to verifyzmodify-metadataa  Interactive metadata modification.  Use this to produce a new version of a metadata file (like root.json or key_mgr.json), or correct an error in an unpublished metadata file, or review and sign a metadata file.  This increments version number / timestamp, reports changes on console, etc. For example, "conda-content-trust modify-metadata 8.root.json" for assistance in producing a new version of root (version 9) using version 8.metadata_filenamez4the filename of the existing metadata file to modify zJ[Unavailable]: Requires optional dependencies: securesystemslib and gpg.  zgpg-key-lookupz~Given the OpenPGP fingerprint of an ed25519-type OpenPGP key, fetch the actual ed25519 public key value of the underlying key.gpg_key_fingerprintzthe 40-hex-character key fingerprint (long keyid) for the OpenPGP/GPG key that you want to sign something with.  Do not add prefix "0x".zgpg-signzSign a given piece of metadata using GPG instead of the usual signing mechanisms.  Takes an OpenPGP key fingerprint and a filename.filenamez,the filename of the file that will be signedzhABORTED.  Expected key file to contain only a hex string representation of an ed25519 key.  It does not.z%Underlying ed25519 public key value: signedtyperootz&Root metadata verification successful.r   
   )delegation_nameuntrusted_delegated_metadatatrusted_delegating_metadataz!Metadata verification successful.   z?Verification of untrusted metadata failed.  Metadata type was "z".  Error reads:
  "")%r   add_argumentr   add_subparsers
add_parsercct_root_signingSSLIB_AVAILABLE
parse_argsr   joinr"   splitlowersign_root_metadata_via_gpgr#   openr   readstripr   printcct_signingsign_all_in_repodatar   private_key_hexfetch_keyval_from_gpgstrr   r    interactive_modify_metadatar   r   cct_authenticationverify_rootr   verify_delegation
print_help)argsparsersp
p_signrepo
p_verifymd
p_modifymdopt_reqs_strp_gpglookup	p_gpgsignr"   key_fobjr=   keyvalold_metadatauntrusted_metadatatrusted_metadatametadata_typee	errorcodeerrorstrings                       7lib/python3.11/site-packages/conda_content_trust/cli.pyclirX      s    >"  F D(;6     
		]9J		K	KB R   J C     /     I   J #*     %J     K   J G     L+ 
8 	 --II    K       L L   I      H     T""Dz)) !ggd&>&D&D&F&FGGMMOO3DMCVWWWWW		!1	1	1$()) 	>X 'mmoo3355;;==O	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	> 	>
 /** 	B   F()<d>RSSSSS		!1	1	1 ggd&>&D&D&F&FGGMMOO!78KLL5FCDDDDD		!2	2	2& /t/EFF 	$L11111		!2	2	2* 5T5UVV243QRR +84V<F""%"./?ASTTT>???q % % %	!!ff%%"4$11C0@   
 9:::q % % %	!!ff%
 	()+BCEPQSVW	
 	
 	
  	sB   %9I**I.1I.,$O 
O7O22O7;&P# #
Q-QQc                      }t          |           	 ddl}ddl}ddl}n'# t          $ r t          d           d}ddlm} Y nw xY wd  fd}d } fd}d	 }d
 }d }	d }
 fd}d }d }|dg|dg|dg|dg|dg|	dg|
dg|dg|dg|dgd
}t          dz   t          z   }|D ]D}|dt          z   t          |          z   t          z   dz   ||         d         z   t          z   dz   z  }Ed }|st          t          t          z   d!z   t          z              |xt           d"d#$          }t          |                    |                    d%          |j                                        |j                                                             n |            t          |           t)          t          d&z   t          z             }	 t+          |          }n/#  t          t,          t          z   d'z   t          z              Y xY w||vr)t          t,          t          z   d'z   t          z              Jt          t          d(z   ||         d         z   d)z   t          z               ||         d                     }|dS dS )* r   Na"  interactive modify-metadata mode employs pygments for syntax highlighting, if pygments is available.  pygments was not found, so the JSON contents will be... uglier than they would otherwise be.  If you would like syntax highlighting and prettier printing of JSON, you may install pygments.)pprintc                 L    t          t          dz   | z   t          z   dz             S )Nz
----- Please provide : )input
F_INSTRUCTENDC)ss    rW   	promptforz.interactive_modify_metadata.<locals>.promptforN  s%    Z";;a?$FMNNN    c                  z     d          } t          d           t          |            t          d           dS )Nz#a filename to save this metadata aszWriting to file....zModified metadata written!r   )r:   r   )fnamemetadatarb   s    rW   fn_writez-interactive_modify_metadata.<locals>.fn_writeQ  sF    	?@@#$$$x///*+++qrc   c                  T    t          t          t          z   dz   t          z              dS )Nz
Aborting!
r   )r:   REDBOLDr`    rc   rW   fn_abortz-interactive_modify_metadata.<locals>.fn_abortX  s#    cDj?*T1222qrc   c                     t           j        s*t          t          dz   t          z   dz   t
          z               d          } d                    |                                                                           } t          |           rIt          j        |           }t          j        |           t          t          dz   t
          z              nt          |           rj	 t          j        |            t          t          dz   t
          z              n[#  t          t          dz   t          z   dz   t          z   dz              Y n*xY wt          t          t          z   d	z   t
          z              d
S )Nz
Signing.  zTPlease ABORT (control-c) if the metadata above is not EXACTLY what you want to sign!a	  a key: either:
     - a 40-character-hex-string GPG PUBLIC key fingerprint
       for GPG keys (e.g. root YubiKeys), or 
     - a 64-character-hex-string PRIVATE key value for normal keys.

     Whitespace will be removed and characters will be lowercased.
     Keyr!   z(

--- Successfully signed!  Please save.

--- zSigning FAILED.z4  Do you have this key loaded in GPG on this system?z+Unable to recognize key.  Please try again.r   )r0   r1   r:   F_OPTSri   r`   r3   r4   r5   r   r   from_hexr;   sign_signabler   sign_root_metadata_dict_via_gpg)keyprivate_keyrf   rb   s     rW   	fn_addsigz.interactive_modify_metadata.<locals>.fn_addsig]  s   / 	%+ /K KMQR   i'
 
 ggciikk""((**c?? 	W$-c22K%h<<<&GG$NOOOO$$ 	WT @3OOO fKKdRSSSS	 ! (( 	
##     &3,!NNQUUVVVqs   $D /E
c                      dS Nr   rk   rk   rc   rW   	fn_remsigz.interactive_modify_metadata.<locals>.fn_remsig      qrc   c                      dS rw   rk   rk   rc   rW   	fn_updatez.interactive_modify_metadata.<locals>.fn_update  ry   rc   c                      dS rw   rk   rk   rc   rW   	fn_adddelz.interactive_modify_metadata.<locals>.fn_adddel  ry   rc   c                      dS rw   rk   rk   rc   rW   	fn_remdelz.interactive_modify_metadata.<locals>.fn_remdel  ry   rc   c                      d          } | d         d         vr,t          t          dz   t          z   dz   t          z              dS  dt	          d         d         |          d                   z             }	 t          |          }|d	k    sJ n2#  t          t          d
z   t          z   dz   t          z              Y dS xY w|d         d         |          d<   t          t          dz   t          z              dS )Nza delegation name (one of the entries in the
     "delegations" dictionary in the metadata above).  This will
     be the delegation whose threshold number of required keys we
     will change.r$   delegationsrn   z2Unable to find that delegation.  Please try again.r   z-a new threshold value.  The current value is 	thresholdr   z
--- zPInvalid value.  Expecting integer greater than or equal to 1.  Please try again.z$
--- Threshold successfully updated.)r:   ro   ri   r`   r?   int)
delegation
new_threshrf   rb   s     rW   	fn_threshz.interactive_modify_metadata.<locals>.fn_thresh  s=   Y2
 

 Xh/>>>#c) -& &(,-   1Y;(8$]3J?LMMN
 


	ZJ?????	!C' +A ACGH   11EO=)*5kBf>>EFFFqs   =B ,Cc                      dS rw   rk   rk   rc   rW   	fn_addkeyz.interactive_modify_metadata.<locals>.fn_addkey  ry   rc   c                      dS rw   rk   rk   rc   rW   	fn_remkeyz.interactive_modify_metadata.<locals>.fn_remkey  ry   rc   zDone: write and save metadataz/Abort: discard changes -- abort without writingz*Add a signature (sign with a key you have)zRemove a signaturez%Update any top-level dictionary entryzAdd a delegationzRemove a delegationz4Change the threshold number of keys for a delegationz%Add an authorized key to a delegationz*Remove an authorized key from a delegation)
r   r                        	   z7
--- Please choose an operation by entering its number
z    r]   r   
FzD

---------------------
--- Current metadata:
---------------------
Tr   )	sort_keysindentzutf-8zChoice: z
Invalid entry.  Try again.
z
Chose "r,   )r   pygmentspygments.formatterspygments.lexersImportErrorr:   r[   r_   r`   F_LABELr?   ro   rj   r   	highlightencodelexers	JsonLexer
formattersTerminalFormatterr^   r   ri   )rf   initial_metadatar   r[   rg   rl   ru   rx   r{   r}   r   r   r   r   optionsoption_textindexdoneformatted_metadataselectedrb   s   `                   @rW   r@   r@   "  s   2  ())H""""" 	" 	" 	"G	
 	
 	
 !!!!!!!!	"O O O       
' ' ' ' ' 'R        ! ! ! ! ! !F     56GHCD+,>?)*,-MN>?CD G 	  		   

 

%jj  	
 enQ   		
 D !&YZ 	
 	
 	
 !&x4!J!J!J""&--g66O--//'99;;     F8k*,t344	8}}HH	#*??$FGGGH7""#*??$FGGGf{"WX%6q%99C?$FGGG#wx #%%C  !& !& !& !& !&s   " !AAG )H	z[95mz[94mz[96mz[92mz[93mz[91mz[0mz[1mz[4m__main__)N))__doc__argparser   copyr   jsonr   r!   r   r   rA   r	   r0   r
   r;   commonr   r   r   r   r   r   rX   r@   PINKBLUECYANGREENYELLOWri   r`   rj   	UNDERLINEr   r_   ro   __name__sysargvexit_statusexitrk   rc   rW   <module>r      s   
 $ # # # # #                   2 2 2 2 2 2 . . . . . . $ $ $ $ $ $               E E E EPX& X& X&@ 		 
T
!D
(D[4
	 zJJJ#chqrrl##KCH[	 rc   