Skip to content

Commit 51bc8a7

Browse files
authored
feat: allow multiple KMS keys to create CMEK database/backup (#2099)
Add kms_key_names field to create database/create backup code snippets.
1 parent 2a19ef1 commit 51bc8a7

7 files changed

+618
-12
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,17 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
9191
| --------------------------- | --------------------------------- | ------ |
9292
| Add and drop new database role | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/add-and-drop-new-database-role.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/add-and-drop-new-database-role.js,samples/README.md) |
9393
| Backups-cancel | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-cancel.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-cancel.js,samples/README.md) |
94+
| Copies a source backup | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-copy-with-multiple-kms-keys.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-copy-with-multiple-kms-keys.js,samples/README.md) |
9495
| Copies a source backup | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-copy.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-copy.js,samples/README.md) |
9596
| Backups-create-with-encryption-key | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-create-with-encryption-key.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-create-with-encryption-key.js,samples/README.md) |
97+
| Backups-create-with-multiple-kms-keys | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-create-with-multiple-kms-keys.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-create-with-multiple-kms-keys.js,samples/README.md) |
9698
| Backups-create | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-create.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-create.js,samples/README.md) |
9799
| Backups-delete | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-delete.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-delete.js,samples/README.md) |
98100
| Backups-get-database-operations | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-get-database-operations.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-get-database-operations.js,samples/README.md) |
99101
| Backups-get-operations | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-get-operations.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-get-operations.js,samples/README.md) |
100102
| Backups-get | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-get.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-get.js,samples/README.md) |
101103
| Backups-restore-with-encryption-key | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-restore-with-encryption-key.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-restore-with-encryption-key.js,samples/README.md) |
104+
| Backups-restore-with-multiple-kms-keys | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-restore-with-multiple-kms-keys.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-restore-with-multiple-kms-keys.js,samples/README.md) |
102105
| Backups-restore | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-restore.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-restore.js,samples/README.md) |
103106
| Backups-update | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-update.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-update.js,samples/README.md) |
104107
| Backups | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups.js,samples/README.md) |
@@ -109,6 +112,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/nodejs-spanner/tre
109112
| CRUD | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/crud.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/crud.js,samples/README.md) |
110113
| Creates a new database with a specific default leader | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-default-leader.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-create-with-default-leader.js,samples/README.md) |
111114
| Database-create-with-encryption-key | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-encryption-key.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-create-with-encryption-key.js,samples/README.md) |
115+
| Database-create-with-multiple-kms-keys | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-multiple-kms-keys.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-create-with-multiple-kms-keys.js,samples/README.md) |
112116
| Database-create-with-version-retention-period | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-version-retention-period.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-create-with-version-retention-period.js,samples/README.md) |
113117
| Gets the schema definition of an existing database | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-get-ddl.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-get-ddl.js,samples/README.md) |
114118
| Gets the default leader option of an existing database | [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-get-default-leader.js) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-get-default-leader.js,samples/README.md) |

samples/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ and automatic, synchronous replication for high availability.
1717
* [Add and drop new database role](#add-and-drop-new-database-role)
1818
* [Backups-cancel](#backups-cancel)
1919
* [Copies a source backup](#copies-a-source-backup)
20+
* [Copies a source backup](#copies-a-source-backup)
2021
* [Backups-create-with-encryption-key](#backups-create-with-encryption-key)
22+
* [Backups-create-with-multiple-kms-keys](#backups-create-with-multiple-kms-keys)
2123
* [Backups-create](#backups-create)
2224
* [Backups-delete](#backups-delete)
2325
* [Backups-get-database-operations](#backups-get-database-operations)
2426
* [Backups-get-operations](#backups-get-operations)
2527
* [Backups-get](#backups-get)
2628
* [Backups-restore-with-encryption-key](#backups-restore-with-encryption-key)
29+
* [Backups-restore-with-multiple-kms-keys](#backups-restore-with-multiple-kms-keys)
2730
* [Backups-restore](#backups-restore)
2831
* [Backups-update](#backups-update)
2932
* [Backups](#backups)
@@ -34,6 +37,7 @@ and automatic, synchronous replication for high availability.
3437
* [CRUD](#crud)
3538
* [Creates a new database with a specific default leader](#creates-a-new-database-with-a-specific-default-leader)
3639
* [Database-create-with-encryption-key](#database-create-with-encryption-key)
40+
* [Database-create-with-multiple-kms-keys](#database-create-with-multiple-kms-keys)
3741
* [Database-create-with-version-retention-period](#database-create-with-version-retention-period)
3842
* [Gets the schema definition of an existing database](#gets-the-schema-definition-of-an-existing-database)
3943
* [Gets the default leader option of an existing database](#gets-the-default-leader-option-of-an-existing-database)
@@ -176,6 +180,23 @@ __Usage:__
176180

177181

178182

183+
### Copies a source backup
184+
185+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-copy-with-multiple-kms-keys.js).
186+
187+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-copy-with-multiple-kms-keys.js,samples/README.md)
188+
189+
__Usage:__
190+
191+
192+
`node spannerCopyBackup <INSTANCE_ID> <COPY_BACKUP_ID> <SOURCE_BACKUP_ID> <PROJECT_ID>`
193+
194+
195+
-----
196+
197+
198+
199+
179200
### Copies a source backup
180201

181202
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-copy.js).
@@ -210,6 +231,23 @@ __Usage:__
210231

211232

212233

234+
### Backups-create-with-multiple-kms-keys
235+
236+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-create-with-multiple-kms-keys.js).
237+
238+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-create-with-multiple-kms-keys.js,samples/README.md)
239+
240+
__Usage:__
241+
242+
243+
`node samples/backups-create-with-multiple-kms-keys.js`
244+
245+
246+
-----
247+
248+
249+
250+
213251
### Backups-create
214252

215253
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-create.js).
@@ -312,6 +350,23 @@ __Usage:__
312350

313351

314352

353+
### Backups-restore-with-multiple-kms-keys
354+
355+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-restore-with-multiple-kms-keys.js).
356+
357+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/backups-restore-with-multiple-kms-keys.js,samples/README.md)
358+
359+
__Usage:__
360+
361+
362+
`node samples/backups-restore-with-multiple-kms-keys.js`
363+
364+
365+
-----
366+
367+
368+
369+
315370
### Backups-restore
316371

317372
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/backups-restore.js).
@@ -482,6 +537,23 @@ __Usage:__
482537

483538

484539

540+
### Database-create-with-multiple-kms-keys
541+
542+
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-multiple-kms-keys.js).
543+
544+
[![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/nodejs-spanner&page=editor&open_in_editor=samples/database-create-with-multiple-kms-keys.js,samples/README.md)
545+
546+
__Usage:__
547+
548+
549+
`node samples/database-create-with-multiple-kms-keys.js`
550+
551+
552+
-----
553+
554+
555+
556+
485557
### Database-create-with-version-retention-period
486558

487559
View the [source code](https://github.com/googleapis/nodejs-spanner/blob/main/samples/database-create-with-version-retention-period.js).
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2024 Google LLC
2+
//
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+
// sample-metadata:
16+
// title: Copies a source backup
17+
// usage: node spannerCopyBackup <INSTANCE_ID> <COPY_BACKUP_ID> <SOURCE_BACKUP_ID> <PROJECT_ID>
18+
19+
'use strict';
20+
21+
function main(
22+
instanceId = 'my-instance',
23+
backupId = 'my-backup',
24+
sourceBackupPath = 'projects/my-project-id/instances/my-source-instance/backups/my-source-backup',
25+
projectId = 'my-project-id',
26+
kmsKeyNames = 'key1,key2'
27+
) {
28+
// [START spanner_copy_backup_with_MR_CMEK]
29+
/**
30+
* TODO(developer): Uncomment these variables before running the sample.
31+
*/
32+
// const instanceId = 'my-instance';
33+
// const backupId = 'my-backup',
34+
// const sourceBackupPath = 'projects/my-project-id/instances/my-source-instance/backups/my-source-backup',
35+
// const projectId = 'my-project-id';
36+
// const kmsKeyNames =
37+
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key1,
38+
// projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key2';
39+
40+
// Imports the Google Cloud Spanner client library
41+
const {Spanner} = require('@google-cloud/spanner');
42+
const {PreciseDate} = require('@google-cloud/precise-date');
43+
44+
// Creates a client
45+
const spanner = new Spanner({
46+
projectId: projectId,
47+
});
48+
49+
// Gets a reference to a Cloud Spanner Database Admin Client object
50+
const databaseAdminClient = spanner.getDatabaseAdminClient();
51+
52+
async function spannerCopyBackupWithMultipleKmsKeys() {
53+
// Expire copy backup 14 days in the future
54+
const expireTime = Spanner.timestamp(
55+
Date.now() + 1000 * 60 * 60 * 24 * 14
56+
).toStruct();
57+
58+
// Copy the source backup
59+
try {
60+
console.log(`Creating copy of the source backup ${sourceBackupPath}.`);
61+
const [operation] = await databaseAdminClient.copyBackup({
62+
parent: databaseAdminClient.instancePath(projectId, instanceId),
63+
sourceBackup: sourceBackupPath,
64+
backupId: backupId,
65+
expireTime: expireTime,
66+
kmsKeyNames: kmsKeyNames.split(','),
67+
});
68+
69+
console.log(
70+
`Waiting for backup copy ${databaseAdminClient.backupPath(
71+
projectId,
72+
instanceId,
73+
backupId
74+
)} to complete...`
75+
);
76+
await operation.promise();
77+
78+
// Verify the copy backup is ready
79+
const [copyBackup] = await databaseAdminClient.getBackup({
80+
name: databaseAdminClient.backupPath(projectId, instanceId, backupId),
81+
});
82+
83+
if (copyBackup.state === 'READY') {
84+
console.log(
85+
`Backup copy ${copyBackup.name} of size ` +
86+
`${copyBackup.sizeBytes} bytes was created at ` +
87+
`${new PreciseDate(copyBackup.createTime).toISOString()} ` +
88+
'with version time ' +
89+
`${new PreciseDate(copyBackup.versionTime).toISOString()}`
90+
);
91+
} else {
92+
console.error('ERROR: Copy of backup is not ready.');
93+
}
94+
} catch (err) {
95+
console.error('ERROR:', err);
96+
}
97+
}
98+
spannerCopyBackupWithMultipleKmsKeys();
99+
// [END spanner_copy_backup_with_MR_CMEK]
100+
}
101+
process.on('unhandledRejection', err => {
102+
console.error(err.message);
103+
process.exitCode = 1;
104+
});
105+
main(...process.argv.slice(2));

0 commit comments

Comments
 (0)