Skip to content

Commit

Permalink
Python KMS Apiary P1 samples [(#779)](#779)
Browse files Browse the repository at this point in the history
* Draft of first half of KMS samples

* reversed wrong change

* KMS Apiary Python samples - P1

* Few minor style issues

* Adding back in space i accidentally deleted

* Addressed all code review comments

* Renamed api directory to api-client

* Addressed more code review comments

* Formatting change

* Fix quickstart test

Change-Id: Ib79dc1345c9c40547f3fd4e9c3c9a48963a3b399

* Update readme

Change-Id: Icf4a66083f56d6f51be76ba1cf3b5dc8daf2c4c1

* Add readme

Change-Id: I2fbaa55092ef8787f1423d499aa310cab258c0c1

* Added parsers

* Final minor changes to parsers

* Added autogenerated README

* Changed snippets_test keyring name and cryptokey name
  • Loading branch information
ryanmats authored and rsamborski committed Nov 14, 2022
1 parent 0a52d22 commit c3dae48
Show file tree
Hide file tree
Showing 7 changed files with 715 additions and 0 deletions.
109 changes: 109 additions & 0 deletions kms/snippets/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
.. This file is automatically generated. Do not edit this file directly.
Google Cloud KMS API Python Samples
===============================================================================

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
++++++++++++++

Authentication is typically done through `Application Default Credentials`_,
which means you do not have to change the code to authenticate as long as
your environment has credentials. You have a few options for setting up
authentication:

#. When running locally, use the `Google Cloud SDK`_

.. code-block:: bash
gcloud beta auth application-default login
#. When running on App Engine or Compute Engine, credentials are already
set-up. However, you may need to configure your Compute Engine instance
with `additional scopes`_.

#. You can create a `Service Account key file`_. This file can be used to
authenticate to Google Cloud Platform services from any environment. To use
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
the path to the key file, for example:

.. code-block:: bash
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using
.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount

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

#. Install `pip`_ and `virtualenv`_ if you do not already have them.

#. 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
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



To run this sample:

.. code-block:: bash
$ python quickstart.py
Snippets
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



To run this sample:

.. code-block:: bash
$ python snippets.py
usage: snippets.py [-h]
{create_keyring,create_cryptokey,encrypt,decrypt,disable_cryptokey_version,destroy_cryptokey_version,add_member_to_cryptokey_policy,get_keyring_policy}
...
positional arguments:
{create_keyring,create_cryptokey,encrypt,decrypt,disable_cryptokey_version,destroy_cryptokey_version,add_member_to_cryptokey_policy,get_keyring_policy}
optional arguments:
-h, --help show this help message and exit
.. _Google Cloud SDK: https://cloud.google.com/sdk/
20 changes: 20 additions & 0 deletions kms/snippets/README.rst.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# 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
- name: Snippets
file: snippets.py
show_help: True
48 changes: 48 additions & 0 deletions kms/snippets/quickstart.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python

# Copyright 2017 Google, Inc
#
# 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


def run_quickstart():
# [START kms_quickstart]
# Imports the Google APIs client library
from googleapiclient import discovery

# Your Google Cloud Platform project ID
project_id = 'YOUR_PROJECT_ID'

# Lists keys in the "global" location.
location = 'global'

# Creates an API client for the KMS API.
kms_client = discovery.build('cloudkms', 'v1beta1')

# The resource name of the location associated with the key rings.
parent = 'projects/{}/locations/{}'.format(project_id, location)

# Lists key rings
request = kms_client.projects().locations().keyRings().list(parent=parent)
response = request.execute()

if 'keyRings' in response and response['keyRings']:
print('Key rings:')
for key_ring in response['keyRings']:
print(key_ring['name'])
else:
print('No key rings found.')
# [END kms_quickstart]


if __name__ == '__main__':
run_quickstart()
19 changes: 19 additions & 0 deletions kms/snippets/quickstart_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright 2017 Google Inc. All Rights Reserved.
#
# 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.


def test_quickstart(api_client_inject_project_id):
import quickstart

quickstart.run_quickstart()
1 change: 1 addition & 0 deletions kms/snippets/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-api-python-client==1.6.1
Loading

0 comments on commit c3dae48

Please sign in to comment.