Skip to content

Add KMS quickstart. #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions kms/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<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"/>

# Google Cloud KMS API Node.js Samples

The [Cloud KMS API][kms_docs] is a service that allows you to keep encryption
keys centrally in the cloud, for direct use by cloud services.

[kms_docs]: https://cloud.google.com/kms/docs/

## Table of Contents

* [Setup](#setup)
* [Samples](#samples)
* [Quickstart](#quickstart)

## Setup

1. Read [Prerequisites][prereq] and [How to run a sample][run] first.
1. Install dependencies:

npm install

[prereq]: ../README.md#prerequisities
[run]: ../README.md#how-to-run-a-sample

## Samples

### Quickstart

View the [documentation][quickstart_docs] or the [source code][quickstart_code].

Run the sample:

node quickstart.js

[quickstart_docs]: https://cloud.google.com/kms/docs
[quickstart_code]: hostedmodels.js
13 changes: 13 additions & 0 deletions kms/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "nodejs-docs-samples-kms",
"version": "0.0.1",
"private": true,
"license": "Apache Version 2.0",
"author": "Google Inc.",
"scripts": {
"test": "cd ..; npm run st -- --verbose kms/system-test/*.test.js"
},
"dependencies": {
"googleapis": "16.1.0"
}
}
67 changes: 67 additions & 0 deletions kms/quickstart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2017, 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';

// [START kms_quickstart]
// Imports the Google APIs client library
const google = require('googleapis');

// Your Google Cloud Platform project ID
const projectId = 'YOUR_PROJECT_ID';

// The "global" zone will list all keys. It can be a specific zone if desired.
const zone = 'global';

// Acquires credentials
google.auth.getApplicationDefault((err, authClient) => {
if (err) {
console.error('Failed to acquire credentials');
return;
}

if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/cloud-platform'
]);
}

// Instantiates an authorized client
const cloudkms = google.cloudkms({
version: 'v1beta1',
auth: authClient
});
const params = {
parent: `projects/${projectId}/locations/${zone}`
};

// Lists key rings
cloudkms.projects.locations.keyRings.list(params, (err, result) => {
if (err) {
console.error(err);
return;
}

const keyRings = result.keyRings || [];

if (keyRings.length) {
console.log('Key kings:');
result.keyRings.forEach((keyRing) => console.log(keyRing.name));
} else {
console.log(`No key rings found.`);
}
});
});
// [END kms_quickstart]
91 changes: 91 additions & 0 deletions kms/system-test/quickstart.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright 2017, 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';

require(`../../system-test/_setup`);

const proxyquire = require(`proxyquire`).noPreserveCache();
const google = proxyquire(`googleapis`, {});

function list (callback) {
google.auth.getApplicationDefault((err, authClient) => {
if (err) {
callback(err);
return;
}

if (authClient.createScopedRequired && authClient.createScopedRequired()) {
authClient = authClient.createScoped([
'https://www.googleapis.com/auth/cloud-platform'
]);
}

const cloudkms = google.cloudkms({
version: 'v1beta1',
auth: authClient
});
const params = {
parent: `projects/${process.env.GCLOUD_PROJECT}/locations/global`
};

cloudkms.projects.locations.keyRings.list(params, callback);
});
}

test.beforeEach(stubConsole);
test.afterEach.always(restoreConsole);

test.cb(`should list key rings`, (t) => {
const googleapisMock = {
cloudkms () {
return {
projects: {
locations: {
keyRings: {
list (params, callback) {
list((err, result) => {
if (err) {
t.end(err);
return;
}
callback(err, result);

setTimeout(() => {
try {
t.true(console.log.called);
if (result && result.keyRings && result.keyRings.length) {
t.deepEqual(console.log.getCall(0).args, [`Key rings:`]);
} else {
t.deepEqual(console.log.getCall(0).args, [`No key rings found.`]);
}
t.end();
} catch (err) {
t.end(err);
}
}, 200);
});
}
}
}
}
};
}
};

proxyquire(`../quickstart`, {
'googleapis': googleapisMock
});
});
Loading