OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2011 Mitch Garnaat http://garnaat.org/ |
| 2 # Copyright (c) 2011, Eucalyptus Systems, Inc. |
| 3 # |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a |
| 5 # copy of this software and associated documentation files (the |
| 6 # "Software"), to deal in the Software without restriction, including |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- |
| 10 # lowing conditions: |
| 11 # |
| 12 # The above copyright notice and this permission notice shall be included |
| 13 # in all copies or substantial portions of the Software. |
| 14 # |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 # IN THE SOFTWARE. |
| 22 |
| 23 import os |
| 24 import datetime |
| 25 |
| 26 import boto.utils |
| 27 from boto.compat import json |
| 28 |
| 29 |
| 30 class Credentials(object): |
| 31 """ |
| 32 :ivar access_key: The AccessKeyID. |
| 33 :ivar secret_key: The SecretAccessKey. |
| 34 :ivar session_token: The session token that must be passed with |
| 35 requests to use the temporary credentials |
| 36 :ivar expiration: The timestamp for when the credentials will expire |
| 37 """ |
| 38 |
| 39 def __init__(self, parent=None): |
| 40 self.parent = parent |
| 41 self.access_key = None |
| 42 self.secret_key = None |
| 43 self.session_token = None |
| 44 self.expiration = None |
| 45 |
| 46 @classmethod |
| 47 def from_json(cls, json_doc): |
| 48 """ |
| 49 Create and return a new Session Token based on the contents |
| 50 of a JSON document. |
| 51 |
| 52 :type json_doc: str |
| 53 :param json_doc: A string containing a JSON document with a |
| 54 previously saved Credentials object. |
| 55 """ |
| 56 d = json.loads(json_doc) |
| 57 token = cls() |
| 58 token.__dict__.update(d) |
| 59 return token |
| 60 |
| 61 @classmethod |
| 62 def load(cls, file_path): |
| 63 """ |
| 64 Create and return a new Session Token based on the contents |
| 65 of a previously saved JSON-format file. |
| 66 |
| 67 :type file_path: str |
| 68 :param file_path: The fully qualified path to the JSON-format |
| 69 file containing the previously saved Session Token information. |
| 70 """ |
| 71 fp = open(file_path) |
| 72 json_doc = fp.read() |
| 73 fp.close() |
| 74 return cls.from_json(json_doc) |
| 75 |
| 76 def startElement(self, name, attrs, connection): |
| 77 return None |
| 78 |
| 79 def endElement(self, name, value, connection): |
| 80 if name == 'AccessKeyId': |
| 81 self.access_key = value |
| 82 elif name == 'SecretAccessKey': |
| 83 self.secret_key = value |
| 84 elif name == 'SessionToken': |
| 85 self.session_token = value |
| 86 elif name == 'Expiration': |
| 87 self.expiration = value |
| 88 elif name == 'RequestId': |
| 89 self.request_id = value |
| 90 else: |
| 91 pass |
| 92 |
| 93 def to_dict(self): |
| 94 """ |
| 95 Return a Python dict containing the important information |
| 96 about this Session Token. |
| 97 """ |
| 98 return {'access_key': self.access_key, |
| 99 'secret_key': self.secret_key, |
| 100 'session_token': self.session_token, |
| 101 'expiration': self.expiration, |
| 102 'request_id': self.request_id} |
| 103 |
| 104 def save(self, file_path): |
| 105 """ |
| 106 Persist a Session Token to a file in JSON format. |
| 107 |
| 108 :type path: str |
| 109 :param path: The fully qualified path to the file where the |
| 110 the Session Token data should be written. Any previous |
| 111 data in the file will be overwritten. To help protect |
| 112 the credentials contained in the file, the permissions |
| 113 of the file will be set to readable/writable by owner only. |
| 114 """ |
| 115 fp = open(file_path, 'wb') |
| 116 json.dump(self.to_dict(), fp) |
| 117 fp.close() |
| 118 os.chmod(file_path, 0600) |
| 119 |
| 120 def is_expired(self, time_offset_seconds=0): |
| 121 """ |
| 122 Checks to see if the Session Token is expired or not. By default |
| 123 it will check to see if the Session Token is expired as of the |
| 124 moment the method is called. However, you can supply an |
| 125 optional parameter which is the number of seconds of offset |
| 126 into the future for the check. For example, if you supply |
| 127 a value of 5, this method will return a True if the Session |
| 128 Token will be expired 5 seconds from this moment. |
| 129 |
| 130 :type time_offset_seconds: int |
| 131 :param time_offset_seconds: The number of seconds into the future |
| 132 to test the Session Token for expiration. |
| 133 """ |
| 134 now = datetime.datetime.utcnow() |
| 135 if time_offset_seconds: |
| 136 now = now + datetime.timedelta(seconds=time_offset_seconds) |
| 137 ts = boto.utils.parse_ts(self.expiration) |
| 138 delta = ts - now |
| 139 return delta.total_seconds() <= 0 |
| 140 |
| 141 class FederationToken(object): |
| 142 """ |
| 143 :ivar credentials: A Credentials object containing the credentials. |
| 144 :ivar federated_user_arn: ARN specifying federated user using credentials. |
| 145 :ivar federated_user_id: The ID of the federated user using credentials. |
| 146 :ivar packed_policy_size: A percentage value indicating the size of |
| 147 the policy in packed form |
| 148 """ |
| 149 |
| 150 def __init__(self, parent=None): |
| 151 self.parent = parent |
| 152 self.credentials = None |
| 153 self.federated_user_arn = None |
| 154 self.federated_user_id = None |
| 155 self.packed_policy_size = None |
| 156 |
| 157 def startElement(self, name, attrs, connection): |
| 158 if name == 'Credentials': |
| 159 self.credentials = Credentials() |
| 160 return self.credentials |
| 161 else: |
| 162 return None |
| 163 |
| 164 def endElement(self, name, value, connection): |
| 165 if name == 'Arn': |
| 166 self.federated_user_arn = value |
| 167 elif name == 'FederatedUserId': |
| 168 self.federated_user_id = value |
| 169 elif name == 'PackedPolicySize': |
| 170 self.packed_policy_size = int(value) |
| 171 elif name == 'RequestId': |
| 172 self.request_id = value |
| 173 else: |
| 174 pass |
| 175 |
| 176 |
| 177 class AssumedRole(object): |
| 178 """ |
| 179 :ivar user: The assumed role user. |
| 180 :ivar credentials: A Credentials object containing the credentials. |
| 181 """ |
| 182 def __init__(self, connection=None, credentials=None, user=None): |
| 183 self._connection = connection |
| 184 self.credentials = credentials |
| 185 self.user = user |
| 186 |
| 187 def startElement(self, name, attrs, connection): |
| 188 if name == 'Credentials': |
| 189 self.credentials = Credentials() |
| 190 return self.credentials |
| 191 elif name == 'AssumedRoleUser': |
| 192 self.user = User() |
| 193 return self.user |
| 194 |
| 195 def endElement(self, name, value, connection): |
| 196 pass |
| 197 |
| 198 |
| 199 class User(object): |
| 200 """ |
| 201 :ivar arn: The arn of the user assuming the role. |
| 202 :ivar assume_role_id: The identifier of the assumed role. |
| 203 """ |
| 204 def __init__(self, arn=None, assume_role_id=None): |
| 205 self.arn = arn |
| 206 self.assume_role_id = assume_role_id |
| 207 |
| 208 def startElement(self, name, attrs, connection): |
| 209 pass |
| 210 |
| 211 def endElement(self, name, value, connection): |
| 212 if name == 'Arn': |
| 213 self.arn = value |
| 214 elif name == 'AssumedRoleId': |
| 215 self.assume_role_id = value |
OLD | NEW |