Skip to content

Commit dff6740

Browse files
committed
Add Cloud Storage encryption samples and tests.
1 parent d3799fe commit dff6740

File tree

7 files changed

+215
-70
lines changed

7 files changed

+215
-70
lines changed

pubsub/system-test/subscriptions.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@ describe('pubsub:subscriptions', function () {
3939
assert.ifError(err);
4040
assert.equal(subscription.name, name);
4141
assert(console.log.calledWith('Created subscription %s to topic %s', subscriptionName, topicName));
42-
done();
42+
// The next test sometimes fails, so, slow this test down
43+
setTimeout(done, 2000);
4344
});
4445
});
4546
});

storage/README.md

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@ View the [documentation][buckets_docs] or the [source code][buckets_code].
3333
__Usage:__ `node buckets --help`
3434

3535
```
36-
Usage: node buckets [COMMAND] [ARGS...]
36+
Usage: node buckets COMMAND [ARGS...]
3737
3838
Commands:
3939
40-
create [BUCKET_NAME]
40+
create BUCKET_NAME
4141
list
42-
delete [BUCKET_NAME]
42+
delete BUCKET_NAME
43+
44+
Examples:
45+
46+
node buckets create my-bucket
47+
node buckets list
48+
node buckets delete my-bucket
4349
```
4450

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

5460
```
55-
Usage: node files [COMMAND] [ARGS...]
61+
Usage: node files COMMAND [ARGS...]
5662
5763
Commands:
5864
59-
list [BUCKET_NAME]
60-
listByPrefix [BUCKET_NAME] [PREFIX] [DELIMITER]
61-
upload [BUCKET_NAME] [FILE_NAME]
62-
download [BUCKET_NAME] [SRC_FILE_NAME] [DEST_FILE_NAME]
63-
delete [BUCKET_NAME] [FILE_NAME]
64-
getMetadata [BUCKET_NAME] [FILE_NAME]
65-
makePublic [BUCKET_NAME] [FILE_NAME]
66-
move [BUCKET_NAME] [SRC_FILE_NAME] [DEST_FILE_NAME]
67-
copy [BUCKET_NAME] [SRC_FILE_NAME] [DEST_BUCKET_NAME] [DEST_FILE_NAME]
65+
list BUCKET_NAME
66+
listByPrefix BUCKET_NAME PREFIX [DELIMITER]
67+
upload BUCKET_NAME FILE_NAME
68+
download BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME
69+
delete BUCKET_NAME FILE_NAME
70+
getMetadata BUCKET_NAME FILE_NAME
71+
makePublic BUCKET_NAME FILE_NAME
72+
move BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME
73+
copy BUCKET_NAME SRC_FILE_NAME DEST_BUCKET_NAME DEST_FILE_NAME
74+
75+
Examples:
76+
77+
list my-bucket
78+
listByPrefix my-bucket /some-folder
79+
listByPrefix my-bucket /some-folder -
80+
upload my-bucket ./file.txt
81+
download my-bucket file.txt ./file.txt
82+
delete my-bucket file.txt
83+
getMetadata my-bucket file.txt
84+
makePublic my-bucket file.txt
85+
move my-bucket file.txt file2.txt
86+
copy my-bucket file.txt my-other-bucket file.txt
6887
```
6988

7089
[files_docs]: https://cloud.google.com/storage/docs
7190
[files_code]: files.js
91+
92+
### Encryption
93+
94+
View the [documentation][encryption_docs] or the [source code][encryption_code].
95+
96+
__Usage:__ `node encryption --help`
97+
98+
```
99+
Usage: node encryption COMMAND [ARGS...]
100+
101+
Commands:
102+
103+
generate-encryption-key
104+
upload BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME KEY
105+
download BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME KEY
106+
rotate BUCKET_NAME FILE_NAME OLD_KEY NEW_KEY
107+
108+
Examples:
109+
110+
node encryption generate-encryption-key
111+
node encryption upload my-bucket resources/test.txt file_encrypted.txt QxhqaZEqBGVTW55HhQw9Q=
112+
node encryption download my-bucket file_encrypted.txt ./file.txt QxhqaZEqBGVTW55HhQw9Q=
113+
node encryption rotate my-bucket file_encrypted.txt QxhqaZEqBGVTW55HhQw9Q= SxafpsdfSDFS89sds9Q=
114+
```
115+
116+
[encryption_docs]: https://cloud.google.com/storage/docs
117+
[encryption_code]: encryption.js

storage/encryption.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,23 +162,23 @@ function printUsage () {
162162

163163
// The command-line program
164164
var program = {
165-
generate: generateEncryptionKey,
166-
upload: uploadEncryptedFile,
167-
download: downloadEncryptedFile,
168-
rotate: rotateEncryptionKey,
165+
generateEncryptionKey: generateEncryptionKey,
166+
uploadEncryptedFile: uploadEncryptedFile,
167+
downloadEncryptedFile: downloadEncryptedFile,
168+
rotateEncryptionKey: rotateEncryptionKey,
169169
printUsage: printUsage,
170170

171171
// Executed when this program is run from the command-line
172172
main: function (args, cb) {
173173
var command = args.shift();
174174
if (command === 'generate-encryption-key') {
175-
this.generate(cb);
175+
this.generateEncryptionKey();
176176
} else if (command === 'upload') {
177-
this.upload(args[0], args[1], args[2], args[3], cb);
177+
this.uploadEncryptedFile(args[0], args[1], args[2], args[3], cb);
178178
} else if (command === 'download') {
179-
this.download(args[0], args[1], args[2], args[3], cb);
179+
this.downloadEncryptedFile(args[0], args[1], args[2], args[3], cb);
180180
} else if (command === 'rotate') {
181-
this.rotate(cb);
181+
this.rotateEncryptionKey(cb);
182182
} else {
183183
this.printUsage();
184184
}

storage/files.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -324,17 +324,28 @@ function copyFile (name, srcFileName, destBucketName, destFileName, callback) {
324324

325325
// [START usage]
326326
function printUsage () {
327-
console.log('Usage: node files [COMMAND] [ARGS...]');
327+
console.log('Usage: node files COMMAND [ARGS...]');
328328
console.log('\nCommands:\n');
329-
console.log('\tlist [BUCKET_NAME]');
330-
console.log('\tlistByPrefix [BUCKET_NAME] [PREFIX] [DELIMITER]');
331-
console.log('\tupload [BUCKET_NAME] [FILE_NAME]');
332-
console.log('\tdownload [BUCKET_NAME] [SRC_FILE_NAME] [DEST_FILE_NAME]');
333-
console.log('\tdelete [BUCKET_NAME] [FILE_NAME]');
334-
console.log('\tgetMetadata [BUCKET_NAME] [FILE_NAME]');
335-
console.log('\tmakePublic [BUCKET_NAME] [FILE_NAME]');
336-
console.log('\tmove [BUCKET_NAME] [SRC_FILE_NAME] [DEST_FILE_NAME]');
337-
console.log('\tcopy [BUCKET_NAME] [SRC_FILE_NAME] [DEST_BUCKET_NAME] [DEST_FILE_NAME]');
329+
console.log('\tlist BUCKET_NAME');
330+
console.log('\tlistByPrefix BUCKET_NAME PREFIX [DELIMITER]');
331+
console.log('\tupload BUCKET_NAME FILE_NAME');
332+
console.log('\tdownload BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME');
333+
console.log('\tdelete BUCKET_NAME FILE_NAME');
334+
console.log('\tgetMetadata BUCKET_NAME FILE_NAME');
335+
console.log('\tmakePublic BUCKET_NAME FILE_NAME');
336+
console.log('\tmove BUCKET_NAME SRC_FILE_NAME DEST_FILE_NAME');
337+
console.log('\tcopy BUCKET_NAME SRC_FILE_NAME DEST_BUCKET_NAME DEST_FILE_NAME');
338+
console.log('\nExamples:\n');
339+
console.log('\tlist my-bucket');
340+
console.log('\tlistByPrefix my-bucket /some-folder');
341+
console.log('\tlistByPrefix my-bucket /some-folder -');
342+
console.log('\tupload my-bucket ./file.txt');
343+
console.log('\tdownload my-bucket file.txt ./file.txt');
344+
console.log('\tdelete my-bucket file.txt');
345+
console.log('\tgetMetadata my-bucket file.txt');
346+
console.log('\tmakePublic my-bucket file.txt');
347+
console.log('\tmove my-bucket file.txt file2.txt');
348+
console.log('\tcopy my-bucket file.txt my-other-bucket file.txt');
338349
}
339350
// [END usage]
340351

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
var fs = require('fs');
17+
var path = require('path');
18+
var gcloud = require('gcloud');
19+
var uuid = require('node-uuid');
20+
var storage = gcloud.storage();
21+
var program = require('../encryption');
22+
23+
var bucketName = 'nodejs-docs-samples-test-' + uuid.v4();
24+
var fileName = 'test.txt';
25+
var filePath = path.join(__dirname, '../resources', fileName);
26+
var downloadFilePath = path.join(__dirname, '../resources/downloaded.txt');
27+
28+
describe('storage:encryption', function () {
29+
var key;
30+
before(function (done) {
31+
key = program.generateEncryptionKey();
32+
storage.createBucket(bucketName, done);
33+
});
34+
35+
after(function (done) {
36+
try {
37+
fs.unlinkSync(downloadFilePath);
38+
} catch (err) {
39+
console.log(err);
40+
}
41+
storage.bucket(bucketName).deleteFiles({ force: true }, function (err) {
42+
if (err) {
43+
return done(err);
44+
}
45+
storage.bucket(bucketName).delete(done);
46+
});
47+
});
48+
49+
describe('uploadEncryptedFile', function () {
50+
it('should upload a file', function (done) {
51+
program.uploadEncryptedFile(bucketName, filePath, fileName, key, function (err, file) {
52+
assert.ifError(err);
53+
assert(file);
54+
assert.equal(file.name, fileName);
55+
assert(console.log.calledWith('Uploaded encrypted file: %s', fileName));
56+
done();
57+
});
58+
});
59+
});
60+
61+
describe('downloadEncryptedFile', function () {
62+
it('should download a file', function (done) {
63+
program.downloadEncryptedFile(bucketName, fileName, downloadFilePath, key, function (err) {
64+
assert.ifError(err);
65+
assert.doesNotThrow(function () {
66+
fs.statSync(downloadFilePath);
67+
});
68+
assert(console.log.calledWith('Downloaded encrypted file: %s', downloadFilePath));
69+
done();
70+
});
71+
});
72+
});
73+
});

0 commit comments

Comments
 (0)