This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathhmac_samples_test.py
More file actions
139 lines (117 loc) · 4.79 KB
/
hmac_samples_test.py
File metadata and controls
139 lines (117 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# Copyright 2019 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.
"""
Tests for hmac.py. Requires GOOGLE_CLOUD_PROJECT (valid project) and
HMAC_KEY_TEST_SERVICE_ACCOUNT (valid service account email) env variables to be
set in order to run.
"""
import os
import google.api_core.exceptions
from google.cloud import storage
import pytest
import storage_activate_hmac_key
import storage_create_hmac_key
import storage_deactivate_hmac_key
import storage_delete_hmac_key
import storage_get_hmac_key
import storage_list_hmac_keys
# We are reaching maximum number of HMAC keys on the service account.
# We change the service account based on the value of
# RUN_TESTS_SESSION in noxfile_config.py.
# The reason we can not use multiple project is that our new projects
# are enforced to have
# 'constraints/iam.disableServiceAccountKeyCreation' policy.
PROJECT_ID = os.environ["MAIN_GOOGLE_CLOUD_PROJECT"]
SERVICE_ACCOUNT_EMAIL = os.environ["HMAC_KEY_TEST_SERVICE_ACCOUNT"]
STORAGE_CLIENT = storage.Client(project=PROJECT_ID)
@pytest.fixture(scope="module")
def new_hmac_key():
"""
Fixture to create a new HMAC key, and to guarantee all keys are deleted at
the end of the module.
NOTE: Due to the module scope, test order in this file is significant
"""
try:
hmac_key, secret = STORAGE_CLIENT.create_hmac_key(
service_account_email=SERVICE_ACCOUNT_EMAIL, project_id=PROJECT_ID
)
except google.api_core.exceptions.PreconditionFailed as e:
# Check if the failure is due to the Organization Policy constraint
if "constraints/iam.disableServiceAccountKeyCreation" in str(e):
pytest.skip(
"Temporary skip: HMAC key creation is disabled by organization policy "
"on project python-docs-samples-tests. See b/493225655."
)
raise
yield hmac_key
# Re-fetch the key metadata in case state has changed during the test.
hmac_key = STORAGE_CLIENT.get_hmac_key_metadata(
hmac_key.access_id, project_id=PROJECT_ID
)
if hmac_key.state == "DELETED":
return
if not hmac_key.state == "INACTIVE":
hmac_key.state = "INACTIVE"
hmac_key.update()
try:
hmac_key.delete()
except google.api_core.exceptions.BadRequest:
pass
def test_list_keys(capsys, new_hmac_key):
hmac_keys = storage_list_hmac_keys.list_keys(PROJECT_ID)
assert "HMAC Keys:" in capsys.readouterr().out
assert hmac_keys.num_results >= 1
def test_create_key(capsys):
try:
hmac_key = storage_create_hmac_key.create_key(PROJECT_ID, SERVICE_ACCOUNT_EMAIL)
except google.api_core.exceptions.PreconditionFailed as e:
if "constraints/iam.disableServiceAccountKeyCreation" in str(e):
pytest.skip(
"Temporary skip: HMAC key creation is disabled by organization policy "
"on project python-docs-samples-tests. See b/493225655."
)
raise
hmac_key.state = "INACTIVE"
hmac_key.update()
hmac_key.delete()
assert "Key ID:" in capsys.readouterr().out
assert hmac_key.access_id
def test_get_key(capsys, new_hmac_key):
hmac_key = storage_get_hmac_key.get_key(new_hmac_key.access_id, PROJECT_ID)
assert "HMAC key metadata" in capsys.readouterr().out
assert hmac_key.access_id == new_hmac_key.access_id
def test_activate_key(capsys, new_hmac_key):
new_hmac_key.state = "INACTIVE"
new_hmac_key.update()
hmac_key = storage_activate_hmac_key.activate_key(
new_hmac_key.access_id, PROJECT_ID
)
assert "State: ACTIVE" in capsys.readouterr().out
assert hmac_key.state == "ACTIVE"
def test_deactivate_key(capsys, new_hmac_key):
hmac_key = storage_deactivate_hmac_key.deactivate_key(
new_hmac_key.access_id, PROJECT_ID
)
assert "State: INACTIVE" in capsys.readouterr().out
assert hmac_key.state == "INACTIVE"
def test_delete_key(capsys, new_hmac_key):
# Due to reuse of the HMAC key for each test function, the previous
# test has deactivated the key already.
try:
new_hmac_key.state = "INACTIVE"
new_hmac_key.update()
except google.api_core.exceptions.BadRequest:
pass
storage_delete_hmac_key.delete_key(new_hmac_key.access_id, PROJECT_ID)
assert "The key is deleted" in capsys.readouterr().out