Skip to content
This repository was archived by the owner on Dec 31, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 51 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
3fcf98f
Python KMS Apiary P1 samples [(#779)](https://github.com/GoogleCloudP…
ryanmats Feb 7, 2017
1f3c3b9
random generation of keyring / cryptokey names [(#786)](https://githu…
ryanmats Feb 8, 2017
d3c9a9b
Auto-update dependencies. [(#790)](https://github.com/GoogleCloudPlat…
dpebot Feb 9, 2017
2f0e389
Remove usage of GoogleCredentials [(#810)](https://github.com/GoogleC…
Feb 17, 2017
27611b3
Updates Could-KMS sample code to use V1 libraries. [(#856)](https://g…
philcoakley Mar 16, 2017
57dbdb1
Remove cloud config fixture [(#887)](https://github.com/GoogleCloudPl…
Apr 4, 2017
fa6ee74
Re-generate all readmes
Apr 27, 2017
966d0d9
KMS: Clean up base64 logic in the encrypt and decrypt functions. [(#1…
Aug 16, 2017
77e5c1a
KMS: Make the samples consistent with the other languages. [(#1075)](…
Aug 17, 2017
aa49dbc
Auto-update dependencies. [(#1094)](https://github.com/GoogleCloudPla…
dpebot Aug 30, 2017
04e00af
Update all generated readme auth instructions [(#1121)](https://githu…
Sep 18, 2017
5156f09
Auto-update dependencies. [(#1133)](https://github.com/GoogleCloudPla…
dpebot Sep 21, 2017
f894679
Added Link to Python Setup Guide [(#1158)](https://github.com/GoogleC…
Oct 12, 2017
98fa815
Auto-update dependencies. [(#1186)](https://github.com/GoogleCloudPla…
dpebot Nov 1, 2017
091dbc6
Adds snippets for enabling and restoring a key version [(#1196)](http…
WalterHub Nov 7, 2017
ff973d8
Auto-update dependencies. [(#1217)](https://github.com/GoogleCloudPla…
dpebot Nov 15, 2017
b9a4c34
Added "Open in Cloud Shell" buttons to README files [(#1254)](https:/…
Dec 7, 2017
251d796
Auto-update dependencies. [(#1309)](https://github.com/GoogleCloudPla…
dpebot Jan 10, 2018
091bcac
Auto-update dependencies. [(#1320)](https://github.com/GoogleCloudPla…
dpebot Feb 2, 2018
c4f16b4
Auto-update dependencies. [(#1355)](https://github.com/GoogleCloudPla…
dpebot Feb 9, 2018
4f3ccbf
Auto-update dependencies. [(#1359)](https://github.com/GoogleCloudPla…
dpebot Feb 26, 2018
7264e6a
Auto-update dependencies.
dpebot Apr 2, 2018
d140516
Regenerate the README files and fix the Open in Cloud Shell link for …
Apr 7, 2018
2857b8d
Update READMEs to fix numbering and add git clone [(#1464)](https://g…
frankyn Apr 26, 2018
cf6aa1a
Resolved conflicts.
michaelawyu Jun 29, 2018
6e26023
Merge branch 'dpebot-repositorygardener' despite flaky dataproc tests
michaelawyu Jun 29, 2018
e2c0637
added kms asymmetric samples [(#1638)](https://github.com/GoogleCloud…
daniel-sanche Aug 17, 2018
45107a0
kms text fixes [(#1647)](https://github.com/GoogleCloudPlatform/pytho…
daniel-sanche Aug 22, 2018
8cbac2e
KMS test fix [(#1690)](https://github.com/GoogleCloudPlatform/python-…
daniel-sanche Sep 7, 2018
c6ac726
KMS changes [(#1723)](https://github.com/GoogleCloudPlatform/python-d…
daniel-sanche Sep 28, 2018
2b8e5e1
KMS import comments [(#1771)](https://github.com/GoogleCloudPlatform/…
daniel-sanche Oct 25, 2018
9a7aa6c
Auto-update dependencies. [(#1846)](https://github.com/GoogleCloudPla…
dpebot Nov 20, 2018
0f27818
Auto-update dependencies. [(#1862)](https://github.com/GoogleCloudPla…
dpebot Nov 21, 2018
15d486d
KMS: Updated for new client library [(#1903)](https://github.com/Goog…
daniel-sanche Jan 7, 2019
7b25fbc
Auto-update dependencies. [(#1980)](https://github.com/GoogleCloudPla…
dpebot Feb 6, 2019
fe32c0c
Move import statements into tagged regions [(#2219)](https://github.c…
engelke Jun 13, 2019
dc0df22
Adds split updates for Firebase ... opencensus [(#2438)](https://gith…
gguuss Oct 8, 2019
18100cc
Auto-update dependencies. [(#2005)](https://github.com/GoogleCloudPla…
dpebot Dec 21, 2019
17f78cf
chore(deps): update dependency google-cloud-kms to v1.3.0 [(#3160)](h…
renovate-bot Mar 30, 2020
a4df66a
Simplify noxfile setup. [(#2806)](https://github.com/GoogleCloudPlatf…
kurtisvg Apr 2, 2020
e1fe4da
[kms] fix flaky test [(#3268)](https://github.com/GoogleCloudPlatform…
daniel-sanche Apr 7, 2020
b376b7b
Update dependency google-cloud-kms to v1.4.0 [(#3410)](https://github…
renovate-bot Apr 21, 2020
5581e83
chore(deps): update dependency cryptography to v2.9 [(#3266)](https:/…
renovate-bot Apr 22, 2020
b33aa1c
fix: use `crypto_key_path` instead of `crypto_key_path_path` [(#3319)…
busunkim96 Apr 22, 2020
1e095c3
chore(deps): update dependency cryptography to v2.9.1 [(#3463)](https…
renovate-bot Apr 22, 2020
aa5277b
[kms] chore: remove gcp-devrel-py-tools [(#3479)](https://github.com/…
Apr 23, 2020
6283510
Update dependency cryptography to v2.9.2 [(#3473)](https://github.com…
renovate-bot Apr 23, 2020
2f018a3
[kms] fix: use unique ids for test [(#3563)](https://github.com/Googl…
Apr 28, 2020
97d80d5
Update and add Cloud KMS samples [(#3690)](https://github.com/GoogleC…
sethvargo May 5, 2020
1b1d5da
chore: some lint fixes [(#3748)](https://github.com/GoogleCloudPlatfo…
May 13, 2020
309319a
Merge branch 'snippets-samples' into add-snippets-samples
busunkim96 May 28, 2020
2bc9430
chore: update templates
busunkim96 May 28, 2020
515d022
chore: update templates
busunkim96 May 28, 2020
990e7a6
chore: update docs index
busunkim96 May 28, 2020
7b36aeb
chore: only trigger review for python file changes
busunkim96 Jun 3, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions samples/snippets/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
.. This file is automatically generated. Do not edit this file directly.

Google Cloud KMS API 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/api-client/README.rst


This directory contains samples for Google Cloud KMS API. The `Google Cloud KMS API`_ is a service that allows you to keep encryption keys centrally in the cloud, for direct use by cloud services.




.. _Google Cloud KMS API: https://cloud.google.com/kms/docs/

Setup
-------------------------------------------------------------------------------


Authentication
++++++++++++++

This sample requires you to have authentication setup. Refer to the
`Authentication Getting Started Guide`_ for instructions on setting up
credentials for applications.

.. _Authentication Getting Started Guide:
https://cloud.google.com/docs/authentication/getting-started

Install Dependencies
++++++++++++++++++++

#. Clone python-docs-samples 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
-------------------------------------------------------------------------------

Quickstart
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

.. 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/api-client/quickstart.py,kms/api-client/README.rst




To run this sample:

.. code-block:: bash

$ python quickstart.py




.. _Google Cloud SDK: https://cloud.google.com/sdk/
19 changes: 19 additions & 0 deletions samples/snippets/README.rst.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file is used to generate README.rst

product:
name: Google Cloud KMS API
short_name: Cloud KMS API
url: https://cloud.google.com/kms/docs/
description: >
The `Google Cloud KMS API`_ is a service that allows you to keep encryption
keys centrally in the cloud, for direct use by cloud services.

setup:
- auth
- install_deps

samples:
- name: Quickstart
file: quickstart.py

folder: kms/api-client
54 changes: 54 additions & 0 deletions samples/snippets/create_key_asymmetric_decrypt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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


# [START kms_create_key_asymmetric_decrypt]
def create_key_asymmetric_decrypt(project_id, location_id, key_ring_id, id):
"""
Creates a new asymmetric decryption key in Cloud KMS.

Args:
project_id (string): Google Cloud project ID (e.g. 'my-project').
location_id (string): Cloud KMS location (e.g. 'us-east1').
key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').
id (string): ID of the key to create (e.g. 'my-asymmetric-decrypt-key').

Returns:
CryptoKey: Cloud KMS key.

"""

# Import the client library.
from google.cloud import kms

# Create the client.
client = kms.KeyManagementServiceClient()

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

# Build the key.
purpose = kms.enums.CryptoKey.CryptoKeyPurpose.ASYMMETRIC_DECRYPT
algorithm = kms.enums.CryptoKeyVersion.CryptoKeyVersionAlgorithm.RSA_DECRYPT_OAEP_2048_SHA256
key = {
'purpose': purpose,
'version_template': {
'algorithm': algorithm,
}
}

# Call the API.
created_key = client.create_crypto_key(key_ring_name, id, key)
print('Created asymmetric decrypt key: {}'.format(created_key.name))
return created_key
# [END kms_create_key_asymmetric_decrypt]
54 changes: 54 additions & 0 deletions samples/snippets/create_key_asymmetric_sign.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# 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


# [START kms_create_key_asymmetric_sign]
def create_key_asymmetric_sign(project_id, location_id, key_ring_id, id):
"""
Creates a new asymmetric signing key in Cloud KMS.

Args:
project_id (string): Google Cloud project ID (e.g. 'my-project').
location_id (string): Cloud KMS location (e.g. 'us-east1').
key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').
id (string): ID of the key to create (e.g. 'my-asymmetric-signing-key').

Returns:
CryptoKey: Cloud KMS key.

"""

# Import the client library.
from google.cloud import kms

# Create the client.
client = kms.KeyManagementServiceClient()

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

# Build the key.
purpose = kms.enums.CryptoKey.CryptoKeyPurpose.ASYMMETRIC_SIGN
algorithm = kms.enums.CryptoKeyVersion.CryptoKeyVersionAlgorithm.RSA_SIGN_PKCS1_2048_SHA256
key = {
'purpose': purpose,
'version_template': {
'algorithm': algorithm,
}
}

# Call the API.
created_key = client.create_crypto_key(key_ring_name, id, key)
print('Created asymmetric signing key: {}'.format(created_key.name))
return created_key
# [END kms_create_key_asymmetric_sign]
56 changes: 56 additions & 0 deletions samples/snippets/create_key_hsm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# 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


# [START kms_create_key_hsm]
def create_key_hsm(project_id, location_id, key_ring_id, id):
"""
Creates a new key in Cloud KMS backed by Cloud HSM.

Args:
project_id (string): Google Cloud project ID (e.g. 'my-project').
location_id (string): Cloud KMS location (e.g. 'us-east1').
key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').
id (string): ID of the key to create (e.g. 'my-hsm-key').

Returns:
CryptoKey: Cloud KMS key.

"""

# Import the client library.
from google.cloud import kms

# Create the client.
client = kms.KeyManagementServiceClient()

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

# Build the key.
purpose = kms.enums.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT
algorithm = kms.enums.CryptoKeyVersion.CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION
protection_level = kms.enums.ProtectionLevel.HSM
key = {
'purpose': purpose,
'version_template': {
'algorithm': algorithm,
'protection_level': protection_level
}
}

# Call the API.
created_key = client.create_crypto_key(key_ring_name, id, key)
print('Created hsm key: {}'.format(created_key.name))
return created_key
# [END kms_create_key_hsm]
58 changes: 58 additions & 0 deletions samples/snippets/create_key_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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


# [START kms_create_key_labels]
def create_key_labels(project_id, location_id, key_ring_id, id):
"""
Creates a new key in Cloud KMS with labels.

Args:
project_id (string): Google Cloud project ID (e.g. 'my-project').
location_id (string): Cloud KMS location (e.g. 'us-east1').
key_ring_id (string): ID of the Cloud KMS key ring (e.g. 'my-key-ring').
id (string): ID of the key to create (e.g. 'my-labeled-key').

Returns:
CryptoKey: Cloud KMS key.

"""

# Import the client library.
from google.cloud import kms

# Create the client.
client = kms.KeyManagementServiceClient()

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

# Build the key.
purpose = kms.enums.CryptoKey.CryptoKeyPurpose.ENCRYPT_DECRYPT
algorithm = kms.enums.CryptoKeyVersion.CryptoKeyVersionAlgorithm.GOOGLE_SYMMETRIC_ENCRYPTION
key = {
'purpose': purpose,
'version_template': {
'algorithm': algorithm,
},
'labels': {
'team': 'alpha',
'cost_center': 'cc1234'
}
}

# Call the API.
created_key = client.create_crypto_key(key_ring_name, id, key)
print('Created labeled key: {}'.format(created_key.name))
return created_key
# [END kms_create_key_labels]
46 changes: 46 additions & 0 deletions samples/snippets/create_key_ring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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


# [START kms_create_key_ring]
def create_key_ring(project_id, location_id, id):
"""
Creates a new key ring in Cloud KMS

Args:
project_id (string): Google Cloud project ID (e.g. 'my-project').
location_id (string): Cloud KMS location (e.g. 'us-east1').
id (string): ID of the key ring to create (e.g. 'my-key-ring').

Returns:
KeyRing: Cloud KMS key ring.

"""

# Import the client library.
from google.cloud import kms

# Create the client.
client = kms.KeyManagementServiceClient()

# Build the parent location name.
location_name = client.location_path(project_id, location_id)

# Build the key ring.
key_ring = {}

# Call the API.
created_key_ring = client.create_key_ring(location_name, id, key_ring)
print('Created key ring: {}'.format(created_key_ring.name))
return created_key_ring
# [END kms_create_key_ring]
Loading