Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Use boto.exception for checking expirations

The exceptions in boto.exception are able to parse
all of the "credentials expired" messages, so those
classes are used to check whether or not credentials
are expired.  This removes the usage of ElementTree
and StringIO.

I ran the s3 integration tests against 2.6 and 2.7 and
they're passing for me.
  • Loading branch information...
commit 58a44d78a4cb4ef152c15ccd27d14f0968f2238d 1 parent 2894d9a
@jamesls jamesls authored
View
20 boto/connection.py
@@ -55,8 +55,6 @@
import time
import urllib, urlparse
import xml.sax
-from xml.etree import ElementTree
-from cStringIO import StringIO
import auth
import auth_handler
@@ -67,7 +65,7 @@
from boto import config, UserAgent
from boto.exception import AWSConnectionError, BotoClientError
-from boto.exception import BotoServerError, XMLParseError
+from boto.exception import BotoServerError
from boto.provider import Provider
from boto.resultset import ResultSet
@@ -372,9 +370,8 @@ def authorize(self, connection, **kwargs):
class HTTPResponse(httplib.HTTPResponse):
- def __init__(self, sock, debuglevel=0, strict=0, method=None, buffering=False):
- httplib.HTTPResponse.__init__(self, sock, debuglevel, strict, method,
- buffering)
+ def __init__(self, *args, **kwargs):
+ httplib.HTTPResponse.__init__(self, *args, **kwargs)
self._cached_response = ''
def read(self, amt=None):
@@ -873,15 +870,8 @@ def _credentials_expired(self, response):
# renewed.
if response.status != 403:
return False
- try:
- for event, node in ElementTree.iterparse(StringIO(response.read()),
- events=['start']):
- if node.tag.endswith('Code'):
- if node.text == 'ExpiredToken':
- return True
- except XMLParseError:
- return False
- return False
+ error = BotoServerError('', '', body=response.read())
+ return error.error_code == 'ExpiredToken'
def _renew_credentials(self):
# By resetting the provider with a new provider, this will trigger the
View
10 boto/ec2/connection.py
@@ -28,8 +28,6 @@
import warnings
from datetime import datetime
from datetime import timedelta
-from xml.etree import ElementTree
-from cStringIO import StringIO
import boto
from boto.connection import AWSQueryConnection
@@ -98,10 +96,10 @@ def _required_auth_capability(self):
def _credentials_expired(self, response):
if response.status != 400:
return False
- for event, node in ElementTree.iterparse(StringIO(response.read()),
- events=['start']):
- if node.tag.endswith('Code'):
- if node.text == 'RequestExpired':
+ error = EC2ResponseError('', '', body=response.read())
+ if error.errors is not None:
+ for code, message in error.errors:
+ if code == 'RequestExpired':
return True
return False
View
6 boto/exception.py
@@ -29,12 +29,6 @@
from boto import handler
from boto.resultset import ResultSet
-try:
- from xml.etree.ElementTree import ParseError as XMLParseError
-except ImportError:
- # In python2.6/5, xml.etree.ElementTree.ParseError does not exist.
- from xml.parsers.expat import ExpatError as XMLParseError
-
class BotoClientError(StandardError):
"""
View
14 boto/route53/connection.py
@@ -24,12 +24,9 @@
import time
import uuid
import urllib
-from xml.etree import ElementTree
-from cStringIO import StringIO
import boto
from boto.connection import AWSAuthConnection
-from boto.exception import XMLParseError
from boto import handler
from boto.resultset import ResultSet
import boto.jsonresponse
@@ -72,15 +69,8 @@ def _required_auth_capability(self):
def _credentials_expired(self, response):
if response.status != 403:
return False
- try:
- for event, node in ElementTree.iterparse(StringIO(response.read()),
- events=['start']):
- if node.tag.endswith('Code'):
- if node.text == 'InvalidClientTokenId':
- return True
- except XMLParseError:
- return False
- return False
+ error = exception.DNSServerError('', '', body=response.read())
+ return error.error_code == 'InvalidClientTokenId'
def make_request(self, action, path, headers=None, data='', params=None):
if params:
View
15 boto/s3/connection.py
@@ -22,11 +22,9 @@
# IN THE SOFTWARE.
import xml.sax
-from xml.etree import ElementTree
import urllib
import base64
import time
-from cStringIO import StringIO
import boto.utils
from boto.connection import AWSAuthConnection
@@ -34,7 +32,7 @@
from boto.s3.bucket import Bucket
from boto.s3.key import Key
from boto.resultset import ResultSet
-from boto.exception import BotoClientError, XMLParseError
+from boto.exception import BotoClientError, S3ResponseError
def check_lowercase_bucketname(n):
@@ -177,15 +175,8 @@ def _required_auth_capability(self):
def _credentials_expired(self, response):
if response.status != 400:
return False
- try:
- for event, node in ElementTree.iterparse(StringIO(response.read()),
- events=['start']):
- if node.tag.endswith('Code'):
- if node.text == 'ExpiredToken':
- return True
- except XMLParseError:
- return False
- return False
+ error = S3ResponseError('', '', body=response.read())
+ return error.error_code == 'ExpiredToken'
def __iter__(self):
for bucket in self.get_all_buckets():
View
14 boto/ses/connection.py
@@ -21,8 +21,6 @@
# IN THE SOFTWARE.
import urllib
import base64
-from xml.etree import ElementTree
-from cStringIO import StringIO
from boto.connection import AWSAuthConnection
from boto.exception import BotoServerError
@@ -30,6 +28,7 @@
import boto
import boto.jsonresponse
from boto.ses import exceptions as ses_exceptions
+from boto.exception import BotoServerError
class SESConnection(AWSAuthConnection):
@@ -61,15 +60,8 @@ def _required_auth_capability(self):
def _credentials_expired(self, response):
if response.status != 403:
return False
- try:
- for event, node in ElementTree.iterparse(StringIO(response.read()),
- events=['start']):
- if node.tag.endswith('Code'):
- if node.text == 'InvalidClientTokenId':
- return True
- except ElementTree.ParseError:
- return False
- return False
+ error = BotoServerError('', '', body=response.read())
+ return error.error_code == 'InvalidClientTokenId'
def _build_list_params(self, params, items, label):
"""Add an AWS API-compatible parameter list to a dictionary.
View
15 boto/sqs/connection.py
@@ -18,8 +18,6 @@
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
-from xml.etree import ElementTree
-from cStringIO import StringIO
from boto.connection import AWSQueryConnection
from boto.sqs.regioninfo import SQSRegionInfo
@@ -27,7 +25,7 @@
from boto.sqs.message import Message
from boto.sqs.attributes import Attributes
from boto.sqs.batchresults import BatchResults
-from boto.exception import SQSError, XMLParseError
+from boto.exception import SQSError, BotoServerError
class SQSConnection(AWSQueryConnection):
@@ -64,15 +62,8 @@ def _required_auth_capability(self):
def _credentials_expired(self, response):
if response.status != 401:
return False
- try:
- for event, node in ElementTree.iterparse(StringIO(response.read()),
- events=['start']):
- if node.tag.endswith('Code'):
- if node.text == 'InvalidAccessKeyId':
- return True
- except XMLParseError:
- return False
- return False
+ error = BotoServerError('', '', body=response.read())
+ return error.error_code == 'InvalidAccessKeyId'
def create_queue(self, queue_name, visibility_timeout=None):
"""
Please sign in to comment.
Something went wrong with that request. Please try again.