Skip to content
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

fix(storage): use OrderedDict while encoding POST policy #95

Merged
merged 3 commits into from Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion google/cloud/storage/client.py
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
Member

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
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