-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1646 from tseaver/pubsub-subscription-test_iam_pe…
…rmissions Add 'Subscription.test_iam_permissions' API wrapper.
- Loading branch information
Showing
7 changed files
with
142 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ IAM Policy | |
|
||
.. automodule:: gcloud.pubsub.iam | ||
:members: | ||
:member-order: bysource | ||
:undoc-members: | ||
:show-inheritance: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,11 +99,13 @@ Test permissions allowed by the current IAM policy on a topic: | |
.. doctest:: | ||
|
||
>>> from gcloud import pubsub | ||
>>> from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
>>> client = pubsub.Client() | ||
>>> topic = client.topic('topic_name') | ||
>>> topic.test_iam_permissions( | ||
... ['roles/reader', 'roles/writer', 'roles/owner']) # API request | ||
['roles/reader', 'roles/writer'] | ||
>>> allowed = topic.test_iam_permissions( | ||
... [READER_ROLE, WRITER_ROLE, OWNER_ROLE]) # API request | ||
>>> allowed == [READER_ROLE, WRITER_ROLE] | ||
True | ||
|
||
|
||
Publish messages to a topic | ||
|
@@ -341,3 +343,17 @@ Update the IAM policy for a subscription: | |
>>> policy = subscription.get_iam_policy() # API request | ||
>>> policy.writers.add(policy.group('[email protected]')) | ||
>>> subscription.set_iam_policy(policy) # API request | ||
|
||
Test permissions allowed by the current IAM policy on a subscription: | ||
|
||
.. doctest:: | ||
|
||
>>> from gcloud import pubsub | ||
>>> from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
>>> client = pubsub.Client() | ||
>>> topic = client.topic('topic_name') | ||
>>> subscription = topic.subscription('subscription_name') | ||
>>> allowed = subscription.test_iam_permissions( | ||
... [READER_ROLE, WRITER_ROLE, OWNER_ROLE]) # API request | ||
>>> allowed == [READER_ROLE, WRITER_ROLE] | ||
True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -87,7 +87,7 @@ def test_from_api_repr_only_etag(self): | |
self.assertEqual(list(policy.readers), []) | ||
|
||
def test_from_api_repr_complete(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
OWNER1 = 'user:[email protected]' | ||
OWNER2 = 'group:[email protected]' | ||
WRITER1 = 'domain:google.com' | ||
|
@@ -98,9 +98,9 @@ def test_from_api_repr_complete(self): | |
'etag': 'DEADBEEF', | ||
'version': 17, | ||
'bindings': [ | ||
{'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': _READER_ROLE, 'members': [READER1, READER2]}, | ||
{'role': OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': READER_ROLE, 'members': [READER1, READER2]}, | ||
], | ||
} | ||
klass = self._getTargetClass() | ||
|
@@ -134,7 +134,7 @@ def test_to_api_repr_only_etag(self): | |
self.assertEqual(policy.to_api_repr(), {'etag': 'DEADBEEF'}) | ||
|
||
def test_to_api_repr_full(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
OWNER1 = 'group:[email protected]' | ||
OWNER2 = 'user:[email protected]' | ||
WRITER1 = 'domain:google.com' | ||
|
@@ -145,9 +145,9 @@ def test_to_api_repr_full(self): | |
'etag': 'DEADBEEF', | ||
'version': 17, | ||
'bindings': [ | ||
{'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': _READER_ROLE, 'members': [READER1, READER2]}, | ||
{'role': OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': READER_ROLE, 'members': [READER1, READER2]}, | ||
], | ||
} | ||
policy = self._makeOne('DEADBEEF', 17) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -485,7 +485,7 @@ def test_delete_w_alternate_client(self): | |
self.assertEqual(req['path'], '/%s' % SUB_PATH) | ||
|
||
def test_get_iam_policy_w_bound_client(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
OWNER1 = 'user:[email protected]' | ||
OWNER2 = 'group:[email protected]' | ||
WRITER1 = 'domain:google.com' | ||
|
@@ -496,9 +496,9 @@ def test_get_iam_policy_w_bound_client(self): | |
'etag': 'DEADBEEF', | ||
'version': 17, | ||
'bindings': [ | ||
{'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': _READER_ROLE, 'members': [READER1, READER2]}, | ||
{'role': OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': READER_ROLE, 'members': [READER1, READER2]}, | ||
], | ||
} | ||
PROJECT = 'PROJECT' | ||
|
@@ -557,7 +557,7 @@ def test_get_iam_policy_w_alternate_client(self): | |
self.assertEqual(req['path'], '/%s' % PATH) | ||
|
||
def test_set_iam_policy_w_bound_client(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
from gcloud.pubsub.iam import Policy | ||
OWNER1 = 'group:[email protected]' | ||
OWNER2 = 'user:[email protected]' | ||
|
@@ -569,9 +569,9 @@ def test_set_iam_policy_w_bound_client(self): | |
'etag': 'DEADBEEF', | ||
'version': 17, | ||
'bindings': [ | ||
{'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': _READER_ROLE, 'members': [READER1, READER2]}, | ||
{'role': OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': READER_ROLE, 'members': [READER1, READER2]}, | ||
], | ||
} | ||
RESPONSE = POLICY.copy() | ||
|
@@ -641,6 +641,61 @@ def test_set_iam_policy_w_alternate_client(self): | |
self.assertEqual(req['path'], '/%s' % PATH) | ||
self.assertEqual(req['data'], {}) | ||
|
||
def test_test_iam_permissions_w_bound_client(self): | ||
PROJECT = 'PROJECT' | ||
TOPIC_NAME = 'topic_name' | ||
SUB_NAME = 'sub_name' | ||
PATH = 'projects/%s/subscriptions/%s:testIamPermissions' % ( | ||
PROJECT, SUB_NAME) | ||
ROLES = ['roles/reader', 'roles/writer', 'roles/owner'] | ||
REQUESTED = { | ||
'permissions': ROLES, | ||
} | ||
RESPONSE = { | ||
'permissions': ROLES[:-1], | ||
} | ||
conn = _Connection(RESPONSE) | ||
CLIENT = _Client(project=PROJECT, connection=conn) | ||
topic = _Topic(TOPIC_NAME, client=CLIENT) | ||
subscription = self._makeOne(SUB_NAME, topic) | ||
|
||
allowed = subscription.test_iam_permissions(ROLES) | ||
|
||
self.assertEqual(allowed, ROLES[:-1]) | ||
self.assertEqual(len(conn._requested), 1) | ||
req = conn._requested[0] | ||
self.assertEqual(req['method'], 'POST') | ||
self.assertEqual(req['path'], '/%s' % PATH) | ||
self.assertEqual(req['data'], REQUESTED) | ||
|
||
def test_test_iam_permissions_w_alternate_client(self): | ||
PROJECT = 'PROJECT' | ||
TOPIC_NAME = 'topic_name' | ||
SUB_NAME = 'sub_name' | ||
PATH = 'projects/%s/subscriptions/%s:testIamPermissions' % ( | ||
PROJECT, SUB_NAME) | ||
ROLES = ['roles/reader', 'roles/writer', 'roles/owner'] | ||
REQUESTED = { | ||
'permissions': ROLES, | ||
} | ||
RESPONSE = {} | ||
conn1 = _Connection() | ||
CLIENT1 = _Client(project=PROJECT, connection=conn1) | ||
conn2 = _Connection(RESPONSE) | ||
CLIENT2 = _Client(project=PROJECT, connection=conn2) | ||
topic = _Topic(TOPIC_NAME, client=CLIENT1) | ||
subscription = self._makeOne(SUB_NAME, topic) | ||
|
||
allowed = subscription.test_iam_permissions(ROLES, client=CLIENT2) | ||
|
||
self.assertEqual(len(allowed), 0) | ||
self.assertEqual(len(conn1._requested), 0) | ||
self.assertEqual(len(conn2._requested), 1) | ||
req = conn2._requested[0] | ||
self.assertEqual(req['method'], 'POST') | ||
self.assertEqual(req['path'], '/%s' % PATH) | ||
self.assertEqual(req['data'], REQUESTED) | ||
|
||
|
||
class _Connection(object): | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -453,7 +453,7 @@ def test_list_subscriptions_missing_key(self): | |
self.assertEqual(req['query_params'], {}) | ||
|
||
def test_get_iam_policy_w_bound_client(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
OWNER1 = 'user:[email protected]' | ||
OWNER2 = 'group:[email protected]' | ||
WRITER1 = 'domain:google.com' | ||
|
@@ -464,9 +464,9 @@ def test_get_iam_policy_w_bound_client(self): | |
'etag': 'DEADBEEF', | ||
'version': 17, | ||
'bindings': [ | ||
{'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': _READER_ROLE, 'members': [READER1, READER2]}, | ||
{'role': OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': READER_ROLE, 'members': [READER1, READER2]}, | ||
], | ||
} | ||
TOPIC_NAME = 'topic_name' | ||
|
@@ -522,7 +522,7 @@ def test_get_iam_policy_w_alternate_client(self): | |
|
||
def test_set_iam_policy_w_bound_client(self): | ||
from gcloud.pubsub.iam import Policy | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
OWNER1 = 'group:[email protected]' | ||
OWNER2 = 'user:[email protected]' | ||
WRITER1 = 'domain:google.com' | ||
|
@@ -533,9 +533,9 @@ def test_set_iam_policy_w_bound_client(self): | |
'etag': 'DEADBEEF', | ||
'version': 17, | ||
'bindings': [ | ||
{'role': _OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': _WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': _READER_ROLE, 'members': [READER1, READER2]}, | ||
{'role': OWNER_ROLE, 'members': [OWNER1, OWNER2]}, | ||
{'role': WRITER_ROLE, 'members': [WRITER1, WRITER2]}, | ||
{'role': READER_ROLE, 'members': [READER1, READER2]}, | ||
], | ||
} | ||
RESPONSE = POLICY.copy() | ||
|
@@ -602,12 +602,12 @@ def test_set_iam_policy_w_alternate_client(self): | |
self.assertEqual(req['data'], {}) | ||
|
||
def test_test_iam_permissions_w_bound_client(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
TOPIC_NAME = 'topic_name' | ||
PROJECT = 'PROJECT' | ||
PATH = 'projects/%s/topics/%s:testIamPermissions' % ( | ||
PROJECT, TOPIC_NAME) | ||
ROLES = [_READER_ROLE, _WRITER_ROLE, _OWNER_ROLE] | ||
ROLES = [READER_ROLE, WRITER_ROLE, OWNER_ROLE] | ||
REQUESTED = { | ||
'permissions': ROLES, | ||
} | ||
|
@@ -628,12 +628,12 @@ def test_test_iam_permissions_w_bound_client(self): | |
self.assertEqual(req['data'], REQUESTED) | ||
|
||
def test_test_iam_permissions_w_alternate_client(self): | ||
from gcloud.pubsub.iam import _OWNER_ROLE, _WRITER_ROLE, _READER_ROLE | ||
from gcloud.pubsub.iam import OWNER_ROLE, WRITER_ROLE, READER_ROLE | ||
TOPIC_NAME = 'topic_name' | ||
PROJECT = 'PROJECT' | ||
PATH = 'projects/%s/topics/%s:testIamPermissions' % ( | ||
PROJECT, TOPIC_NAME) | ||
ROLES = [_READER_ROLE, _WRITER_ROLE, _OWNER_ROLE] | ||
ROLES = [READER_ROLE, WRITER_ROLE, OWNER_ROLE] | ||
REQUESTED = { | ||
'permissions': ROLES, | ||
} | ||
|