~txawsteam/txaws/trunk

« back to all changes in this revision

Viewing changes to txaws/credentials.py

Merged 416109-arbitrary-endpoints [r=therve,jkakar] [f=416109].

The primary change of this branch is support of arbitrary endpoints (needed for
the support of Eucalyptus). In addition, the following was also performed:
 * Added a parse utility function from Twisted
 * Created a testing subpackage for use by txAWS unit tests
 * Created a service module for abstracting regions and associated
   serices/credentials

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
import os
7
7
 
8
 
from txaws.util import *
 
8
from txaws.util import hmac_sha1
9
9
 
10
10
 
11
11
__all__ = ['AWSCredentials']
12
12
 
13
13
 
 
14
ENV_ACCESS_KEY = "AWS_ACCESS_KEY_ID"
 
15
ENV_SECRET_KEY = "AWS_SECRET_ACCESS_KEY"
 
16
 
 
17
 
14
18
class AWSCredentials(object):
15
 
 
16
 
    def __init__(self, access_key=None, secret_key=None):
17
 
        """Create an AWSCredentials object.
18
 
 
19
 
        :param access_key: The access key to use. If None the environment
20
 
            variable AWS_ACCESS_KEY_ID is consulted.
21
 
        :param secret_key: The secret key to use. If None the environment
22
 
            variable AWS_SECRET_ACCESS_KEY is consulted.
23
 
        """
 
19
    """Create an AWSCredentials object.
 
20
 
 
21
    @param access_key: The access key to use. If None the environment variable
 
22
        AWS_ACCESS_KEY_ID is consulted.
 
23
    @param secret_key: The secret key to use. If None the environment variable
 
24
        AWS_SECRET_ACCESS_KEY is consulted.
 
25
    """
 
26
 
 
27
    def __init__(self, access_key="", secret_key=""):
 
28
        self.access_key = access_key
24
29
        self.secret_key = secret_key
25
 
        if self.secret_key is None:
26
 
            self.secret_key = os.environ.get('AWS_SECRET_ACCESS_KEY')
27
 
        if self.secret_key is None:
28
 
            raise ValueError('Could not find AWS_SECRET_ACCESS_KEY')
29
 
        self.access_key = access_key
30
 
        if self.access_key is None:
31
 
            self.access_key = os.environ.get('AWS_ACCESS_KEY_ID')
32
 
        if self.access_key is None:
33
 
            raise ValueError('Could not find AWS_ACCESS_KEY_ID')
 
30
        # perform checks for access key
 
31
        if not self.access_key:
 
32
            self.access_key = os.environ.get(ENV_ACCESS_KEY)
 
33
        if not self.access_key:
 
34
            raise ValueError("Could not find %s" % ENV_ACCESS_KEY)
 
35
        # perform checks for secret key
 
36
        if not self.secret_key:
 
37
            self.secret_key = os.environ.get(ENV_SECRET_KEY)
 
38
        if not self.secret_key:
 
39
            raise ValueError("Could not find %s" % ENV_SECRET_KEY)
34
40
 
35
41
    def sign(self, bytes):
36
42
        """Sign some bytes."""