-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Shorten S3 prefix to meet the requirement of RDS export API (#4955)
* Shorten prefix Signed-off-by: Hai Yan <[email protected]> * Add unit tests Signed-off-by: Hai Yan <[email protected]> --------- Signed-off-by: Hai Yan <[email protected]>
- Loading branch information
Showing
7 changed files
with
112 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
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
34 changes: 34 additions & 0 deletions
34
...rc/main/java/org/opensearch/dataprepper/plugins/source/rds/utils/IdentifierShortener.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,34 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source.rds.utils; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Base64; | ||
|
||
public class IdentifierShortener { | ||
|
||
public static String shortenIdentifier(final String identifier, final int maxLength) { | ||
if (identifier.length() <= maxLength) { | ||
return identifier; | ||
} | ||
|
||
try { | ||
// Create SHA-256 hash | ||
MessageDigest digest = MessageDigest.getInstance("SHA-256"); | ||
byte[] encodedhash = digest.digest(identifier.getBytes(StandardCharsets.UTF_8)); | ||
|
||
// Convert byte array to Base64 string | ||
String base64Hash = Base64.getUrlEncoder().withoutPadding().encodeToString(encodedhash); | ||
|
||
// Return the first maxLength characters | ||
return base64Hash.substring(0, Math.min(base64Hash.length(), maxLength)); | ||
} catch (final NoSuchAlgorithmException e) { | ||
return identifier.substring(0, maxLength); | ||
} | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
...est/java/org/opensearch/dataprepper/plugins/source/rds/utils/IdentifierShortenerTest.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,59 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.opensearch.dataprepper.plugins.source.rds.utils; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.MockedStatic; | ||
|
||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.UUID; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.equalTo; | ||
import static org.hamcrest.Matchers.greaterThan; | ||
import static org.hamcrest.Matchers.lessThanOrEqualTo; | ||
import static org.mockito.Mockito.mockStatic; | ||
|
||
class IdentifierShortenerTest { | ||
|
||
@Test | ||
void shortenIdentifier_when_input_is_shorter_than_max_then_return_original_input() { | ||
final int maxLength = 10; | ||
final String testInput = UUID.randomUUID().toString().substring(0, maxLength); | ||
|
||
final String result = IdentifierShortener.shortenIdentifier(testInput, maxLength); | ||
|
||
assertThat(result, equalTo(testInput)); | ||
} | ||
|
||
@Test | ||
void shortenIdentifier_when_input_is_longer_than_max_then_return_shortened_result() { | ||
final int maxLength = 5; | ||
final String testInput = UUID.randomUUID().toString(); | ||
assertThat(testInput.length(), greaterThan(maxLength)); | ||
|
||
final String result = IdentifierShortener.shortenIdentifier(testInput, maxLength); | ||
|
||
assertThat(result.length(), lessThanOrEqualTo(maxLength)); | ||
} | ||
|
||
@Test | ||
void shortenIdentifier_when_NoSuchAlgorithmException_then_return_shortened_result() { | ||
final int maxLength = 5; | ||
final String testInput = UUID.randomUUID().toString(); | ||
assertThat(testInput.length(), greaterThan(maxLength)); | ||
|
||
try (MockedStatic<MessageDigest> messageDigestMockedStatic = mockStatic(MessageDigest.class)) { | ||
messageDigestMockedStatic.when(() -> MessageDigest.getInstance("SHA-256")) | ||
.thenThrow(new NoSuchAlgorithmException()); | ||
final String result = IdentifierShortener.shortenIdentifier(testInput, maxLength); | ||
assertThat(result, equalTo(testInput.substring(0, maxLength))); | ||
} | ||
|
||
|
||
} | ||
} |