1
15 package com.amazonaws.auth.profile.internal.securitytoken;
16
17 import com.amazonaws.SdkClientException;
18 import com.amazonaws.annotation.ThreadSafe;
19 import com.amazonaws.auth.AWSCredentials;
20 import com.amazonaws.auth.AWSCredentialsProvider;
21
22 @ThreadSafe
23 public class STSProfileCredentialsServiceProvider implements AWSCredentialsProvider {
24 private static final String CLASS_NAME = "com.amazonaws.services.securitytoken.internal.STSProfileCredentialsService";
25 private static volatile ProfileCredentialsService STS_CREDENTIALS_SERVICE;
26
27 private final RoleInfo roleInfo;
28 private volatile AWSCredentialsProvider profileCredentialsProvider;
29
30 public STSProfileCredentialsServiceProvider(RoleInfo roleInfo) {
31 this.roleInfo = roleInfo;
32 }
33
34 private AWSCredentialsProvider getProfileCredentialsProvider() {
35 if (this.profileCredentialsProvider == null) {
36 synchronized (STSProfileCredentialsServiceProvider.class) {
37 if (this.profileCredentialsProvider == null) {
38 this.profileCredentialsProvider = getProfileCredentialService()
39 .getAssumeRoleCredentialsProvider(roleInfo);
40 }
41 }
42 }
43 return this.profileCredentialsProvider;
44 }
45
46
50 private static synchronized ProfileCredentialsService getProfileCredentialService() {
51 if (STS_CREDENTIALS_SERVICE == null) {
52 try {
53 STS_CREDENTIALS_SERVICE = (ProfileCredentialsService) Class.forName(CLASS_NAME)
54 .newInstance();
55 } catch (ClassNotFoundException ex) {
56 throw new SdkClientException(
57 "To use assume role profiles the aws-java-sdk-sts module must be on the class path.",
58 ex);
59 } catch (InstantiationException ex) {
60 throw new SdkClientException("Failed to instantiate " + CLASS_NAME, ex);
61 } catch (IllegalAccessException ex) {
62 throw new SdkClientException("Failed to instantiate " + CLASS_NAME, ex);
63 }
64 }
65 return STS_CREDENTIALS_SERVICE;
66 }
67
68
69 @Override
70 public AWSCredentials getCredentials() {
71 return getProfileCredentialsProvider().getCredentials();
72 }
73
74 @Override
75 public void refresh() {
76 getProfileCredentialsProvider().refresh();
77 }
78 }
79