def test_decrypts_download_if_stet_is_enabled(self):
        object_uri = self.CreateObject(contents='abc')
        test_file = self.CreateTempFile()

        stderr = self.RunGsUtil([
            '-o', 'GSUtil:stet_binary_path={}'.format(self.stet_binary_path),
            '-o', 'GSUtil:stet_config_path={}'.format(
                self.stet_config_path), 'cp', '--stet',
            suri(object_uri), test_file
        ],
                                return_stderr=True)

        # Progress indicator should show transformed file size (variable based on
        # random string generation above). 4.0 B is the pre-transform size.
        self.assertNotIn('/4.0 B]', stderr)

        with open(test_file) as file_reader:
            downloaded_text = file_reader.read()
        self.assertIn('subcommand: decrypt', downloaded_text)
        self.assertIn(
            'config file: --config-file={}'.format(self.stet_config_path),
            downloaded_text)
        self.assertIn('blob id: --blob-id={}'.format(suri(object_uri)),
                      downloaded_text)
        self.assertIn('in file: {}'.format(test_file), downloaded_text)
        self.assertIn('out file: {}_.stet_tmp'.format(test_file),
                      downloaded_text)

        self.assertFalse(
            os.path.exists(
                temporary_file_util.GetStetTempFileName(
                    storage_url.StorageUrlFromString(test_file))))
def decrypt_download(source_url, destination_url, temporary_file_name, logger):
    """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.
  """
    in_file = temporary_file_name
    out_file = temporary_file_util.GetStetTempFileName(destination_url)
    blob_id = source_url.url_string

    _stet_transform(StetSubcommandName.DECRYPT, blob_id, in_file, out_file,
                    logger)
    shutil.move(out_file, in_file)
def encrypt_upload(source_url, destination_url, logger):
    """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.
  """
    in_file = source_url.object_name
    out_file = temporary_file_util.GetStetTempFileName(source_url)
    blob_id = destination_url.url_string

    _stet_transform(StetSubcommandName.ENCRYPT, blob_id, in_file, out_file,
                    logger)
    return storage_url.StorageUrlFromString(out_file)
 def testGetsStetTemporaryFileName(self):
     self.assertEqual(
         temporary_file_util.GetStetTempFileName(
             storage_url.StorageUrlFromString('file.txt')),
         'file.txt_.stet_tmp')