|
15 | 15 |
|
16 | 16 | package software.amazon.awssdk.services.ec2.transform.internal; |
17 | 17 |
|
18 | | -import static software.amazon.awssdk.auth.signer.AwsSignerExecutionAttribute.AWS_CREDENTIALS; |
19 | 18 | import static software.amazon.awssdk.core.interceptor.SdkInternalExecutionAttribute.SELECTED_AUTH_SCHEME; |
20 | 19 |
|
21 | 20 | import java.net.URI; |
22 | 21 | import java.time.Clock; |
| 22 | +import java.time.Duration; |
| 23 | +import java.time.Instant; |
| 24 | +import java.time.ZoneOffset; |
| 25 | +import java.util.concurrent.CompletableFuture; |
23 | 26 | import software.amazon.awssdk.annotations.SdkInternalApi; |
24 | 27 | import software.amazon.awssdk.annotations.SdkTestInternalApi; |
25 | | -import software.amazon.awssdk.auth.credentials.AwsCredentials; |
26 | | -import software.amazon.awssdk.auth.credentials.CredentialUtils; |
27 | | -import software.amazon.awssdk.auth.signer.Aws4Signer; |
28 | | -import software.amazon.awssdk.auth.signer.params.Aws4PresignerParams; |
29 | 28 | import software.amazon.awssdk.awscore.util.AwsHostNameUtils; |
30 | 29 | import software.amazon.awssdk.core.ClientEndpointProvider; |
31 | 30 | import software.amazon.awssdk.core.SdkRequest; |
| 31 | +import software.amazon.awssdk.core.SelectedAuthScheme; |
32 | 32 | import software.amazon.awssdk.core.client.config.SdkClientConfiguration; |
33 | 33 | import software.amazon.awssdk.core.client.config.SdkClientOption; |
34 | 34 | import software.amazon.awssdk.core.exception.SdkClientException; |
|
38 | 38 | import software.amazon.awssdk.http.SdkHttpFullRequest; |
39 | 39 | import software.amazon.awssdk.http.SdkHttpMethod; |
40 | 40 | import software.amazon.awssdk.http.SdkHttpRequest; |
41 | | -import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity; |
| 41 | +import software.amazon.awssdk.http.auth.aws.signer.AwsV4FamilyHttpSigner; |
| 42 | +import software.amazon.awssdk.http.auth.aws.signer.AwsV4HttpSigner; |
| 43 | +import software.amazon.awssdk.http.auth.aws.signer.SignerConstant; |
| 44 | +import software.amazon.awssdk.http.auth.spi.scheme.AuthSchemeOption; |
| 45 | +import software.amazon.awssdk.http.auth.spi.signer.HttpSigner; |
| 46 | +import software.amazon.awssdk.http.auth.spi.signer.SignRequest; |
| 47 | +import software.amazon.awssdk.http.auth.spi.signer.SignedRequest; |
| 48 | +import software.amazon.awssdk.identity.spi.Identity; |
42 | 49 | import software.amazon.awssdk.protocols.query.AwsEc2ProtocolFactory; |
43 | 50 | import software.amazon.awssdk.regions.Region; |
44 | 51 | import software.amazon.awssdk.services.ec2.Ec2Client; |
@@ -123,38 +130,54 @@ public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context, Execu |
123 | 130 | .method(SdkHttpMethod.GET) |
124 | 131 | .build(); |
125 | 132 |
|
126 | | - Aws4Signer signer = Aws4Signer.create(); |
127 | | - Aws4PresignerParams signingParams = getPresignerParams(executionAttributes, sourceRegion, serviceName); |
128 | | - |
129 | | - SdkHttpFullRequest presignedRequest = signer.presign(requestForPresigning, signingParams); |
| 133 | + URI presignedUrl = |
| 134 | + sraPresignRequest(executionAttributes, requestForPresigning, sourceRegion); |
130 | 135 |
|
131 | 136 | return request.toBuilder() |
132 | 137 | .putRawQueryParameter("DestinationRegion", destinationRegion) |
133 | | - .putRawQueryParameter("PresignedUrl", presignedRequest.getUri().toString()) |
| 138 | + .putRawQueryParameter("PresignedUrl", presignedUrl.toString()) |
134 | 139 | .build(); |
135 | 140 | } |
136 | 141 |
|
137 | 142 | return request; |
138 | 143 | } |
139 | 144 |
|
140 | | - private Aws4PresignerParams getPresignerParams(ExecutionAttributes attributes, String signingRegion, String signingName) { |
141 | | - return Aws4PresignerParams.builder() |
142 | | - .signingRegion(Region.of(signingRegion)) |
143 | | - .signingName(signingName) |
144 | | - .awsCredentials(resolveCredentials(attributes)) |
145 | | - .signingClockOverride(testClock) |
146 | | - .build(); |
| 145 | + private URI sraPresignRequest(ExecutionAttributes executionAttributes, SdkHttpFullRequest request, |
| 146 | + String signingRegion) { |
| 147 | + SelectedAuthScheme<?> selectedAuthScheme = executionAttributes.getAttribute(SELECTED_AUTH_SCHEME); |
| 148 | + Instant signingInstant; |
| 149 | + if (testClock != null) { |
| 150 | + signingInstant = testClock.instant(); |
| 151 | + } else { |
| 152 | + signingInstant = Instant.now(); |
| 153 | + } |
| 154 | + |
| 155 | + Clock signingClock = Clock.fixed(signingInstant, ZoneOffset.UTC); |
| 156 | + Duration expirationDuration = SignerConstant.PRESIGN_URL_MAX_EXPIRATION_DURATION; |
| 157 | + return doSraPresign(request, selectedAuthScheme, signingRegion, signingClock, expirationDuration); |
147 | 158 | } |
148 | 159 |
|
149 | | - private AwsCredentials resolveCredentials(ExecutionAttributes attributes) { |
150 | | - return attributes.getOptionalAttribute(SELECTED_AUTH_SCHEME) |
151 | | - .map(selectedAuthScheme -> selectedAuthScheme.identity()) |
152 | | - .map(identityFuture -> CompletableFutureUtils.joinLikeSync(identityFuture)) |
153 | | - .filter(identity -> identity instanceof AwsCredentialsIdentity) |
154 | | - .map(identity -> { |
155 | | - AwsCredentialsIdentity awsCredentialsIdentity = (AwsCredentialsIdentity) identity; |
156 | | - return CredentialUtils.toCredentials(awsCredentialsIdentity); |
157 | | - }).orElse(attributes.getAttribute(AWS_CREDENTIALS)); |
| 160 | + private <T extends Identity> URI doSraPresign(SdkHttpFullRequest request, |
| 161 | + SelectedAuthScheme<T> selectedAuthScheme, |
| 162 | + String signingRegion, |
| 163 | + Clock signingClock, |
| 164 | + Duration expirationDuration) { |
| 165 | + CompletableFuture<? extends T> identityFuture = selectedAuthScheme.identity(); |
| 166 | + T identity = CompletableFutureUtils.joinLikeSync(identityFuture); |
| 167 | + |
| 168 | + SignRequest.Builder<T> signRequestBuilder = SignRequest |
| 169 | + .builder(identity) |
| 170 | + .putProperty(AwsV4FamilyHttpSigner.AUTH_LOCATION, AwsV4FamilyHttpSigner.AuthLocation.QUERY_STRING) |
| 171 | + .putProperty(AwsV4FamilyHttpSigner.EXPIRATION_DURATION, expirationDuration) |
| 172 | + .putProperty(HttpSigner.SIGNING_CLOCK, signingClock) |
| 173 | + .request(request) |
| 174 | + .payload(request.contentStreamProvider().orElse(null)); |
| 175 | + AuthSchemeOption authSchemeOption = selectedAuthScheme.authSchemeOption(); |
| 176 | + authSchemeOption.forEachSignerProperty(signRequestBuilder::putProperty); |
| 177 | + signRequestBuilder.putProperty(AwsV4HttpSigner.REGION_NAME, signingRegion); |
| 178 | + HttpSigner<T> signer = selectedAuthScheme.signer(); |
| 179 | + SignedRequest signedRequest = signer.sign(signRequestBuilder.build()); |
| 180 | + return signedRequest.request().getUri(); |
158 | 181 | } |
159 | 182 |
|
160 | 183 | /** |
|
0 commit comments