|
| 1 | +/* |
| 2 | + * Copyright 2024 Amazon.com, Inc. or its affiliates. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the |
| 4 | + * "License"); you may not use this file except in compliance |
| 5 | + * with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +package software.amazon.kinesis.multilang.auth; |
| 16 | + |
| 17 | +import java.net.URI; |
| 18 | +import java.util.Arrays; |
| 19 | + |
| 20 | +import software.amazon.awssdk.auth.credentials.AwsCredentials; |
| 21 | +import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; |
| 22 | +import software.amazon.awssdk.regions.Region; |
| 23 | +import software.amazon.awssdk.services.sts.StsClient; |
| 24 | +import software.amazon.awssdk.services.sts.StsClientBuilder; |
| 25 | +import software.amazon.awssdk.services.sts.auth.StsAssumeRoleCredentialsProvider; |
| 26 | +import software.amazon.awssdk.services.sts.model.AssumeRoleRequest; |
| 27 | +import software.amazon.awssdk.services.sts.model.AssumeRoleRequest.Builder; |
| 28 | +import software.amazon.kinesis.multilang.NestedPropertyKey; |
| 29 | +import software.amazon.kinesis.multilang.NestedPropertyProcessor; |
| 30 | + |
| 31 | +public class KclStsAssumeRoleCredentialsProvider implements AwsCredentialsProvider, NestedPropertyProcessor { |
| 32 | + private final Builder assumeRoleRequestBuilder; |
| 33 | + private final StsClientBuilder stsClientBuilder; |
| 34 | + private final StsAssumeRoleCredentialsProvider stsAssumeRoleCredentialsProvider; |
| 35 | + |
| 36 | + public KclStsAssumeRoleCredentialsProvider(String[] params) { |
| 37 | + this(params[0], params[1], Arrays.copyOfRange(params, 2, params.length)); |
| 38 | + } |
| 39 | + |
| 40 | + public KclStsAssumeRoleCredentialsProvider(String roleArn, String roleSessionName, String... params) { |
| 41 | + this.assumeRoleRequestBuilder = |
| 42 | + AssumeRoleRequest.builder().roleArn(roleArn).roleSessionName(roleSessionName); |
| 43 | + this.stsClientBuilder = StsClient.builder(); |
| 44 | + NestedPropertyKey.parse(this, params); |
| 45 | + this.stsAssumeRoleCredentialsProvider = StsAssumeRoleCredentialsProvider.builder() |
| 46 | + .refreshRequest(assumeRoleRequestBuilder.build()) |
| 47 | + .asyncCredentialUpdateEnabled(true) |
| 48 | + .stsClient(stsClientBuilder.build()) |
| 49 | + .build(); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public AwsCredentials resolveCredentials() { |
| 54 | + return stsAssumeRoleCredentialsProvider.resolveCredentials(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void acceptEndpoint(String serviceEndpoint, String signingRegion) { |
| 59 | + if (!serviceEndpoint.startsWith("http://") && !serviceEndpoint.startsWith("https://")) { |
| 60 | + serviceEndpoint = "https://" + serviceEndpoint; |
| 61 | + } |
| 62 | + stsClientBuilder.endpointOverride(URI.create(serviceEndpoint)); |
| 63 | + stsClientBuilder.region(Region.of(signingRegion)); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public void acceptEndpointRegion(Region region) { |
| 68 | + stsClientBuilder.region(region); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void acceptExternalId(String externalId) { |
| 73 | + assumeRoleRequestBuilder.externalId(externalId); |
| 74 | + } |
| 75 | +} |
0 commit comments