Skip to content

Commit 5701105

Browse files
bdhessgcf-owl-bot[bot]Sita04pattishin
authored
feat(kms): add import sample (#3574)
* feat(kms): add import sample * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md * chore(kms): update copyright years * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Sita Lakshmi Sangameswaran <sitalakshmi@google.com> Co-authored-by: Patti Shin <pattishin@users.noreply.github.com>
1 parent 7335e5f commit 5701105

6 files changed

Lines changed: 450 additions & 1 deletion

File tree

kms/checkStateImportJob.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
importJobId = 'my-import-job'
22+
) {
23+
// [START kms_check_state_import_job]
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 importJobId = 'my-import-job';
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 import job name
39+
const importJobName = client.importJobPath(
40+
projectId,
41+
locationId,
42+
keyRingId,
43+
importJobId
44+
);
45+
46+
async function checkStateImportJob() {
47+
const [importJob] = await client.getImportJob({
48+
name: importJobName,
49+
});
50+
51+
console.log(
52+
`Current state of import job ${importJob.name}: ${importJob.state}`
53+
);
54+
return importJob;
55+
}
56+
57+
return checkStateImportJob();
58+
// [END kms_check_state_import_job]
59+
}
60+
module.exports.main = main;
61+
62+
/* c8 ignore next 10 */
63+
if (require.main === module) {
64+
main(...process.argv.slice(2)).catch(err => {
65+
console.error(err.message);
66+
process.exitCode = 1;
67+
});
68+
process.on('unhandledRejection', err => {
69+
console.error(err.message);
70+
process.exitCode = 1;
71+
});
72+
}

kms/checkStateImportedKey.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
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+
cryptoKeyVersionId = '1'
23+
) {
24+
// [START kms_check_state_imported_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 cryptoKeyVersionId = '1';
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 key version name
41+
const keyVersionName = client.cryptoKeyVersionPath(
42+
projectId,
43+
locationId,
44+
keyRingId,
45+
cryptoKeyId,
46+
cryptoKeyVersionId
47+
);
48+
49+
async function checkStateCryptoKeyVersion() {
50+
const [keyVersion] = await client.getCryptoKeyVersion({
51+
name: keyVersionName,
52+
});
53+
54+
console.log(
55+
`Current state of key version ${keyVersion.name}: ${keyVersion.state}`
56+
);
57+
return keyVersion;
58+
}
59+
60+
return checkStateCryptoKeyVersion();
61+
// [END kms_check_state_imported_key]
62+
}
63+
module.exports.main = main;
64+
65+
/* c8 ignore next 10 */
66+
if (require.main === module) {
67+
main(...process.argv.slice(2)).catch(err => {
68+
console.error(err.message);
69+
process.exitCode = 1;
70+
});
71+
process.on('unhandledRejection', err => {
72+
console.error(err.message);
73+
process.exitCode = 1;
74+
});
75+
}

kms/createImportJob.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
id = 'my-import-job'
22+
) {
23+
// [START kms_create_import_job]
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-import-job';
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 createImportJob() {
42+
const [importJob] = await client.createImportJob({
43+
parent: keyRingName,
44+
importJobId: id,
45+
importJob: {
46+
protectionLevel: 'HSM',
47+
importMethod: 'RSA_OAEP_3072_SHA256',
48+
},
49+
});
50+
51+
console.log(`Created import job: ${importJob.name}`);
52+
return importJob;
53+
}
54+
55+
return createImportJob();
56+
// [END kms_create_import_job]
57+
}
58+
module.exports.main = main;
59+
60+
/* c8 ignore next 10 */
61+
if (require.main === module) {
62+
main(...process.argv.slice(2)).catch(err => {
63+
console.error(err.message);
64+
process.exitCode = 1;
65+
});
66+
process.on('unhandledRejection', err => {
67+
console.error(err.message);
68+
process.exitCode = 1;
69+
});
70+
}

kms/createKeyForImport.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
id = 'my-imported-key'
22+
) {
23+
// [START kms_create_key_for_import]
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-imported-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 createKeyForImport() {
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+
protectionLevel: 'HSM',
50+
},
51+
// Optional: ensure that only imported versions may be added to this
52+
// key.
53+
importOnly: true,
54+
},
55+
// Do not allow KMS to generate an initial version of this key.
56+
skipInitialVersionCreation: true,
57+
});
58+
59+
console.log(`Created key for import: ${key.name}`);
60+
return key;
61+
}
62+
63+
return createKeyForImport();
64+
// [END kms_create_key_for_import]
65+
}
66+
module.exports.main = main;
67+
68+
/* c8 ignore next 10 */
69+
if (require.main === module) {
70+
main(...process.argv.slice(2)).catch(err => {
71+
console.error(err.message);
72+
process.exitCode = 1;
73+
});
74+
process.on('unhandledRejection', err => {
75+
console.error(err.message);
76+
process.exitCode = 1;
77+
});
78+
}

kms/importManuallyWrappedKey.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)