-
Notifications
You must be signed in to change notification settings - Fork 315
feat: add support for getting and setting table IAM policy #144
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
b694963
feat(iam): adds get_iam_policy
steffnay f5abb6a
Merge branch 'master' of github.com:googleapis/python-bigquery into t…
steffnay 5f9eead
feat: add ability to get and set table/view IAM policy
steffnay 8fe4681
Merge branch 'master' of github.com:googleapis/python-bigquery into t…
steffnay 13e1be1
test: add updateMask to test_set_iam_policy
steffnay a769276
test: add test for invalid table
steffnay ff65fec
feat: adds test_iam_permissions
steffnay 2539c13
Merge branch 'master' of github.com:googleapis/python-bigquery into t…
steffnay 2d1a5ef
test: adds test_iam_permissions test
steffnay 22c696c
Merge branch 'master' into table-acl
steffnay 8dd8763
test: updates assertion
steffnay 466c622
Merge branch 'master' of github.com:googleapis/python-bigquery into t…
steffnay 4ddee12
Merge branch 'master' into table-acl
steffnay 9ec8d7b
Merge branch 'master' of github.com:steffnay/python-bigquery into tab…
steffnay bf6964e
refactor
steffnay 9a310e6
Merge branch 'table-acl' of github.com:steffnay/python-bigquery into …
steffnay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
"""BigQuery API IAM policy definitions | ||
|
||
For all allowed roles and permissions, see: | ||
|
||
https://cloud.google.com/bigquery/docs/access-control | ||
""" | ||
|
||
# BigQuery-specific IAM roles available for tables and views | ||
|
||
BIGQUERY_DATA_EDITOR_ROLE = "roles/bigquery.dataEditor" | ||
"""When applied to a table or view, this role provides permissions to | ||
read and update data and metadata for the table or view.""" | ||
|
||
BIGQUERY_DATA_OWNER_ROLE = "roles/bigquery.dataOwner" | ||
"""When applied to a table or view, this role provides permissions to | ||
read and update data and metadata for the table or view, share the | ||
table/view, and delete the table/view.""" | ||
|
||
BIGQUERY_DATA_VIEWER_ROLE = "roles/bigquery.dataViewer" | ||
"""When applied to a table or view, this role provides permissions to | ||
read data and metadata from the table or view.""" | ||
|
||
BIGQUERY_METADATA_VIEWER_ROLE = "roles/bigquery.metadataViewer" | ||
"""When applied to a table or view, this role provides persmissions to | ||
read metadata from the table or view.""" |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -1748,6 +1748,216 @@ def test_get_table_sets_user_agent(self): | |
) | ||
self.assertIn("my-application/1.2.3", expected_user_agent) | ||
|
||
def test_get_iam_policy(self): | ||
from google.cloud.bigquery.iam import BIGQUERY_DATA_OWNER_ROLE | ||
from google.cloud.bigquery.iam import BIGQUERY_DATA_EDITOR_ROLE | ||
from google.cloud.bigquery.iam import BIGQUERY_DATA_VIEWER_ROLE | ||
from google.api_core.iam import Policy | ||
|
||
PATH = "/projects/{}/datasets/{}/tables/{}:getIamPolicy".format( | ||
self.PROJECT, self.DS_ID, self.TABLE_ID, | ||
) | ||
BODY = {"options": {"requestedPolicyVersion": 1}} | ||
ETAG = "CARDI" | ||
VERSION = 1 | ||
OWNER1 = "user:[email protected]" | ||
OWNER2 = "group:[email protected]" | ||
EDITOR1 = "domain:google.com" | ||
EDITOR2 = "user:[email protected]" | ||
VIEWER1 = "serviceAccount:[email protected]" | ||
VIEWER2 = "user:[email protected]" | ||
RETURNED = { | ||
"resourceId": PATH, | ||
"etag": ETAG, | ||
"version": VERSION, | ||
"bindings": [ | ||
{"role": BIGQUERY_DATA_OWNER_ROLE, "members": [OWNER1, OWNER2]}, | ||
{"role": BIGQUERY_DATA_EDITOR_ROLE, "members": [EDITOR1, EDITOR2]}, | ||
{"role": BIGQUERY_DATA_VIEWER_ROLE, "members": [VIEWER1, VIEWER2]}, | ||
], | ||
} | ||
EXPECTED = { | ||
binding["role"]: set(binding["members"]) for binding in RETURNED["bindings"] | ||
} | ||
|
||
creds = _make_credentials() | ||
http = object() | ||
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) | ||
conn = client._connection = make_connection(RETURNED) | ||
|
||
policy = client.get_iam_policy(self.TABLE_REF, timeout=7.5) | ||
|
||
conn.api_request.assert_called_once_with( | ||
method="POST", path=PATH, data=BODY, timeout=7.5 | ||
) | ||
|
||
self.assertIsInstance(policy, Policy) | ||
self.assertEqual(policy.etag, RETURNED["etag"]) | ||
self.assertEqual(policy.version, RETURNED["version"]) | ||
self.assertEqual(dict(policy), EXPECTED) | ||
|
||
def test_get_iam_policy_w_invalid_table(self): | ||
creds = _make_credentials() | ||
http = object() | ||
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) | ||
|
||
table_resource_string = "projects/{}/datasets/{}/tables/{}".format( | ||
self.PROJECT, self.DS_ID, self.TABLE_ID, | ||
) | ||
|
||
with self.assertRaises(TypeError): | ||
client.get_iam_policy(table_resource_string) | ||
|
||
def test_get_iam_policy_w_invalid_version(self): | ||
creds = _make_credentials() | ||
http = object() | ||
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) | ||
|
||
with self.assertRaises(ValueError): | ||
client.get_iam_policy(self.TABLE_REF, requested_policy_version=2) | ||
|
||
def test_set_iam_policy(self): | ||
from google.cloud.bigquery.iam import BIGQUERY_DATA_OWNER_ROLE | ||
from google.cloud.bigquery.iam import BIGQUERY_DATA_EDITOR_ROLE | ||
from google.cloud.bigquery.iam import BIGQUERY_DATA_VIEWER_ROLE | ||
from google.api_core.iam import Policy | ||
|
||
PATH = "/projects/%s/datasets/%s/tables/%s:setIamPolicy" % ( | ||
self.PROJECT, | ||
self.DS_ID, | ||
self.TABLE_ID, | ||
) | ||
ETAG = "foo" | ||
VERSION = 1 | ||
OWNER1 = "user:[email protected]" | ||
OWNER2 = "group:[email protected]" | ||
EDITOR1 = "domain:google.com" | ||
EDITOR2 = "user:[email protected]" | ||
VIEWER1 = "serviceAccount:[email protected]" | ||
VIEWER2 = "user:[email protected]" | ||
BINDINGS = [ | ||
{"role": BIGQUERY_DATA_OWNER_ROLE, "members": [OWNER1, OWNER2]}, | ||
{"role": BIGQUERY_DATA_EDITOR_ROLE, "members": [EDITOR1, EDITOR2]}, | ||
{"role": BIGQUERY_DATA_VIEWER_ROLE, "members": [VIEWER1, VIEWER2]}, | ||
] | ||
MASK = "bindings,etag" | ||
RETURNED = {"etag": ETAG, "version": VERSION, "bindings": BINDINGS} | ||
|
||
policy = Policy() | ||
for binding in BINDINGS: | ||
policy[binding["role"]] = binding["members"] | ||
|
||
BODY = {"policy": policy.to_api_repr(), "updateMask": MASK} | ||
|
||
creds = _make_credentials() | ||
http = object() | ||
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) | ||
conn = client._connection = make_connection(RETURNED) | ||
|
||
returned_policy = client.set_iam_policy( | ||
self.TABLE_REF, policy, updateMask=MASK, timeout=7.5 | ||
) | ||
|
||
conn.api_request.assert_called_once_with( | ||
method="POST", path=PATH, data=BODY, timeout=7.5 | ||
) | ||
self.assertEqual(returned_policy.etag, ETAG) | ||
self.assertEqual(returned_policy.version, VERSION) | ||
self.assertEqual(dict(returned_policy), dict(policy)) | ||
|
||
def test_set_iam_policy_no_mask(self): | ||
from google.api_core.iam import Policy | ||
|
||
PATH = "/projects/%s/datasets/%s/tables/%s:setIamPolicy" % ( | ||
self.PROJECT, | ||
self.DS_ID, | ||
self.TABLE_ID, | ||
) | ||
RETURNED = {"etag": "foo", "version": 1, "bindings": []} | ||
|
||
policy = Policy() | ||
BODY = {"policy": policy.to_api_repr()} | ||
|
||
creds = _make_credentials() | ||
http = object() | ||
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) | ||
conn = client._connection = make_connection(RETURNED) | ||
|
||
client.set_iam_policy(self.TABLE_REF, policy, timeout=7.5) | ||
|
||
conn.api_request.assert_called_once_with( | ||
method="POST", path=PATH, data=BODY, timeout=7.5 | ||
) | ||
|
||
def test_set_iam_policy_invalid_policy(self): | ||
from google.api_core.iam import Policy | ||
|
||
policy = Policy() | ||
invalid_policy_repr = policy.to_api_repr() | ||
|
||
creds = _make_credentials() | ||
http = object() | ||
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) | ||
|
||
with self.assertRaises(TypeError): | ||
client.set_iam_policy(self.TABLE_REF, invalid_policy_repr) | ||
|
||
def test_set_iam_policy_w_invalid_table(self): | ||
from google.api_core.iam import Policy | ||
|
||
policy = Policy() | ||
|
||
creds = _make_credentials() | ||
http = object() | ||
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) | ||
|
||
table_resource_string = "projects/%s/datasets/%s/tables/%s" % ( | ||
self.PROJECT, | ||
self.DS_ID, | ||
self.TABLE_ID, | ||
) | ||
|
||
with self.assertRaises(TypeError): | ||
client.set_iam_policy(table_resource_string, policy) | ||
|
||
def test_test_iam_permissions(self): | ||
PATH = "/projects/%s/datasets/%s/tables/%s:testIamPermissions" % ( | ||
self.PROJECT, | ||
self.DS_ID, | ||
self.TABLE_ID, | ||
) | ||
|
||
PERMISSIONS = ["bigquery.tables.get", "bigquery.tables.update"] | ||
BODY = {"permissions": PERMISSIONS} | ||
RETURNED = {"permissions": PERMISSIONS} | ||
|
||
creds = _make_credentials() | ||
http = object() | ||
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) | ||
conn = client._connection = make_connection(RETURNED) | ||
|
||
client.test_iam_permissions(self.TABLE_REF, PERMISSIONS, timeout=7.5) | ||
|
||
conn.api_request.assert_called_once_with( | ||
method="POST", path=PATH, data=BODY, timeout=7.5 | ||
) | ||
|
||
def test_test_iam_permissions_w_invalid_table(self): | ||
creds = _make_credentials() | ||
http = object() | ||
client = self._make_one(project=self.PROJECT, credentials=creds, _http=http) | ||
|
||
table_resource_string = "projects/%s/datasets/%s/tables/%s" % ( | ||
self.PROJECT, | ||
self.DS_ID, | ||
self.TABLE_ID, | ||
) | ||
|
||
PERMISSIONS = ["bigquery.tables.get", "bigquery.tables.update"] | ||
|
||
with self.assertRaises(TypeError): | ||
client.test_iam_permissions(table_resource_string, PERMISSIONS) | ||
|
||
def test_update_dataset_w_invalid_field(self): | ||
from google.cloud.bigquery.dataset import Dataset | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.