MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / main

Function main

kms/createKeyRotationSchedule.js:17–69  ·  view source on GitHub ↗
(
  projectId = 'my-project',
  locationId = 'us-east1',
  keyRingId = 'my-key-ring',
  id = 'my-rotating-encryption-key'
)

Source from the content-addressed store, hash-verified

15'use strict';
16
17async function main(
18 projectId = 'my-project',
19 locationId = 'us-east1',
20 keyRingId = 'my-key-ring',
21 id = 'my-rotating-encryption-key'
22) {
23 // [START kms_create_key_rotation_schedule]
24 //
25 // TODO(developer): Uncomment these variables before running the sample.
26 //
27 // const projectId = 'my-project';
28 // const locationId = 'us-east1';
29 // const keyRingId = 'my-key-ring';
30 // const id = 'my-rotating-encryption-key';
31
32 // Imports the Cloud KMS library
33 const {KeyManagementServiceClient} = require('@google-cloud/kms');
34
35 // Instantiates a client
36 const client = new KeyManagementServiceClient();
37
38 // Build the parent key ring name
39 const keyRingName = client.keyRingPath(projectId, locationId, keyRingId);
40
41 async function createKeyRotationSchedule() {
42 const [key] = await client.createCryptoKey({
43 parent: keyRingName,
44 cryptoKeyId: id,
45 cryptoKey: {
46 purpose: 'ENCRYPT_DECRYPT',
47 versionTemplate: {
48 algorithm: 'GOOGLE_SYMMETRIC_ENCRYPTION',
49 },
50
51 // Rotate the key every 30 days.
52 rotationPeriod: {
53 seconds: 60 * 60 * 24 * 30,
54 },
55
56 // Start the first rotation in 24 hours.
57 nextRotationTime: {
58 seconds: new Date().getTime() / 1000 + 60 * 60 * 24,
59 },
60 },
61 });
62
63 console.log(`Created rotating key: ${key.name}`);
64 return key;
65 }
66
67 return createKeyRotationSchedule();
68 // [END kms_create_key_rotation_schedule]
69}
70module.exports.main = main;
71
72/* c8 ignore next 10 */

Callers 1

Calls 1

Tested by

no test coverage detected