Skip to content

Commit

Permalink
feat(kms): add samples for new rng and hmac kms apis (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethvargo authored Aug 16, 2021
1 parent 8f8f5c4 commit 35b4610
Show file tree
Hide file tree
Showing 36 changed files with 689 additions and 93 deletions.
16 changes: 13 additions & 3 deletions kms/createKeyAsymmetricDecrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ async function main(
versionTemplate: {
algorithm: 'RSA_DECRYPT_OAEP_2048_SHA256',
},

// Optional: customize how long key versions should be kept before
// destroying.
destroyScheduledDuration: {seconds: 60 * 60 * 24},
},
});

Expand All @@ -59,8 +63,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
16 changes: 13 additions & 3 deletions kms/createKeyAsymmetricSign.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ async function main(
versionTemplate: {
algorithm: 'RSA_SIGN_PKCS1_2048_SHA256',
},

// Optional: customize how long key versions should be kept before
// destroying.
destroyScheduledDuration: {seconds: 60 * 60 * 24},
},
});

Expand All @@ -59,8 +63,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
16 changes: 13 additions & 3 deletions kms/createKeyHsm.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ async function main(
algorithm: 'GOOGLE_SYMMETRIC_ENCRYPTION',
protectionLevel: 'HSM',
},

// Optional: customize how long key versions should be kept before
// destroying.
destroyScheduledDuration: {seconds: 60 * 60 * 24},
},
});

Expand All @@ -60,8 +64,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
12 changes: 9 additions & 3 deletions kms/createKeyLabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
76 changes: 76 additions & 0 deletions kms/createKeyMac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2021 Google LLC
//
// 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
//
// https://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';

async function main(
projectId = 'my-project',
locationId = 'us-east1',
keyRingId = 'my-key-ring',
id = 'my-mac-key'
) {
// [START kms_create_key_mac]
//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-east1';
// const keyRingId = 'my-key-ring';
// const id = 'my-mac-key';

// Imports the Cloud KMS library
const {KeyManagementServiceClient} = require('@google-cloud/kms');

// Instantiates a client
const client = new KeyManagementServiceClient();

// Build the parent key ring name
const keyRingName = client.keyRingPath(projectId, locationId, keyRingId);

async function createKeyMac() {
const [key] = await client.createCryptoKey({
parent: keyRingName,
cryptoKeyId: id,
cryptoKey: {
purpose: 'MAC',
versionTemplate: {
algorithm: 'HMAC_SHA256',
},

// Optional: customize how long key versions should be kept before
// destroying.
destroyScheduledDuration: {seconds: 60 * 60 * 24},
},
});

console.log(`Created mac key: ${key.name}`);
return key;
}

return createKeyMac();
// [END kms_create_key_mac]
}
module.exports.main = main;

/* c8 ignore next 10 */
if (require.main === module) {
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
12 changes: 9 additions & 3 deletions kms/createKeyRing.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
12 changes: 9 additions & 3 deletions kms/createKeyRotationSchedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
12 changes: 9 additions & 3 deletions kms/createKeySymmetricEncryptDecrypt.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
12 changes: 9 additions & 3 deletions kms/createKeyVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
12 changes: 9 additions & 3 deletions kms/decryptAsymmetric.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
12 changes: 9 additions & 3 deletions kms/decryptSymmetric.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
12 changes: 9 additions & 3 deletions kms/destroyKeyVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
12 changes: 9 additions & 3 deletions kms/disableKeyVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
12 changes: 9 additions & 3 deletions kms/enableKeyVersion.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
12 changes: 9 additions & 3 deletions kms/encryptAsymmetric.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
12 changes: 9 additions & 3 deletions kms/encryptSymmetric.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,14 @@ async function main(
}
module.exports.main = main;

/* c8 ignore next 4 */
/* c8 ignore next 10 */
if (require.main === module) {
const args = process.argv.slice(2);
main(...args).catch(console.error);
main(...process.argv.slice(2)).catch(err => {
console.error(err.message);
process.exitCode = 1;
});
process.on('unhandledRejection', err => {
console.error(err.message);
process.exitCode = 1;
});
}
Loading

0 comments on commit 35b4610

Please sign in to comment.