-
Notifications
You must be signed in to change notification settings - Fork 914
Implement Asynchronous EC2 Metadata Client #3523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
c81a347
IMDS Ec2 Metada refactoring
L-Applin 2eb31a1
IMDS Ec2 Metada refactoring
L-Applin c4bfaf0
IMDS Ec2 Metada refactoring
L-Applin 6a6bb3b
IMDS Ec2 Metada refactoring
L-Applin ce7e5cf
IMDS Ec2 Metada refactoring
L-Applin 38486f5
IMDS Ec2 Metada refactoring
L-Applin d5a44a7
IMDS Ec2 Metada refactoring
L-Applin 80f6c2f
IMDS Async
L-Applin 7eb6f00
IMDS Async client
L-Applin 1b25ae1
IMDS Async client
L-Applin 3d14c96
IMDS Async client
L-Applin e0c31c4
IMDS Async client
L-Applin cdb801c
IMDS Async client
L-Applin 884d552
IMDS Async
L-Applin a130632
IMDS Async client
L-Applin 30be64c
IMDS Async client
L-Applin 9981b14
IMDS Async client
L-Applin 73eeefa
IMDS Async client
L-Applin c5991e9
IMDS Async client
L-Applin d50c0be
IMDS Async client
L-Applin 58c589a
IMDS Async client
L-Applin d59b4b8
IMDS Async client
L-Applin 0db213f
Fix test classes
L-Applin 2c04edb
JavaDoc
L-Applin 132ce9b
PR comments and added IMDS to test-coverage-reporting
L-Applin 71b0ab3
Improve JavaDoc and rename some variables
L-Applin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataClientBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.imds; | ||
|
||
import java.net.URI; | ||
import java.time.Duration; | ||
import software.amazon.awssdk.annotations.SdkPublicApi; | ||
import software.amazon.awssdk.utils.builder.SdkBuilder; | ||
|
||
/** | ||
* Base shared builder interface for Ec2MetadataClient | ||
* @param <B> the Builder Type | ||
* @param <T> the Ec2MetadataClient Type | ||
*/ | ||
@SdkPublicApi | ||
public interface Ec2MetadataClientBuilder<B, T> extends SdkBuilder<Ec2MetadataClientBuilder<B, T>, T> { | ||
/** | ||
* Define the retry policy which includes the number of retry attempts for any failed request. | ||
cenedhryn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param retryPolicy The retry policy which includes the number of retry attempts for any failed request. | ||
* @return a reference to this builder | ||
*/ | ||
B retryPolicy(Ec2MetadataRetryPolicy retryPolicy); | ||
|
||
Ec2MetadataRetryPolicy getRetryPolicy(); | ||
|
||
/** | ||
* Define the endpoint of IMDS. | ||
cenedhryn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param endpoint The endpoint of IMDS. | ||
* @return a reference to this builder | ||
*/ | ||
B endpoint(URI endpoint); | ||
|
||
URI getEndpoint(); | ||
|
||
/** | ||
* Define the Time to live (TTL) of the token. | ||
cenedhryn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param tokenTtl The Time to live (TTL) of the token. | ||
* @return a reference to this builder | ||
*/ | ||
B tokenTtl(Duration tokenTtl); | ||
|
||
Duration getTokenTtl(); | ||
|
||
/** | ||
* Define the endpoint mode of IMDS. Supported values include IPv4 and IPv6. | ||
cenedhryn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* | ||
* @param endpointMode The endpoint mode of IMDS. Supported values include IPv4 and IPv6. | ||
* @return a reference to this builder | ||
*/ | ||
B endpointMode(EndpointMode endpointMode); | ||
|
||
EndpointMode getEndpointMode(); | ||
|
||
} |
77 changes: 77 additions & 0 deletions
77
core/imds/src/main/java/software/amazon/awssdk/imds/internal/BaseEc2MetadataClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.imds.internal; | ||
|
||
import static software.amazon.awssdk.imds.internal.Ec2MetadataEndpointProvider.DEFAULT_ENDPOINT_PROVIDER; | ||
|
||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.net.URI; | ||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.http.AbortableInputStream; | ||
import software.amazon.awssdk.imds.Ec2MetadataClientBuilder; | ||
import software.amazon.awssdk.imds.Ec2MetadataRetryPolicy; | ||
import software.amazon.awssdk.imds.EndpointMode; | ||
import software.amazon.awssdk.utils.IoUtils; | ||
import software.amazon.awssdk.utils.Logger; | ||
import software.amazon.awssdk.utils.Validate; | ||
|
||
@SdkInternalApi | ||
public abstract class BaseEc2MetadataClient { | ||
|
||
protected static final Duration DEFAULT_TOKEN_TTL = Duration.of(21_600, ChronoUnit.SECONDS); | ||
private static final Logger log = Logger.loggerFor(BaseEc2MetadataClient.class); | ||
|
||
protected final Ec2MetadataRetryPolicy retryPolicy; | ||
protected final URI endpoint; | ||
protected final RequestMarshaller requestMarshaller; | ||
protected final Duration tokenTtl; | ||
|
||
protected BaseEc2MetadataClient(Ec2MetadataClientBuilder<?, ?> builder) { | ||
this.retryPolicy = Validate.getOrDefault(builder.getRetryPolicy(), Ec2MetadataRetryPolicy.builder()::build); | ||
this.tokenTtl = Validate.getOrDefault(builder.getTokenTtl(), () -> DEFAULT_TOKEN_TTL); | ||
this.endpoint = getEndpoint(builder); | ||
this.requestMarshaller = new RequestMarshaller(this.endpoint); | ||
} | ||
|
||
private URI getEndpoint(Ec2MetadataClientBuilder<?, ?> builder) { | ||
URI builderEndpoint = builder.getEndpoint(); | ||
EndpointMode builderEndpointMode = builder.getEndpointMode(); | ||
Validate.mutuallyExclusive("Only one of 'endpoint' or 'endpointMode' must be specified, but not both", | ||
builderEndpoint, builderEndpointMode); | ||
if (builderEndpoint != null) { | ||
return builderEndpoint; | ||
} | ||
if (builderEndpointMode != null) { | ||
return URI.create(DEFAULT_ENDPOINT_PROVIDER.resolveEndpoint(builderEndpointMode)); | ||
} | ||
EndpointMode resolvedEndpointMode = DEFAULT_ENDPOINT_PROVIDER.resolveEndpointMode(); | ||
return URI.create(DEFAULT_ENDPOINT_PROVIDER.resolveEndpoint(resolvedEndpointMode)); | ||
} | ||
|
||
protected static String uncheckedInputStreamToUtf8(AbortableInputStream inputStream) { | ||
try { | ||
return IoUtils.toUtf8String(inputStream); | ||
} catch (IOException ioe) { | ||
throw new UncheckedIOException(ioe); | ||
} finally { | ||
IoUtils.closeQuietly(inputStream, log.logger()); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.