
    U                         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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  G d de      Zd Zd Zd Zd Zy)z=Helper functions for Split Trust Encryption Tool integration.    )absolute_import)print_function)division)unicode_literalsN)storage_url)execution_util)temporary_file_util)configc                       e Zd ZdZdZdZy)StetSubcommandNamez*Enum class for available STET subcommands.encryptdecryptN)__name__
__module____qualname____doc__ENCRYPTDECRYPT     (platform/gsutil/gslib/utils/stet_util.pyr   r       s    2''r   r   c                     t        j                  d      j                  t         j                  j                        D ]E  } t         j                  j                  | d      }t         j                  j                  |      sC|c S  y)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$   &   sS    		&)//@n'',,~v6K	ww~~k" Ar   c                    t        j                  ddt                     }|st        d      t        j
                  j                  |      | g}t        j                  ddd      }|r1|j                  dt        j
                  j                  |      z          |j                  d|z   ||g       t        j                  |      \  }}	|j                  |	       y)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_transformr9   .   s      

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

8%7>+(277+=+=k+JJK|g-|]KL33LA)!V,,vr   c                     | j                   }t        j                  |       }|j                  }t	        t
        j                  ||||       t        j                  |      S )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_stringr9   r   r   r   StorageUrlFromString)
source_urldestination_urlr4   in_fileout_filer1   s         r   encrypt_uploadrC   M   sU     ""' 44Z@(&&'$,,gw		)	)(	33r   c                     |}t        j                  |      }| j                  }t        t        j
                  ||||       t        j                  ||       y)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=   r9   r   r   shutilmove)r?   r@   temporary_file_namer4   rA   rB   r1   s          r   decrypt_downloadrH   a   sL      ' 44_E(!!'$,,gw++h r   )r   
__future__r   r   r   r   r   rE   gslibr   gslib.utilsr   r	   botor
   objectr   r$   r9   rC   rH   r   r   r   <module>rN      sI    D & %  ' 	   & +  >4(!r   