Skip to content

Commit 043ad08

Browse files
authored
Fix encrypted file downloads and adds encryption key rotation (GoogleCloudPlatform#257)
* fixes encrypted file downloads in storage sample * adds encryption key rotation
1 parent 0b9108c commit 043ad08

File tree

8 files changed

+199
-63
lines changed

8 files changed

+199
-63
lines changed

storage/api/composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"require": {
3-
"google/cloud": "0.9",
3+
"google/cloud": "^0.13",
44
"paragonie/random_compat": "^2.0",
55
"symfony/console": " ^3.0"
66
},

storage/api/composer.lock

Lines changed: 51 additions & 48 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

storage/api/src/EncryptionCommand.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ protected function configure()
7070
InputOption::VALUE_REQUIRED,
7171
'Supply your encryption key'
7272
)
73+
->addOption(
74+
'rotate-key',
75+
null,
76+
InputOption::VALUE_REQUIRED,
77+
'Supply a new encryption key'
78+
)
7379
->addOption(
7480
'generate-key',
7581
null,
@@ -92,11 +98,16 @@ protected function execute(InputInterface $input, OutputInterface $output)
9298
upload_encrypted_object($bucketName, $objectName, $source, $encryptionKey);
9399
} elseif ($destination = $input->getOption('download-to')) {
94100
download_encrypted_object($bucketName, $objectName, $destination, $encryptionKey);
101+
} elseif ($rotateKey = $input->getOption('rotate-key')) {
102+
if (is_null($encryptionKey)) {
103+
throw new \Exception('--key is required when using --rotate-key');
104+
}
105+
rotate_encryption_key($bucketName, $objectName, $encryptionKey, $rotateKey);
95106
} else {
96-
throw new \Exception('supply either --upload-from or --download-to');
107+
throw new \Exception('Supply --rotate-key, --upload-from or --download-to');
97108
}
98109
} else {
99-
throw new \Exception('Supply a bucket, object and --key OR --generate-key');
110+
throw new \Exception('Supply a bucket and object OR --generate-key');
100111
}
101112
}
102113
}

storage/api/src/functions/download_encrypted_object.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@
3232
* @param string $bucketName the name of your Google Cloud bucket.
3333
* @param string $objectName the name of your Google Cloud object.
3434
* @param string $destination the local destination to save the encrypted file.
35-
* @param string $encryptionKey the encryption key.
35+
* @param string $base64EncryptionKey the base64 encoded encryption key.
3636
*
3737
* @return void
3838
*/
39-
function download_encrypted_object($bucketName, $objectName, $destination, $encryptionKey)
39+
function download_encrypted_object($bucketName, $objectName, $destination, $base64EncryptionKey)
4040
{
41+
$encryptionKey = base64_decode($base64EncryptionKey);
4142
$storage = new StorageClient();
4243
$bucket = $storage->bucket($bucketName);
4344
$object = $bucket->object($objectName);

storage/api/src/functions/generate_encryption_key.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
# [START generate_encryption_key]
2727

2828
/**
29-
* Generate an encryption key for Google Cloud Storage.
29+
* Generate a base64 encoded encryption key for Google Cloud Storage.
3030
*
3131
* @return void
3232
*/

storage/api/src/functions/rotate_encryption_key.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,38 @@
2323

2424
namespace Google\Cloud\Samples\Storage;
2525

26-
use Exception;
27-
2826
# [START rotate_encryption_key]
27+
use Google\Cloud\Storage\StorageClient;
2928

3029
/**
31-
* Rotate your encryption keys
30+
* Change the encryption key used to store an existing object.
3231
*
33-
* @param string $encryptionKey the encryption key to rotate.
32+
* @param string $bucketName the name of your Google Cloud bucket.
33+
* @param string $objectName the name of your Google Cloud object.
34+
* @param string $base64EncryptionKey the base64 encoded encryption key.
35+
* @param string $newBase64EncryptionKey the new base64 encoded encryption key.
3436
*
3537
* @return void
3638
*/
37-
function rotate_encryption_key($encryptionKey)
38-
{
39-
throw new Exception('This is currently not available using the Cloud Client Library.');
39+
function rotate_encryption_key(
40+
$bucketName,
41+
$objectName,
42+
$base64EncryptionKey,
43+
$newBase64EncryptionKey
44+
) {
45+
$encryptionKey = base64_decode($base64EncryptionKey);
46+
$newEncryptionKey = base64_decode($newBase64EncryptionKey);
47+
$storage = new StorageClient();
48+
$object = $storage->bucket($bucketName)->object($objectName);
49+
50+
$rewrittenObject = $object->rewrite($bucketName, [
51+
'encryptionKey' => $encryptionKey,
52+
'encryptionKeySHA256' => hash('SHA256', $encryptionKey, true),
53+
'destinationEncryptionKey' => $newEncryptionKey,
54+
'destinationEncryptionKeySHA256' => hash('SHA256', $newEncryptionKey, true),
55+
]);
56+
57+
printf('Rotated encryption key for object gs://%s/%s' . PHP_EOL,
58+
$bucketName, $objectName);
4059
}
4160
# [END rotate_encryption_key]

0 commit comments

Comments
 (0)