Skip to content
Open
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 @@ -35,6 +35,7 @@ public final class UserAgentConfigurator {
public static final String VERSION_PROPERTIES_FILENAME = "version.properties";
public static final String AWS_EXECUTION_ENV = "AWS_EXECUTION_ENV";
private static final String SDK_USER_AGENT_APP_ID = "sdk.ua.appId";
private static final String SDK_USER_AGENT_APP_ID_ENV = "AWS_SDK_UA_APP_ID";
private static final Logger LOG = LoggerFactory.getLogger(UserAgentConfigurator.class);
private static final String NO_OP = "no-op";
private static final String POWERTOOLS_VERSION = getProjectVersion();
Expand Down Expand Up @@ -82,13 +83,17 @@ static String getVersionFromProperties(String propertyFileName, String versionKe

/**
* Configures the AWS SDK to use Powertools user agent by setting the sdk.ua.appId system property.
* Preserves any user-provided value and replaces any existing Powertools user agent.
* Preserves any user-provided value (from the sdk.ua.appId system property or, if unset, the AWS_SDK_UA_APP_ID
* environment variable) and replaces any existing Powertools user agent.
* Enforces a 50 character limit to comply with AWS SDK recommendations.
* This should be called during library initialization to ensure the user agent is properly configured.
*/
public static void configureUserAgent(String ptFeature) {
try {
String existingValue = System.getProperty(SDK_USER_AGENT_APP_ID);
if (existingValue == null || existingValue.isEmpty()) {
existingValue = System.getenv(SDK_USER_AGENT_APP_ID_ENV);
}
String powertoolsUserAgent = getUserAgent(ptFeature);
String newValue;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@
}

@Test
void testConfigureUserAgent_WithExistingUserValue() {

Check warning on line 134 in powertools-common/src/test/java/software/amazon/lambda/powertools/common/internal/UserAgentConfiguratorTest.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Replace these 3 tests with a single Parameterized one.

See more on https://sonarcloud.io/project/issues?id=aws-powertools_powertools-lambda-java&issues=AaIDi91xFqfB9fmDeCd_&open=AaIDi91xFqfB9fmDeCd_&pullRequest=2762
System.setProperty("sdk.ua.appId", "UserValueABC123");
UserAgentConfigurator.configureUserAgent("test-feature");

Expand All @@ -139,6 +139,26 @@
.isEqualTo("UserValueABC123/PT/TEST-FEATURE/" + VERSION + " PTENV/NA");
}

@Test
@SetEnvironmentVariable(key = "AWS_SDK_UA_APP_ID", value = "UserEnvABC123")
void testConfigureUserAgent_WithExistingEnvVariable() {
System.clearProperty("sdk.ua.appId");
UserAgentConfigurator.configureUserAgent("test-feature");

assertThat(System.getProperty("sdk.ua.appId"))
.isEqualTo("UserEnvABC123/PT/TEST-FEATURE/" + VERSION + " PTENV/NA");
}

@Test
@SetEnvironmentVariable(key = "AWS_SDK_UA_APP_ID", value = "UserEnvValue")
void testConfigureUserAgent_SystemPropertyTakesPrecedenceOverEnv() {
System.setProperty("sdk.ua.appId", "UserPropValue");
UserAgentConfigurator.configureUserAgent("test-feature");

assertThat(System.getProperty("sdk.ua.appId"))
.isEqualTo("UserPropValue/PT/TEST-FEATURE/" + VERSION + " PTENV/NA");
}

@Test
void testConfigureUserAgent_ReplacePowertoolsUserAgent() {
System.setProperty("sdk.ua.appId", "PT/BATCH/" + VERSION + " PTENV/NA");
Expand Down