+ * If not specified, defaults to {@link Executors#newScheduledThreadPool} with a default value of 3 thread in the + * pool. + *
+ * @param scheduledExecutorService the ScheduledExecutorService to use for retry attempt. + * @return a reference to this builder + */ + Builder scheduledExecutorService(ScheduledExecutorService scheduledExecutorService); + + /** + * Define the http client used by the Ec2 Metadata client. If provided, the Ec2MetadataClient will NOT manage the + * lifetime if the httpClient and must therefore be closed explicitly by calling the {@link SdkAsyncHttpClient#close()} + * method on it. + *+ * If not specified, the IMDS client will look for a SdkAsyncHttpClient class included in the classpath of the + * application and creates a new instance of that class, managed by the IMDS Client, that will be closed when the IMDS + * Client is closed. If no such class can be found, will throw a {@link SdkClientException}. + *
+ * @param httpClient the http client + * @return a reference to this builder + */ + Builder httpClient(SdkAsyncHttpClient httpClient); + } +} diff --git a/core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataClient.java b/core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataClient.java new file mode 100644 index 000000000000..23eb1a95bc23 --- /dev/null +++ b/core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataClient.java @@ -0,0 +1,73 @@ +/* + * 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 software.amazon.awssdk.annotations.SdkPublicApi; +import software.amazon.awssdk.core.exception.SdkClientException; +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; + + +/** + * Interface to represent the Ec2Metadata Client Class. Used to access instance metadata from a running instance. + */ +@SdkPublicApi +public interface Ec2MetadataClient extends SdkAutoCloseable { + + /** + * Gets the specified instance metadata value by the given path. + * @param path Input path + * @return Instance metadata value as part of MetadataResponse Object + */ + MetadataResponse get(String path); + + /** + * Create an {@link Ec2MetadataClient} instance using the default values. + */ + static Ec2MetadataClient create() { + return builder().build(); + } + + /** + * Creates a default builder for {@link Ec2MetadataClient}. + */ + static Builder builder() { + return DefaultEc2MetadataClient.builder(); + } + + /** + * The builder definition for a {@link Ec2MetadataClient}. + */ + interface Builder extends Ec2MetadataClientBuilder+ * If not specified, the IMDS client will look for a SdkHttpClient class included in the classpath of the + * application and creates a new instance of that class, managed by the IMDS Client, that will be closed when the IMDS + * Client is closed. If no such class can be found, will throw a {@link SdkClientException}. + *
+ * @param httpClient the http client + * @return a reference to this builder + */ + Builder httpClient(SdkHttpClient httpClient); + } + +} diff --git a/core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataClientBuilder.java b/core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataClientBuilder.java new file mode 100644 index 000000000000..c619ec3ba4e8 --- /dev/null +++ b/core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataClientBuilder.java @@ -0,0 +1,86 @@ +/* + * 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.core.retry.RetryMode; +import software.amazon.awssdk.core.retry.backoff.BackoffStrategy; +import software.amazon.awssdk.imds.internal.Ec2MetadataEndpointProvider; +import software.amazon.awssdk.utils.builder.SdkBuilder; + +/** + * Base shared builder interface for Ec2MetadataClient + * @param the Builder Type + * @param+ * If not specified, defaults to 3 retry attempts and a {@link BackoffStrategy#defaultStrategy()} backoff strategy} that + * uses {@link RetryMode#STANDARD} + *
+ * @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. + *+ * If not specified, the IMDS client will attempt to automatically resolve the endpoint value + * based on the logic of {@link Ec2MetadataEndpointProvider}. + *
+ * @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. The token represents a logical session. The TTL specifies the length of time + * that the token is valid and, therefore, the duration of the session. Defaults to 21,600 seconds (6 hours) if not specified. + * + * @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. Used to determine the endpoint of the IMDS + * Client only if {@link Ec2MetadataClientBuilder#endpoint(URI)} is not specified. Only one of both endpoint or endpoint mode + * but not both. If both are specified in the Builder, a {@link IllegalArgumentException} will be thrown. + *+ * If not specified, the IMDS client will attempt to automatically resolve the endpoint mode value + * based on the logic of {@link Ec2MetadataEndpointProvider}. + *
+ * + * @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(); + +} diff --git a/core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataRetryPolicy.java b/core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataRetryPolicy.java index 1870bf107712..7ac1ea3afb22 100644 --- a/core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataRetryPolicy.java +++ b/core/imds/src/main/java/software/amazon/awssdk/imds/Ec2MetadataRetryPolicy.java @@ -34,12 +34,14 @@ @SdkPublicApi public class Ec2MetadataRetryPolicy implements ToCopyableBuilder