Python botocore.exceptions() Examples

The following are 30 code examples of botocore.exceptions(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module botocore , or try the search function .
Example #1
Source File: utils.py    From AWS-Transit-Gateway-Demo-MultiAccount with MIT License 6 votes vote down vote up
def should_bypass_proxies(url):
    """
    Returns whether we should bypass proxies or not.
    """
    # NOTE: requests allowed for ip/cidr entries in no_proxy env that we don't
    # support current as urllib only checks DNS suffix
    # If the system proxy settings indicate that this URL should be bypassed,
    # don't proxy.
    # The proxy_bypass function is incredibly buggy on OS X in early versions
    # of Python 2.6, so allow this call to fail. Only catch the specific
    # exceptions we've seen, though: this call failing in other ways can reveal
    # legitimate problems.
    try:
        if proxy_bypass(urlparse(url).netloc):
            return True
    except (TypeError, socket.gaierror):
        pass

    return False 
Example #2
Source File: utils.py    From aws-builders-fair-projects with Apache License 2.0 6 votes vote down vote up
def should_bypass_proxies(url):
    """
    Returns whether we should bypass proxies or not.
    """
    # NOTE: requests allowed for ip/cidr entries in no_proxy env that we don't
    # support current as urllib only checks DNS suffix
    # If the system proxy settings indicate that this URL should be bypassed,
    # don't proxy.
    # The proxy_bypass function is incredibly buggy on OS X in early versions
    # of Python 2.6, so allow this call to fail. Only catch the specific
    # exceptions we've seen, though: this call failing in other ways can reveal
    # legitimate problems.
    try:
        if proxy_bypass(urlparse(url).netloc):
            return True
    except (TypeError, socket.gaierror):
        pass

    return False 
Example #3
Source File: test_awsclient.py    From chalice with Apache License 2.0 6 votes vote down vote up
def test_update_function_fails_after_max_retries(self, stubbed_session):
        stubbed_session.stub('lambda').update_function_code(
            FunctionName='name', ZipFile=b'foo').returns(
                {'FunctionArn': 'arn'})

        update_config_kwargs = {
            'FunctionName': 'name',
            'Role': 'role-arn'
        }
        for _ in range(TypedAWSClient.LAMBDA_CREATE_ATTEMPTS):
            stubbed_session.stub('lambda').update_function_configuration(
                **update_config_kwargs).raises_error(
                    error_code='InvalidParameterValueException',
                    message=('The role defined for the function cannot '
                             'be assumed by Lambda.'))
        stubbed_session.activate_stubs()
        awsclient = TypedAWSClient(stubbed_session, mock.Mock(spec=time.sleep))

        with pytest.raises(botocore.exceptions.ClientError):
            awsclient.update_function('name', b'foo', role_arn='role-arn')
        stubbed_session.verify_stubs() 
Example #4
Source File: test_awsclient.py    From chalice with Apache License 2.0 6 votes vote down vote up
def test_create_function_fails_after_max_retries(self, stubbed_session):
        kwargs = {
            'FunctionName': 'name',
            'Runtime': 'python2.7',
            'Code': {'ZipFile': b'foo'},
            'Handler': 'app.app',
            'Role': 'myarn',
        }
        for _ in range(TypedAWSClient.LAMBDA_CREATE_ATTEMPTS):
            stubbed_session.stub('lambda').create_function(
                **kwargs).raises_error(
                error_code='InvalidParameterValueException',
                message=('The role defined for the function cannot '
                         'be assumed by Lambda.')
                )

        stubbed_session.activate_stubs()
        awsclient = TypedAWSClient(stubbed_session, mock.Mock(spec=time.sleep))
        with pytest.raises(LambdaClientError) as excinfo:
            awsclient.create_function('name', 'myarn', b'foo', 'python2.7',
                                      'app.app')
        assert isinstance(
            excinfo.value.original_error, botocore.exceptions.ClientError)
        stubbed_session.verify_stubs() 
Example #5
Source File: test_driver_s3.py    From streamalert with Apache License 2.0 6 votes vote down vote up
def test_upload_with_error():
        """LookupTables - Drivers - S3 Driver - Adapter - AWS Error"""
        driver = S3Driver({
            'bucket': 'bucket',
            'key': 'key',
            'compression': 'gzip'
        })
        boto_s3_client = MagicMock(name='Boto3Client')
        boto_s3_client.Bucket.return_value.put_object.side_effect = \
            botocore.exceptions.ClientError({'Error': {'Message': 'uh oh'}}, 'operation_name')
        adapter = S3Adapter(
            driver,
            boto_s3_client,
            'bucket',
            'key'
        )

        assert_raises(LookupTablesCommitError, adapter.upload, 'asdf') 
Example #6
Source File: test_driver_s3.py    From streamalert with Apache License 2.0 6 votes vote down vote up
def test_upload_with_timeout():
        """LookupTables - Drivers - S3 Driver - Adapter - AWS Timeout"""
        driver = S3Driver({
            'bucket': 'bucket',
            'key': 'key',
            'compression': 'gzip'
        })
        boto_s3_client = MagicMock(name='Boto3Client')
        boto_s3_client.Bucket.return_value.put_object.side_effect = \
            botocore.exceptions.ConnectTimeoutError(endpoint_url='http://yay')
        adapter = S3Adapter(
            driver,
            boto_s3_client,
            'bucket',
            'key'
        )

        assert_raises(LookupTablesCommitError, adapter.upload, 'asdf') 
Example #7
Source File: test_awsclient.py    From chalice with Apache License 2.0 6 votes vote down vote up
def test_create_role_raises_error_on_failure(self, stubbed_session):
        arn = 'good_arn' * 3
        role_id = 'abcd' * 4
        today = datetime.datetime.today()
        stubbed_session.stub('iam').create_role(
            RoleName='role_name',
            AssumeRolePolicyDocument=json.dumps({'trust': 'policy'})
        ).returns({'Role': {
            'RoleName': 'No', 'Arn': arn, 'Path': '/',
            'RoleId': role_id, 'CreateDate': today}}
        )
        stubbed_session.stub('iam').put_role_policy(
            RoleName='role_name',
            PolicyName='role_name',
            PolicyDocument={'policy': 'document'}
        ).raises_error(
            error_code='MalformedPolicyDocumentException',
            message='MalformedPolicyDocument'
        )
        stubbed_session.activate_stubs()
        awsclient = TypedAWSClient(stubbed_session)
        with pytest.raises(botocore.exceptions.ClientError):
            awsclient.create_role(
                'role_name', {'trust': 'policy'}, {'policy': 'document'})
        stubbed_session.verify_stubs() 
Example #8
Source File: aws.py    From deepWordBug with Apache License 2.0 6 votes vote down vote up
def _set_operation(service_name, operation_name):
    try:
        client = _get_client(service_name)
    except botocore.exceptions.UnknownEndpointError as e:
        raise NoRegionError(e)
    except botocore.exceptions.PartialCredentialsError as e:
        LOG.debug('Credentials incomplete')
        raise CredentialsError('Your credentials are not complete. Error: {0}'
                               .format(e))
    except botocore.exceptions.NoRegionError:
        raise NoRegionError()

    if not _verify_ssl:
        warnings.filterwarnings("ignore")

    return getattr(client, operation_name) 
Example #9
Source File: utils.py    From deepWordBug with Apache License 2.0 6 votes vote down vote up
def should_bypass_proxies(url):
    """
    Returns whether we should bypass proxies or not.
    """
    # NOTE: requests allowed for ip/cidr entries in no_proxy env that we don't
    # support current as urllib only checks DNS suffix
    # If the system proxy settings indicate that this URL should be bypassed,
    # don't proxy.
    # The proxy_bypass function is incredibly buggy on OS X in early versions
    # of Python 2.6, so allow this call to fail. Only catch the specific
    # exceptions we've seen, though: this call failing in other ways can reveal
    # legitimate problems.
    try:
        if proxy_bypass(urlparse(url).netloc):
            return True
    except (TypeError, socket.gaierror):
        pass

    return False 
Example #10
Source File: deployer.py    From chalice with Apache License 2.0 6 votes vote down vote up
def _get_error_message_for_connection_error(self, connection_error):
        # type: (RequestsConnectionError) -> str

        # To get the underlying error that raised the
        # requests.ConnectionError it is required to go down two levels of
        # arguments to get the underlying exception. The instantiation of
        # one of these exceptions looks like this:
        #
        # requests.ConnectionError(
        #     urllib3.exceptions.ProtocolError(
        #         'Connection aborted.', <SomeException>)
        # )
        message = connection_error.args[0].args[0]
        underlying_error = connection_error.args[0].args[1]
        if is_broken_pipe_error(underlying_error):
            message += (
                ' Lambda closed the connection before chalice finished '
                'sending all of the data.'
            )
        elif isinstance(underlying_error, socket.timeout):
            message += ' Timed out sending your app to Lambda.'
        return message 
Example #11
Source File: __main__.py    From awsmfa with Apache License 2.0 6 votes vote down vote up
def make_session(identity_profile):
    session = botocore.session.Session(profile=identity_profile)
    try:
        session3 = boto3.session.Session(botocore_session=session)
    except botocore.exceptions.ProfileNotFound as err:
        print(str(err), file=sys.stderr)
        if session.available_profiles:
            print("Available profiles: %s" %
                  ", ".join(sorted(session.available_profiles)),
                  file=sys.stderr)
            print("You can specify a profile by passing it with the -i "
                  "command line flag.", file=sys.stderr)
        else:
            print("You have no AWS profiles configured. Please run 'aws "
                  "configure --profile identity' to get started.",
                  file=sys.stderr)
        return None, None, USER_RECOVERABLE_ERROR
    return session, session3, None 
Example #12
Source File: signers.py    From aiobotocore with Apache License 2.0 6 votes vote down vote up
def get_auth_instance(self, signing_name, region_name,
                                signature_version=None, **kwargs):
        if signature_version is None:
            signature_version = self._signature_version

        cls = botocore.auth.AUTH_TYPE_MAPS.get(signature_version)
        if cls is None:
            raise UnknownSignatureVersionError(
                signature_version=signature_version)

        frozen_credentials = None
        if self._credentials is not None:
            frozen_credentials = await self._credentials.get_frozen_credentials()
        kwargs['credentials'] = frozen_credentials
        if cls.REQUIRES_REGION:
            if self._region_name is None:
                raise botocore.exceptions.NoRegionError()
            kwargs['region_name'] = region_name
            kwargs['service_name'] = signing_name
        auth = cls(**kwargs)
        return auth

    # Alias get_auth for backwards compatibility. 
Example #13
Source File: utils.py    From AWS-Transit-Gateway-Demo-MultiAccount with MIT License 6 votes vote down vote up
def should_bypass_proxies(url):
    """
    Returns whether we should bypass proxies or not.
    """
    # NOTE: requests allowed for ip/cidr entries in no_proxy env that we don't
    # support current as urllib only checks DNS suffix
    # If the system proxy settings indicate that this URL should be bypassed,
    # don't proxy.
    # The proxy_bypass function is incredibly buggy on OS X in early versions
    # of Python 2.6, so allow this call to fail. Only catch the specific
    # exceptions we've seen, though: this call failing in other ways can reveal
    # legitimate problems.
    try:
        if proxy_bypass(urlparse(url).netloc):
            return True
    except (TypeError, socket.gaierror):
        pass

    return False 
Example #14
Source File: workspaces_helper.py    From workspaces-cost-optimizer with Apache License 2.0 6 votes vote down vote up
def modify_workspace_properties(self, workspaceID, newRunningMode, isDryRun):
        log.debug('modifyWorkspaceProperties')
        try:
            if isDryRun == False:
                wsModWS = self.client.modify_workspace_properties(
                    WorkspaceId = workspaceID,
                    WorkspaceProperties = { 'RunningMode': newRunningMode }
                )
            else:
                log.info('Skipping modifyWorkspaceProperties for Workspace %s due to dry run', workspaceID)

            if newRunningMode == 'ALWAYS_ON':
                result = '-M-'
            elif newRunningMode == 'AUTO_STOP':
                result = '-H-'

            return result

        except botocore.exceptions.ClientError as e:
            log.error('Exceeded retries for %s due to error: %s', workspaceID, e)

        return result 
Example #15
Source File: accessadvisor_automation.py    From aws-iam-accessadvisor-permissionboundary with Apache License 2.0 6 votes vote down vote up
def tag_role(role, key, value):
    client = boto3.client('iam')
    try:
        response = client.tag_role(
            RoleName=role,
            Tags=[
                {
                    'Key': key,
                    'Value': value
                },
            ]
        )
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == True:
            response = 'We got an error'
        else:
            response = "Unexpected error: %s" % e
    return response

# Tag the user with key and value 
Example #16
Source File: accessadvisor_automation.py    From aws-iam-accessadvisor-permissionboundary with Apache License 2.0 6 votes vote down vote up
def tag_user(user, key, value):
    client = boto3.client('iam')
    try:
        response = client.tag_user(
            UserName=user,
            Tags=[
                {
                    'Key': key,
                    'Value': value
                },
            ]
        )
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == True:
            response = 'We got an error'
        else:
            response = "Unexpected error: %s" % e
    return response


###########################################
# Review of IAM users with access advisor
########################################### 
Example #17
Source File: s3uploader.py    From bash-lambda-layer with MIT License 6 votes vote down vote up
def file_exists(self, remote_path):
        """
        Check if the file we are trying to upload already exists in S3

        :param remote_path:
        :return: True, if file exists. False, otherwise
        """

        try:
            # Find the object that matches this ETag
            self.s3.head_object(
                Bucket=self.bucket_name, Key=remote_path)
            return True
        except botocore.exceptions.ClientError:
            # Either File does not exist or we are unable to get
            # this information.
            return False 
Example #18
Source File: utils.py    From bash-lambda-layer with MIT License 6 votes vote down vote up
def should_bypass_proxies(url):
    """
    Returns whether we should bypass proxies or not.
    """
    # NOTE: requests allowed for ip/cidr entries in no_proxy env that we don't
    # support current as urllib only checks DNS suffix
    # If the system proxy settings indicate that this URL should be bypassed,
    # don't proxy.
    # The proxy_bypass function is incredibly buggy on OS X in early versions
    # of Python 2.6, so allow this call to fail. Only catch the specific
    # exceptions we've seen, though: this call failing in other ways can reveal
    # legitimate problems.
    try:
        if proxy_bypass(urlparse(url).netloc):
            return True
    except (TypeError, socket.gaierror):
        pass

    return False 
Example #19
Source File: aws.py    From aws-elastic-beanstalk-cli with Apache License 2.0 6 votes vote down vote up
def _set_operation(service_name, operation_name):
    try:
        client = _get_client(service_name)
    except botocore.exceptions.UnknownEndpointError as e:
        raise NoRegionError(e)
    except botocore.exceptions.PartialCredentialsError as e:
        LOG.debug('Credentials incomplete')
        raise CredentialsError('Your credentials are not complete. Error: {0}'
                               .format(e))
    except botocore.exceptions.NoRegionError:
        raise NoRegionError()

    if not _verify_ssl:
        warnings.filterwarnings("ignore")

    return getattr(client, operation_name) 
Example #20
Source File: accessadvisor_automation.py    From aws-iam-accessadvisor-permissionboundary with Apache License 2.0 5 votes vote down vote up
def getServiceLastAccessedDetails(jobid):
    # if you have thousands of roles lambda may time out
    response = None
    marker = None
    client = boto3.client('iam')

    # By default, only 100 roles are returned at a time.
    # 'Marker' is used for pagination.
    status = False
    while (response is None or response['IsTruncated']):
        # Marker is only accepted if result was truncated.
        while status != "COMPLETED":
            try:
                if marker is None:
                    response = client.get_service_last_accessed_details(
                        JobId=jobid
                    )
                else:
                    response = client.get_service_last_accessed_details(
                        JobId=jobid,
                        Marker=marker
                    )

                status = response['JobStatus']
                print('job status: ', status)
                time.sleep(2)
            except botocore.exceptions.ClientError as e:
                status = False
                if e.response['Error']['Code'] == True:
                    response = 'We got an error'
                else:
                    response = "Unexpected error: %s" % e
    return response

# Using jobid obtain permission used and not used information with entities 
Example #21
Source File: accessadvisor_automation.py    From aws-iam-accessadvisor-permissionboundary with Apache License 2.0 5 votes vote down vote up
def list_attached_role_policies(role, path):
    client = boto3.client('iam')
    try:
        response = client.list_attached_role_policies(
            RoleName=role,
            PathPrefix=path
        )
        policies = response
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == True:
            policies = 'We got an error'
        else:
            policies = "Unexpected error: %s" % e
    return policies 
Example #22
Source File: handlers.py    From aws-builders-fair-projects with Apache License 2.0 5 votes vote down vote up
def check_for_200_error(response, **kwargs):
    # From: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html
    # There are two opportunities for a copy request to return an error. One
    # can occur when Amazon S3 receives the copy request and the other can
    # occur while Amazon S3 is copying the files. If the error occurs before
    # the copy operation starts, you receive a standard Amazon S3 error. If the
    # error occurs during the copy operation, the error response is embedded in
    # the 200 OK response. This means that a 200 OK response can contain either
    # a success or an error. Make sure to design your application to parse the
    # contents of the response and handle it appropriately.
    #
    # So this handler checks for this case.  Even though the server sends a
    # 200 response, conceptually this should be handled exactly like a
    # 500 response (with respect to raising exceptions, retries, etc.)
    # We're connected *before* all the other retry logic handlers, so as long
    # as we switch the error code to 500, we'll retry the error as expected.
    if response is None:
        # A None response can happen if an exception is raised while
        # trying to retrieve the response.  See Endpoint._get_response().
        return
    http_response, parsed = response
    if _looks_like_special_case_error(http_response):
        logger.debug("Error found for response with 200 status code, "
                     "errors: %s, changing status code to "
                     "500.", parsed)
        http_response.status_code = 500 
Example #23
Source File: accessadvisor_automation.py    From aws-iam-accessadvisor-permissionboundary with Apache License 2.0 5 votes vote down vote up
def get_list_s3(bucket, key):
    print({'msg': 'get_s3_object', 'bucket': bucket, 'key': key})
    s3 = boto3.resource('s3')
    do_not_list = []
    try:
        object = s3.Object(bucket, key)

        '''
        Iterates through all the objects, doing the pagination for you. Each obj is an ObjectSummary, so it doesn't 
        contain the body. You'll need to call get to get the whole body.
        '''
        body = object.get()['Body'].read()
        body = (body.decode('utf8'))
        list = body.split (',')
        for i in list:
            i = i.strip('\n')
            if i:
                do_not_list.append(i)
    except botocore.exceptions.ClientError as e:
        if e.response['Error']['Code'] == True:
            do_not_list = 'We got an error'
        else:
            do_not_list = "Unexpected error: %s" % e

    return do_not_list

# Get list of IAM users in the AWS account 
Example #24
Source File: metrics_helper.py    From workspaces-cost-optimizer with Apache License 2.0 5 votes vote down vote up
def get_billable_time(self, workspaceID, startTime, endTime):

        log.debug('getMetricStatistics')
        try:
            metrics = self.client.get_metric_statistics(
                Dimensions=[{
                    'Name': 'WorkspaceId',
                    'Value': workspaceID
                }],
                Namespace='AWS/WorkSpaces',
                MetricName='UserConnected',
                StartTime=startTime,
                EndTime=endTime,
                Period=3600,
                Statistics=['Maximum']
            )
            log.debug(metrics)
        except botocore.exceptions.ClientError as e:
            log.error(e)
            raise

        billable_time = 0
        log.info('METRICS for WS:%s:', workspaceID)
        for metric in metrics['Datapoints']:
            if metric['Maximum'] >= 1:
                billable_time += 1
                log.info('METRIC %d -> %s', billable_time, metric)

        return int(billable_time) 
Example #25
Source File: handlers.py    From aws-extender with MIT License 5 votes vote down vote up
def check_for_200_error(response, **kwargs):
    # From: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html
    # There are two opportunities for a copy request to return an error. One
    # can occur when Amazon S3 receives the copy request and the other can
    # occur while Amazon S3 is copying the files. If the error occurs before
    # the copy operation starts, you receive a standard Amazon S3 error. If the
    # error occurs during the copy operation, the error response is embedded in
    # the 200 OK response. This means that a 200 OK response can contain either
    # a success or an error. Make sure to design your application to parse the
    # contents of the response and handle it appropriately.
    #
    # So this handler checks for this case.  Even though the server sends a
    # 200 response, conceptually this should be handled exactly like a
    # 500 response (with respect to raising exceptions, retries, etc.)
    # We're connected *before* all the other retry logic handlers, so as long
    # as we switch the error code to 500, we'll retry the error as expected.
    if response is None:
        # A None response can happen if an exception is raised while
        # trying to retrieve the response.  See Endpoint._get_response().
        return
    http_response, parsed = response
    if _looks_like_special_case_error(http_response):
        logger.debug("Error found for response with 200 status code, "
                     "errors: %s, changing status code to "
                     "500.", parsed)
        http_response.status_code = 500 
Example #26
Source File: test_awsclient.py    From chalice with Apache License 2.0 5 votes vote down vote up
def test_lambda_function_bad_error_propagates(self, stubbed_session):
        stubbed_session.stub('lambda').get_function(FunctionName='myappname')\
                .raises_error(error_code='UnexpectedError',
                              message='Unknown')

        stubbed_session.activate_stubs()

        awsclient = TypedAWSClient(stubbed_session)
        with pytest.raises(botocore.exceptions.ClientError):
            awsclient.lambda_function_exists(name='myappname')

        stubbed_session.verify_stubs() 
Example #27
Source File: auth.py    From hass-nabucasa with GNU General Public License v3.0 5 votes vote down vote up
def _map_aws_exception(err):
    """Map AWS exception to our exceptions."""
    ex = AWS_EXCEPTIONS.get(err.response["Error"]["Code"], UnknownError)
    return ex(err.response["Error"]["Message"]) 
Example #28
Source File: workspaces_helper.py    From workspaces-cost-optimizer with Apache License 2.0 5 votes vote down vote up
def get_tags(self, workspaceID):
        try:
            workspaceTags = self.client.describe_tags(
                ResourceId = workspaceID
            )
            log.debug(workspaceTags)

            return workspaceTags['TagList']

        except botocore.exceptions.ClientError as e:
            log.error(e) 
Example #29
Source File: handlers.py    From faces with GNU General Public License v2.0 5 votes vote down vote up
def check_for_200_error(response, **kwargs):
    # From: http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectCOPY.html
    # There are two opportunities for a copy request to return an error. One
    # can occur when Amazon S3 receives the copy request and the other can
    # occur while Amazon S3 is copying the files. If the error occurs before
    # the copy operation starts, you receive a standard Amazon S3 error. If the
    # error occurs during the copy operation, the error response is embedded in
    # the 200 OK response. This means that a 200 OK response can contain either
    # a success or an error. Make sure to design your application to parse the
    # contents of the response and handle it appropriately.
    #
    # So this handler checks for this case.  Even though the server sends a
    # 200 response, conceptually this should be handled exactly like a
    # 500 response (with respect to raising exceptions, retries, etc.)
    # We're connected *before* all the other retry logic handlers, so as long
    # as we switch the error code to 500, we'll retry the error as expected.
    if response is None:
        # A None response can happen if an exception is raised while
        # trying to retrieve the response.  See Endpoint._get_response().
        return
    http_response, parsed = response
    if _looks_like_special_case_error(http_response):
        logger.debug("Error found for response with 200 status code, "
                     "errors: %s, changing status code to "
                     "500.", parsed)
        http_response.status_code = 500 
Example #30
Source File: workspaces_helper.py    From workspaces-cost-optimizer with Apache License 2.0 5 votes vote down vote up
def get_workspaces_page(self, directoryID, nextToken):
        try:
            if nextToken == 'None':
                result = self.client.describe_workspaces(
                    DirectoryId = directoryID
                )
            else:
                result = self.client.describe_workspaces(
                    DirectoryId = directoryID,
                    NextToken = nextToken
                )

            return result
        except botocore.exceptions.ClientError as e:
            log.error(e)