def get_s3_resource(): s3_profile = get_config("s3_profile") if s3_profile is None: if not has_default_credentials(): return boto3.resource( "s3", config=botocore.client.Config(signature_version=botocore.UNSIGNED) ) else: return boto3.resource("s3") try: session = boto3.session.Session(profile_name=s3_profile) except ProfileNotFound: logger.error( "awscliの設定が正しくなされていません。管理者にアクセストークンを発行してもらったのち、" f"aws configure --profile {s3_profile} でprofileを登録してください\n" "https://qiita.com/itooww/items/bdc2dc15213da43a10a7" ) raise ProfileNotFound(profile=s3_profile) if session.get_credentials() is None: # Use unsigned requests. s3_resource = session.resource( "s3", config=botocore.client.Config(signature_version=botocore.UNSIGNED) ) else: s3_resource = session.resource("s3") return s3_resource
def test_get_botocore_client_use_awsprofile_profile_not_found(mocker, capsys): config = get_config() get_target_region_mock = mocker.patch('mount_efs.get_target_region', return_value=DEFAULT_REGION) mount_efs.BOTOCORE_PRESENT = True boto_session_mock = MagicMock() boto_session_mock.set_config_variable.return_value = None boto_session_mock.create_client.side_effect = [ ProfileNotFound(profile='test_profile') ] mocker.patch('botocore.session.get_session', return_value=boto_session_mock) with pytest.raises(SystemExit) as ex: mount_efs.get_botocore_client(config, 'efs', {'awsprofile': 'test-profile'}) assert 0 != ex.value.code out, err = capsys.readouterr() assert 'could not be found' in err boto_session_mock.set_config_variable.assert_called_once() utils.assert_called(get_target_region_mock)
def test_choose_timestamp_parser_profile_not_found(self): session = Mock(spec=Session) session.get_scoped_config.side_effect = ProfileNotFound(profile='foo') factory = session.get_component.return_value scalarparse.add_scalar_parsers(session) factory.set_parser_defaults.assert_called_with( timestamp_parser=scalarparse.identity)
def test_get_assumed_profile_credentials_via_botocore_botocore_present_profile_not_found( mocker, capsys): mount_efs.BOTOCORE_PRESENT = True boto_session_mock = MagicMock() boto_session_mock.set_config_variable.return_value = None boto_session_mock.get_credentials.side_effect = [ ProfileNotFound(profile='test_profile') ] mocker.patch('botocore.session.get_session', return_value=boto_session_mock) with pytest.raises(SystemExit) as ex: mount_efs.botocore_credentials_helper('test_profile') assert 0 != ex.value.code out, err = capsys.readouterr() assert 'could not be found' in err boto_session_mock.set_config_variable.assert_called_once() boto_session_mock.get_credentials.assert_called_once()
def get_config(self): """ Returns the configuration associated with this session. If the configuration has not yet been loaded, it will be loaded using the default ``profile`` session variable. If it has already been loaded, the cached configuration will be returned. Note that this configuration is specific to a single profile (the ``profile`` session variable). If the ``profile`` session variable is set and the profile does not exist in the config file, a ``ProfileNotFound`` exception will be raised. :raises: ConfigNotFound, ConfigParseError, ProfileNotFound :rtype: dict """ config = self.full_config profile_name = self.get_variable('profile') profile_map = self._build_profile_map() # If a profile is not explicitly set return the default # profile config or an empty config dict if we don't have # a default profile. if profile_name is None: return profile_map.get('default', {}) elif profile_name not in profile_map: # Otherwise if they specified a profile, it has to # exist (even if it's the default profile) otherwise # we complain. raise ProfileNotFound(profile=profile_name) else: return profile_map[profile_name]
def test_profile_not_found_is_not_propagated(self): self.session.get_component.side_effect = ProfileNotFound( profile='unknown') try: inject_json_file_cache(self.session, event_name='session-initialized') except ProfileNotFound: self.fail('ProfileNotFound should not have been raised.')
def test_session_no_profile(self, mock_boto3, mock_cache_lookup, mock_cache_set): mock_cache_lookup.side_effect = ProfileNotFound( profile="non-existent-profile") Boto3Cache().session() # default value should be "default" profile self.assertEqual(mock_boto3.called, True) self.assertEqual(mock_cache_lookup.called, True) self.assertEqual(mock_cache_set.called, True)
def test_session_no_profile(self, mock_boto3, mock_cache_lookup, mock_cache_set, mock_get_partition): mock_get_partition.return_value = (None, "us-east-1") mock_cache_lookup.side_effect = ProfileNotFound( profile="non-existent-profile") Boto3Cache().session() # default value should be "default" profile self.assertEqual(mock_boto3.called, True) self.assertEqual(mock_cache_lookup.called, True) self.assertEqual(mock_cache_set.called, True)
def test_no_registration_if_profile_does_not_exist(self): session = mock.Mock() session.get_component.side_effect = ProfileNotFound(profile='unknown') assumerole.inject_assume_role_provider_cache( session, event_name='building-command-table.foo') credential_provider = session.get_component.return_value self.assertFalse(credential_provider.get_provider.called)
def test_inject_mfa_credential_provider__no_profile(): session = mock.Mock() session.get_component.side_effect = ProfileNotFound(profile='unknown') awscli_plugin.inject_mfa_credential_provider( session, event_name='session-initialized') credential_provider = session.get_component.return_value assert not credential_provider.insert_after.called
def get_config_variable(self, name, methods=None): if self.profile_does_not_exist and not name == 'config_file': raise ProfileNotFound(profile='foo') if methods is not None: if 'env' in methods: return self.environment_vars.get(name) elif 'config' in methods: return self.config_file_vars.get(name) else: return self.variables.get(name)
def test_profile_not_found(self, mock_handler_cls): session = mock.Mock(spec=Session) session.get_scoped_config.side_effect = ProfileNotFound(profile='foo') register_uri_param_handler(session) cases = mock_handler_cls.call_args[0][0] self.assertIn('file://', cases) self.assertIn('fileb://', cases) self.assertIn('http://', cases) self.assertIn('http://', cases)
def test_session_invalid_profile(self, mock_cache_lookup, mock_cache_set): mock_cache_lookup.side_effect = ProfileNotFound( profile="non-existent-profile") cache = Boto3Cache() with self.assertRaises(ProfileNotFound): cache.session(profile="non-existent-profile") self.assertEqual(mock_cache_lookup.called, False) cache._get_region = mock.Mock(return_value="us-east-1") with self.assertRaises(ProfileNotFound): cache.session(profile="non-existent-profile") self.assertEqual(mock_cache_lookup.called, True)
def manage_stack( region: str, stack_name: str, template_body: str, profile: Optional[str] = None, parameter_overrides: Optional[Dict[str, Union[str, List[str]]]] = None, ) -> List[Dict[str, str]]: """ get or create a CloudFormation stack Parameters ---------- region: str AWS region for the CloudFormation stack stack_name: str CloudFormation stack name template_body: str CloudFormation template's content profile: Optional[str] AWS named profile for the AWS account parameter_overrides: Optional[Dict[str, Union[str, List[str]]]] Values of template parameters, if any. Returns: Stack output section(list of OutputKey, OutputValue pairs) """ try: if profile: session = boto3.Session( profile_name=profile, region_name=region if region else None) # type: ignore cloudformation_client = session.client("cloudformation") else: cloudformation_client = boto3.client( "cloudformation", config=Config(region_name=region if region else None)) except ProfileNotFound as ex: raise ProfileNotFound( f"Error Setting Up Managed Stack Client: the provided AWS name profile '{profile}' is not found. " "please check the documentation for setting up a named profile: " "https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html" ) from ex except NoCredentialsError as ex: raise NoCredentialsError( "Error Setting Up Managed Stack Client: Unable to resolve credentials for the AWS SDK for Python client. " "Please see their documentation for options to pass in credentials: " "https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html" ) from ex except NoRegionError as ex: raise NoRegionError( "Error Setting Up Managed Stack Client: Unable to resolve a region. " "Please provide a region via the --region parameter or by the AWS_REGION environment variable." ) from ex return _create_or_get_stack(cloudformation_client, stack_name, template_body, parameter_overrides)
def test_profile_does_not_exist(self, mock_recorder, mock_db_sqlite3, mock_sqlite3): mock_session = mock.Mock(Session) mock_session.get_scoped_config.side_effect = ProfileNotFound( profile='no-exist') parsed_args = argparse.Namespace() parsed_args.command = 'configure' attach_history_handler(session=mock_session, parsed_args=parsed_args) self.assertFalse(mock_recorder.add_handler.called) self.assertFalse(mock_db_sqlite3.connect.called)
def test__get_account_info(self, mock_session, mock__get_region, mock__get_partition, mock_boto3): mock__get_region.return_value = "us-east-1" session = boto3.Session() session.get_available_regions = mock.Mock() session.client = mock.Mock() sts = mock.Mock() sts.get_caller_identity.return_value = {"Account": "123412341234"} session.client.return_value = sts mock_session.return_value = session mock_boto3.session.Session = mock.Mock() mock_boto3.session.Session.return_value = session cache = Boto3Cache(_boto3=mock_boto3) mock__get_partition.return_value = ("aws-us-gov", "us-gov-east-1") partition = cache._get_account_info("default")["partition"] self.assertEqual(partition, "aws-us-gov") mock__get_partition.return_value = ("aws-cn", "cn-north-1") partition = cache._get_account_info("default")["partition"] self.assertEqual(partition, "aws-cn") mock__get_partition.return_value = ("aws", "us-east-1") partition = cache._get_account_info("default")["partition"] self.assertEqual(partition, "aws") self.assertEqual(3, sts.get_caller_identity.call_count) sts.get_caller_identity.side_effect = ClientError( error_response={"Error": { "Code": "test" }}, operation_name="test") with self.assertRaises(ClientError): cache._get_account_info("default") sts.get_caller_identity.side_effect = ClientError( error_response={"Error": { "Code": "AccessDenied" }}, operation_name="test") with self.assertRaises(TaskCatException): cache._get_account_info("default") sts.get_caller_identity.side_effect = NoCredentialsError() with self.assertRaises(TaskCatException): cache._get_account_info("default") sts.get_caller_identity.side_effect = ProfileNotFound( profile="non-existent_profile") with self.assertRaises(TaskCatException): cache._get_account_info("default")
def get_config_variable(self, name, methods=None): if name == 'credentials_file': # The credentials_file var doesn't require a # profile to exist. return '~/fake_credentials_filename' if self.profile_does_not_exist and not name == 'config_file': raise ProfileNotFound(profile='foo') if methods is not None: if 'env' in methods: return self.environment_vars.get(name) elif 'config' in methods: return self.config_file_vars.get(name) else: return self.variables.get(name)
def test_get_assumed_profile_credentials_via_botocore_botocore_present_profile_not_found(mocker): expected_credentials = {'AccessKeyId': None, 'SecretAccessKey': None, 'Token': None} boto_session_mock = MagicMock() boto_session_mock.set_config_variable.return_value = None boto_session_mock.get_credentials.side_effect = [ProfileNotFound(profile='test_profile')] mocker.patch('botocore.session.get_session', return_value=boto_session_mock) credentials = watchdog.botocore_credentials_helper('test_profile') assert credentials == expected_credentials boto_session_mock.set_config_variable.assert_called_once() boto_session_mock.get_credentials.assert_called_once()
def test_handles_non_existant_profile(self): not_found_exception = ProfileNotFound(profile=self.profile) self.mock_session.get_scoped_config.side_effect = not_found_exception self.configure_sso = ConfigureSSOCommand( self.mock_session, prompter=self.prompter, selector=self.selector, config_writer=self.writer, sso_token_cache=self.token_cache, ) self._add_prompt_responses() self._add_simple_single_item_responses() with self.sso_stub: self.configure_sso(args=[], parsed_globals=self.global_args) self.sso_stub.assert_no_pending_responses() self.assert_config_updates()
def get_config(self): """ Returns the configuration associated with this session. If the configuration has not yet been loaded, it will be loaded using the default ``profile`` session variable. If it has already been loaded, the cached configuration will be returned. The configuration data is loaded **only** from the config file. It does not resolve variables based on different locations (e.g. first from the session instance, then from environment variables, then from the config file). If you want this lookup behavior, use the ``get_config_variable`` method instead. Note that this configuration is specific to a single profile (the ``profile`` session variable). If the ``profile`` session variable is set and the profile does not exist in the config file, a ``ProfileNotFound`` exception will be raised. :raises: ConfigNotFound, ConfigParseError, ProfileNotFound :rtype: dict """ profile_name = self.get_config_variable('profile') profile_map = self._build_profile_map() # If a profile is not explicitly set return the default # profile config or an empty config dict if we don't have # a default profile. if profile_name is None: return profile_map.get('default', {}) elif profile_name not in profile_map: # Otherwise if they specified a profile, it has to # exist (even if it's the default profile) otherwise # we complain. raise ProfileNotFound(profile=profile_name) else: return profile_map[profile_name]
def test_get_assumed_profile_credentials_via_botocore_botocore_present_profile_not_found( mocker, ): expected_credentials = { "AccessKeyId": None, "SecretAccessKey": None, "Token": None } boto_session_mock = MagicMock() boto_session_mock.set_config_variable.return_value = None boto_session_mock.get_credentials.side_effect = [ ProfileNotFound(profile="test_profile") ] mocker.patch("botocore.session.get_session", return_value=boto_session_mock) credentials = watchdog.botocore_credentials_helper("test_profile") assert credentials == expected_credentials boto_session_mock.set_config_variable.assert_called_once() boto_session_mock.get_credentials.assert_called_once()
def get_scoped_config(self): if self.profile_does_not_exist: raise ProfileNotFound(profile='foo') return self.config
feature_group_name="MyGroup", sagemaker_fs_runtime_client_config=fs_runtime_client_config_mock, max_workers=1, ) with pytest.raises(IngestionError) as error: manager.run(df) assert "Failed to ingest some data into FeatureGroup MyGroup" in str(error) assert error.value.failed_rows == [1] assert manager.failed_rows == [1] @patch( "sagemaker.feature_store.feature_group.IngestionManagerPandas._ingest_single_batch", MagicMock(side_effect=ProfileNotFound(profile="non_exist")), ) def test_ingestion_manager_with_profile_name_run_failure(): df = pd.DataFrame({"float": pd.Series([2.0], dtype="float64")}) manager = IngestionManagerPandas( feature_group_name="MyGroup", sagemaker_fs_runtime_client_config=fs_runtime_client_config_mock, max_workers=1, profile_name="non_exist", ) try: manager.run(df) except Exception as e: assert "The config profile (non_exist) could not be found" in str(e)
def test_session_missing_profile(self, boto_mock): boto_mock.side_effect = ProfileNotFound(profile="test-profile") with self.assertRaises(CredentialsError): manage_stack("test-profile", "fake-region", SAM_CLI_STACK_NAME, _get_stack_template())
def test_defaults_to_base64_if_unkonwn_profile(self): self.parsed_args.cli_binary_format = None exception = ProfileNotFound(profile='notaprofile') self.mock_session.get_config_variable.side_effect = exception add_binary_formatter(self.mock_session, self.parsed_args) self._assert_base64_handlers_added()
def get_variable(self, name, methods=None): if self.profile_does_not_exist and not name == 'config_file': raise ProfileNotFound(profile='foo') return self.variables.get(name)