Skip to content
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

Refactor tryDetectRegion Method . Remove aws-json-protocol dependency #3324

Open
wants to merge 3 commits into
base: feature/master/imds
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 4 additions & 10 deletions core/imds/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>url-connection-client</artifactId>
<version>${awsjavasdk.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>http-client-spi</artifactId>
Expand Down Expand Up @@ -101,19 +95,19 @@
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>aws-json-protocol</artifactId>
<artifactId>test-utils</artifactId>
<version>${awsjavasdk.version}</version>
<scope>compile</scope>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>test-utils</artifactId>
<artifactId>profiles</artifactId>
<version>${awsjavasdk.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>profiles</artifactId>
<artifactId>apache-client</artifactId>
<version>${awsjavasdk.version}</version>
<scope>compile</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.core.document.Document;
import software.amazon.awssdk.protocols.json.internal.unmarshall.document.DocumentUnmarshaller;
import software.amazon.awssdk.imds.internal.unmarshall.document.DocumentUnmarshaller;
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
import software.amazon.awssdk.protocols.jsoncore.JsonNodeParser;
import software.amazon.awssdk.utils.Validate;
Expand All @@ -34,8 +32,6 @@
@SdkPublicApi
public class MetadataResponse {

private static final Logger log = LoggerFactory.getLogger(MetadataResponse.class);

private static final JsonNodeParser JSON_NODE_PARSER = JsonNode.parserBuilder().removeErrorLocations(true).build();

private final String body;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import software.amazon.awssdk.http.HttpExecuteResponse;
import software.amazon.awssdk.http.SdkHttpClient;
import software.amazon.awssdk.http.SdkHttpMethod;
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
import software.amazon.awssdk.http.apache.ApacheHttpClient;
import software.amazon.awssdk.imds.Ec2Metadata;
import software.amazon.awssdk.imds.Ec2MetadataRetryPolicy;
import software.amazon.awssdk.imds.MetadataResponse;
Expand Down Expand Up @@ -72,7 +72,7 @@ private DefaultEc2Metadata(DefaultEc2Metadata.Ec2MetadataBuilder builder) {
this.tokenTtl = builder.tokenTtl != null ? builder.tokenTtl : Duration.ofSeconds(21600);
this.endpointMode = ENDPOINT_PROVIDER.resolveEndpointMode(builder.endpointMode);
this.httpDebugOutput = builder.httpDebugOutput;
this.httpClient = builder.httpClient != null ? builder.httpClient : UrlConnectionHttpClient.create();
this.httpClient = builder.httpClient != null ? builder.httpClient : ApacheHttpClient.create();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would cause build to fail for applications that only have/use UrlConnectionHttpClient as HTTP dependency.
We should load the http client using the default chain instead of defaulting to an implementation explicitly

private static final SdkHttpServiceProvider<SdkHttpService> DEFAULT_CHAIN = new CachingSdkHttpServiceProvider<>(

}

public static Ec2Metadata.Builder builder() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.unmarshall.document;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.document.Document;
import software.amazon.awssdk.protocols.jsoncore.JsonNode;
import software.amazon.awssdk.protocols.jsoncore.JsonNodeVisitor;

@SdkInternalApi
public class DocumentUnmarshaller implements JsonNodeVisitor<Document> {
@Override
public Document visitNull() {
return Document.fromNull();
}

@Override
public Document visitBoolean(boolean bool) {
return Document.fromBoolean(bool);
}

@Override
public Document visitNumber(String number) {
return Document.fromNumber(number);
}

@Override
public Document visitString(String string) {
return Document.fromString(string);
}

@Override
public Document visitArray(List<JsonNode> array) {
return Document.fromList(array.stream()
.map(node -> node.visit(this))
.collect(Collectors.toList()));
}

@Override
public Document visitObject(Map<String, JsonNode> object) {
return Document.fromMap(object.entrySet()
.stream().collect(Collectors.toMap(entry -> entry.getKey(),
entry -> entry.getValue().visit(this),
(left, right) -> left,
LinkedHashMap::new)));
}

@Override
public Document visitEmbeddedObject(Object embeddedObject) {
throw new UnsupportedOperationException("Embedded objects are not supported within Document types.");
}
}
6 changes: 6 additions & 0 deletions core/regions/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@
<artifactId>equalsverifier</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>imds</artifactId>
<version>${awsjavasdk.version}</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@

package software.amazon.awssdk.regions.providers;

import java.io.IOException;
import java.util.Map;
import software.amazon.awssdk.annotations.SdkProtectedApi;
import software.amazon.awssdk.core.SdkSystemSetting;
import software.amazon.awssdk.core.document.Document;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.imds.Ec2Metadata;
import software.amazon.awssdk.imds.MetadataResponse;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.regions.internal.util.EC2MetadataUtils;
import software.amazon.awssdk.utils.Logger;

/**
* Attempts to load region information from the EC2 Metadata service. If the application is not
Expand All @@ -32,6 +37,14 @@
@SdkProtectedApi
public final class InstanceProfileRegionProvider implements AwsRegionProvider {

private static final Logger log = Logger.loggerFor(InstanceProfileRegionProvider.class);

private static final String REGION = "region";

private static final String EC2_DYNAMICDATA_ROOT = "/latest/dynamic/";

private static final String INSTANCE_IDENTITY_DOCUMENT = "instance-identity/document";

/**
* Cache region as it will not change during the lifetime of the JVM.
*/
Expand Down Expand Up @@ -65,6 +78,20 @@ public Region getRegion() throws SdkClientException {
}

private String tryDetectRegion() {
return EC2MetadataUtils.getEC2InstanceRegion();

Ec2Metadata ec2Metadata = Ec2Metadata.create();
MetadataResponse metadataResponse = ec2Metadata.get(EC2_DYNAMICDATA_ROOT + INSTANCE_IDENTITY_DOCUMENT);

try {
Document document = metadataResponse.asDocument();
if (document.isMap()) {
Map<String, Document> documentMap = document.asMap();
Document regionDocument = documentMap.get(REGION);
return regionDocument.asString();
}
} catch (IOException e) {
log.warn(() -> "Received IOException", e);
}
return null;
}
}