3
3
import org .slf4j .Logger ;
4
4
import org .slf4j .LoggerFactory ;
5
5
import org .springframework .beans .factory .annotation .Autowired ;
6
+ import org .springframework .beans .factory .annotation .Value ;
6
7
import org .springframework .context .annotation .Profile ;
7
8
import org .springframework .stereotype .Service ;
8
9
import software .amazon .awssdk .auth .credentials .AwsSessionCredentials ;
10
+ import software .amazon .awssdk .auth .credentials .InstanceProfileCredentialsProvider ;
9
11
import software .amazon .awssdk .auth .credentials .StaticCredentialsProvider ;
10
12
import software .amazon .awssdk .http .SdkHttpClient ;
11
13
import software .amazon .awssdk .services .s3 .S3Client ;
@@ -27,18 +29,21 @@ public class AWSClientBuilder {
27
29
private final StsClientProvider stsClientProvider ;
28
30
private final S3ClientBuilder s3ClientBuilder ;
29
31
private final SdkHttpClient sdkHttpClient ;
32
+ private final boolean retainRole ;
30
33
31
34
@ Autowired
32
35
public AWSClientBuilder (
33
36
Map <String , SiteAWSInfo > sites ,
34
37
StsClientProvider stsClientProvider ,
35
38
S3ClientBuilder s3ClientBuilder ,
36
- @ Autowired (required = false ) SdkHttpClient sdkHttpClient
39
+ @ Autowired (required = false ) SdkHttpClient sdkHttpClient ,
40
+ @ Value ("${s3.retain_role:false}" ) boolean retainRole
37
41
) {
38
42
this .sites = sites ;
39
43
this .stsClientProvider = stsClientProvider ;
40
44
this .s3ClientBuilder = s3ClientBuilder ;
41
45
this .sdkHttpClient = sdkHttpClient ;
46
+ this .retainRole = false ;
42
47
}
43
48
44
49
public Optional <S3Client > buildClientForSite (String siteName ) {
@@ -48,6 +53,14 @@ public Optional<S3Client> buildClientForSite(String siteName) {
48
53
return Optional .empty ();
49
54
}
50
55
56
+ if (retainRole ) {
57
+ log .info ("s3.retain_role set to true. Will retain current role rather than assuming one for site" );
58
+ S3Client client = s3ClientBuilder
59
+ .credentialsProvider (InstanceProfileCredentialsProvider .create ())
60
+ .build ();
61
+ return Optional .of (client );
62
+ }
63
+
51
64
log .info ("Found site, making assume role request" );
52
65
SiteAWSInfo site = sites .get (siteName );
53
66
AssumeRoleRequest roleRequest = AssumeRoleRequest .builder ()
0 commit comments