-
Notifications
You must be signed in to change notification settings - Fork 232
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
Add IMDSv2 support to AwsCandidateHarvester #294
Closed
Closed
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -21,6 +21,8 @@ | |
|
||
import java.io.*; | ||
import java.net.*; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.logging.*; | ||
|
||
/** | ||
|
@@ -52,10 +54,28 @@ public class AwsCandidateHarvester | |
= "http://169.254.169.254/latest/meta-data/local-ipv4"; | ||
|
||
/** | ||
* The URL to use to test whether we are running on Amazon EC2. | ||
* The URL to get IMDSv2 API token for further meta-data requests. | ||
*/ | ||
private static final String EC2_TEST_URL | ||
= "http://169.254.169.254/latest/meta-data/"; | ||
private static final String IMDS_API_TOKEN_URL | ||
= "http://169.254.169.254/latest/api/token"; | ||
|
||
/** | ||
* The HTTP header name to provide session token. | ||
*/ | ||
private static final String EC2_METADATA_TOKEN_HEADER | ||
= "X-aws-ec2-metadata-token"; | ||
|
||
/** | ||
* The HTTP header name to request session token TTL. | ||
*/ | ||
private static final String EC2_METADATA_TOKEN_TTL_HEADER | ||
= "X-aws-ec2-metadata-token-ttl-seconds"; | ||
|
||
/** | ||
* The default session token TTL value. | ||
*/ | ||
private static final String EC2_METADATA_TOKEN_DEFAULT_TTL | ||
= "21600"; | ||
|
||
/** | ||
* Whether we are running on Amazon EC2. | ||
|
@@ -103,8 +123,11 @@ private static synchronized void obtainEC2Addresses() | |
|
||
try | ||
{ | ||
localIPStr = fetch(LOCAL_IP_URL); | ||
publicIPStr = fetch(PUBLIC_IP_URL); | ||
String metaDataToken = fetch(IMDS_API_TOKEN_URL, | ||
Collections.singletonMap(EC2_METADATA_TOKEN_TTL_HEADER, EC2_METADATA_TOKEN_DEFAULT_TTL), "PUT"); | ||
Map<String, String> tokenHeader = Collections.singletonMap(EC2_METADATA_TOKEN_HEADER, metaDataToken); | ||
localIPStr = fetch(LOCAL_IP_URL, tokenHeader); | ||
publicIPStr = fetch(PUBLIC_IP_URL, tokenHeader); | ||
|
||
//now let's cross our fingers and hope that what we got above are | ||
//real IP addresses | ||
|
@@ -183,11 +206,10 @@ private static boolean doTestEc2() | |
{ | ||
try | ||
{ | ||
URLConnection conn = new URL(EC2_TEST_URL).openConnection(); | ||
conn.setConnectTimeout(500); //don't hang for too long | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you bring back the short timeout please? |
||
conn.getContent(); | ||
String metaDataToken = fetch(IMDS_API_TOKEN_URL, | ||
Collections.singletonMap(EC2_METADATA_TOKEN_TTL_HEADER, EC2_METADATA_TOKEN_DEFAULT_TTL), "PUT"); | ||
|
||
return true; | ||
return metaDataToken != null; | ||
} | ||
catch(Exception exc) | ||
{ | ||
|
@@ -201,14 +223,38 @@ private static boolean doTestEc2() | |
* | ||
* @param url the URL we'd like to open and query. | ||
* | ||
* @param headers the HTTP headers to put into the request. | ||
* | ||
* @throws Exception if anything goes wrong. | ||
*/ | ||
private static String fetch(String url, Map<String, String> headers) | ||
throws Exception | ||
{ | ||
return fetch(url, headers, "GET"); | ||
} | ||
|
||
/** | ||
* Retrieves the content at the specified <tt>url</tt>. No more, no less. | ||
* | ||
* @param url the URL we'd like to open and query. | ||
* | ||
* @param headers the HTTP headers to put into the request. | ||
* | ||
* @param method the HTTP method we'd like to use. | ||
* | ||
* @return the String we retrieved from the URL. | ||
* | ||
* @throws Exception if anything goes wrong. | ||
*/ | ||
private static String fetch(String url) | ||
private static String fetch(String url, Map<String, String> headers, String method) | ||
throws Exception | ||
{ | ||
URLConnection conn = new URL(url).openConnection(); | ||
HttpURLConnection conn = (HttpURLConnection) (new URL(url).openConnection()); | ||
for (Map.Entry<String, String> header : headers.entrySet()) | ||
{ | ||
conn.setRequestProperty(header.getKey(), header.getValue()); | ||
} | ||
conn.setRequestMethod(method); | ||
BufferedReader in = new BufferedReader(new InputStreamReader( | ||
conn.getInputStream(), "UTF-8")); | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change this to a
*
import please