|
| 1 | +// Copyright 2023 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 | +// https://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 | +'use strict'; |
| 16 | + |
| 17 | +async function main( |
| 18 | + projectId = 'my-project', |
| 19 | + locationId = 'us-east1', |
| 20 | + keyRingId = 'my-key-ring', |
| 21 | + cryptoKeyId = 'my-imported-key', |
| 22 | + importJobId = 'my-import-job' |
| 23 | +) { |
| 24 | + // [START kms_import_manually_wrapped_key] |
| 25 | + // |
| 26 | + // TODO(developer): Uncomment these variables before running the sample. |
| 27 | + // |
| 28 | + // const projectId = 'my-project'; |
| 29 | + // const locationId = 'us-east1'; |
| 30 | + // const keyRingId = 'my-key-ring'; |
| 31 | + // const cryptoKeyId = 'my-imported-key'; |
| 32 | + // const importJobId = 'my-import-job'; |
| 33 | + |
| 34 | + // Imports the Cloud KMS library |
| 35 | + const {KeyManagementServiceClient} = require('@google-cloud/kms'); |
| 36 | + |
| 37 | + // Instantiates a client |
| 38 | + const client = new KeyManagementServiceClient(); |
| 39 | + |
| 40 | + // Build the crypto key and importjob resource names |
| 41 | + const cryptoKeyName = client.cryptoKeyPath( |
| 42 | + projectId, |
| 43 | + locationId, |
| 44 | + keyRingId, |
| 45 | + cryptoKeyId |
| 46 | + ); |
| 47 | + const importJobName = client.importJobPath( |
| 48 | + projectId, |
| 49 | + locationId, |
| 50 | + keyRingId, |
| 51 | + importJobId |
| 52 | + ); |
| 53 | + |
| 54 | + async function wrapAndImportKey() { |
| 55 | + // Generate a 32-byte key to import. |
| 56 | + const crypto = require('crypto'); |
| 57 | + const targetKey = crypto.randomBytes(32); |
| 58 | + |
| 59 | + const [importJob] = await client.getImportJob({name: importJobName}); |
| 60 | + |
| 61 | + // Wrap the target key using the import job key |
| 62 | + const wrappedTargetKey = crypto.publicEncrypt( |
| 63 | + { |
| 64 | + key: importJob.publicKey.pem, |
| 65 | + oaepHash: 'sha256', |
| 66 | + padding: crypto.constants.RSA_PKCS1_OAEP_PADDING, |
| 67 | + }, |
| 68 | + targetKey |
| 69 | + ); |
| 70 | + |
| 71 | + // Import the target key version |
| 72 | + const [version] = await client.importCryptoKeyVersion({ |
| 73 | + parent: cryptoKeyName, |
| 74 | + importJob: importJobName, |
| 75 | + algorithm: 'GOOGLE_SYMMETRIC_ENCRYPTION', |
| 76 | + wrappedKey: wrappedTargetKey, |
| 77 | + }); |
| 78 | + |
| 79 | + console.log(`Imported key version: ${version.name}`); |
| 80 | + return version; |
| 81 | + } |
| 82 | + |
| 83 | + return wrapAndImportKey(); |
| 84 | + // [END kms_import_manually_wrapped_key] |
| 85 | +} |
| 86 | +module.exports.main = main; |
| 87 | + |
| 88 | +/* c8 ignore next 10 */ |
| 89 | +if (require.main === module) { |
| 90 | + main(...process.argv.slice(2)).catch(err => { |
| 91 | + console.error(err.message); |
| 92 | + process.exitCode = 1; |
| 93 | + }); |
| 94 | + process.on('unhandledRejection', err => { |
| 95 | + console.error(err.message); |
| 96 | + process.exitCode = 1; |
| 97 | + }); |
| 98 | +} |
0 commit comments