Skip to content

Adding Java vendor to user agent and using it everywhere #1140

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 2 commits into from
Mar 19, 2019
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
5 changes: 5 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-caed172.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"category": "AWS SDK for Java v2",
"type": "feature",
"description": "Adds the Java vendor the user agent as well as using the updated user agent for all HTTP calls"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.awssdk.auth.credentials;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
Expand All @@ -30,6 +31,7 @@
import org.junit.ClassRule;
import org.junit.Test;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.internal.util.UserAgentUtils;
import software.amazon.awssdk.regions.util.ResourcesEndpointProvider;
import software.amazon.awssdk.testutils.EnvironmentVariableHelper;

Expand Down Expand Up @@ -93,6 +95,7 @@ public void testGetCredentialsReturnsValidResponseFromEcsEndpoint() {
private void stubForSuccessResponse() {
stubFor(
get(urlPathEqualTo(CREDENTIALS_PATH))
.withHeader("User-Agent", equalTo(UserAgentUtils.getUserAgent()))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package software.amazon.awssdk.auth.credentials;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
Expand All @@ -34,6 +35,7 @@
import org.junit.ClassRule;
import org.junit.Test;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.internal.util.UserAgentUtils;
import software.amazon.awssdk.regions.util.ResourcesEndpointProvider;
import software.amazon.awssdk.utils.DateUtils;
import software.amazon.awssdk.utils.IoUtils;
Expand Down Expand Up @@ -130,6 +132,7 @@ public void basicCachingFunctionalityWorks() {
private void stubForSuccessResponseWithCustomBody(String body) {
stubFor(
get(urlPathEqualTo(CREDENTIALS_PATH))
.withHeader("User-Agent", equalTo(UserAgentUtils.getUserAgent()))
.willReturn(aResponse()
.withStatus(200)
.withHeader("Content-Type", "application/json")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.util.HashMap;
import java.util.Map;
import software.amazon.awssdk.annotations.SdkProtectedApi;
import software.amazon.awssdk.core.util.VersionInfo;
import software.amazon.awssdk.core.internal.util.UserAgentUtils;

/**
* <p>
Expand Down Expand Up @@ -57,7 +57,7 @@ default ResourcesEndpointRetryPolicy retryPolicy() {
*/
default Map<String, String> headers() {
Map<String, String> requestHeaders = new HashMap<>();
requestHeaders.put("User-Agent", String.format("aws-sdk-java/%s", VersionInfo.SDK_VERSION));
requestHeaders.put("User-Agent", UserAgentUtils.getUserAgent());
requestHeaders.put("Accept", "*/*");
requestHeaders.put("Connection", "keep-alive");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.mockito.runners.MockitoJUnitRunner;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.exception.SdkServiceException;
import software.amazon.awssdk.core.internal.util.UserAgentUtils;
import software.amazon.awssdk.core.util.VersionInfo;
import software.amazon.awssdk.regions.internal.util.ConnectionUtils;
import software.amazon.awssdk.regions.internal.util.SocketUtils;
Expand All @@ -52,7 +53,7 @@ public class HttpCredentialsUtilsTest {
private static Map<String, String> headers = new HashMap<String, String>()
{
{
put("User-Agent", String.format("aws-sdk-java/%s", VersionInfo.SDK_VERSION));
put("User-Agent", UserAgentUtils.getUserAgent());
put("Accept", "*/*");
put("Connection", "keep-alive");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
public final class UserAgentUtils {

private static final String UA_STRING = "aws-sdk-{platform}/{version} {os.name}/{os.version} {java.vm.name}/{java.vm"
+ ".version} Java/{java.version}{language.and.region}{additional.languages}";
+ ".version} Java/{java.version}{language.and.region}{additional.languages} "
+ "vendor/{java.vendor}";

/** Disallowed characters in the user agent token: @see <a href="https://tools.ietf.org/html/rfc7230#section-3.2.6">RFC 7230</a> */
private static final String UA_BLACKLIST_REGEX = "[() ,/:;<=>?@\\[\\]{}\\\\]";
Expand Down Expand Up @@ -84,6 +85,7 @@ static String userAgent() {
.replace("{java.vm.name}", sanitizeInput(JavaSystemSetting.JAVA_VM_NAME.getStringValue().orElse(null)))
.replace("{java.vm.version}", sanitizeInput(JavaSystemSetting.JAVA_VM_VERSION.getStringValue().orElse(null)))
.replace("{java.version}", sanitizeInput(JavaSystemSetting.JAVA_VERSION.getStringValue().orElse(null)))
.replace("{java.vendor}", sanitizeInput(JavaSystemSetting.JAVA_VENDOR.getStringValue().orElse(null)))
.replace("{additional.languages}", getAdditionalJvmLanguages());

Optional<String> language = JavaSystemSetting.USER_LANGUAGE.getStringValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.Arrays;
import org.junit.Test;
import software.amazon.awssdk.core.internal.util.UserAgentUtils;
import software.amazon.awssdk.utils.JavaSystemSetting;

public class UserAgentUtilsTest {

Expand All @@ -31,6 +32,21 @@ public void userAgent() {
Arrays.stream(userAgent.split(" ")).forEach(str -> assertThat(isValidInput(str)).isTrue());
}

@Test
public void userAgent_HasVendor() {
System.setProperty(JavaSystemSetting.JAVA_VENDOR.property(), "finks");
String userAgent = UserAgentUtils.userAgent();
System.clearProperty(JavaSystemSetting.JAVA_VENDOR.property());
assertThat(userAgent).contains("vendor/finks");
}

@Test
public void userAgent_HasUnknownVendor() {
System.clearProperty(JavaSystemSetting.JAVA_VENDOR.property());
String userAgent = UserAgentUtils.userAgent();
assertThat(userAgent).contains("vendor/unknown");
}

private boolean isValidInput(String input) {
return input.startsWith("(") || input.contains("/") && input.split("/").length == 2;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
@SdkProtectedApi
public enum JavaSystemSetting implements SystemSetting {
JAVA_VERSION("java.version"),
JAVA_VENDOR("java.vendor"),
TEMP_DIRECTORY("java.io.tmpdir"),
JAVA_VM_NAME("java.vm.name"),
JAVA_VM_VERSION("java.vm.version"),
Expand Down