Title: google.api_core.iam.Policy.__getitem__ does not correctly save empty bindings · Issue #154 · googleapis/python-api-core · GitHub
Open Graph Title: google.api_core.iam.Policy.__getitem__ does not correctly save empty bindings · Issue #154 · googleapis/python-api-core
X Title: google.api_core.iam.Policy.__getitem__ does not correctly save empty bindings · Issue #154 · googleapis/python-api-core
Description: We recently tried to upgrade a tool from 1.15.0 to 1.26.1 and found it breaks some code that handles IAM policies. The commit that introduces the bug was released in 1.16.0: fd47fda#diff-7cc73ea72342c139ff54060be9ff25b2f792f9225e0cc0f501...
Open Graph Description: We recently tried to upgrade a tool from 1.15.0 to 1.26.1 and found it breaks some code that handles IAM policies. The commit that introduces the bug was released in 1.16.0: fd47fda#diff-7cc73ea723...
X Description: We recently tried to upgrade a tool from 1.15.0 to 1.26.1 and found it breaks some code that handles IAM policies. The commit that introduces the bug was released in 1.16.0: fd47fda#diff-7cc73ea723...
Opengraph URL: https://github.com/googleapis/python-api-core/issues/154
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"google.api_core.iam.Policy.__getitem__ does not correctly save empty bindings","articleBody":"We recently tried to upgrade a tool from 1.15.0 to 1.26.1 and found it breaks some code that handles IAM policies. The commit that introduces the bug was released in 1.16.0: https://github.com/googleapis/python-api-core/commit/fd47fda5e3f5eca63522c8d81cffa22bc2a29ab6#diff-7cc73ea72342c139ff54060be9ff25b2f792f9225e0cc0f501dca9dbed9c4741 - \r\n\r\nThe new `__getitem__` implementation returns a new empty `set()` for roles not in the current policy. But it doesn't save that set in the bindings. So if the user manipulates it, the policy isn't actually updated. That breaks code written like this:\r\n```python\r\npolicy = resource.get_iam_policy()\r\npolicy['roles/storage.objectAdmin'].add(principal)\r\nbucket.set_iam_policy(policy)\r\n```\r\nThis worked fine on v1.15.0 because of the use of `defaultdict`. But now, this adds the principal to a set that's not used by the policy.\r\n\r\nSomething like the following (untested) patch should do the trick:\r\n```diff\r\ndiff --git a/google/api_core/iam.py b/google/api_core/iam.py\r\nindex f130936..d650336 100644\r\n--- a/google/api_core/iam.py\r\n+++ b/google/api_core/iam.py\r\n@@ -136,7 +136,9 @@ class Policy(collections_abc.MutableMapping):\r\n for b in self._bindings:\r\n if b[\"role\"] == key:\r\n return b[\"members\"]\r\n- return set()\r\n+\r\n+ self[key] = set()\r\n+ return self[key]\r\n\r\n def __setitem__(self, key, value):\r\n self.__check_version__()\r\n\r\n```","author":{"url":"https://github.com/rvandegrift","@type":"Person","name":"rvandegrift"},"datePublished":"2021-03-18T19:55:20.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":2},"url":"https://github.com/154/python-api-core/issues/154"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:4868b49c-cc32-e39a-0bcc-3fa5568fff9d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A704:26A272:2AC001:3D93FD:6A4E2ADE |
| html-safe-nonce | 055952c78c2440282c3228f1cf3aa65663dc959cec7b1786bd7a1a00ab7282ea |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBNzA0OjI2QTI3MjoyQUMwMDE6M0Q5M0ZEOjZBNEUyQURFIiwidmlzaXRvcl9pZCI6IjMwNjQxMzg2MzQwNzAyMDcxOTgiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 6614b738542dfe7db2c0c6faa9e52ea9d52f4264aeb0d2fe2cb1e3391594d3c1 |
| hovercard-subject-tag | issue:835197463 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/googleapis/python-api-core/154/issue_layout |
| twitter:image | https://opengraph.githubassets.com/96c80cac8580c8afdb72f6ad71eb757a1a47dc745d64a3f21bf5e9b940b450e4/googleapis/python-api-core/issues/154 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/96c80cac8580c8afdb72f6ad71eb757a1a47dc745d64a3f21bf5e9b940b450e4/googleapis/python-api-core/issues/154 |
| og:image:alt | We recently tried to upgrade a tool from 1.15.0 to 1.26.1 and found it breaks some code that handles IAM policies. The commit that introduces the bug was released in 1.16.0: fd47fda#diff-7cc73ea723... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | rvandegrift |
| hostname | github.com |
| expected-hostname | github.com |
| None | 030096ee0db095447bfe77409d33bfac127ca7128299c58deef27c52eaa1b1f0 |
| turbo-cache-control | no-preview |
| go-import | github.com/googleapis/python-api-core git https://github.com/googleapis/python-api-core.git |
| octolytics-dimension-user_id | 16785467 |
| octolytics-dimension-user_login | googleapis |
| octolytics-dimension-repository_id | 226992456 |
| octolytics-dimension-repository_nwo | googleapis/python-api-core |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 226992456 |
| octolytics-dimension-repository_network_root_nwo | googleapis/python-api-core |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | d9dd20d38f8ae3c4cb6b597807431db300d0bd2a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width