|
| 1 | +# Copyright 2019 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the 'License'); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an 'AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +""" |
| 15 | +Samples to illustrate management of HMAC keys via the python client library. |
| 16 | +""" |
| 17 | + |
| 18 | + |
| 19 | +from google.cloud import storage |
| 20 | + |
| 21 | + |
| 22 | +def list_keys(project_id): |
| 23 | + """ |
| 24 | + List all HMAC keys associated with the project. |
| 25 | + """ |
| 26 | + # [START storage_list_hmac_keys] |
| 27 | + # project_id = 'Your Google Cloud project ID' |
| 28 | + storage_client = storage.Client(project=project_id) |
| 29 | + hmac_keys = storage_client.list_hmac_keys(project_id=project_id) |
| 30 | + print('HMAC Keys:') |
| 31 | + for hmac_key in hmac_keys: |
| 32 | + print('The HMAC key metadata is:') |
| 33 | + print('Key ID: {}'.format(hmac_key.id)) |
| 34 | + print('Access ID: {}'.format(hmac_key.access_id)) |
| 35 | + print('Project ID: {}'.format(hmac_key.project)) |
| 36 | + print('State: {}'.format(hmac_key.state)) |
| 37 | + print('Created At: {}'.format(hmac_key.time_created)) |
| 38 | + print('Updated At: {}'.format(hmac_key.updated)) |
| 39 | + print('Etag: {}'.format(hmac_key.etag)) |
| 40 | + # [END storage_list_hmac_keys] |
| 41 | + return hmac_keys |
| 42 | + |
| 43 | + |
| 44 | +def create_key(project_id, service_account_email): |
| 45 | + """ |
| 46 | + Create a new HMAC key using the given project and service account. |
| 47 | + """ |
| 48 | + # [START storage_create_hmac_key] |
| 49 | + # project_id = 'Your Google Cloud project ID' |
| 50 | + # service_account_email = 'Service account used to generate HMAC key' |
| 51 | + storage_client = storage.Client(project=project_id) |
| 52 | + hmac_key, secret = storage_client.create_hmac_key( |
| 53 | + service_account_email=service_account_email, |
| 54 | + project_id=project_id) |
| 55 | + print('The base64 encoded secret is {}'.format(secret)) |
| 56 | + print('Do not miss that secret, there is no API to recover it.') |
| 57 | + print('The HMAC key metadata is:') |
| 58 | + print('Key ID: {}'.format(hmac_key.id)) |
| 59 | + print('Access ID: {}'.format(hmac_key.access_id)) |
| 60 | + print('Project ID: {}'.format(hmac_key.project)) |
| 61 | + print('State: {}'.format(hmac_key.state)) |
| 62 | + print('Created At: {}'.format(hmac_key.time_created)) |
| 63 | + print('Updated At: {}'.format(hmac_key.updated)) |
| 64 | + print('Etag: {}'.format(hmac_key.etag)) |
| 65 | + # [END storage_create_hmac_key] |
| 66 | + return hmac_key |
| 67 | + |
| 68 | + |
| 69 | +def get_key(access_id, project_id): |
| 70 | + """ |
| 71 | + Retrieve the HMACKeyMetadata with the given access id. |
| 72 | + """ |
| 73 | + # [START storage_get_hmac_key] |
| 74 | + # project_id = 'Your Google Cloud project ID' |
| 75 | + # access_id = 'ID of an HMAC key' |
| 76 | + storage_client = storage.Client(project=project_id) |
| 77 | + hmac_key = storage_client.get_hmac_key_metadata( |
| 78 | + access_id, |
| 79 | + project_id=project_id) |
| 80 | + print('The HMAC key metadata is:') |
| 81 | + print('Key ID: {}'.format(hmac_key.id)) |
| 82 | + print('Access ID: {}'.format(hmac_key.access_id)) |
| 83 | + print('Project ID: {}'.format(hmac_key.project)) |
| 84 | + print('State: {}'.format(hmac_key.state)) |
| 85 | + print('Created At: {}'.format(hmac_key.time_created)) |
| 86 | + print('Updated At: {}'.format(hmac_key.updated)) |
| 87 | + print('Etag: {}'.format(hmac_key.etag)) |
| 88 | + # [END storage_get_hmac_key] |
| 89 | + return hmac_key |
| 90 | + |
| 91 | + |
| 92 | +def activate_key(access_id, project_id): |
| 93 | + """ |
| 94 | + Activate the HMAC key with the given access ID. |
| 95 | + """ |
| 96 | + # [START storage_activate_hmac_key] |
| 97 | + # project_id = 'Your Google Cloud project ID' |
| 98 | + # access_id = 'ID of an inactive HMAC key' |
| 99 | + storage_client = storage.Client(project=project_id) |
| 100 | + hmac_key = storage_client.get_hmac_key_metadata( |
| 101 | + access_id, |
| 102 | + project_id=project_id) |
| 103 | + hmac_key.state = 'ACTIVE' |
| 104 | + hmac_key.update() |
| 105 | + print('The HMAC key metadata is:') |
| 106 | + print('Key ID: {}'.format(hmac_key.id)) |
| 107 | + print('Access ID: {}'.format(hmac_key.access_id)) |
| 108 | + print('Project ID: {}'.format(hmac_key.project)) |
| 109 | + print('State: {}'.format(hmac_key.state)) |
| 110 | + print('Created At: {}'.format(hmac_key.time_created)) |
| 111 | + print('Updated At: {}'.format(hmac_key.updated)) |
| 112 | + print('Etag: {}'.format(hmac_key.etag)) |
| 113 | + # [END storage_activate_hmac_key] |
| 114 | + return hmac_key |
| 115 | + |
| 116 | + |
| 117 | +def deactivate_key(access_id, project_id): |
| 118 | + """ |
| 119 | + Deactivate the HMAC key with the given access ID. |
| 120 | + """ |
| 121 | + # [START storage_deactivate_hmac_key] |
| 122 | + # project_id = 'Your Google Cloud project ID' |
| 123 | + # access_id = 'ID of an active HMAC key' |
| 124 | + storage_client = storage.Client(project=project_id) |
| 125 | + hmac_key = storage_client.get_hmac_key_metadata( |
| 126 | + access_id, |
| 127 | + project_id=project_id) |
| 128 | + hmac_key.state = 'INACTIVE' |
| 129 | + hmac_key.update() |
| 130 | + print('The HMAC key is now inactive.') |
| 131 | + print('The HMAC key metadata is:') |
| 132 | + print('Key ID: {}'.format(hmac_key.id)) |
| 133 | + print('Access ID: {}'.format(hmac_key.access_id)) |
| 134 | + print('Project ID: {}'.format(hmac_key.project)) |
| 135 | + print('State: {}'.format(hmac_key.state)) |
| 136 | + print('Created At: {}'.format(hmac_key.time_created)) |
| 137 | + print('Updated At: {}'.format(hmac_key.updated)) |
| 138 | + print('Etag: {}'.format(hmac_key.etag)) |
| 139 | + # [END storage_deactivate_hmac_key] |
| 140 | + return hmac_key |
| 141 | + |
| 142 | + |
| 143 | +def delete_key(access_id, project_id): |
| 144 | + """ |
| 145 | + Delete the HMAC key with the given access ID. Key must have state INACTIVE |
| 146 | + in order to succeed. |
| 147 | + """ |
| 148 | + # [START storage_delete_hmac_key] |
| 149 | + # project_id = 'Your Google Cloud project ID' |
| 150 | + # access_id = 'ID of an HMAC key (must be in INACTIVE state)' |
| 151 | + storage_client = storage.Client(project=project_id) |
| 152 | + hmac_key = storage_client.get_hmac_key_metadata( |
| 153 | + access_id, |
| 154 | + project_id=project_id) |
| 155 | + hmac_key.delete() |
| 156 | + print('The key is deleted, though it may still appear in list_hmac_keys()' |
| 157 | + ' results.') |
| 158 | + # [END storage_delete_hmac_key] |
0 commit comments