1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
14- """PubSub API IAM policy definitions"""
14+ """PubSub API IAM policy definitions
15+
16+ For allowed roles / permissions, see:
17+ https://cloud.google.com/pubsub/access_control#permissions
18+ """
19+
20+ # Generic IAM roles
1521
1622OWNER_ROLE = 'roles/owner'
17- """IAM permission implying all rights to an object."""
23+ """Generic role implying all rights to an object."""
1824
1925EDITOR_ROLE = 'roles/editor'
20- """IAM permission implying rights to modify an object."""
26+ """Generic role implying rights to modify an object."""
2127
2228VIEWER_ROLE = 'roles/viewer'
23- """IAM permission implying rights to access an object without modifying it."""
29+ """Generic role implying rights to access an object."""
30+
31+ # Pubsub-specific IAM roles
32+
33+ PUBSUB_ADMIN_ROLE = 'roles/pubsub.admin'
34+ """Role implying all rights to an object."""
35+
36+ PUBSUB_EDITOR_ROLE = 'roles/pubsub.editor'
37+ """Role implying rights to modify an object."""
38+
39+ PUBSUB_VIEWER_ROLE = 'roles/pubsub.viewer'
40+ """Role implying rights to access an object."""
41+
42+ PUBSUB_PUBLISHER_ROLE = 'roles/pubsub.publisher'
43+ """Role implying rights to publish to a topic."""
44+
45+ PUBSUB_SUBSCRIBER_ROLE = 'roles/pubsub.subscriber'
46+ """Role implying rights to subscribe to a topic."""
47+
48+
49+ # Pubsub-specific permissions
50+
51+ PUBSUB_TOPICS_CONSUME = 'pubsub.topics.consume'
52+ """Permission: consume events from a subscription."""
53+
54+ PUBSUB_TOPICS_CREATE = 'pubsub.topics.create'
55+ """Permission: create topics."""
56+
57+ PUBSUB_TOPICS_DELETE = 'pubsub.topics.delete'
58+ """Permission: delete topics."""
59+
60+ PUBSUB_TOPICS_GET = 'pubsub.topics.get'
61+ """Permission: retrieve topics."""
62+
63+ PUBSUB_TOPICS_GET_IAM_POLICY = 'pubsub.topics.getIamPolicy'
64+ """Permission: retrieve subscription IAM policies."""
65+
66+ PUBSUB_TOPICS_LIST = 'pubsub.topics.list'
67+ """Permission: list topics."""
68+
69+ PUBSUB_TOPICS_SET_IAM_POLICY = 'pubsub.topics.setIamPolicy'
70+ """Permission: update subscription IAM policies."""
71+
72+ PUBSUB_SUBSCRIPTIONS_CONSUME = 'pubsub.subscriptions.consume'
73+ """Permission: consume events from a subscription."""
74+
75+ PUBSUB_SUBSCRIPTIONS_CREATE = 'pubsub.subscriptions.create'
76+ """Permission: create subscriptions."""
77+
78+ PUBSUB_SUBSCRIPTIONS_DELETE = 'pubsub.subscriptions.delete'
79+ """Permission: delete subscriptions."""
80+
81+ PUBSUB_SUBSCRIPTIONS_GET = 'pubsub.subscriptions.get'
82+ """Permission: retrieve subscriptions."""
83+
84+ PUBSUB_SUBSCRIPTIONS_GET_IAM_POLICY = 'pubsub.subscriptions.getIamPolicy'
85+ """Permission: retrieve subscription IAM policies."""
86+
87+ PUBSUB_SUBSCRIPTIONS_LIST = 'pubsub.subscriptions.list'
88+ """Permission: list subscriptions."""
89+
90+ PUBSUB_SUBSCRIPTIONS_SET_IAM_POLICY = 'pubsub.subscriptions.setIamPolicy'
91+ """Permission: update subscription IAM policies."""
92+
93+ PUBSUB_SUBSCRIPTIONS_UPDATE = 'pubsub.subscriptions.update'
94+ """Permission: update subscriptions."""
2495
2596
2697class Policy (object ):
@@ -42,6 +113,8 @@ def __init__(self, etag=None, version=None):
42113 self .owners = set ()
43114 self .editors = set ()
44115 self .viewers = set ()
116+ self .publishers = set ()
117+ self .subscribers = set ()
45118
46119 @staticmethod
47120 def user (email ):
@@ -125,12 +198,16 @@ def from_api_repr(cls, resource):
125198 for binding in resource .get ('bindings' , ()):
126199 role = binding ['role' ]
127200 members = set (binding ['members' ])
128- if role == OWNER_ROLE :
129- policy .owners = members
130- elif role == EDITOR_ROLE :
131- policy .editors = members
132- elif role == VIEWER_ROLE :
133- policy .viewers = members
201+ if role in (OWNER_ROLE , PUBSUB_ADMIN_ROLE ):
202+ policy .owners |= members
203+ elif role in (EDITOR_ROLE , PUBSUB_EDITOR_ROLE ):
204+ policy .editors |= members
205+ elif role in (VIEWER_ROLE , PUBSUB_VIEWER_ROLE ):
206+ policy .viewers |= members
207+ elif role == PUBSUB_PUBLISHER_ROLE :
208+ policy .publishers |= members
209+ elif role == PUBSUB_SUBSCRIBER_ROLE :
210+ policy .subscribers |= members
134211 else :
135212 raise ValueError ('Unknown role: %s' % (role ,))
136213 return policy
@@ -153,15 +230,28 @@ def to_api_repr(self):
153230
154231 if self .owners :
155232 bindings .append (
156- {'role' : OWNER_ROLE , 'members' : sorted (self .owners )})
233+ {'role' : PUBSUB_ADMIN_ROLE ,
234+ 'members' : sorted (self .owners )})
157235
158236 if self .editors :
159237 bindings .append (
160- {'role' : EDITOR_ROLE , 'members' : sorted (self .editors )})
238+ {'role' : PUBSUB_EDITOR_ROLE ,
239+ 'members' : sorted (self .editors )})
161240
162241 if self .viewers :
163242 bindings .append (
164- {'role' : VIEWER_ROLE , 'members' : sorted (self .viewers )})
243+ {'role' : PUBSUB_VIEWER_ROLE ,
244+ 'members' : sorted (self .viewers )})
245+
246+ if self .publishers :
247+ bindings .append (
248+ {'role' : PUBSUB_PUBLISHER_ROLE ,
249+ 'members' : sorted (self .publishers )})
250+
251+ if self .subscribers :
252+ bindings .append (
253+ {'role' : PUBSUB_SUBSCRIBER_ROLE ,
254+ 'members' : sorted (self .subscribers )})
165255
166256 if bindings :
167257 resource ['bindings' ] = bindings
0 commit comments