SystemPropertiesCredentialsProvider implements AWSCredentialsProvider { @Override public AWSCredentials getCredentials() { String accessKey = StringUtils.trim(System.getProperty(ACCESS_KEY_SYSTEM_PROPERTY)); String secretKey = StringUtils.trim(System.getProperty(SECRET_KEY_SYSTEM_PROPERTY)); String sessionToken = StringUtils.trim(System.getProperty(SESSION_TOKEN_SYSTEM_PROPERTY)); if (StringUtils.isNullOrEmpty(accessKey) || StringUtils.isNullOrEmpty(secretKey)) { throw new SdkClientException( "Unable to load AWS credentials from Java system " + "properties (" + ACCESS_KEY_SYSTEM_PROPERTY + " and " + SECRET_KEY_SYSTEM_PROPERTY + ")"); } if (StringUtils.isNullOrEmpty(sessionToken)) { return new BasicAWSCredentials(accessKey, secretKey); } else { return new BasicSessionCredentials(accessKey, secretKey, sessionToken); } }  @Override AWSCredentials getCredentials(); @Override void refresh(); @Override String toString();  }
@Test public void accessKeyAndSecretPropertiesSet_ReturnsBasicCredentials() { System.setProperty(ACCESS_KEY_SYSTEM_PROPERTY, "akid-value"); System.setProperty(SECRET_KEY_SYSTEM_PROPERTY, "skid-value"); final AWSCredentials credentials = provider.getCredentials(); assertThat(credentials, not(instanceOf(AWSSessionCredentials.class))); assertEquals("akid-value", credentials.getAWSAccessKeyId()); assertEquals("skid-value", credentials.getAWSSecretKey()); }
@Test public void sessionTokenSet_ReturnsSessionCredentials() { System.setProperty(ACCESS_KEY_SYSTEM_PROPERTY, "akid-value"); System.setProperty(SECRET_KEY_SYSTEM_PROPERTY, "skid-value"); System.setProperty(SESSION_TOKEN_SYSTEM_PROPERTY, "session-value"); final AWSCredentials credentials = provider.getCredentials(); assertThat(credentials, instanceOf(AWSSessionCredentials.class)); assertEquals("akid-value", credentials.getAWSAccessKeyId()); assertEquals("skid-value", credentials.getAWSSecretKey()); assertEquals("session-value", ((AWSSessionCredentials) credentials).getSessionToken()); }
@Test(expected = SdkClientException.class) public void noPropertiesSet_ProviderThrowsException() { provider.getCredentials(); }