Skip to content

Commit

Permalink
Allow a base header for setEncryptionKey.
Browse files Browse the repository at this point in the history
  • Loading branch information
lukesneeringer committed May 21, 2017
1 parent d152416 commit 3f00678
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
13 changes: 9 additions & 4 deletions packages/storage/src/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -1135,6 +1135,9 @@ File.prototype.download = function(options, callback) {
* @resource [Customer-supplied Encryption Keys]{@link https://cloud.google.com/storage/docs/encryption#customer-supplied}
*
* @param {string|buffer} encryptionKey - An AES-256 encryption key.
* @param {Object=} options - Optional configuration.
* @param {string} options.baseHeader - The prefix of the header being set.
* Defaults to 'x-goog-encryption'.
* @return {module:storage/file}
*
* @example
Expand All @@ -1160,7 +1163,9 @@ File.prototype.download = function(options, callback) {
* });
* });
*/
File.prototype.setEncryptionKey = function(encryptionKey) {
File.prototype.setEncryptionKey = function(encryptionKey, options) {
options = options || {}
options.baseHeader = options.baseHeader || 'x-goog-encryption';
this.encryptionKey = encryptionKey;

encryptionKey = new Buffer(encryptionKey).toString('base64');
Expand All @@ -1171,9 +1176,9 @@ File.prototype.setEncryptionKey = function(encryptionKey) {
this.interceptors.push({
request: function(reqOpts) {
reqOpts.headers = reqOpts.headers || {};
reqOpts.headers['x-goog-encryption-algorithm'] = 'AES256';
reqOpts.headers['x-goog-encryption-key'] = encryptionKey;
reqOpts.headers['x-goog-encryption-key-sha256'] = hash;
reqOpts.headers[options.baseHeader + '-algorithm'] = 'AES256';
reqOpts.headers[options.baseHeader + '-key'] = encryptionKey;
reqOpts.headers[options.baseHeader + '-key-sha256'] = hash;
return reqOpts;
}
});
Expand Down
27 changes: 23 additions & 4 deletions packages/storage/test/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2429,19 +2429,18 @@ describe('File', function() {
var KEY = crypto.randomBytes(32);
var _file;

beforeEach(function() {
_file = file.setEncryptionKey(KEY);
});

it('should localize the key', function() {
var _file = file.setEncryptionKey(KEY);
assert.strictEqual(file.encryptionKey, KEY);
});

it('should return the file instance', function() {
var _file = file.setEncryptionKey(KEY);
assert.strictEqual(_file, file);
});

it('should push the correct request interceptor', function(done) {
var _file = file.setEncryptionKey(KEY);
var base64Key = new Buffer(KEY).toString('base64');
var base64KeyHash = crypto.createHash('sha256');
base64KeyHash.update(base64Key, 'base64');
Expand All @@ -2456,6 +2455,26 @@ describe('File', function() {

done();
});

it('should allow a different base header', function(done) {
var _file = file.setEncryptionKey(KEY, {
baseHeader: 'x-goog-copy-source-encryption',
});
var base64Key = new Buffer(KEY).toString('base64');
var base64KeyHash = crypto.createHash('sha256');
base64KeyHash.update(base64Key, 'base64');

assert.deepEqual(file.interceptors[0].request({}), {
headers: {
'x-goog-copy-source-encryption-algorithm': 'AES256',
'x-goog-copy-source-encryption-key': base64Key,
'x-goog-copy-source-encryption-key-sha256':
base64KeyHash.digest('base64')
}
});

done();
});
});

describe('startResumableUpload_', function() {
Expand Down

0 comments on commit 3f00678

Please sign in to comment.