Skip to content

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 26 commits into from
Nov 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/imds/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,8 @@
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-crt-client</artifactId>
<version>${awsjavasdk.version}-PREVIEW</version>
<artifactId>netty-nio-client</artifactId>
<version>${awsjavasdk.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,33 @@

package software.amazon.awssdk.imds;

import java.net.URI;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.core.internal.http.loader.DefaultSdkAsyncHttpClientBuilder;
import software.amazon.awssdk.core.retry.RetryMode;
import software.amazon.awssdk.core.retry.backoff.BackoffStrategy;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.imds.internal.DefaultEc2MetadataAsyncClient;
import software.amazon.awssdk.imds.internal.Ec2MetadataEndpointProvider;
import software.amazon.awssdk.utils.SdkAutoCloseable;

/**
* Interface to represent the Ec2Metadata Client Class. Used to access instance metadata from a running instance.
* Interface to represent the Ec2Metadata Client Class. Used to access instance metadata from a running instance.
*/
@SdkPublicApi
public interface Ec2MetadataAsyncClient extends SdkAutoCloseable {

/**
* Gets the specified instance metadata value by the given path.
* @param path Input path
*
* @param path Input path
* @return A CompletableFuture that completes when the MetadataResponse is made available.
*/
CompletableFuture<MetadataResponse> get(String path);

/**
* @return The Builder Object consisting all the fields.
* Create an {@link Ec2MetadataAsyncClient} instance using the default values.
*
* @return
*/
Ec2MetadataAsyncClient.Builder toBuilder();

static Ec2MetadataAsyncClient create() {
return builder().build();
}
Expand All @@ -57,65 +52,30 @@ static Ec2MetadataAsyncClient.Builder builder() {

/**
* The builder definition for a {@link Ec2MetadataClient}. All parameters are optional and have default values if not
* specified. Therefore, an instance can be simply created with {@code Ec2MetadataAsyncClient.builder().build()} or {@code
* Ec2MetadataAsyncClient.create()}, both having the same result.
* specified. Therefore, an instance can be simply created with {@code Ec2MetadataAsyncClient.builder().build()} or
* {@code Ec2MetadataAsyncClient.create()}, both having the same result.
*/
interface Builder {

/**
* Define the retry policy which includes the number of retry attempts for any failed request. If not specified, defaults
* to 3 retries using {@link BackoffStrategy#defaultStrategy(RetryMode)} with {@link RetryMode#STANDARD}.
*
* @param retryPolicy The retry policy which includes the number of retry attempts for any failed request.
* @return Returns a reference to this builder
*/
Builder retryPolicy(Ec2MetadataRetryPolicy retryPolicy);

/**
* Define the endpoint of IMDS. If not specified, the endpoint is resolved using
* {@link Ec2MetadataEndpointProvider#resolveEndpoint(EndpointMode)}
*
* @param endpoint The endpoint of IMDS.
* @return Returns a reference to this builder
*/
Builder endpoint(URI endpoint);

/**
* Define the Time to live (TTL) of the token. If not specified, defaults to 21,600 secs or 6 hours.
*
* @param tokenTtl The Time to live (TTL) of the token.
* @return Returns a reference to this builder
*/
Builder tokenTtl(Duration tokenTtl);

/**
* Define the endpoint mode of IMDS. Supported values include IPv4 and IPv6. If not specified, the default value is
* resolved using {@link Ec2MetadataEndpointProvider#resolveEndpointMode()}
*
* @param endpointMode The endpoint mode of IMDS.Supported values include IPv4 and IPv6.
* @return Returns a reference to this builder
*/
Builder endpointMode(EndpointMode endpointMode);

/**
* Define the {@link SdkAsyncHttpClient} instance to make the http requests. If not specified, the Client will try to
* instantiate a http client using {@link DefaultSdkAsyncHttpClientBuilder}.
*
* @param httpClient The SdkHttpClient instance to make the http requests.
* @return Returns a reference to this builder
*/
Builder httpClient(SdkAsyncHttpClient httpClient);
interface Builder extends Ec2MetadataClientBuilder<Ec2MetadataAsyncClient.Builder, Ec2MetadataAsyncClient> {

/**
* Define the {@link ScheduledExecutorService} used to schedule asynchronous retry attempts. If not specified,
* defaults to {@link Executors#newScheduledThreadPool} with a default value of 3 thread in the pool.
* If provided, the Ec2MetadataClient will <em>NOT</em> manage the lifetime if the httpClient and must therefore be
* closed explicitly by calling the {@link SdkAsyncHttpClient#close()} method on it.
*
* @param scheduledExecutorService the ScheduledExecutorService to use for retry attempt.
* @return a reference to this builder
*/
Builder scheduledExecutorService(ScheduledExecutorService scheduledExecutorService);

Ec2MetadataAsyncClient build();

/**
* Define the http client used by the Ec2 Metadata client. If provided, the Ec2MetadataClient will <em>NOT</em> manage the
* lifetime if the httpClient and must therefore be closed explicitly by calling the {@link SdkAsyncHttpClient#close()}
* method on it.
*
* @param httpClient the http client
* @return a reference to this builder
*/
Builder httpClient(SdkAsyncHttpClient httpClient);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@

package software.amazon.awssdk.imds;

import java.net.URI;
import java.time.Duration;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.imds.internal.DefaultEc2MetadataClient;
import software.amazon.awssdk.utils.SdkAutoCloseable;

Expand All @@ -37,12 +36,7 @@ public interface Ec2MetadataClient extends SdkAutoCloseable {
MetadataResponse get(String path);

/**
* @return The Builder Object consisting all the fields.
*/
Ec2MetadataClient.Builder toBuilder();

/**
* Create an {@code Ec2Metadata} using the default values.
* Create an {@link Ec2MetadataClient} instance using the default values.
*/
static Ec2MetadataClient create() {
return builder().build();
Expand All @@ -51,57 +45,23 @@ static Ec2MetadataClient create() {
/**
* Creates a default builder for {@link Ec2MetadataClient}.
*/
static Ec2MetadataClient.Builder builder() {
static Builder builder() {
return DefaultEc2MetadataClient.builder();
}

/**
* The builder definition for a {@link Ec2MetadataClient}.
*/
interface Builder {

/**
* Define the retry policy which includes the number of retry attempts for any failed request.
*
* @param retryPolicy The retry policy which includes the number of retry attempts for any failed request.
* @return Returns a reference to this builder
*/
Builder retryPolicy(Ec2MetadataRetryPolicy retryPolicy);

/**
* Define the endpoint of IMDS.
*
* @param endpoint The endpoint of IMDS.
* @return Returns a reference to this builder
*/
Builder endpoint(URI endpoint);
interface Builder extends Ec2MetadataClientBuilder<Ec2MetadataClient.Builder, Ec2MetadataClient> {

/**
* Define the Time to live (TTL) of the token.
*
* @param tokenTtl The Time to live (TTL) of the token.
* @return Returns a reference to this builder
*/
Builder tokenTtl(Duration tokenTtl);

/**
* Define the endpoint mode of IMDS. Supported values include IPv4 and IPv6.
*
* @param endpointMode The endpoint mode of IMDS.Supported values include IPv4 and IPv6.
* @return Returns a reference to this builder
*/
Builder endpointMode(EndpointMode endpointMode);

/**
* Define the SdkHttpClient instance to make the http requests.
*
* @param httpClient The SdkHttpClient instance to make the http requests.
* @return Returns a reference to this builder
* Define the http client used by the Ec2 Metadata client. If provided, the Ec2MetadataClient will <em>NOT</em> manage the
* lifetime if the httpClient and must therefore be closed explicitly by calling the {@link SdkAsyncHttpClient#close()}
* method on it.
* @param httpClient the http client
* @return a reference to this builder
*/
Builder httpClient(SdkHttpClient httpClient);

Ec2MetadataClient build();

}

}
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.
*
* @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.
*
* @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.
*
* @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.
*
* @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();

}
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());
}
}

}
Loading