This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logic that allows some credential providers to be skippable (#39)
* Add logic that allows some credential providers to be skippable * Cache results of env utils
- Loading branch information
Showing
9 changed files
with
149 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
80 changes: 80 additions & 0 deletions
80
src/main/java/com/nike/cerberus/client/util/EnvironmentUtils.java
This file contains 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,80 @@ | ||
package com.nike.cerberus.client.util; | ||
|
||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
|
||
/** | ||
* Simple Util for determining environment metadata | ||
*/ | ||
public class EnvironmentUtils { | ||
|
||
private static final Map<String, Boolean> canGetSuccessfullyResults = new HashMap<>(); | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(EnvironmentUtils.class); | ||
|
||
/** | ||
* This endpoint is available on EC2 instances | ||
*/ | ||
private static final String INSTANCE_IDENTITY_DOCUMENT = "http://169.254.169.254/latest/dynamic/instance-identity/document"; | ||
|
||
/** | ||
* This endpoint is available on ECS Containers | ||
*/ | ||
private static final String ECS_METADATA_ENDPOINT = "http://localhost:51678/v1/metadata"; | ||
|
||
private EnvironmentUtils() { | ||
} | ||
|
||
/** | ||
* True if the current system is running in a pure EC2 environment, and not ECS. | ||
*/ | ||
public static boolean isRunningInEc2() { | ||
return hasInstanceIdentity() && ! isRunningInEcs(); | ||
} | ||
|
||
/** | ||
* True if the http://169.254.169.254/latest/dynamic/instance-identity/document endpoint | ||
* is available and returns a 2xx status code. True means we are currently running on | ||
* an Ec2 instance. This check should work both on Linux and Windows. | ||
*/ | ||
private static boolean hasInstanceIdentity() { | ||
return canGetSuccessfully(INSTANCE_IDENTITY_DOCUMENT); | ||
} | ||
|
||
/** | ||
* | ||
* @return true if this is a container in ECS | ||
*/ | ||
public static boolean isRunningInEcs() { | ||
return canGetSuccessfully(ECS_METADATA_ENDPOINT); | ||
} | ||
|
||
static boolean canGetSuccessfully(String url) { | ||
return canGetSuccessfullyResults.computeIfAbsent(url, theUrl -> { | ||
OkHttpClient httpClient = new OkHttpClient.Builder() | ||
.connectTimeout(500, MILLISECONDS) | ||
.readTimeout(500, MILLISECONDS) | ||
.writeTimeout(500, MILLISECONDS) | ||
.build(); | ||
|
||
Request request = new Request.Builder().get().url(theUrl).build(); | ||
|
||
try (Response response = httpClient.newCall(request).execute()) { | ||
if (response.isSuccessful()) { | ||
return true; | ||
} | ||
} catch (Exception e) { | ||
LOGGER.debug("Error when trying to GET {}", theUrl, e); | ||
} | ||
return false; | ||
}); | ||
} | ||
} |
This file contains 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
23 changes: 23 additions & 0 deletions
23
src/test/java/com/nike/cerberus/client/util/EnvironmentUtilsTest.java
This file contains 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,23 @@ | ||
package com.nike.cerberus.client.util; | ||
|
||
import org.junit.Test; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class EnvironmentUtilsTest { | ||
|
||
@Test | ||
public void test_that_canGetSuccessfully_can_get_google_index() { | ||
assertTrue(EnvironmentUtils.canGetSuccessfully("http://www.google.com")); | ||
assertTrue(EnvironmentUtils.canGetSuccessfully("http://www.google.com")); | ||
} | ||
|
||
@Test | ||
public void test_that_canGetSuccessfully_can_not_get_random_http_address() { | ||
assertFalse(EnvironmentUtils.canGetSuccessfully("http://" + UUID.randomUUID().toString() + ".com/" + UUID.randomUUID().toString())); | ||
} | ||
|
||
} |