-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding new logic to send user-agent in S3 Requests
With this commit, we will send s3connectorframework user-agent in every S3 request to help with observability. This commit also introduces ObjectClientConfiguration to let uses to prepend user-agent with custom values.
- Loading branch information
Showing
5 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
object-client/src/main/java/com/amazon/connector/s3/ObjectClientConfiguration.java
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.amazon.connector.s3; | ||
|
||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Getter; | ||
|
||
/** Configuration for {@link ObjectClient} */ | ||
@Getter | ||
@Builder | ||
@EqualsAndHashCode | ||
public class ObjectClientConfiguration { | ||
private static final String DEFAULT_USER_AGENT_PREFIX = null; | ||
|
||
/** User Agent Prefix. {@link ObjectClientConfiguration#DEFAULT_USER_AGENT_PREFIX} by default. */ | ||
@Builder.Default private String userAgentPrefix = DEFAULT_USER_AGENT_PREFIX; | ||
|
||
public static final ObjectClientConfiguration DEFAULT = | ||
ObjectClientConfiguration.builder().build(); | ||
|
||
/** | ||
* Construct {@link ObjectClientConfiguration} | ||
* | ||
* @param userAgentPrefix Prefix to prepend to ObjectClient's userAgent. | ||
*/ | ||
@Builder | ||
private ObjectClientConfiguration(String userAgentPrefix) { | ||
this.userAgentPrefix = userAgentPrefix; | ||
} | ||
} |
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
33 changes: 33 additions & 0 deletions
33
object-client/src/main/java/com/amazon/connector/s3/UserAgent.java
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.amazon.connector.s3; | ||
|
||
import java.util.Objects; | ||
import lombok.Getter; | ||
|
||
/** User-Agent to be used by ObjectClients */ | ||
@Getter | ||
public final class UserAgent { | ||
// Hard-coded user-agent string | ||
private static final String UA_STRING = "s3connectorframework"; | ||
// TODO: Get VersionInfo and append it to UA. We need to understand how to create (if we want a) | ||
// global version (for InputStream and ObjectClient). | ||
private static final String VERSION_INFO = null; | ||
/** | ||
* 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_DENYLIST_REGEX = "[() ,/:;<=>?@\\[\\]{}\\\\]"; | ||
|
||
private String userAgent = UA_STRING + ":" + VERSION_INFO; | ||
|
||
/** | ||
* @param userAgentPrefix to prepend the default user-agent string | ||
*/ | ||
public void prepend(String userAgentPrefix) { | ||
if (Objects.nonNull(userAgentPrefix)) | ||
this.userAgent = sanitizeInput(userAgentPrefix) + "/" + this.userAgent; | ||
} | ||
|
||
private static String sanitizeInput(String input) { | ||
return input == null ? "unknown" : input.replaceAll(UA_DENYLIST_REGEX, "_"); | ||
} | ||
} |
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
37 changes: 37 additions & 0 deletions
37
object-client/src/test/java/com/amazon/connector/s3/UserAgentTest.java
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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.amazon.connector.s3; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class UserAgentTest { | ||
|
||
@Test | ||
void testDefaultUserAgent() { | ||
UserAgent agent = new UserAgent(); | ||
assertNotNull(agent.getUserAgent()); | ||
} | ||
|
||
@Test | ||
void testPrependUserAgent() { | ||
UserAgent agent = new UserAgent(); | ||
agent.prepend("unit_test"); | ||
assertTrue(agent.getUserAgent().startsWith("unit_test")); | ||
} | ||
|
||
@Test | ||
void testNullPrependIsNoop() { | ||
UserAgent agent = new UserAgent(); | ||
String pre = agent.getUserAgent(); | ||
agent.prepend(null); | ||
String pos = agent.getUserAgent(); | ||
assertEquals(pre, pos); | ||
} | ||
|
||
@Test | ||
void testInvalidInputIsSanitised() { | ||
UserAgent agent = new UserAgent(); | ||
agent.prepend("unit/test"); | ||
assertTrue(agent.getUserAgent().startsWith("unit_test")); | ||
} | ||
} |