mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor(core): share one UUID-shape check across services (#8446)
This commit is contained in:
1 parent
f5e08d5e9c
commit
b8c08f1be4
14 files changed
+126
-30
No files matched your search
@@ -0,0 +1,28 @@
|
||||
package io.floci.aws.core.common;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* The canonical 8-4-4-4-12 UUID text form, tested as a shape only: no version or variant is read.
|
||||
* Which case a service accepts is part of its wire behaviour, so the two checks are not interchangeable.
|
||||
*/
|
||||
public final class UuidShapes {
|
||||
|
||||
private static final Pattern EITHER_CASE = Pattern.compile(
|
||||
"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}");
|
||||
private static final Pattern LOWER_CASE = Pattern.compile(
|
||||
"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
|
||||
|
||||
private UuidShapes() {
|
||||
}
|
||||
|
||||
/** Whether the whole value is 8-4-4-4-12 hex digits of either case. A null value is not. */
|
||||
public static boolean matchesEitherCase(String value) {
|
||||
return value != null && EITHER_CASE.matcher(value).matches();
|
||||
}
|
||||
|
||||
/** Whether the whole value is 8-4-4-4-12 lower-case hex digits. A null value is not. */
|
||||
public static boolean matchesLowerCase(String value) {
|
||||
return value != null && LOWER_CASE.matcher(value).matches();
|
||||
}
|
||||
}
|
||||
+2
-1
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.UuidShapes;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.bcmpricingcalculator.model.WorkloadEstimate;
|
||||
@@ -216,7 +217,7 @@ public class BcmPricingCalculatorService {
|
||||
}
|
||||
|
||||
private static void validateId(String id) {
|
||||
if (id == null || !id.matches("[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}")) {
|
||||
if (!UuidShapes.matchesLowerCase(id)) {
|
||||
throw validation("identifier is invalid.");
|
||||
}
|
||||
}
|
||||
|
||||
+3
-2
@@ -7,6 +7,7 @@ import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.UuidShapes;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.bedrockagentcorecontrol.model.Gateway;
|
||||
@@ -87,7 +88,7 @@ public class BedrockAgentCoreGatewayRuleService {
|
||||
|
||||
public ObjectNode get(String gatewayId, String ruleId, String region) {
|
||||
gatewayService.get(gatewayId, region);
|
||||
if (ruleId == null || !ruleId.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) {
|
||||
if (!UuidShapes.matchesLowerCase(ruleId)) {
|
||||
throw new AwsException("ValidationException", "ruleId does not satisfy the required UUID pattern", 400);
|
||||
}
|
||||
return storage.get(key(region, gatewayId, ruleId))
|
||||
@@ -252,7 +253,7 @@ public class BedrockAgentCoreGatewayRuleService {
|
||||
if (!arn.matches("arn:" + AwsArnUtils.PARTITION_REGEX + ":bedrock-agentcore:[a-z0-9-]+:[0-9]{12}:configuration-bundle/[a-zA-Z][a-zA-Z0-9-_]{0,99}-[a-zA-Z0-9]{10}")) {
|
||||
throw new AwsException("ValidationException", field + ".bundleArn is invalid", 400);
|
||||
}
|
||||
if (!version.matches("[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}")) {
|
||||
if (!UuidShapes.matchesLowerCase(version)) {
|
||||
throw new AwsException("ValidationException", field + ".bundleVersion is invalid", 400);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.Resettable;
|
||||
import io.floci.aws.core.common.UuidShapes;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.budgets.model.BudgetActionRecord;
|
||||
@@ -32,7 +33,6 @@ import java.util.regex.Pattern;
|
||||
@ApplicationScoped
|
||||
public class BudgetsService implements Resettable {
|
||||
private static final Pattern ACCOUNT_ID = Pattern.compile("\\d{12}");
|
||||
private static final Pattern ACTION_ID = Pattern.compile("^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$");
|
||||
private static final Pattern IAM_POLICY_ARN = Pattern.compile("^arn:" + AwsArnUtils.PARTITION_REGEX + ":iam::(?:\\d{12}|aws):policy/.+$");
|
||||
private static final Pattern SCP_POLICY_ID = Pattern.compile("^p-[0-9a-zA-Z_]{8,128}$");
|
||||
private static final Pattern SCP_TARGET_ID = Pattern.compile("^(?:ou-[0-9a-z]{4,32}-[a-z0-9]{8,32}|\\d{12})$");
|
||||
@@ -834,7 +834,7 @@ public class BudgetsService implements Resettable {
|
||||
}
|
||||
String budgetName = requireBudgetNameValue(value.substring(0, actionMarker));
|
||||
String actionId = value.substring(actionMarker + "/action/".length());
|
||||
if (!ACTION_ID.matcher(actionId).matches()) {
|
||||
if (!UuidShapes.matchesEitherCase(actionId)) {
|
||||
throw invalid("ResourceARN contains an invalid action ID.");
|
||||
}
|
||||
return new ResourceRef(account, budgetName, actionId);
|
||||
@@ -861,7 +861,7 @@ public class BudgetsService implements Resettable {
|
||||
|
||||
private static String requireActionId(JsonNode request) {
|
||||
String actionId = text(request, "ActionId");
|
||||
if (actionId == null || !ACTION_ID.matcher(actionId).matches()) {
|
||||
if (!UuidShapes.matchesEitherCase(actionId)) {
|
||||
throw invalid("ActionId is invalid.");
|
||||
}
|
||||
return actionId;
|
||||
|
||||
@@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.UuidShapes;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.controltower.model.EnabledControl;
|
||||
@@ -152,7 +153,7 @@ public class ControlTowerControlService {
|
||||
}
|
||||
|
||||
public synchronized ControlOperation operation(String accountId, String region, String operationIdentifier) {
|
||||
if (operationIdentifier == null || !operationIdentifier.matches("[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}")) {
|
||||
if (!UuidShapes.matchesLowerCase(operationIdentifier)) {
|
||||
throw validation("operationIdentifier must be a UUID.");
|
||||
}
|
||||
ControlOperation operation = operations.get(operationKey(accountId, region, operationIdentifier));
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.UuidShapes;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.controltower.model.EnabledBaseline;
|
||||
@@ -731,8 +732,7 @@ public class ControlTowerService {
|
||||
}
|
||||
|
||||
private static void validateOperationIdentifier(String operationIdentifier) {
|
||||
if (operationIdentifier == null
|
||||
|| !operationIdentifier.matches("[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}")) {
|
||||
if (!UuidShapes.matchesLowerCase(operationIdentifier)) {
|
||||
throw validation("operationIdentifier must be a UUID.");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.config.EmulatorConfig;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.Base64Url;
|
||||
import io.floci.aws.core.common.UuidShapes;
|
||||
import io.floci.aws.services.identitystore.model.Group;
|
||||
import io.floci.aws.services.identitystore.model.Membership;
|
||||
import io.floci.aws.services.identitystore.model.User;
|
||||
@@ -70,9 +71,6 @@ public class ScimController {
|
||||
private static final Pattern PREFIXED_TENANT = Pattern.compile(
|
||||
"([0-9a-f]{10})-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
|
||||
Pattern.CASE_INSENSITIVE);
|
||||
private static final Pattern LEGACY_TENANT = Pattern.compile(
|
||||
"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}",
|
||||
Pattern.CASE_INSENSITIVE);
|
||||
|
||||
private final IdentityStoreService identityStoreService;
|
||||
private final SsoAdminService ssoAdminService;
|
||||
@@ -686,7 +684,7 @@ public class ScimController {
|
||||
Matcher prefixed = PREFIXED_TENANT.matcher(tenantId);
|
||||
if (prefixed.matches()) {
|
||||
identityStoreId = "d-" + prefixed.group(1).toLowerCase(Locale.ROOT);
|
||||
} else if (LEGACY_TENANT.matcher(tenantId).matches()) {
|
||||
} else if (UuidShapes.matchesEitherCase(tenantId)) {
|
||||
identityStoreId = tenantId.toLowerCase(Locale.ROOT);
|
||||
} else {
|
||||
throw unauthorized("Authorization header is invalid or tenant ID is incorrect.");
|
||||
|
||||
@@ -13,6 +13,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.UuidShapes;
|
||||
import io.floci.aws.services.redshift.model.Cluster;
|
||||
import io.floci.aws.services.redshiftserverless.RedshiftServerlessService;
|
||||
import io.floci.aws.services.redshiftserverless.model.Namespace;
|
||||
@@ -22,7 +23,6 @@ import jakarta.inject.Inject;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Redshift resource policies, which authorize the sources of zero-ETL integrations into a Redshift
|
||||
@@ -40,9 +40,6 @@ public class RedshiftResourcePolicies {
|
||||
private static final List<String> STATEMENT_KEY_ORDER =
|
||||
List.of("Effect", "Principal", "Action", "Resource", "Condition");
|
||||
|
||||
private static final Pattern NAMESPACE_ID =
|
||||
Pattern.compile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}");
|
||||
|
||||
private final RedshiftService redshift;
|
||||
private final RedshiftServerlessService serverless;
|
||||
private final RegionResolver regionResolver;
|
||||
@@ -109,7 +106,7 @@ public class RedshiftResourcePolicies {
|
||||
+ "Region. Change your current Region to the same Region in your resource ARN.", 400);
|
||||
}
|
||||
if ("redshift-serverless".equals(arn.service()) && arn.resource().startsWith("namespace/")) {
|
||||
if (!NAMESPACE_ID.matcher(arn.resource().substring("namespace/".length())).matches()) {
|
||||
if (!UuidShapes.matchesEitherCase(arn.resource().substring("namespace/".length()))) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"The format of the provided identifier is invalid.", 400);
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ import io.floci.aws.core.common.RegionResolver;
|
||||
import io.floci.aws.core.common.RequestContext;
|
||||
import io.floci.aws.core.common.ResteasyRequests;
|
||||
import io.floci.aws.core.common.StreamingResponses;
|
||||
import io.floci.aws.core.common.UuidShapes;
|
||||
import io.floci.aws.core.common.XmlBuilder;
|
||||
import io.floci.aws.core.common.XmlParser;
|
||||
import io.floci.aws.core.common.auth.SigV4Canonicalization;
|
||||
@@ -122,9 +123,6 @@ public class S3Controller {
|
||||
/** The errors S3Service answers a read of an object that is not there with. */
|
||||
private static final Set<String> MISSING_OBJECT_ERRORS = Set.of("NoSuchBucket", "NoSuchKey", "NoSuchVersion");
|
||||
|
||||
private static final Pattern MINTED_VERSION_ID =
|
||||
Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
|
||||
|
||||
private static final Logger LOG = Logger.getLogger(S3Controller.class);
|
||||
static final DateTimeFormatter ISO_FORMAT = DateTimeFormatter
|
||||
.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'")
|
||||
@@ -3960,7 +3958,7 @@ public class S3Controller {
|
||||
* {@code versionId=}, which a {@code @QueryParam} reads as absent.
|
||||
*/
|
||||
private void checkVersionId(String bucket, String versionId) {
|
||||
if (versionId == null || "null".equals(versionId) || MINTED_VERSION_ID.matcher(versionId).matches()
|
||||
if (versionId == null || "null".equals(versionId) || UuidShapes.matchesLowerCase(versionId)
|
||||
|| !s3Service.bucketExists(bucket)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.RequestScopes;
|
||||
import io.floci.aws.core.common.Resettable;
|
||||
import io.floci.aws.core.common.SmithyValidationErrors;
|
||||
import io.floci.aws.core.common.UuidShapes;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
@@ -38,7 +39,6 @@ import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static io.floci.aws.services.ses.SesV2Json.intMemberOrAbsent;
|
||||
|
||||
@@ -82,8 +82,6 @@ public class SesExportJobService implements Resettable {
|
||||
private static final DateTimeFormatter OBJECT_STAMP =
|
||||
DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss", Locale.ROOT).withZone(ZoneOffset.UTC);
|
||||
private static final int PRESIGNED_URL_EXPIRY_SECONDS = 3600;
|
||||
private static final Pattern JOB_ID_SHAPE = Pattern.compile(
|
||||
"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}");
|
||||
|
||||
private final StorageBackend<String, ExportJob> exportJobStore;
|
||||
private final SesSentEmailService sentEmailService;
|
||||
@@ -375,7 +373,7 @@ public class SesExportJobService implements Resettable {
|
||||
}
|
||||
|
||||
private static AwsException unknownJob(String jobId) {
|
||||
if (JOB_ID_SHAPE.matcher(jobId).matches()) {
|
||||
if (UuidShapes.matchesEitherCase(jobId)) {
|
||||
return new AwsException("NotFoundException", "Job ID: <" + jobId + "> not found", 404);
|
||||
}
|
||||
return new AwsException("BadRequestException", "Invalid JobId", 400);
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.common.Pagination;
|
||||
import io.floci.aws.core.common.Resettable;
|
||||
import io.floci.aws.core.common.StoredResourceArns;
|
||||
import io.floci.aws.core.common.UuidShapes;
|
||||
import io.floci.aws.core.storage.AccountAwareStorageBackend;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
@@ -2102,7 +2103,7 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
|
||||
|
||||
public AssignmentDeletionOperation getAssignmentDeletionOperation(String instanceArn, String requestId) {
|
||||
requireInstance(instanceArn);
|
||||
if (requestId == null || !requestId.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) {
|
||||
if (!UuidShapes.matchesLowerCase(requestId)) {
|
||||
throw validation("AccountAssignmentDeletionRequestId must be a UUID.");
|
||||
}
|
||||
return assignmentDeletionOperations.get(requestId)
|
||||
@@ -2111,7 +2112,7 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
|
||||
|
||||
public PermissionSetProvisioningOperation getPermissionSetProvisioningOperation(String instanceArn, String requestId) {
|
||||
requireInstance(instanceArn);
|
||||
if (requestId == null || !requestId.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) {
|
||||
if (!UuidShapes.matchesLowerCase(requestId)) {
|
||||
throw validation("ProvisionPermissionSetRequestId must be a UUID.");
|
||||
}
|
||||
return permissionSetProvisioningOperations.get(requestId)
|
||||
@@ -2145,7 +2146,7 @@ public class SsoAdminService implements Resettable, StoredResourceArns {
|
||||
|
||||
public AssignmentOperation getAssignmentOperation(String instanceArn, String requestId) {
|
||||
requireInstance(instanceArn);
|
||||
if (requestId == null || !requestId.matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) {
|
||||
if (!UuidShapes.matchesLowerCase(requestId)) {
|
||||
throw validation("AccountAssignmentCreationRequestId must be a UUID.");
|
||||
}
|
||||
return assignmentOperations.get(requestId).orElseThrow(() -> notFound("Assignment operation not found: " + requestId));
|
||||
|
||||
Reference in new issue
Block a user