Skip to content

Commit 1b10356

Browse files
authored
Merge pull request #1562 from willbengtson/add-useragent-to-metadata-calls
aws-java-sdk-core: EC2MetadataClient.java - Adds sending of a User-Ag…
2 parents be1ea79 + c14eba0 commit 1b10356

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

aws-java-sdk-core/src/main/java/com/amazonaws/internal/EC2CredentialsUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.amazonaws.retry.internal.CredentialsEndpointRetryPolicy;
3232
import com.amazonaws.util.IOUtils;
3333
import com.amazonaws.util.json.Jackson;
34+
import com.amazonaws.util.VersionInfoUtils;
3435
import com.fasterxml.jackson.databind.JsonNode;
3536

3637
@SdkInternalApi
@@ -42,6 +43,8 @@ public final class EC2CredentialsUtils {
4243

4344
private final ConnectionUtils connectionUtils;
4445

46+
private static final String USER_AGENT = String.format("aws-sdk-java/%s", VersionInfoUtils.getVersion());
47+
4548
private EC2CredentialsUtils() {
4649
this(ConnectionUtils.getInstance());
4750
}
@@ -76,7 +79,12 @@ public static EC2CredentialsUtils getInstance() {
7679
* If the requested service is not found.
7780
*/
7881
public String readResource(URI endpoint) throws IOException {
79-
return readResource(endpoint, CredentialsEndpointRetryPolicy.NO_RETRY, new HashMap<String, String>());
82+
Map<String, String> headers = new HashMap<String, String>();
83+
headers.put("User-Agent", USER_AGENT);
84+
headers.put("Accept", "*/*");
85+
headers.put("Connection", "keep-alive");
86+
87+
return readResource(endpoint, CredentialsEndpointRetryPolicy.NO_RETRY, headers);
8088
}
8189

8290
/**

aws-java-sdk-core/src/main/java/com/amazonaws/internal/EC2MetadataClient.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import com.amazonaws.SdkClientException;
2828
import com.amazonaws.util.EC2MetadataUtils;
29+
import com.amazonaws.util.VersionInfoUtils;
2930

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

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

47+
/** User-Agent for requests to the metadata service **/
48+
private static final String USER_AGENT = String.format("aws-sdk-java/%s", VersionInfoUtils.getVersion());
49+
4650
/**
4751
* Connects to the Amazon EC2 Instance Metadata Service to retrieve the
4852
* default credential information (if any).
@@ -91,6 +95,7 @@ public String readResource(String resourcePath) throws IOException, SdkClientExc
9195
connection.setReadTimeout(1000 * 5);
9296
connection.setRequestMethod("GET");
9397
connection.setDoOutput(true);
98+
connection.addRequestProperty("User-Agent", USER_AGENT);
9499
connection.connect();
95100

96101
return readResponse(connection);

0 commit comments

Comments
 (0)