|
1 | 1 | #!/usr/bin/env python
|
2 |
| - |
3 |
| -# Copyright 2018 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Copyright 2020 Google Inc. All Rights Reserved. |
4 | 4 | #
|
5 | 5 | # Licensed under the Apache License, Version 2.0 (the "License");
|
6 | 6 | # you may not use this file except in compliance with the License.
|
|
14 | 14 | # See the License for the specific language governing permissions and
|
15 | 15 | # limitations under the License.
|
16 | 16 |
|
| 17 | +# [START iam_quickstart] |
| 18 | +import os |
| 19 | + |
| 20 | +from google.oauth2 import service_account |
| 21 | +import googleapiclient.discovery |
| 22 | + |
| 23 | + |
| 24 | +def quickstart(project_id, member): |
| 25 | + """Gets a policy, adds a member, prints their permissions, and removes the member.""" |
| 26 | + |
| 27 | + # Role to be granted. |
| 28 | + role = "roles/logging.logWriter" |
17 | 29 |
|
18 |
| -def quickstart(): |
19 |
| - # [START iam_quickstart] |
20 |
| - import os |
| 30 | + # Initializes service. |
| 31 | + crm_service = initialize_service() |
21 | 32 |
|
22 |
| - from google.oauth2 import service_account |
23 |
| - import googleapiclient.discovery |
| 33 | + # Grants your member the 'Log Writer' role for the project. |
| 34 | + modify_policy_add_role(crm_service, project_id, role, member) |
| 35 | + |
| 36 | + # Gets the project's policy and prints all members with the 'Log Writer' role. |
| 37 | + policy = get_policy(crm_service, project_id) |
| 38 | + binding = next(b for b in policy["bindings"] if b["role"] == role) |
| 39 | + print(f'Role: {(binding["role"])}') |
| 40 | + print("Members: ") |
| 41 | + for m in binding["members"]: |
| 42 | + print(f'[{m}]') |
| 43 | + |
| 44 | + # Removes the member from the 'Log Writer' role. |
| 45 | + modify_policy_remove_member(crm_service, project_id, role, member) |
| 46 | + |
| 47 | + |
| 48 | +def initialize_service(): |
| 49 | + """Initializes a Cloud Resource Manager service.""" |
24 | 50 |
|
25 |
| - # Get credentials |
26 | 51 | credentials = service_account.Credentials.from_service_account_file(
|
27 |
| - filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'], |
28 |
| - scopes=['https://www.googleapis.com/auth/cloud-platform']) |
| 52 | + filename=os.environ["GOOGLE_APPLICATION_CREDENTIALS"], |
| 53 | + scopes=["https://www.googleapis.com/auth/cloud-platform"], |
| 54 | + ) |
| 55 | + crm_service = googleapiclient.discovery.build( |
| 56 | + "cloudresourcemanager", "v1", credentials=credentials |
| 57 | + ) |
| 58 | + return crm_service |
| 59 | + |
| 60 | + |
| 61 | +def modify_policy_add_role(crm_service, project_id, role, member): |
| 62 | + """Adds a new role binding to a policy.""" |
29 | 63 |
|
30 |
| - # Create the Cloud IAM service object |
31 |
| - service = googleapiclient.discovery.build( |
32 |
| - 'iam', 'v1', credentials=credentials) |
| 64 | + policy = get_policy(crm_service, project_id) |
33 | 65 |
|
34 |
| - # Call the Cloud IAM Roles API |
35 |
| - # If using pylint, disable weak-typing warnings |
36 |
| - # pylint: disable=no-member |
37 |
| - response = service.roles().list().execute() |
38 |
| - roles = response['roles'] |
| 66 | + binding = None |
| 67 | + for b in policy["bindings"]: |
| 68 | + if b["role"] == role: |
| 69 | + binding = b |
| 70 | + break |
| 71 | + if binding is not None: |
| 72 | + binding["members"].append(member) |
| 73 | + else: |
| 74 | + binding = {"role": role, "members": [member]} |
| 75 | + policy["bindings"].append(binding) |
39 | 76 |
|
40 |
| - # Process the response |
41 |
| - for role in roles: |
42 |
| - print('Title: ' + role['title']) |
43 |
| - print('Name: ' + role['name']) |
44 |
| - if 'description' in role: |
45 |
| - print('Description: ' + role['description']) |
46 |
| - print('') |
47 |
| - # [END iam_quickstart] |
| 77 | + set_policy(crm_service, project_id, policy) |
| 78 | + |
| 79 | + |
| 80 | +def modify_policy_remove_member(crm_service, project_id, role, member): |
| 81 | + """Removes a member from a role binding.""" |
| 82 | + |
| 83 | + policy = get_policy(crm_service, project_id) |
| 84 | + |
| 85 | + binding = next(b for b in policy["bindings"] if b["role"] == role) |
| 86 | + if "members" in binding and member in binding["members"]: |
| 87 | + binding["members"].remove(member) |
| 88 | + |
| 89 | + set_policy(crm_service, project_id, policy) |
| 90 | + |
| 91 | + |
| 92 | +def get_policy(crm_service, project_id, version=3): |
| 93 | + """Gets IAM policy for a project.""" |
| 94 | + |
| 95 | + policy = ( |
| 96 | + crm_service.projects() |
| 97 | + .getIamPolicy( |
| 98 | + resource=project_id, |
| 99 | + body={"options": {"requestedPolicyVersion": version}}, |
| 100 | + ) |
| 101 | + .execute() |
| 102 | + ) |
| 103 | + return policy |
| 104 | + |
| 105 | + |
| 106 | +def set_policy(crm_service, project_id, policy): |
| 107 | + """Sets IAM policy for a project.""" |
| 108 | + |
| 109 | + policy = ( |
| 110 | + crm_service.projects() |
| 111 | + .setIamPolicy(resource=project_id, body={"policy": policy}) |
| 112 | + .execute() |
| 113 | + ) |
| 114 | + return policy |
48 | 115 |
|
49 | 116 |
|
50 | 117 | if __name__ == '__main__':
|
51 |
| - quickstart() |
| 118 | + |
| 119 | + # TODO: replace with your project ID |
| 120 | + project_id = "your_project_id" |
| 121 | + # TODO: Replace with the ID of your member in the form 'user:[email protected]. |
| 122 | + member = "your_member" |
| 123 | + quickstart(project_id, member) |
| 124 | +# [END iam_quickstart] |
0 commit comments