mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
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
This commit is contained in:
1 parent
c7b4a393d9
commit
ca18ad22f0
3 files changed
+55
-76
No files matched your search
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-38
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user