Skip to content

Commit

Permalink
Python KMS Apiary P1 samples (#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 dpebot committed Feb 7, 2017
1 parent faddda1 commit f7179af
Show file tree
Hide file tree
Showing 8 changed files with 638 additions and 10 deletions.
14 changes: 7 additions & 7 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,13 @@ def api_client_inject_project_id(cloud_config):
the project ID from cloud_config."""
import googleapiclient.http

class ProjectIdInjectingHttpRequest(googleapiclient.http.HttpRequest):
def __init__(self, http, postproc, uri, *args, **kwargs):
uri = uri.replace('YOUR_PROJECT_ID', cloud_config.project)
super(ProjectIdInjectingHttpRequest, self).__init__(
http, postproc, uri, *args, **kwargs)
old_execute = googleapiclient.http.HttpRequest.execute

def new_execute(self, http=None, num_retries=0):
self.uri = self.uri.replace('YOUR_PROJECT_ID', cloud_config.project)
return old_execute(self, http=http, num_retries=num_retries)

with mock.patch(
'googleapiclient.http.HttpRequest',
new=ProjectIdInjectingHttpRequest):
'googleapiclient.http.HttpRequest.execute',
new=new_execute):
yield
109 changes: 109 additions & 0 deletions kms/api-client/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/
3 changes: 3 additions & 0 deletions kms/api/README.rst.in → kms/api-client/README.rst.in
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ setup:
samples:
- name: Quickstart
file: quickstart.py
- name: Snippets
file: snippets.py
show_help: True
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
# limitations under the License.


def test_quickstart(api_client_inject_project_id, capsys):
def test_quickstart(api_client_inject_project_id):
import quickstart

quickstart.run_quickstart()
out, _ = capsys.readouterr()
assert 'No key rings found' in out
File renamed without changes.
Loading

0 comments on commit f7179af

Please sign in to comment.