|
1 | 1 | package org.prebid.server.spring.config; |
2 | 2 |
|
| 3 | +import org.apache.commons.lang3.StringUtils; |
| 4 | +import org.prebid.server.log.Logger; |
| 5 | +import org.prebid.server.log.LoggerFactory; |
3 | 6 | import io.vertx.core.Vertx; |
4 | 7 | import io.vertx.core.file.FileSystem; |
5 | 8 | import lombok.Data; |
|
40 | 43 | import org.springframework.stereotype.Component; |
41 | 44 | import org.springframework.validation.annotation.Validated; |
42 | 45 | import software.amazon.awssdk.auth.credentials.AwsBasicCredentials; |
| 46 | +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; |
43 | 47 | import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; |
| 48 | +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; |
44 | 49 | import software.amazon.awssdk.regions.Region; |
45 | 50 | import software.amazon.awssdk.services.s3.S3AsyncClient; |
| 51 | +import software.amazon.awssdk.core.exception.SdkClientException; |
46 | 52 |
|
47 | 53 | import javax.validation.constraints.Min; |
48 | 54 | import javax.validation.constraints.NotBlank; |
|
58 | 64 | @UtilityClass |
59 | 65 | public class SettingsConfiguration { |
60 | 66 |
|
| 67 | + private static final Logger logger = LoggerFactory.getLogger(SettingsConfiguration.class); |
| 68 | + |
61 | 69 | @Configuration |
62 | 70 | @ConditionalOnProperty(prefix = "settings.filesystem", |
63 | 71 | name = {"settings-filename", "stored-requests-dir", "stored-imps-dir"}) |
@@ -233,18 +241,30 @@ static class S3SettingsConfiguration { |
233 | 241 |
|
234 | 242 | @Component |
235 | 243 | @ConfigurationProperties(prefix = "settings.s3") |
236 | | - @ConditionalOnProperty(prefix = "settings.s3", name = {"accessKeyId", "secretAccessKey"}) |
237 | 244 | @Validated |
238 | 245 | @Data |
239 | 246 | @NoArgsConstructor |
240 | 247 | protected static class S3ConfigurationProperties { |
241 | 248 |
|
242 | | - @NotBlank |
| 249 | + /** |
| 250 | + * If accessKeyId and secretAccessKey are provided in the |
| 251 | + * configuration file then they will be used. Otherwise, the |
| 252 | + * DefaultCredentialsProvider will look for credentials in this order: |
| 253 | + * |
| 254 | + * - Java System Properties |
| 255 | + * - Environment Variables |
| 256 | + * - Web Identity Token |
| 257 | + * - AWS credentials file (~/.aws/credentials) |
| 258 | + * - ECS container credentials |
| 259 | + * - EC2 instance profile |
| 260 | + */ |
243 | 261 | private String accessKeyId; |
244 | | - |
245 | | - @NotBlank |
246 | 262 | private String secretAccessKey; |
247 | 263 |
|
| 264 | + private boolean useStaticCredentials() { |
| 265 | + return StringUtils.isNotBlank(accessKeyId) && StringUtils.isNotBlank(secretAccessKey); |
| 266 | + } |
| 267 | + |
248 | 268 | /** |
249 | 269 | * If not provided AWS_GLOBAL will be used as a region |
250 | 270 | */ |
@@ -274,22 +294,33 @@ protected static class S3ConfigurationProperties { |
274 | 294 |
|
275 | 295 | @Bean |
276 | 296 | S3AsyncClient s3AsyncClient(S3ConfigurationProperties s3ConfigurationProperties) throws URISyntaxException { |
277 | | - final AwsBasicCredentials credentials = AwsBasicCredentials.create( |
278 | | - s3ConfigurationProperties.getAccessKeyId(), |
279 | | - s3ConfigurationProperties.getSecretAccessKey()); |
280 | 297 | final Region awsRegion = Optional.ofNullable(s3ConfigurationProperties.getRegion()) |
281 | 298 | .map(Region::of) |
282 | 299 | .orElse(Region.AWS_GLOBAL); |
283 | 300 |
|
284 | | - return S3AsyncClient |
285 | | - .builder() |
286 | | - .credentialsProvider(StaticCredentialsProvider.create(credentials)) |
| 301 | + return S3AsyncClient.builder() |
| 302 | + .credentialsProvider(awsCredentialsProvider(s3ConfigurationProperties)) |
287 | 303 | .endpointOverride(new URI(s3ConfigurationProperties.getEndpoint())) |
288 | 304 | .forcePathStyle(s3ConfigurationProperties.getForcePathStyle()) |
289 | 305 | .region(awsRegion) |
290 | 306 | .build(); |
291 | 307 | } |
292 | 308 |
|
| 309 | + private static AwsCredentialsProvider awsCredentialsProvider(S3ConfigurationProperties config) { |
| 310 | + final AwsCredentialsProvider credentialsProvider = config.useStaticCredentials() |
| 311 | + ? StaticCredentialsProvider.create( |
| 312 | + AwsBasicCredentials.create(config.getAccessKeyId(), config.getSecretAccessKey())) |
| 313 | + : DefaultCredentialsProvider.create(); |
| 314 | + |
| 315 | + try { |
| 316 | + credentialsProvider.resolveCredentials(); |
| 317 | + } catch (SdkClientException e) { |
| 318 | + logger.error("Failed to resolve AWS credentials", e); |
| 319 | + } |
| 320 | + |
| 321 | + return credentialsProvider; |
| 322 | + } |
| 323 | + |
293 | 324 | @Bean |
294 | 325 | S3ApplicationSettings s3ApplicationSettings(S3AsyncClient s3AsyncClient, |
295 | 326 | S3ConfigurationProperties s3ConfigurationProperties, |
|
0 commit comments