Skip to content

Commit 50d465b

Browse files
stephenpluspluscallmehiphop
authored andcommitted
storage: rename key: to encryptionKey: (#1384)
1 parent 464ff6c commit 50d465b

File tree

5 files changed

+47
-43
lines changed

5 files changed

+47
-43
lines changed

lib/storage/bucket.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -942,10 +942,10 @@ Bucket.prototype.makePublic = function(options, callback) {
942942
* will be uploaded to the File object's bucket and under the File object's
943943
* name. Lastly, when this argument is omitted, the file is uploaded to your
944944
* bucket using the name of the local file.
945+
* @param {string} options.encryptionKey - A custom encryption key. See
946+
* [Customer-supplied Encryption Keys](https://cloud.google.com/storage/docs/encryption#customer-supplied).
945947
* @param {boolean} options.gzip - Automatically gzip the file. This will set
946948
* `options.metadata.contentEncoding` to `gzip`.
947-
* @param {string} options.key - A custom encryption key. See
948-
* [Customer-supplied Encryption Keys](https://cloud.google.com/storage/docs/encryption#customer-supplied).
949949
* @param {object} options.metadata - See an
950950
* [Objects: insert request body](https://cloud.google.com/storage/docs/json_api/v1/objects/insert#request_properties_JSON).
951951
* @param {string} options.offset - The starting byte of the upload stream, for
@@ -1050,14 +1050,15 @@ Bucket.prototype.makePublic = function(options, callback) {
10501050
* });
10511051
*
10521052
* //-
1053-
* // To use [Customer-supplied Encryption Keys](https://cloud.google.com/storage/docs/encryption#customer-supplied),
1054-
* // provide the `key` option.
1053+
* // To use
1054+
* // <a href="https://cloud.google.com/storage/docs/encryption#customer-supplied">
1055+
* // Customer-supplied Encryption Keys</a>, provide the `encryptionKey` option.
10551056
* //-
10561057
* var crypto = require('crypto');
10571058
* var encryptionKey = crypto.randomBytes(32);
10581059
*
10591060
* bucket.upload('img.png', {
1060-
* key: encryptionKey
1061+
* encryptionKey: encryptionKey
10611062
* }, function(err, newFile) {
10621063
* // `img.png` was uploaded with your custom encryption key.
10631064
*
@@ -1067,12 +1068,12 @@ Bucket.prototype.makePublic = function(options, callback) {
10671068
* // However, to use your encryption key later, you must create a `File`
10681069
* // instance with the `key` supplied:
10691070
* var file = bucket.file('img.png', {
1070-
* key: encryptionKey
1071+
* encryptionKey: encryptionKey
10711072
* });
10721073
*
1073-
* // Or with `file#setKey`:
1074+
* // Or with `file#setEncryptionKey`:
10741075
* var file = bucket.file('img.png');
1075-
* file.setKey(encryptionKey);
1076+
* file.setEncryptionKey(encryptionKey);
10761077
* });
10771078
*/
10781079
Bucket.prototype.upload = function(localPath, options, callback) {
@@ -1091,12 +1092,12 @@ Bucket.prototype.upload = function(localPath, options, callback) {
10911092
} else if (is.string(options.destination)) {
10921093
// Use the string as the name of the file.
10931094
newFile = this.file(options.destination, {
1094-
key: options.key
1095+
encryptionKey: options.encryptionKey
10951096
});
10961097
} else {
10971098
// Resort to using the name of the incoming file.
10981099
newFile = this.file(path.basename(localPath), {
1099-
key: options.key
1100+
encryptionKey: options.encryptionKey
11001101
});
11011102
}
11021103

lib/storage/file.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ var STORAGE_UPLOAD_BASE_URL = 'https://www.googleapis.com/upload/storage/v1/b';
8585
* attached to.
8686
* @param {string} name - The name of the remote file.
8787
* @param {object=} options - Configuration object.
88+
* @param {string} options.encryptionKey - A custom encryption key.
8889
* @param {number} options.generation - Generation to scope the file to.
89-
* @param {string} options.key - A custom encryption key.
9090
*/
9191
/**
9292
* A File object is created from your Bucket object using
@@ -242,8 +242,8 @@ function File(bucket, name, options) {
242242
methods: methods
243243
});
244244

245-
if (options.key) {
246-
this.setKey(options.key);
245+
if (options.encryptionKey) {
246+
this.setEncryptionKey(options.encryptionKey);
247247
}
248248

249249
/**
@@ -997,47 +997,48 @@ File.prototype.download = function(options, callback) {
997997

998998
/**
999999
* The Storage API allows you to use a custom key for server-side encryption.
1000-
* Supply this method with a passphrase and the correct key (AES-256) will be
1001-
* generated and used for you.
10021000
*
10031001
* @resource [Customer-supplied Encryption Keys]{@link https://cloud.google.com/storage/docs/encryption#customer-supplied}
10041002
*
1005-
* @param {string|buffer} key - An AES-256 encryption key.
1003+
* @param {string|buffer} encryptionKey - An AES-256 encryption key.
10061004
* @return {module:storage/file}
10071005
*
10081006
* @example
10091007
* var crypto = require('crypto');
10101008
* var encryptionKey = crypto.randomBytes(32);
10111009
*
10121010
* var fileWithCustomEncryption = myBucket.file('my-file');
1013-
* fileWithCustomEncryption.setKey(encryptionKey);
1011+
* fileWithCustomEncryption.setEncryptionKey(encryptionKey);
10141012
*
10151013
* var fileWithoutCustomEncryption = myBucket.file('my-file');
10161014
*
10171015
* fileWithCustomEncryption.save('data', function(err) {
1018-
* // Try to download with the File object that hasn't had `setKey()` called:
1016+
* // Try to download with the File object that hasn't had
1017+
* // `setEncryptionKey()` called:
10191018
* fileWithoutCustomEncryption.download(function(err) {
10201019
* // We will receive an error:
10211020
* // err.message === 'Bad Request'
10221021
*
1023-
* // Try again with the File object we called `setKey()` on:
1022+
* // Try again with the File object we called `setEncryptionKey()` on:
10241023
* fileWithCustomEncryption.download(function(err, contents) {
10251024
* // contents.toString() === 'data'
10261025
* });
10271026
* });
10281027
* });
10291028
*/
1030-
File.prototype.setKey = function(key) {
1031-
this.key = key;
1029+
File.prototype.setEncryptionKey = function(encryptionKey) {
1030+
this.encryptionKey = encryptionKey;
10321031

1033-
key = new Buffer(key).toString('base64');
1034-
var hash = crypto.createHash('sha256').update(key, 'base64').digest('base64');
1032+
encryptionKey = new Buffer(encryptionKey).toString('base64');
1033+
var hash = crypto.createHash('sha256')
1034+
.update(encryptionKey, 'base64')
1035+
.digest('base64');
10351036

10361037
this.interceptors.push({
10371038
request: function(reqOpts) {
10381039
reqOpts.headers = reqOpts.headers || {};
10391040
reqOpts.headers['x-goog-encryption-algorithm'] = 'AES256';
1040-
reqOpts.headers['x-goog-encryption-key'] = key;
1041+
reqOpts.headers['x-goog-encryption-key'] = encryptionKey;
10411042
reqOpts.headers['x-goog-encryption-key-sha256'] = hash;
10421043
return reqOpts;
10431044
}
@@ -1613,7 +1614,7 @@ File.prototype.startResumableUpload_ = function(dup, options) {
16131614
bucket: this.bucket.name,
16141615
file: this.name,
16151616
generation: this.generation,
1616-
key: this.key,
1617+
key: this.encryptionKey,
16171618
metadata: options.metadata,
16181619
offset: options.offset,
16191620
predefinedAcl: options.predefinedAcl,

system-test/storage.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ describe('storage', function() {
383383
var key = crypto.randomBytes(32);
384384

385385
bucket.upload(FILES.big.path, {
386-
key: key,
386+
encryptionKey: key,
387387
resumable: false
388388
}, function(err, file) {
389389
assert.ifError(err);
@@ -403,7 +403,7 @@ describe('storage', function() {
403403
var key = crypto.randomBytes(32);
404404

405405
bucket.upload(FILES.big.path, {
406-
key: key,
406+
encryptionKey: key,
407407
resumable: true
408408
}, function(err, file) {
409409
assert.ifError(err);
@@ -802,7 +802,9 @@ describe('storage', function() {
802802
describe('customer-supplied encryption keys', function() {
803803
var encryptionKey = crypto.randomBytes(32);
804804

805-
var file = bucket.file('encrypted-file', { key: encryptionKey });
805+
var file = bucket.file('encrypted-file', {
806+
encryptionKey: encryptionKey
807+
});
806808
var unencryptedFile = bucket.file(file.name);
807809

808810
before(function(done) {

test/storage/bucket.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -911,13 +911,13 @@ describe('Bucket', function() {
911911
it('should accept a path, metadata, & cb', function(done) {
912912
var options = {
913913
metadata: metadata,
914-
key: 'key'
914+
encryptionKey: 'key'
915915
};
916916
bucket.upload(filepath, options, function(err, file) {
917917
assert.ifError(err);
918918
assert.equal(file.bucket.name, bucket.name);
919919
assert.deepEqual(file.metadata, metadata);
920-
assert.strictEqual(file.options.key, options.key);
920+
assert.strictEqual(file.options.encryptionKey, options.encryptionKey);
921921
done();
922922
});
923923
});
@@ -926,13 +926,13 @@ describe('Bucket', function() {
926926
var newFileName = 'new-file-name.png';
927927
var options = {
928928
destination: newFileName,
929-
key: 'key'
929+
encryptionKey: 'key'
930930
};
931931
bucket.upload(filepath, options, function(err, file) {
932932
assert.ifError(err);
933933
assert.equal(file.bucket.name, bucket.name);
934934
assert.equal(file.name, newFileName);
935-
assert.strictEqual(file.options.key, options.key);
935+
assert.strictEqual(file.options.encryptionKey, options.encryptionKey);
936936
done();
937937
});
938938
});
@@ -942,14 +942,14 @@ describe('Bucket', function() {
942942
var options = {
943943
destination: newFileName,
944944
metadata: metadata,
945-
key: 'key'
945+
encryptionKey: 'key'
946946
};
947947
bucket.upload(filepath, options, function(err, file) {
948948
assert.ifError(err);
949949
assert.equal(file.bucket.name, bucket.name);
950950
assert.equal(file.name, newFileName);
951951
assert.deepEqual(file.metadata, metadata);
952-
assert.strictEqual(file.options.key, options.key);
952+
assert.strictEqual(file.options.encryptionKey, options.encryptionKey);
953953
done();
954954
});
955955
});

test/storage/file.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,14 @@ describe('File', function() {
228228
it('should set a custom encryption key', function(done) {
229229
var key = 'key';
230230

231-
var setKey = File.prototype.setKey;
232-
File.prototype.setKey = function(key_) {
233-
File.prototype.setKey = setKey;
231+
var setEncryptionKey = File.prototype.setEncryptionKey;
232+
File.prototype.setEncryptionKey = function(key_) {
233+
File.prototype.setEncryptionKey = setEncryptionKey;
234234
assert.strictEqual(key_, key);
235235
done();
236236
};
237237

238-
new File(BUCKET, FILE_NAME, { key: key });
238+
new File(BUCKET, FILE_NAME, { encryptionKey: key });
239239
});
240240
});
241241

@@ -2191,15 +2191,15 @@ describe('File', function() {
21912191
});
21922192
});
21932193

2194-
describe('setKey', function() {
2194+
describe('setEncryptionKey', function() {
21952195
var KEY = crypto.randomBytes(32);
21962196

21972197
beforeEach(function() {
2198-
file.setKey(KEY);
2198+
file.setEncryptionKey(KEY);
21992199
});
22002200

22012201
it('should localize the key', function() {
2202-
assert.strictEqual(file.key, KEY);
2202+
assert.strictEqual(file.encryptionKey, KEY);
22032203
});
22042204

22052205
it('should push the correct request interceptor', function(done) {
@@ -2232,7 +2232,7 @@ describe('File', function() {
22322232
};
22332233

22342234
file.generation = 3;
2235-
file.key = 'key';
2235+
file.encryptionKey = 'key';
22362236

22372237
resumableUploadOverride = function(opts) {
22382238
var bucket = file.bucket;
@@ -2243,7 +2243,7 @@ describe('File', function() {
22432243
assert.strictEqual(opts.bucket, bucket.name);
22442244
assert.strictEqual(opts.file, file.name);
22452245
assert.strictEqual(opts.generation, file.generation);
2246-
assert.strictEqual(opts.key, file.key);
2246+
assert.strictEqual(opts.key, file.encryptionKey);
22472247
assert.strictEqual(opts.metadata, options.metadata);
22482248
assert.strictEqual(opts.offset, options.offset);
22492249
assert.strictEqual(opts.predefinedAcl, options.predefinedAcl);

0 commit comments

Comments
 (0)