-
Notifications
You must be signed in to change notification settings - Fork 8
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
Adding new logic to send user-agent in S3 Requests #42
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 36 additions & 0 deletions
36
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,36 @@ | ||
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). | ||
// https://app.asana.com/0/1206885953994785/1207481230403504/f | ||
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; | ||
|
||
/** | ||
* Prepend hard-coded user-agent string with input string provided. | ||
* | ||
* @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")); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So far our practice was to list code TODOs to Asana tickets. I believe we have a similar item already, let me dig it up for you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might not be directly related, but similar: https://app.asana.com/0/1206885953994785/1207481230403504/f