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

Generate url #504

Merged
merged 14 commits into from
Apr 6, 2015
Merged

Generate url #504

merged 14 commits into from
Apr 6, 2015

Conversation

kyleknap
Copy link
Contributor

So this pull request is to expose an interface for supporting presigned url. Note that this PR does not include support for s3's presigned POST's. With this interface, in order to generate a presigned url. All you need is a RequestSigner. With the RequestSigner you can call generate_url() method that takes in a request dictionary (from the serializer and prepare_request_dict) and will return a url that is presigned. Note that I considered calling the method presign_url(), but I decided to name it how boto does. You also have the option to specify how many seconds till the url expires and if you want to sign with a different region name.

Also, this PR removes the last of the skips in the integration test for the clients-only branch.

cc @jamesls @danielgtaylor

# We only want to include relevant headers in the query string.
# These can be anything that starts with x-amz, is Content-MD5,
# or is Content-Type.
elif lk.startswith('x-amz-') or lk in ['content-md5',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered abstracting a public method out in HmacV1Auth, but I was trying to avoid touching existing code. The bottom line is that the auth class picks what headers it will sign for (much like the sigv4 signer), but it just does not have a public headers_to_sign method. I can add one though.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.1%) to 95.73% when pulling 770f802 on kyleknap:generate-url into e318dd6 on boto:clients-only.

@kyleknap
Copy link
Contributor Author

kyleknap commented Apr 1, 2015

@jamesls @danielgtaylor

So the commits I recently pushed are for the ability to presign s3 post (both sigv2 and sigv4). The public method is build_post_form_args. To get a sense on how the CLI and boto3 will use it, check out the integration tests in test_s3.py. That has some examples of how you can leverage the presigner and requests library to make the post request.

One thing that would be great to get your opinions on is the interface in general. I feel that it is a little weird that the method is attached to the general signing class, but only applies to s3. Ideally for me, there would be some isolation between this method and the general signer class (because it only applies to s3 posts) but I have not been able to think of a better way. Maybe I can check that the service of the signer is s3, and if not throw an error? Or rename it to have s3 in the name of the method? Also let me know what you think of the name build_post_form_args. I picked it because that is the name boto uses, but the inputs and return values are a little different.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.49%) to 96.12% when pulling 77cc71d on kyleknap:generate-url into e318dd6 on boto:clients-only.

@kyleknap
Copy link
Contributor Author

kyleknap commented Apr 2, 2015

In the latest commit, I exposed some client level methods, generate_url and build_s3_post_form_args, in signers.py that will eventually be translated to actual methods of the client for the presigning methods. So I would pay more attention to the internals of the methods. When I do dangle them off the client, they will pretty much take the same arguments as they are currently minus the client argument.

if conditions is None:
conditions = []

request_signer = client._request_signer
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I realize that these are internal. However, we will get access to them when we add the appropriate event for adding methods to classes.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.49%) to 96.12% when pulling 77cc71d on kyleknap:generate-url into e318dd6 on boto:clients-only.


# We choose the CreateBucket operation model because its url gets
# serialized to what a presign post requires.
operation_model = client.meta.service_model.operation_model('CreateBucket')
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I considered creating my own model for this. However, I felt CreateBucket is suitable. All I really need is the url when I serialize the request and the CreateBucket serialization is exactly how I need the url serialized. Let me know if you want me to change this.

[{"acl": "public-read"},
{"bucket": "mybucket"},
["starts-with", "$key", "mykey"]])
self.assertIn('signature', result_fields)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not assert do assert equal because we dump a dictionary to json and there is no guarantee that gets dumped the same each time unless you order the json elements, which is not needed functionality-wise.

@coveralls
Copy link

Coverage Status

Coverage increased (+0.49%) to 96.12% when pulling f293551 on kyleknap:generate-url into e318dd6 on boto:clients-only.

@@ -23,6 +23,7 @@
import functools
import time
import calendar
import json
Copy link
Member

Choose a reason for hiding this comment

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

from botocore.compat import json

@jamesls
Copy link
Member

jamesls commented Apr 3, 2015

Overall looks good, just some minor review feedback. As far as the interface goes, I'd prefer to just have a separate class that does the s3 post forms stuff. I agree that it doesn't feel right having it in the general signer class.

Also updated code based on feedback.
@coveralls
Copy link

Coverage Status

Coverage increased (+0.53%) to 96.17% when pulling 3bcfda5 on kyleknap:generate-url into edfc623 on boto:clients-only.

@kyleknap
Copy link
Contributor Author

kyleknap commented Apr 3, 2015

Alright in the very latest commit I did two things:

  1. Update the code based on feedback
  2. Dangle the presigning methods on clients as: client.generate_presigned_url, client.generate_presigned_post

With these additions presigning is officially available in boto3 as well.

Let me know what you think. @jamesls @danielgtaylor

@@ -1,6 +1,7 @@
"""Abstractions to interact with service models."""
from collections import defaultdict

from botocore import xform_name
Copy link
Member

Choose a reason for hiding this comment

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

Is this used anywhere?

Copy link
Member

Choose a reason for hiding this comment

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

Is this still needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nah. It did not get cleaned up after I removed the client_name_to_operation_name function I added to the model. Will remove

@danielgtaylor
Copy link
Member

Just a few minor comments. LGTM 👍

@jamesls
Copy link
Member

jamesls commented Apr 3, 2015

Just some small stuff, otherwise looks good. :shipit:

@coveralls
Copy link

Coverage Status

Coverage increased (+0.53%) to 96.17% when pulling 95c4913 on kyleknap:generate-url into edfc623 on boto:clients-only.

@kyleknap
Copy link
Contributor Author

kyleknap commented Apr 6, 2015

@danielgtaylor
I updated the code based on your feedback. The only thing that I did not change was the regular expression as I was hesitant to change that as it was just a copy and paste from a different file. Let me know what you think.

@danielgtaylor
Copy link
Member

Thanks, LGTM 🚢-it!

kyleknap added a commit that referenced this pull request Apr 6, 2015
@kyleknap kyleknap merged commit bc2c982 into boto:clients-only Apr 6, 2015
@kyleknap kyleknap deleted the generate-url branch April 6, 2015 17:42
@vlcinsky
Copy link

vlcinsky commented May 7, 2015

It would be great, if the method would allow also absolute expiration time.

Adding additional parameter expires_in_absolute with True of False would work (copying the way, boto does that).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants