def testSnapshotCsekKeyFileRsaWrappedKey(self):
        disk_ref = self._GetDiskRef('disk-1')
        snapshot_ref = self._GetSnapshotRef('random-name-0')
        operation_ref = self._GetOperationRef('operation-1')

        csek_key_file = os.path.join(self.temp_path, 'key-file.json')
        key_store = utils.CsekKeyStore()
        key_store.AddKey(disk_ref.SelfLink(), key_type='rsa-encrypted')
        key_store.WriteToFile(csek_key_file)

        self.api_mock.batch_responder.ExpectBatch([
            (self._GetCreateSnapshotRequest(
                disk_ref,
                snapshot_ref,
                rsa_encryption_key=utils.SAMPLE_WRAPPED_CSEK_KEY),
             self._GetOperationMessage(operation_ref,
                                       self.status_enum.PENDING)),
        ])

        self.Run('compute disks snapshot {disk} '
                 '--csek-key-file={keyfile} --async'.format(
                     disk=disk_ref.SelfLink(), keyfile=csek_key_file))

        self.AssertOutputEquals('')
        self.AssertErrEquals(
            'Disk snapshot in progress for [{}].\n'
            'Use [gcloud compute operations describe URI] command to check '
            'the status of the operation(s).\n'.format(
                operation_ref.SelfLink()))
    def testSnapshotCsekStdin(self):
        disk_ref = self._GetDiskRef('disk-1')
        snapshot_ref = self._GetSnapshotRef('random-name-0')
        operation_ref = self._GetOperationRef('operation-1')

        customer_encryption_key = self.api_mock.messages.CustomerEncryptionKey(
            rawKey='abcdefghijklmnopqrstuvwxyz1234567890AAAAAAA=')

        self.api_mock.batch_responder.ExpectBatch([
            (self._GetCreateSnapshotRequest(
                disk_ref,
                snapshot_ref,
                raw_encryption_key=customer_encryption_key.rawKey),
             self._GetOperationMessage(operation_ref,
                                       self.status_enum.PENDING)),
        ])

        key_store = utils.CsekKeyStore()
        key_store.AddKey(disk_ref.SelfLink(), customer_encryption_key.rawKey)
        self.WriteInput(key_store.AsString())

        self.Run('compute disks snapshot {disk} --zone {zone} '
                 '--csek-key-file - --async'.format(
                     disk=disk_ref.Name(),
                     zone=disk_ref.zone,
                 ))

        self.AssertOutputEquals('')
        self.AssertErrEquals(
            'Disk snapshot in progress for [{}].\n'
            'Use [gcloud compute operations describe URI] command to check '
            'the status of the operation(s).\n'.format(
                operation_ref.SelfLink()))
    def testSnapshotCsekKeyFileRsaWrappedKey(self):
        disk_ref = self._GetDiskRef('disk-1')

        csek_key_file = os.path.join(self.temp_path, 'key-file.json')
        key_store = utils.CsekKeyStore()
        key_store.AddKey(disk_ref.SelfLink(), key_type='rsa-encrypted')
        key_store.WriteToFile(csek_key_file)

        with self.assertRaisesRegex(
                csek_utils.BadKeyTypeException,
                re.escape(
                    'Invalid key type [rsa-encrypted]: this feature is only allowed in the '
                    'alpha and beta versions of this command.')):
            self.Run('compute disks snapshot {disk} --zone {zone} '
                     '--csek-key-file={keyfile}'.format(disk=disk_ref.Name(),
                                                        zone=disk_ref.zone,
                                                        keyfile=csek_key_file))

        self.AssertOutputEquals('')