
    U                         S r SSKJr  SSKJr  SSKJr  SSKJr  SSKrSSKrSSKJ	r	  SSK
Jr  SS	K
Jr  SS
KJr   " S S\5      rS rS rS rS rg)z=Helper functions for Split Trust Encryption Tool integration.    )absolute_import)print_function)division)unicode_literalsN)storage_url)execution_util)temporary_file_util)configc                        \ rS rSrSrSrSrSrg)StetSubcommandName    z*Enum class for available STET subcommands.encryptdecrypt N)__name__
__module____qualname____firstlineno____doc__ENCRYPTDECRYPT__static_attributes__r       (platform/gsutil/gslib/utils/stet_util.pyr   r       s    2''r   r   c                     [         R                  " S5      R                  [         R                  R                  5       HK  n [         R                  R                  U S5      n[         R                  R                  U5      (       d  MI  Us  $    g)zBRetrieves STET binary from path if available. Python 2 compatible.PATHstetN)osgetenvsplitpathpathsepjoinexists)path_directorybinary_paths     r   _get_stet_binary_from_pathr'   &   sV    		&)//@n'',,~v6K	ww~~k"" Ar   c                    [         R                  " SS[        5       5      nU(       d  [        S5      e[        R
                  R                  U5      U /n[         R                  " SSS5      nU(       a1  UR                  S[        R
                  R                  U5      -   5        UR                  SU-   X#/5        [        R                  " U5      u  pUR                  U	5        g)a  Runs a STET transform on a file.

Encrypts for uploads. Decrypts for downloads. Automatically populates
flags for the STET binary.

Args:
  subcommand (StetSubcommandName): Subcommand to call on STET binary.
  blob_id (str): Cloud URL that binary uses for validation.
  in_file_path (str): File to be transformed source.
  out_file_path (str): Where to write result of transform.
  logger (logging.Logger): For logging STET binary output.

Raises:
  KeyError: STET binary or config could not be found.
GSUtilstet_binary_pathz2Could not find STET binary in boto config or PATH.stet_config_pathNz--config-file=z
--blob-id=)r
   getr'   KeyErrorr   r!   
expanduserappendextendr   ExecuteExternalCommanddebug)

subcommandblob_idin_file_pathout_file_pathloggerr&   command_argsconfig_path_stderrs
             r   _stet_transformr<   .   s      

8%7579+	
G
HH''$$[1:>,

8%7>+(277+=+=k+JJK|g-|KL33LA)!,,vr   c                     U R                   n[        R                  " U 5      nUR                  n[	        [
        R                  XSUU5        [        R                  " U5      $ )a  Encrypts a file with STET binary before upload.

Args:
  source_url (StorageUrl): Copy source.
  destination_url (StorageUrl): Copy destination.
  logger (logging.Logger): For logging STET binary output.

Returns:
  stet_temporary_file_url (StorageUrl): Path to STET-encrypted file.
)	object_namer	   GetStetTempFileName
url_stringr<   r   r   r   StorageUrlFromString)
source_urldestination_urlr7   in_fileout_filer4   s         r   encrypt_uploadrF   M   sS     ""' 44Z@(&&'$,,g		)	)(	33r   c                     Un[         R                  " U5      nU R                  n[        [        R
                  XdUU5        [        R                  " XT5        g)a  STET-decrypts downloaded file.

Args:
  source_url (StorageUrl): Copy source.
  destination_url (StorageUrl): Copy destination.
  temporary_file_name (str): Path to temporary file used for download.
  logger (logging.Logger): For logging STET binary output.
N)r	   r?   r@   r<   r   r   shutilmove)rB   rC   temporary_file_namer7   rD   rE   r4   s          r   decrypt_downloadrK   a   sH      ' 44_E(!!'$,,g++h r   )r   
__future__r   r   r   r   r   rH   gslibr   gslib.utilsr   r	   botor
   objectr   r'   r<   rF   rK   r   r   r   <module>rQ      sI    D & %  ' 	   & +  >4(!r   