-
Notifications
You must be signed in to change notification settings - Fork 909
[Default Configuration Part 2]:Implement auto mode discovery #2786
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
zoewangg
merged 4 commits into
feature/master/defaults-mode
from
zoewang/defaults-mode-2
Oct 26, 2021
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
128 changes: 128 additions & 0 deletions
128
.../java/software/amazon/awssdk/awscore/internal/defaultsmode/AutoDefaultsModeDiscovery.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,128 @@ | ||
/* | ||
* 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.awscore.internal.defaultsmode; | ||
|
||
import java.util.Optional; | ||
import software.amazon.awssdk.annotations.SdkInternalApi; | ||
import software.amazon.awssdk.core.SdkSystemSetting; | ||
import software.amazon.awssdk.defaultsmode.DefaultsMode; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.regions.internal.util.EC2MetadataUtils; | ||
import software.amazon.awssdk.utils.JavaSystemSetting; | ||
import software.amazon.awssdk.utils.OptionalUtils; | ||
import software.amazon.awssdk.utils.SystemSetting; | ||
import software.amazon.awssdk.utils.internal.SystemSettingUtils; | ||
|
||
/** | ||
* This class attempts to discover the appropriate {@link DefaultsMode} by inspecting the environment. It falls | ||
* back to the {@link DefaultsMode#STANDARD} mode if the target mode cannot be determined. | ||
*/ | ||
@SdkInternalApi | ||
public final class AutoDefaultsModeDiscovery { | ||
private static final String EC2_METADATA_REGION_PATH = "/latest/meta-data/placement/region"; | ||
private static final DefaultsMode FALLBACK_DEFAULTS_MODE = DefaultsMode.STANDARD; | ||
private static final String ANDROID_JAVA_VENDOR = "The Android Project"; | ||
private static final String AWS_DEFAULT_REGION_ENV_VAR = "AWS_DEFAULT_REGION"; | ||
|
||
/** | ||
* Discovers the defaultMode using the following workflow: | ||
* | ||
* 1. Check if it's on mobile | ||
* 2. If it's not on mobile (best we can tell), see if we can determine whether we're an in-region or cross-region client. | ||
* 3. If we couldn't figure out the region from environment variables. Check IMDSv2. This step might take up to 1 second | ||
* (default connect timeout) | ||
* 4. Finally, use fallback mode | ||
*/ | ||
public DefaultsMode discover(Region regionResolvedFromSdkClient) { | ||
|
||
if (isMobile()) { | ||
return DefaultsMode.MOBILE; | ||
} | ||
|
||
if (isAwsExecutionEnvironment()) { | ||
Optional<String> regionStr = regionFromAwsExecutionEnvironment(); | ||
|
||
if (regionStr.isPresent()) { | ||
return compareRegion(regionStr.get(), regionResolvedFromSdkClient); | ||
} | ||
} | ||
|
||
Optional<String> regionFromEc2 = queryImdsV2(); | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (regionFromEc2.isPresent()) { | ||
return compareRegion(regionFromEc2.get(), regionResolvedFromSdkClient); | ||
} | ||
|
||
return FALLBACK_DEFAULTS_MODE; | ||
} | ||
|
||
private static DefaultsMode compareRegion(String region, Region clientRegion) { | ||
if (region.equalsIgnoreCase(clientRegion.id())) { | ||
return DefaultsMode.IN_REGION; | ||
} | ||
|
||
return DefaultsMode.CROSS_REGION; | ||
} | ||
|
||
private static Optional<String> queryImdsV2() { | ||
try { | ||
String ec2InstanceRegion = EC2MetadataUtils.fetchData(EC2_METADATA_REGION_PATH, false, 1); | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// ec2InstanceRegion could be null | ||
return Optional.ofNullable(ec2InstanceRegion); | ||
} catch (Exception exception) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
/** | ||
* Check to see if the application is running on a mobile device by verifying the Java | ||
* vendor system property. Currently only checks for Android. While it's technically possible to | ||
* use Java with iOS, it's not a common use-case. | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* <p> | ||
* https://developer.android.com/reference/java/lang/System#getProperties() | ||
*/ | ||
private static boolean isMobile() { | ||
return JavaSystemSetting.JAVA_VENDOR.getStringValue() | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.filter(o -> o.equals(ANDROID_JAVA_VENDOR)) | ||
.isPresent(); | ||
} | ||
|
||
private static boolean isAwsExecutionEnvironment() { | ||
return SdkSystemSetting.AWS_EXECUTION_ENV.getStringValue().isPresent(); | ||
} | ||
|
||
private static Optional<String> regionFromAwsExecutionEnvironment() { | ||
Optional<String> regionFromRegionEnvVar = SdkSystemSetting.AWS_REGION.getStringValue(); | ||
return OptionalUtils.firstPresent(regionFromRegionEnvVar, | ||
() -> SystemSettingUtils.resolveEnvironmentVariable(new DefaultRegionEnvVar())); | ||
} | ||
|
||
private static final class DefaultRegionEnvVar implements SystemSetting { | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Override | ||
public String property() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String environmentVariable() { | ||
return AWS_DEFAULT_REGION_ENV_VAR; | ||
} | ||
|
||
@Override | ||
public String defaultValue() { | ||
return null; | ||
} | ||
} | ||
} |
232 changes: 232 additions & 0 deletions
232
.../src/test/java/software/amazon/awssdk/awscore/internal/AutoDefaultsModeDiscoveryTest.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,232 @@ | ||
/* | ||
* 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.awscore.internal; | ||
|
||
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.get; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.put; | ||
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.github.tomakehurst.wiremock.junit.WireMockRule; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.concurrent.Callable; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Parameterized; | ||
import software.amazon.awssdk.awscore.internal.defaultsmode.AutoDefaultsModeDiscovery; | ||
import software.amazon.awssdk.core.SdkSystemSetting; | ||
import software.amazon.awssdk.defaultsmode.DefaultsMode; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.regions.internal.util.EC2MetadataUtils; | ||
import software.amazon.awssdk.testutils.EnvironmentVariableHelper; | ||
import software.amazon.awssdk.utils.JavaSystemSetting; | ||
|
||
@RunWith(Parameterized.class) | ||
public class AutoDefaultsModeDiscoveryTest { | ||
private static final EnvironmentVariableHelper ENVIRONMENT_VARIABLE_HELPER = new EnvironmentVariableHelper(); | ||
@Parameterized.Parameter | ||
public TestData testData; | ||
|
||
@Parameterized.Parameters | ||
public static Collection<Object> data() { | ||
return Arrays.asList(new Object[] { | ||
|
||
// Mobile | ||
new TestData().clientRegion(Region.US_EAST_1) | ||
.javaVendorProperty("The Android Project") | ||
.awsExecutionEnvVar("AWS_Lambda_java8") | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.awsRegionEnvVar("us-east-1") | ||
.expectedResolvedMode(DefaultsMode.MOBILE), | ||
|
||
// Region available from AWS execution environment | ||
new TestData().clientRegion(Region.US_EAST_1) | ||
.awsExecutionEnvVar("AWS_Lambda_java8") | ||
.awsRegionEnvVar("us-east-1") | ||
.expectedResolvedMode(DefaultsMode.IN_REGION), | ||
|
||
// Region available from AWS execution environment | ||
new TestData().clientRegion(Region.US_EAST_1) | ||
.awsExecutionEnvVar("AWS_Lambda_java8") | ||
.awsDefaultRegionEnvVar("us-west-2") | ||
.expectedResolvedMode(DefaultsMode.CROSS_REGION), | ||
|
||
// ImdsV2 available, in-region | ||
new TestData().clientRegion(Region.US_EAST_1) | ||
.awsDefaultRegionEnvVar("us-west-2") | ||
.ec2MetadataConfig(new Ec2MetadataConfig().region("us-east-1") | ||
.imdsAvailable(true)) | ||
.expectedResolvedMode(DefaultsMode.IN_REGION), | ||
|
||
// ImdsV2 available, cross-region | ||
new TestData().clientRegion(Region.US_EAST_1) | ||
.awsDefaultRegionEnvVar("us-west-2") | ||
.ec2MetadataConfig(new Ec2MetadataConfig().region("us-west-2") | ||
.imdsAvailable(true) | ||
.ec2MetadataDisabledEnvVar("false")) | ||
.expectedResolvedMode(DefaultsMode.CROSS_REGION), | ||
|
||
// Imdsv2 disabled, should not query ImdsV2 and use fallback mode | ||
new TestData().clientRegion(Region.US_EAST_1) | ||
.awsDefaultRegionEnvVar("us-west-2") | ||
.ec2MetadataConfig(new Ec2MetadataConfig().region("us-west-2") | ||
.imdsAvailable(true) | ||
.ec2MetadataDisabledEnvVar("true")) | ||
.expectedResolvedMode(DefaultsMode.STANDARD), | ||
|
||
// Imdsv2 not available, should use fallback mode. | ||
new TestData().clientRegion(Region.US_EAST_1) | ||
.awsDefaultRegionEnvVar("us-west-2") | ||
.ec2MetadataConfig(new Ec2MetadataConfig().imdsAvailable(false)) | ||
.expectedResolvedMode(DefaultsMode.STANDARD), | ||
}); | ||
} | ||
|
||
@Rule | ||
public WireMockRule wireMock = new WireMockRule(0); | ||
|
||
@Before | ||
public void methodSetup() { | ||
System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT.property(), | ||
"http://localhost:" + wireMock.port()); | ||
} | ||
|
||
@After | ||
public void cleanUp() { | ||
EC2MetadataUtils.clearCache(); | ||
wireMock.resetAll(); | ||
ENVIRONMENT_VARIABLE_HELPER.reset(); | ||
System.clearProperty(JavaSystemSetting.JAVA_VENDOR.property()); | ||
} | ||
|
||
@Test | ||
public void differentCombinationOfConfigs_shouldResolveCorrectly() throws Exception { | ||
if (testData.javaVendorProperty != null) { | ||
System.setProperty(JavaSystemSetting.JAVA_VENDOR.property(), testData.javaVendorProperty); | ||
} | ||
|
||
if (testData.awsExecutionEnvVar != null) { | ||
ENVIRONMENT_VARIABLE_HELPER.set(SdkSystemSetting.AWS_EXECUTION_ENV.environmentVariable(), | ||
testData.awsExecutionEnvVar); | ||
} else { | ||
ENVIRONMENT_VARIABLE_HELPER.remove(SdkSystemSetting.AWS_EXECUTION_ENV.environmentVariable()); | ||
} | ||
|
||
if (testData.awsRegionEnvVar != null) { | ||
ENVIRONMENT_VARIABLE_HELPER.set(SdkSystemSetting.AWS_REGION.environmentVariable(), testData.awsRegionEnvVar); | ||
} else { | ||
ENVIRONMENT_VARIABLE_HELPER.remove(SdkSystemSetting.AWS_REGION.environmentVariable()); | ||
} | ||
|
||
if (testData.awsDefaultRegionEnvVar != null) { | ||
ENVIRONMENT_VARIABLE_HELPER.set("AWS_DEFAULT_REGION", testData.awsDefaultRegionEnvVar); | ||
} else { | ||
ENVIRONMENT_VARIABLE_HELPER.remove("AWS_DEFAULT_REGION"); | ||
} | ||
|
||
if (testData.ec2MetadataConfig != null) { | ||
if (testData.ec2MetadataConfig.ec2MetadataDisabledEnvVar != null) { | ||
ENVIRONMENT_VARIABLE_HELPER.set(SdkSystemSetting.AWS_EC2_METADATA_DISABLED.environmentVariable(), | ||
testData.ec2MetadataConfig.ec2MetadataDisabledEnvVar); | ||
} | ||
|
||
if (testData.ec2MetadataConfig.imdsAvailable) { | ||
stubSuccessfulResponse(testData.ec2MetadataConfig.region); | ||
} | ||
} | ||
|
||
Callable<DefaultsMode> result = () -> new AutoDefaultsModeDiscovery().discover(testData.clientRegion); | ||
assertThat(result.call()).isEqualTo(testData.expectedResolvedMode); | ||
} | ||
|
||
public void stubSuccessfulResponse(String region) { | ||
stubFor(put("/latest/api/token") | ||
.willReturn(aResponse().withStatus(200).withBody("token"))); | ||
|
||
stubFor(get("/latest/meta-data/placement/region") | ||
.willReturn(aResponse().withStatus(200).withBody(region))); | ||
} | ||
|
||
private static final class TestData { | ||
private Region clientRegion; | ||
private String javaVendorProperty; | ||
private String awsExecutionEnvVar; | ||
private String awsRegionEnvVar; | ||
private String awsDefaultRegionEnvVar; | ||
private Ec2MetadataConfig ec2MetadataConfig; | ||
private DefaultsMode expectedResolvedMode; | ||
|
||
public TestData clientRegion(Region clientRegion) { | ||
this.clientRegion = clientRegion; | ||
return this; | ||
} | ||
|
||
public TestData javaVendorProperty(String javaVendorProperty) { | ||
this.javaVendorProperty = javaVendorProperty; | ||
return this; | ||
} | ||
|
||
public TestData awsExecutionEnvVar(String awsExecutionEnvVar) { | ||
this.awsExecutionEnvVar = awsExecutionEnvVar; | ||
return this; | ||
} | ||
|
||
public TestData awsRegionEnvVar(String awsRegionEnvVar) { | ||
this.awsRegionEnvVar = awsRegionEnvVar; | ||
return this; | ||
} | ||
|
||
public TestData awsDefaultRegionEnvVar(String awsDefaultRegionEnvVar) { | ||
this.awsDefaultRegionEnvVar = awsDefaultRegionEnvVar; | ||
return this; | ||
} | ||
|
||
public TestData ec2MetadataConfig(Ec2MetadataConfig ec2MetadataConfig) { | ||
this.ec2MetadataConfig = ec2MetadataConfig; | ||
return this; | ||
} | ||
|
||
public TestData expectedResolvedMode(DefaultsMode expectedResolvedMode) { | ||
this.expectedResolvedMode = expectedResolvedMode; | ||
return this; | ||
} | ||
} | ||
|
||
private static final class Ec2MetadataConfig { | ||
private boolean imdsAvailable; | ||
private String region; | ||
private String ec2MetadataDisabledEnvVar; | ||
|
||
public Ec2MetadataConfig imdsAvailable(boolean imdsAvailable) { | ||
this.imdsAvailable = imdsAvailable; | ||
return this; | ||
} | ||
|
||
public Ec2MetadataConfig region(String region) { | ||
this.region = region; | ||
return this; | ||
} | ||
|
||
public Ec2MetadataConfig ec2MetadataDisabledEnvVar(String ec2MetadataDisabledEnvVar) { | ||
this.ec2MetadataDisabledEnvVar = ec2MetadataDisabledEnvVar; | ||
return this; | ||
} | ||
} | ||
} |
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,18 @@ | ||
# | ||
# 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. | ||
# | ||
|
||
# Set up logging implementation | ||
org.eclipse.jetty.util.log.class=org.eclipse.jetty.util.log.StdErrLog | ||
org.eclipse.jetty.LEVEL=OFF |
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.