Skip to content

Commit f04bcff

Browse files
lwolfowitz-googledandhlee
authored andcommitted
samples: Minor fixes for importing-a-key snippets (#68)
1 parent 100f533 commit f04bcff

File tree

4 files changed

+26
-34
lines changed

4 files changed

+26
-34
lines changed

kms/snippets/create_import_job.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ def create_import_job(project_id, location_id, key_ring_id, import_job_id):
3535

3636
# Set paramaters for the import job, allowed values for ImportMethod and ProtectionLevel found here:
3737
# https://googleapis.dev/python/cloudkms/latest/_modules/google/cloud/kms_v1/types/resources.html
38-
import_job_params = {"import_method": kms.ImportJob.ImportMethod.RSA_OAEP_3072_SHA1_AES_256, "protection_level": kms.ProtectionLevel.HSM}
38+
39+
import_method = kms.ImportJob.ImportMethod.RSA_OAEP_3072_SHA1_AES_256
40+
protection_level = kms.ProtectionLevel.HSM
41+
import_job_params = {"import_method": import_method, "protection_level": protection_level}
3942

4043
# Call the client to create a new import job.
4144
import_job = client.create_import_job({"parent": key_ring_name, "import_job_id": import_job_id, "import_job": import_job_params})

kms/snippets/create_key_for_import.py

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515
# [START kms_create_key_for_import]
1616
def create_key_for_import(project_id, location_id, key_ring_id, crypto_key_id):
1717
"""
18-
Generate Cloud KMS-compatible key material locally and sets up an empty CryptoKey within a KeyRing for import.
18+
19+
Sets up an empty CryptoKey within a KeyRing for import.
20+
1921
2022
Args:
2123
project_id (string): Google Cloud project ID (e.g. 'my-project').
@@ -24,24 +26,9 @@ def create_key_for_import(project_id, location_id, key_ring_id, crypto_key_id):
2426
crypto_key_id (string): ID of the key to import (e.g. 'my-asymmetric-signing-key').
2527
"""
2628

27-
# Import Python standard cryptographic libraries.
28-
from cryptography.hazmat.backends import default_backend
29-
from cryptography.hazmat.primitives import serialization
30-
from cryptography.hazmat.primitives.asymmetric import ec
31-
3229
# Import the client library.
3330
from google.cloud import kms
3431

35-
# Generate some key material in Python and format it in PKCS #8 DER as
36-
# required by Google Cloud KMS.
37-
key = ec.generate_private_key(ec.SECP256R1, default_backend())
38-
formatted_key = key.private_bytes(
39-
serialization.Encoding.DER,
40-
serialization.PrivateFormat.PKCS8,
41-
serialization.NoEncryption())
42-
43-
print('Generated key bytes: {}'.format(formatted_key))
44-
4532
# Create the client.
4633
client = kms.KeyManagementServiceClient()
4734

kms/snippets/import_manually_wrapped_key.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,35 @@
1313

1414

1515
# [START kms_import_manually_wrapped_key]
16-
def import_manually_wrapped_key(project_id, location_id, key_ring_id, crypto_key_id, import_job_id, key_material):
16+
def import_manually_wrapped_key(project_id, location_id, key_ring_id, crypto_key_id, import_job_id):
1717
"""
18-
Imports local key material to Cloud KMS.
18+
Generates and imports local key material to Cloud KMS.
1919
2020
Args:
2121
project_id (string): Google Cloud project ID (e.g. 'my-project').
2222
location_id (string): Cloud KMS location (e.g. 'us-east1').
2323
key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').
2424
crypto_key_id (string): ID of the key to import (e.g. 'my-asymmetric-signing-key').
2525
import_job_id (string): ID of the import job (e.g. 'my-import-job').
26-
key_material (bytes): Locally generated key material in PKCS #8 DER format.
27-
Returns:
28-
CryptoKeyVersion: An instance of the imported key in Cloud KMS.
2926
"""
3027

3128
# Import the client library and Python standard cryptographic libraries.
3229
import os
33-
from cryptography.hazmat.backends import default_backend
30+
from cryptography.hazmat import backends
3431
from cryptography.hazmat.primitives import hashes, keywrap, serialization
35-
from cryptography.hazmat.primitives.asymmetric import padding
32+
from cryptography.hazmat.primitives.asymmetric import ec, padding
3633
from google.cloud import kms
3734

35+
# Generate some key material in Python and format it in PKCS #8 DER as
36+
# required by Google Cloud KMS.
37+
key = ec.generate_private_key(ec.SECP256R1, backends.default_backend())
38+
formatted_key = key.private_bytes(
39+
serialization.Encoding.DER,
40+
serialization.PrivateFormat.PKCS8,
41+
serialization.NoEncryption())
42+
43+
print('Generated key bytes: {}'.format(formatted_key))
44+
3845
# Create the client.
3946
client = kms.KeyManagementServiceClient()
4047

@@ -47,12 +54,12 @@ def import_manually_wrapped_key(project_id, location_id, key_ring_id, crypto_key
4754
# Generate a temporary 32-byte key for AES-KWP and wrap the key material.
4855
kwp_key = os.urandom(32)
4956
wrapped_target_key = keywrap.aes_key_wrap_with_padding(
50-
kwp_key, key_material, default_backend())
57+
kwp_key, formatted_key, backends.default_backend())
5158

5259
# Retrieve the public key from the import job.
5360
import_job = client.get_import_job(name=import_job_name)
5461
import_job_pub = serialization.load_pem_public_key(
55-
bytes(import_job.public_key.pem, 'UTF-8'), default_backend())
62+
bytes(import_job.public_key.pem, 'UTF-8'), backends.default_backend())
5663

5764
# Wrap the KWP key using the import job key.
5865
wrapped_kwp_key = import_job_pub.encrypt(

kms/snippets/snippets_test.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from cryptography.exceptions import InvalidSignature
2121
from cryptography.hazmat.backends import default_backend
2222
from cryptography.hazmat.primitives import hashes, serialization
23-
from cryptography.hazmat.primitives.asymmetric import ec, padding, utils
23+
from cryptography.hazmat.primitives.asymmetric import padding, utils
2424
from google.cloud import kms
2525
import pytest
2626

@@ -226,7 +226,7 @@ def test_create_key_asymmetric_sign(project_id, location_id, key_ring_id):
226226
def test_create_key_for_import(project_id, location_id, key_ring_id, import_tests_key_id, capsys):
227227
create_key_for_import(project_id, location_id, key_ring_id, import_tests_key_id)
228228
out, _ = capsys.readouterr()
229-
assert "Generated key" in out
229+
assert "Created hsm key" in out
230230

231231

232232
def test_create_key_hsm(project_id, location_id, key_ring_id):
@@ -387,12 +387,7 @@ def test_iam_remove_member(client, project_id, location_id, key_ring_id, asymmet
387387

388388

389389
def test_import_manually_wrapped_key(project_id, location_id, key_ring_id, import_job_id, import_tests_key_id, capsys):
390-
key = ec.generate_private_key(ec.SECP256R1, default_backend())
391-
formatted_key = key.private_bytes(
392-
serialization.Encoding.DER,
393-
serialization.PrivateFormat.PKCS8,
394-
serialization.NoEncryption())
395-
import_manually_wrapped_key(project_id, location_id, key_ring_id, import_tests_key_id, import_job_id, formatted_key)
390+
import_manually_wrapped_key(project_id, location_id, key_ring_id, import_tests_key_id, import_job_id)
396391
out, _ = capsys.readouterr()
397392
assert "Imported" in out
398393

0 commit comments

Comments
 (0)