From 042a8f5ec583a7431971847995f1aabd9bc48366 Mon Sep 17 00:00:00 2001 From: liamfallon Date: Mon, 6 Jul 2020 09:31:18 +0100 Subject: Fix URL checking on REST plugins The URL parameter must always be provided on the REST client carrier plugin The URL parameter must always be provided on the REST requestor consumer The URL parameter is not required on the REST requestor producer This patch fixes the implementation to always checks the URL for existence on the REST Client plugin. The current checks on the REST Requestor are correct for the REST requestor plugin consumer and producer classes, so the checks are not required in the REST Requestor parameters. Issue-ID: POLICY-2692 Change-Id: I4e1534202002cf4a03bd159ac97b2a4f5b1746ab Signed-off-by: liamfallon --- .../examples/config/ONAPvCPE/ApexConfig.json | 4 ++- .../RestClientCarrierTechnologyParameters.java | 17 +++++++++++ .../RestRequestorCarrierTechnologyParameters.java | 32 -------------------- .../RestPluginCarrierTechnologyParameters.java | 34 +++++++++++----------- .../restclient/TestExecutionPropertyRest.java | 26 ++++++++--------- 5 files changed, 50 insertions(+), 63 deletions(-) diff --git a/examples/examples-onap-vcpe/src/main/resources/examples/config/ONAPvCPE/ApexConfig.json b/examples/examples-onap-vcpe/src/main/resources/examples/config/ONAPvCPE/ApexConfig.json index 5208767b0..01269c6ba 100644 --- a/examples/examples-onap-vcpe/src/main/resources/examples/config/ONAPvCPE/ApexConfig.json +++ b/examples/examples-onap-vcpe/src/main/resources/examples/config/ONAPvCPE/ApexConfig.json @@ -40,7 +40,9 @@ "url": "http://172.18.0.6:8081/pdp/api/getDecision", "httpMethod": "POST", "restRequestTimeout": 2000, - "httpHeader": "Authorization:dGVzdHBkcDphbHBoYTEyMw== ClientAuth:cHl0aG9uOnRlc3Q=" + "httpHeaders": [ + ["Authorization:dGVzdHBkcDphbHBoYTEyMw== ClientAuth:cHl0aG9uOnRlc3Q="] + ] } }, "eventProtocolParameters": { diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java index 47e94ff51..d5bca3f5a 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restclient/src/main/java/org/onap/policy/apex/plugins/event/carrier/restclient/RestClientCarrierTechnologyParameters.java @@ -24,6 +24,8 @@ package org.onap.policy.apex.plugins.event.carrier.restclient; import lombok.Getter; import lombok.Setter; import org.onap.policy.apex.service.parameters.carriertechnology.RestPluginCarrierTechnologyParameters; +import org.onap.policy.common.parameters.GroupValidationResult; +import org.onap.policy.common.parameters.ValidationStatus; // @formatter:off /** @@ -59,4 +61,19 @@ public class RestClientCarrierTechnologyParameters extends RestPluginCarrierTech this.setEventProducerPluginClass(ApexRestClientProducer.class.getName()); this.setEventConsumerPluginClass(ApexRestClientConsumer.class.getName()); } + + /** + * {@inheritDoc} + */ + @Override + public GroupValidationResult validateUrl(final GroupValidationResult result) { + // Check if the URL has been set for event output + final String urlNullMessage = "no URL has been set for event sending on " + getLabel(); + if (getUrl() == null) { + result.setResult("url", ValidationStatus.INVALID, urlNullMessage); + return result; + } + + return super.validateUrl(result); + } } diff --git a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java index 9f3a631e1..9e55e22d6 100644 --- a/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java +++ b/plugins/plugins-event/plugins-event-carrier/plugins-event-carrier-restrequestor/src/main/java/org/onap/policy/apex/plugins/event/carrier/restrequestor/RestRequestorCarrierTechnologyParameters.java @@ -21,12 +21,9 @@ package org.onap.policy.apex.plugins.event.carrier.restrequestor; -import java.util.regex.Matcher; import lombok.Getter; import lombok.Setter; import org.onap.policy.apex.service.parameters.carriertechnology.RestPluginCarrierTechnologyParameters; -import org.onap.policy.common.parameters.GroupValidationResult; -import org.onap.policy.common.parameters.ValidationStatus; // @formatter:off /** @@ -70,33 +67,4 @@ public class RestRequestorCarrierTechnologyParameters extends RestPluginCarrierT this.setEventProducerPluginClass(ApexRestRequestorProducer.class.getName()); this.setEventConsumerPluginClass(ApexRestRequestorConsumer.class.getName()); } - - // @formatter:off - /** - * Validate the URL. - * - *

Checks: - * http://www.blah.com/{par1/somethingelse (Missing end tag) use {[^\\{}]*$ - * http://www.blah.com/{par1/{some}thingelse (Nested tag) use {[^}]*{ - * http://www.blah.com/{par1}/some}thingelse (Missing start tag1) use }[^{}]*.} - * http://www.blah.com/par1}/somethingelse (Missing start tag2) use }[^{}]*} - * http://www.blah.com/{}/somethingelse (Empty tag) use {[\s]*} - * @param result the result of the validation - */ - // @formatter:on - @Override - public GroupValidationResult validateUrl(final GroupValidationResult result) { - // URL is only set on Requestor consumers - if (getUrl() == null) { - return result; - } - - Matcher matcher = patternErrorKey.matcher(getUrl()); - if (matcher.find()) { - result.setResult("url", ValidationStatus.INVALID, - "no proper URL has been set for event sending on REST requestor"); - } - - return result; - } } diff --git a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/RestPluginCarrierTechnologyParameters.java b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/RestPluginCarrierTechnologyParameters.java index 9a3311905..eab936edc 100644 --- a/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/RestPluginCarrierTechnologyParameters.java +++ b/services/services-engine/src/main/java/org/onap/policy/apex/service/parameters/carriertechnology/RestPluginCarrierTechnologyParameters.java @@ -64,12 +64,14 @@ public class RestPluginCarrierTechnologyParameters extends CarrierTechnologyPara private static final Logger LOGGER = LoggerFactory.getLogger(RestPluginCarrierTechnologyParameters.class); /** The supported HTTP methods. */ + // @formatter:off public enum HttpMethod { GET, PUT, POST, DELETE } + // @formatter:on /** The default HTTP code filter, allows 2xx HTTP codes through. */ public static final String DEFAULT_HTTP_CODE_FILTER = "[2][0-9][0-9]"; @@ -80,8 +82,8 @@ public class RestPluginCarrierTechnologyParameters extends CarrierTechnologyPara // Regular expression patterns for finding and checking keys in URLs private static final Pattern patternProperKey = Pattern.compile("(?<=\\{)[^}]*(?=\\})"); - protected static final Pattern patternErrorKey = - Pattern.compile("(\\{[^\\{}]*.?\\{)|(\\{[^\\{}]*$)|(\\}[^\\{}]*.?\\})|(^[^\\{}]*.?\\})|\\{\\s*\\}"); + protected static final Pattern patternErrorKey = Pattern + .compile("(\\{[^\\{}]*.?\\{)|(\\{[^\\{}]*$)|(\\}[^\\{}]*.?\\})|(^[^\\{}]*.?\\})|\\{\\s*\\}"); // variable protected String url = null; @@ -90,8 +92,8 @@ public class RestPluginCarrierTechnologyParameters extends CarrierTechnologyPara protected String httpCodeFilter = DEFAULT_HTTP_CODE_FILTER; /** - * Constructor to create a REST carrier technology parameters instance and register the instance with the parameter - * service. + * Constructor to create a REST carrier technology parameters instance and + * register the instance with the parameter service. */ public RestPluginCarrierTechnologyParameters() { super(); @@ -176,16 +178,16 @@ public class RestPluginCarrierTechnologyParameters extends CarrierTechnologyPara */ // @formatter:on public GroupValidationResult validateUrl(final GroupValidationResult result) { - // Check if the URL has been set for event output - String urlErrorMessage = "no URL has been set for event sending on " + getLabel(); + // The URL may be optional so existence must be checked in the plugin code if (getUrl() == null) { - result.setResult("url", ValidationStatus.INVALID, urlErrorMessage); return result; } Matcher matcher = patternErrorKey.matcher(getUrl()); if (matcher.find()) { - result.setResult("url", ValidationStatus.INVALID, urlErrorMessage); + final String urlInvalidMessage = "invalid URL " + getUrl() + " has been set for event sending on " + + getLabel(); + result.setResult("url", ValidationStatus.INVALID, urlInvalidMessage); } return result; @@ -206,14 +208,13 @@ public class RestPluginCarrierTechnologyParameters extends CarrierTechnologyPara result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, "HTTP header array entry is null"); } else if (httpHeader.length != 2) { result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, - "HTTP header array entries must have one key and one value: " - + Arrays.deepToString(httpHeader)); + "HTTP header array entries must have one key and one value: " + Arrays.deepToString(httpHeader)); } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[0])) { result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, - "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader)); + "HTTP header key is null or blank: " + Arrays.deepToString(httpHeader)); } else if (!ParameterValidationUtils.validateStringParameter(httpHeader[1])) { result.setResult(HTTP_HEADERS, ValidationStatus.INVALID, - "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader)); + "HTTP header value is null or blank: " + Arrays.deepToString(httpHeader)); } } @@ -231,14 +232,13 @@ public class RestPluginCarrierTechnologyParameters extends CarrierTechnologyPara } else if (StringUtils.isBlank(httpCodeFilter)) { result.setResult(HTTP_CODE_FILTER, ValidationStatus.INVALID, - "HTTP code filter must be specified as a three digit regular expression"); + "HTTP code filter must be specified as a three digit regular expression"); } else { try { Pattern.compile(httpCodeFilter); } catch (PatternSyntaxException pse) { - String message = - "Invalid HTTP code filter, the filter must be specified as a three digit regular expression: " - + pse.getMessage(); + String message = "Invalid HTTP code filter, the filter must be specified as a three digit " + + "regular expression: " + pse.getMessage(); result.setResult(HTTP_CODE_FILTER, ValidationStatus.INVALID, message); LOGGER.debug(message, pse); } @@ -253,6 +253,6 @@ public class RestPluginCarrierTechnologyParameters extends CarrierTechnologyPara @Override public String toString() { return getLabel() + "CarrierTechnologyParameters [url=" + url + ", httpMethod=" + httpMethod + ", httpHeaders=" - + Arrays.deepToString(httpHeaders) + ", httpCodeFilter=" + httpCodeFilter + "]"; + + Arrays.deepToString(httpHeaders) + ", httpCodeFilter=" + httpCodeFilter + "]"; } } diff --git a/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestExecutionPropertyRest.java b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestExecutionPropertyRest.java index e483597d0..71c8833be 100644 --- a/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestExecutionPropertyRest.java +++ b/testsuites/integration/integration-uservice-test/src/test/java/org/onap/policy/apex/testsuites/integration/uservice/adapt/restclient/TestExecutionPropertyRest.java @@ -63,8 +63,7 @@ public class TestExecutionPropertyRest { @BeforeClass public static void compilePolicy() { // @formatter:off - final String[] cliArgs = { - "-c", + final String[] cliArgs = { "-c", "src/test/resources/policies/executionproperties/policy/ExecutionPropertiesRestTestPolicyModel.apex", "-l", "target/ExecutionPropertiesRestTestPolicyModel.log", @@ -88,7 +87,7 @@ public class TestExecutionPropertyRest { } server = HttpServletServerFactoryInstance.getServerFactory().build("TestExecutionPropertyRest", false, null, - PORT, "/TestExecutionRest", false, false); + PORT, "/TestExecutionRest", false, false); server.addServletClass(null, TestRestClientEndpoint.class.getName()); server.setSerializationProvider(GsonMessageBodyHandler.class.getName()); @@ -129,7 +128,7 @@ public class TestExecutionPropertyRest { System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); - final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventBadUrl.json"}; + final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventBadUrl.json" }; final ApexMain apexMain = new ApexMain(args); ThreadUtilities.sleep(500); @@ -142,7 +141,8 @@ public class TestExecutionPropertyRest { System.setErr(stderr); LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl"); - assertTrue(outString.contains("no URL has been set for event sending on RESTCLIENT")); + assertTrue(outString.contains("invalid URL http://localhost:32801/TestExecutionRest/apex/event/tagId}" + + " has been set for event sending on RESTCLIENT")); } /** @@ -153,7 +153,7 @@ public class TestExecutionPropertyRest { System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); - final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json"}; + final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventNoValueSetForTag.json" }; final ApexMain apexMain = new ApexMain(args); ThreadUtilities.sleep(2000); @@ -167,7 +167,7 @@ public class TestExecutionPropertyRest { LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl"); assertTrue(outString.contains("key \"Number\" specified on url \"http://localhost:32801/TestExecutionRest/apex" - + "/event/{tagId}/{Number}\" not found in execution properties passed by the current policy")); + + "/event/{tagId}/{Number}\" not found in execution properties passed by the current policy")); } /** @@ -178,7 +178,7 @@ public class TestExecutionPropertyRest { System.setOut(new PrintStream(outContent)); System.setErr(new PrintStream(errContent)); - final String[] args = {"src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json"}; + final String[] args = { "src/test/resources/testdata/executionproperties/RESTEventBadHttpCodeFilter.json" }; final ApexMain apexMain = new ApexMain(args); ThreadUtilities.sleep(500); @@ -201,7 +201,8 @@ public class TestExecutionPropertyRest { public void testReplaceUrlTag() throws Exception { final Client client = ClientBuilder.newClient(); - final String[] args = {"src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json"}; + final String[] args = { + "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToTagUrlOK.json" }; final ApexMain apexMain = new ApexMain(args); ThreadUtilities.sleep(1000); apexMain.shutdown(); @@ -212,7 +213,7 @@ public class TestExecutionPropertyRest { Response response = null; response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl") - .request("application/json").get(); + .request("application/json").get(); LOGGER.info("testReplaceUrlTag-OUTSTRING=\n" + outString + "\nEnd-TagUrl"); final String responseEntity = response.readEntity(String.class); @@ -227,8 +228,7 @@ public class TestExecutionPropertyRest { final Client client = ClientBuilder.newClient(); // @formatter:off final String[] args = { - "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json" - }; + "src/test/resources/testdata/executionproperties/RESTHttpCodeFilterSetToMultiTagUrlOK.json" }; // @formatter:on final ApexMain apexMain = new ApexMain(args); ThreadUtilities.sleep(1500); @@ -238,7 +238,7 @@ public class TestExecutionPropertyRest { System.setErr(stderr); Response response = null; response = client.target("http://localhost:32801/TestExecutionRest/apex/event/GetProperUrl") - .request("application/json").get(); + .request("application/json").get(); final String responseEntity = response.readEntity(String.class); LOGGER.info("testReplaceUrlMultiTag-OUTSTRING=\n" + responseEntity + "\nEnd-MultiTagUrl"); assertTrue(responseEntity.contains("\"PostProperUrl\": 3")); -- cgit 1.2.3-korg