Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 6.7k
KMS: Updated for new client library#1903
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
19165ae
updated kms samples to use new gapic client library
daniel-sanche dbab859
fixed test issues
daniel-sanche c621c33
addressed PR comments
daniel-sanche f656872
Merge branch 'master' into kms-gapic-update
engelke 66b5b39
Merge branch 'master' into kms-gapic-update
daniel-sanche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -13,71 +13,103 @@ | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License.rom googleapiclient import discovery | ||
| import base64 | ||
| import hashlib | ||
| from cryptography.exceptions import InvalidSignature | ||
| from cryptography.hazmat.backends import default_backend | ||
| from cryptography.hazmat.primitives import hashes, serialization | ||
| from cryptography.hazmat.primitives.asymmetric import ec, padding, utils | ||
| from google.cloud import kms_v1 | ||
| from google.cloud.kms_v1 import enums | ||
| # [START kms_create_asymmetric_key] | ||
| def create_asymmetric_key(project_id, location_id, key_ring_id, crypto_key_id): | ||
| """Creates an RSA encrypt/decrypt key pair within a specified KeyRing.""" | ||
| # Creates an API client for the KMS API. | ||
| client = kms_v1.KeyManagementServiceClient() | ||
| # The resource name of the KeyRing associated with the CryptoKey. | ||
| parent = client.key_ring_path(project_id, location_id, key_ring_id) | ||
| # Create the CryptoKey object template | ||
| purpose = enums.CryptoKey.CryptoKeyPurpose.ASYMMETRIC_DECRYPT | ||
| algorithm = enums.CryptoKeyVersion.CryptoKeyVersionAlgorithm.\ | ||
| RSA_DECRYPT_OAEP_2048_SHA256 | ||
| crypto_key = {'purpose': purpose, | ||
| 'version_template': {'algorithm': algorithm}} | ||
| # Create a CryptoKey for the given KeyRing. | ||
| response = client.create_crypto_key(parent, crypto_key_id, crypto_key) | ||
| print('Created CryptoKey {}.'.format(response.name)) | ||
| return response | ||
| # [END kms_create_asymmetric_key] | ||
| # [START kms_get_asymmetric_public] | ||
| def getAsymmetricPublicKey(client, key_path): | ||
| def get_asymmetric_public_key(key_name): | ||
| """ | ||
| Retrieves the public key from a saved asymmetric key pair on Cloud KMS | ||
| Example key_name: | ||
| "projects/PROJECT_ID/locations/global/keyRings/RING_ID/cryptoKeys\ | ||
| /KEY_ID/cryptoKeyVersions/1" | ||
| Requires: | ||
| cryptography.hazmat.backends.default_backend | ||
| cryptography.hazmat.primitives.serialization | ||
| """ | ||
| request = client.projects() \ | ||
| .locations() \ | ||
| .keyRings() \ | ||
| .cryptoKeys() \ | ||
| .cryptoKeyVersions() \ | ||
| .getPublicKey(name=key_path) | ||
| response = request.execute() | ||
| key_txt = response['pem'].encode('ascii') | ||
| client = kms_v1.KeyManagementServiceClient() | ||
| response = client.get_public_key(key_name) | ||
| key_txt = response.pem.encode('ascii') | ||
| key = serialization.load_pem_public_key(key_txt, default_backend()) | ||
| return key | ||
| # [END kms_get_asymmetric_public] | ||
| # [START kms_decrypt_rsa] | ||
| def decryptRSA(ciphertext, client, key_path): | ||
| def decrypt_rsa(ciphertext, key_name): | ||
| """ | ||
| Decrypt the input ciphertext (bytes) using an | ||
| 'RSA_DECRYPT_OAEP_2048_SHA256' private key stored on Cloud KMS | ||
| Requires: | ||
| base64 | ||
| Example key_name: | ||
| "projects/PROJECT_ID/locations/global/keyRings/RING_ID/cryptoKeys\ | ||
| /KEY_ID/cryptoKeyVersions/1" | ||
| """ | ||
| request_body = {'ciphertext': base64.b64encode(ciphertext).decode('utf-8')} | ||
| request = client.projects() \ | ||
| .locations() \ | ||
| .keyRings() \ | ||
| .cryptoKeys() \ | ||
| .cryptoKeyVersions() \ | ||
| .asymmetricDecrypt(name=key_path, | ||
| body=request_body) | ||
| response = request.execute() | ||
| plaintext = base64.b64decode(response['plaintext']) | ||
| return plaintext | ||
| client = kms_v1.KeyManagementServiceClient() | ||
| response = client.asymmetric_decrypt(key_name, ciphertext) | ||
| return response.plaintext | ||
| # [END kms_decrypt_rsa] | ||
| # [START kms_encrypt_rsa] | ||
| def encryptRSA(plaintext, client, key_path): | ||
| def encrypt_rsa(plaintext, key_name): | ||
daniel-sanche marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """ | ||
| Encrypt the input plaintext (bytes) locally using an | ||
| 'RSA_DECRYPT_OAEP_2048_SHA256' public key retrieved from Cloud KMS | ||
| Example key_name: | ||
| "projects/PROJECT_ID/locations/global/keyRings/RING_ID/cryptoKeys\ | ||
| /KEY_ID/cryptoKeyVersions/1" | ||
| Requires: | ||
| cryptography.hazmat.primitives.asymmetric.padding | ||
| cryptography.hazmat.primitives.hashes | ||
| """ | ||
| public_key = getAsymmetricPublicKey(client, key_path) | ||
| # get the public key | ||
| client = kms_v1.KeyManagementServiceClient() | ||
| response = client.get_public_key(key_name) | ||
| key_txt = response.pem.encode('ascii') | ||
| public_key = serialization.load_pem_public_key(key_txt, default_backend()) | ||
| # encrypt plaintext | ||
| pad = padding.OAEP(mgf=padding.MGF1(algorithm=hashes.SHA256()), | ||
| algorithm=hashes.SHA256(), | ||
| label=None) | ||
| @@ -86,46 +118,53 @@ def encryptRSA(plaintext, client, key_path): | ||
| # [START kms_sign_asymmetric] | ||
| def signAsymmetric(message, client, key_path): | ||
| def sign_asymmetric(message, key_name): | ||
engelke marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| """ | ||
| Create a signature for a message using a private key stored on Cloud KMS | ||
| Example key_name: | ||
| "projects/PROJECT_ID/locations/global/keyRings/RING_ID/cryptoKeys\ | ||
| /KEY_ID/cryptoKeyVersions/1" | ||
| Requires: | ||
| base64 | ||
| hashlib | ||
| """ | ||
| # Note: some key algorithms will require a different hash function | ||
| # For example, EC_SIGN_P384_SHA384 requires SHA384 | ||
| client = kms_v1.KeyManagementServiceClient() | ||
| digest_bytes = hashlib.sha256(message).digest() | ||
| digest64 = base64.b64encode(digest_bytes) | ||
| digest_JSON = {'sha256': digest64.decode('utf-8')} | ||
| request = client.projects() \ | ||
| .locations() \ | ||
| .keyRings() \ | ||
| .cryptoKeys() \ | ||
| .cryptoKeyVersions() \ | ||
| .asymmetricSign(name=key_path, | ||
| body={'digest': digest_JSON}) | ||
| response = request.execute() | ||
| return base64.b64decode(response.get('signature', None)) | ||
| digest_json = {'sha256': digest_bytes} | ||
| response = client.asymmetric_sign(key_name, digest_json) | ||
| return response.signature | ||
| # [END kms_sign_asymmetric] | ||
| # [START kms_verify_signature_rsa] | ||
| def verifySignatureRSA(signature, message, client, key_path): | ||
| def verify_signature_rsa(signature, message, key_name): | ||
| """ | ||
| Verify the validity of an 'RSA_SIGN_PSS_2048_SHA256' signature for the | ||
| specified message | ||
| Example key_name: | ||
| "projects/PROJECT_ID/locations/global/keyRings/RING_ID/cryptoKeys\ | ||
| /KEY_ID/cryptoKeyVersions/1" | ||
| Requires: | ||
| cryptography.exceptions.InvalidSignature | ||
| cryptography.hazmat.primitives.asymmetric.padding | ||
| cryptography.hazmat.primitives.asymmetric.utils | ||
| cryptography.hazmat.primitives.hashes | ||
| hashlib | ||
| """ | ||
| public_key = getAsymmetricPublicKey(client, key_path) | ||
| # get the public key | ||
| client = kms_v1.KeyManagementServiceClient() | ||
| response = client.get_public_key(key_name) | ||
| key_txt = response.pem.encode('ascii') | ||
| public_key = serialization.load_pem_public_key(key_txt, default_backend()) | ||
| # get the digest of the message | ||
| digest_bytes = hashlib.sha256(message).digest() | ||
| try: | ||
| @@ -143,19 +182,29 @@ def verifySignatureRSA(signature, message, client, key_path): | ||
| # [START kms_verify_signature_ec] | ||
| def verifySignatureEC(signature, message, client, key_path): | ||
| def verify_signature_ec(signature, message, key_name): | ||
| """ | ||
| Verify the validity of an 'EC_SIGN_P256_SHA256' signature | ||
| for the specified message | ||
| Example key_name: | ||
| "projects/PROJECT_ID/locations/global/keyRings/RING_ID/cryptoKeys\ | ||
| /KEY_ID/cryptoKeyVersions/1" | ||
| Requires: | ||
| cryptography.exceptions.InvalidSignature | ||
| cryptography.hazmat.primitives.asymmetric.ec | ||
| cryptography.hazmat.primitives.asymmetric.utils | ||
| cryptography.hazmat.primitives.hashes | ||
| hashlib | ||
| """ | ||
| public_key = getAsymmetricPublicKey(client, key_path) | ||
| # get the public key | ||
| client = kms_v1.KeyManagementServiceClient() | ||
| response = client.get_public_key(key_name) | ||
| key_txt = response.pem.encode('ascii') | ||
| public_key = serialization.load_pem_public_key(key_txt, default_backend()) | ||
| # get the digest of the message | ||
| digest_bytes = hashlib.sha256(message).digest() | ||
| try: | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.