Skip to content

aws-java-sdk-core: EC2MetadataClient.java - Adds sending of a User-Ag… #1562

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
merged 3 commits into from
Apr 25, 2018
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.amazonaws.retry.internal.CredentialsEndpointRetryPolicy;
import com.amazonaws.util.IOUtils;
import com.amazonaws.util.json.Jackson;
import com.amazonaws.util.VersionInfoUtils;
import com.fasterxml.jackson.databind.JsonNode;

@SdkInternalApi
Expand All @@ -42,6 +43,8 @@ public final class EC2CredentialsUtils {

private final ConnectionUtils connectionUtils;

private static final String USER_AGENT = String.format("aws-sdk-java/%s", VersionInfoUtils.getVersion());

private EC2CredentialsUtils() {
this(ConnectionUtils.getInstance());
}
Expand Down Expand Up @@ -76,7 +79,12 @@ public static EC2CredentialsUtils getInstance() {
* If the requested service is not found.
*/
public String readResource(URI endpoint) throws IOException {
return readResource(endpoint, CredentialsEndpointRetryPolicy.NO_RETRY, new HashMap<String, String>());
Map<String, String> headers = new HashMap<String, String>();
headers.put("User-Agent", USER_AGENT);
headers.put("Accept", "*/*");
headers.put("Connection", "keep-alive");

return readResource(endpoint, CredentialsEndpointRetryPolicy.NO_RETRY, headers);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.amazonaws.SdkClientException;
import com.amazonaws.util.EC2MetadataUtils;
import com.amazonaws.util.VersionInfoUtils;

/**
* Simple client for accessing the Amazon EC2 Instance Metadata Service.
Expand All @@ -43,6 +44,9 @@ public class EC2MetadataClient {

private static final Log log = LogFactory.getLog(EC2MetadataClient.class);

/** User-Agent for requests to the metadata service **/
private static final String USER_AGENT = String.format("aws-sdk-java/%s", VersionInfoUtils.getVersion());

/**
* Connects to the Amazon EC2 Instance Metadata Service to retrieve the
* default credential information (if any).
Expand Down Expand Up @@ -91,6 +95,7 @@ public String readResource(String resourcePath) throws IOException, SdkClientExc
connection.setReadTimeout(1000 * 5);
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.addRequestProperty("User-Agent", USER_AGENT);
connection.connect();

return readResponse(connection);
Expand Down