From ca18ad22f0c78d2d8f69efda2c65feac74f440da Mon Sep 17 00:00:00 2001 From: Priamo Germosen <6233424+pgermosen@users.noreply.github.com> Date: Tue, 15 Sep 2026 07:35:31 -0400 Subject: [PATCH] refactor(bcmdataexports,cur): extract shared S3 destination validation (#3666) BcmDataExportsService and CurService each carried identical requireValidBucketName and requireSafeKeySegment helpers used to validate an export configuration's S3 destination. Move them into a new core.common.S3DestinationValidation utility and have both services delegate to it instead of maintaining two independent copies. Fixes #3651 --- .../core/common/S3DestinationValidation.java | 49 +++++++++++++++++++ .../bcmdataexports/BcmDataExportsService.java | 41 ++-------------- .../floci/services/cur/CurService.java | 41 ++-------------- 3 files changed, 55 insertions(+), 76 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/core/common/S3DestinationValidation.java diff --git a/src/main/java/io/github/hectorvent/floci/core/common/S3DestinationValidation.java b/src/main/java/io/github/hectorvent/floci/core/common/S3DestinationValidation.java new file mode 100644 index 0000000000..d32c5c86df --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/S3DestinationValidation.java @@ -0,0 +1,49 @@ +package io.github.hectorvent.floci.core.common; + +/** + * S3 bucket-name and key-segment validation shared by services that accept an S3 destination for + * an export configuration: BCM Data Exports and CUR both validated the same character-set and + * length rules independently before this class existed. + */ +public final class S3DestinationValidation { + + private S3DestinationValidation() { + } + + public static void requireValidBucketName(String bucket, String field) { + if (bucket.length() < 3 || bucket.length() > 63) { + throw new AwsException("ValidationException", + field + " must be between 3 and 63 characters.", 400); + } + for (int i = 0; i < bucket.length(); i++) { + char c = bucket.charAt(i); + boolean valid = (c >= 'a' && c <= 'z') + || (c >= '0' && c <= '9') + || c == '-' || c == '.'; + if (!valid) { + throw new AwsException("ValidationException", + field + " contains invalid characters.", 400); + } + } + if (bucket.startsWith("-") || bucket.endsWith("-") + || bucket.startsWith(".") || bucket.endsWith(".") + || bucket.contains("..")) { + throw new AwsException("ValidationException", + field + " is not a valid S3 bucket name.", 400); + } + } + + public static void requireSafeKeySegment(String value, String field) { + for (int i = 0; i < value.length(); i++) { + char c = value.charAt(i); + boolean ok = (c >= 'A' && c <= 'Z') + || (c >= 'a' && c <= 'z') + || (c >= '0' && c <= '9') + || c == '-' || c == '_' || c == '.' || c == '/'; + if (!ok) { + throw new AwsException("ValidationException", + field + " contains characters not permitted in an S3 key segment.", 400); + } + } + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/bcmdataexports/BcmDataExportsService.java b/src/main/java/io/github/hectorvent/floci/services/bcmdataexports/BcmDataExportsService.java index 4fb6979821..d6dcdc3d31 100644 --- a/src/main/java/io/github/hectorvent/floci/services/bcmdataexports/BcmDataExportsService.java +++ b/src/main/java/io/github/hectorvent/floci/services/bcmdataexports/BcmDataExportsService.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.S3DestinationValidation; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.bcmdataexports.model.DataQuery; @@ -295,12 +296,12 @@ public class BcmDataExportsService { } DestinationConfiguration.S3Destination s3 = dest.getS3Destination(); requireNonEmpty(s3.getS3Bucket(), "S3Destination.S3Bucket"); - requireValidBucketName(s3.getS3Bucket(), "S3Destination.S3Bucket"); + S3DestinationValidation.requireValidBucketName(s3.getS3Bucket(), "S3Destination.S3Bucket"); requireNonEmpty(s3.getS3Region(), "S3Destination.S3Region"); if (s3.getS3Prefix() == null) { s3.setS3Prefix(""); } else if (!s3.getS3Prefix().isEmpty()) { - requireSafeKeySegment(s3.getS3Prefix(), "S3Destination.S3Prefix"); + S3DestinationValidation.requireSafeKeySegment(s3.getS3Prefix(), "S3Destination.S3Prefix"); } DestinationConfiguration.S3OutputConfigurations out = s3.getS3OutputConfigurations(); if (out != null) { @@ -353,40 +354,4 @@ public class BcmDataExportsService { } } - private static void requireValidBucketName(String bucket, String field) { - if (bucket.length() < 3 || bucket.length() > 63) { - throw new AwsException("ValidationException", - field + " must be between 3 and 63 characters.", 400); - } - for (int i = 0; i < bucket.length(); i++) { - char c = bucket.charAt(i); - boolean valid = (c >= 'a' && c <= 'z') - || (c >= '0' && c <= '9') - || c == '-' || c == '.'; - if (!valid) { - throw new AwsException("ValidationException", - field + " contains invalid characters.", 400); - } - } - if (bucket.startsWith("-") || bucket.endsWith("-") - || bucket.startsWith(".") || bucket.endsWith(".") - || bucket.contains("..")) { - throw new AwsException("ValidationException", - field + " is not a valid S3 bucket name.", 400); - } - } - - private static void requireSafeKeySegment(String value, String field) { - for (int i = 0; i < value.length(); i++) { - char c = value.charAt(i); - boolean ok = (c >= 'A' && c <= 'Z') - || (c >= 'a' && c <= 'z') - || (c >= '0' && c <= '9') - || c == '-' || c == '_' || c == '.' || c == '/'; - if (!ok) { - throw new AwsException("ValidationException", - field + " contains characters not permitted in an S3 key segment.", 400); - } - } - } } diff --git a/src/main/java/io/github/hectorvent/floci/services/cur/CurService.java b/src/main/java/io/github/hectorvent/floci/services/cur/CurService.java index a1787b42a0..4d2db578f7 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cur/CurService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cur/CurService.java @@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.cur; import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.core.common.S3DestinationValidation; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.cur.model.ReportDefinition; @@ -244,9 +245,9 @@ public class CurService { requireOneOf(d.getFormat(), ALLOWED_FORMATS, "Format"); requireOneOf(d.getCompression(), ALLOWED_COMPRESSIONS, "Compression"); requireNonEmpty(d.getS3Bucket(), "S3Bucket"); - requireValidBucketName(d.getS3Bucket(), "S3Bucket"); + S3DestinationValidation.requireValidBucketName(d.getS3Bucket(), "S3Bucket"); if (d.getS3Prefix() != null) { - requireSafeKeySegment(d.getS3Prefix(), "S3Prefix"); + S3DestinationValidation.requireSafeKeySegment(d.getS3Prefix(), "S3Prefix"); } if (d.getS3Prefix() == null) { d.setS3Prefix(""); @@ -293,40 +294,4 @@ public class CurService { } } - private static void requireValidBucketName(String bucket, String field) { - if (bucket.length() < 3 || bucket.length() > 63) { - throw new AwsException("ValidationException", - field + " must be between 3 and 63 characters.", 400); - } - for (int i = 0; i < bucket.length(); i++) { - char c = bucket.charAt(i); - boolean valid = (c >= 'a' && c <= 'z') - || (c >= '0' && c <= '9') - || c == '-' || c == '.'; - if (!valid) { - throw new AwsException("ValidationException", - field + " contains invalid characters.", 400); - } - } - if (bucket.startsWith("-") || bucket.endsWith("-") - || bucket.startsWith(".") || bucket.endsWith(".") - || bucket.contains("..")) { - throw new AwsException("ValidationException", - field + " is not a valid S3 bucket name.", 400); - } - } - - private static void requireSafeKeySegment(String value, String field) { - for (int i = 0; i < value.length(); i++) { - char c = value.charAt(i); - boolean ok = (c >= 'A' && c <= 'Z') - || (c >= 'a' && c <= 'z') - || (c >= '0' && c <= '9') - || c == '-' || c == '_' || c == '.' || c == '/'; - if (!ok) { - throw new AwsException("ValidationException", - field + " contains characters not permitted in an S3 key segment.", 400); - } - } - } }