-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(storage): migrate encryption samples and tests #4268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
angelcaamal
wants to merge
5
commits into
GoogleCloudPlatform:main
Choose a base branch
from
angelcaamal:encryption-storage-migration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b52ec56
feat(storage): introduce encryption samples and tests for migration
angelcaamal b51cb15
fix(storage): address code review feedback for encryption samples
angelcaamal 9855ace
Merge branch 'main' into encryption-storage-migration
angelcaamal 1638e65
Merge branch 'main' into encryption-storage-migration
angelcaamal 942f2ce
Merge branch 'main' into encryption-storage-migration
angelcaamal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| // Copyright 2021 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| // sample-metadata: | ||
| // title: Storage File Convert CSEK to CMEK. | ||
| // description: Storage File Convert CSEK to CMEK. | ||
| // usage: node changeFileCSEKToCMEK.js <BUCKET_NAME> <FILE_NAME> <ENCRYPTION_KEY> <KMS_KEY_NAME> | ||
|
|
||
| function main( | ||
| bucketName = 'my-bucket', | ||
| fileName = 'test.txt', | ||
| encryptionKey = 'my-encription-key', | ||
| kmsKeyName = 'my-kms-key', | ||
| generationMatchPrecondition = 0 | ||
| ) { | ||
| // [START storage_object_csek_to_cmek] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The ID of your GCS file | ||
| // const fileName = 'your-file-name'; | ||
|
|
||
| // The Base64 encoded decryption key, which should be the same key originally | ||
| // used to encrypt the file | ||
| // const encryptionKey = 'TIbv/fjexq+VmtXzAlc63J4z5kFmWJ6NdAPQulQBT7g='; | ||
|
|
||
| // The name of the KMS key to manage this file with | ||
| // const kmsKeyName = 'projects/your-project-id/locations/global/keyRings/your-key-ring/cryptoKeys/your-key'; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function changeFileCSEKToCMEK() { | ||
| try { | ||
| const rotateEncryptionKeyOptions = { | ||
| kmsKeyName, | ||
| // Optional: set a generation-match precondition to avoid potential race | ||
| // conditions and data corruptions. The request to copy is aborted if the | ||
| // object's generation number does not match your precondition. | ||
| preconditionOpts: { | ||
| ifGenerationMatch: generationMatchPrecondition, | ||
| }, | ||
| }; | ||
|
|
||
| console.log(rotateEncryptionKeyOptions); | ||
|
|
||
| await storage | ||
| .bucket(bucketName) | ||
| .file(fileName, { | ||
| encryptionKey: Buffer.from(encryptionKey, 'base64'), | ||
| }) | ||
| .rotateEncryptionKey({ | ||
| rotateEncryptionKeyOptions, | ||
| }); | ||
|
angelcaamal marked this conversation as resolved.
Outdated
|
||
|
|
||
| console.log( | ||
| `file ${fileName} in bucket ${bucketName} is now managed by KMS key ${kmsKeyName} instead of customer-supplied encryption key` | ||
| ); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error executing change file CSEK to CMEK:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| changeFileCSEKToCMEK(); | ||
| // [END storage_object_csek_to_cmek] | ||
| } | ||
| main(...process.argv.slice(2)); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| // Copyright 2020 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on encrypted | ||
| * files with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the README.md under /storage and the documentation | ||
| * at https://cloud.google.com/storage/docs. | ||
| */ | ||
|
|
||
| const path = require('path'); | ||
|
|
||
| function main( | ||
| bucketName = 'my-bucket', | ||
| srcFileName = path.join(__dirname, '../resources', 'test.txt'), | ||
|
angelcaamal marked this conversation as resolved.
Outdated
|
||
| destFileName = 'test.txt', | ||
| encryptionKey = process.env.GOOGLE_CLOUD_KMS_KEY_US | ||
|
angelcaamal marked this conversation as resolved.
Outdated
|
||
| ) { | ||
| // [START storage_download_encrypted_file] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The ID of your GCS file | ||
| // const srcFileName = 'your-file-name'; | ||
|
|
||
| // The path to which the file should be downloaded | ||
| // const destFileName = '/local/path/to/file.txt'; | ||
|
|
||
| // The Base64 encoded decryption key, which should be the same key originally | ||
| // used to encrypt the file | ||
| // const encryptionKey = 'TIbv/fjexq+VmtXzAlc63J4z5kFmWJ6NdAPQulQBT7g='; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function downloadEncryptedFile() { | ||
| try { | ||
| const options = { | ||
| destination: destFileName, | ||
| }; | ||
|
|
||
| // Decrypts and downloads the file. This can only be done with the key used | ||
| // to encrypt and upload the file. | ||
| await storage | ||
| .bucket(bucketName) | ||
| .file(srcFileName) | ||
| .setEncryptionKey(Buffer.from(encryptionKey, 'base64')) | ||
| .download(options); | ||
|
|
||
| console.log(`File ${srcFileName} downloaded to ${destFileName}`); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error executing download encrypted file:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| downloadEncryptedFile(); | ||
| // [END storage_download_encrypted_file] | ||
| } | ||
| main(...process.argv.slice(2)); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| // Copyright 2020 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on encrypted | ||
| * files with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the README.md under /storage and the documentation | ||
| * at https://cloud.google.com/storage/docs. | ||
| */ | ||
|
|
||
| function main() { | ||
| // [START storage_generate_encryption_key] | ||
| const crypto = require('crypto'); | ||
|
|
||
| function generateEncryptionKey() { | ||
| try { | ||
| /** | ||
| * Generates a 256 bit (32 byte) AES encryption key and prints the base64 | ||
| * representation. | ||
| * | ||
| * This is included for demonstration purposes. You should generate your own | ||
| * key. Please remember that encryption keys should be handled with a | ||
| * comprehensive security policy. | ||
| */ | ||
| const buffer = crypto.randomBytes(32); | ||
| const encodedKey = buffer.toString('base64'); | ||
| console.log(`Base 64 encoded encryption key: ${encodedKey}`); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error executing generate encryption key:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
| generateEncryptionKey(); | ||
| // [END storage_generate_encryption_key] | ||
| } | ||
| main(...process.argv.slice(2)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| { | ||
| "name": "@google-cloud/storage-samples", | ||
| "description": "Samples for the Cloud Storage Client Library for Node.js.", | ||
| "license": "Apache-2.0", | ||
| "author": "Google Inc.", | ||
| "engines": { | ||
| "node": ">=12" | ||
| }, | ||
| "repository": "googleapis/nodejs-storage", | ||
| "private": true, | ||
| "files": [ | ||
| "*.js" | ||
| ], | ||
| "scripts": { | ||
| "cleanup": "node scripts/cleanup", | ||
| "test": "mocha system-test/*.js --timeout 800000" | ||
| }, | ||
| "dependencies": { | ||
| "@google-cloud/pubsub": "^4.0.0", | ||
| "@google-cloud/storage": "^7.19.0", | ||
| "node-fetch": "^2.6.7", | ||
| "uuid": "^8.0.0", | ||
| "yargs": "^16.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "chai": "^4.2.0", | ||
| "mocha": "^8.0.0", | ||
| "p-limit": "^3.1.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| downloaded.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Sub1 | ||
| Hello World! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Hello World! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Hello World 2! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Copyright 2020 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| 'use strict'; | ||
|
|
||
| /** | ||
| * This application demonstrates how to perform basic operations on encrypted | ||
| * files with the Google Cloud Storage API. | ||
| * | ||
| * For more information, see the README.md under /storage and the documentation | ||
| * at https://cloud.google.com/storage/docs. | ||
| */ | ||
|
|
||
| function main( | ||
| bucketName = 'my-bucket', | ||
| fileName = 'test.txt', | ||
| oldKey = process.env.GOOGLE_CLOUD_KMS_KEY_US, | ||
| newKey = process.env.GOOGLE_CLOUD_KMS_KEY_ASIA, | ||
|
angelcaamal marked this conversation as resolved.
Outdated
|
||
| generationMatchPrecondition = 0 | ||
| ) { | ||
| // [START storage_rotate_encryption_key] | ||
| /** | ||
| * TODO(developer): Uncomment the following lines before running the sample. | ||
| */ | ||
| // The ID of your GCS bucket | ||
| // const bucketName = 'your-unique-bucket-name'; | ||
|
|
||
| // The ID of your GCS file | ||
| // const fileName = 'your-file-name'; | ||
|
|
||
| // The Base64 encoded AES-256 encryption key originally used to encrypt the | ||
| // object. See the documentation on Customer-Supplied Encryption keys for | ||
| // more info: | ||
| // https://cloud.google.com/storage/docs/encryption/using-customer-supplied-keys | ||
| // The Base64 encoded AES-256 encryption key originally used to encrypt the | ||
|
angelcaamal marked this conversation as resolved.
Outdated
|
||
| // const oldKey = 'TIbv/fjexq+VmtXzAlc63J4z5kFmWJ6NdAPQulQBT7g='; | ||
|
|
||
| // The new encryption key to use | ||
| // const newKey = '0mMWhFvQOdS4AmxRpo8SJxXn5MjFhbz7DkKBUdUIef8='; | ||
|
|
||
| // Imports the Google Cloud client library | ||
| const {Storage} = require('@google-cloud/storage'); | ||
|
|
||
| // Creates a client | ||
| const storage = new Storage(); | ||
|
|
||
| async function rotateEncryptionKey() { | ||
| try { | ||
| const rotateEncryptionKeyOptions = { | ||
| encryptionKey: Buffer.from(newKey, 'base64'), | ||
|
|
||
| // Optional: set a generation-match precondition to avoid potential race | ||
| // conditions and data corruptions. The request to copy is aborted if the | ||
| // object's generation number does not match your precondition. | ||
| preconditionOpts: { | ||
| ifGenerationMatch: generationMatchPrecondition, | ||
| }, | ||
| }; | ||
| await storage | ||
| .bucket(bucketName) | ||
| .file(fileName, { | ||
| encryptionKey: Buffer.from(oldKey, 'base64'), | ||
| }) | ||
| .rotateEncryptionKey({ | ||
| rotateEncryptionKeyOptions, | ||
| }); | ||
|
angelcaamal marked this conversation as resolved.
Outdated
|
||
|
|
||
| console.log('Encryption key rotated successfully'); | ||
| } catch (error) { | ||
| console.error( | ||
| 'Error executing rotate encryption key:', | ||
| error.message || error | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| rotateEncryptionKey(); | ||
| // [END storage_rotate_encryption_key] | ||
| } | ||
| main(...process.argv.slice(2)); | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.