Skip to content

Commit 9cd9f4f

Browse files
committed
Add KMS quickstart.
1 parent f0f1e17 commit 9cd9f4f

File tree

7 files changed

+660
-12
lines changed

7 files changed

+660
-12
lines changed

kms/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<img src="https://avatars2.githubusercontent.com/u/2810941?v=3&s=96" alt="Google Cloud Platform logo" title="Google Cloud Platform" align="right" height="96" width="96"/>
2+
3+
# Google Cloud KMS API Node.js Samples
4+
5+
The [Cloud KMS API][kms_docs] is a service that allows you to keep encryption
6+
keys centrally in the cloud, for direct use by cloud services.
7+
8+
[kms_docs]: https://cloud.google.com/kms/docs/
9+
10+
## Table of Contents
11+
12+
* [Setup](#setup)
13+
* [Samples](#samples)
14+
* [Quickstart](#quickstart)
15+
16+
## Setup
17+
18+
1. Read [Prerequisites][prereq] and [How to run a sample][run] first.
19+
1. Install dependencies:
20+
21+
npm install
22+
23+
[prereq]: ../README.md#prerequisities
24+
[run]: ../README.md#how-to-run-a-sample
25+
26+
## Samples
27+
28+
### Quickstart
29+
30+
View the [documentation][quickstart_docs] or the [source code][quickstart_code].
31+
32+
Run the sample:
33+
34+
node quickstart.js
35+
36+
[quickstart_docs]: https://cloud.google.com/kms/docs
37+
[quickstart_code]: hostedmodels.js

kms/package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "nodejs-docs-samples-kms",
3+
"version": "0.0.1",
4+
"private": true,
5+
"license": "Apache Version 2.0",
6+
"author": "Google Inc.",
7+
"scripts": {
8+
"test": "cd ..; npm run st -- --verbose kms/system-test/*.test.js"
9+
},
10+
"dependencies": {
11+
"googleapis": "16.1.0"
12+
}
13+
}

kms/quickstart.js

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
// [START kms_quickstart]
19+
// Imports the Google APIs client library
20+
const google = require('googleapis');
21+
22+
// Your Google Cloud Platform project ID
23+
const projectId = 'YOUR_PROJECT_ID';
24+
25+
// The "global" zone will list all keys. It can be a specific zone if desired.
26+
const zone = 'global';
27+
28+
// Acquires credentials
29+
google.auth.getApplicationDefault((err, authClient) => {
30+
if (err) {
31+
console.error('Failed to acquire credentials');
32+
return;
33+
}
34+
35+
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
36+
authClient = authClient.createScoped([
37+
'https://www.googleapis.com/auth/cloud-platform'
38+
]);
39+
}
40+
41+
// Instantiates an authorized client
42+
const cloudkms = google.cloudkms({
43+
version: 'v1beta1',
44+
auth: authClient
45+
});
46+
const params = {
47+
parent: `projects/${projectId}/locations/${zone}`
48+
};
49+
50+
// Lists key rings
51+
cloudkms.projects.locations.keyRings.list(params, (err, result) => {
52+
if (err) {
53+
console.error(err);
54+
return;
55+
}
56+
57+
const keyRings = result.keyRings || [];
58+
59+
if (keyRings.length) {
60+
console.log('Key kings:');
61+
result.keyRings.forEach((keyRing) => console.log(keyRing.name));
62+
} else {
63+
console.log(`No key rings found.`);
64+
}
65+
});
66+
});
67+
// [END kms_quickstart]

kms/system-test/quickstart.test.js

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/**
2+
* Copyright 2017, Google, Inc.
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS,
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
'use strict';
17+
18+
require(`../../system-test/_setup`);
19+
20+
const proxyquire = require(`proxyquire`).noPreserveCache();
21+
const google = proxyquire(`googleapis`, {});
22+
23+
function list (callback) {
24+
google.auth.getApplicationDefault((err, authClient) => {
25+
if (err) {
26+
callback(err);
27+
return;
28+
}
29+
30+
if (authClient.createScopedRequired && authClient.createScopedRequired()) {
31+
authClient = authClient.createScoped([
32+
'https://www.googleapis.com/auth/cloud-platform'
33+
]);
34+
}
35+
36+
const cloudkms = google.cloudkms({
37+
version: 'v1beta1',
38+
auth: authClient
39+
});
40+
const params = {
41+
parent: `projects/${process.env.GCLOUD_PROJECT}/locations/global`
42+
};
43+
44+
cloudkms.projects.locations.keyRings.list(params, callback);
45+
});
46+
}
47+
48+
test.beforeEach(stubConsole);
49+
test.afterEach.always(restoreConsole);
50+
51+
test.cb(`should list key rings`, (t) => {
52+
const googleapisMock = {
53+
cloudkms () {
54+
return {
55+
projects: {
56+
locations: {
57+
keyRings: {
58+
list (params, callback) {
59+
list((err, result) => {
60+
if (err) {
61+
t.end(err);
62+
return;
63+
}
64+
callback(err, result);
65+
66+
setTimeout(() => {
67+
try {
68+
t.true(console.log.called);
69+
if (result && result.keyRings && result.keyRings.length) {
70+
t.deepEqual(console.log.getCall(0).args, [`Key rings:`]);
71+
} else {
72+
t.deepEqual(console.log.getCall(0).args, [`No key rings found.`]);
73+
}
74+
t.end();
75+
} catch (err) {
76+
t.end(err);
77+
}
78+
}, 200);
79+
});
80+
}
81+
}
82+
}
83+
}
84+
};
85+
}
86+
};
87+
88+
proxyquire(`../quickstart`, {
89+
'googleapis': googleapisMock
90+
});
91+
});

0 commit comments

Comments
 (0)