Skip to content

Commit dfd632d

Browse files
FrodoTheTruetelpirion
authored andcommitted
feat(samples): add local generation for crypto keys (#98)
1 parent 156f38d commit dfd632d

File tree

3 files changed

+21
-59
lines changed

3 files changed

+21
-59
lines changed

privateca/snippets/create_certificate.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
# limitations under the License.
1616

1717
# [START privateca_create_certificate]
18-
from google.cloud import kms
1918
import google.cloud.security.privateca_v1 as privateca_v1
2019
from google.protobuf import duration_pb2
2120

@@ -26,13 +25,10 @@ def create_certificate(
2625
ca_pool_name: str,
2726
ca_name: str,
2827
certificate_name: str,
29-
kms_location: str,
30-
key_ring_id: str,
31-
key_id: str,
32-
key_version_id: str,
3328
common_name: str,
3429
domain_name: str,
3530
certificate_lifetime: int,
31+
public_key_bytes: bytes,
3632
) -> None:
3733
"""
3834
Create a Certificate which is issued by the Certificate Authority present in the CA Pool.
@@ -44,30 +40,21 @@ def create_certificate(
4440
ca_pool_name: set a unique name for the CA pool.
4541
ca_name: the name of the certificate authority which issues the certificate.
4642
certificate_name: set a unique name for the certificate.
47-
kms_location: Cloud KMS location.
48-
key_ring_id: ID of the Cloud KMS key ring.
49-
key_id: ID of the key to use.
50-
key_version_id: verstion ID of the key to use.
5143
common_name: a title for your certificate.
5244
domain_name: fully qualified domain name for your certificate.
5345
certificate_lifetime: the validity of the certificate in seconds.
46+
public_key_bytes: public key used in signing the certificates.
5447
"""
5548

56-
kmsClient = kms.KeyManagementServiceClient()
5749
caServiceClient = privateca_v1.CertificateAuthorityServiceClient()
5850

59-
# To sign and issue a certificate, a public key is essential. Here, we are making use
60-
# of Cloud KMS to retrieve an already created public key. For more info, see: https://cloud.google.com/kms/docs/retrieve-public-key.
61-
# Generating keys locally is also possible.
51+
# The public key used to sign the certificate can be generated using any crypto library/framework.
52+
# Also you can use Cloud KMS to retrieve an already created public key.
53+
# For more info, see: https://cloud.google.com/kms/docs/retrieve-public-key.
6254

63-
key_version_name = kmsClient.crypto_key_version_path(
64-
project_id, kms_location, key_ring_id, key_id, key_version_id
65-
)
66-
kms_public_key = kmsClient.get_public_key(name=key_version_name)
67-
68-
# Set the Public Key and its format as obtained from the Cloud KMS.
55+
# Set the Public Key and its format.
6956
public_key = privateca_v1.PublicKey(
70-
key=str.encode(kms_public_key.pem),
57+
key=public_key_bytes,
7158
format_=privateca_v1.PublicKey.KeyFormat.PEM,
7259
)
7360

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pytest==6.2.4
2-
google-auth==1.34.0
2+
google-auth==1.34.0
3+
cryptography==3.4.7

privateca/snippets/test_certificates.py

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@
1717
import typing
1818
import uuid
1919

20+
from cryptography.hazmat.backends.openssl.backend import backend
21+
from cryptography.hazmat.primitives.asymmetric import rsa
22+
23+
from cryptography.hazmat.primitives.serialization import Encoding
24+
from cryptography.hazmat.primitives.serialization import PublicFormat
25+
2026
import google.auth
21-
from google.cloud import kms
2227

2328
from create_certificate import create_certificate
2429
from disable_certificate_authority import disable_certificate_authority
@@ -31,7 +36,6 @@
3136
COMMON_NAME = "COMMON_NAME"
3237
ORGANIZATION = "ORGANIZATION"
3338
CERTIFICATE_LIFETIME = 1000000
34-
KEY_VERSION = 1
3539
DOMAIN_NAME = "domain.com"
3640

3741

@@ -42,62 +46,32 @@ def generate_name() -> str:
4246
def test_create_and_revoke_certificate_authority(
4347
certificate_authority, capsys: typing.Any
4448
) -> None:
45-
KEY_RING_ID = generate_name()
46-
CRYPTO_KEY_ID = generate_name()
4749
CERT_NAME = generate_name()
4850

4951
CA_POOL_NAME, CA_NAME = certificate_authority
5052
enable_certificate_authority(PROJECT, LOCATION, CA_POOL_NAME, CA_NAME)
5153

52-
kms_client = kms.KeyManagementServiceClient()
53-
54-
kms_location_name = kms_client.common_location_path(PROJECT, LOCATION)
55-
56-
kms_client.create_key_ring(
57-
request={
58-
"parent": kms_location_name,
59-
"key_ring_id": KEY_RING_ID,
60-
"key_ring": {},
61-
}
54+
private_key = rsa.generate_private_key(
55+
public_exponent=65537, key_size=2048, backend=backend
6256
)
6357

64-
key_ring_path = kms_client.key_ring_path(PROJECT, LOCATION, KEY_RING_ID)
65-
66-
purpose = kms.CryptoKey.CryptoKeyPurpose.ASYMMETRIC_SIGN
67-
algorithm = (
68-
kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.RSA_SIGN_PKCS1_4096_SHA256
69-
)
70-
key = {
71-
"purpose": purpose,
72-
"version_template": {
73-
"algorithm": algorithm,
74-
},
75-
}
76-
77-
kms_client.create_crypto_key(
78-
request={
79-
"parent": key_ring_path,
80-
"crypto_key_id": CRYPTO_KEY_ID,
81-
"crypto_key": key,
82-
}
58+
public_key_bytes = private_key.public_key().public_bytes(
59+
Encoding.PEM, PublicFormat.SubjectPublicKeyInfo
8360
)
8461

8562
# Wait while crypto key is generating
86-
time.sleep(30)
63+
time.sleep(5)
8764

8865
create_certificate(
8966
PROJECT,
9067
LOCATION,
9168
CA_POOL_NAME,
9269
CA_NAME,
9370
CERT_NAME,
94-
LOCATION,
95-
KEY_RING_ID,
96-
CRYPTO_KEY_ID,
97-
KEY_VERSION,
9871
COMMON_NAME,
9972
DOMAIN_NAME,
10073
CERTIFICATE_LIFETIME,
74+
public_key_bytes,
10175
)
10276

10377
revoke_certificate(

0 commit comments

Comments
 (0)