mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): name an S3 bucket from BucketNamePrefix in the account regional namespace (#8467)
This commit is contained in:
1 parent
696eeda8e0
commit
e3f18da59e
3 files changed
+121
-7
No files matched your search
+45
-4
@@ -24,6 +24,7 @@ import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* Provisions {@code AWS::S3::Bucket} and {@code AWS::S3::BucketPolicy}.
|
||||
@@ -33,7 +34,8 @@ import java.util.Set;
|
||||
* are validated here before the service stores their XML. A property the template does not declare is left alone, except that
|
||||
* an update removing {@code NotificationConfiguration} or {@code OwnershipControls} clears it.
|
||||
*
|
||||
* <p>Covered on {@code AWS::S3::Bucket}: {@code BucketName}, {@code CorsConfiguration},
|
||||
* <p>Covered on {@code AWS::S3::Bucket}: {@code BucketName}, {@code BucketNamePrefix} in the
|
||||
* {@code account-regional} {@code BucketNamespace}, {@code CorsConfiguration},
|
||||
* {@code VersioningConfiguration}, {@code PublicAccessBlockConfiguration},
|
||||
* {@code BucketEncryption}, {@code LifecycleConfiguration},
|
||||
* {@code OwnershipControls}, {@code NotificationConfiguration} and
|
||||
@@ -60,6 +62,8 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
|
||||
private static final String BUCKET = "AWS::S3::Bucket";
|
||||
private static final String BUCKET_POLICY = "AWS::S3::BucketPolicy";
|
||||
private static final int BUCKET_NAME_MAX_LENGTH = 63;
|
||||
private static final String ACCOUNT_REGIONAL_NAMESPACE = "account-regional";
|
||||
private static final Pattern BUCKET_NAME_PREFIX_CHARS = Pattern.compile("[a-z0-9][a-z0-9.-]*");
|
||||
|
||||
private final S3Service s3Service;
|
||||
|
||||
@@ -88,14 +92,51 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The name the template fixes: {@code BucketName}, or {@code BucketNamePrefix} expanded in the
|
||||
* account regional namespace to {@code <prefix>-<accountId>-<region>-an}. Null when it fixes none.
|
||||
*/
|
||||
private static String templateBucketName(JsonNode props, ProvisionContext ctx) {
|
||||
String name = ctx.resolveOptional(props, "BucketName");
|
||||
String prefix = ctx.resolveOptional(props, "BucketNamePrefix");
|
||||
if (prefix == null || prefix.isBlank()) {
|
||||
return name;
|
||||
}
|
||||
if (name != null && !name.isBlank()) {
|
||||
throw new AwsException("ValidationError",
|
||||
BUCKET + " cannot set both BucketName and BucketNamePrefix", 400);
|
||||
}
|
||||
if (!ACCOUNT_REGIONAL_NAMESPACE.equals(ctx.resolveOptional(props, "BucketNamespace"))) {
|
||||
throw new AwsException("ValidationError",
|
||||
BUCKET + " BucketNamePrefix requires BucketNamespace " + ACCOUNT_REGIONAL_NAMESPACE, 400);
|
||||
}
|
||||
// AWS: "The name can only consist of lowercase letters, numbers, periods (.), and hyphens (-).
|
||||
// Bucket names must begin and end with a letter or number and cannot contain two adjacent periods."
|
||||
if (!BUCKET_NAME_PREFIX_CHARS.matcher(prefix).matches() || prefix.contains("..")) {
|
||||
throw new AwsException("ValidationError", BUCKET + " BucketNamePrefix " + prefix
|
||||
+ " must use only lowercase letters, numbers, periods and hyphens, start with a letter"
|
||||
+ " or number and not contain two adjacent periods", 400);
|
||||
}
|
||||
String derived = prefix + "-" + ctx.accountId() + "-" + ctx.region() + "-an";
|
||||
if (derived.length() > BUCKET_NAME_MAX_LENGTH) {
|
||||
throw new AwsException("ValidationError", BUCKET + " BucketNamePrefix " + prefix + " makes the name "
|
||||
+ derived + ", longer than " + BUCKET_NAME_MAX_LENGTH + " characters", 400);
|
||||
}
|
||||
return derived;
|
||||
}
|
||||
|
||||
private void provisionBucket(StackResource r, JsonNode props, ProvisionContext ctx) {
|
||||
Map<String, String> attributesBefore = Map.copyOf(r.getAttributes());
|
||||
String explicitName = ctx.resolveOptional(props, "BucketName");
|
||||
// The engine's change decides, as it does for the change set's Replacement: BucketName,
|
||||
// BucketNamePrefix and BucketNamespace are create-only in the registry. A bucket named in the
|
||||
// template cannot be replaced under that name; an unnamed one gets a fresh generated name.
|
||||
boolean replaces = ctx.isUpdate() && ctx.priorPhysicalId() != null
|
||||
&& ctx.changes(BUCKET, props).requiresReplacement();
|
||||
boolean updatesExisting = ctx.isUpdate() && ctx.priorPhysicalId() != null;
|
||||
boolean replaces = updatesExisting && ctx.changes(BUCKET, props).requiresReplacement();
|
||||
// An update that keeps the bucket does not decide its name, so a prefix it carries is not
|
||||
// derived or refused: one created before the prefix was read has a generated name.
|
||||
String explicitName = updatesExisting && !replaces
|
||||
? ctx.resolveOptional(props, "BucketName")
|
||||
: templateBucketName(props, ctx);
|
||||
if (replaces && explicitName != null && explicitName.equals(ctx.priorPhysicalId())) {
|
||||
throw CfnExceptions.customNamedReplacement(explicitName);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user