@Test public void credentialsProvider_noExplicitCredentialsProviderConfigured_configuresDefaultAwsCredentialsProviderChainWithInstanceProfile() throws Exception { // Arrange this.context = new AnnotationConfigApplicationContext(); this.context.register(ContextCredentialsAutoConfiguration.class); // Act this.context.refresh(); // Assert AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AmazonWebserviceClientConfigurationUtils.CREDENTIALS_PROVIDER_BEAN_NAME, AWSCredentialsProvider.class); assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(2, credentialsProviders.size()); assertTrue(EC2ContainerCredentialsProviderWrapper.class.isInstance(credentialsProviders.get(0))); assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(1))); }
@Test public void credentialsProvider_instanceProfileConfigured_configuresInstanceProfileCredentialsProvider() { this.context = new AnnotationConfigApplicationContext(); this.context.register(ContextCredentialsAutoConfiguration.class); TestPropertyValues.of( "cloud.aws.credentials.instanceProfile").applyTo(this.context); this.context.refresh(); AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AmazonWebserviceClientConfigurationUtils.CREDENTIALS_PROVIDER_BEAN_NAME, AWSCredentialsProvider.class); assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(2, credentialsProviders.size()); assertTrue(EC2ContainerCredentialsProviderWrapper.class.isInstance(credentialsProviders.get(0))); assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(1))); }
@Test public void credentialsProvider_profileNameConfigured_configuresProfileCredentialsProvider() { this.context = new AnnotationConfigApplicationContext(); this.context.register(ContextCredentialsAutoConfiguration.class); TestPropertyValues.of( "cloud.aws.credentials.profileName:test").applyTo(this.context); this.context.refresh(); AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AmazonWebserviceClientConfigurationUtils.CREDENTIALS_PROVIDER_BEAN_NAME, AWSCredentialsProvider.class); assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(2, credentialsProviders.size()); assertTrue(EC2ContainerCredentialsProviderWrapper.class.isInstance(credentialsProviders.get(0))); assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(1))); assertEquals("test", ReflectionTestUtils.getField(credentialsProviders.get(1), "profileName")); }
@Test public void credentialsProvider_profileNameAndPathConfigured_configuresProfileCredentialsProvider() throws IOException { this.context = new AnnotationConfigApplicationContext(); this.context.register(ContextCredentialsAutoConfiguration.class); TestPropertyValues.of( "cloud.aws.credentials.profileName:customProfile", "cloud.aws.credentials.profilePath:" + new ClassPathResource(getClass().getSimpleName() + "-profile", getClass()).getFile().getAbsolutePath()).applyTo(this.context); this.context.refresh(); AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AmazonWebserviceClientConfigurationUtils.CREDENTIALS_PROVIDER_BEAN_NAME, AWSCredentialsProvider.class); assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(2, credentialsProviders.size()); assertTrue(EC2ContainerCredentialsProviderWrapper.class.isInstance(credentialsProviders.get(0))); assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(1))); ProfileCredentialsProvider provider = (ProfileCredentialsProvider) credentialsProviders.get(1); assertEquals("testAccessKey", provider.getCredentials().getAWSAccessKeyId()); assertEquals("testSecretKey", provider.getCredentials().getAWSSecretKey()); }
@Test public void credentialsProvider_configWithAccessAndSecretKeyAndInstanceProfile_staticAwsCredentialsProviderConfiguredWithInstanceProfile() throws Exception { //Arrange this.context = new AnnotationConfigApplicationContext(ApplicationConfigurationWithAccessKeyAndSecretKeyAndInstanceProfile.class); //Act AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AWSCredentialsProvider.class); //Assert assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(2, credentialsProviders.size()); assertTrue(AWSStaticCredentialsProvider.class.isInstance(credentialsProviders.get(0))); assertTrue(EC2ContainerCredentialsProviderWrapper.class.isInstance(credentialsProviders.get(1))); }
@Test public void credentialsProvider_configWithInstanceProfile_instanceProfileCredentialsProviderConfigured() throws Exception { //Arrange this.context = new AnnotationConfigApplicationContext(ApplicationConfigurationWithInstanceProfileOnly.class); //Act AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AWSCredentialsProvider.class); //Assert assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(1, credentialsProviders.size()); assertTrue(EC2ContainerCredentialsProviderWrapper.class.isInstance(credentialsProviders.get(0))); }
@Test public void credentialsProvider_configWithAllProviders_allCredentialsProvidersConfigured() throws Exception { //Arrange this.context = new AnnotationConfigApplicationContext(ApplicationConfigurationWithAllProviders.class); //Act AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AWSCredentialsProvider.class); //Assert assertNotNull(awsCredentialsProvider); @SuppressWarnings("unchecked") List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils.getField(awsCredentialsProvider, "credentialsProviders"); assertEquals(3, credentialsProviders.size()); assertTrue(AWSStaticCredentialsProvider.class.isInstance(credentialsProviders.get(0))); assertTrue(EC2ContainerCredentialsProviderWrapper.class.isInstance(credentialsProviders.get(1))); assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(2))); }
public static void registerCredentialsProvider(BeanDefinitionRegistry registry, String accessKey, String secretKey, boolean instanceProfile, String profileName, String profilePath) { BeanDefinitionBuilder factoryBeanBuilder = BeanDefinitionBuilder.genericBeanDefinition(CredentialsProviderFactoryBean.class); ManagedList<BeanDefinition> awsCredentialsProviders = new ManagedList<>(); if (StringUtils.hasText(accessKey)) { BeanDefinitionBuilder credentials = BeanDefinitionBuilder.rootBeanDefinition(BasicAWSCredentials.class); credentials.addConstructorArgValue(accessKey); credentials.addConstructorArgValue(secretKey); BeanDefinitionBuilder provider = BeanDefinitionBuilder.rootBeanDefinition(AWSStaticCredentialsProvider.class); provider.addConstructorArgValue(credentials.getBeanDefinition()); awsCredentialsProviders.add(provider.getBeanDefinition()); } if (instanceProfile) { awsCredentialsProviders.add(BeanDefinitionBuilder.rootBeanDefinition(EC2ContainerCredentialsProviderWrapper.class).getBeanDefinition()); } if (StringUtils.hasText(profileName)) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ProfileCredentialsProvider.class); if (StringUtils.hasText(profilePath)) { builder.addConstructorArgValue(profilePath); } builder.addConstructorArgValue(profileName); awsCredentialsProviders.add(builder.getBeanDefinition()); } factoryBeanBuilder.addConstructorArgValue(awsCredentialsProviders); registry.registerBeanDefinition(CredentialsProviderFactoryBean.CREDENTIALS_PROVIDER_BEAN_NAME, factoryBeanBuilder.getBeanDefinition()); AmazonWebserviceClientConfigurationUtils.replaceDefaultCredentialsProvider(registry, CredentialsProviderFactoryBean.CREDENTIALS_PROVIDER_BEAN_NAME); }
public CustomCredentialsProviderChain(ClientConfiguration clientConfiguration, ProfileIdentifier profile, Supplier<MFAToken> mfaTokenSupplier) { super(new EnvironmentVariableCredentialsProvider(), new SystemPropertiesCredentialsProvider(), new ProfileCredentialProvider(clientConfiguration, profile, mfaTokenSupplier), new EC2ContainerCredentialsProviderWrapper()); }
private static AmazonS3ClientBuilder configureEC2Authentication(AmazonS3ClientBuilder builder) { LOGGER.info("Using EC2 authentication"); return builder.withCredentials(new EC2ContainerCredentialsProviderWrapper()); }