mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(iot): provision AWS::IoT::Thing, Policy and TopicRule (#3014)
* feat(iot): provision AWS::IoT::Thing, Policy and TopicRule CloudFormation now creates, updates and deletes things, policies and topic rules through the existing IoT operations. Ref returns the name; Fn::GetAtt returns the thing ARN and id, the policy ARN and id (the name, as on AWS) and the rule ARN. Attribute payloads are replaced in place, a changed policy document becomes a new default version, a rule is replaced in place, and a rename replaces the entity. The topic rule payload is handed to the service in the API's own shape. A policy is detached from its principals before it is deleted. * test(iot): keep policy variables verbatim in a stack-created policy * fix(iot): cap a policy at five versions and prune the oldest on a stack update CreatePolicyVersion refuses a sixth version with VersionsLimitExceededException (HTTP 409), as AWS does, and every policy version write runs under one lock so racing creates cannot slip past the check. A stack update with a changed document now does what the AWS CloudFormation handler does when the service refuses: it deletes the oldest version, after moving the default to the newest when the oldest is the default, and creates the new version again. Before this change the versions of a policy updated by a stack piled up without limit. * fix(iot): keep pruning when another version fills the slot before the retry The prune and the retry are separate calls, so another client could create a version between them and the single retry failed the stack update. The provisioner now prunes and tries again, up to as many times as a policy has versions, before it gives up. * fix(iot): delete the oldest policy version in one step under the version lock A stack update pruning a full policy selected the oldest version, moved the default and deleted it in separate service calls, so a direct client could delete or promote that version in between and the stale delete failed the update. The selection, the default move and the delete are now one step in IotService under the version lock; the provisioner only asks for it and retries. * fix(iot): make room for a policy version in one step and lock the policy delete A policy persisted before the cap can hold more than five versions, and the one-version prune then needed one failed stack update per four surplus versions. The service now deletes the oldest versions until one more fits, in one step under the lock, and the provisioner asks for that. DeletePolicy takes the same lock: a version created in the middle of a delete used to put the policy back after the delete had returned. The limit error now carries the message AWS sends, and the tolerated limit error is logged at debug level. * docs(cloudformation): regenerate the resource-type table after the rebase onto main
This commit is contained in:
1 parent
8468dd1ef3
commit
dec348d382
12 files changed
+1789
-39
No files matched your search
+11
@@ -38,6 +38,7 @@ import io.github.hectorvent.floci.services.cloudformation.provisioners.EcrCfnPro
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.EcsCapacityCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.FirehoseCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.IamRoleCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.IotDomainConfigurationCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.KinesisCfnProvisioner;
|
||||
import io.github.hectorvent.floci.services.cloudformation.provisioners.KmsCfnProvisioner;
|
||||
@@ -68,6 +69,7 @@ import io.github.hectorvent.floci.services.eventbridge.EventBridgeService;
|
||||
import io.github.hectorvent.floci.services.firehose.FirehoseService;
|
||||
import io.github.hectorvent.floci.services.iam.IamService;
|
||||
import io.github.hectorvent.floci.services.iot.IotDomainConfigurationService;
|
||||
import io.github.hectorvent.floci.services.iot.IotService;
|
||||
import io.github.hectorvent.floci.services.kinesis.KinesisService;
|
||||
import io.github.hectorvent.floci.services.kms.KmsService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaLayerService;
|
||||
@@ -150,6 +152,7 @@ final class CfnProvisionerFixture {
|
||||
// dispatcher. They exist only so inferredProvisioners() can wire their provisioner.
|
||||
private FlowLogService flowLogService;
|
||||
private IotDomainConfigurationService iotDomainConfigurationService;
|
||||
private IotService iotService;
|
||||
private LambdaMicrovmsService lambdaMicrovmsService;
|
||||
private AwsConfigService awsConfigService;
|
||||
private OrganizationsService organizationsService;
|
||||
@@ -254,6 +257,9 @@ final class CfnProvisionerFixture {
|
||||
if (iotDomainConfigurationService != null) {
|
||||
discovered.add(new IotDomainConfigurationCfnProvisioner(iotDomainConfigurationService));
|
||||
}
|
||||
if (iotService != null) {
|
||||
discovered.add(new IotCfnProvisioner(iotService));
|
||||
}
|
||||
if (lambdaMicrovmsService != null) {
|
||||
discovered.add(new LambdaMicrovmsCfnProvisioner(lambdaMicrovmsService));
|
||||
}
|
||||
@@ -471,6 +477,11 @@ final class CfnProvisionerFixture {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder iot(IotService v) {
|
||||
this.iotService = v;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder lambdaMicrovms(LambdaMicrovmsService v) {
|
||||
this.lambdaMicrovmsService = v;
|
||||
return this;
|
||||
|
||||
+286
@@ -0,0 +1,286 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation;
|
||||
|
||||
import io.github.hectorvent.floci.core.common.XmlParser;
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.contains;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
/**
|
||||
* Provisions an {@code AWS::IoT::Thing}, an {@code AWS::IoT::Policy} and an
|
||||
* {@code AWS::IoT::TopicRule} through one CloudFormation stack and asserts that {@code Ref} and every
|
||||
* {@code Fn::GetAtt} attribute carry what the IoT API reports, rather than the literal
|
||||
* {@code Logical.Attr} the stub arm would leave behind. The rule's action targets a queue from the
|
||||
* same template, so a publish through the rule proves the payload reached the service in the API's
|
||||
* shape; a second action on a queue that does not exist proves that one failing action neither
|
||||
* fails the publish nor the other action, and that the error action receives the failure. Also
|
||||
* covers the in-place update of all three and that deleting the stack removes them.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class IotCfnIntegrationTest {
|
||||
|
||||
private static final String CFN_AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=test/20260903/us-east-1/cloudformation/aws4_request";
|
||||
private static final String STACK = "iot-cfn-it";
|
||||
private static final String THING = "iot-cfn-it-sensor";
|
||||
private static final String POLICY = "iot-cfn-it-policy";
|
||||
private static final String RULE = "iotCfnItRule";
|
||||
private static final String ROLE_ARN = "arn:aws:iam::000000000000:role/iot-cfn-it";
|
||||
|
||||
private static String template(String serialNumber, String policyAction, String topicFilter, String tagValue) {
|
||||
return """
|
||||
{
|
||||
"Resources": {
|
||||
"Queue": {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": "iot-cfn-it-queue"}},
|
||||
"ErrorQueue": {"Type": "AWS::SQS::Queue", "Properties": {"QueueName": "iot-cfn-it-error-queue"}},
|
||||
"Sensor": {
|
||||
"Type": "AWS::IoT::Thing",
|
||||
"Properties": {
|
||||
"ThingName": "%s",
|
||||
"AttributePayload": {"Attributes": {"SerialNumber": "%s"}}
|
||||
}
|
||||
},
|
||||
"Policy": {
|
||||
"Type": "AWS::IoT::Policy",
|
||||
"Properties": {
|
||||
"PolicyName": "%s",
|
||||
"PolicyDocument": {
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [{"Effect": "Allow", "Action": "%s", "Resource": "arn:aws:iot:us-east-1:000000000000:client/${iot:Connection.Thing.ThingName}"}]
|
||||
},
|
||||
"Tags": [{"Key": "stack", "Value": "%s"}]
|
||||
}
|
||||
},
|
||||
"Rule": {
|
||||
"Type": "AWS::IoT::TopicRule",
|
||||
"Properties": {
|
||||
"RuleName": "%s",
|
||||
"TopicRulePayload": {
|
||||
"Sql": "SELECT * FROM '%s'",
|
||||
"AwsIotSqlVersion": "2016-03-23",
|
||||
"RuleDisabled": false,
|
||||
"Actions": [
|
||||
{"Sqs": {"QueueUrl": {"Ref": "Queue"}, "RoleArn": "%s", "UseBase64": false}},
|
||||
{"Sqs": {"QueueUrl": "http://localhost:4566/000000000000/iot-cfn-it-missing-queue", "RoleArn": "%s"}}
|
||||
],
|
||||
"ErrorAction": {"Sqs": {"QueueUrl": {"Ref": "ErrorQueue"}, "RoleArn": "%s"}}
|
||||
},
|
||||
"Tags": [{"Key": "stack", "Value": "%s"}]
|
||||
}
|
||||
}
|
||||
},
|
||||
"Outputs": {
|
||||
"QueueUrl": {"Value": {"Ref": "Queue"}},
|
||||
"ErrorQueueUrl": {"Value": {"Ref": "ErrorQueue"}},
|
||||
"SensorRef": {"Value": {"Ref": "Sensor"}},
|
||||
"SensorArn": {"Value": {"Fn::GetAtt": ["Sensor", "Arn"]}},
|
||||
"SensorId": {"Value": {"Fn::GetAtt": ["Sensor", "Id"]}},
|
||||
"PolicyRef": {"Value": {"Ref": "Policy"}},
|
||||
"PolicyArn": {"Value": {"Fn::GetAtt": ["Policy", "Arn"]}},
|
||||
"PolicyId": {"Value": {"Fn::GetAtt": ["Policy", "Id"]}},
|
||||
"RuleRef": {"Value": {"Ref": "Rule"}},
|
||||
"RuleArn": {"Value": {"Fn::GetAtt": ["Rule", "Arn"]}}
|
||||
}
|
||||
}
|
||||
""".formatted(THING, serialNumber, POLICY, policyAction, tagValue, RULE, topicFilter,
|
||||
ROLE_ARN, ROLE_ARN, ROLE_ARN, tagValue);
|
||||
}
|
||||
|
||||
@Test
|
||||
void iotStackExposesRealIdentifiersUpdatesInPlaceAndDeletesTheResources() throws Exception {
|
||||
cloudFormation("CreateStack", template("SN-1", "iot:Connect", "iot-cfn-it/+/telemetry", "v1"));
|
||||
|
||||
String stacks = describeStacks("CREATE_COMPLETE");
|
||||
String queueUrl = outputValue(stacks, "QueueUrl");
|
||||
String errorQueueUrl = outputValue(stacks, "ErrorQueueUrl");
|
||||
String thingArn = outputValue(stacks, "SensorArn");
|
||||
String thingId = outputValue(stacks, "SensorId");
|
||||
String policyArn = outputValue(stacks, "PolicyArn");
|
||||
String ruleArn = outputValue(stacks, "RuleArn");
|
||||
assertEquals(THING, outputValue(stacks, "SensorRef"));
|
||||
assertEquals("arn:aws:iot:us-east-1:000000000000:thing/" + THING, thingArn);
|
||||
assertEquals(POLICY, outputValue(stacks, "PolicyRef"));
|
||||
assertEquals(POLICY, outputValue(stacks, "PolicyId"));
|
||||
assertEquals("arn:aws:iot:us-east-1:000000000000:policy/" + POLICY, policyArn);
|
||||
assertEquals(RULE, outputValue(stacks, "RuleRef"));
|
||||
assertEquals("arn:aws:iot:us-east-1:000000000000:rule/" + RULE, ruleArn);
|
||||
|
||||
given()
|
||||
.when()
|
||||
.get("/things/" + THING)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("thingArn", equalTo(thingArn))
|
||||
.body("thingId", equalTo(thingId))
|
||||
.body("attributes.SerialNumber", equalTo("SN-1"));
|
||||
given()
|
||||
.when()
|
||||
.get("/policies/" + POLICY)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("policyArn", equalTo(policyArn))
|
||||
.body("defaultVersionId", equalTo("1"))
|
||||
.body("policyDocument", containsString("iot:Connect"))
|
||||
.body("policyDocument", containsString("${iot:Connection.Thing.ThingName}"));
|
||||
assertTagValue(policyArn, "v1");
|
||||
given()
|
||||
.when()
|
||||
.get("/rules/" + RULE)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ruleArn", equalTo(ruleArn))
|
||||
.body("rule.sql", equalTo("SELECT * FROM 'iot-cfn-it/+/telemetry'"))
|
||||
.body("rule.awsIotSqlVersion", equalTo("2016-03-23"))
|
||||
.body("rule.ruleDisabled", equalTo(false))
|
||||
.body("rule.actions[0].sqs.queueUrl", equalTo(queueUrl))
|
||||
.body("rule.errorAction.sqs.queueUrl", equalTo(errorQueueUrl));
|
||||
assertTagValue(ruleArn, "v1");
|
||||
|
||||
given()
|
||||
.contentType("text/plain")
|
||||
.body("iot-cfn-it-payload")
|
||||
.when()
|
||||
.post("/topics/iot-cfn-it/d1/telemetry")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
receiveMessage(queueUrl).body(containsString("iot-cfn-it-payload"));
|
||||
receiveMessage(errorQueueUrl)
|
||||
.body(containsString(RULE))
|
||||
.body(containsString("SqsAction"))
|
||||
.body(containsString("iot-cfn-it-missing-queue"));
|
||||
|
||||
cloudFormation("UpdateStack", template("SN-2", "iot:Publish", "iot-cfn-it/+/telemetry/v2", "v2"));
|
||||
|
||||
String updated = describeStacks("UPDATE_COMPLETE");
|
||||
assertEquals(thingId, outputValue(updated, "SensorId"));
|
||||
given()
|
||||
.when()
|
||||
.get("/things/" + THING)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("thingId", equalTo(thingId))
|
||||
.body("attributes.SerialNumber", equalTo("SN-2"));
|
||||
given()
|
||||
.when()
|
||||
.get("/policies/" + POLICY)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("defaultVersionId", equalTo("2"))
|
||||
.body("policyDocument", containsString("iot:Publish"));
|
||||
assertTagValue(policyArn, "v2");
|
||||
given()
|
||||
.when()
|
||||
.get("/rules/" + RULE)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ruleArn", equalTo(ruleArn))
|
||||
.body("rule.sql", equalTo("SELECT * FROM 'iot-cfn-it/+/telemetry/v2'"));
|
||||
assertTagValue(ruleArn, "v2");
|
||||
|
||||
// Four more document changes. The service refuses a sixth version, so the stack deletes
|
||||
// the oldest one first, as the AWS handler does, and the policy keeps five versions.
|
||||
for (String action : List.of("iot:Subscribe", "iot:Receive", "iot:GetThingShadow", "iot:UpdateThingShadow")) {
|
||||
cloudFormation("UpdateStack", template("SN-2", action, "iot-cfn-it/+/telemetry/v2", "v2"));
|
||||
describeStacks("UPDATE_COMPLETE");
|
||||
}
|
||||
given()
|
||||
.when()
|
||||
.get("/policies/" + POLICY + "/version")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("policyVersions.versionId", contains("2", "3", "4", "5", "6"))
|
||||
.body("policyVersions.find { it.isDefaultVersion }.versionId", equalTo("6"));
|
||||
given()
|
||||
.when()
|
||||
.get("/policies/" + POLICY)
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("defaultVersionId", equalTo("6"))
|
||||
.body("policyDocument", containsString("iot:UpdateThingShadow"));
|
||||
|
||||
cloudFormation("DeleteStack", null);
|
||||
awaitStackDeleted();
|
||||
|
||||
given().when().get("/things/" + THING).then().statusCode(404);
|
||||
given().when().get("/policies/" + POLICY).then().statusCode(404);
|
||||
given().when().get("/rules/" + RULE).then().statusCode(404);
|
||||
}
|
||||
|
||||
private static ValidatableResponse receiveMessage(String queueUrl) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.formParam("Action", "ReceiveMessage")
|
||||
.formParam("QueueUrl", queueUrl)
|
||||
.formParam("MaxNumberOfMessages", "1")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
private static void assertTagValue(String arn, String expected) {
|
||||
given()
|
||||
.queryParam("resourceArn", arn)
|
||||
.when()
|
||||
.get("/tags")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("tags.find { it.Key == 'stack' }.Value", equalTo(expected));
|
||||
}
|
||||
|
||||
private static void cloudFormation(String action, String templateBody) {
|
||||
RequestSpecification request = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", action)
|
||||
.formParam("StackName", STACK);
|
||||
if (templateBody != null) {
|
||||
request.formParam("TemplateBody", templateBody);
|
||||
}
|
||||
request.when().post("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
private static String describeStacks(String expectedStatus) {
|
||||
return given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", STACK)
|
||||
.when().post("/").then().statusCode(200)
|
||||
.body(containsString("<StackStatus>" + expectedStatus + "</StackStatus>"))
|
||||
.extract().asString();
|
||||
}
|
||||
|
||||
/** DeleteStack runs asynchronously; a successful delete removes the stack entirely. */
|
||||
private static void awaitStackDeleted() throws InterruptedException {
|
||||
for (int i = 0; i < 100; i++) {
|
||||
String body = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", CFN_AUTH)
|
||||
.formParam("Action", "DescribeStacks")
|
||||
.formParam("StackName", STACK)
|
||||
.when().post("/").then().extract().asString();
|
||||
if (body.contains("does not exist")) {
|
||||
return;
|
||||
}
|
||||
if (body.contains("<StackStatus>DELETE_FAILED</StackStatus>")) {
|
||||
fail("stack delete failed: " + body);
|
||||
}
|
||||
Thread.sleep(50);
|
||||
}
|
||||
fail("stack " + STACK + " was not deleted within the timeout");
|
||||
}
|
||||
|
||||
private static String outputValue(String xml, String key) {
|
||||
return XmlParser.extractPairs(xml, "Outputs", "OutputKey", "OutputValue").get(key);
|
||||
}
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.fasterxml.jackson.databind.node.TextNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.CloudFormationTemplateEngine;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* What the three {@link IotCfnProvisioner} unit tests share: a mocked template engine that behaves
|
||||
* like the real one for the shapes these templates use, and the resource and error factories.
|
||||
*/
|
||||
final class IotCfnProvisionerTestSupport {
|
||||
|
||||
static final String REGION = "us-east-1";
|
||||
static final String ACCOUNT = "000000000000";
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private IotCfnProvisionerTestSupport() {
|
||||
}
|
||||
|
||||
static ProvisionContext ctx() {
|
||||
return ctx(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Scalars resolve to their text, an intrinsic collapses to a {@code resolved:} marker and
|
||||
* everything else is walked in place, so a nested payload keeps its shape and scalar types.
|
||||
*/
|
||||
static ProvisionContext ctx(String priorPhysicalId) {
|
||||
CloudFormationTemplateEngine engine = mock(CloudFormationTemplateEngine.class);
|
||||
when(engine.resolve(any())).thenAnswer(inv -> {
|
||||
JsonNode node = inv.getArgument(0);
|
||||
return node == null || node.isMissingNode() || node.isNull() ? null : node.asText();
|
||||
});
|
||||
when(engine.resolveNode(any())).thenAnswer(inv -> resolveIntrinsics(inv.getArgument(0)));
|
||||
when(engine.resolveJsonAttribute(any())).thenAnswer(inv -> {
|
||||
JsonNode node = inv.getArgument(0);
|
||||
if (node == null || node.isMissingNode() || node.isNull()) {
|
||||
return null;
|
||||
}
|
||||
return node.isTextual() ? node.asText() : node.toString();
|
||||
});
|
||||
return new ProvisionContext(engine, REGION, ACCOUNT, "my-stack", priorPhysicalId);
|
||||
}
|
||||
|
||||
static JsonNode resolveIntrinsics(JsonNode node) {
|
||||
if (node == null || !node.isContainerNode()) {
|
||||
return node;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
ArrayNode out = MAPPER.createArrayNode();
|
||||
node.forEach(item -> out.add(resolveIntrinsics(item)));
|
||||
return out;
|
||||
}
|
||||
if (node.has("Fn::GetAtt")) {
|
||||
JsonNode target = node.get("Fn::GetAtt");
|
||||
return TextNode.valueOf("resolved:" + target.get(0).asText() + "." + target.get(1).asText());
|
||||
}
|
||||
if (node.has("Ref")) {
|
||||
return TextNode.valueOf("resolved:" + node.get("Ref").asText());
|
||||
}
|
||||
ObjectNode out = MAPPER.createObjectNode();
|
||||
node.fields().forEachRemaining(field -> out.set(field.getKey(), resolveIntrinsics(field.getValue())));
|
||||
return out;
|
||||
}
|
||||
|
||||
static StackResource resource(String type, String logicalId) {
|
||||
StackResource r = new StackResource();
|
||||
r.setLogicalId(logicalId);
|
||||
r.setResourceType(type);
|
||||
r.setAttributes(new HashMap<>());
|
||||
return r;
|
||||
}
|
||||
|
||||
static AwsException notFound(String what) {
|
||||
return new AwsException("ResourceNotFoundException", what + " not found", 404);
|
||||
}
|
||||
}
|
||||
+369
@@ -0,0 +1,369 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.iot.IotService;
|
||||
import io.github.hectorvent.floci.services.iot.model.IotPolicy;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.InOrder;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import static io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisionerTestSupport.REGION;
|
||||
import static io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisionerTestSupport.ctx;
|
||||
import static io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisionerTestSupport.notFound;
|
||||
import static io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisionerTestSupport.resource;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyBoolean;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.calls;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.inOrder;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* {@code AWS::IoT::Policy} through the IoT CFN provisioner in isolation: one mocked service. Every
|
||||
* case asserts the exact physical id and the exact {@code Fn::GetAtt} attribute keys, because an
|
||||
* unmapped type still reports CREATE_COMPLETE through the dispatcher's stub arm.
|
||||
*/
|
||||
class IotPolicyCfnProvisionerTest {
|
||||
|
||||
private static final String TYPE = "AWS::IoT::Policy";
|
||||
private static final String ARN = "arn:aws:iot:us-east-1:000000000000:policy/device-policy";
|
||||
private static final String RENAMED_ARN = "arn:aws:iot:us-east-1:000000000000:policy/renamed";
|
||||
private static final String DOCUMENT =
|
||||
"{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":\"iot:Connect\",\"Resource\":\"*\"}]}";
|
||||
|
||||
private final IotService iot = mock(IotService.class);
|
||||
private final IotCfnProvisioner provisioner = new IotCfnProvisioner(iot);
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
private static IotPolicy policy(String name, String document) {
|
||||
IotPolicy policy = new IotPolicy();
|
||||
policy.setPolicyName(name);
|
||||
policy.setPolicyArn("arn:aws:iot:us-east-1:000000000000:policy/" + name);
|
||||
policy.setPolicyDocument(document);
|
||||
policy.setDefaultVersionId("1");
|
||||
return policy;
|
||||
}
|
||||
|
||||
private ObjectNode props(String name, JsonNode document, Map<String, String> tags) {
|
||||
ObjectNode props = mapper.createObjectNode();
|
||||
if (name != null) {
|
||||
props.put("PolicyName", name);
|
||||
}
|
||||
if (document != null) {
|
||||
props.set("PolicyDocument", document);
|
||||
}
|
||||
if (tags != null) {
|
||||
tags.forEach((key, value) -> props.withArray("Tags").addObject().put("Key", key).put("Value", value));
|
||||
}
|
||||
return props;
|
||||
}
|
||||
|
||||
private JsonNode document() throws Exception {
|
||||
return mapper.readTree(DOCUMENT);
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyWithAnObjectDocumentSerialisesItAndSetsNameAsPhysicalIdWithArnAndId() throws Exception {
|
||||
when(iot.createPolicy("device-policy", DOCUMENT, REGION)).thenReturn(policy("device-policy", DOCUMENT));
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
|
||||
provisioner.provision(r, props("device-policy", document(), null), ctx());
|
||||
|
||||
assertEquals("device-policy", r.getPhysicalId());
|
||||
assertEquals(ARN, r.getAttributes().get("Arn"));
|
||||
assertEquals("device-policy", r.getAttributes().get("Id"));
|
||||
verify(iot, never()).tagResource(anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyWithAStringDocumentPassesItThroughUnchanged() {
|
||||
String document = "{\"Version\": \"2012-10-17\", \"Statement\": []}";
|
||||
when(iot.createPolicy("device-policy", document, REGION)).thenReturn(policy("device-policy", document));
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
|
||||
provisioner.provision(r, props("device-policy", mapper.getNodeFactory().textNode(document), null), ctx());
|
||||
|
||||
verify(iot).createPolicy("device-policy", document, REGION);
|
||||
assertEquals(ARN, r.getAttributes().get("Arn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyRequiresADocument() {
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
ObjectNode props = props("device-policy", null, null);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> provisioner.provision(r, props, ctx()));
|
||||
verify(iot, never()).createPolicy(anyString(), anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyWithoutNameGetsAGeneratedName() throws Exception {
|
||||
when(iot.createPolicy(anyString(), eq(DOCUMENT), eq(REGION))).thenAnswer(inv -> policy(inv.getArgument(0), DOCUMENT));
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
|
||||
provisioner.provision(r, props(null, document(), null), ctx());
|
||||
|
||||
assertTrue(r.getPhysicalId().matches("my-stack-Policy-[0-9a-f]{12}"), "generated policy name: " + r.getPhysicalId());
|
||||
assertEquals(r.getPhysicalId(), r.getAttributes().get("Id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyCreateAppliesTheTemplateTags() throws Exception {
|
||||
when(iot.createPolicy("device-policy", DOCUMENT, REGION)).thenReturn(policy("device-policy", DOCUMENT));
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
|
||||
provisioner.provision(r, props("device-policy", document(), Map.of("env", "test")), ctx());
|
||||
|
||||
verify(iot).tagResource(ARN, Map.of("env", "test"));
|
||||
assertFalse(r.getAttributes().containsKey(CfnRollback.ROLLBACK_OWNED_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyCreateLeavesTheResourceOwnedWhenTaggingFailsSoTheRollbackDeletesIt() throws Exception {
|
||||
when(iot.createPolicy("device-policy", DOCUMENT, REGION)).thenReturn(policy("device-policy", DOCUMENT));
|
||||
doThrow(new AwsException("InternalFailureException", "tagging failed", 500)).when(iot).tagResource(eq(ARN), any());
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
ObjectNode props = props("device-policy", document(), Map.of("env", "test"));
|
||||
|
||||
assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx()));
|
||||
|
||||
assertEquals("device-policy", r.getPhysicalId());
|
||||
assertEquals("true", r.getAttributes().get(CfnRollback.ROLLBACK_OWNED_ATTR));
|
||||
verify(iot, never()).deletePolicy(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyUpdateWithAChangedDocumentCreatesANewDefaultVersion() throws Exception {
|
||||
String changed = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
|
||||
when(iot.getPolicy("device-policy", REGION)).thenReturn(policy("device-policy", DOCUMENT));
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
|
||||
provisioner.provision(r, props("device-policy", mapper.readTree(changed), null), ctx("device-policy"));
|
||||
|
||||
verify(iot).createPolicyVersion("device-policy", changed, true, REGION);
|
||||
verify(iot, never()).createPolicy(anyString(), anyString(), anyString());
|
||||
verify(iot, never()).deletePolicy(anyString(), anyString());
|
||||
assertEquals("device-policy", r.getPhysicalId());
|
||||
assertEquals(ARN, r.getAttributes().get("Arn"));
|
||||
assertEquals("device-policy", r.getAttributes().get("Id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyUpdateWithTheSameDocumentDoesNotCreateAVersion() throws Exception {
|
||||
when(iot.getPolicy("device-policy", REGION)).thenReturn(policy("device-policy", DOCUMENT));
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
|
||||
provisioner.provision(r, props("device-policy", document(), null), ctx("device-policy"));
|
||||
|
||||
verify(iot, never()).createPolicyVersion(anyString(), anyString(), anyBoolean(), anyString());
|
||||
assertEquals(ARN, r.getAttributes().get("Arn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyUpdateDrivesTheTagsToTheTemplate() throws Exception {
|
||||
when(iot.getPolicy("device-policy", REGION)).thenReturn(policy("device-policy", DOCUMENT));
|
||||
when(iot.listTagsForResource(ARN)).thenReturn(Map.of("stale", "1", "env", "old"));
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
|
||||
provisioner.provision(r, props("device-policy", document(), Map.of("env", "test")), ctx("device-policy"));
|
||||
|
||||
verify(iot).untagResource(ARN, List.of("stale"));
|
||||
verify(iot).tagResource(ARN, Map.of("env", "test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyUpdateWithoutTagsRemovesEveryStoredTag() throws Exception {
|
||||
when(iot.getPolicy("device-policy", REGION)).thenReturn(policy("device-policy", DOCUMENT));
|
||||
when(iot.listTagsForResource(ARN)).thenReturn(Map.of("stale", "1"));
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
|
||||
provisioner.provision(r, props("device-policy", document(), null), ctx("device-policy"));
|
||||
|
||||
verify(iot).untagResource(ARN, List.of("stale"));
|
||||
verify(iot, never()).tagResource(anyString(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyRenameCreatesTheReplacementThenDetachesAndDeletesThePriorPolicy() throws Exception {
|
||||
when(iot.createPolicy("renamed", DOCUMENT, REGION)).thenReturn(policy("renamed", DOCUMENT));
|
||||
when(iot.listTargetsForPolicy("device-policy", REGION)).thenReturn(Set.of("arn:aws:iot:us-east-1:000000000000:cert/abc"));
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
|
||||
provisioner.provision(r, props("renamed", document(), null), ctx("device-policy"));
|
||||
|
||||
InOrder inOrder = inOrder(iot);
|
||||
inOrder.verify(iot).createPolicy("renamed", DOCUMENT, REGION);
|
||||
inOrder.verify(iot).detachPolicy("device-policy", "arn:aws:iot:us-east-1:000000000000:cert/abc", REGION);
|
||||
inOrder.verify(iot).deletePolicy("device-policy", REGION);
|
||||
verify(iot, never()).createPolicyVersion(anyString(), anyString(), anyBoolean(), anyString());
|
||||
assertEquals("renamed", r.getPhysicalId());
|
||||
assertEquals(RENAMED_ARN, r.getAttributes().get("Arn"));
|
||||
assertEquals("renamed", r.getAttributes().get("Id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyRenameKeepsTheUpdateWhenThePriorPolicyCannotBeDeleted() throws Exception {
|
||||
// As in CloudFormation's cleanup phase: the new policy exists and the stack points at it,
|
||||
// so a failed removal of the old one is logged and the update completes.
|
||||
when(iot.createPolicy("renamed", DOCUMENT, REGION)).thenReturn(policy("renamed", DOCUMENT));
|
||||
doThrow(new AwsException("InternalFailureException", "storage", 500)).when(iot).deletePolicy("device-policy", REGION);
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
|
||||
assertDoesNotThrow(() -> provisioner.provision(r, props("renamed", document(), null), ctx("device-policy")));
|
||||
|
||||
assertEquals("renamed", r.getPhysicalId());
|
||||
verify(iot, never()).deletePolicy("renamed", REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyReplacementRemovesTheNewPolicyWhenTaggingItFails() throws Exception {
|
||||
when(iot.createPolicy("renamed", DOCUMENT, REGION)).thenReturn(policy("renamed", DOCUMENT));
|
||||
doThrow(new AwsException("InternalFailureException", "tagging failed", 500)).when(iot).tagResource(eq(RENAMED_ARN), any());
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
ObjectNode props = props("renamed", document(), Map.of("env", "test"));
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx("device-policy")));
|
||||
|
||||
assertEquals("InternalFailureException", e.getErrorCode());
|
||||
verify(iot).deletePolicy("renamed", REGION);
|
||||
verify(iot, never()).deletePolicy("device-policy", REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyReplacementReportsARollbackFailureWhenTheNewPolicyCannotBeRemoved() throws Exception {
|
||||
when(iot.createPolicy("renamed", DOCUMENT, REGION)).thenReturn(policy("renamed", DOCUMENT));
|
||||
doThrow(new AwsException("InternalFailureException", "tagging failed", 500)).when(iot).tagResource(eq(RENAMED_ARN), any());
|
||||
doThrow(new AwsException("InternalFailureException", "storage", 500)).when(iot).deletePolicy("renamed", REGION);
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
ObjectNode props = props("renamed", document(), Map.of("env", "test"));
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx("device-policy")));
|
||||
|
||||
assertEquals("InternalFailureException", e.getErrorCode());
|
||||
assertEquals(1, e.getSuppressed().length);
|
||||
assertTrue(r.getAttributes().get(CfnRollback.UPDATE_ROLLBACK_FAILURE_ATTR).contains("renamed"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletePolicyDetachesEveryTargetBeforeDeleting() {
|
||||
when(iot.listTargetsForPolicy("device-policy", REGION)).thenReturn(new TreeSet<>(List.of(
|
||||
"arn:aws:iot:us-east-1:000000000000:cert/abc", "arn:aws:iot:us-east-1:000000000000:cert/def")));
|
||||
|
||||
provisioner.delete(TYPE, "device-policy", REGION);
|
||||
|
||||
InOrder inOrder = inOrder(iot);
|
||||
inOrder.verify(iot).detachPolicy("device-policy", "arn:aws:iot:us-east-1:000000000000:cert/abc", REGION);
|
||||
inOrder.verify(iot).detachPolicy("device-policy", "arn:aws:iot:us-east-1:000000000000:cert/def", REGION);
|
||||
inOrder.verify(iot).deletePolicy("device-policy", REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletePolicyToleratesOnlyNotFound() {
|
||||
doThrow(notFound("Policy")).when(iot).listTargetsForPolicy("gone", REGION);
|
||||
doThrow(new AwsException("InternalFailureException", "storage", 500)).when(iot).deletePolicy("stuck", REGION);
|
||||
|
||||
assertDoesNotThrow(() -> provisioner.delete(TYPE, "gone", REGION));
|
||||
verify(iot, never()).deletePolicy("gone", REGION);
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.delete(TYPE, "stuck", REGION));
|
||||
assertEquals("InternalFailureException", e.getErrorCode());
|
||||
}
|
||||
|
||||
private static IotPolicy.PolicyVersion version(String id) {
|
||||
IotPolicy.PolicyVersion version = new IotPolicy.PolicyVersion();
|
||||
version.setVersionId(id);
|
||||
return version;
|
||||
}
|
||||
|
||||
private static AwsException versionsLimit() {
|
||||
return new AwsException("VersionsLimitExceededException", "The number of policy versions exceeds the limit", 409);
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyUpdateDeletesTheOldestVersionWhenTheServiceRefusesASixthAndCreatesAgain() throws Exception {
|
||||
String changed = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
|
||||
when(iot.getPolicy("device-policy", REGION)).thenReturn(policy("device-policy", DOCUMENT));
|
||||
when(iot.createPolicyVersion("device-policy", changed, true, REGION))
|
||||
.thenThrow(versionsLimit())
|
||||
.thenReturn(version("6"));
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
|
||||
provisioner.provision(r, props("device-policy", mapper.readTree(changed), null), ctx("device-policy"));
|
||||
|
||||
InOrder inOrder = inOrder(iot);
|
||||
inOrder.verify(iot, calls(1)).createPolicyVersion("device-policy", changed, true, REGION);
|
||||
inOrder.verify(iot).makeRoomForPolicyVersion("device-policy", REGION);
|
||||
inOrder.verify(iot, calls(1)).createPolicyVersion("device-policy", changed, true, REGION);
|
||||
verify(iot, times(1)).makeRoomForPolicyVersion(anyString(), anyString());
|
||||
verify(iot, never()).deletePolicyVersion(anyString(), anyString(), anyString());
|
||||
assertEquals(ARN, r.getAttributes().get("Arn"));
|
||||
assertEquals("device-policy", r.getAttributes().get("Id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyUpdatePassesOtherVersionErrorsThroughWithoutPruning() throws Exception {
|
||||
String changed = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
|
||||
when(iot.getPolicy("device-policy", REGION)).thenReturn(policy("device-policy", DOCUMENT));
|
||||
when(iot.createPolicyVersion("device-policy", changed, true, REGION))
|
||||
.thenThrow(new AwsException("InternalFailureException", "storage", 500));
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
ObjectNode props = props("device-policy", mapper.readTree(changed), null);
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx("device-policy")));
|
||||
|
||||
assertEquals("InternalFailureException", e.getErrorCode());
|
||||
verify(iot, never()).makeRoomForPolicyVersion(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyUpdatePrunesAgainWhenAnotherVersionRefilledTheSlotBeforeTheRetry() throws Exception {
|
||||
// Another client created a version between the prune and the retry.
|
||||
String changed = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
|
||||
when(iot.getPolicy("device-policy", REGION)).thenReturn(policy("device-policy", DOCUMENT));
|
||||
when(iot.createPolicyVersion("device-policy", changed, true, REGION))
|
||||
.thenThrow(versionsLimit())
|
||||
.thenThrow(versionsLimit())
|
||||
.thenReturn(version("7"));
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
|
||||
provisioner.provision(r, props("device-policy", mapper.readTree(changed), null), ctx("device-policy"));
|
||||
|
||||
verify(iot, times(2)).makeRoomForPolicyVersion("device-policy", REGION);
|
||||
verify(iot, times(3)).createPolicyVersion("device-policy", changed, true, REGION);
|
||||
assertEquals(ARN, r.getAttributes().get("Arn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void policyUpdateGivesUpAfterAsManyPrunesAsAPolicyHasVersions() throws Exception {
|
||||
String changed = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
|
||||
when(iot.getPolicy("device-policy", REGION)).thenReturn(policy("device-policy", DOCUMENT));
|
||||
when(iot.createPolicyVersion("device-policy", changed, true, REGION))
|
||||
.thenAnswer(inv -> { throw versionsLimit(); });
|
||||
StackResource r = resource(TYPE, "Policy");
|
||||
ObjectNode props = props("device-policy", mapper.readTree(changed), null);
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx("device-policy")));
|
||||
|
||||
assertEquals("VersionsLimitExceededException", e.getErrorCode());
|
||||
verify(iot, times(IotService.MAX_POLICY_VERSIONS - 1)).makeRoomForPolicyVersion("device-policy", REGION);
|
||||
verify(iot, times(IotService.MAX_POLICY_VERSIONS)).createPolicyVersion("device-policy", changed, true, REGION);
|
||||
}
|
||||
}
|
||||
+198
@@ -0,0 +1,198 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.iot.IotService;
|
||||
import io.github.hectorvent.floci.services.iot.model.Thing;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import static io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisionerTestSupport.REGION;
|
||||
import static io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisionerTestSupport.ctx;
|
||||
import static io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisionerTestSupport.notFound;
|
||||
import static io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisionerTestSupport.resource;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.ArgumentMatchers.isNull;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* {@code AWS::IoT::Thing} through the IoT CFN provisioner in isolation: one mocked service. Every
|
||||
* case asserts the exact physical id and the exact {@code Fn::GetAtt} attribute keys, because an
|
||||
* unmapped type still reports CREATE_COMPLETE through the dispatcher's stub arm.
|
||||
*/
|
||||
class IotThingCfnProvisionerTest {
|
||||
|
||||
private static final String TYPE = "AWS::IoT::Thing";
|
||||
private static final String ARN = "arn:aws:iot:us-east-1:000000000000:thing/sensor-1";
|
||||
private static final String THING_ID = "11111111-2222-3333-4444-555555555555";
|
||||
|
||||
private final IotService iot = mock(IotService.class);
|
||||
private final IotCfnProvisioner provisioner = new IotCfnProvisioner(iot);
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
private static Thing thing(String name, Map<String, String> attributes) {
|
||||
Thing thing = new Thing();
|
||||
thing.setThingName(name);
|
||||
thing.setThingArn("arn:aws:iot:us-east-1:000000000000:thing/" + name);
|
||||
thing.setThingId(THING_ID);
|
||||
thing.setAttributes(attributes);
|
||||
return thing;
|
||||
}
|
||||
|
||||
private ObjectNode props(String name, Map<String, String> attributes) {
|
||||
ObjectNode props = mapper.createObjectNode();
|
||||
if (name != null) {
|
||||
props.put("ThingName", name);
|
||||
}
|
||||
if (attributes != null) {
|
||||
ObjectNode attrs = props.putObject("AttributePayload").putObject("Attributes");
|
||||
attributes.forEach(attrs::put);
|
||||
}
|
||||
return props;
|
||||
}
|
||||
|
||||
private Map<String, String> capturedAttributes(String thingName) {
|
||||
@SuppressWarnings("unchecked")
|
||||
ArgumentCaptor<Map<String, String>> captor = ArgumentCaptor.forClass(Map.class);
|
||||
verify(iot).createThing(eq(thingName), captor.capture(), eq(REGION));
|
||||
return captor.getValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void resourceTypesCoverThingPolicyAndTopicRule() {
|
||||
assertEquals(Set.of(TYPE, "AWS::IoT::Policy", "AWS::IoT::TopicRule"), provisioner.resourceTypes());
|
||||
}
|
||||
|
||||
@Test
|
||||
void thingSetsNameAsPhysicalIdAndArnAndIdAttributes() {
|
||||
Map<String, String> attributes = Map.of("SerialNumber", "SN-1", "Model", "M2");
|
||||
when(iot.createThing(eq("sensor-1"), any(), eq(REGION))).thenReturn(thing("sensor-1", attributes));
|
||||
StackResource r = resource(TYPE, "Sensor");
|
||||
|
||||
provisioner.provision(r, props("sensor-1", attributes), ctx());
|
||||
|
||||
assertEquals("sensor-1", r.getPhysicalId());
|
||||
assertEquals(ARN, r.getAttributes().get("Arn"));
|
||||
assertEquals(THING_ID, r.getAttributes().get("Id"));
|
||||
assertEquals(attributes, capturedAttributes("sensor-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void thingWithoutAttributePayloadIsCreatedWithNoAttributes() {
|
||||
when(iot.createThing(eq("sensor-1"), any(), eq(REGION))).thenReturn(thing("sensor-1", Map.of()));
|
||||
StackResource r = resource(TYPE, "Sensor");
|
||||
|
||||
provisioner.provision(r, props("sensor-1", null), ctx());
|
||||
|
||||
assertEquals(Map.of(), capturedAttributes("sensor-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void thingWithoutNameGetsAGeneratedNameWithinTheLimit() {
|
||||
when(iot.createThing(anyString(), any(), eq(REGION))).thenAnswer(inv -> thing(inv.getArgument(0), Map.of()));
|
||||
StackResource r = resource(TYPE, "Sensor");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode(), ctx());
|
||||
|
||||
String name = r.getPhysicalId();
|
||||
assertTrue(name.matches("my-stack-Sensor-[0-9a-f]{12}"), "generated thing name: " + name);
|
||||
assertTrue(name.length() <= 128);
|
||||
assertEquals("arn:aws:iot:us-east-1:000000000000:thing/" + name, r.getAttributes().get("Arn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void thingUpdateWithTheSameNameReplacesTheAttributesInPlace() {
|
||||
Map<String, String> attributes = Map.of("Model", "M3");
|
||||
when(iot.updateThing(eq("sensor-1"), any(), isNull(), eq(REGION))).thenReturn(thing("sensor-1", attributes));
|
||||
StackResource r = resource(TYPE, "Sensor");
|
||||
|
||||
provisioner.provision(r, props("sensor-1", attributes), ctx("sensor-1"));
|
||||
|
||||
verify(iot, never()).createThing(anyString(), any(), anyString());
|
||||
verify(iot, never()).deleteThing(anyString(), anyString());
|
||||
assertEquals("sensor-1", r.getPhysicalId());
|
||||
assertEquals(ARN, r.getAttributes().get("Arn"));
|
||||
assertEquals(THING_ID, r.getAttributes().get("Id"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void thingUpdateWithoutAnExplicitNameKeepsThePriorGeneratedName() {
|
||||
when(iot.updateThing(eq("my-stack-Sensor-0123456789ab"), any(), isNull(), eq(REGION)))
|
||||
.thenReturn(thing("my-stack-Sensor-0123456789ab", Map.of()));
|
||||
StackResource r = resource(TYPE, "Sensor");
|
||||
|
||||
provisioner.provision(r, mapper.createObjectNode(), ctx("my-stack-Sensor-0123456789ab"));
|
||||
|
||||
verify(iot, never()).createThing(anyString(), any(), anyString());
|
||||
assertEquals("my-stack-Sensor-0123456789ab", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void thingRenameCreatesTheReplacementThenDeletesThePriorThing() {
|
||||
when(iot.createThing(eq("sensor-2"), any(), eq(REGION))).thenReturn(thing("sensor-2", Map.of()));
|
||||
StackResource r = resource(TYPE, "Sensor");
|
||||
|
||||
provisioner.provision(r, props("sensor-2", null), ctx("sensor-1"));
|
||||
|
||||
verify(iot).deleteThing("sensor-1", REGION);
|
||||
verify(iot, never()).updateThing(anyString(), any(), any(), anyString());
|
||||
assertEquals("sensor-2", r.getPhysicalId());
|
||||
assertEquals("arn:aws:iot:us-east-1:000000000000:thing/sensor-2", r.getAttributes().get("Arn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void thingRenameToleratesAPriorThingThatIsAlreadyGone() {
|
||||
when(iot.createThing(eq("sensor-2"), any(), eq(REGION))).thenReturn(thing("sensor-2", Map.of()));
|
||||
doThrow(notFound("Thing")).when(iot).deleteThing("sensor-1", REGION);
|
||||
StackResource r = resource(TYPE, "Sensor");
|
||||
|
||||
assertDoesNotThrow(() -> provisioner.provision(r, props("sensor-2", null), ctx("sensor-1")));
|
||||
|
||||
assertEquals("sensor-2", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void thingRenameKeepsTheUpdateWhenThePriorThingCannotBeDeleted() {
|
||||
// As in CloudFormation's cleanup phase: the new thing exists and the stack points at it,
|
||||
// so a failed removal of the old one is logged and the update completes.
|
||||
when(iot.createThing(eq("sensor-2"), any(), eq(REGION))).thenReturn(thing("sensor-2", Map.of()));
|
||||
doThrow(new AwsException("InvalidRequestException", "Cannot delete", 400)).when(iot).deleteThing("sensor-1", REGION);
|
||||
StackResource r = resource(TYPE, "Sensor");
|
||||
|
||||
assertDoesNotThrow(() -> provisioner.provision(r, props("sensor-2", null), ctx("sensor-1")));
|
||||
|
||||
assertEquals("sensor-2", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteThingDelegatesToTheService() {
|
||||
provisioner.delete(TYPE, "sensor-1", REGION);
|
||||
|
||||
verify(iot).deleteThing("sensor-1", REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteThingToleratesOnlyNotFound() {
|
||||
doThrow(notFound("Thing")).when(iot).deleteThing("gone", REGION);
|
||||
doThrow(new AwsException("InvalidRequestException", "Cannot delete", 400)).when(iot).deleteThing("held", REGION);
|
||||
|
||||
assertDoesNotThrow(() -> provisioner.delete(TYPE, "gone", REGION));
|
||||
AwsException e = assertThrows(AwsException.class, () -> provisioner.delete(TYPE, "held", REGION));
|
||||
assertEquals("InvalidRequestException", e.getErrorCode());
|
||||
}
|
||||
}
|
||||
+248
@@ -0,0 +1,248 @@
|
||||
package io.github.hectorvent.floci.services.cloudformation.provisioners;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import io.github.hectorvent.floci.core.common.AwsException;
|
||||
import io.github.hectorvent.floci.services.cloudformation.model.StackResource;
|
||||
import io.github.hectorvent.floci.services.iot.IotService;
|
||||
import io.github.hectorvent.floci.services.iot.model.IotTopicRule;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisionerTestSupport.REGION;
|
||||
import static io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisionerTestSupport.ctx;
|
||||
import static io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisionerTestSupport.notFound;
|
||||
import static io.github.hectorvent.floci.services.cloudformation.provisioners.IotCfnProvisionerTestSupport.resource;
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* {@code AWS::IoT::TopicRule} through the IoT CFN provisioner in isolation: one mocked service.
|
||||
* Every case asserts the exact physical id and the exact {@code Fn::GetAtt} attribute key, because
|
||||
* an unmapped type still reports CREATE_COMPLETE through the dispatcher's stub arm.
|
||||
*/
|
||||
class IotTopicRuleCfnProvisionerTest {
|
||||
|
||||
private static final String TYPE = "AWS::IoT::TopicRule";
|
||||
private static final String ARN = "arn:aws:iot:us-east-1:000000000000:rule/telemetryRule";
|
||||
private static final String RENAMED_ARN = "arn:aws:iot:us-east-1:000000000000:rule/renamedRule";
|
||||
|
||||
private final IotService iot = mock(IotService.class);
|
||||
private final IotCfnProvisioner provisioner = new IotCfnProvisioner(iot);
|
||||
private final ObjectMapper mapper = new ObjectMapper();
|
||||
|
||||
private static IotTopicRule rule(String name) {
|
||||
IotTopicRule rule = new IotTopicRule();
|
||||
rule.setRuleName(name);
|
||||
rule.setRuleArn("arn:aws:iot:us-east-1:000000000000:rule/" + name);
|
||||
return rule;
|
||||
}
|
||||
|
||||
/** The payload as CloudFormation spells it, with an intrinsic where CDK puts one. */
|
||||
private ObjectNode templatePayload() {
|
||||
ObjectNode payload = mapper.createObjectNode()
|
||||
.put("Sql", "SELECT * FROM 'devices/+/telemetry'")
|
||||
.put("AwsIotSqlVersion", "2016-03-23")
|
||||
.put("RuleDisabled", false)
|
||||
.put("Description", "telemetry fan-out");
|
||||
ObjectNode functionArn = mapper.createObjectNode();
|
||||
functionArn.set("Fn::GetAtt", mapper.createArrayNode().add("Handler").add("Arn"));
|
||||
payload.withArray("Actions").addObject().putObject("Lambda").set("FunctionArn", functionArn);
|
||||
ObjectNode sqs = payload.withArray("Actions").addObject().putObject("Sqs");
|
||||
sqs.put("RoleArn", "arn:aws:iam::000000000000:role/rule").put("UseBase64", false);
|
||||
sqs.set("QueueUrl", mapper.createObjectNode().put("Ref", "Queue"));
|
||||
payload.putObject("ErrorAction").putObject("Sqs")
|
||||
.put("QueueUrl", "http://localhost:4566/000000000000/dlq")
|
||||
.put("RoleArn", "arn:aws:iam::000000000000:role/rule");
|
||||
return payload;
|
||||
}
|
||||
|
||||
private ObjectNode props(String name, ObjectNode payload, Map<String, String> tags) {
|
||||
ObjectNode props = mapper.createObjectNode();
|
||||
if (name != null) {
|
||||
props.put("RuleName", name);
|
||||
}
|
||||
if (payload != null) {
|
||||
props.set("TopicRulePayload", payload);
|
||||
}
|
||||
if (tags != null) {
|
||||
tags.forEach((key, value) -> props.withArray("Tags").addObject().put("Key", key).put("Value", value));
|
||||
}
|
||||
return props;
|
||||
}
|
||||
|
||||
private JsonNode capturedPayload(String ruleName) {
|
||||
ArgumentCaptor<JsonNode> captor = ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(iot).createTopicRule(eq(ruleName), captor.capture(), eq(REGION));
|
||||
return captor.getValue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicRuleHandsTheServiceThePayloadInTheApiShapeAndSetsArn() {
|
||||
when(iot.createTopicRule(eq("telemetryRule"), any(), eq(REGION))).thenReturn(rule("telemetryRule"));
|
||||
StackResource r = resource(TYPE, "Rule");
|
||||
|
||||
provisioner.provision(r, props("telemetryRule", templatePayload(), null), ctx());
|
||||
|
||||
assertEquals("telemetryRule", r.getPhysicalId());
|
||||
assertEquals(ARN, r.getAttributes().get("Arn"));
|
||||
JsonNode payload = capturedPayload("telemetryRule");
|
||||
assertEquals("SELECT * FROM 'devices/+/telemetry'", payload.get("sql").asText());
|
||||
assertEquals("2016-03-23", payload.get("awsIotSqlVersion").asText());
|
||||
assertEquals("telemetry fan-out", payload.get("description").asText());
|
||||
assertTrue(payload.get("ruleDisabled").isBoolean());
|
||||
assertFalse(payload.get("ruleDisabled").asBoolean());
|
||||
assertEquals("resolved:Handler.Arn", payload.at("/actions/0/lambda/functionArn").asText());
|
||||
assertEquals("resolved:Queue", payload.at("/actions/1/sqs/queueUrl").asText());
|
||||
assertTrue(payload.at("/actions/1/sqs/useBase64").isBoolean());
|
||||
assertEquals("http://localhost:4566/000000000000/dlq", payload.at("/errorAction/sqs/queueUrl").asText());
|
||||
assertFalse(payload.has("Sql") || payload.has("Actions") || payload.has("ErrorAction"),
|
||||
"no PascalCase keys may reach the service: " + payload);
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicRuleDropsNullPayloadMembers() {
|
||||
when(iot.createTopicRule(eq("telemetryRule"), any(), eq(REGION))).thenReturn(rule("telemetryRule"));
|
||||
ObjectNode payload = mapper.createObjectNode().put("Sql", "SELECT * FROM 'a'");
|
||||
payload.putNull("Description");
|
||||
payload.withArray("Actions").addObject().putObject("Lambda").put("FunctionArn", "arn:fn");
|
||||
StackResource r = resource(TYPE, "Rule");
|
||||
|
||||
provisioner.provision(r, props("telemetryRule", payload, null), ctx());
|
||||
|
||||
assertFalse(capturedPayload("telemetryRule").has("description"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicRuleRequiresAPayload() {
|
||||
StackResource r = resource(TYPE, "Rule");
|
||||
ObjectNode props = props("telemetryRule", null, null);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> provisioner.provision(r, props, ctx()));
|
||||
verify(iot, never()).createTopicRule(anyString(), any(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicRuleWithoutNameGetsAGeneratedNameWithoutHyphens() {
|
||||
when(iot.createTopicRule(anyString(), any(), eq(REGION))).thenAnswer(inv -> rule(inv.getArgument(0)));
|
||||
StackResource r = resource(TYPE, "Rule");
|
||||
|
||||
provisioner.provision(r, props(null, templatePayload(), null), ctx());
|
||||
|
||||
assertTrue(r.getPhysicalId().matches("mystackRule[0-9a-f]{12}"), "generated rule name: " + r.getPhysicalId());
|
||||
assertEquals("arn:aws:iot:us-east-1:000000000000:rule/" + r.getPhysicalId(), r.getAttributes().get("Arn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicRuleHandsAnExplicitNameToTheServiceUnchanged() {
|
||||
// Only generated names are trimmed to the characters a rule name allows; validating an
|
||||
// explicit name is the service's job, so it must not be altered on the way there.
|
||||
when(iot.createTopicRule(eq("telemetry-rule"), any(), eq(REGION))).thenReturn(rule("telemetry-rule"));
|
||||
StackResource r = resource(TYPE, "Rule");
|
||||
|
||||
provisioner.provision(r, props("telemetry-rule", templatePayload(), null), ctx());
|
||||
|
||||
assertEquals("telemetry-rule", r.getPhysicalId());
|
||||
verify(iot).createTopicRule(eq("telemetry-rule"), any(), eq(REGION));
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicRuleCreateAppliesTheTemplateTags() {
|
||||
when(iot.createTopicRule(eq("telemetryRule"), any(), eq(REGION))).thenReturn(rule("telemetryRule"));
|
||||
StackResource r = resource(TYPE, "Rule");
|
||||
|
||||
provisioner.provision(r, props("telemetryRule", templatePayload(), Map.of("env", "test")), ctx());
|
||||
|
||||
verify(iot).tagResource(ARN, Map.of("env", "test"));
|
||||
assertFalse(r.getAttributes().containsKey(CfnRollback.ROLLBACK_OWNED_ATTR));
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicRuleUpdateWithTheSameNameReplacesTheRule() {
|
||||
when(iot.replaceTopicRule(eq("telemetryRule"), any(), eq(REGION))).thenReturn(rule("telemetryRule"));
|
||||
StackResource r = resource(TYPE, "Rule");
|
||||
|
||||
provisioner.provision(r, props("telemetryRule", templatePayload(), null), ctx("telemetryRule"));
|
||||
|
||||
ArgumentCaptor<JsonNode> captor = ArgumentCaptor.forClass(JsonNode.class);
|
||||
verify(iot).replaceTopicRule(eq("telemetryRule"), captor.capture(), eq(REGION));
|
||||
assertEquals("SELECT * FROM 'devices/+/telemetry'", captor.getValue().get("sql").asText());
|
||||
verify(iot, never()).createTopicRule(anyString(), any(), anyString());
|
||||
verify(iot, never()).deleteTopicRule(anyString(), anyString());
|
||||
assertEquals("telemetryRule", r.getPhysicalId());
|
||||
assertEquals(ARN, r.getAttributes().get("Arn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicRuleUpdateWithoutAnExplicitNameKeepsThePriorGeneratedName() {
|
||||
when(iot.replaceTopicRule(eq("mystackRule0123456789ab"), any(), eq(REGION))).thenReturn(rule("mystackRule0123456789ab"));
|
||||
StackResource r = resource(TYPE, "Rule");
|
||||
|
||||
provisioner.provision(r, props(null, templatePayload(), null), ctx("mystackRule0123456789ab"));
|
||||
|
||||
verify(iot, never()).createTopicRule(anyString(), any(), anyString());
|
||||
assertEquals("mystackRule0123456789ab", r.getPhysicalId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicRuleUpdateDrivesTheTagsToTheTemplate() {
|
||||
when(iot.replaceTopicRule(eq("telemetryRule"), any(), eq(REGION))).thenReturn(rule("telemetryRule"));
|
||||
when(iot.listTagsForResource(ARN)).thenReturn(Map.of("stale", "1"));
|
||||
StackResource r = resource(TYPE, "Rule");
|
||||
|
||||
provisioner.provision(r, props("telemetryRule", templatePayload(), Map.of("env", "test")), ctx("telemetryRule"));
|
||||
|
||||
verify(iot).untagResource(ARN, List.of("stale"));
|
||||
verify(iot).tagResource(ARN, Map.of("env", "test"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicRuleRenameCreatesTheReplacementThenDeletesThePriorRule() {
|
||||
when(iot.createTopicRule(eq("renamedRule"), any(), eq(REGION))).thenReturn(rule("renamedRule"));
|
||||
StackResource r = resource(TYPE, "Rule");
|
||||
|
||||
provisioner.provision(r, props("renamedRule", templatePayload(), null), ctx("telemetryRule"));
|
||||
|
||||
verify(iot).deleteTopicRule("telemetryRule", REGION);
|
||||
verify(iot, never()).replaceTopicRule(anyString(), any(), anyString());
|
||||
assertEquals("renamedRule", r.getPhysicalId());
|
||||
assertEquals(RENAMED_ARN, r.getAttributes().get("Arn"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void topicRuleReplacementRemovesTheNewRuleWhenTaggingItFails() {
|
||||
when(iot.createTopicRule(eq("renamedRule"), any(), eq(REGION))).thenReturn(rule("renamedRule"));
|
||||
doThrow(new AwsException("InternalFailureException", "tagging failed", 500)).when(iot).tagResource(eq(RENAMED_ARN), any());
|
||||
StackResource r = resource(TYPE, "Rule");
|
||||
ObjectNode props = props("renamedRule", templatePayload(), Map.of("env", "test"));
|
||||
|
||||
assertThrows(AwsException.class, () -> provisioner.provision(r, props, ctx("telemetryRule")));
|
||||
|
||||
verify(iot).deleteTopicRule("renamedRule", REGION);
|
||||
verify(iot, never()).deleteTopicRule("telemetryRule", REGION);
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteTopicRuleToleratesOnlyNotFound() {
|
||||
doThrow(notFound("Topic rule")).when(iot).deleteTopicRule("gone", REGION);
|
||||
doThrow(new AwsException("InternalFailureException", "storage", 500)).when(iot).deleteTopicRule("stuck", REGION);
|
||||
|
||||
assertDoesNotThrow(() -> provisioner.delete(TYPE, "gone", REGION));
|
||||
assertThrows(AwsException.class, () -> provisioner.delete(TYPE, "stuck", REGION));
|
||||
}
|
||||
}
|
||||
@@ -749,6 +749,52 @@ class IotIntegrationTest {
|
||||
.body("__type", equalTo("ResourceNotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(20)
|
||||
void aSixthPolicyVersionIsRefusedUntilOneIsDeleted() {
|
||||
String document = "{\"policyDocument\":\"{\\\"Version\\\":\\\"2012-10-17\\\",\\\"Statement\\\":[]}\"}";
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(document)
|
||||
.when()
|
||||
.post("/policies/five-versions")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
for (int version = 2; version <= 5; version++) {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(document)
|
||||
.when()
|
||||
.post("/policies/five-versions/version")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("policyVersionId", equalTo(Integer.toString(version)));
|
||||
}
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(document)
|
||||
.when()
|
||||
.post("/policies/five-versions/version")
|
||||
.then()
|
||||
.statusCode(409)
|
||||
.body("__type", equalTo("VersionsLimitExceededException"));
|
||||
|
||||
given()
|
||||
.when()
|
||||
.delete("/policies/five-versions/version/3")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.body(document)
|
||||
.when()
|
||||
.post("/policies/five-versions/version")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("policyVersionId", equalTo("6"));
|
||||
}
|
||||
|
||||
private String createThingAndReturnArn(String thingName) {
|
||||
return given()
|
||||
.contentType("application/json")
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService
|
||||
import io.github.hectorvent.floci.services.dynamodb.DynamoDbService;
|
||||
import io.github.hectorvent.floci.services.firehose.FirehoseService;
|
||||
import io.github.hectorvent.floci.services.firehose.model.Record;
|
||||
import io.github.hectorvent.floci.services.iot.model.IotPolicy;
|
||||
import io.github.hectorvent.floci.services.iot.model.IotTopicRule;
|
||||
import io.github.hectorvent.floci.services.kinesis.KinesisService;
|
||||
import io.github.hectorvent.floci.services.lambda.LambdaService;
|
||||
@@ -23,9 +24,14 @@ import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -47,6 +53,7 @@ import static org.mockito.Mockito.when;
|
||||
* Rules engine behaviour of {@link IotService} with in-memory stores and mocked action targets:
|
||||
* one failing action never fails the publish or the other actions, the error action receives the
|
||||
* failure document, and the {@code firehose} and {@code cloudwatchLogs} actions deliver the payload.
|
||||
* Also the five-version cap on a policy, including under racing creates.
|
||||
*/
|
||||
class IotServiceTest {
|
||||
|
||||
@@ -499,4 +506,161 @@ class IotServiceTest {
|
||||
verify(lambda).invoke(eq(REGION), eq(FUNCTION_ARN), any(), eq(InvocationType.Event));
|
||||
verify(lambda, never()).invoke(eq(REGION), eq(ERROR_FUNCTION_ARN), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPolicyHoldsAtMostFiveVersionsUntilOneIsDeleted() {
|
||||
service.createPolicy("capped", "{\"v\":1}", REGION);
|
||||
for (int v = 2; v <= 5; v++) {
|
||||
assertEquals(Integer.toString(v),
|
||||
service.createPolicyVersion("capped", "{\"v\":" + v + "}", true, REGION).getVersionId());
|
||||
}
|
||||
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> service.createPolicyVersion("capped", "{\"v\":6}", true, REGION));
|
||||
|
||||
assertEquals("VersionsLimitExceededException", e.getErrorCode());
|
||||
assertEquals(409, e.getHttpStatus());
|
||||
assertEquals(5, service.listPolicyVersions("capped", REGION).size());
|
||||
assertEquals("5", service.getPolicy("capped", REGION).getDefaultVersionId());
|
||||
service.deletePolicyVersion("capped", "2", REGION);
|
||||
assertEquals("6", service.createPolicyVersion("capped", "{\"v\":6}", true, REGION).getVersionId());
|
||||
assertEquals("6", service.getPolicy("capped", REGION).getDefaultVersionId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void racingVersionCreatesNeverPushAPolicyPastFiveVersions() throws Exception {
|
||||
service.createPolicy("raced", "{\"v\":1}", REGION);
|
||||
int writers = 8;
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
ExecutorService pool = Executors.newFixedThreadPool(writers);
|
||||
List<Future<Boolean>> outcomes = new ArrayList<>();
|
||||
try {
|
||||
for (int i = 0; i < writers; i++) {
|
||||
outcomes.add(pool.submit(() -> {
|
||||
start.await();
|
||||
try {
|
||||
service.createPolicyVersion("raced", "{\"v\":true}", false, REGION);
|
||||
return true;
|
||||
} catch (AwsException e) {
|
||||
assertEquals("VersionsLimitExceededException", e.getErrorCode());
|
||||
return false;
|
||||
}
|
||||
}));
|
||||
}
|
||||
start.countDown();
|
||||
int created = 0;
|
||||
for (Future<Boolean> outcome : outcomes) {
|
||||
if (outcome.get()) {
|
||||
created++;
|
||||
}
|
||||
}
|
||||
assertEquals(4, created, "exactly four of eight racing creates fit under the cap");
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
assertEquals(5, service.listPolicyVersions("raced", REGION).size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletingTheOldestVersionRemovesTheNumericallySmallestIdAndKeepsTheDefault() {
|
||||
service.createPolicy("pruned", "{\"v\":1}", REGION);
|
||||
for (int v = 2; v <= 10; v++) {
|
||||
if (service.listPolicyVersions("pruned", REGION).size() == IotService.MAX_POLICY_VERSIONS) {
|
||||
service.makeRoomForPolicyVersion("pruned", REGION);
|
||||
}
|
||||
service.createPolicyVersion("pruned", "{\"v\":" + v + "}", true, REGION);
|
||||
}
|
||||
assertEquals(List.of(6, 7, 8, 9, 10), versionIds("pruned"));
|
||||
|
||||
// Sorted as text, "10" would come before "6"; the oldest version is the numerically smallest id.
|
||||
service.makeRoomForPolicyVersion("pruned", REGION);
|
||||
|
||||
assertEquals(List.of(7, 8, 9, 10), versionIds("pruned"));
|
||||
assertEquals("10", service.getPolicy("pruned", REGION).getDefaultVersionId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletingTheOldestVersionMovesTheDefaultToTheNewestWhenTheOldestIsTheDefault() {
|
||||
service.createPolicy("pinned", "{\"v\":1}", REGION);
|
||||
for (int v = 2; v <= 5; v++) {
|
||||
service.createPolicyVersion("pinned", "{\"v\":" + v + "}", false, REGION);
|
||||
}
|
||||
assertEquals("1", service.getPolicy("pinned", REGION).getDefaultVersionId());
|
||||
|
||||
service.makeRoomForPolicyVersion("pinned", REGION);
|
||||
|
||||
assertEquals(List.of(2, 3, 4, 5), versionIds("pinned"));
|
||||
assertEquals("5", service.getPolicy("pinned", REGION).getDefaultVersionId());
|
||||
assertEquals("{\"v\":5}", service.getPolicy("pinned", REGION).getPolicyDocument());
|
||||
}
|
||||
|
||||
@Test
|
||||
void makingRoomOnAPolicyStoredWithMoreThanFiveVersionsDeletesDownToFourInOneStep() {
|
||||
// A policy persisted before the cap existed can hold more than five versions; the stores
|
||||
// hand out the live object, so adding to it is the same as having persisted it that way.
|
||||
service.createPolicy("legacy", "{\"v\":1}", REGION);
|
||||
for (int v = 2; v <= 5; v++) {
|
||||
service.createPolicyVersion("legacy", "{\"v\":" + v + "}", true, REGION);
|
||||
}
|
||||
IotPolicy stored = service.getPolicy("legacy", REGION);
|
||||
List<IotPolicy.PolicyVersion> versions = new ArrayList<>(stored.getVersions());
|
||||
for (int v = 6; v <= 8; v++) {
|
||||
IotPolicy.PolicyVersion version = new IotPolicy.PolicyVersion();
|
||||
version.setVersionId(Integer.toString(v));
|
||||
version.setDocument("{\"v\":" + v + "}");
|
||||
versions.add(version);
|
||||
}
|
||||
stored.setVersions(versions);
|
||||
assertEquals(List.of(1, 2, 3, 4, 5, 6, 7, 8), versionIds("legacy"));
|
||||
|
||||
service.makeRoomForPolicyVersion("legacy", REGION);
|
||||
|
||||
assertEquals(List.of(5, 6, 7, 8), versionIds("legacy"));
|
||||
assertEquals("5", service.getPolicy("legacy", REGION).getDefaultVersionId());
|
||||
assertEquals("9", service.createPolicyVersion("legacy", "{\"v\":9}", true, REGION).getVersionId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aPolicyDeletedWhileVersionsAreBeingCreatedStaysDeleted() throws Exception {
|
||||
for (int round = 0; round < 20; round++) {
|
||||
String name = "vanishing-" + round;
|
||||
service.createPolicy(name, "{\"v\":1}", REGION);
|
||||
CountDownLatch start = new CountDownLatch(1);
|
||||
ExecutorService pool = Executors.newFixedThreadPool(4);
|
||||
List<Future<?>> outcomes = new ArrayList<>();
|
||||
try {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
outcomes.add(pool.submit(() -> {
|
||||
start.await();
|
||||
try {
|
||||
service.createPolicyVersion(name, "{\"v\":2}", false, REGION);
|
||||
} catch (AwsException e) {
|
||||
assertEquals("ResourceNotFoundException", e.getErrorCode());
|
||||
}
|
||||
return null;
|
||||
}));
|
||||
}
|
||||
outcomes.add(pool.submit(() -> {
|
||||
start.await();
|
||||
service.deletePolicy(name, REGION);
|
||||
return null;
|
||||
}));
|
||||
start.countDown();
|
||||
for (Future<?> outcome : outcomes) {
|
||||
outcome.get();
|
||||
}
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.getPolicy(name, REGION));
|
||||
assertEquals("ResourceNotFoundException", e.getErrorCode(), "round " + round + " brought the policy back");
|
||||
}
|
||||
}
|
||||
|
||||
private List<Integer> versionIds(String policyName) {
|
||||
return service.listPolicyVersions(policyName, REGION).stream()
|
||||
.map(version -> Integer.parseInt(version.getVersionId()))
|
||||
.sorted()
|
||||
.toList();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user