Skip to content
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

Reduce the chance of bucket name collisions in S3BucketUtils.temporaryBucketName. #4954

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -66,9 +66,11 @@ public static String temporaryBucketName(Class<?> clz) {
* @return an s3 bucket name
*/
public static String temporaryBucketName(String prefix) {
String shortenedUserName = shortenIfNeeded(USER_NAME.getStringValue().orElse("unknown"), 7);
String bucketName =
lowerCase(prefix) + "-" + lowerCase(shortenedUserName) + "-" + RANDOM.nextInt(10000);
String currentTimeMillis = Long.toString(System.currentTimeMillis());
int suffixLength = 12;

String suffix = currentTimeMillis.substring(currentTimeMillis.length() - suffixLength);
String bucketName = lowerCase(prefix) + "-" + suffix;
if (bucketName.length() > 63) {
logger.error(() -> "S3 buckets can only be 63 chars in length, try a shorter prefix");
throw new RuntimeException("S3 buckets can only be 63 chars in length, try a shorter prefix");
Expand All @@ -79,8 +81,4 @@ public static String temporaryBucketName(String prefix) {
private static String shortenClassName(String clzName) {
return clzName.length() <= 45 ? clzName : clzName.substring(0, 45);
}

private static String shortenIfNeeded(String str, int length) {
return str.length() <= length ? str : str.substring(0, length);
}
}