Skip to content

Commit 4077045

Browse files
committed
add requested_policy_version to blob
1 parent 2dd85e1 commit 4077045

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

storage/google/cloud/storage/blob.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1456,7 +1456,7 @@ def create_resumable_upload_session(
14561456
except resumable_media.InvalidResponse as exc:
14571457
_raise_from_invalid_response(exc)
14581458

1459-
def get_iam_policy(self, client=None):
1459+
def get_iam_policy(self, client=None, requested_policy_version=None):
14601460
"""Retrieve the IAM policy for the object.
14611461
14621462
.. note:
@@ -1475,6 +1475,18 @@ def get_iam_policy(self, client=None):
14751475
:param client: Optional. The client to use. If not passed, falls back
14761476
to the ``client`` stored on the current object's bucket.
14771477
1478+
:type requested_policy_version: int or ``NoneType``
1479+
:param requested_policy_version: Optional. The version of IAM policies to request.
1480+
If a policy with a condition is requested without
1481+
setting this, the server will return an error.
1482+
This must be set to a value of 3 to retrieve IAM
1483+
policies containing conditions. This is to prevent
1484+
client code that isn't aware of IAM conditions from
1485+
interpreting and modifying policies incorrectly.
1486+
The service might return a policy with version lower
1487+
than the one that was requested, based on the
1488+
feature syntax in the policy fetched.
1489+
14781490
:rtype: :class:`google.api_core.iam.Policy`
14791491
:returns: the policy instance, based on the resource returned from
14801492
the ``getIamPolicy`` API request.
@@ -1486,6 +1498,9 @@ def get_iam_policy(self, client=None):
14861498
if self.user_project is not None:
14871499
query_params["userProject"] = self.user_project
14881500

1501+
if requested_policy_version is not None:
1502+
query_params["optionsRequestedPolicyVersion"] = requested_policy_version
1503+
14891504
info = client._connection.api_request(
14901505
method="GET",
14911506
path="%s/iam" % (self.path,),

storage/tests/unit/test_blob.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,7 +1928,7 @@ def test_get_iam_policy(self):
19281928
BLOB_NAME = "blob-name"
19291929
PATH = "/b/name/o/%s" % (BLOB_NAME,)
19301930
ETAG = "DEADBEEF"
1931-
VERSION = 17
1931+
VERSION = 1
19321932
OWNER1 = "user:phred@example.com"
19331933
OWNER2 = "group:cloud-logs@google.com"
19341934
EDITOR1 = "domain:google.com"
@@ -1973,14 +1973,53 @@ def test_get_iam_policy(self):
19731973
},
19741974
)
19751975

1976+
def test_get_iam_policy_w_requested_policy_version(self):
1977+
from google.cloud.storage.iam import STORAGE_OWNER_ROLE
1978+
from google.api_core.iam import Policy
1979+
1980+
BLOB_NAME = "blob-name"
1981+
PATH = "/b/name/o/%s" % (BLOB_NAME,)
1982+
ETAG = "DEADBEEF"
1983+
VERSION = 3
1984+
OWNER1 = "user:phred@example.com"
1985+
OWNER2 = "group:cloud-logs@google.com"
1986+
RETURNED = {
1987+
"resourceId": PATH,
1988+
"etag": ETAG,
1989+
"version": VERSION,
1990+
"bindings": [{"role": STORAGE_OWNER_ROLE, "members": [OWNER1, OWNER2]}],
1991+
}
1992+
after = ({"status": http_client.OK}, RETURNED)
1993+
EXPECTED = {
1994+
binding["role"]: set(binding["members"]) for binding in RETURNED["bindings"]
1995+
}
1996+
connection = _Connection(after)
1997+
client = _Client(connection)
1998+
bucket = _Bucket(client=client)
1999+
blob = self._make_one(BLOB_NAME, bucket=bucket)
2000+
2001+
policy = blob.get_iam_policy()
2002+
2003+
kw = connection._requested
2004+
self.assertEqual(len(kw), 1)
2005+
self.assertEqual(
2006+
kw[0],
2007+
{
2008+
"method": "GET",
2009+
"path": "%s/iam" % (PATH,),
2010+
"query_params": {"optionsRequestedPolicyVersion": 3},
2011+
"_target_object": None,
2012+
},
2013+
)
2014+
19762015
def test_get_iam_policy_w_user_project(self):
19772016
from google.api_core.iam import Policy
19782017

19792018
BLOB_NAME = "blob-name"
19802019
USER_PROJECT = "user-project-123"
19812020
PATH = "/b/name/o/%s" % (BLOB_NAME,)
19822021
ETAG = "DEADBEEF"
1983-
VERSION = 17
2022+
VERSION = 1
19842023
RETURNED = {
19852024
"resourceId": PATH,
19862025
"etag": ETAG,
@@ -2023,7 +2062,7 @@ def test_set_iam_policy(self):
20232062
BLOB_NAME = "blob-name"
20242063
PATH = "/b/name/o/%s" % (BLOB_NAME,)
20252064
ETAG = "DEADBEEF"
2026-
VERSION = 17
2065+
VERSION = 1
20272066
OWNER1 = "user:phred@example.com"
20282067
OWNER2 = "group:cloud-logs@google.com"
20292068
EDITOR1 = "domain:google.com"
@@ -2074,7 +2113,7 @@ def test_set_iam_policy_w_user_project(self):
20742113
USER_PROJECT = "user-project-123"
20752114
PATH = "/b/name/o/%s" % (BLOB_NAME,)
20762115
ETAG = "DEADBEEF"
2077-
VERSION = 17
2116+
VERSION = 1
20782117
BINDINGS = []
20792118
RETURNED = {"etag": ETAG, "version": VERSION, "bindings": BINDINGS}
20802119
after = ({"status": http_client.OK}, RETURNED)

0 commit comments

Comments
 (0)