ProfileCredentialsProvider implements AWSCredentialsProvider { @Override public AWSCredentials getCredentials() { if (profilesConfigFile == null) { synchronized (this) { if (profilesConfigFile == null) { profilesConfigFile = new ProfilesConfigFile(); lastRefreshed = System.nanoTime(); } } } long now = System.nanoTime(); long age = now - lastRefreshed; if (age > refreshForceIntervalNanos) { refresh(); } else if (age > refreshIntervalNanos) { if (refreshSemaphore.tryAcquire()) { try { refresh(); } finally { refreshSemaphore.release(); } } } return profilesConfigFile.getCredentials(profileName); } ProfileCredentialsProvider();  ProfileCredentialsProvider(String profileName);  ProfileCredentialsProvider(String profilesConfigFilePath, String profileName);  ProfileCredentialsProvider(ProfilesConfigFile profilesConfigFile, String profileName); @Override AWSCredentials getCredentials(); @Override void refresh(); long getRefreshIntervalNanos(); void setRefreshIntervalNanos(long refreshIntervalNanos); long getRefreshForceIntervalNanos(); void setRefreshForceIntervalNanos(long refreshForceIntervalNanos);  }
@Test public void testDefault() { ProfileCredentialsProvider provider = newProvider(); AWSCredentials credentials = provider.getCredentials(); Assert.assertEquals("defaultSecretAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("defaultAccessKey", credentials.getAWSSecretKey()); }
@Test public void testEnvironmentVariable() throws Exception { Map<String, String> immutableEnv = System.getenv(); Class<?> unMap = Class.forName("java.util.Collections$UnmodifiableMap"); Field m = unMap.getDeclaredField("m"); m.setAccessible(true); @SuppressWarnings("unchecked") Map<String, String> env = (Map<String, String>) m.get(immutableEnv); try { env.put(AwsProfileNameLoader.AWS_PROFILE_ENVIRONMENT_VARIABLE, "test"); ProfileCredentialsProvider provider = newProvider(); AWSCredentials credentials = provider.getCredentials(); Assert.assertEquals("test", credentials.getAWSAccessKeyId()); Assert.assertEquals("test key", credentials.getAWSSecretKey()); } finally { env.remove(AwsProfileNameLoader.AWS_PROFILE_ENVIRONMENT_VARIABLE); } }
@Test public void testSystemProperty() { try { System.setProperty(AwsProfileNameLoader.AWS_PROFILE_SYSTEM_PROPERTY, "test"); ProfileCredentialsProvider provider = newProvider(); AWSCredentials credentials = provider.getCredentials(); Assert.assertEquals("test", credentials.getAWSAccessKeyId()); Assert.assertEquals("test key", credentials.getAWSSecretKey()); } finally { System.setProperty(AwsProfileNameLoader.AWS_PROFILE_SYSTEM_PROPERTY, ""); } }
@Test public void testBoth() throws Exception { Map<String, String> immutableEnv = System.getenv(); Class<?> unMap = Class.forName("java.util.Collections$UnmodifiableMap"); Field m = unMap.getDeclaredField("m"); m.setAccessible(true); @SuppressWarnings("unchecked") Map<String, String> env = (Map<String, String>) m.get(immutableEnv); try { env.put(AwsProfileNameLoader.AWS_PROFILE_ENVIRONMENT_VARIABLE, "test"); System.setProperty(AwsProfileNameLoader.AWS_PROFILE_SYSTEM_PROPERTY, "bogus"); ProfileCredentialsProvider provider = newProvider(); AWSCredentials credentials = provider.getCredentials(); Assert.assertEquals("test", credentials.getAWSAccessKeyId()); Assert.assertEquals("test key", credentials.getAWSSecretKey()); } finally { System.setProperty(AwsProfileNameLoader.AWS_PROFILE_SYSTEM_PROPERTY, ""); env.remove(AwsProfileNameLoader.AWS_PROFILE_ENVIRONMENT_VARIABLE); } }
@Test public void testExplicit() throws Exception { Map<String, String> immutableEnv = System.getenv(); Class<?> unMap = Class.forName("java.util.Collections$UnmodifiableMap"); Field m = unMap.getDeclaredField("m"); m.setAccessible(true); @SuppressWarnings("unchecked") Map<String, String> env = (Map<String, String>) m.get(immutableEnv); try { env.put(AwsProfileNameLoader.AWS_PROFILE_ENVIRONMENT_VARIABLE, "test"); System.setProperty(AwsProfileNameLoader.AWS_PROFILE_SYSTEM_PROPERTY, "test"); ProfileCredentialsProvider provider = new ProfileCredentialsProvider( profileLocation.getAbsolutePath(), "bogus"); try { provider.getCredentials(); Assert.fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } finally { System.setProperty(AwsProfileNameLoader.AWS_PROFILE_SYSTEM_PROPERTY, ""); env.remove(AwsProfileNameLoader.AWS_PROFILE_ENVIRONMENT_VARIABLE); } }
@Test public void testAssumeRole() throws Exception { ProfilesConfigFile profilesFile = new ProfilesConfigFile( ProfileResourceLoader.profileWithRole().asFile(), new ProfileCredentialsService() { @Override public AWSCredentialsProvider getAssumeRoleCredentialsProvider( RoleInfo targetRoleInfo) { AWSCredentials credentials = targetRoleInfo.getLongLivedCredentialsProvider() .getCredentials(); Assert.assertEquals("sourceProfile AWSAccessKeyId", "defaultAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("sourceProfile AWSSecretKey", "defaultSecretAccessKey", credentials.getAWSSecretKey()); Assert.assertEquals("role_arn", "arn:aws:iam::123456789012:role/testRole", targetRoleInfo.getRoleArn()); Assert.assertNull("external_id", targetRoleInfo.getExternalId()); Assert.assertTrue("role_session_name", targetRoleInfo.getRoleSessionName().startsWith("aws-sdk-java-")); return new StaticCredentialsProvider( new BasicAWSCredentials("sessionAccessKey", "sessionSecretKey")); } }); ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider( profilesFile, "test"); AWSCredentials credentials = profileCredentialsProvider.getCredentials(); Assert.assertEquals("sessionAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("sessionSecretKey", credentials.getAWSSecretKey()); }
@Test public void testAssumeRoleWithNameAndExternalId() throws Exception { ProfilesConfigFile profilesFile = new ProfilesConfigFile( ProfileResourceLoader.profileWithRole2().asFile(), new ProfileCredentialsService() { @Override public AWSCredentialsProvider getAssumeRoleCredentialsProvider( RoleInfo targetRoleInfo) { AWSCredentials credentials = targetRoleInfo.getLongLivedCredentialsProvider() .getCredentials(); Assert.assertEquals("sourceProfile AWSAccessKeyId", "defaultAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("sourceProfile AWSSecretKey", "defaultSecretAccessKey", credentials.getAWSSecretKey()); Assert.assertEquals("role_arn", "arn:aws:iam::123456789012:role/testRole", targetRoleInfo.getRoleArn()); Assert.assertEquals("external_id", "testExternalId", targetRoleInfo.getExternalId()); Assert.assertEquals("role_session_name", "testSessionName", targetRoleInfo.getRoleSessionName()); return new StaticCredentialsProvider( new BasicAWSCredentials("sessionAccessKey", "sessionSecretKey")); } }); ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider( profilesFile, "test"); AWSCredentials credentials = profileCredentialsProvider.getCredentials(); Assert.assertEquals("sessionAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("sessionSecretKey", credentials.getAWSSecretKey()); }
@Test public void testAssumeRoleWithSourceAfterRole() throws Exception { ProfilesConfigFile profilesFile = new ProfilesConfigFile( ProfileResourceLoader.profileWithSourceAfterRole().asFile(), new ProfileCredentialsService() { @Override public AWSCredentialsProvider getAssumeRoleCredentialsProvider( RoleInfo targetRoleInfo) { AWSCredentials credentials = targetRoleInfo .getLongLivedCredentialsProvider().getCredentials(); Assert.assertEquals("sourceProfile AWSAccessKeyId", "defaultAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("sourceProfile AWSSecretKey", "defaultSecretAccessKey", credentials.getAWSSecretKey()); Assert.assertEquals("role_arn", "arn:aws:iam::123456789012:role/testRole", targetRoleInfo.getRoleArn()); Assert.assertNull("external_id", targetRoleInfo.getExternalId()); Assert.assertTrue("role_session_name", targetRoleInfo.getRoleSessionName() .startsWith("aws-sdk-java-")); return new StaticCredentialsProvider( new BasicAWSCredentials("sessionAccessKey", "sessionSecretKey")); } }); ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider( profilesFile, "test"); AWSCredentials credentials = profileCredentialsProvider.getCredentials(); Assert.assertEquals("sessionAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("sessionSecretKey", credentials.getAWSSecretKey()); }
@Test public void testDefault() { ProfileCredentialsProvider provider = new ProfileCredentialsProvider(profileLocation, null); AWSCredentials credentials = provider.getCredentials(); Assert.assertEquals("defaultSecretAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("defaultAccessKey", credentials.getAWSSecretKey()); }
@Test public void testEnvironmentVariable() throws Exception { Map<String, String> immutableEnv = System.getenv(); Class<?> unMap = Class.forName("java.util.Collections$UnmodifiableMap"); Field m = unMap.getDeclaredField("m"); m.setAccessible(true); @SuppressWarnings("unchecked") Map<String, String> env = (Map<String, String>) m.get(immutableEnv); try { env.put(ProfilesConfigFile.AWS_PROFILE_ENVIRONMENT_VARIABLE, "test"); ProfileCredentialsProvider provider = new ProfileCredentialsProvider(profileLocation, null); AWSCredentials credentials = provider.getCredentials(); Assert.assertEquals("test", credentials.getAWSAccessKeyId()); Assert.assertEquals("test key", credentials.getAWSSecretKey()); } finally { env.remove(ProfilesConfigFile.AWS_PROFILE_ENVIRONMENT_VARIABLE); } }
@Test public void testSystemProperty() { try { System.setProperty( ProfilesConfigFile.AWS_PROFILE_SYSTEM_PROPERTY, "test"); ProfileCredentialsProvider provider = new ProfileCredentialsProvider(profileLocation, null); AWSCredentials credentials = provider.getCredentials(); Assert.assertEquals("test", credentials.getAWSAccessKeyId()); Assert.assertEquals("test key", credentials.getAWSSecretKey()); } finally { System.setProperty( ProfilesConfigFile.AWS_PROFILE_SYSTEM_PROPERTY, ""); } }
@Test public void testBoth() throws Exception { Map<String, String> immutableEnv = System.getenv(); Class<?> unMap = Class.forName("java.util.Collections$UnmodifiableMap"); Field m = unMap.getDeclaredField("m"); m.setAccessible(true); @SuppressWarnings("unchecked") Map<String, String> env = (Map<String, String>) m.get(immutableEnv); try { env.put(ProfilesConfigFile.AWS_PROFILE_ENVIRONMENT_VARIABLE, "test"); System.out.println(env.containsKey(ProfilesConfigFile.AWS_PROFILE_ENVIRONMENT_VARIABLE)); System.getenv(); System.setProperty( ProfilesConfigFile.AWS_PROFILE_SYSTEM_PROPERTY, "bogus"); ProfileCredentialsProvider provider = new ProfileCredentialsProvider(profileLocation, null); AWSCredentials credentials = provider.getCredentials(); Assert.assertEquals("test", credentials.getAWSAccessKeyId()); Assert.assertEquals("test key", credentials.getAWSSecretKey()); } finally { System.setProperty( ProfilesConfigFile.AWS_PROFILE_SYSTEM_PROPERTY, ""); env.remove(ProfilesConfigFile.AWS_PROFILE_ENVIRONMENT_VARIABLE); } }
@Test public void testExplicit() throws Exception { Map<String, String> immutableEnv = System.getenv(); Class<?> unMap = Class.forName("java.util.Collections$UnmodifiableMap"); Field m = unMap.getDeclaredField("m"); m.setAccessible(true); @SuppressWarnings("unchecked") Map<String, String> env = (Map<String, String>) m.get(immutableEnv); try { env.put(ProfilesConfigFile.AWS_PROFILE_ENVIRONMENT_VARIABLE, "test"); System.setProperty( ProfilesConfigFile.AWS_PROFILE_SYSTEM_PROPERTY, "test"); ProfileCredentialsProvider provider = new ProfileCredentialsProvider(profileLocation, "bogus"); try { provider.getCredentials(); Assert.fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } } finally { System.setProperty( ProfilesConfigFile.AWS_PROFILE_SYSTEM_PROPERTY, ""); env.remove(ProfilesConfigFile.AWS_PROFILE_ENVIRONMENT_VARIABLE); } }
@Test public void testAssumeRole() throws Exception { ProfilesConfigFile profilesFile = new ProfilesConfigFile(getLocationForTestFile("ProfileWithRole.tst"), new ProfileCredentialsService() { @Override public AWSCredentialsProvider getAssumeRoleCredentialsProvider(RoleInfo targetRoleInfo) { AWSCredentials credentials = targetRoleInfo.getLongLivedCredentialsProvider().getCredentials(); Assert.assertEquals("sourceProfile AWSAccessKeyId", "defaultAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("sourceProfile AWSSecretKey", "defaultSecretAccessKey", credentials.getAWSSecretKey()); Assert.assertEquals("role_arn", "arn:aws:iam::123456789012:role/testRole", targetRoleInfo.getRoleArn()); Assert.assertNull("external_id", targetRoleInfo.getExternalId()); Assert.assertTrue("role_session_name", targetRoleInfo.getRoleSessionName().startsWith("aws-sdk-java-")); return new StaticCredentialsProvider(new BasicAWSCredentials("sessionAccessKey", "sessionSecretKey")); } }); ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider(profilesFile, "test"); AWSCredentials credentials = profileCredentialsProvider.getCredentials(); Assert.assertEquals("sessionAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("sessionSecretKey", credentials.getAWSSecretKey()); }
@Test public void testAssumeRoleWithNameAndExternalId() throws Exception { ProfilesConfigFile profilesFile = new ProfilesConfigFile(getLocationForTestFile("ProfileWithRole2.tst"), new ProfileCredentialsService() { @Override public AWSCredentialsProvider getAssumeRoleCredentialsProvider(RoleInfo targetRoleInfo) { AWSCredentials credentials = targetRoleInfo.getLongLivedCredentialsProvider().getCredentials(); Assert.assertEquals("sourceProfile AWSAccessKeyId", "defaultAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("sourceProfile AWSSecretKey", "defaultSecretAccessKey", credentials.getAWSSecretKey()); Assert.assertEquals("role_arn", "arn:aws:iam::123456789012:role/testRole", targetRoleInfo.getRoleArn()); Assert.assertEquals("external_id", "testExternalId", targetRoleInfo.getExternalId()); Assert.assertEquals("role_session_name", "testSessionName", targetRoleInfo.getRoleSessionName()); return new StaticCredentialsProvider(new BasicAWSCredentials("sessionAccessKey", "sessionSecretKey")); } }); ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider(profilesFile, "test"); AWSCredentials credentials = profileCredentialsProvider.getCredentials(); Assert.assertEquals("sessionAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("sessionSecretKey", credentials.getAWSSecretKey()); }
@Test public void testAssumeRoleWithSourceAfterRole() throws Exception { ProfilesConfigFile profilesFile = new ProfilesConfigFile(getLocationForTestFile("ProfileWithSourceAfterRole.tst"), new ProfileCredentialsService() { @Override public AWSCredentialsProvider getAssumeRoleCredentialsProvider(RoleInfo targetRoleInfo) { AWSCredentials credentials = targetRoleInfo.getLongLivedCredentialsProvider().getCredentials(); Assert.assertEquals("sourceProfile AWSAccessKeyId", "defaultAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("sourceProfile AWSSecretKey", "defaultSecretAccessKey", credentials.getAWSSecretKey()); Assert.assertEquals("role_arn", "arn:aws:iam::123456789012:role/testRole", targetRoleInfo.getRoleArn()); Assert.assertNull("external_id", targetRoleInfo.getExternalId()); Assert.assertTrue("role_session_name", targetRoleInfo.getRoleSessionName().startsWith("aws-sdk-java-")); return new StaticCredentialsProvider(new BasicAWSCredentials("sessionAccessKey", "sessionSecretKey")); } }); ProfileCredentialsProvider profileCredentialsProvider = new ProfileCredentialsProvider(profilesFile, "test"); AWSCredentials credentials = profileCredentialsProvider.getCredentials(); Assert.assertEquals("sessionAccessKey", credentials.getAWSAccessKeyId()); Assert.assertEquals("sessionSecretKey", credentials.getAWSSecretKey()); }