Created
March 13, 2012 15:15
-
-
Save kesor/2029358 to your computer and use it in GitHub Desktop.
Making Amazon AWS API Queries
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Very simple wrapper for signed and authenticated Amazon API queries. | |
Works with most Amazon AWS APIs and various other services. | |
The APIs themselves are described in the Amazon docs and are not part | |
of the code in this module. | |
More information about the API for various AWS services can be found | |
in the documentation at http://aws.amazon.com/documentation/ | |
The code is under a BSD license, detailed in the LICENSE file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2013, Evgeny Zislis | |
All rights reserved. | |
Redistribution and use in source and binary forms, with or without modification, | |
are permitted provided that the following conditions are met: | |
* Redistributions of source code must retain the above copyright notice, | |
this list of conditions and the following disclaimer. | |
* Redistributions in binary form must reproduce the above copyright notice, | |
this list of conditions and the following disclaimer in the documentation | |
and/or other materials provided with the distribution. | |
* Neither the name of `aws-api-engine` nor the names of its contributors | |
may be used to endorse or promote products derived from this software | |
without specific prior written permission. | |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import urllib | |
from amazon.query import AmazonQuery | |
if __name__ == '__main__': | |
key_id = 'AKIAIEXAMPLEEXAMPLE6' | |
secret = 'example+example+example+example+example7' | |
endpoint = 'https://cloudformation.us-west-1.amazonaws.com' | |
question = { 'Version': '2010-05-15', 'Action': 'ListStacks' } | |
endpoint = 'http://monitoring.us-west-1.amazonaws.com' | |
question = { 'Version': '2010-08-01', 'Action': 'ListMetrics' } | |
endpoint = 'http://ec2.us-west-1.amazonaws.com' | |
question = { 'Version': '2012-03-01', 'Action': 'DescribeInstances' } | |
question = { 'Version': '2012-03-01', 'Action': 'DescribeReservedInstancesOfferings' } | |
question = { 'Version': '2012-03-01', 'Action': 'DescribeAvailabilityZones' } | |
endpoint = 'http://elasticmapreduce.us-west-1.amazonaws.com' | |
question = { 'Version': '2009-03-31', 'Action': 'DescribeJobFlows' } | |
endpoint = 'http://ec2.us-east-1.amazonaws.com' | |
question = { 'Version': '2012-10-01', 'Action': 'DescribeSpotPriceHistory' } | |
endpoint = 'https://elasticache.us-west-1.amazonaws.com' | |
question = { 'Version': '2011-07-15', 'Action': 'DescribeCacheClusters' } | |
endpoint = 'https://rds.us-west-1.amazonaws.com' | |
question = { 'Version': '2012-01-15', 'Action': 'DescribeDBEngineVersions' } | |
endpoint = 'http://sns.us-west-1.amazonaws.com' | |
question = { 'Version': '2010-03-31', 'Action': 'ListTopics' } | |
endpoint = 'http://sqs.us-west-1.amazonaws.com' | |
question = { 'Version': '2011-10-01', 'Action': 'ListQueues' } | |
endpoint = 'http://sdb.us-west-1.amazonaws.com' | |
question = { 'Version': '2009-04-15', 'Action': 'ListDomains' } | |
endpoint = 'http://s3-us-west-1.amazonaws.com' | |
question = { 'Version': '2006-03-01', 'Action': 'ListAllMyBuckets' } | |
endpoint = 'https://elasticbeanstalk.us-east-1.amazonaws.com' | |
question = { 'Version': '2010-12-01', 'Action': 'ListAvailableSolutionStacks' } | |
endpoint = 'https://iam.amazonaws.com' | |
question = { 'Version': '2010-05-08', 'Action': 'ListUsers' } | |
endpoint = 'http://autoscaling.us-west-1.amazonaws.com' | |
question = { 'Version': '2011-01-01', 'Action': 'DescribeTags' } | |
endpoint = 'https://importexport.amazonaws.com' | |
question = { 'Version': '2010-06-01', 'Action': 'ListJobs' } | |
endpoint = 'https://sts.amazonaws.com' | |
question = { 'Version': '2011-06-15', 'Action': 'GetSessionToken' } | |
endpoint = 'https://iam.amazonaws.com' | |
question = { 'Version': '2010-05-08', 'Action': 'GetUser' } | |
question = { 'Version': '2010-05-08', 'Action': 'ListUserPolicies', 'UserName': 'test_iam' } | |
question = { 'Version': '2010-05-08', 'Action': 'GetUserPolicy', 'UserName': 'test_iam', 'PolicyName': 'ReadOnlyAccess-test_iam-201203291722' } | |
endpoint = 'http://elasticloadbalancing.us-west-1.amazonaws.com' | |
question = { 'Version': '2011-11-15', 'Action': 'DescribeLoadBalancers' } | |
query = AmazonQuery(endpoint, key_id, secret, question) | |
print urllib.FancyURLopener().open(endpoint, query.signed_parameters).read() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import urllib, urlparse | |
import hmac, hashlib, base64 | |
class AmazonQuery(object): | |
""" | |
Send a signed request to an Amazon AWS endpoint. | |
:type endpoint: string | |
:param endpoint: from http://docs.amazonwebservices.com/general/latest/gr/rande.html | |
:type key_id: string | |
:param key_id: The Access Key ID for the request sender. | |
:type secret_key: string | |
:param secret_key: Secret Access Key used for request signature. | |
:type parameters: dict | |
:param parameters: Optional additional request parameters. | |
""" | |
def __init__(self, endpoint, key_id, secret_key, parameters=dict()): | |
parsed = urlparse.urlparse(endpoint) | |
self.host = parsed.hostname | |
self.path = parsed.path or '/' | |
self.endpoint = endpoint | |
self.secret_key = secret_key | |
self.parameters = dict({ | |
'AWSAccessKeyId': key_id, | |
'SignatureVersion': 2, | |
'SignatureMethod': 'HmacSHA256', | |
}, **parameters) | |
def signed_parameters(self): | |
self.parameters['Timestamp'] = datetime.datetime.utcnow().isoformat() | |
params = dict(self.parameters, **{ 'Signature': self.signature }) | |
return urllib.urlencode(params) | |
signed_parameters = property(signed_parameters) | |
def signature(self): | |
params = urllib.urlencode( sorted(self.parameters.items()) ) | |
text = "\n".join(['POST', self.host, self.path, params]) | |
auth = hmac.new(self.secret_key, msg=text, digestmod=hashlib.sha256) | |
return base64.b64encode(auth.digest()) | |
signature = property(signature) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from amazon_query import AmazonQuery | |
class TestAmazonQuery(unittest.TestCase): | |
def test_apicall_parameters(self): | |
q = AmazonQuery('https://url.com', 'keyid', 'key', | |
{ 'Action': 'act', 'Version': '2012-03-01', 'foo': 'bar' }) | |
expected = { | |
'Action': 'act', | |
'Version': '2012-03-01', | |
'AWSAccessKeyId': 'keyid', | |
'SignatureVersion': 2, | |
'SignatureMethod': 'HmacSHA256', | |
'foo': 'bar', | |
} | |
self.assertDictContainsSubset(expected, q.parameters) | |
self.assertDictContainsSubset(q.parameters, expected) | |
def test_apicall_secret_key(self): | |
q = AmazonQuery('https://url.com', 'keyid', 'key') | |
self.assertEquals(q.secret_key, 'key') | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment