diff --git a/src/main/java/io/github/hectorvent/floci/services/pipes/PipesService.java b/src/main/java/io/github/hectorvent/floci/services/pipes/PipesService.java index 319184dd70..b4ba5106ec 100644 --- a/src/main/java/io/github/hectorvent/floci/services/pipes/PipesService.java +++ b/src/main/java/io/github/hectorvent/floci/services/pipes/PipesService.java @@ -141,6 +141,10 @@ public class PipesService implements TagHandler, ResourceProvider { /** * Puts the pipe back to a configuration held in full: a property given as null is cleared, so * the pipe ends up carrying exactly what is passed here and nothing the caller left out. + * + *

The configuration was accepted when the pipe was written, so it is not validated again: + * a snapshot that predates a rule added since must still be restorable, or a CloudFormation + * rollback would fail on the very pipe it is putting back. */ public Pipe restorePipe(String name, String target, String roleArn, String description, DesiredState desiredState, String enrichment, @@ -155,7 +159,11 @@ public class PipesService implements TagHandler, ResourceProvider { * *

With {@code clearUnsetProperties} false, a null property leaves the pipe's value alone: * that is what UpdatePipe promises its callers. With it true, a null property clears the pipe's - * value, which is what putting a pipe back to a configuration recorded in full needs. + * value, which is what putting a pipe back to a configuration recorded in full needs; that + * configuration was accepted when it was written, so only the update path validates. + * + *

Validation runs on the same retrieved pipe the write then mutates, so a pipe replaced + * between two lookups cannot be validated as one pipe and updated as another. */ private Pipe writePipeConfiguration(String name, String target, String roleArn, String description, DesiredState desiredState, String enrichment, @@ -167,10 +175,10 @@ public class PipesService implements TagHandler, ResourceProvider { .orElseThrow(() -> new AwsException("NotFoundException", "Pipe " + name + " does not exist.", 404)); - JsonNode effectiveSourceParameters = sourceParameters == null && !clearUnsetProperties - ? pipe.getSourceParameters() - : sourceParameters; - validateSourceConfiguration(pipe.getSource(), effectiveSourceParameters); + if (!clearUnsetProperties) { + validateSourceConfiguration(pipe.getSource(), + sourceParameters != null ? sourceParameters : pipe.getSourceParameters()); + } writeProperty(target, clearUnsetProperties, pipe::setTarget); writeProperty(roleArn, clearUnsetProperties, pipe::setRoleArn); diff --git a/src/test/java/io/github/hectorvent/floci/services/pipes/PipesServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/pipes/PipesServiceTest.java index e47fc9303d..24a2e1a467 100644 --- a/src/test/java/io/github/hectorvent/floci/services/pipes/PipesServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/pipes/PipesServiceTest.java @@ -25,13 +25,15 @@ class PipesServiceTest { private static final ObjectMapper MAPPER = new ObjectMapper(); + private AccountAwareStorageBackend storage; private PipesService pipesService; @BeforeEach void setUp() { + storage = AccountAwareStorageBackend.inMemory("000000000000"); StorageFactory storageFactory = Mockito.mock(StorageFactory.class); - when(storageFactory.create(Mockito.anyString(), Mockito.anyString(), Mockito.any())) - .thenReturn(AccountAwareStorageBackend.inMemory("000000000000")); + Mockito.doReturn(storage).when(storageFactory) + .create(Mockito.anyString(), Mockito.anyString(), Mockito.any()); RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000"); @@ -233,6 +235,38 @@ class PipesServiceTest { assertEquals(PipeState.STOPPED, restored.getCurrentState()); } + /** + * A restore puts back a configuration that was accepted when it was written, so it is not run + * through the source validation again. A persisted pipe can predate a rule added since, and a + * CloudFormation rollback that re-validated its snapshot would fail on the very pipe it is + * putting back. The pipe is seeded straight into storage, as one written before the Kafka + * TopicName check existed would be. + */ + @Test + void restorePipeDoesNotRevalidateAnAlreadyAcceptedConfiguration() { + JsonNode legacyParameters = sourceParameters(""" + {"SelfManagedKafkaParameters":{"BatchSize":10}}"""); + Pipe legacy = new Pipe(); + legacy.setName("legacy-pipe"); + legacy.setArn("arn:aws:pipes:us-east-1:000000000000:pipe/legacy-pipe"); + legacy.setSource("smk://broker:9092"); + legacy.setTarget("arn:aws:sqs:us-east-1:000000000000:target"); + legacy.setRoleArn("arn:aws:iam::000000000000:role/role"); + legacy.setDesiredState(DesiredState.RUNNING); + legacy.setCurrentState(PipeState.RUNNING); + legacy.setSourceParameters(legacyParameters); + storage.put("us-east-1::legacy-pipe", legacy); + + Pipe restored = pipesService.restorePipe("legacy-pipe", + "arn:aws:sqs:us-east-1:000000000000:target", + "arn:aws:iam::000000000000:role/role", + null, null, null, legacyParameters, null, null, "us-east-1"); + + assertEquals(legacyParameters, restored.getSourceParameters()); + assertEquals(legacyParameters, + pipesService.describePipe("legacy-pipe", "us-east-1").getSourceParameters()); + } + @Test void deletePipe() { pipesService.createPipe("del-pipe",