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
migrate code from googleapis/python-kms#8470
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
c3dae48c031a34cc8e1b47147ccf69a7649e67c3995b298d5480435aadefefeb97884f38d062d832a6bed65342d409a57f6d2a5f938da194515d306a4fe3966db525e650cc533ad2bc03161da400bcfc872fee988aa3024366fa72887f48f909a24bce34f10d8894214f159eadf03dc06f2c0dcac7a0aa821e6aaef44d09eecdc662dd03086f8f31d966796a5fb151148170cb5c1926ae6944efc6a8bfa9992fabc24b425005b35bcd445b9b7f9366aecda49c6a6f2863607047084ad41bdb253cd68144656593d762435964056c1f59e62cdeeaedc03427d835d6e90bc546689d82d9ad8e0fdf3dd5d05abe0570fb39d76057017e2b39571391284a6101b1fea46a5e31fdee1462b1f7277ffb82597804bcb80c6e695b22079922cb88b9babf615fd8911a484d72d4daf529ec8653eebd52bbd8802d4c0537837006dbf90dec3cc389a6f2e8121d9d7944d965005d26f0c5cb54b8836d804e44a1473fc68b949e329c75d7df10185a0e8ddd221607bc34019d55042fec9433504d1ac927b81f6a077451562646ba4589f95d52681be897dad1ae0ad3f392f41d5a2f11a91c0cf955d67ef9e23a0ba0fe23040450d181e08574d51f2b9d6082e1e35bf40dfca181dd63a73b0204062ea7cb7357280fa24685b8496c01ca228bb58291File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -126,6 +126,11 @@ assign_issues_by: | ||
| - 'api: monitoring' | ||
| to: | ||
| - GoogleCloudPlatform/dee-observability | ||
| - labels: | ||
| - 'api: kms' | ||
rsamborski marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| - 'api: cloudkms' | ||
| to: | ||
| - GoogleCloudPlatform/dee-infra | ||
| assign_prs_by: | ||
| - labels: | ||
This file was deleted.
Uh oh!
There was an error while loading. Please reload this page.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| Google Cloud Key Management Service Python Samples | ||
| =============================================================================== | ||
| .. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
| :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=kms/attestations/README.rst | ||
| This directory contains samples for Google Cloud Key Management Service. The `Cloud Key Management Service`_ allows you to create, import, and manage cryptographic keys and perform cryptographic operations in a single centralized cloud service. | ||
| .. _Cloud Key Management Service: https://cloud.google.com/kms/docs/ | ||
| Setup | ||
| ------------------------------------------------------------------------------- | ||
| Install Dependencies | ||
| ++++++++++++++++++++ | ||
| #. Clone python-kms and change directory to the sample directory you want to use. | ||
| .. code-block:: bash | ||
| $ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git | ||
| #. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions. | ||
| .. _Python Development Environment Setup Guide: | ||
| https://cloud.google.com/python/setup | ||
| #. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. | ||
| .. code-block:: bash | ||
| $ virtualenv env | ||
| $ source env/bin/activate | ||
| #. Install the dependencies needed to run the samples. | ||
| .. code-block:: bash | ||
| $ pip install -r requirements.txt | ||
| .. _pip: https://pip.pypa.io/ | ||
| .. _virtualenv: https://virtualenv.pypa.io/ | ||
| Samples | ||
| ------------------------------------------------------------------------------- | ||
| Verify attestations and certificate chains for keys generated by Cloud HSM | ||
| +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
| .. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
| :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=kms/attestations/verify_attestation_chains.py,kms/attestations/README.rst | ||
| To run this sample: | ||
| .. code-block:: bash | ||
| $ python verify_attestation_chains.py | ||
| usage: verify_attestation_chains.py [-h] [--certificates CERTIFICATES] | ||
| [--attestation ATTESTATION] | ||
| This application verifies HSM attestations using certificate chains | ||
| obtained from Cloud HSM and the HSM manufacturer. | ||
| For more information, visit https://cloud.google.com/kms/docs/attest-key. | ||
| optional arguments: | ||
| -h, --help show this help message and exit | ||
| --certificates CERTIFICATES | ||
| The certificate chains filename. | ||
| --attestation ATTESTATION | ||
| The attestation filename. | ||
| Verify attestations for keys generated by Cloud HSM | ||
| +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
| .. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
| :target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=kms/attestations/verify_attestation.py,kms/attestations/README.rst | ||
| To run this sample: | ||
| .. code-block:: bash | ||
| $ python verify_attestation.py | ||
| usage: verify_attestation.py [-h] attestation_file bundle_file | ||
| This application verifies HSM attestations using certificate bundles obtained | ||
| from Cloud HSM. For more information, visit https://cloud.google.com/kms/docs | ||
| /attest-key. | ||
| positional arguments: | ||
| attestation_file Name of attestation file. | ||
| bundle_file Name of certificate bundle file. | ||
| optional arguments: | ||
| -h, --help show this help message and exit | ||
| .. _Google Cloud SDK: https://cloud.google.com/sdk/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # Copyright 2021 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # Default TEST_CONFIG_OVERRIDE for python repos. | ||
| # You can copy this file into your directory, then it will be imported from | ||
| # the noxfile.py. | ||
| # The source of truth: | ||
| # https://github.com/GoogleCloudPlatform/python-docs-samples/blob/main/noxfile_config.py | ||
| TEST_CONFIG_OVERRIDE = { | ||
| # You can opt out from the test for specific Python versions. | ||
| "ignored_versions": ["2.7"], | ||
| # Old samples are opted out of enforcing Python type hints | ||
| # All new samples should feature them | ||
| "enforce_type_hints": False, | ||
| # An envvar key for determining the project id to use. Change it | ||
| # to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a | ||
| # build specific Cloud project. You can also use your own string | ||
| # to use your own Cloud project. | ||
| "gcloud_project_env": "GOOGLE_CLOUD_PROJECT", | ||
| # 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT', | ||
| # If you need to use a specific version of pip, | ||
| # change pip_version_override to the string representation | ||
| # of the version number, for example, "20.2.4" | ||
| "pip_version_override": None, | ||
| # A dictionary you want to inject into your test. Don't put any | ||
| # secrets here. These values will override predefined values. | ||
| "envs": {}, | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| pytest==7.2.0 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| cryptography==38.0.2 | ||
| pem==21.2.0 | ||
| requests==2.28.1 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| #!/usr/bin/env python | ||
| # Copyright 2020 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| """This application verifies HSM attestations using certificate bundles | ||
| obtained from Cloud HSM. | ||
| For more information, visit https://cloud.google.com/kms/docs/attest-key. | ||
| """ | ||
| # [START kms_verify_attestations] | ||
| import argparse | ||
| import gzip | ||
| from cryptography import exceptions | ||
| from cryptography import x509 | ||
| from cryptography.hazmat import backends | ||
| from cryptography.hazmat.primitives.asymmetric import padding | ||
| import pem | ||
| def verify(attestation_file, bundle_file): | ||
| """Verifies an attestation using a bundle of certificates. | ||
| Args: | ||
| attestation_file: The name of the attestation file. | ||
| bundle_file: The name of the bundle file containing the certificates | ||
| used to verify the attestation. | ||
| Returns: | ||
| True if at least one of the certificates in bundle_file can verify the | ||
| attestation data and its signature. | ||
| """ | ||
| with gzip.open(attestation_file, 'rb') as f: | ||
| # An attestation file consists of a data portion and a 256 byte | ||
| # signature portion concatenated together. | ||
| attestation = f.read() | ||
| # Separate the components. | ||
| data = attestation[:-256] | ||
| signature = attestation[-256:] | ||
| # Verify the attestation with one of the certificates in the bundle | ||
| for cert in pem.parse_file(bundle_file): | ||
| cert_obj = x509.load_pem_x509_certificate( | ||
| str(cert).encode('utf-8'), backends.default_backend()) | ||
| try: | ||
| # Check if the data was signed by the private key associated | ||
| # with the public key in the certificate. The data should have | ||
| # been signed with PKCS1v15 padding. | ||
| cert_obj.public_key().verify( | ||
| signature, data, padding.PKCS1v15(), | ||
| cert_obj.signature_hash_algorithm) | ||
| return True | ||
| except exceptions.InvalidSignature: | ||
| # Certificate bundles contain certificates that will not be | ||
| # able to verify the attestation, so the InvalidSignature | ||
| # errors can be ignored. | ||
| continue | ||
| return False | ||
| # [END kms_verify_attestations] | ||
| if __name__ == '__main__': | ||
| parser = argparse.ArgumentParser( | ||
| description=__doc__) | ||
| parser.add_argument('attestation_file', help="Name of attestation file.") | ||
| parser.add_argument('bundle_file', help="Name of certificate bundle file.") | ||
| args = parser.parse_args() | ||
| if verify(args.attestation_file, args.bundle_file): | ||
| print('Signature verified.') | ||
| else: | ||
| print('Signature verification failed.') |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.