Skip to content

Commit b003718

Browse files
authored
fix: Add type hints for KMS snippets (#9979)
* fix: Add type hints for KMS snippets * fix: type * fix: even more types * black * fix: remove debugging note * isort * manual sort, apparently * Move all imports to top of files, sort, move region tags * even more import fixes * black, isort * black, again
1 parent 233508a commit b003718

43 files changed

Lines changed: 1432 additions & 754 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎kms/snippets/check_state_import_job.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414

1515
# [START kms_check_state_import_job]
16-
def check_state_import_job(project_id, location_id, key_ring_id, import_job_id):
16+
from google.cloud import kms
17+
18+
19+
def check_state_import_job(
20+
project_id: str, location_id: str, key_ring_id: str, import_job_id: str
21+
) -> None:
1722
"""
1823
Check the state of an import job in Cloud KMS.
1924
@@ -24,18 +29,18 @@ def check_state_import_job(project_id, location_id, key_ring_id, import_job_id):
2429
import_job_id (string): ID of the import job (e.g. 'my-import-job').
2530
"""
2631

27-
# Import the client library.
28-
from google.cloud import kms
29-
3032
# Create the client.
3133
client = kms.KeyManagementServiceClient()
3234

3335
# Retrieve the fully-qualified import_job string.
3436
import_job_name = client.import_job_path(
35-
project_id, location_id, key_ring_id, import_job_id)
37+
project_id, location_id, key_ring_id, import_job_id
38+
)
3639

3740
# Retrieve the state from an existing import job.
3841
import_job = client.get_import_job(name=import_job_name)
3942

40-
print(f'Current state of import job {import_job.name}: {import_job.state}')
43+
print(f"Current state of import job {import_job.name}: {import_job.state}")
44+
45+
4146
# [END kms_check_state_import_job]

‎kms/snippets/check_state_imported_key.py‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414

1515
# [START kms_check_state_imported_key]
16-
def check_state_imported_key(project_id, location_id, key_ring_id, import_job_id):
16+
from google.cloud import kms
17+
18+
19+
def check_state_imported_key(
20+
project_id: str, location_id: str, key_ring_id: str, import_job_id: str
21+
) -> None:
1722
"""
1823
Check the state of an import job in Cloud KMS.
1924
@@ -24,18 +29,18 @@ def check_state_imported_key(project_id, location_id, key_ring_id, import_job_id
2429
import_job_id (string): ID of the import job (e.g. 'my-import-job').
2530
"""
2631

27-
# Import the client library.
28-
from google.cloud import kms
29-
3032
# Create the client.
3133
client = kms.KeyManagementServiceClient()
3234

3335
# Retrieve the fully-qualified import_job string.
3436
import_job_name = client.import_job_path(
35-
project_id, location_id, key_ring_id, import_job_id)
37+
project_id, location_id, key_ring_id, import_job_id
38+
)
3639

3740
# Retrieve the state from an existing import job.
3841
import_job = client.get_import_job(name=import_job_name)
3942

40-
print(f'Current state of import job {import_job.name}: {import_job.state}')
43+
print(f"Current state of import job {import_job.name}: {import_job.state}")
44+
45+
4146
# [END kms_check_state_imported_key]

‎kms/snippets/create_import_job.py‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414

1515
# [START kms_create_import_job]
16-
def create_import_job(project_id, location_id, key_ring_id, import_job_id):
16+
from google.cloud import kms
17+
18+
19+
def create_import_job(
20+
project_id: str, location_id: str, key_ring_id: str, import_job_id: str
21+
) -> None:
1722
"""
1823
Create a new import job in Cloud KMS.
1924
@@ -24,9 +29,6 @@ def create_import_job(project_id, location_id, key_ring_id, import_job_id):
2429
import_job_id (string): ID of the import job (e.g. 'my-import-job').
2530
"""
2631

27-
# Import the client library.
28-
from google.cloud import kms
29-
3032
# Create the client.
3133
client = kms.KeyManagementServiceClient()
3234

@@ -38,10 +40,21 @@ def create_import_job(project_id, location_id, key_ring_id, import_job_id):
3840

3941
import_method = kms.ImportJob.ImportMethod.RSA_OAEP_3072_SHA1_AES_256
4042
protection_level = kms.ProtectionLevel.HSM
41-
import_job_params = {"import_method": import_method, "protection_level": protection_level}
43+
import_job_params = {
44+
"import_method": import_method,
45+
"protection_level": protection_level,
46+
}
4247

4348
# Call the client to create a new import job.
44-
import_job = client.create_import_job({"parent": key_ring_name, "import_job_id": import_job_id, "import_job": import_job_params})
49+
import_job = client.create_import_job(
50+
{
51+
"parent": key_ring_name,
52+
"import_job_id": import_job_id,
53+
"import_job": import_job_params,
54+
}
55+
)
56+
57+
print(f"Created import job: {import_job.name}")
58+
4559

46-
print(f'Created import job: {import_job.name}')
4760
# [END kms_create_import_job]

‎kms/snippets/create_key_asymmetric_decrypt.py‎

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,17 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313

14-
1514
# [START kms_create_key_asymmetric_decrypt]
16-
def create_key_asymmetric_decrypt(project_id, location_id, key_ring_id, key_id):
15+
import datetime
16+
17+
# Import the client library.
18+
from google.cloud import kms
19+
from google.protobuf import duration_pb2 # type: ignore
20+
21+
22+
def create_key_asymmetric_decrypt(
23+
project_id: str, location_id: str, key_ring_id: str, key_id: str
24+
) -> kms.CryptoKey:
1725
"""
1826
Creates a new asymmetric decryption key in Cloud KMS.
1927
@@ -28,11 +36,6 @@ def create_key_asymmetric_decrypt(project_id, location_id, key_ring_id, key_id):
2836
2937
"""
3038

31-
# Import the client library.
32-
from google.cloud import kms
33-
from google.protobuf import duration_pb2
34-
import datetime
35-
3639
# Create the client.
3740
client = kms.KeyManagementServiceClient()
3841

@@ -41,21 +44,27 @@ def create_key_asymmetric_decrypt(project_id, location_id, key_ring_id, key_id):
4144

4245
# Build the key.
4346
purpose = kms.CryptoKey.CryptoKeyPurpose.ASYMMETRIC_DECRYPT
44-
algorithm = kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.RSA_DECRYPT_OAEP_2048_SHA256
47+
algorithm = (
48+
kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.RSA_DECRYPT_OAEP_2048_SHA256
49+
)
4550
key = {
46-
'purpose': purpose,
47-
'version_template': {
48-
'algorithm': algorithm,
51+
"purpose": purpose,
52+
"version_template": {
53+
"algorithm": algorithm,
4954
},
50-
5155
# Optional: customize how long key versions should be kept before
5256
# destroying.
53-
'destroy_scheduled_duration': duration_pb2.Duration().FromTimedelta(datetime.timedelta(days=1))
57+
"destroy_scheduled_duration": duration_pb2.Duration().FromTimedelta(
58+
datetime.timedelta(days=1)
59+
),
5460
}
5561

5662
# Call the API.
5763
created_key = client.create_crypto_key(
58-
request={'parent': key_ring_name, 'crypto_key_id': key_id, 'crypto_key': key})
59-
print(f'Created asymmetric decrypt key: {created_key.name}')
64+
request={"parent": key_ring_name, "crypto_key_id": key_id, "crypto_key": key}
65+
)
66+
print(f"Created asymmetric decrypt key: {created_key.name}")
6067
return created_key
68+
69+
6170
# [END kms_create_key_asymmetric_decrypt]

‎kms/snippets/create_key_asymmetric_sign.py‎

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313

14-
1514
# [START kms_create_key_asymmetric_sign]
16-
def create_key_asymmetric_sign(project_id, location_id, key_ring_id, key_id):
15+
16+
import datetime
17+
18+
# Import the client library.
19+
from google.cloud import kms
20+
from google.protobuf import duration_pb2 # type: ignore
21+
22+
23+
def create_key_asymmetric_sign(
24+
project_id: str, location_id: str, key_ring_id: str, key_id: str
25+
) -> kms.CryptoKey:
1726
"""
1827
Creates a new asymmetric signing key in Cloud KMS.
1928
@@ -28,11 +37,6 @@ def create_key_asymmetric_sign(project_id, location_id, key_ring_id, key_id):
2837
2938
"""
3039

31-
# Import the client library.
32-
from google.cloud import kms
33-
from google.protobuf import duration_pb2
34-
import datetime
35-
3640
# Create the client.
3741
client = kms.KeyManagementServiceClient()
3842

@@ -41,21 +45,27 @@ def create_key_asymmetric_sign(project_id, location_id, key_ring_id, key_id):
4145

4246
# Build the key.
4347
purpose = kms.CryptoKey.CryptoKeyPurpose.ASYMMETRIC_SIGN
44-
algorithm = kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.RSA_SIGN_PKCS1_2048_SHA256
48+
algorithm = (
49+
kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.RSA_SIGN_PKCS1_2048_SHA256
50+
)
4551
key = {
46-
'purpose': purpose,
47-
'version_template': {
48-
'algorithm': algorithm,
52+
"purpose": purpose,
53+
"version_template": {
54+
"algorithm": algorithm,
4955
},
50-
5156
# Optional: customize how long key versions should be kept before
5257
# destroying.
53-
'destroy_scheduled_duration': duration_pb2.Duration().FromTimedelta(datetime.timedelta(days=1))
58+
"destroy_scheduled_duration": duration_pb2.Duration().FromTimedelta(
59+
datetime.timedelta(days=1)
60+
),
5461
}
5562

5663
# Call the API.
5764
created_key = client.create_crypto_key(
58-
request={'parent': key_ring_name, 'crypto_key_id': key_id, 'crypto_key': key})
59-
print(f'Created asymmetric signing key: {created_key.name}')
65+
request={"parent": key_ring_name, "crypto_key_id": key_id, "crypto_key": key}
66+
)
67+
print(f"Created asymmetric signing key: {created_key.name}")
6068
return created_key
69+
70+
6171
# [END kms_create_key_asymmetric_sign]

‎kms/snippets/create_key_for_import.py‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414

1515
# [START kms_create_key_for_import]
16-
def create_key_for_import(project_id, location_id, key_ring_id, crypto_key_id):
16+
from google.cloud import kms
17+
18+
19+
def create_key_for_import(
20+
project_id: str, location_id: str, key_ring_id: str, crypto_key_id: str
21+
) -> None:
1722
"""
1823
1924
Sets up an empty CryptoKey within a KeyRing for import.
@@ -26,9 +31,6 @@ def create_key_for_import(project_id, location_id, key_ring_id, crypto_key_id):
2631
crypto_key_id (string): ID of the key to import (e.g. 'my-asymmetric-signing-key').
2732
"""
2833

29-
# Import the client library.
30-
from google.cloud import kms
31-
3234
# Create the client.
3335
client = kms.KeyManagementServiceClient()
3436

@@ -38,17 +40,25 @@ def create_key_for_import(project_id, location_id, key_ring_id, crypto_key_id):
3840
algorithm = kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.EC_SIGN_P256_SHA256
3941
protection_level = kms.ProtectionLevel.HSM
4042
key = {
41-
'purpose': purpose,
42-
'version_template': {
43-
'algorithm': algorithm,
44-
'protection_level': protection_level
45-
}
43+
"purpose": purpose,
44+
"version_template": {
45+
"algorithm": algorithm,
46+
"protection_level": protection_level,
47+
},
4648
}
4749

4850
# Build the parent key ring name.
4951
key_ring_name = client.key_ring_path(project_id, location_id, key_ring_id)
5052

5153
# Call the API.
52-
created_key = client.create_crypto_key(request={'parent': key_ring_name, 'crypto_key_id': crypto_key_id, 'crypto_key': key})
53-
print(f'Created hsm key: {created_key.name}')
54+
created_key = client.create_crypto_key(
55+
request={
56+
"parent": key_ring_name,
57+
"crypto_key_id": crypto_key_id,
58+
"crypto_key": key,
59+
}
60+
)
61+
print(f"Created hsm key: {created_key.name}")
62+
63+
5464
# [END kms_create_key_for_import]

‎kms/snippets/create_key_hsm.py‎

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313

14-
1514
# [START kms_create_key_hsm]
16-
def create_key_hsm(project_id, location_id, key_ring_id, key_id):
15+
import datetime
16+
17+
from google.cloud import kms
18+
from google.protobuf import duration_pb2 # type: ignore
19+
20+
21+
def create_key_hsm(
22+
project_id: str, location_id: str, key_ring_id: str, key_id: str
23+
) -> kms.CryptoKey:
1724
"""
1825
Creates a new key in Cloud KMS backed by Cloud HSM.
1926
@@ -28,11 +35,6 @@ def create_key_hsm(project_id, location_id, key_ring_id, key_id):
2835
2936
"""
3037

31-
# Import the client library.
32-
from google.cloud import kms
33-
from google.protobuf import duration_pb2
34-
import datetime
35-
3638
# Create the client.
3739
client = kms.KeyManagementServiceClient()
3840

@@ -41,23 +43,29 @@ def create_key_hsm(project_id, location_id, key_ring_id, key_id):
4143

4244
# Build the key.
4345
purpose = kms.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT
44-
algorithm = kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION
46+
algorithm = (
47+
kms.CryptoKeyVersion.CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION
48+
)
4549
protection_level = kms.ProtectionLevel.HSM
4650
key = {
47-
'purpose': purpose,
48-
'version_template': {
49-
'algorithm': algorithm,
50-
'protection_level': protection_level
51+
"purpose": purpose,
52+
"version_template": {
53+
"algorithm": algorithm,
54+
"protection_level": protection_level,
5155
},
52-
5356
# Optional: customize how long key versions should be kept before
5457
# destroying.
55-
'destroy_scheduled_duration': duration_pb2.Duration().FromTimedelta(datetime.timedelta(days=1))
58+
"destroy_scheduled_duration": duration_pb2.Duration().FromTimedelta(
59+
datetime.timedelta(days=1)
60+
),
5661
}
5762

5863
# Call the API.
5964
created_key = client.create_crypto_key(
60-
request={'parent': key_ring_name, 'crypto_key_id': key_id, 'crypto_key': key})
61-
print(f'Created hsm key: {created_key.name}')
65+
request={"parent": key_ring_name, "crypto_key_id": key_id, "crypto_key": key}
66+
)
67+
print(f"Created hsm key: {created_key.name}")
6268
return created_key
69+
70+
6371
# [END kms_create_key_hsm]

0 commit comments

Comments
 (0)