|
| 1 | +// Copyright 2015-2016, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +// [START all] |
| 17 | +// [START setup] |
| 18 | +// By default, gcloud will authenticate using the service account file specified |
| 19 | +// by the GOOGLE_APPLICATION_CREDENTIALS environment variable and use the |
| 20 | +// project specified by the GCLOUD_PROJECT environment variable. See |
| 21 | +// https://googlecloudplatform.github.io/gcloud-node/#/docs/guides/authentication |
| 22 | +var gcloud = require('gcloud'); |
| 23 | + |
| 24 | +// Instantiate a storage client |
| 25 | +var storage = gcloud.storage(); |
| 26 | + |
| 27 | +var crypto = require('crypto'); |
| 28 | +// [END setup] |
| 29 | + |
| 30 | +// [START generate_encryption_key] |
| 31 | +/** |
| 32 | + * Generates a 256 bit (32 byte) AES encryption key and prints the base64 |
| 33 | + * representation. |
| 34 | + * |
| 35 | + * This is included for demonstration purposes. You should generate your own |
| 36 | + * key. Please remember that encryption keys should be handled with a |
| 37 | + * comprehensive security policy. |
| 38 | + * |
| 39 | + * @returns {string} The encryption key. |
| 40 | + */ |
| 41 | +function generateEncryptionKey () { |
| 42 | + var buffer = crypto.randomBytes(32); |
| 43 | + var encodedKey = buffer.toString('base64'); |
| 44 | + |
| 45 | + console.log('Base 64 encoded encryption key: %s', encodedKey); |
| 46 | + |
| 47 | + return encodedKey; |
| 48 | +} |
| 49 | +// [END generate_encryption_key] |
| 50 | + |
| 51 | +// [START upload_encrypted_file] |
| 52 | +/** |
| 53 | + * Uploads a file to a Google Cloud Storage bucket using a custom encryption key. |
| 54 | + * |
| 55 | + * The file will be encrypted by Google Cloud Storage and only retrievable using |
| 56 | + * the provided encryption key. |
| 57 | + * |
| 58 | + * @param {string} bucketName The bucket where the file will be uploaded. |
| 59 | + * @param {string} srcFileName The local file to be uploaded. |
| 60 | + * @param {string} destFileName The name of the destination file. |
| 61 | + * @param {string} key The encryption key. |
| 62 | + * @param {function} key The callback function. |
| 63 | + */ |
| 64 | +function uploadEncryptedFile (bucketName, srcFileName, destFileName, key, callback) { |
| 65 | + if (!bucketName) { |
| 66 | + return callback(new Error('"bucketName" is required!')); |
| 67 | + } else if (!srcFileName) { |
| 68 | + return callback(new Error('"srcFileName" is required!')); |
| 69 | + } else if (!destFileName) { |
| 70 | + return callback(new Error('"destFileName" is required!')); |
| 71 | + } else if (!key) { |
| 72 | + return callback(new Error('"key" is required!')); |
| 73 | + } |
| 74 | + |
| 75 | + var bucket = storage.bucket(bucketName); |
| 76 | + var options = { |
| 77 | + destination: destFileName, |
| 78 | + encryptionKey: new Buffer(key, 'base64') |
| 79 | + }; |
| 80 | + |
| 81 | + bucket.upload(srcFileName, options, function (err, file) { |
| 82 | + if (err) { |
| 83 | + return callback(err); |
| 84 | + } |
| 85 | + |
| 86 | + console.log('Uploaded encrypted file: %s', destFileName); |
| 87 | + return callback(null, file); |
| 88 | + }); |
| 89 | +} |
| 90 | +// [END upload_encrypted_file] |
| 91 | + |
| 92 | +// [START download_encrypted_file] |
| 93 | +/** |
| 94 | + * Downloads a previously-encrypted file from Google Cloud Storage. |
| 95 | + * |
| 96 | + * The encryption key provided must be the same key provided when uploading the |
| 97 | + * file. |
| 98 | + * |
| 99 | + * @param {string} bucketName The bucket from which the file will be downloaded. |
| 100 | + * @param {string} srcFileName The local file to be downloaded. |
| 101 | + * @param {string} destFileName The name of the destination file. |
| 102 | + * @param {string} key The encryption key. |
| 103 | + * @param {function} key The callback function. |
| 104 | + */ |
| 105 | +function downloadEncryptedFile (bucketName, srcFileName, destFileName, key, callback) { |
| 106 | + if (!bucketName) { |
| 107 | + return callback(new Error('"bucketName" is required!')); |
| 108 | + } else if (!srcFileName) { |
| 109 | + return callback(new Error('"srcFileName" is required!')); |
| 110 | + } else if (!destFileName) { |
| 111 | + return callback(new Error('"destFileName" is required!')); |
| 112 | + } else if (!key) { |
| 113 | + return callback(new Error('"key" is required!')); |
| 114 | + } |
| 115 | + |
| 116 | + var bucket = storage.bucket(bucketName); |
| 117 | + var file = bucket.file(srcFileName); |
| 118 | + var options = { |
| 119 | + destination: destFileName |
| 120 | + }; |
| 121 | + |
| 122 | + file.setEncryptionKey(new Buffer(key, 'base64')); |
| 123 | + |
| 124 | + file.download(options, function (err) { |
| 125 | + if (err) { |
| 126 | + return callback(err); |
| 127 | + } |
| 128 | + |
| 129 | + console.log('Downloaded encrypted file: %s', destFileName); |
| 130 | + return callback(null); |
| 131 | + }); |
| 132 | +} |
| 133 | +// [END download_encrypted_file] |
| 134 | + |
| 135 | +// [START rotate_encryption_key] |
| 136 | +/** |
| 137 | + * Performs a key rotation by re-writing an encrypted blob with a new encryption |
| 138 | + * key. |
| 139 | + * |
| 140 | + * @param {function} key The callback function. |
| 141 | + */ |
| 142 | +function rotateEncryptionKey (callback) { |
| 143 | + callback(new Error('This is currently not available using the Cloud Client Library.')); |
| 144 | +} |
| 145 | +// [END rotate_encryption_key] |
| 146 | + |
| 147 | +// [START usage] |
| 148 | +function printUsage () { |
| 149 | + console.log('Usage: node encryption COMMAND [ARGS...]'); |
| 150 | + console.log('\nCommands:\n'); |
| 151 | + console.log('\tgenerate-encryption-key'); |
| 152 | + console.log('\tupload BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME KEY'); |
| 153 | + console.log('\tdownload BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME KEY'); |
| 154 | + console.log('\trotate BUCKET_NAME FILE_NAME OLD_KEY NEW_KEY'); |
| 155 | + console.log('\nExamples:\n'); |
| 156 | + console.log('\tnode encryption generate-encryption-key'); |
| 157 | + console.log('\tnode encryption upload my-bucket resources/test.txt file_encrypted.txt QxhqaZEqBGVTW55HhQw9Q='); |
| 158 | + console.log('\tnode encryption download my-bucket file_encrypted.txt ./file.txt QxhqaZEqBGVTW55HhQw9Q='); |
| 159 | + console.log('\tnode encryption rotate my-bucket file_encrypted.txt QxhqaZEqBGVTW55HhQw9Q= SxafpsdfSDFS89sds9Q='); |
| 160 | +} |
| 161 | +// [END usage] |
| 162 | + |
| 163 | +// The command-line program |
| 164 | +var program = { |
| 165 | + generate: generateEncryptionKey, |
| 166 | + upload: uploadEncryptedFile, |
| 167 | + download: downloadEncryptedFile, |
| 168 | + rotate: rotateEncryptionKey, |
| 169 | + printUsage: printUsage, |
| 170 | + |
| 171 | + // Executed when this program is run from the command-line |
| 172 | + main: function (args, cb) { |
| 173 | + var command = args.shift(); |
| 174 | + if (command === 'generate-encryption-key') { |
| 175 | + this.generate(cb); |
| 176 | + } else if (command === 'upload') { |
| 177 | + this.upload(args[0], args[1], args[2], args[3], cb); |
| 178 | + } else if (command === 'download') { |
| 179 | + this.download(args[0], args[1], args[2], args[3], cb); |
| 180 | + } else if (command === 'rotate') { |
| 181 | + this.rotate(cb); |
| 182 | + } else { |
| 183 | + this.printUsage(); |
| 184 | + } |
| 185 | + } |
| 186 | +}; |
| 187 | + |
| 188 | +if (module === require.main) { |
| 189 | + program.main(process.argv.slice(2), console.log); |
| 190 | +} |
| 191 | +// [END all] |
| 192 | + |
| 193 | +module.exports = program; |
0 commit comments