diff --git a/data-prepper-plugin-framework/src/main/java/org/opensearch/dataprepper/plugin/VariableExpander.java b/data-prepper-plugin-framework/src/main/java/org/opensearch/dataprepper/plugin/VariableExpander.java index 7b249f0389..a23c3e4cd6 100644 --- a/data-prepper-plugin-framework/src/main/java/org/opensearch/dataprepper/plugin/VariableExpander.java +++ b/data-prepper-plugin-framework/src/main/java/org/opensearch/dataprepper/plugin/VariableExpander.java @@ -57,8 +57,47 @@ public T translate(final JsonParser jsonParser, final Class destinationTy }) .findFirst() - .orElseGet(() -> objectMapper.convertValue(rawValue, destinationType)); + .orElseGet(() -> { + // This change is to support any call + // to validate the secret with a placeholder secret expression like "AWS_SECRET_EXPRESSION" + // which is not of a secrets expression that we would check (filter check above fails) but + // still expects an instance of PluginConfigVariable object returned + if (destinationType.equals(PluginConfigVariable.class)) { + return destinationType.cast(new ImmutablePluginConfigVariable(rawValue)); + } + return objectMapper.convertValue(rawValue, destinationType); + }); } return objectMapper.readValue(jsonParser, destinationType); } + + private static class ImmutablePluginConfigVariable implements PluginConfigVariable { + private final Object rawValue; + + private ImmutablePluginConfigVariable(final Object rawValue) { + this.rawValue = rawValue; + } + + @Override + public Object getValue() { + return rawValue; + } + + @Override + public void setValue(Object updatedValue) { + throw new UnsupportedOperationException("ImmutablePluginConfigVariable doesn't support this operation"); + } + + @Override + public void refresh() { + // No-op as this is immutable + throw new UnsupportedOperationException("ImmutablePluginConfigVariable doesn't support this operation"); + } + + @Override + public boolean isUpdatable() { + return false; + } + } + } diff --git a/data-prepper-plugin-framework/src/test/java/org/opensearch/dataprepper/plugin/VariableExpanderTest.java b/data-prepper-plugin-framework/src/test/java/org/opensearch/dataprepper/plugin/VariableExpanderTest.java index 6e4fd88681..ded8728e87 100644 --- a/data-prepper-plugin-framework/src/test/java/org/opensearch/dataprepper/plugin/VariableExpanderTest.java +++ b/data-prepper-plugin-framework/src/test/java/org/opensearch/dataprepper/plugin/VariableExpanderTest.java @@ -28,10 +28,14 @@ import java.util.Collections; import java.util.Map; import java.util.Set; +import java.util.UUID; import java.util.stream.Stream; import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.eq; @@ -177,6 +181,20 @@ public boolean isUpdatable() { assertThat(actualResult, equalTo(mockPluginConfigVariable)); } + @Test + void testTranslateJsonParserWithSPluginConfigVariableValue_when_non_secret_format_is_given() throws IOException { + final String testSecretReference = UUID.randomUUID().toString(); + final JsonParser jsonParser = JSON_FACTORY.createParser(String.format("\"%s\"", testSecretReference)); + jsonParser.nextToken(); + objectUnderTest = new VariableExpander(OBJECT_MAPPER, Set.of(pluginConfigValueTranslator)); + final Object actualResult = objectUnderTest.translate(jsonParser, PluginConfigVariable.class); + assertNotNull(actualResult); + assertInstanceOf(PluginConfigVariable.class, actualResult); + PluginConfigVariable pluginConfigVariableInstance = (PluginConfigVariable) actualResult; + assertEquals(testSecretReference, pluginConfigVariableInstance.getValue().toString()); + assertFalse(pluginConfigVariableInstance.isUpdatable()); + } + @Test void testTranslateJsonParserWithSPluginConfigVariableValue_translate_failure() throws IOException { final String testSecretKey = "testSecretKey";