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

Add support for temp. IAM credentials for S3 access #69

Merged
merged 1 commit into from
Mar 25, 2014

Conversation

ckaenzig
Copy link
Member

These credentials are generated and rotated by Amazon for EC2
instances that have an instance role attributed. They are fetched
through the EC2 metadata API.

Note: this code is not fully tested yet and feedback about coding style is also welcome!

if self.iam_token is None \
or self.iam_token_expiration > datetime.utcnow() + timedelta(0, 300):
# Step 1: fetch the instance role name
api_connection = httplib.HTTPConnection('169.254.169.254')
Copy link
Member

Choose a reason for hiding this comment

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

From where does this IP com from ?

@ckaenzig
Copy link
Member Author

Damn you tests! I will push a single commit once all the tests pass (and I manage to stop adding errors)

@asaunier
Copy link
Member

asaunier commented Mar 5, 2014

Hi! I have tried to run this file in a project that uses S3 as tiles storage.
When running tilecloud-chain to generate a WMTS capabilities file

sudo -u www-data ./buildout/bin/generate_controller --capabilities

I get the following error stack:

Traceback (most recent call last):
  File "./buildout/bin/generate_controller", line 20, in <module>
    tilecloud_chain.controller.main()
  File "/home/alex/geoportal/buildout/eggs/tilecloud_chain-0.7.1-py2.7.egg/tilecloud_chain/controller.py", line 85, in main
    _generate_wmts_capabilities(gene)
  File "/home/alex/geoportal/buildout/eggs/tilecloud_chain-0.7.1-py2.7.egg/tilecloud_chain/controller.py", line 209, in _generate_wmts_capabilities
    _send(capabilities, cache['wmtscapabilities_file'], 'application/xml', cache)
  File "/home/alex/geoportal/buildout/eggs/tilecloud_chain-0.7.1-py2.7.egg/tilecloud_chain/controller.py", line 104, in _send
    s3key.put()
  File "/home/alex/geoportal/buildout/eggs/tilecloud-0.2dev_20131025-py2.7.egg/tilecloud/lib/s3.py", line 128, in put
    return self.bucket.put(self.name, self.headers, self.body)
  File "/home/alex/geoportal/buildout/eggs/tilecloud-0.2dev_20131025-py2.7.egg/tilecloud/lib/s3.py", line 188, in put
    return self.connection.put(self.name, '/' + key_name, headers, body)
  File "/home/alex/geoportal/buildout/eggs/tilecloud-0.2dev_20131025-py2.7.egg/tilecloud/lib/s3.py", line 280, in put
    return self.request('PUT', bucket_name, url, body=body, headers=headers)
  File "/home/alex/geoportal/buildout/eggs/tilecloud-0.2dev_20131025-py2.7.egg/tilecloud/lib/s3.py", line 292, in request
    sub_resources)
  File "/home/alex/geoportal/buildout/eggs/tilecloud-0.2dev_20131025-py2.7.egg/tilecloud/lib/s3.py", line 347, in sign
    signature = hmac.new(self.secret_access_key, ''.join(string_to_sign), hashlib.sha1)
  File "/usr/lib/python2.7/hmac.py", line 133, in new
    return HMAC(key, msg, digestmod)
  File "/usr/lib/python2.7/hmac.py", line 72, in __init__
    self.outer.update(key.translate(trans_5C))
TypeError: character mapping must return integer, None or unicode

@sbrunner
Copy link
Member

sbrunner commented Mar 5, 2014

What's the relation with this pull request ?
you seem using an older egg 0.2dev_20131025 !

@sbrunner
Copy link
Member

sbrunner commented Mar 5, 2014

Or you copy the code in the existing egg ?

@asaunier
Copy link
Member

asaunier commented Mar 5, 2014

Or you copy the code in the existing egg ?

Right!

% (response.status, response.reason))
credentials = json.loads(response.read())
self.access_key = credentials['AccessKeyId']
self.secret_access_key = credentials['SecretAccessKey']
Copy link
Member

Choose a reason for hiding this comment

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

@asaunier can you try to replace the upper line with:

print credentials['AccessKeyId']
print credentials['SecretAccessKey']

print type(credentials['AccessKeyId'])
print type(credentials['SecretAccessKey'])

self.secret_access_key = str(credentials['SecretAccessKey'])

Copy link
Member

Choose a reason for hiding this comment

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

I don't know if it is safe to paste here the access keys :P so I obfuscate them a bit:

A******************Q
G********************************2
<type 'unicode'>
<type 'unicode'>

Copy link
Member

Choose a reason for hiding this comment

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

and then same error ?

Copy link
Member

Choose a reason for hiding this comment

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

We should have a look together tomorrow it will be easier !

Copy link
Member

Choose a reason for hiding this comment

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

Oops, I forgot to add the str().
The error mentioned above no longer occurs but I get the original problem back:

Traceback (most recent call last):
  File "./buildout/bin/generate_controller", line 20, in <module>
    tilecloud_chain.controller.main()
  File "/home/alex/geoportal/buildout/eggs/tilecloud_chain-0.7.1-py2.7.egg/tilecloud_chain/controller.py", line 85, in main
    _generate_wmts_capabilities(gene)
  File "/home/alex/geoportal/buildout/eggs/tilecloud_chain-0.7.1-py2.7.egg/tilecloud_chain/controller.py", line 209, in _generate_wmts_capabilities
    _send(capabilities, cache['wmtscapabilities_file'], 'application/xml', cache)
  File "/home/alex/geoportal/buildout/eggs/tilecloud_chain-0.7.1-py2.7.egg/tilecloud_chain/controller.py", line 104, in _send
    s3key.put()
  File "/home/alex/geoportal/buildout/eggs/tilecloud-0.2dev_20131025-py2.7.egg/tilecloud/lib/s3.py", line 128, in put
    return self.bucket.put(self.name, self.headers, self.body)
  File "/home/alex/geoportal/buildout/eggs/tilecloud-0.2dev_20131025-py2.7.egg/tilecloud/lib/s3.py", line 188, in put
    return self.connection.put(self.name, '/' + key_name, headers, body)
  File "/home/alex/geoportal/buildout/eggs/tilecloud-0.2dev_20131025-py2.7.egg/tilecloud/lib/s3.py", line 285, in put
    return self.request('PUT', bucket_name, url, body=body, headers=headers)
  File "/home/alex/geoportal/buildout/eggs/tilecloud-0.2dev_20131025-py2.7.egg/tilecloud/lib/s3.py", line 305, in request
    raise S3Error(method, url, body, headers, response)
tilecloud.lib.s3.S3Error: InvalidAccessKeyId: The AWS Access Key Id you provided does not exist in our records.

@ckaenzig
Copy link
Member Author

ckaenzig commented Mar 5, 2014

According to this http://bugs.python.org/issue5285 (assuming it also applies to python 2.7), we should probably convert to the strings passed to hmac.new to String objects, one or both of them are probably Unicode now.

I'd happily take some time to check that out with you guys.

@twpayne
Copy link
Contributor

twpayne commented Mar 24, 2014

@ckaenzig thanks for updating the pull request!

For info, you can run the tests locally with the command:

make pep8 pyflakes test

This is much quicker than waiting for Travis to run...

These credentials are generated and rotated by Amazon for EC2
instances that have an instance role attributed. They are fetched
through the EC2 metadata API.
@twpayne
Copy link
Contributor

twpayne commented Mar 25, 2014

Many thanks for this nice functionality!

twpayne added a commit that referenced this pull request Mar 25, 2014
Add support for temp. IAM credentials for S3 access
@twpayne twpayne merged commit 7d69504 into camptocamp:master Mar 25, 2014
@ckaenzig ckaenzig deleted the s3-temp-iam-credentials branch March 25, 2014 14:06
@sbrunner sbrunner added this to the 0.3 milestone Apr 13, 2018
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.

4 participants