8000 Storage: HMAC key samples (#2372) · hanv89/python-docs-samples@ac0ed47 · GitHub
[go: up one dir, main page]

Skip to content

Commit ac0ed47

Browse files
authored
Storage: HMAC key samples (GoogleCloudPlatform#2372)
Add samples for HMAC key functionality: list, create, get, activate, deactivate, delete. Includes tests and version bump for client library.
1 parent 678fb5b commit ac0ed47

File tree

4 files changed

+254
-1
lines changed

4 files changed

+254
-1
lines changed

storage/cloud-client/hmac.py

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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]

storage/cloud-client/hmac_test.py

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
Tests for hmac.py. Requires GOOGLE_CLOUD_PROJECT (valid project) and
16+
HMAC_KEY_TEST_SERVICE_ACCOUNT (valid service account email) env variables to be
17+
set in order to run.
18+
"""
19+
20+
21+
import os
22+
23+
from google.cloud import storage
24+
import pytest
25+
26+
import hmac
27+
28+
29+
PROJECT_ID = os.environ['GOOGLE_CLOUD_PROJECT']
30+
SERVICE_ACCOUNT_EMAIL = os.environ['HMAC_KEY_TEST_SERVICE_ACCOUNT']
31+
STORAGE_CLIENT = storage.Client(project=PROJECT_ID)
32+
33+
34+
@pytest.fixture
35+
def new_hmac_key():
36+
"""
37+
Fixture to create a new HMAC key, and to guarantee all keys are deleted at
38+
the end of each test.
39+
"""
40+
hmac_key, secret = STORAGE_CLIENT.create_hmac_key(
41+
service_account_email=SERVICE_ACCOUNT_EMAIL,
42+
project_id=PROJECT_ID)
43+
yield hmac_key
44+
# Re-fetch the key metadata in case state has changed during the test.
45+
hmac_key = STORAGE_CLIENT.get_hmac_key_metadata(
46+
hmac_key.access_id,
47+
project_id=PROJECT_ID)
48+
if hmac_key.state == 'DELETED':
49+
return
50+
if not hmac_key.state == 'INACTIVE':
51+
hmac_key.state = 'INACTIVE'
52+
hmac_key.update()
53+
hmac_key.delete()
54+
55+
56+
def test_list_keys(capsys, new_hmac_key):
57+
hmac_keys = hmac.list_keys(PROJECT_ID)
58+
assert 'HMAC Keys:' in capsys.readouterr().out
59+
assert hmac_keys.num_results >= 1
60+
61+
62+
def test_create_key(capsys):
63+
hmac_key = hmac.create_key(PROJECT_ID, SERVICE_ACCOUNT_EMAIL)
64+
hmac_key.state = 'INACTIVE'
65+
hmac_key.update()
66+
hmac_key.delete()
67+
assert 'Key ID:' in capsys.readouterr().out
68+
assert hmac_key.access_id
69+
70+
71+
def test_get_key(capsys, new_hmac_key):
72+
hmac_key = hmac.get_key(new_hmac_key.access_id, PROJECT_ID)
73+
assert 'HMAC key metadata' in capsys.readouterr().out
74+
assert hmac_key.access_id == new_hmac_key.access_id
75+
76+
77+
def test_activate_key(capsys, new_hmac_key):
78+
new_hmac_key.state = 'INACTIVE'
79+
new_hmac_key.update()
80+
hmac_key = hmac.activate_key(new_hmac_key.access_id, PROJECT_ID)
81+
assert 'State: ACTIVE' in capsys.readouterr().out
82+
assert hmac_key.state == 'ACTIVE'
83+
84+
85+
def test_deactivate_key(capsys, new_hmac_key):
86+
hmac_key = hmac.deactivate_key(new_hmac_key.access_id, PROJECT_ID)
87+
assert 'State: INACTIVE' in capsys.readouterr().out
88+
assert hmac_key.state == 'INACTIVE'
89+
90+
91+
def test_delete_key(capsys, new_hmac_key):
92+
new_hmac_key.state = 'INACTIVE'
93+
new_hmac_key.update()
94+
hmac.delete_key(new_hmac_key.access_id, PROJECT_ID)
95+
assert 'The key is deleted' in capsys.readouterr().out

storage/cloud-client/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
google-cloud-pubsub==0.39.1
2-
google-cloud-storage==1.17.0
2+
google-cloud-storage==1.19.0

testing/secrets.tar.enc

512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)
0