Example #1
0
    def RunCommand(self):
        """Command entry point for the hash command."""
        (calc_crc32c, calc_md5, format_func,
         output_format) = (self._ParseOpts(self.sub_opts, self.logger))

        matched_one = False
        for url_str in self.args:
            if not StorageUrlFromString(url_str).IsFileUrl():
                raise CommandException('"hash" command requires a file URL')

            for file_ref in self.WildcardIterator(url_str).IterObjects():
                matched_one = True
                file_name = file_ref.storage_url.object_name
                file_size = os.path.getsize(file_name)
                callback_processor = ProgressCallbackWithBackoff(
                    file_size,
                    FileProgressCallbackHandler(
                        ConstructAnnounceText('Hashing', file_name),
                        self.logger).call)
                hash_dict = self._GetHashClassesFromArgs(calc_crc32c, calc_md5)
                with open(file_name, 'rb') as fp:
                    CalculateHashesFromContents(
                        fp, hash_dict, callback_processor=callback_processor)
                print 'Hashes [%s] for %s:' % (output_format, file_name)
                for name, digest in hash_dict.iteritems():
                    print '\tHash (%s):\t\t%s' % (name, format_func(digest))

        if not matched_one:
            raise CommandException('No files matched')

        return 0
Example #2
0
    def CryptoRewrite(self, transform_url, gsutil_api):
        """Make the cloud object at transform_url match encryption configuration.

    Args:
      transform_url: CloudUrl to rewrite.
      gsutil_api: gsutil CloudApi instance for making API calls.
    """
        # Get all fields so that we can ensure that the target metadata is
        # specified correctly.
        src_metadata = gsutil_api.GetObjectMetadata(
            transform_url.bucket_name,
            transform_url.object_name,
            generation=transform_url.generation,
            provider=transform_url.scheme)

        if self.no_preserve_acl:
            # Leave ACL unchanged.
            src_metadata.acl = []
        elif not src_metadata.acl:
            raise CommandException(
                'No OWNER permission found for object %s. OWNER permission is '
                'required for rewriting objects, (otherwise their ACLs would be '
                'reset).' % transform_url)

        src_encryption_sha256 = None
        if (src_metadata.customerEncryption
                and src_metadata.customerEncryption.keySha256):
            src_encryption_sha256 = src_metadata.customerEncryption.keySha256

        if src_encryption_sha256 == self.current_encryption_sha256:
            if self.current_encryption_sha256 is not None:
                self.logger.info(
                    'Skipping %s, already has current encryption key' %
                    transform_url)
            else:
                self.logger.info('Skipping %s, already decrypted' %
                                 transform_url)
        else:
            # Make a deep copy of the source metadata
            dst_metadata = encoding.PyValueToMessage(
                apitools_messages.Object,
                encoding.MessageToPyValue(src_metadata))

            # Remove some unnecessary/invalid fields.
            dst_metadata.customerEncryption = None
            dst_metadata.generation = None
            # Service has problems if we supply an ID, but it is responsible for
            # generating one, so it is not necessary to include it here.
            dst_metadata.id = None
            decryption_tuple = None

            if src_encryption_sha256 is None:
                announce_text = 'Encrypting'
            else:
                decryption_key = FindMatchingCryptoKey(src_encryption_sha256)
                if not decryption_key:
                    raise EncryptionException(
                        'Missing decryption key with SHA256 hash %s. No decryption key '
                        'matches object %s' %
                        (src_encryption_sha256, transform_url))
                decryption_tuple = CryptoTupleFromKey(decryption_key)

                if self.current_encryption_sha256 is None:
                    announce_text = 'Decrypting'
                else:
                    announce_text = 'Rotating'

            progress_callback = FileProgressCallbackHandler(
                ConstructAnnounceText(announce_text, transform_url.url_string),
                gsutil_api.status_queue).call

            gsutil_api.CopyObject(
                src_metadata,
                dst_metadata,
                src_generation=transform_url.generation,
                preconditions=self.preconditions,
                progress_callback=progress_callback,
                decryption_tuple=decryption_tuple,
                encryption_tuple=self.current_encryption_tuple,
                provider=transform_url.scheme,
                fields=[])