mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(cloudformation): preserve JSON strings in EventBridge bus policies (#9630)
This commit is contained in:
1 parent
489a6ffc7d
commit
2c5363131e
4 files changed
+132
-1
No files matched your search
+21
-1
@@ -2,6 +2,7 @@ package io.floci.aws.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
@@ -713,7 +714,22 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
|
||||
if (props == null || !props.has("Policy") || props.get("Policy").isNull()) {
|
||||
return JsonNodeFactory.instance.nullNode();
|
||||
}
|
||||
return ctx.engine().resolveNode(props.get("Policy"));
|
||||
JsonNode policy = ctx.engine().resolveNode(props.get("Policy"));
|
||||
if (policy == null || policy.isNull() || policy.isMissingNode()) {
|
||||
return JsonNodeFactory.instance.nullNode();
|
||||
}
|
||||
if (policy.isTextual()) {
|
||||
try {
|
||||
policy = MAPPER.reader().with(DeserializationFeature.FAIL_ON_TRAILING_TOKENS)
|
||||
.readTree(policy.textValue());
|
||||
} catch (JsonProcessingException e) {
|
||||
throw new AwsException("ValidationError", "EventBus Policy must contain valid JSON.", 400);
|
||||
}
|
||||
}
|
||||
if (policy == null || !policy.isObject()) {
|
||||
throw new AwsException("ValidationError", "EventBus Policy must resolve to a JSON object.", 400);
|
||||
}
|
||||
return policy;
|
||||
}
|
||||
|
||||
private JsonNode parseEventBusPolicy(String policy, String source) {
|
||||
@@ -722,6 +738,10 @@ public class EventsCfnProvisioner implements CfnResourceProvisioner {
|
||||
}
|
||||
try {
|
||||
JsonNode parsed = MAPPER.readTree(policy);
|
||||
// Older stacks stored string policies with an extra layer of JSON quoting.
|
||||
if (parsed != null && parsed.isTextual()) {
|
||||
parsed = MAPPER.readTree(parsed.textValue());
|
||||
}
|
||||
return parsed != null ? parsed : JsonNodeFactory.instance.nullNode();
|
||||
} catch (Exception e) {
|
||||
throw new AwsException("InternalFailure",
|
||||
|
||||
+41
@@ -1,5 +1,8 @@
|
||||
package io.floci.aws.services.cloudformation;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.floci.aws.core.common.XmlParser;
|
||||
import io.floci.aws.testing.CfnStackWaits;
|
||||
import io.floci.aws.testing.RestAssuredJsonUtils;
|
||||
@@ -883,6 +886,44 @@ class CloudFormationEventBusIntegrationTest {
|
||||
return callEventBridge("AWSEvents.DescribeEventBus", "{\"Name\":\"" + busName + "\"}", 200).getMap("");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"object", "string", "substitution"})
|
||||
void eventBusPolicyJsonSurvivesResolutionAndNoOpUpdate(String form) throws Exception {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
String busName = "resolved-policy-bus-" + suffix;
|
||||
String stackName = "resolved-policy-stack-" + suffix;
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
ObjectNode template = (ObjectNode) mapper.readTree(eventBusWithPolicyTemplate(busName, "AllowEvents"));
|
||||
ObjectNode properties = (ObjectNode) template.path("Resources").path("Bus").path("Properties");
|
||||
JsonNode policy = properties.get("Policy");
|
||||
if ("string".equals(form)) {
|
||||
properties.put("Policy", policy.toString());
|
||||
} else if ("substitution".equals(form)) {
|
||||
((ObjectNode) policy.path("Statement").get(0)).put("Resource",
|
||||
"arn:${AWS::Partition}:events:${AWS::Region}:${AWS::AccountId}:event-bus/" + busName);
|
||||
properties.set("Policy", mapper.createObjectNode().put("Fn::Sub", policy.toString()));
|
||||
}
|
||||
String body = template.toString();
|
||||
|
||||
createStack(stackName, body);
|
||||
try {
|
||||
assertStackStatus(stackName, "CREATE_COMPLETE");
|
||||
JsonNode stored = mapper.readTree((String) describeBus(busName).get("Policy"));
|
||||
assertEquals("AllowEvents", stored.path("Statement").get(0).path("Sid").asText());
|
||||
if ("substitution".equals(form)) {
|
||||
assertEquals("arn:aws:events:us-east-1:000000000000:event-bus/" + busName,
|
||||
stored.path("Statement").get(0).path("Resource").asText());
|
||||
}
|
||||
updateStack(stackName, CfnUpdates.forceUpdate(body));
|
||||
assertStackStatus(stackName, "UPDATE_COMPLETE");
|
||||
assertEquals(stored, mapper.readTree((String) describeBus(busName).get("Policy")));
|
||||
} finally {
|
||||
deleteStack(stackName);
|
||||
CfnStackWaits.awaitStackDeleted(stackName);
|
||||
}
|
||||
callEventBridge("AWSEvents.DescribeEventBus", "{\"Name\":\"" + busName + "\"}", 400);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mutableEventBusPolicyUpdateRollsBackWithoutChangingPolicy() {
|
||||
String suffix = Long.toString(System.nanoTime(), 36);
|
||||
|
||||
+18
@@ -117,6 +117,24 @@ class EventBusProvisionOwnershipTest {
|
||||
.putPermission(anyString(), any(), any(), any(), any(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unchangedStringPolicyFromAnOlderStackCanBeUpdated() throws Exception {
|
||||
JsonNode policy = props(true).get("Policy");
|
||||
String legacyPolicy = MAPPER.writeValueAsString(policy.toString());
|
||||
when(eventBridgeService.createEventBus(eq(BUS), any(), any(), any(), eq(REGION)))
|
||||
.thenThrow(new AwsException("ResourceAlreadyExistsException", "exists", 400));
|
||||
EventBus existingBus = bus(Instant.parse("2026-01-01T00:00:00Z"));
|
||||
existingBus.setPolicy(legacyPolicy);
|
||||
when(eventBridgeService.describeEventBus(BUS, REGION)).thenReturn(existingBus);
|
||||
ObjectNode properties = MAPPER.createObjectNode().put("Name", BUS).put("Policy", policy.toString());
|
||||
|
||||
StackResource updated = provisionExisting(Map.of(MANAGED_POLICY_ATTR, legacyPolicy), properties);
|
||||
|
||||
assertEquals("CREATE_COMPLETE", updated.getStatus(), updated.getStatusReason());
|
||||
assertEquals(policy, MAPPER.readTree(updated.getAttributes().get(MANAGED_POLICY_ATTR)));
|
||||
verify(eventBridgeService, never()).putPermission(any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void changedTrackedPolicyFailsWithoutMutatingTheOwnedBus() {
|
||||
when(eventBridgeService.createEventBus(eq(BUS), any(), any(), any(), eq(REGION)))
|
||||
|
||||
+52
@@ -10,6 +10,7 @@ import io.floci.aws.services.cloudformation.model.StackResource;
|
||||
import io.floci.aws.services.eventbridge.EventBridgeService;
|
||||
import io.floci.aws.services.eventbridge.model.ApiDestination;
|
||||
import io.floci.aws.services.eventbridge.model.EcsParameters;
|
||||
import io.floci.aws.services.eventbridge.model.EventBus;
|
||||
import io.floci.aws.services.eventbridge.model.HttpParameters;
|
||||
import io.floci.aws.services.eventbridge.model.Rule;
|
||||
import io.floci.aws.services.eventbridge.model.Target;
|
||||
@@ -24,6 +25,7 @@ import org.mockito.ArgumentMatchers;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.stubbing.OngoingStubbing;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
@@ -628,6 +630,56 @@ class EventsCfnProvisionerTest {
|
||||
verify(events, never()).putPermission(any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void eventBusPolicyPreservesResolvedJson(boolean text) throws Exception {
|
||||
String policy = """
|
||||
{"Version":"2012-10-17","Statement":[{"Sid":"AllowEvents","Effect":"Allow",
|
||||
"Principal":"*","Action":"events:PutEvents","Resource":"*"}]}
|
||||
""";
|
||||
ObjectNode props = mapper.createObjectNode().put("Name", "orders-bus");
|
||||
props.set("Policy", text ? mapper.getNodeFactory().textNode(policy) : mapper.readTree(policy));
|
||||
when(events.createEventBus(eq("orders-bus"), any(), any(), any(), eq(REGION)))
|
||||
.thenReturn(new EventBus("orders-bus",
|
||||
"arn:aws:events:us-east-1:000000000000:event-bus/orders-bus", null, Instant.now()));
|
||||
|
||||
provisioner.provision(resource("AWS::Events::EventBus", "Bus"), props, ctx(null));
|
||||
|
||||
ArgumentCaptor<String> submitted = ArgumentCaptor.forClass(String.class);
|
||||
verify(events).putPermission(eq("orders-bus"), any(), any(), any(), any(), submitted.capture(), eq(REGION));
|
||||
assertEquals(mapper.readTree(policy), mapper.readTree(submitted.getValue()));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"", "{", "null", "[]", "true", "\"policy\"", "{} {}"})
|
||||
void eventBusRejectsInvalidResolvedPolicyBeforeCreatingTheBus(String policy) {
|
||||
ObjectNode props = mapper.createObjectNode().put("Name", "orders-bus").put("Policy", policy);
|
||||
|
||||
AwsException failure = assertThrows(AwsException.class, () -> provisioner.provision(
|
||||
resource("AWS::Events::EventBus", "Bus"), props, ctx(null)));
|
||||
|
||||
assertEquals("ValidationError", failure.getErrorCode());
|
||||
verify(events, never()).createEventBus(any(), any(), any(), any(), any());
|
||||
verify(events, never()).putPermission(any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void eventBusAllowsPolicyOmittedByResolution() {
|
||||
ObjectNode props = mapper.createObjectNode().put("Name", "orders-bus");
|
||||
props.set("Policy", mapper.createObjectNode().put("Ref", "AWS::NoValue"));
|
||||
ProvisionContext context = ctx(null);
|
||||
when(context.engine().resolveNode(props.get("Policy"))).thenReturn(mapper.nullNode());
|
||||
when(events.createEventBus(eq("orders-bus"), any(), any(), any(), eq(REGION)))
|
||||
.thenReturn(new EventBus("orders-bus",
|
||||
"arn:aws:events:us-east-1:000000000000:event-bus/orders-bus", null, Instant.now()));
|
||||
StackResource bus = resource("AWS::Events::EventBus", "Bus");
|
||||
|
||||
provisioner.provision(bus, props, context);
|
||||
|
||||
assertEquals("orders-bus", bus.getPhysicalId());
|
||||
verify(events, never()).putPermission(any(), any(), any(), any(), any(), any(), any());
|
||||
}
|
||||
|
||||
// ── the policy statement id is held to the modeled pattern ────────────────
|
||||
|
||||
@Test
|
||||
|
||||
Reference in new issue
Block a user