Skip to content

Commit cdfa595

Browse files
fix(iam): fix tests in cloud-client/snippets (#13097)
* fix(iam): proof of concept to check if it passes Kokoro CI * fix(iam): WIP comment unused import * fix(iam): add exponential backoff to quickstart_test.py::test_member * fix(iam): fixes for linting * fix(iam): fix create_servce_account.py to be run from CLI * fix(iam): format create_deny_policy.py * fix(iam): format iam_check_permissions.py * fix(iam): add exponential backoff to service account creation, and enable failing test * fix(iam): fix Kokoro CI failing for quickstart_test.py * fix(iam): fix linting in quickstart_test * fix(iam): add exponentia backoff to test_service_account * fix(iam): fix linting in test_service_account.py * fix(iam): format disable_service_account.py * fix(iam): format modify_policy_add_member.py * fix(iam): format query_testable_permissions.py * fix(iam): update requirements.txt to latest dependencies * fix(iam): fix return value in service_account_email fixture * fix(iam): unify style across the folder and resolve linter suggestions * fix(iam): add more time to sleep to fix failing test * fix(iam): add exponential backoff to test_service_account_key * fix(iam): fix linting for test_service_account_key * fix(iam): fix fixture for test_service_account_key.py * fix(iam): add messages to quickstart_test * fix(iam): add backoff to 'test_disable_role' to fix "Please retry the whole read-modify-write with exponential backoff."
1 parent b26e054 commit cdfa595

38 files changed

+501
-342
lines changed

iam/cloud-client/snippets/conftest.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
from snippets.get_role import get_role
3232

3333
PROJECT = google.auth.default()[1]
34-
GOOGLE_APPLICATION_CREDENTIALS = os.environ["IAM_CREDENTIALS"]
34+
GOOGLE_APPLICATION_CREDENTIALS = os.getenv("IAM_CREDENTIALS", "")
3535

3636

3737
@pytest.fixture
@@ -87,6 +87,7 @@ def iam_role() -> str:
8787
role_id = f"{role_prefix}_{uuid.uuid4().hex[:10]}"
8888
permissions = ["iam.roles.get", "iam.roles.list"]
8989
title = "test_role_title"
90+
9091
# Delete any iam roles with `role_prefix` prefix. Otherwise, it might throw quota issue.
9192
delete_iam_roles_by_prefix(PROJECT, role_prefix)
9293
created = False
@@ -103,12 +104,12 @@ def iam_role() -> str:
103104

104105

105106
def delete_iam_roles_by_prefix(iam_role: str, delete_name_prefix: str) -> None:
106-
"""
107-
Helper function to clean-up roles starting with a prefix
107+
"""Helper function to clean-up roles starting with a prefix.
108+
108109
Args:
109110
iam_role: project id
110-
delete_name_prefix: start of the role id to be deleted. F.e. "test-role" in role id "test-role-123"
111-
111+
delete_name_prefix: start of the role id to be deleted.
112+
F.e. "test-role" in role id "test-role-123"
112113
"""
113114
client = IAMClient()
114115
parent = f"projects/{PROJECT}"

iam/cloud-client/snippets/create_deny_policy.py

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,29 @@
1414

1515
# This file contains code samples that demonstrate how to create IAM deny policies.
1616

17-
# [START iam_create_deny_policy]
17+
import os
1818

1919

20+
# [START iam_create_deny_policy]
2021
def create_deny_policy(project_id: str, policy_id: str) -> None:
22+
"""Create a deny policy.
23+
24+
You can add deny policies to organizations, folders, and projects.
25+
Each of these resources can have up to 5 deny policies.
26+
27+
Deny policies contain deny rules, which specify the following:
28+
1. The permissions to deny and/or exempt.
29+
2. The principals that are denied, or exempted from denial.
30+
3. An optional condition on when to enforce the deny rules.
31+
32+
Params:
33+
project_id: ID or number of the Google Cloud project you want to use.
34+
policy_id: Specify the ID of the deny policy you want to create.
35+
"""
36+
2137
from google.cloud import iam_v2
2238
from google.cloud.iam_v2 import types
2339

24-
"""
25-
Create a deny policy.
26-
You can add deny policies to organizations, folders, and projects.
27-
Each of these resources can have up to 5 deny policies.
28-
29-
Deny policies contain deny rules, which specify the following:
30-
1. The permissions to deny and/or exempt.
31-
2. The principals that are denied, or exempted from denial.
32-
3. An optional condition on when to enforce the deny rules.
33-
34-
Params:
35-
project_id: ID or number of the Google Cloud project you want to use.
36-
policy_id: Specify the ID of the deny policy you want to create.
37-
"""
3840
policies_client = iam_v2.PoliciesClient()
3941

4042
# Each deny policy is attached to an organization, folder, or project.
@@ -108,11 +110,11 @@ def create_deny_policy(project_id: str, policy_id: str) -> None:
108110
import uuid
109111

110112
# Your Google Cloud project ID.
111-
project_id = "your-google-cloud-project-id"
113+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "your-google-cloud-project-id")
114+
112115
# Any unique ID (0 to 63 chars) starting with a lowercase letter.
113116
policy_id = f"deny-{uuid.uuid4()}"
114117

115118
# Test the policy lifecycle.
116-
create_deny_policy(project_id, policy_id)
117-
119+
create_deny_policy(PROJECT_ID, policy_id)
118120
# [END iam_create_deny_policy]

iam/cloud-client/snippets/create_key.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
# This file contains code samples that demonstrate how to get create IAM key for service account.
1616

17+
import os
18+
1719
# [START iam_create_key]
1820
from google.cloud import iam_admin_v1
1921
from google.cloud.iam_admin_v1 import types
@@ -41,8 +43,6 @@ def create_key(project_id: str, account: str) -> types.ServiceAccountKey:
4143
# key_id = json_key_data["private_key_id"]
4244

4345
return key
44-
45-
4646
# [END iam_create_key]
4747

4848

@@ -51,10 +51,12 @@ def create_key(project_id: str, account: str) -> types.ServiceAccountKey:
5151
# iam.serviceAccountKeys.create permission (roles/iam.serviceAccountKeyAdmin)
5252

5353
# Your Google Cloud project ID.
54-
project_id = "your-google-cloud-project-id"
54+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "your-google-cloud-project-id")
55+
5556
# Existing service account name within the project specified above.
5657
account_name = "test-account-name"
58+
5759
# Note: If you have different email format, you can just paste it directly
58-
email = f"{account_name}@{project_id}.iam.gserviceaccount.com"
60+
email = f"{account_name}@{PROJECT_ID}.iam.gserviceaccount.com"
5961

60-
create_key(project_id, email)
62+
create_key(PROJECT_ID, email)

iam/cloud-client/snippets/create_role.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
16+
1517
# [START iam_create_role]
1618
from typing import List, Optional
1719

@@ -22,8 +24,8 @@
2224
def create_role(
2325
project_id: str, role_id: str, permissions: List[str], title: Optional[str] = None
2426
) -> Role:
25-
"""
26-
Creates iam role with given parameters.
27+
"""Creates iam role with given parameters.
28+
2729
Args:
2830
project_id: GCP project id
2931
role_id: id of GCP iam role
@@ -51,16 +53,14 @@ def create_role(
5153
print(
5254
f"Role with id [{role_id}] already exists and in deleted state, take some actions"
5355
)
54-
55-
5656
# [END iam_create_role]
5757

5858

5959
if __name__ == "__main__":
60-
import google.auth
60+
# Your Google Cloud project ID.
61+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "your-google-cloud-project-id")
6162

62-
PROJECT = google.auth.default()[1]
6363
role_id = "custom1_python"
6464
permissions = ["iam.roles.get", "iam.roles.list"]
6565
title = "custom1_python_title"
66-
create_role(PROJECT, role_id, permissions, title)
66+
create_role(PROJECT_ID, role_id, permissions, title)

iam/cloud-client/snippets/create_service_account.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
# This file contains code samples that demonstrate how to get create service account.
15+
# This file contains code samples that demonstrate
16+
# how to create a service account.
17+
18+
import os
1619

1720
# [START iam_create_service_account]
1821
from typing import Optional
@@ -24,12 +27,14 @@
2427
def create_service_account(
2528
project_id: str, account_id: str, display_name: Optional[str] = None
2629
) -> types.ServiceAccount:
27-
"""
28-
Creates a service account.
30+
"""Creates a service account.
2931
3032
project_id: ID or number of the Google Cloud project you want to use.
3133
account_id: ID which will be unique identifier of the service account
32-
display_name (optional): human-readable name, which will be assigned to the service account
34+
display_name (optional): human-readable name, which will be assigned
35+
to the service account
36+
37+
return: ServiceAccount
3338
"""
3439

3540
iam_admin_client = iam_admin_v1.IAMClient()
@@ -46,8 +51,6 @@ def create_service_account(
4651

4752
print(f"Created a service account: {account.email}")
4853
return account
49-
50-
5154
# [END iam_create_service_account]
5255

5356

@@ -56,9 +59,10 @@ def create_service_account(
5659
# iam.serviceAccounts.create permission (roles/iam.serviceAccountCreator)
5760

5861
# Your Google Cloud project ID.
59-
project_id = "your-google-cloud-project-id"
62+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "your-google-cloud-project-id")
6063

6164
# Existing service account name within the project specified above.
62-
account_id = account_name = "test-service-account"
65+
ACCOUNT_ID = os.getenv("ACCOUNT_ID", "test-service-account")
66+
DISPLAY_NAME = ACCOUNT_ID
6367

64-
create_service_account(project_id, account_id, account_name)
68+
create_service_account(PROJECT_ID, ACCOUNT_ID, DISPLAY_NAME)

iam/cloud-client/snippets/delete_deny_policy.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414

1515
# This file contains code samples that demonstrate how to delete IAM deny policies.
1616

17+
import os
18+
1719

1820
# [START iam_delete_deny_policy]
1921
def delete_deny_policy(project_id: str, policy_id: str) -> None:
20-
from google.cloud import iam_v2
21-
from google.cloud.iam_v2 import types
22-
23-
"""
24-
Delete the policy if you no longer want to enforce the rules in a deny policy.
22+
"""Delete the policy if you no longer want to enforce the rules in a deny policy.
2523
2624
project_id: ID or number of the Google Cloud project you want to use.
2725
policy_id: The ID of the deny policy you want to retrieve.
2826
"""
27+
28+
from google.cloud import iam_v2
29+
from google.cloud.iam_v2 import types
30+
2931
policies_client = iam_v2.PoliciesClient()
3032

3133
# Each deny policy is attached to an organization, folder, or project.
@@ -54,10 +56,10 @@ def delete_deny_policy(project_id: str, policy_id: str) -> None:
5456
import uuid
5557

5658
# Your Google Cloud project ID.
57-
project_id = "your-google-cloud-project-id"
59+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "your-google-cloud-project-id")
60+
5861
# Any unique ID (0 to 63 chars) starting with a lowercase letter.
5962
policy_id = f"deny-{uuid.uuid4()}"
6063

61-
delete_deny_policy(project_id, policy_id)
62-
64+
delete_deny_policy(PROJECT_ID, policy_id)
6365
# [END iam_delete_deny_policy]

iam/cloud-client/snippets/delete_key.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414

1515
# This file contains code samples that demonstrate how to get delete IAM key for service account.
1616

17+
import os
18+
1719
# [START iam_delete_key]
1820
from google.cloud import iam_admin_v1
1921
from google.cloud.iam_admin_v1 import types
2022

2123

2224
def delete_key(project_id: str, account: str, key_id: str) -> None:
23-
"""
24-
Deletes a key for a service account.
25+
"""Deletes a key for a service account.
2526
2627
project_id: ID or number of the Google Cloud project you want to use.
2728
account: ID or email which is unique identifier of the service account.
@@ -34,8 +35,6 @@ def delete_key(project_id: str, account: str, key_id: str) -> None:
3435

3536
iam_admin_client.delete_service_account_key(request=request)
3637
print(f"Deleted key: {key_id}")
37-
38-
3938
# [END iam_delete_key]
4039

4140

@@ -44,12 +43,13 @@ def delete_key(project_id: str, account: str, key_id: str) -> None:
4443
# iam.serviceAccountKeys.delete permission (roles/iam.serviceAccountKeyAdmin)
4544

4645
# Your Google Cloud project ID.
47-
project_id = "your-google-cloud-project-id"
46+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "your-google-cloud-project-id")
47+
4848
# Existing service account name within the project specified above.
4949
account_name = "test-account-name"
5050
# Existing ID of the key
5151
key_id = "your-key-id"
5252
# Note: If you have different email format, you can just paste it directly
53-
email = f"{account_name}@{project_id}.iam.gserviceaccount.com"
53+
email = f"{account_name}@{PROJECT_ID}.iam.gserviceaccount.com"
5454

55-
delete_key(project_id, email, key_id)
55+
delete_key(PROJECT_ID, email, key_id)

iam/cloud-client/snippets/delete_role.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
import os
16+
17+
1518
# [START iam_delete_role]
1619
# [START iam_undelete_role]
1720
from google.api_core.exceptions import FailedPrecondition, NotFound
@@ -21,15 +24,13 @@
2124
Role,
2225
UndeleteRoleRequest,
2326
)
24-
2527
# [END iam_undelete_role]
2628
# [END iam_delete_role]
2729

2830

2931
# [START iam_delete_role]
3032
def delete_role(project_id: str, role_id: str) -> Role:
31-
"""
32-
Deletes iam role in GCP project. Can be undeleted later
33+
"""Deletes iam role in GCP project. Can be undeleted later.
3334
Args:
3435
project_id: GCP project id
3536
role_id: id of GCP iam role
@@ -47,20 +48,16 @@ def delete_role(project_id: str, role_id: str) -> Role:
4748
print(f"Role with id [{role_id}] not found, take some actions")
4849
except FailedPrecondition as err:
4950
print(f"Role with id [{role_id}] already deleted, take some actions)", err)
50-
51-
5251
# [END iam_delete_role]
5352

5453

5554
# [START iam_undelete_role]
5655
def undelete_role(project_id: str, role_id: str) -> Role:
57-
"""
58-
Undeleted deleted iam role in GCP project
56+
"""Undeleted deleted iam role in GCP project.
57+
5958
Args:
6059
project_id: GCP project id
6160
role_id: id of GCP iam role
62-
63-
Returns: google.cloud.iam_admin_v1.Role object
6461
"""
6562
client = IAMClient()
6663
name = f"projects/{project_id}/roles/{role_id}"
@@ -73,15 +70,13 @@ def undelete_role(project_id: str, role_id: str) -> Role:
7370
print(f"Role with id [{role_id}] not found, take some actions")
7471
except FailedPrecondition as err:
7572
print(f"Role with id [{role_id}] is not deleted, take some actions)", err)
76-
77-
7873
# [END iam_undelete_role]
7974

8075

8176
if __name__ == "__main__":
82-
import google.auth
77+
# Your Google Cloud project ID.
78+
PROJECT_ID = os.getenv("GOOGLE_CLOUD_PROJECT", "your-google-cloud-project-id")
8379

84-
PROJECT = google.auth.default()[1]
8580
role_id = "custom1_python"
86-
delete_role(PROJECT, role_id)
87-
undelete_role(PROJECT, role_id)
81+
delete_role(PROJECT_ID, role_id)
82+
undelete_role(PROJECT_ID, role_id)

0 commit comments

Comments
 (0)