Skip to content

Commit

Permalink
Add Cloud Storage encryption samples and tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdobry committed Aug 10, 2016
1 parent d3799fe commit dff6740
Show file tree
Hide file tree
Showing 7 changed files with 215 additions and 70 deletions.
3 changes: 2 additions & 1 deletion pubsub/system-test/subscriptions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ describe('pubsub:subscriptions', function () {
assert.ifError(err);
assert.equal(subscription.name, name);
assert(console.log.calledWith('Created subscription %s to topic %s', subscriptionName, topicName));
done();
// The next test sometimes fails, so, slow this test down
setTimeout(done, 2000);
});
});
});
Expand Down
72 changes: 59 additions & 13 deletions storage/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,19 @@ View the [documentation][buckets_docs] or the [source code][buckets_code].
__Usage:__ `node buckets --help`

```
Usage: node buckets [COMMAND] [ARGS...]
Usage: node buckets COMMAND [ARGS...]
Commands:
create [BUCKET_NAME]
create BUCKET_NAME
list
delete [BUCKET_NAME]
delete BUCKET_NAME
Examples:
node buckets create my-bucket
node buckets list
node buckets delete my-bucket
```

[buckets_docs]: https://cloud.google.com/storage/docs
Expand All @@ -52,20 +58,60 @@ View the [documentation][files_docs] or the [source code][files_code].
__Usage:__ `node files --help`

```
Usage: node files [COMMAND] [ARGS...]
Usage: node files COMMAND [ARGS...]
Commands:
list [BUCKET_NAME]
listByPrefix [BUCKET_NAME] [PREFIX] [DELIMITER]
upload [BUCKET_NAME] [FILE_NAME]
download [BUCKET_NAME] [SRC_FILE_NAME] [DEST_FILE_NAME]
delete [BUCKET_NAME] [FILE_NAME]
getMetadata [BUCKET_NAME] [FILE_NAME]
makePublic [BUCKET_NAME] [FILE_NAME]
move [BUCKET_NAME] [SRC_FILE_NAME] [DEST_FILE_NAME]
copy [BUCKET_NAME] [SRC_FILE_NAME] [DEST_BUCKET_NAME] [DEST_FILE_NAME]
list BUCKET_NAME
listByPrefix BUCKET_NAME PREFIX [DELIMITER]
upload BUCKET_NAME FILE_NAME
download BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME
delete BUCKET_NAME FILE_NAME
getMetadata BUCKET_NAME FILE_NAME
makePublic BUCKET_NAME FILE_NAME
move BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME
copy BUCKET_NAME SRC_FILE_NAME DEST_BUCKET_NAME DEST_FILE_NAME
Examples:
list my-bucket
listByPrefix my-bucket /some-folder
listByPrefix my-bucket /some-folder -
upload my-bucket ./file.txt
download my-bucket file.txt ./file.txt
delete my-bucket file.txt
getMetadata my-bucket file.txt
makePublic my-bucket file.txt
move my-bucket file.txt file2.txt
copy my-bucket file.txt my-other-bucket file.txt
```

[files_docs]: https://cloud.google.com/storage/docs
[files_code]: files.js

### Encryption

View the [documentation][encryption_docs] or the [source code][encryption_code].

__Usage:__ `node encryption --help`

```
Usage: node encryption COMMAND [ARGS...]
Commands:
generate-encryption-key
upload BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME KEY
download BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME KEY
rotate BUCKET_NAME FILE_NAME OLD_KEY NEW_KEY
Examples:
node encryption generate-encryption-key
node encryption upload my-bucket resources/test.txt file_encrypted.txt QxhqaZEqBGVTW55HhQw9Q=
node encryption download my-bucket file_encrypted.txt ./file.txt QxhqaZEqBGVTW55HhQw9Q=
node encryption rotate my-bucket file_encrypted.txt QxhqaZEqBGVTW55HhQw9Q= SxafpsdfSDFS89sds9Q=
```

[encryption_docs]: https://cloud.google.com/storage/docs
[encryption_code]: encryption.js
16 changes: 8 additions & 8 deletions storage/encryption.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,23 +162,23 @@ function printUsage () {

// The command-line program
var program = {
generate: generateEncryptionKey,
upload: uploadEncryptedFile,
download: downloadEncryptedFile,
rotate: rotateEncryptionKey,
generateEncryptionKey: generateEncryptionKey,
uploadEncryptedFile: uploadEncryptedFile,
downloadEncryptedFile: downloadEncryptedFile,
rotateEncryptionKey: rotateEncryptionKey,
printUsage: printUsage,

// Executed when this program is run from the command-line
main: function (args, cb) {
var command = args.shift();
if (command === 'generate-encryption-key') {
this.generate(cb);
this.generateEncryptionKey();
} else if (command === 'upload') {
this.upload(args[0], args[1], args[2], args[3], cb);
this.uploadEncryptedFile(args[0], args[1], args[2], args[3], cb);
} else if (command === 'download') {
this.download(args[0], args[1], args[2], args[3], cb);
this.downloadEncryptedFile(args[0], args[1], args[2], args[3], cb);
} else if (command === 'rotate') {
this.rotate(cb);
this.rotateEncryptionKey(cb);
} else {
this.printUsage();
}
Expand Down
31 changes: 21 additions & 10 deletions storage/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,17 +324,28 @@ function copyFile (name, srcFileName, destBucketName, destFileName, callback) {

// [START usage]
function printUsage () {
console.log('Usage: node files [COMMAND] [ARGS...]');
console.log('Usage: node files COMMAND [ARGS...]');
console.log('\nCommands:\n');
console.log('\tlist [BUCKET_NAME]');
console.log('\tlistByPrefix [BUCKET_NAME] [PREFIX] [DELIMITER]');
console.log('\tupload [BUCKET_NAME] [FILE_NAME]');
console.log('\tdownload [BUCKET_NAME] [SRC_FILE_NAME] [DEST_FILE_NAME]');
console.log('\tdelete [BUCKET_NAME] [FILE_NAME]');
console.log('\tgetMetadata [BUCKET_NAME] [FILE_NAME]');
console.log('\tmakePublic [BUCKET_NAME] [FILE_NAME]');
console.log('\tmove [BUCKET_NAME] [SRC_FILE_NAME] [DEST_FILE_NAME]');
console.log('\tcopy [BUCKET_NAME] [SRC_FILE_NAME] [DEST_BUCKET_NAME] [DEST_FILE_NAME]');
console.log('\tlist BUCKET_NAME');
console.log('\tlistByPrefix BUCKET_NAME PREFIX [DELIMITER]');
console.log('\tupload BUCKET_NAME FILE_NAME');
console.log('\tdownload BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME');
console.log('\tdelete BUCKET_NAME FILE_NAME');
console.log('\tgetMetadata BUCKET_NAME FILE_NAME');
console.log('\tmakePublic BUCKET_NAME FILE_NAME');
console.log('\tmove BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME');
console.log('\tcopy BUCKET_NAME SRC_FILE_NAME DEST_BUCKET_NAME DEST_FILE_NAME');
console.log('\nExamples:\n');
console.log('\tlist my-bucket');
console.log('\tlistByPrefix my-bucket /some-folder');
console.log('\tlistByPrefix my-bucket /some-folder -');
console.log('\tupload my-bucket ./file.txt');
console.log('\tdownload my-bucket file.txt ./file.txt');
console.log('\tdelete my-bucket file.txt');
console.log('\tgetMetadata my-bucket file.txt');
console.log('\tmakePublic my-bucket file.txt');
console.log('\tmove my-bucket file.txt file2.txt');
console.log('\tcopy my-bucket file.txt my-other-bucket file.txt');
}
// [END usage]

Expand Down
73 changes: 73 additions & 0 deletions storage/system-test/encryption.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2015-2016, Google, Inc.
// 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';

var fs = require('fs');
var path = require('path');
var gcloud = require('gcloud');
var uuid = require('node-uuid');
var storage = gcloud.storage();
var program = require('../encryption');

var bucketName = 'nodejs-docs-samples-test-' + uuid.v4();
var fileName = 'test.txt';
var filePath = path.join(__dirname, '../resources', fileName);
var downloadFilePath = path.join(__dirname, '../resources/downloaded.txt');

describe('storage:encryption', function () {
var key;
before(function (done) {
key = program.generateEncryptionKey();
storage.createBucket(bucketName, done);
});

after(function (done) {
try {
fs.unlinkSync(downloadFilePath);
} catch (err) {
console.log(err);
}
storage.bucket(bucketName).deleteFiles({ force: true }, function (err) {
if (err) {
return done(err);
}
storage.bucket(bucketName).delete(done);
});
});

describe('uploadEncryptedFile', function () {
it('should upload a file', function (done) {
program.uploadEncryptedFile(bucketName, filePath, fileName, key, function (err, file) {
assert.ifError(err);
assert(file);
assert.equal(file.name, fileName);
assert(console.log.calledWith('Uploaded encrypted file: %s', fileName));
done();
});
});
});

describe('downloadEncryptedFile', function () {
it('should download a file', function (done) {
program.downloadEncryptedFile(bucketName, fileName, downloadFilePath, key, function (err) {
assert.ifError(err);
assert.doesNotThrow(function () {
fs.statSync(downloadFilePath);
});
assert(console.log.calledWith('Downloaded encrypted file: %s', downloadFilePath));
done();
});
});
});
});
Loading

0 comments on commit dff6740

Please sign in to comment.