mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): carry a template's Object Lock onto its bucket (#9618)
Signed-off-by: Matheus Oliveira <matheus.6148@gmail.com>
This commit is contained in:
1 parent
1b7ddb1101
commit
ccd76cbec8
5 files changed
+381
-7
No files matched your search
+81
-5
@@ -38,13 +38,14 @@ import java.util.regex.Pattern;
|
|||||||
* {@code account-regional} {@code BucketNamespace}, {@code CorsConfiguration},
|
* {@code account-regional} {@code BucketNamespace}, {@code CorsConfiguration},
|
||||||
* {@code VersioningConfiguration}, {@code PublicAccessBlockConfiguration},
|
* {@code VersioningConfiguration}, {@code PublicAccessBlockConfiguration},
|
||||||
* {@code BucketEncryption}, {@code LifecycleConfiguration},
|
* {@code BucketEncryption}, {@code LifecycleConfiguration},
|
||||||
* {@code OwnershipControls}, {@code NotificationConfiguration} and
|
* {@code OwnershipControls}, {@code NotificationConfiguration}, {@code ObjectLockEnabled},
|
||||||
* {@code Tags}. {@code NotificationConfiguration} is the exception to the XML translation: its
|
* {@code ObjectLockConfiguration} and {@code Tags}. {@code NotificationConfiguration} is the
|
||||||
* service method takes the parsed model, so the provisioner builds that model instead.
|
* exception to the XML translation: its service method takes the parsed model, so the provisioner
|
||||||
|
* builds that model instead.
|
||||||
*
|
*
|
||||||
* <p><b>Silently dropped</b>, each of which has a service method waiting for it:
|
* <p><b>Silently dropped</b>, each of which has a service method waiting for it:
|
||||||
* {@code WebsiteConfiguration}, {@code LoggingConfiguration},
|
* {@code WebsiteConfiguration}, {@code LoggingConfiguration},
|
||||||
* {@code ObjectLockConfiguration}, {@code ReplicationConfiguration},
|
* {@code ReplicationConfiguration},
|
||||||
* {@code AccelerateConfiguration}, {@code AccessControl}, and the four numbered configuration
|
* {@code AccelerateConfiguration}, {@code AccessControl}, and the four numbered configuration
|
||||||
* lists ({@code AnalyticsConfigurations}, {@code IntelligentTieringConfigurations},
|
* lists ({@code AnalyticsConfigurations}, {@code IntelligentTieringConfigurations},
|
||||||
* {@code InventoryConfigurations}, {@code MetricsConfigurations}). Declaring one of these still
|
* {@code InventoryConfigurations}, {@code MetricsConfigurations}). Declaring one of these still
|
||||||
@@ -61,6 +62,7 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
|
|||||||
|
|
||||||
private static final String BUCKET = "AWS::S3::Bucket";
|
private static final String BUCKET = "AWS::S3::Bucket";
|
||||||
private static final String BUCKET_POLICY = "AWS::S3::BucketPolicy";
|
private static final String BUCKET_POLICY = "AWS::S3::BucketPolicy";
|
||||||
|
private static final String RETENTION_POINTER = "#/ObjectLockConfiguration/Rule/DefaultRetention";
|
||||||
private static final int BUCKET_NAME_MAX_LENGTH = 63;
|
private static final int BUCKET_NAME_MAX_LENGTH = 63;
|
||||||
private static final String ACCOUNT_REGIONAL_NAMESPACE = "account-regional";
|
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 static final Pattern BUCKET_NAME_PREFIX_CHARS = Pattern.compile("[a-z0-9][a-z0-9.-]*");
|
||||||
@@ -155,17 +157,26 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
|
|||||||
// Built before anything is created, so a configuration that fails validation creates nothing.
|
// Built before anything is created, so a configuration that fails validation creates nothing.
|
||||||
NotificationConfiguration notifications = notificationConfiguration(props, ctx);
|
NotificationConfiguration notifications = notificationConfiguration(props, ctx);
|
||||||
String ownershipControlsXml = ownershipControlsXml(props, ctx);
|
String ownershipControlsXml = ownershipControlsXml(props, ctx);
|
||||||
|
boolean objectLockEnabledOnCreate = objectLockEnabled(props, ctx);
|
||||||
if (ctx.reusesPriorEntity(bucketName)) {
|
if (ctx.reusesPriorEntity(bucketName)) {
|
||||||
r.getAttributes().put(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR,
|
r.getAttributes().put(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR,
|
||||||
s3Service.snapshotBucketConfiguration(bucketName));
|
s3Service.snapshotBucketConfiguration(bucketName));
|
||||||
}
|
}
|
||||||
if (!ctx.reusesPriorEntity(bucketName)) {
|
if (!ctx.reusesPriorEntity(bucketName)) {
|
||||||
// In us-east-1 CreateBucket also answers for a bucket the account already owns,
|
// In us-east-1 CreateBucket also answers for a bucket the account already owns,
|
||||||
// which this resource must never delete.
|
// which this resource must never delete. Its settings are captured before anything
|
||||||
|
// changes them, so a later failure of this provision puts them back instead of
|
||||||
|
// leaving a bucket the template did not create with a configuration it declared.
|
||||||
boolean owned = s3Service.ownsBucket(bucketName);
|
boolean owned = s3Service.ownsBucket(bucketName);
|
||||||
|
if (owned) {
|
||||||
|
unwind.preOwnedSnapshot = s3Service.snapshotBucketConfiguration(bucketName);
|
||||||
|
}
|
||||||
s3Service.createBucket(bucketName, ctx.region());
|
s3Service.createBucket(bucketName, ctx.region());
|
||||||
unwind.created = !owned;
|
unwind.created = !owned;
|
||||||
replacementCreated = !owned;
|
replacementCreated = !owned;
|
||||||
|
if (objectLockEnabledOnCreate) {
|
||||||
|
s3Service.enableObjectLockOnCreate(bucketName);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Ahead of the other properties, so a destination S3 cannot validate fails the resource
|
// Ahead of the other properties, so a destination S3 cannot validate fails the resource
|
||||||
// before any of them has changed the bucket. An unchanged configuration is not put again:
|
// before any of them has changed the bucket. An unchanged configuration is not put again:
|
||||||
@@ -179,6 +190,7 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
|
|||||||
}
|
}
|
||||||
applyBucketCorsConfiguration(bucketName, props, ctx);
|
applyBucketCorsConfiguration(bucketName, props, ctx);
|
||||||
applyBucketVersioningConfiguration(bucketName, props, ctx);
|
applyBucketVersioningConfiguration(bucketName, props, ctx);
|
||||||
|
applyObjectLockConfiguration(bucketName, props, ctx, objectLockEnabledOnCreate);
|
||||||
applyPublicAccessBlockConfiguration(bucketName, props, ctx);
|
applyPublicAccessBlockConfiguration(bucketName, props, ctx);
|
||||||
applyBucketEncryption(bucketName, props, ctx);
|
applyBucketEncryption(bucketName, props, ctx);
|
||||||
applyLifecycleConfiguration(bucketName, props, ctx);
|
applyLifecycleConfiguration(bucketName, props, ctx);
|
||||||
@@ -263,6 +275,66 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The template's {@code ObjectLockEnabled: true}, which {@code enableObjectLockOnCreate} applies
|
||||||
|
* beside CreateBucket: it sets versioning {@code Enabled} and the lock in one step, as
|
||||||
|
* {@code create-bucket --object-lock-enabled-for-bucket} does.
|
||||||
|
*/
|
||||||
|
private static boolean objectLockEnabled(JsonNode props, ProvisionContext ctx) {
|
||||||
|
return Boolean.parseBoolean(ctx.resolveOptional(props, "ObjectLockEnabled"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The Object Lock configuration the template declares, through the same
|
||||||
|
* {@code PutObjectLockConfiguration} call the API takes. A create with
|
||||||
|
* {@code ObjectLockEnabled: true} was already enabled beside CreateBucket, so only an update
|
||||||
|
* that adds the lock to a bucket it reuses lands in the configuration-less arm, and S3's
|
||||||
|
* 409 {@code InvalidBucketState} for one whose versioning is not {@code Enabled} propagates
|
||||||
|
* rather than a stack that reports a configuration the bucket does not carry. A
|
||||||
|
* {@code DefaultRetention} requires a {@code Mode} and one period, {@code Days} or {@code Years},
|
||||||
|
* and takes neither both, so an incomplete one fails the resource instead of silently storing
|
||||||
|
* a different configuration, and an update whose template declares no configuration leaves a
|
||||||
|
* retention the lock already carries alone.
|
||||||
|
*/
|
||||||
|
private void applyObjectLockConfiguration(String bucketName, JsonNode props, ProvisionContext ctx,
|
||||||
|
boolean objectLockEnabledOnCreate) {
|
||||||
|
JsonNode configuration = declared(props, "ObjectLockConfiguration");
|
||||||
|
if (configuration == null) {
|
||||||
|
// An update that adds only ObjectLockEnabled: true enables the lock on a bucket it
|
||||||
|
// reuses; one already carrying the lock keeps its default retention, which a template
|
||||||
|
// declaring no configuration has no business touching.
|
||||||
|
if (objectLockEnabledOnCreate && ctx.reusesPriorEntity(bucketName)
|
||||||
|
&& !s3Service.isObjectLockEnabled(bucketName)) {
|
||||||
|
s3Service.putObjectLockConfiguration(bucketName, null, null, 0);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!"Enabled".equals(ctx.resolveOptional(configuration, "ObjectLockEnabled"))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
JsonNode retention = configuration.path("Rule").path("DefaultRetention");
|
||||||
|
Integer days = ctx.resolveOptionalInt(retention, "Days");
|
||||||
|
Integer years = ctx.resolveOptionalInt(retention, "Years");
|
||||||
|
if (days != null && years != null) {
|
||||||
|
throw CfnPropertyTypes.modelValidation(RETENTION_POINTER + ": Days and Years cannot both be specified)");
|
||||||
|
}
|
||||||
|
String mode = ctx.resolveOptional(retention, "Mode");
|
||||||
|
if (mode == null || mode.isBlank()) {
|
||||||
|
if (days != null || years != null) {
|
||||||
|
throw CfnPropertyTypes.modelValidation(RETENTION_POINTER + ": required key [Mode] not found)");
|
||||||
|
}
|
||||||
|
} else if (days == null && years == null) {
|
||||||
|
throw CfnPropertyTypes.modelValidation(RETENTION_POINTER + ": a period is required, Days or Years)");
|
||||||
|
}
|
||||||
|
if (days != null) {
|
||||||
|
s3Service.putObjectLockConfiguration(bucketName, mode, "Days", days);
|
||||||
|
} else if (years != null) {
|
||||||
|
s3Service.putObjectLockConfiguration(bucketName, mode, "Years", years);
|
||||||
|
} else {
|
||||||
|
s3Service.putObjectLockConfiguration(bucketName, mode, null, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void applyPublicAccessBlockConfiguration(String bucketName, JsonNode props, ProvisionContext ctx) {
|
private void applyPublicAccessBlockConfiguration(String bucketName, JsonNode props, ProvisionContext ctx) {
|
||||||
JsonNode pab = declared(props, "PublicAccessBlockConfiguration");
|
JsonNode pab = declared(props, "PublicAccessBlockConfiguration");
|
||||||
if (pab == null) {
|
if (pab == null) {
|
||||||
@@ -359,6 +431,8 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
|
|||||||
private static final class Unwind {
|
private static final class Unwind {
|
||||||
private boolean created;
|
private boolean created;
|
||||||
private NotificationConfiguration replacedNotifications;
|
private NotificationConfiguration replacedNotifications;
|
||||||
|
// Settings of a bucket us-east-1's create answered for, so a failed provision restores them.
|
||||||
|
private String preOwnedSnapshot;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Restores an in-place bucket update or removes a newly created bucket after a failed provision. */
|
/** Restores an in-place bucket update or removes a newly created bucket after a failed provision. */
|
||||||
@@ -367,6 +441,8 @@ public class S3CfnProvisioner implements CfnResourceProvisioner {
|
|||||||
try {
|
try {
|
||||||
if (unwind.created) {
|
if (unwind.created) {
|
||||||
s3Service.deleteBucket(bucketName);
|
s3Service.deleteBucket(bucketName);
|
||||||
|
} else if (unwind.preOwnedSnapshot != null) {
|
||||||
|
s3Service.restoreBucketConfiguration(bucketName, unwind.preOwnedSnapshot);
|
||||||
} else if (r.getAttributes().containsKey(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR)) {
|
} else if (r.getAttributes().containsKey(CfnRollback.S3_BUCKET_UPDATE_SNAPSHOT_ATTR)) {
|
||||||
rollbackUpdate(r);
|
rollbackUpdate(r);
|
||||||
} else if (unwind.replacedNotifications != null) {
|
} else if (unwind.replacedNotifications != null) {
|
||||||
|
|||||||
@@ -3148,6 +3148,13 @@ public class S3Service implements Resettable, ResourceProvider {
|
|||||||
return bucket.getVersioningStatus();
|
return bucket.getVersioningStatus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Whether the bucket carries Object Lock, without the 404 GetObjectLockConfiguration answers when it has not. */
|
||||||
|
public boolean isObjectLockEnabled(String bucketName) {
|
||||||
|
return bucketStore.get(bucketName)
|
||||||
|
.map(Bucket::isObjectLockEnabled)
|
||||||
|
.orElse(false);
|
||||||
|
}
|
||||||
|
|
||||||
/** Captures only the bucket settings CloudFormation can change during an in-place update. */
|
/** Captures only the bucket settings CloudFormation can change during an in-place update. */
|
||||||
public String snapshotBucketConfiguration(String bucketName) {
|
public String snapshotBucketConfiguration(String bucketName) {
|
||||||
Bucket bucket = bucketStore.get(bucketName)
|
Bucket bucket = bucketStore.get(bucketName)
|
||||||
@@ -3164,6 +3171,17 @@ public class S3Service implements Resettable, ResourceProvider {
|
|||||||
snapshot.put("ownershipControlsConfiguration", bucket.getOwnershipControlsConfiguration());
|
snapshot.put("ownershipControlsConfiguration", bucket.getOwnershipControlsConfiguration());
|
||||||
snapshot.set("tags", objectMapper.valueToTree(bucket.getTags()));
|
snapshot.set("tags", objectMapper.valueToTree(bucket.getTags()));
|
||||||
snapshot.set("notificationConfiguration", objectMapper.valueToTree(bucket.getNotificationConfiguration()));
|
snapshot.set("notificationConfiguration", objectMapper.valueToTree(bucket.getNotificationConfiguration()));
|
||||||
|
snapshot.put("objectLockEnabled", bucket.isObjectLockEnabled());
|
||||||
|
ObjectLockRetention retention = bucket.getDefaultRetention();
|
||||||
|
if (retention == null) {
|
||||||
|
snapshot.putNull("objectLockDefaultRetention");
|
||||||
|
} else {
|
||||||
|
ObjectNode retentionNode = objectMapper.createObjectNode();
|
||||||
|
retentionNode.put("mode", retention.mode());
|
||||||
|
retentionNode.put("unit", retention.unit());
|
||||||
|
retentionNode.put("value", retention.value());
|
||||||
|
snapshot.set("objectLockDefaultRetention", retentionNode);
|
||||||
|
}
|
||||||
return snapshot.toString();
|
return snapshot.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3207,9 +3225,34 @@ public class S3Service implements Resettable, ResourceProvider {
|
|||||||
bucket.setOwnershipControlsConfiguration(ownership);
|
bucket.setOwnershipControlsConfiguration(ownership);
|
||||||
bucket.setTags(tags);
|
bucket.setTags(tags);
|
||||||
bucket.setNotificationConfiguration(notifications);
|
bucket.setNotificationConfiguration(notifications);
|
||||||
|
restoreObjectLock(snapshot, bucket);
|
||||||
bucketStore.put(bucketName, bucket);
|
bucketStore.put(bucketName, bucket);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restores the Object Lock state the snapshot carries. A snapshot saved before the field existed
|
||||||
|
* leaves the bucket's state as it is, and a retention is read back only when the snapshot carries
|
||||||
|
* a full one.
|
||||||
|
*/
|
||||||
|
private void restoreObjectLock(JsonNode snapshot, Bucket bucket) {
|
||||||
|
JsonNode enabled = snapshot.get("objectLockEnabled");
|
||||||
|
if (enabled == null || !enabled.isBoolean()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bucket.setObjectLockEnabled(enabled.asBoolean());
|
||||||
|
JsonNode retention = snapshot.get("objectLockDefaultRetention");
|
||||||
|
if (retention != null && !retention.isNull()) {
|
||||||
|
String mode = retention.path("mode").asText(null);
|
||||||
|
String unit = retention.path("unit").asText(null);
|
||||||
|
int value = retention.path("value").asInt(0);
|
||||||
|
if (mode != null && unit != null && value > 0) {
|
||||||
|
bucket.setDefaultRetention(new ObjectLockRetention(mode, unit, value));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
bucket.setDefaultRetention(null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static String snapshotText(JsonNode snapshot, String field) {
|
private static String snapshotText(JsonNode snapshot, String field) {
|
||||||
JsonNode value = snapshot.get(field);
|
JsonNode value = snapshot.get(field);
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
|
|||||||
+120
@@ -270,6 +270,126 @@ class CloudFormationS3BucketPropertiesIntegrationTest {
|
|||||||
.body(not(containsString("Fn::")));
|
.body(not(containsString("Fn::")));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** ObjectLockEnabled and ObjectLockConfiguration reach the bucket the template declares. */
|
||||||
|
@Test
|
||||||
|
void objectLockFollowsTheTemplate() throws InterruptedException {
|
||||||
|
String suffix = Long.toString(System.nanoTime(), 36);
|
||||||
|
String bucket = "cfn-object-lock-" + suffix;
|
||||||
|
String stackId = stack("CreateStack", "cfn-object-lock-stack-" + suffix,
|
||||||
|
objectLockTemplate(bucket, retention("GOVERNANCE", "Days", 1)));
|
||||||
|
await(stackId, "CREATE_COMPLETE");
|
||||||
|
|
||||||
|
bucketGet(bucket, "object-lock")
|
||||||
|
.body(containsString("<ObjectLockEnabled>Enabled</ObjectLockEnabled>"))
|
||||||
|
.body(containsString("<Mode>GOVERNANCE</Mode>"))
|
||||||
|
.body(containsString("<Days>1</Days>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aStackUpdateChangesTheDefaultRetention() throws InterruptedException {
|
||||||
|
String suffix = Long.toString(System.nanoTime(), 36);
|
||||||
|
String bucket = "cfn-object-lock-retention-" + suffix;
|
||||||
|
String stackName = "cfn-object-lock-retention-stack-" + suffix;
|
||||||
|
String stackId = stack("CreateStack", stackName,
|
||||||
|
objectLockTemplate(bucket, retention("GOVERNANCE", "Days", 1)));
|
||||||
|
await(stackId, "CREATE_COMPLETE");
|
||||||
|
|
||||||
|
stack("UpdateStack", stackName, objectLockTemplate(bucket, retention("COMPLIANCE", "Years", 2)));
|
||||||
|
await(stackId, "UPDATE_COMPLETE");
|
||||||
|
|
||||||
|
bucketGet(bucket, "object-lock")
|
||||||
|
.body(containsString("<Mode>COMPLIANCE</Mode>"))
|
||||||
|
.body(containsString("<Years>2</Years>"))
|
||||||
|
.body(not(containsString("<Days>")));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A retention a stack update does not declare survives the update: a tag-only one keeps it. */
|
||||||
|
@Test
|
||||||
|
void anUpdateWithoutAConfigurationKeepsARetentionTheLockCarries() throws InterruptedException {
|
||||||
|
String suffix = Long.toString(System.nanoTime(), 36);
|
||||||
|
String bucket = "cfn-object-lock-keep-" + suffix;
|
||||||
|
String stackName = "cfn-object-lock-keep-stack-" + suffix;
|
||||||
|
String template = objectLockTemplate(bucket, "");
|
||||||
|
String stackId = stack("CreateStack", stackName, template);
|
||||||
|
await(stackId, "CREATE_COMPLETE");
|
||||||
|
given().body("""
|
||||||
|
<ObjectLockConfiguration><ObjectLockEnabled>Enabled</ObjectLockEnabled>
|
||||||
|
<Rule><DefaultRetention><Mode>GOVERNANCE</Mode><Days>3</Days></DefaultRetention></Rule>
|
||||||
|
</ObjectLockConfiguration>
|
||||||
|
""")
|
||||||
|
.when().put("/" + bucket + "?object-lock").then().statusCode(200);
|
||||||
|
|
||||||
|
stack("UpdateStack", stackName, """
|
||||||
|
{ "Resources": { "Bucket": { "Type": "AWS::S3::Bucket", "Properties": {
|
||||||
|
"BucketName": "%s", "ObjectLockEnabled": true,
|
||||||
|
"Tags": [{ "Key": "marker", "Value": "after" }] } } } }
|
||||||
|
""".formatted(bucket));
|
||||||
|
await(stackId, "UPDATE_COMPLETE");
|
||||||
|
|
||||||
|
bucketGet(bucket, "object-lock")
|
||||||
|
.body(containsString("<Mode>GOVERNANCE</Mode>"))
|
||||||
|
.body(containsString("<Days>3</Days>"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A default retention without a mode is refused, as it requires both a mode and a period. */
|
||||||
|
@Test
|
||||||
|
void aDefaultRetentionWithoutAModeFailsTheCreate() throws InterruptedException {
|
||||||
|
String suffix = Long.toString(System.nanoTime(), 36);
|
||||||
|
String stackId = stack("CreateStack", "cfn-object-lock-mode-stack-" + suffix, objectLockTemplate(
|
||||||
|
"cfn-object-lock-mode-" + suffix,
|
||||||
|
", \"ObjectLockConfiguration\": { \"ObjectLockEnabled\": \"Enabled\", "
|
||||||
|
+ "\"Rule\": { \"DefaultRetention\": { \"Days\": 1 } } }"));
|
||||||
|
await(stackId, "ROLLBACK_COMPLETE");
|
||||||
|
|
||||||
|
String events = stackEvents(stackId);
|
||||||
|
assertTrue(events.contains("Model validation failed (#/ObjectLockConfiguration/Rule/DefaultRetention:"
|
||||||
|
+ " required key [Mode] not found)"), "the create failed on the missing mode: " + events);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** A later failure restores the retention the update attempted to change, as the snapshot carries it. */
|
||||||
|
@Test
|
||||||
|
void aFailedUpdateRestoresTheDefaultRetention() throws InterruptedException {
|
||||||
|
String suffix = Long.toString(System.nanoTime(), 36);
|
||||||
|
String bucket = "cfn-object-lock-rollback-" + suffix;
|
||||||
|
String stackName = "cfn-object-lock-rollback-stack-" + suffix;
|
||||||
|
String stackId = stack("CreateStack", stackName,
|
||||||
|
objectLockTemplate(bucket, retention("GOVERNANCE", "Days", 1)));
|
||||||
|
await(stackId, "CREATE_COMPLETE");
|
||||||
|
|
||||||
|
String update = """
|
||||||
|
{ "Resources": {
|
||||||
|
"Bucket": { "Type": "AWS::S3::Bucket",
|
||||||
|
"Properties": { "BucketName": "%s", "ObjectLockEnabled": true,
|
||||||
|
"ObjectLockConfiguration": { "ObjectLockEnabled": "Enabled",
|
||||||
|
"Rule": { "DefaultRetention": { "Mode": "GOVERNANCE", "Days": 7 } } } } },
|
||||||
|
"Broken": { "Type": "AWS::S3::Bucket", "DependsOn": "Bucket",
|
||||||
|
"Properties": { "BucketName": "cfn-broken-%s",
|
||||||
|
"VersioningConfiguration": { "Status": "Bogus" } } }
|
||||||
|
} }
|
||||||
|
""".formatted(bucket, suffix);
|
||||||
|
stack("UpdateStack", stackName, update);
|
||||||
|
await(stackId, "UPDATE_ROLLBACK_COMPLETE");
|
||||||
|
|
||||||
|
bucketGet(bucket, "object-lock")
|
||||||
|
.body(containsString("<Mode>GOVERNANCE</Mode>"))
|
||||||
|
.body(containsString("<Days>1</Days>"))
|
||||||
|
.body(not(containsString("<Days>7</Days>")));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String objectLockTemplate(String bucket, String objectLock) {
|
||||||
|
return """
|
||||||
|
{ "Resources": { "Bucket": { "Type": "AWS::S3::Bucket", "Properties": {
|
||||||
|
"BucketName": "%s", "ObjectLockEnabled": true%s } } } }
|
||||||
|
""".formatted(bucket, objectLock);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** The ObjectLockConfiguration property whose rule enables the lock with one default retention. */
|
||||||
|
private static String retention(String mode, String unit, int value) {
|
||||||
|
return ", \"ObjectLockConfiguration\": { \"ObjectLockEnabled\": \"Enabled\", "
|
||||||
|
+ "\"Rule\": { \"DefaultRetention\": { \"Mode\": \"" + mode + "\", \"" + unit
|
||||||
|
+ "\": " + value + " } } }";
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void aStackUpdateChangesTheLifecycleWindow() throws InterruptedException {
|
void aStackUpdateChangesTheLifecycleWindow() throws InterruptedException {
|
||||||
String suffix = Long.toString(System.nanoTime(), 36);
|
String suffix = Long.toString(System.nanoTime(), 36);
|
||||||
|
|||||||
+137
@@ -16,6 +16,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.CsvSource;
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
import org.mockito.ArgumentCaptor;
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.InOrder;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -27,9 +28,11 @@ import static org.junit.jupiter.api.Assertions.assertNull;
|
|||||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyInt;
|
||||||
import static org.mockito.ArgumentMatchers.anyString;
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
import static org.mockito.ArgumentMatchers.eq;
|
import static org.mockito.ArgumentMatchers.eq;
|
||||||
import static org.mockito.Mockito.doThrow;
|
import static org.mockito.Mockito.doThrow;
|
||||||
|
import static org.mockito.Mockito.inOrder;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
import static org.mockito.Mockito.never;
|
import static org.mockito.Mockito.never;
|
||||||
import static org.mockito.Mockito.verify;
|
import static org.mockito.Mockito.verify;
|
||||||
@@ -458,6 +461,140 @@ class S3CfnProvisionerTest {
|
|||||||
verify(s3, never()).putBucketVersioning("c", null);
|
verify(s3, never()).putBucketVersioning("c", null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void objectLockEnabledOnCreateAlsoEnablesVersioning() {
|
||||||
|
provisionBucket("""
|
||||||
|
{"BucketName": "b", "ObjectLockEnabled": true,
|
||||||
|
"ObjectLockConfiguration": {"ObjectLockEnabled": "Enabled",
|
||||||
|
"Rule": {"DefaultRetention": {"Mode": "GOVERNANCE", "Days": 1}}}}
|
||||||
|
""");
|
||||||
|
|
||||||
|
verify(s3).enableObjectLockOnCreate("b");
|
||||||
|
verify(s3).putObjectLockConfiguration("b", "GOVERNANCE", "Days", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aRetentionInYearsIsAppliedAsYears() {
|
||||||
|
provisionBucket("""
|
||||||
|
{"BucketName": "b", "ObjectLockEnabled": true,
|
||||||
|
"ObjectLockConfiguration": {"ObjectLockEnabled": "Enabled",
|
||||||
|
"Rule": {"DefaultRetention": {"Mode": "COMPLIANCE", "Years": 2}}}}
|
||||||
|
""");
|
||||||
|
|
||||||
|
verify(s3).putObjectLockConfiguration("b", "COMPLIANCE", "Years", 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void objectLockEnabledWithoutAConfigurationEnablesNothingElse() {
|
||||||
|
provisionBucket("""
|
||||||
|
{"BucketName": "b", "ObjectLockEnabled": true}
|
||||||
|
""");
|
||||||
|
|
||||||
|
verify(s3).enableObjectLockOnCreate("b");
|
||||||
|
verify(s3, never()).putObjectLockConfiguration(anyString(), any(), any(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void anUpdateAddingTheLockGoesThroughPutObjectLockConfiguration() {
|
||||||
|
updateBucket("""
|
||||||
|
{"BucketName": "b"}
|
||||||
|
""", """
|
||||||
|
{"BucketName": "b", "ObjectLockEnabled": true}
|
||||||
|
""");
|
||||||
|
|
||||||
|
verify(s3, never()).enableObjectLockOnCreate(anyString());
|
||||||
|
verify(s3).putObjectLockConfiguration("b", null, null, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void anUpdateLeavesTheRetentionAnEnabledLockCarries() {
|
||||||
|
when(s3.isObjectLockEnabled("b")).thenReturn(true);
|
||||||
|
updateBucket("""
|
||||||
|
{"BucketName": "b"}
|
||||||
|
""", """
|
||||||
|
{"BucketName": "b", "ObjectLockEnabled": true,
|
||||||
|
"Tags": [{"Key": "k", "Value": "v"}]}
|
||||||
|
""");
|
||||||
|
|
||||||
|
verify(s3, never()).enableObjectLockOnCreate(anyString());
|
||||||
|
verify(s3, never()).putObjectLockConfiguration(anyString(), any(), any(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aRetentionWithoutAModeFailsTheResource() {
|
||||||
|
assertThrows(AwsException.class, () -> provisionBucket("""
|
||||||
|
{"BucketName": "b", "ObjectLockEnabled": true,
|
||||||
|
"ObjectLockConfiguration": {"ObjectLockEnabled": "Enabled",
|
||||||
|
"Rule": {"DefaultRetention": {"Days": 7}}}}
|
||||||
|
"""));
|
||||||
|
|
||||||
|
verify(s3, never()).putObjectLockConfiguration(anyString(), any(), any(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aModeWithoutAPeriodFailsTheResource() {
|
||||||
|
assertThrows(AwsException.class, () -> provisionBucket("""
|
||||||
|
{"BucketName": "b", "ObjectLockEnabled": true,
|
||||||
|
"ObjectLockConfiguration": {"ObjectLockEnabled": "Enabled",
|
||||||
|
"Rule": {"DefaultRetention": {"Mode": "GOVERNANCE"}}}}
|
||||||
|
"""));
|
||||||
|
|
||||||
|
verify(s3, never()).putObjectLockConfiguration(anyString(), any(), any(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aRetentionWithBothPeriodsFailsTheResource() {
|
||||||
|
assertThrows(AwsException.class, () -> provisionBucket("""
|
||||||
|
{"BucketName": "b", "ObjectLockEnabled": true,
|
||||||
|
"ObjectLockConfiguration": {"ObjectLockEnabled": "Enabled",
|
||||||
|
"Rule": {"DefaultRetention": {"Mode": "GOVERNANCE", "Days": 7, "Years": 1}}}}
|
||||||
|
"""));
|
||||||
|
|
||||||
|
verify(s3, never()).putObjectLockConfiguration(anyString(), any(), any(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aFailedCreateOfABucketTheAccountOwnedIsRestored() {
|
||||||
|
when(s3.ownsBucket("b")).thenReturn(true);
|
||||||
|
doThrow(new AwsException("MalformedXML", "bad", 400)).when(s3).putBucketCors(eq("b"), anyString());
|
||||||
|
|
||||||
|
assertThrows(AwsException.class, () -> provisionBucket("""
|
||||||
|
{"BucketName": "b", "ObjectLockEnabled": true,
|
||||||
|
"ObjectLockConfiguration": {"ObjectLockEnabled": "Enabled"},
|
||||||
|
"CorsConfiguration": {"CorsRules": [{"AllowedOrigins": ["*"], "AllowedMethods": ["GET"]}]}}
|
||||||
|
"""));
|
||||||
|
|
||||||
|
InOrder order = inOrder(s3);
|
||||||
|
order.verify(s3).snapshotBucketConfiguration("b");
|
||||||
|
order.verify(s3).enableObjectLockOnCreate("b");
|
||||||
|
order.verify(s3).restoreBucketConfiguration("b", "snapshot");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void anUpdateChangesTheDefaultRetention() {
|
||||||
|
updateBucket("""
|
||||||
|
{"BucketName": "b"}
|
||||||
|
""", """
|
||||||
|
{"BucketName": "b", "ObjectLockEnabled": true,
|
||||||
|
"ObjectLockConfiguration": {"ObjectLockEnabled": "Enabled",
|
||||||
|
"Rule": {"DefaultRetention": {"Mode": "COMPLIANCE", "Years": 3}}}}
|
||||||
|
""");
|
||||||
|
|
||||||
|
verify(s3, never()).enableObjectLockOnCreate(anyString());
|
||||||
|
verify(s3).putObjectLockConfiguration("b", "COMPLIANCE", "Years", 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void aConfigurationWithoutAnEnabledLockIsNotApplied() {
|
||||||
|
provisionBucket("""
|
||||||
|
{"BucketName": "b",
|
||||||
|
"ObjectLockConfiguration": {"Rule": {"DefaultRetention": {"Mode": "GOVERNANCE", "Days": 1}}}}
|
||||||
|
""");
|
||||||
|
|
||||||
|
verify(s3, never()).enableObjectLockOnCreate(anyString());
|
||||||
|
verify(s3, never()).putObjectLockConfiguration(anyString(), any(), any(), anyInt());
|
||||||
|
}
|
||||||
|
|
||||||
private NotificationConfiguration provisionedNotifications(String json) {
|
private NotificationConfiguration provisionedNotifications(String json) {
|
||||||
ArgumentCaptor<NotificationConfiguration> config = ArgumentCaptor.forClass(NotificationConfiguration.class);
|
ArgumentCaptor<NotificationConfiguration> config = ArgumentCaptor.forClass(NotificationConfiguration.class);
|
||||||
provisionBucket(json);
|
provisionBucket(json);
|
||||||
|
|||||||
@@ -604,8 +604,6 @@ AWS::S3::Bucket LoggingConfiguration silently dropped, as the S3CfnProvisioner j
|
|||||||
AWS::S3::Bucket MetadataConfiguration accepted and ignored
|
AWS::S3::Bucket MetadataConfiguration accepted and ignored
|
||||||
AWS::S3::Bucket MetadataTableConfiguration accepted and ignored
|
AWS::S3::Bucket MetadataTableConfiguration accepted and ignored
|
||||||
AWS::S3::Bucket MetricsConfigurations silently dropped, as the S3CfnProvisioner javadoc records
|
AWS::S3::Bucket MetricsConfigurations silently dropped, as the S3CfnProvisioner javadoc records
|
||||||
AWS::S3::Bucket ObjectLockConfiguration silently dropped, as the S3CfnProvisioner javadoc records
|
|
||||||
AWS::S3::Bucket ObjectLockEnabled accepted and ignored
|
|
||||||
AWS::S3::Bucket ReplicationConfiguration silently dropped, as the S3CfnProvisioner javadoc records
|
AWS::S3::Bucket ReplicationConfiguration silently dropped, as the S3CfnProvisioner javadoc records
|
||||||
AWS::S3::Bucket WebsiteConfiguration silently dropped, as the S3CfnProvisioner javadoc records
|
AWS::S3::Bucket WebsiteConfiguration silently dropped, as the S3CfnProvisioner javadoc records
|
||||||
AWS::SES::ConfigurationSet ArchivingOptions explicitly rejected; only the Name property is currently provisioned
|
AWS::SES::ConfigurationSet ArchivingOptions explicitly rejected; only the Name property is currently provisioned
|
||||||
|
|||||||
|
Reference in new issue
Block a user