Python botocore.exceptions.NoCredentialsError() Examples

The following are 30 code examples of botocore.exceptions.NoCredentialsError(). 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.exceptions , or try the search function .
Example #1
Source File: s3.py    From recruit with Apache License 2.0 7 votes vote down vote up
def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
                           compression=None, mode=None):

    if mode is None:
        mode = 'rb'

    fs = s3fs.S3FileSystem(anon=False)
    try:
        filepath_or_buffer = fs.open(_strip_schema(filepath_or_buffer), mode)
    except (compat.FileNotFoundError, NoCredentialsError):
        # boto3 has troubles when trying to access a public file
        # when credentialed...
        # An OSError is raised if you have credentials, but they
        # aren't valid for that bucket.
        # A NoCredentialsError is raised if you don't have creds
        # for that bucket.
        fs = s3fs.S3FileSystem(anon=True)
        filepath_or_buffer = fs.open(_strip_schema(filepath_or_buffer), mode)
    return filepath_or_buffer, None, compression, True 
Example #2
Source File: yaml_spec.py    From hokusai with MIT License 6 votes vote down vote up
def to_string(self):
    template_config = {
      "project_name": config.project_name
    }

    try:
      template_config["project_repo"] = self.ecr.project_repo
    except NoCredentialsError:
      print_yellow("WARNING: Could not get template variable project_repo")

    if config.template_config_files:
      for template_config_file in config.template_config_files:
        try:
          config_loader = ConfigLoader(template_config_file)
          template_config.update(config_loader.load())
        except NoCredentialsError:
          print_yellow("WARNING: Could not get template config file %s" % template_config_file)

    return TemplateRenderer(self.template_file, template_config).render() 
Example #3
Source File: qi.py    From aws-support-tools with Apache License 2.0 6 votes vote down vote up
def main():
    option_list = 'region= type= role= key= volume= ami= bootstrap='.split()
    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:], '', option_list)
    except:
        usage()
        sys.exit(2)
    if len(args) > 0:
        if args[0] == 'configure':
            configure()
        elif args[0] in os_list:
            try:
                launch(opts, args[0])
            except NoCredentialsError:
                advise_credentials()
            except:
                troubleshoot()
        elif args[0] == 'help':
            usage()
        else:
            usage()
    else:
        usage() 
Example #4
Source File: _client_factory.py    From taskcat with Apache License 2.0 6 votes vote down vote up
def _get_account_info(self, profile):
        partition, region = self._get_partition(profile)
        session = self.session(profile, region)
        sts_client = session.client("sts", region_name=region)
        try:
            account_id = sts_client.get_caller_identity()["Account"]
        except ClientError as e:
            if e.response["Error"]["Code"] == "AccessDenied":
                raise TaskCatException(
                    f"Not able to fetch account number from {region} using profile "
                    f"{profile}. {str(e)}"
                )
            raise
        except NoCredentialsError as e:
            raise TaskCatException(
                f"Not able to fetch account number from {region} using profile "
                f"{profile}. {str(e)}"
            )
        except ProfileNotFound as e:
            raise TaskCatException(
                f"Not able to fetch account number from {region} using profile "
                f"{profile}. {str(e)}"
            )
        return {"partition": partition, "account_id": account_id} 
Example #5
Source File: file_reader.py    From modin with Apache License 2.0 6 votes vote down vote up
def file_exists(cls, file_path):
        if isinstance(file_path, str):
            match = S3_ADDRESS_REGEX.search(file_path)
            if match is not None:
                if file_path[0] == "S":
                    file_path = "{}{}".format("s", file_path[1:])
                import s3fs as S3FS
                from botocore.exceptions import NoCredentialsError

                s3fs = S3FS.S3FileSystem(anon=False)
                exists = False
                try:
                    exists = s3fs.exists(file_path) or exists
                except NoCredentialsError:
                    pass
                s3fs = S3FS.S3FileSystem(anon=True)
                return exists or s3fs.exists(file_path)
        return os.path.exists(file_path) 
Example #6
Source File: s3.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 6 votes vote down vote up
def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
                           compression=None, mode=None):

    if mode is None:
        mode = 'rb'

    fs = s3fs.S3FileSystem(anon=False)
    try:
        filepath_or_buffer = fs.open(_strip_schema(filepath_or_buffer), mode)
    except (compat.FileNotFoundError, NoCredentialsError):
        # boto3 has troubles when trying to access a public file
        # when credentialed...
        # An OSError is raised if you have credentials, but they
        # aren't valid for that bucket.
        # A NoCredentialsError is raised if you don't have creds
        # for that bucket.
        fs = s3fs.S3FileSystem(anon=True)
        filepath_or_buffer = fs.open(_strip_schema(filepath_or_buffer), mode)
    return filepath_or_buffer, None, compression, True 
Example #7
Source File: s3_broker.py    From scale with Apache License 2.0 6 votes vote down vote up
def validate_configuration(self, config):
        """See :meth:`storage.brokers.broker.Broker.validate_configuration`"""

        warnings = []
        if 'bucket_name' not in config or not config['bucket_name']:
            raise InvalidBrokerConfiguration('INVALID_BROKER', 'S3 broker requires "bucket_name" to be populated')
        region_name = config.get('region_name')

        credentials = AWSClient.instantiate_credentials_from_config(config)

        # Check whether the bucket can actually be accessed
        with S3Client(credentials, region_name) as client:
            try:
                client.get_bucket(config['bucket_name'])
            except (ClientError, NoCredentialsError):
                warnings.append(ValidationWarning('bucket_access',
                                                  'Unable to access bucket. Check the bucket name and credentials.'))

        return warnings 
Example #8
Source File: functions.py    From openstax-cms with GNU Affero General Public License v3.0 6 votes vote down vote up
def invalidate_cloudfront_caches():
    try:
        distribution = CloudfrontDistribution.objects.all()[0]
        client = boto3.client('cloudfront')
        response = client.create_invalidation(
            DistributionId=distribution.distribution_id,
            InvalidationBatch={
                'Paths': {
                    'Quantity': 1,
                    'Items': [
                        '/apps/cms/api/*' # invalidate the entire cache for the website
                    ],
                },
                'CallerReference': str(datetime.datetime.now().strftime('%m%d%Y%H%M'))
            }
        )
    except CloudfrontDistribution.DoesNotExist:
        return
    except IndexError:
        return
    except NoCredentialsError:
        print('No AWS credentials set - unable to invalidate cache')
    except ClientError as e:
            print("Unexpected AWS error: %s" % e) 
Example #9
Source File: states.py    From ssm-diff with MIT License 6 votes vote down vote up
def get(self, paths=['/'], flat=True):
        p = self.ssm.get_paginator('get_parameters_by_path')
        output = {}
        for path in paths:
            try:
                for page in p.paginate(
                    Path=path,
                    Recursive=True,
                    WithDecryption=True):
                    for param in page['Parameters']:
                        add(obj=output,
                            path=param['Name'],
                            value=self._read_param(param['Value'], param['Type']))
            except (ClientError, NoCredentialsError) as e:
                print("Failed to fetch parameters from SSM!", e, file=sys.stderr)

        return flatten(output) if flat else output 
Example #10
Source File: aws_client.py    From sentinelhub-py with MIT License 6 votes vote down vote up
def _do_download(request, s3_client):
        """ Does the download from s3
        """
        _, _, bucket_name, url_key = request.url.split('/', 3)

        try:
            response = s3_client.get_object(Bucket=bucket_name, Key=url_key, RequestPayer='requester')
            return response['Body'].read()
        except NoCredentialsError:
            raise ValueError(
                'The requested data is in Requester Pays AWS bucket. In order to download the data please set '
                'your access key either in AWS credentials file or in sentinelhub config.json file using '
                'command line:\n'
                '$ sentinelhub.config --aws_access_key_id <your AWS key> --aws_secret_access_key '
                '<your AWS secret key>')
        except s3_client.exceptions.NoSuchKey:
            raise AwsDownloadFailedException('File in location %s is missing' % request.url)
        except s3_client.exceptions.NoSuchBucket:
            raise ValueError('Aws bucket %s does not exist' % bucket_name) 
Example #11
Source File: s3.py    From vnpy_crypto with MIT License 6 votes vote down vote up
def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
                           compression=None, mode=None):

    if mode is None:
        mode = 'rb'

    fs = s3fs.S3FileSystem(anon=False)
    try:
        filepath_or_buffer = fs.open(_strip_schema(filepath_or_buffer), mode)
    except (compat.FileNotFoundError, NoCredentialsError):
        # boto3 has troubles when trying to access a public file
        # when credentialed...
        # An OSError is raised if you have credentials, but they
        # aren't valid for that bucket.
        # A NoCredentialsError is raised if you don't have creds
        # for that bucket.
        fs = s3fs.S3FileSystem(anon=True)
        filepath_or_buffer = fs.open(_strip_schema(filepath_or_buffer), mode)
    return filepath_or_buffer, None, compression, True 
Example #12
Source File: s3.py    From cli with MIT License 5 votes vote down vote up
def split_url(url: urllib.parse.ParseResult) -> Tuple[S3Bucket, str]:
    """
    Splits the given s3:// *url* into a Bucket object and normalized path
    with some sanity checking.
    """
    # Require a bucket name
    if not url.netloc:
        raise UserError("No bucket name specified in url (%s)" % url.geturl())

    # Remove leading slashes from any destination path in order to use it as a
    # prefix for uploaded files.  Internal and trailing slashes are untouched.
    prefix = url.path.lstrip("/")

    try:
        bucket = boto3.resource("s3").Bucket(url.netloc)

    except (NoCredentialsError, PartialCredentialsError) as error:
        raise UserError("Unable to authenticate with S3: %s" % error) from error

    # Find the bucket and ensure we have access and that it already exists so
    # we don't automagically create new buckets.
    try:
        boto3.client("s3").head_bucket(Bucket = bucket.name)

    except ClientError as error:
        raise UserError(dedent('''\
            No bucket exists with the name "%s".

            Buckets are not automatically created for safety reasons.
            ''' % bucket.name))

    return bucket, prefix 
Example #13
Source File: plugin.py    From diffy with Apache License 2.0 5 votes vote down vote up
def get_default_aws_account_number() -> dict:
    """Retrieves current account number"""
    sts = boto3.client('sts')
    accountId = '1234'
    try:
        accountId = sts.get_caller_identity()['Account']
    except (ClientError, NoCredentialsError) as e:
        logger.debug(f'Failed to get AWS AccountID, using Prod: {e}')
    return accountId 
Example #14
Source File: engines.py    From aq with MIT License 5 votes vote down vote up
def load_tables(self, query, meta):
        """
        Load necessary resources tables into db to execute given query.
        """
        try:
            for table in meta.tables:
                self.load_table(table)
        except NoCredentialsError:
            help_link = 'http://boto3.readthedocs.io/en/latest/guide/configuration.html'
            raise QueryError('Unable to locate AWS credential. '
                             'Please see {0} on how to configure AWS credential.'.format(help_link)) 
Example #15
Source File: amazon.py    From harpoon with MIT License 5 votes vote down vote up
def catch_no_credentials(message, **info):
    """Turn a NoCredentialsError into a BadAmazon"""
    try:
        yield
    except NoCredentialsError as error:
        if hasattr(error, "response"):
            info["error_code"] = error.response["ResponseMetadata"]["HTTPStatusCode"]
            info["error_message"] = error.response["Error"]["Message"]
        else:
            info["error_message"] = error.fmt

        raise BadAmazon(message, **info) 
Example #16
Source File: s3.py    From elasticintel with GNU General Public License v3.0 5 votes vote down vote up
def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
                           compression=None):
    fs = s3fs.S3FileSystem(anon=False)
    try:
        filepath_or_buffer = fs.open(_strip_schema(filepath_or_buffer))
    except (OSError, NoCredentialsError):
        # boto3 has troubles when trying to access a public file
        # when credentialed...
        # An OSError is raised if you have credentials, but they
        # aren't valid for that bucket.
        # A NoCredentialsError is raised if you don't have creds
        # for that bucket.
        fs = s3fs.S3FileSystem(anon=True)
        filepath_or_buffer = fs.open(_strip_schema(filepath_or_buffer))
    return filepath_or_buffer, None, compression 
Example #17
Source File: sqs_sensor.py    From stackstorm-aws with Apache License 2.0 5 votes vote down vote up
def _get_queue(self, queue, account_id, region):
        ''' Fetch QUEUE by its name or URL and create new one if queue doesn't exist '''
        if account_id not in self.sessions:
            self._logger.error('Session for account id %s does not exist', account_id)
            return None

        sqs_res = self._setup_sqs(self.sessions[account_id], account_id, region)
        if sqs_res is None:
            return None

        try:
            if queue.netloc:
                return sqs_res.Queue(six.moves.urllib.parse.urlunparse(queue))
            return sqs_res.get_queue_by_name(QueueName=queue.path.split('/')[-1])
        except ClientError as e:
            if e.response['Error']['Code'] == 'AWS.SimpleQueueService.NonExistentQueue':
                self._logger.warning("SQS Queue: %s doesn't exist, creating it.", queue)
                return sqs_res.create_queue(QueueName=queue.path.split('/')[-1])
            elif e.response['Error']['Code'] == 'InvalidClientTokenId':
                self._logger.warning("Couldn't operate sqs because of invalid credential config")
            else:
                raise
        except NoCredentialsError:
            self._logger.warning("Couldn't operate sqs because of invalid credential config")
        except EndpointConnectionError as e:
            self._logger.warning(e) 
Example #18
Source File: test_sensor_sqs.py    From stackstorm-aws with Apache License 2.0 5 votes vote down vote up
def Queue(self, queue):
            raise NoCredentialsError() 
Example #19
Source File: test_sensor_sqs.py    From stackstorm-aws with Apache License 2.0 5 votes vote down vote up
def get_queue_by_name(self, **kwargs):
            raise NoCredentialsError() 
Example #20
Source File: provider.py    From koku with GNU Affero General Public License v3.0 5 votes vote down vote up
def _get_sts_access(provider_resource_name):
    """Get for sts access."""
    # create an STS client
    sts_client = boto3.client("sts")

    credentials = dict()
    error_message = f"Unable to assume role with ARN {provider_resource_name}."
    try:
        # Call the assume_role method of the STSConnection object and pass the role
        # ARN and a role session name.
        assumed_role = sts_client.assume_role(RoleArn=provider_resource_name, RoleSessionName="AccountCreationSession")
        credentials = assumed_role.get("Credentials")
    except ParamValidationError as param_error:
        LOG.warn(msg=error_message)
        LOG.info(param_error)
        # We can't use the exc_info here because it will print
        # a traceback that gets picked up by sentry:
        # https://github.com/project-koku/koku/issues/1483
    except (ClientError, BotoConnectionError, NoCredentialsError) as boto_error:
        LOG.warn(msg=error_message, exc_info=boto_error)

    # return a kwargs-friendly format
    return dict(
        aws_access_key_id=credentials.get("AccessKeyId"),
        aws_secret_access_key=credentials.get("SecretAccessKey"),
        aws_session_token=credentials.get("SessionToken"),
    ) 
Example #21
Source File: s3.py    From Splunking-Crime with GNU Affero General Public License v3.0 5 votes vote down vote up
def get_filepath_or_buffer(filepath_or_buffer, encoding=None,
                           compression=None):
    fs = s3fs.S3FileSystem(anon=False)
    try:
        filepath_or_buffer = fs.open(_strip_schema(filepath_or_buffer))
    except (OSError, NoCredentialsError):
        # boto3 has troubles when trying to access a public file
        # when credentialed...
        # An OSError is raised if you have credentials, but they
        # aren't valid for that bucket.
        # A NoCredentialsError is raised if you don't have creds
        # for that bucket.
        fs = s3fs.S3FileSystem(anon=True)
        filepath_or_buffer = fs.open(_strip_schema(filepath_or_buffer))
    return filepath_or_buffer, None, compression 
Example #22
Source File: __init__.py    From jupyterlab-s3-browser with Apache License 2.0 5 votes vote down vote up
def has_aws_s3_role_access():
    """
    Returns true if the user has access to an S3 bucket
    """
    try:
        _test_aws_s3_role_access()
        return True
    except NoCredentialsError:
        return False 
Example #23
Source File: test_deploy.py    From formica with MIT License 5 votes vote down vote up
def test_catches_common_aws_exceptions(session, stack_waiter):
    session.side_effect = NoCredentialsError()
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        cli.main(['deploy', '--stack', STACK])
    assert pytest_wrapped_e.value.code == 1 
Example #24
Source File: test_cli.py    From formica with MIT License 5 votes vote down vote up
def test_commands_use_exception_handling(session, logger):
    session.side_effect = NoCredentialsError()
    for method in METHODS:
        with pytest.raises(SystemExit) as pytest_wrapped_e:
            cli.main([method, '--stack', STACK])
        assert pytest_wrapped_e.value.code == 1

    for method in NO_STACK_METHODS:
        with pytest.raises(SystemExit) as pytest_wrapped_e:
            cli.main([method])
        assert pytest_wrapped_e.value.code == 1 
Example #25
Source File: emrutils.py    From bash-lambda-layer with MIT License 5 votes vote down vote up
def call(session, operation_name, parameters, region_name=None,
         endpoint_url=None, verify=None):
    # We could get an error from get_endpoint() about not having
    # a region configured.  Before this happens we want to check
    # for credentials so we can give a good error message.
    if session.get_credentials() is None:
        raise NoCredentialsError()

    client = session.create_client(
        'emr', region_name=region_name, endpoint_url=endpoint_url,
        verify=verify)
    LOG.debug('Calling ' + str(operation_name))
    return getattr(client, operation_name)(**parameters) 
Example #26
Source File: s3utils.py    From S3Scanner with MIT License 5 votes vote down vote up
def checkAwsCreds():
    """
    Checks to see if the user has credentials for AWS properly configured.
    This is essentially a requirement for getting accurate results.

    :return: True if AWS credentials are properly configured. False if not.
    """

    sts = boto3.client('sts')
    try:
        response = sts.get_caller_identity()
    except NoCredentialsError as e:
            return False

    return True 
Example #27
Source File: test_s3.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_check_for_key_raises_error_with_invalid_conn_id(self, monkeypatch, s3_bucket):
        monkeypatch.delenv('AWS_PROFILE', raising=False)
        monkeypatch.delenv('AWS_ACCESS_KEY_ID', raising=False)
        monkeypatch.delenv('AWS_SECRET_ACCESS_KEY', raising=False)
        hook = S3Hook(aws_conn_id="does_not_exist")
        with pytest.raises(NoCredentialsError):
            hook.check_for_key('a', s3_bucket) 
Example #28
Source File: test_s3.py    From airflow with Apache License 2.0 5 votes vote down vote up
def test_check_for_bucket_raises_error_with_invalid_conn_id(self, s3_bucket, monkeypatch):
        monkeypatch.delenv('AWS_PROFILE', raising=False)
        monkeypatch.delenv('AWS_ACCESS_KEY_ID', raising=False)
        monkeypatch.delenv('AWS_SECRET_ACCESS_KEY', raising=False)
        hook = S3Hook(aws_conn_id="does_not_exist")
        with pytest.raises(NoCredentialsError):
            hook.check_for_bucket(s3_bucket) 
Example #29
Source File: helpers.py    From streamalert with Apache License 2.0 5 votes vote down vote up
def check_credentials():
    """Check for valid AWS credentials in environment variables

    Returns:
        bool: True any of the AWS env variables exist
    """
    try:
        response = boto3.client('sts').get_caller_identity()
    except NoCredentialsError:
        LOGGER.error('No valid AWS Credentials found in your environment!')
        LOGGER.error('Please follow the setup instructions here: '
                     'https://www.streamalert.io/getting-started.html'
                     '#configure-aws-credentials')
        return False
    except ClientError as err:
        # Check for an error related to an expired token
        if err.response['Error']['Code'] == 'ExpiredToken':
            LOGGER.error('%s. Please refresh your credentials.', err.response['Error']['Message'])
            return False
        raise  # Raise the error if it is anything else

    LOGGER.debug(
        'Using credentials for user \'%s\' with user ID \'%s\' in account '
        '\'%s\'', response['Arn'], response['UserId'], response['Account']
    )

    return True 
Example #30
Source File: mturk.py    From Dallinger with MIT License 5 votes vote down vote up
def check_credentials(self):
        """Verifies key/secret/host combination by making a balance inquiry"""
        try:
            return bool(self.mturk.get_account_balance())
        except NoCredentialsError:
            raise MTurkServiceException("No AWS credentials set!")
        except ClientError:
            raise MTurkServiceException("Invalid AWS credentials!")
        except Exception as ex:
            raise MTurkServiceException(
                "Error checking credentials: {}".format(str(ex))
            )