Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion google/cloud/storage/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import base64
import binascii
import collections
import datetime
import functools
import json
Expand Down Expand Up @@ -972,7 +973,14 @@ def generate_signed_post_policy_v4(

# encode policy for signing
policy = json.dumps(
{"conditions": conditions, "expiration": policy_expires.isoformat() + "Z"},
collections.OrderedDict(
sorted(
{
"conditions": conditions,
"expiration": policy_expires.isoformat() + "Z",
}.items()
)
),
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expiration sometimes encoded before conditions - in such a cases tests are failing

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little concerned about imposing order in signing code given it's only used for conformance tests.

However, it doesn't change the outcome of expectation and will work as expected as a user.

separators=(",", ":"),
)
str_to_sign = base64.b64encode(policy.encode("utf-8"))
Expand Down
7 changes: 4 additions & 3 deletions tests/unit/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,10 @@ def test_conformance_post_policy(test_data):
scheme=in_data.get("scheme"),
)
fields = policy["fields"]
out_data = test_data["policyOutput"]

decoded_policy = base64.b64decode(fields["policy"]).decode("unicode_escape")
assert decoded_policy == out_data["expectedDecodedPolicy"]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved decoded_policy assert to the top: if something gone wrong in policy, it'll be easier to see it in decoded state - probably will save some time on debugging


for field in (
"x-goog-algorithm",
Expand All @@ -1771,9 +1775,6 @@ def test_conformance_post_policy(test_data):
):
assert fields[field] == test_data["policyOutput"]["fields"][field]

out_data = test_data["policyOutput"]
decoded_policy = base64.b64decode(fields["policy"]).decode("unicode_escape")
assert decoded_policy == out_data["expectedDecodedPolicy"]
assert policy["url"] == out_data["url"]


Expand Down