Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit dac6c9f

Browse filesBrowse files
melaniedejongengelke
authored andcommitted
IAM: re-add testing permissions (GoogleCloudPlatform#2494)
* Added test_permissions function and tests for this doc: https://cloud.google.com/iam/docs/testing-permissions * Adding access tests Adding back tests that were accidentally removed in a previous commit * Lint * Lint Adding newlines at end of files * Lint * Lint * Fix spacing
1 parent d1813bf commit dac6c9f
Copy full SHA for dac6c9f

File tree

Expand file treeCollapse file tree

2 files changed

+36
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+36
-4
lines changed

‎iam/api-client/access.py

Copy file name to clipboardExpand all lines: iam/api-client/access.py
+32-4Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
# !/usr/bin/env python
2-
#
31
# Copyright 2018 Google LLC
42
#
53
# Licensed under the Apache License, Version 2.0 (the "License");
@@ -31,7 +29,6 @@
3129
def get_policy(project_id):
3230
"""Gets IAM policy for a project."""
3331

34-
# pylint: disable=no-member
3532
credentials = service_account.Credentials.from_service_account_file(
3633
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
3734
scopes=['https://www.googleapis.com/auth/cloud-platform'])
@@ -84,7 +81,6 @@ def modify_policy_remove_member(policy, role, member):
8481
def set_policy(project_id, policy):
8582
"""Sets IAM policy for a project."""
8683

87-
# pylint: disable=no-member
8884
credentials = service_account.Credentials.from_service_account_file(
8985
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
9086
scopes=['https://www.googleapis.com/auth/cloud-platform'])
@@ -100,6 +96,31 @@ def set_policy(project_id, policy):
10096
# [END iam_set_policy]
10197

10298

99+
# [START iam_test_permissions]
100+
def test_permissions(project_id):
101+
"""Tests IAM permissions of the caller"""
102+
103+
credentials = service_account.Credentials.from_service_account_file(
104+
filename=os.environ['GOOGLE_APPLICATION_CREDENTIALS'],
105+
scopes=['https://www.googleapis.com/auth/cloud-platform'])
106+
service = googleapiclient.discovery.build(
107+
'cloudresourcemanager', 'v1', credentials=credentials)
108+
109+
permissions = {
110+
"permissions": [
111+
"resourcemanager.projects.get",
112+
"resourcemanager.projects.delete"
113+
]
114+
}
115+
116+
request = service.projects().testIamPermissions(
117+
resource=project_id, body=permissions)
118+
returnedPermissions = request.execute()
119+
print(returnedPermissions)
120+
return returnedPermissions
121+
# [END iam_test_permissions]
122+
123+
103124
def main():
104125
parser = argparse.ArgumentParser(
105126
description=__doc__,
@@ -140,6 +161,11 @@ def main():
140161
set_parser.add_argument('project_id')
141162
set_parser.add_argument('policy')
142163

164+
# Test permissions
165+
test_permissions_parser = subparsers.add_parser(
166+
'test_permissions', help=get_policy.__doc__)
167+
test_permissions_parser.add_argument('project_id')
168+
143169
args = parser.parse_args()
144170

145171
if args.command == 'get':
@@ -152,6 +178,8 @@ def main():
152178
modify_policy_remove_member(args.policy, args.role, args.member)
153179
elif args.command == 'add_binding':
154180
modify_policy_add_role(args.policy, args.role, args.member)
181+
elif args.command == 'test_permissions':
182+
test_permissions(args.project_id)
155183

156184

157185
if __name__ == '__main__':

‎iam/api-client/access_test.py

Copy file name to clipboardExpand all lines: iam/api-client/access_test.py
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ def test_access(capsys):
5050
out, _ = capsys.readouterr()
5151
assert u'etag' in out
5252

53+
access.test_permissions(project_id)
54+
out, _ = capsys.readouterr()
55+
assert u'permissions' in out
56+
5357
# deleting the service account created above
5458
service_accounts.delete_service_account(
5559
email)

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.