From 50d465b3a960e297abbccf82bd0697f44535f087 Mon Sep 17 00:00:00 2001 From: Stephen Sawchuk Date: Tue, 14 Jun 2016 12:41:28 -0400 Subject: [PATCH] storage: rename key: to encryptionKey: (#1384) --- lib/storage/bucket.js | 21 +++++++++++---------- lib/storage/file.js | 31 ++++++++++++++++--------------- system-test/storage.js | 8 +++++--- test/storage/bucket.js | 12 ++++++------ test/storage/file.js | 18 +++++++++--------- 5 files changed, 47 insertions(+), 43 deletions(-) diff --git a/lib/storage/bucket.js b/lib/storage/bucket.js index c22d6bda212..f893386f017 100644 --- a/lib/storage/bucket.js +++ b/lib/storage/bucket.js @@ -942,10 +942,10 @@ Bucket.prototype.makePublic = function(options, callback) { * will be uploaded to the File object's bucket and under the File object's * name. Lastly, when this argument is omitted, the file is uploaded to your * bucket using the name of the local file. + * @param {string} options.encryptionKey - A custom encryption key. See + * [Customer-supplied Encryption Keys](https://cloud.google.com/storage/docs/encryption#customer-supplied). * @param {boolean} options.gzip - Automatically gzip the file. This will set * `options.metadata.contentEncoding` to `gzip`. - * @param {string} options.key - A custom encryption key. See - * [Customer-supplied Encryption Keys](https://cloud.google.com/storage/docs/encryption#customer-supplied). * @param {object} options.metadata - See an * [Objects: insert request body](https://cloud.google.com/storage/docs/json_api/v1/objects/insert#request_properties_JSON). * @param {string} options.offset - The starting byte of the upload stream, for @@ -1050,14 +1050,15 @@ Bucket.prototype.makePublic = function(options, callback) { * }); * * //- - * // To use [Customer-supplied Encryption Keys](https://cloud.google.com/storage/docs/encryption#customer-supplied), - * // provide the `key` option. + * // To use + * // + * // Customer-supplied Encryption Keys, provide the `encryptionKey` option. * //- * var crypto = require('crypto'); * var encryptionKey = crypto.randomBytes(32); * * bucket.upload('img.png', { - * key: encryptionKey + * encryptionKey: encryptionKey * }, function(err, newFile) { * // `img.png` was uploaded with your custom encryption key. * @@ -1067,12 +1068,12 @@ Bucket.prototype.makePublic = function(options, callback) { * // However, to use your encryption key later, you must create a `File` * // instance with the `key` supplied: * var file = bucket.file('img.png', { - * key: encryptionKey + * encryptionKey: encryptionKey * }); * - * // Or with `file#setKey`: + * // Or with `file#setEncryptionKey`: * var file = bucket.file('img.png'); - * file.setKey(encryptionKey); + * file.setEncryptionKey(encryptionKey); * }); */ Bucket.prototype.upload = function(localPath, options, callback) { @@ -1091,12 +1092,12 @@ Bucket.prototype.upload = function(localPath, options, callback) { } else if (is.string(options.destination)) { // Use the string as the name of the file. newFile = this.file(options.destination, { - key: options.key + encryptionKey: options.encryptionKey }); } else { // Resort to using the name of the incoming file. newFile = this.file(path.basename(localPath), { - key: options.key + encryptionKey: options.encryptionKey }); } diff --git a/lib/storage/file.js b/lib/storage/file.js index 6bbc43e2eda..a023c0c3d45 100644 --- a/lib/storage/file.js +++ b/lib/storage/file.js @@ -85,8 +85,8 @@ var STORAGE_UPLOAD_BASE_URL = 'https://www.googleapis.com/upload/storage/v1/b'; * attached to. * @param {string} name - The name of the remote file. * @param {object=} options - Configuration object. + * @param {string} options.encryptionKey - A custom encryption key. * @param {number} options.generation - Generation to scope the file to. - * @param {string} options.key - A custom encryption key. */ /** * A File object is created from your Bucket object using @@ -242,8 +242,8 @@ function File(bucket, name, options) { methods: methods }); - if (options.key) { - this.setKey(options.key); + if (options.encryptionKey) { + this.setEncryptionKey(options.encryptionKey); } /** @@ -997,12 +997,10 @@ File.prototype.download = function(options, callback) { /** * The Storage API allows you to use a custom key for server-side encryption. - * Supply this method with a passphrase and the correct key (AES-256) will be - * generated and used for you. * * @resource [Customer-supplied Encryption Keys]{@link https://cloud.google.com/storage/docs/encryption#customer-supplied} * - * @param {string|buffer} key - An AES-256 encryption key. + * @param {string|buffer} encryptionKey - An AES-256 encryption key. * @return {module:storage/file} * * @example @@ -1010,34 +1008,37 @@ File.prototype.download = function(options, callback) { * var encryptionKey = crypto.randomBytes(32); * * var fileWithCustomEncryption = myBucket.file('my-file'); - * fileWithCustomEncryption.setKey(encryptionKey); + * fileWithCustomEncryption.setEncryptionKey(encryptionKey); * * var fileWithoutCustomEncryption = myBucket.file('my-file'); * * fileWithCustomEncryption.save('data', function(err) { - * // Try to download with the File object that hasn't had `setKey()` called: + * // Try to download with the File object that hasn't had + * // `setEncryptionKey()` called: * fileWithoutCustomEncryption.download(function(err) { * // We will receive an error: * // err.message === 'Bad Request' * - * // Try again with the File object we called `setKey()` on: + * // Try again with the File object we called `setEncryptionKey()` on: * fileWithCustomEncryption.download(function(err, contents) { * // contents.toString() === 'data' * }); * }); * }); */ -File.prototype.setKey = function(key) { - this.key = key; +File.prototype.setEncryptionKey = function(encryptionKey) { + this.encryptionKey = encryptionKey; - key = new Buffer(key).toString('base64'); - var hash = crypto.createHash('sha256').update(key, 'base64').digest('base64'); + encryptionKey = new Buffer(encryptionKey).toString('base64'); + var hash = crypto.createHash('sha256') + .update(encryptionKey, 'base64') + .digest('base64'); this.interceptors.push({ request: function(reqOpts) { reqOpts.headers = reqOpts.headers || {}; reqOpts.headers['x-goog-encryption-algorithm'] = 'AES256'; - reqOpts.headers['x-goog-encryption-key'] = key; + reqOpts.headers['x-goog-encryption-key'] = encryptionKey; reqOpts.headers['x-goog-encryption-key-sha256'] = hash; return reqOpts; } @@ -1613,7 +1614,7 @@ File.prototype.startResumableUpload_ = function(dup, options) { bucket: this.bucket.name, file: this.name, generation: this.generation, - key: this.key, + key: this.encryptionKey, metadata: options.metadata, offset: options.offset, predefinedAcl: options.predefinedAcl, diff --git a/system-test/storage.js b/system-test/storage.js index 4391ab26f1e..9b294167f00 100644 --- a/system-test/storage.js +++ b/system-test/storage.js @@ -383,7 +383,7 @@ describe('storage', function() { var key = crypto.randomBytes(32); bucket.upload(FILES.big.path, { - key: key, + encryptionKey: key, resumable: false }, function(err, file) { assert.ifError(err); @@ -403,7 +403,7 @@ describe('storage', function() { var key = crypto.randomBytes(32); bucket.upload(FILES.big.path, { - key: key, + encryptionKey: key, resumable: true }, function(err, file) { assert.ifError(err); @@ -802,7 +802,9 @@ describe('storage', function() { describe('customer-supplied encryption keys', function() { var encryptionKey = crypto.randomBytes(32); - var file = bucket.file('encrypted-file', { key: encryptionKey }); + var file = bucket.file('encrypted-file', { + encryptionKey: encryptionKey + }); var unencryptedFile = bucket.file(file.name); before(function(done) { diff --git a/test/storage/bucket.js b/test/storage/bucket.js index c16ea498161..510d6857fa3 100644 --- a/test/storage/bucket.js +++ b/test/storage/bucket.js @@ -911,13 +911,13 @@ describe('Bucket', function() { it('should accept a path, metadata, & cb', function(done) { var options = { metadata: metadata, - key: 'key' + encryptionKey: 'key' }; bucket.upload(filepath, options, function(err, file) { assert.ifError(err); assert.equal(file.bucket.name, bucket.name); assert.deepEqual(file.metadata, metadata); - assert.strictEqual(file.options.key, options.key); + assert.strictEqual(file.options.encryptionKey, options.encryptionKey); done(); }); }); @@ -926,13 +926,13 @@ describe('Bucket', function() { var newFileName = 'new-file-name.png'; var options = { destination: newFileName, - key: 'key' + encryptionKey: 'key' }; bucket.upload(filepath, options, function(err, file) { assert.ifError(err); assert.equal(file.bucket.name, bucket.name); assert.equal(file.name, newFileName); - assert.strictEqual(file.options.key, options.key); + assert.strictEqual(file.options.encryptionKey, options.encryptionKey); done(); }); }); @@ -942,14 +942,14 @@ describe('Bucket', function() { var options = { destination: newFileName, metadata: metadata, - key: 'key' + encryptionKey: 'key' }; bucket.upload(filepath, options, function(err, file) { assert.ifError(err); assert.equal(file.bucket.name, bucket.name); assert.equal(file.name, newFileName); assert.deepEqual(file.metadata, metadata); - assert.strictEqual(file.options.key, options.key); + assert.strictEqual(file.options.encryptionKey, options.encryptionKey); done(); }); }); diff --git a/test/storage/file.js b/test/storage/file.js index cb38e3708c7..dfc39e2eeae 100644 --- a/test/storage/file.js +++ b/test/storage/file.js @@ -228,14 +228,14 @@ describe('File', function() { it('should set a custom encryption key', function(done) { var key = 'key'; - var setKey = File.prototype.setKey; - File.prototype.setKey = function(key_) { - File.prototype.setKey = setKey; + var setEncryptionKey = File.prototype.setEncryptionKey; + File.prototype.setEncryptionKey = function(key_) { + File.prototype.setEncryptionKey = setEncryptionKey; assert.strictEqual(key_, key); done(); }; - new File(BUCKET, FILE_NAME, { key: key }); + new File(BUCKET, FILE_NAME, { encryptionKey: key }); }); }); @@ -2191,15 +2191,15 @@ describe('File', function() { }); }); - describe('setKey', function() { + describe('setEncryptionKey', function() { var KEY = crypto.randomBytes(32); beforeEach(function() { - file.setKey(KEY); + file.setEncryptionKey(KEY); }); it('should localize the key', function() { - assert.strictEqual(file.key, KEY); + assert.strictEqual(file.encryptionKey, KEY); }); it('should push the correct request interceptor', function(done) { @@ -2232,7 +2232,7 @@ describe('File', function() { }; file.generation = 3; - file.key = 'key'; + file.encryptionKey = 'key'; resumableUploadOverride = function(opts) { var bucket = file.bucket; @@ -2243,7 +2243,7 @@ describe('File', function() { assert.strictEqual(opts.bucket, bucket.name); assert.strictEqual(opts.file, file.name); assert.strictEqual(opts.generation, file.generation); - assert.strictEqual(opts.key, file.key); + assert.strictEqual(opts.key, file.encryptionKey); assert.strictEqual(opts.metadata, options.metadata); assert.strictEqual(opts.offset, options.offset); assert.strictEqual(opts.predefinedAcl, options.predefinedAcl);