-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: customer-managed encryption keys (#1274)
- Loading branch information
Showing
11 changed files
with
565 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* 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 | ||
* | ||
* 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'; | ||
|
||
async function createBackupWithEncryptionKey( | ||
instanceId, | ||
databaseId, | ||
backupId, | ||
projectId, | ||
keyName | ||
) { | ||
// [START spanner_create_backup_with_encryption_key] | ||
// Imports the Google Cloud client library and precise date library | ||
const {Spanner} = require('@google-cloud/spanner'); | ||
const {PreciseDate} = require('@google-cloud/precise-date'); | ||
|
||
/** | ||
* TODO(developer): Uncomment the following lines before running the sample. | ||
*/ | ||
// const projectId = 'my-project-id'; | ||
// const instanceId = 'my-instance'; | ||
// const databaseId = 'my-database'; | ||
// const backupId = 'my-backup'; | ||
// const versionTime = Date.now() - 1000 * 60 * 60 * 24; // One day ago | ||
// const keyName = | ||
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key'; | ||
|
||
// Creates a client | ||
const spanner = new Spanner({ | ||
projectId: projectId, | ||
}); | ||
|
||
// Gets a reference to a Cloud Spanner instance and database | ||
const instance = spanner.instance(instanceId); | ||
const database = instance.database(databaseId); | ||
|
||
const backup = instance.backup(backupId); | ||
|
||
// Creates a new backup of the database | ||
try { | ||
console.log(`Creating backup of database ${database.formattedName_}.`); | ||
const databasePath = database.formattedName_; | ||
// Expire backup 14 days in the future | ||
const expireTime = Date.now() + 1000 * 60 * 60 * 24 * 14; | ||
// Create a backup of the state of the database at the current time. | ||
const [, operation] = await backup.create({ | ||
databasePath: databasePath, | ||
expireTime: expireTime, | ||
encryptionConfig: { | ||
encryptionType: 'CUSTOMER_MANAGED_ENCRYPTION', | ||
kmsKeyName: keyName, | ||
}, | ||
}); | ||
|
||
console.log(`Waiting for backup ${backup.formattedName_} to complete...`); | ||
await operation.promise(); | ||
|
||
// Verify backup is ready | ||
const [backupInfo] = await backup.getMetadata(); | ||
if (backupInfo.state === 'READY') { | ||
console.log( | ||
`Backup ${backupInfo.name} of size ` + | ||
`${backupInfo.sizeBytes} bytes was created at ` + | ||
`${new PreciseDate(backupInfo.createTime).toISOString()} ` + | ||
`using encryption key ${backupInfo.encryptionInfo.kmsKeyVersion}` | ||
); | ||
} else { | ||
console.error('ERROR: Backup is not ready.'); | ||
} | ||
} catch (err) { | ||
console.error('ERROR:', err); | ||
} finally { | ||
// Close the database when finished. | ||
await database.close(); | ||
} | ||
// [END spanner_create_backup_with_encryption_key] | ||
} | ||
|
||
module.exports.createBackupWithEncryptionKey = createBackupWithEncryptionKey; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/** | ||
* 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 | ||
* | ||
* 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'; | ||
|
||
async function restoreBackupWithEncryptionKey( | ||
instanceId, | ||
databaseId, | ||
backupId, | ||
projectId, | ||
keyName | ||
) { | ||
// [START spanner_restore_backup_with_encryption_key] | ||
// Imports the Google Cloud client library and precise date library | ||
const {Spanner} = require('@google-cloud/spanner'); | ||
|
||
/** | ||
* TODO(developer): Uncomment the following lines before running the sample. | ||
*/ | ||
// const projectId = 'my-project-id'; | ||
// const instanceId = 'my-instance'; | ||
// const databaseId = 'my-database'; | ||
// const backupId = 'my-backup'; | ||
// const keyName = | ||
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key'; | ||
|
||
// Creates a client | ||
const spanner = new Spanner({ | ||
projectId: projectId, | ||
}); | ||
|
||
// Gets a reference to a Cloud Spanner instance and database | ||
const instance = spanner.instance(instanceId); | ||
const database = instance.database(databaseId); | ||
|
||
// Restore the database | ||
console.log( | ||
`Restoring database ${database.formattedName_} from backup ${backupId}.` | ||
); | ||
const [, restoreOperation] = await database.restore( | ||
`projects/${projectId}/instances/${instanceId}/backups/${backupId}`, | ||
{ | ||
encryptionConfig: { | ||
encryptionType: 'CUSTOMER_MANAGED_ENCRYPTION', | ||
kmsKeyName: keyName, | ||
}, | ||
} | ||
); | ||
|
||
// Wait for restore to complete | ||
console.log('Waiting for database restore to complete...'); | ||
await restoreOperation.promise(); | ||
|
||
console.log('Database restored from backup.'); | ||
const restoreInfo = await database.getRestoreInfo(); | ||
const [data] = await database.get(); | ||
console.log( | ||
`Database ${restoreInfo.backupInfo.sourceDatabase} was restored ` + | ||
`to ${databaseId} from backup ${restoreInfo.backupInfo.backup} ` + | ||
`using encryption key ${data.metadata.encryptionConfig.kmsKeyName}.` | ||
); | ||
// [END spanner_restore_backup_with_encryption_key] | ||
} | ||
|
||
module.exports.restoreBackupWithEncryptionKey = restoreBackupWithEncryptionKey; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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 | ||
// | ||
// 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'; | ||
|
||
async function createDatabaseWithEncryptionKey( | ||
instanceId, | ||
databaseId, | ||
projectId, | ||
keyName | ||
) { | ||
// [START spanner_create_database_with_encryption_key] | ||
// Imports the Google Cloud client library | ||
const {Spanner} = require('@google-cloud/spanner'); | ||
|
||
/** | ||
* TODO(developer): Uncomment the following lines before running the sample. | ||
*/ | ||
// const projectId = 'my-project-id'; | ||
// const instanceId = 'my-instance'; | ||
// const databaseId = 'my-database'; | ||
// const keyName = | ||
// 'projects/my-project-id/my-region/keyRings/my-key-ring/cryptoKeys/my-key'; | ||
|
||
// Creates a client | ||
const spanner = new Spanner({ | ||
projectId: projectId, | ||
}); | ||
|
||
// Gets a reference to a Cloud Spanner instance | ||
const instance = spanner.instance(instanceId); | ||
|
||
const request = { | ||
encryptionConfig: { | ||
kmsKeyName: keyName, | ||
}, | ||
}; | ||
|
||
// Creates a database | ||
const [database, operation] = await instance.createDatabase( | ||
databaseId, | ||
request | ||
); | ||
|
||
console.log(`Waiting for operation on ${database.id} to complete...`); | ||
await operation.promise(); | ||
|
||
console.log(`Created database ${databaseId} on instance ${instanceId}.`); | ||
|
||
// Get encryption key | ||
const [data] = await database.get(); | ||
|
||
console.log( | ||
`Database encrypted with key ${data.metadata.encryptionConfig.kmsKeyName}.` | ||
); | ||
// [END spanner_create_database_with_encryption_key] | ||
} | ||
|
||
module.exports.createDatabaseWithEncryptionKey = createDatabaseWithEncryptionKey; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.