fix(cloudformation): clear dropped descriptions and the trail key prefix (#7884)

This commit is contained in:
Oleksandr Nikolaichuk authored and GitHub committed 2026-11-11 09:17:46 -04:00
1 parent 3a5610d90d
commit 55bb5c0f6e
24 files changed
+1594 -72

No files matched your search

@@ -70,6 +70,9 @@ public class CloudTrailCfnProvisioner implements CfnResourceProvisioner {
trail = trailService.updateTrail(ctx.region(), previousPhysicalId, s3BucketName, s3KeyPrefix,
snsTopicName, includeGlobalServiceEvents, isMultiRegionTrail, enableLogFileValidation,
isOrganizationTrail);
if (s3KeyPrefix == null && ctx.droppedProperties(props).contains("S3KeyPrefix")) {
trail = trailService.replaceTrailS3KeyPrefix(ctx.region(), previousPhysicalId, null);
}
applySelectors(resolved, ctx.region(), previousPhysicalId);
if (resolved.has("IsLogging")) {
if (resolved.path("IsLogging").asBoolean()) {
@@ -95,6 +95,9 @@ public class CognitoUserPoolGroupCfnProvisioner implements CfnResourceProvisione
cognitoService.createGroup(userPoolId, groupName, description, precedence, roleArn);
} else {
cognitoService.updateGroup(userPoolId, groupName, description, precedence, roleArn);
if (description == null && ctx.droppedProperties(props).contains("Description")) {
cognitoService.replaceGroupDescription(userPoolId, groupName, null);
}
}
r.setPhysicalId(groupName);
@@ -151,6 +151,9 @@ public class LambdaVersionAliasCfnProvisioner implements CfnResourceProvisioner
// LambdaService#updateAlias while null keeps what is there.
alias = lambdaService.updateAlias(ctx.region(), functionName, aliasName,
functionVersion, description, routingConfig != null ? routingConfig : Map.of());
if (description == null && ctx.droppedProperties(props).contains("Description")) {
alias = lambdaService.replaceAliasDescription(ctx.region(), functionName, aliasName, null);
}
}
r.setPhysicalId(alias.getAliasArn());
@@ -184,6 +184,9 @@ public class SecretsManagerCfnProvisioner implements CfnResourceProvisioner {
String region = ctx.region();
String arn = prior.getArn();
Secret secret = secretsManagerService.updateSecret(arn, description, kmsKeyId, region);
if (description == null && ctx.droppedProperties(props).contains("Description")) {
secret = secretsManagerService.replaceSecretDescription(arn, null, region);
}
String secretString = ctx.resolveOptional(props, "SecretString");
if (secretString != null && !secretString.equals(currentValue(arn, region))) {
secretsManagerService.putSecretValue(arn, secretString, null, null, region, null);
@@ -187,35 +187,66 @@ public class CloudTrailService {
Boolean includeGlobalServiceEvents, Boolean isMultiRegionTrail,
Boolean enableLogFileValidation, Boolean isOrganizationTrail,
String kmsKeyId, String cloudWatchLogsLogGroupArn, String cloudWatchLogsRoleArn) {
return rewriteTrail(region, trailNameOrArn, existing -> {
Trail updated = withSettings(existing, false, s3KeyPrefix, s3BucketName, snsTopicArn,
includeGlobalServiceEvents, isMultiRegionTrail, enableLogFileValidation, isOrganizationTrail,
kmsKeyId, cloudWatchLogsLogGroupArn, cloudWatchLogsRoleArn);
validateCloudWatchLogs(updated.cloudWatchLogsLogGroupArn(), updated.cloudWatchLogsRoleArn());
return updated;
});
}
/**
* Writes the S3 key prefix exactly as given, null included, which {@link #updateTrail} reads as
* "keep". Only a CloudFormation update that drops the prefix calls it.
*/
public Trail replaceTrailS3KeyPrefix(String region, String trailNameOrArn, String s3KeyPrefix) {
return rewriteTrail(region, trailNameOrArn, existing -> withSettings(existing, true, s3KeyPrefix,
null, null, null, null, null, null, null, null, null));
}
/** Reads the stored trail under its lock, stores what {@code change} returns for it, and returns that. */
private Trail rewriteTrail(String region, String trailNameOrArn, Function<Trail, Trail> change) {
Trail resolved = findTrailOrThrow(region, trailNameOrArn);
String key = regionKey(resolved.homeRegion(), resolved.name());
return withTrailLock(key, () -> {
CloudTrailEntry entry = store.get(key).orElseThrow(() -> new AwsException(
"TrailNotFoundException", "Unknown trail: " + trailNameOrArn, 400));
Trail existing = entry.trail();
Trail updated = new Trail(
existing.name(),
existing.trailArn(),
s3BucketName != null ? s3BucketName : existing.s3BucketName(),
s3KeyPrefix != null ? s3KeyPrefix : existing.s3KeyPrefix(),
snsTopicArn != null ? snsTopicArn : existing.snsTopicArn(),
includeGlobalServiceEvents != null ? includeGlobalServiceEvents : existing.includeGlobalServiceEvents(),
isMultiRegionTrail != null ? isMultiRegionTrail : existing.isMultiRegionTrail(),
existing.homeRegion(),
enableLogFileValidation != null ? enableLogFileValidation : existing.logFileValidationEnabled(),
existing.hasCustomEventSelectors(),
existing.hasInsightSelectors(),
isOrganizationTrail != null ? isOrganizationTrail : existing.isOrganizationTrail(),
kmsKeyId != null ? blankToNull(kmsKeyId) : existing.kmsKeyId(),
cloudWatchLogsLogGroupArn != null
? blankToNull(cloudWatchLogsLogGroupArn) : existing.cloudWatchLogsLogGroupArn(),
cloudWatchLogsRoleArn != null ? blankToNull(cloudWatchLogsRoleArn) : existing.cloudWatchLogsRoleArn());
validateCloudWatchLogs(updated.cloudWatchLogsLogGroupArn(), updated.cloudWatchLogsRoleArn());
Trail updated = change.apply(entry.trail());
store.put(key, entry.withTrail(updated));
return updated;
});
}
/**
* {@code existing} with each non-null setting replaced; an empty KMS or CloudWatch Logs value removes
* that setting. {@code replacePrefix} writes {@code s3KeyPrefix} as given, null included.
*/
private static Trail withSettings(Trail existing, boolean replacePrefix, String s3KeyPrefix,
String s3BucketName, String snsTopicArn,
Boolean includeGlobalServiceEvents, Boolean isMultiRegionTrail,
Boolean enableLogFileValidation, Boolean isOrganizationTrail,
String kmsKeyId, String cloudWatchLogsLogGroupArn,
String cloudWatchLogsRoleArn) {
return new Trail(
existing.name(),
existing.trailArn(),
s3BucketName != null ? s3BucketName : existing.s3BucketName(),
replacePrefix || s3KeyPrefix != null ? s3KeyPrefix : existing.s3KeyPrefix(),
snsTopicArn != null ? snsTopicArn : existing.snsTopicArn(),
includeGlobalServiceEvents != null ? includeGlobalServiceEvents : existing.includeGlobalServiceEvents(),
isMultiRegionTrail != null ? isMultiRegionTrail : existing.isMultiRegionTrail(),
existing.homeRegion(),
enableLogFileValidation != null ? enableLogFileValidation : existing.logFileValidationEnabled(),
existing.hasCustomEventSelectors(),
existing.hasInsightSelectors(),
isOrganizationTrail != null ? isOrganizationTrail : existing.isOrganizationTrail(),
kmsKeyId != null ? blankToNull(kmsKeyId) : existing.kmsKeyId(),
cloudWatchLogsLogGroupArn != null
? blankToNull(cloudWatchLogsLogGroupArn) : existing.cloudWatchLogsLogGroupArn(),
cloudWatchLogsRoleArn != null ? blankToNull(cloudWatchLogsRoleArn) : existing.cloudWatchLogsRoleArn());
}
public List<Trail> describeTrails(String region, List<String> trailNameOrArnList) {
if (trailNameOrArnList == null || trailNameOrArnList.isEmpty()) {
List<Trail> results = new ArrayList<>();
@@ -102,6 +102,7 @@ import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import static io.floci.aws.core.common.ReservedTags.rejectUnknownReservedTags;
@@ -3441,13 +3442,31 @@ public class CognitoService implements ResourceProvider {
public CognitoGroup updateGroup(String userPoolId, String groupName, String description,
Integer precedence, String roleArn) {
CognitoGroup group = rewriteGroup(userPoolId, groupName, stored -> {
if (description != null) stored.setDescription(description);
if (precedence != null) stored.setPrecedence(precedence);
if (roleArn != null) stored.setRoleArn(roleArn);
});
LOG.infov("Updated Cognito group: {0} in pool {1}", groupName, userPoolId);
return group;
}
/**
* Writes the group description exactly as given, null included, which {@link #updateGroup} reads
* as "keep". Only a CloudFormation update that drops the description calls it.
*/
public CognitoGroup replaceGroupDescription(String userPoolId, String groupName, String description) {
CognitoGroup group = rewriteGroup(userPoolId, groupName, stored -> stored.setDescription(description));
LOG.infov("Replaced Cognito group description: {0} in pool {1}", groupName, userPoolId);
return group;
}
/** Reads the stored group, applies {@code change} to it, stamps the modification date and stores it. */
private CognitoGroup rewriteGroup(String userPoolId, String groupName, Consumer<CognitoGroup> change) {
CognitoGroup group = getGroup(userPoolId, groupName);
if (description != null) group.setDescription(description);
if (precedence != null) group.setPrecedence(precedence);
if (roleArn != null) group.setRoleArn(roleArn);
change.accept(group);
group.setLastModifiedDate(System.currentTimeMillis() / 1000L);
groupStore.put(groupKey(userPoolId, groupName), group);
LOG.infov("Updated Cognito group: {0} in pool {1}", groupName, userPoolId);
return group;
}
@@ -78,6 +78,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@@ -3306,15 +3307,34 @@ public class LambdaService implements ResourceProvider {
public LambdaAlias updateAlias(String region, String functionName, String aliasName,
String functionVersion, String description,
Map<String, Double> routingConfig) {
return rewriteAlias(region, functionName, aliasName, functionVersion, description, (fn, alias) -> {
warmPool.validateProvisionedAliasUpdate(fn, aliasName, functionVersion);
if (functionVersion != null) alias.setFunctionVersion(functionVersion);
if (description != null) alias.setDescription(description);
if (routingConfig != null) alias.setRoutingConfig(routingConfig.isEmpty() ? null : routingConfig);
});
}
/**
* Writes the alias description exactly as given, null included, which {@link #updateAlias} reads
* as "keep". Only a CloudFormation update that drops the description calls it.
*/
public LambdaAlias replaceAliasDescription(String region, String functionName, String aliasName,
String description) {
return rewriteAlias(region, functionName, aliasName, null, description,
(fn, alias) -> alias.setDescription(description));
}
/** Validates the alias members, then under the function's lock applies {@code change} to the stored alias and saves it. */
private LambdaAlias rewriteAlias(String region, String functionName, String aliasName,
String functionVersion, String description,
BiConsumer<LambdaFunction, LambdaAlias> change) {
validateAliasName(aliasName);
validateAliasMembers(functionVersion, description);
LambdaFunction fn = getFunction(region, functionName);
synchronized (lockForConcurrencyOp(fn.getFunctionArn())) {
LambdaAlias alias = getAlias(region, functionName, aliasName);
warmPool.validateProvisionedAliasUpdate(fn, aliasName, functionVersion);
if (functionVersion != null) alias.setFunctionVersion(functionVersion);
if (description != null) alias.setDescription(description);
if (routingConfig != null) alias.setRoutingConfig(routingConfig.isEmpty() ? null : routingConfig);
change.accept(fn, alias);
alias.setLastModifiedDate(System.currentTimeMillis() / 1000L);
alias.setRevisionId(UUID.randomUUID().toString());
if (aliasStore != null) aliasStore.save(region, alias);
@@ -440,6 +440,35 @@ public class SecretsManagerService implements ResourceProvider {
}
public Secret updateSecret(String secretId, String description, String kmsKeyId, String region) {
Secret secret = rewriteSecret(secretId, kmsKeyId, region, stored -> {
if (description != null) {
stored.setDescription(description);
}
if (kmsKeyId != null) {
// AWS: "If you set this to an empty string, Secrets Manager uses the AWS managed key
// aws/secretsmanager" - which DescribeSecret reports by omitting KmsKeyId entirely.
stored.setKmsKeyId(kmsKeyId.isEmpty() ? null : kmsKeyId);
}
});
LOG.infov("Updated secret metadata: {0}", secret.getName());
return secret;
}
/**
* Writes the description exactly as given, null included, which {@link #updateSecret} reads as
* "keep". Only a CloudFormation update that drops the description calls it.
*/
public Secret replaceSecretDescription(String secretId, String description, String region) {
Secret secret = rewriteSecret(secretId, null, region, stored -> stored.setDescription(description));
LOG.infov("Replaced secret description: {0}", secret.getName());
return secret;
}
/**
* Checks the secret and {@code kmsKeyId} (null for none), then under the secret's lock applies
* {@code change} to it, stamps the change date and persists it.
*/
private Secret rewriteSecret(String secretId, String kmsKeyId, String region, Consumer<Secret> change) {
Secret secret = resolveSecret(secretId, region);
throwIfPendingDeletion(secret);
throwIfReplica(secret);
@@ -448,18 +477,10 @@ public class SecretsManagerService implements ResourceProvider {
synchronized (lockFor(secret.getArn())) {
throwIfManagedRelease(secret.getArn());
if (description != null) {
secret.setDescription(description);
}
if (kmsKeyId != null) {
// AWS: "If you set this to an empty string, Secrets Manager uses the AWS managed key
// aws/secretsmanager" - which DescribeSecret reports by omitting KmsKeyId entirely.
secret.setKmsKeyId(kmsKeyId.isEmpty() ? null : kmsKeyId);
}
change.accept(secret);
secret.setLastChangedDate(Instant.now());
persist(secret, region);
LOG.infov("Updated secret metadata: {0}", secret.getName());
return secret;
}
}