From 82a550f6b080cb50912d93f7b13ba0fc97a95470 Mon Sep 17 00:00:00 2001 From: lukegleeson Date: Mon, 11 Jul 2022 10:55:53 +0100 Subject: Query CmHandles using CPS path Added withCpsPath condition parameter Validated to prevent misuse and blocking of querying using private properties Updated OpenAPI with examples and links to documentation Moved methods related to cmHandle querying using cps path from InventoryPersistence to CmHandleQueries Renamed private method deleteSchemaSetAndListElementByCmHandleId to deleteCmHandleByCmHandleId Issue-ID: CPS-977 Change-Id: I83827215b7e58de74f8f62cd0140516d217d93f1 Signed-off-by: lukegleeson --- .../CmHandleQueryRestParametersValidator.java | 36 +++++++++++++++++++--- .../org/onap/cps/utils/ValidQueryProperties.java | 36 ++++++++++++++++++++++ ...CmHandleQueryRestParametersValidatorSpec.groovy | 25 +++++++++++++++ 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 cps-service/src/main/java/org/onap/cps/utils/ValidQueryProperties.java (limited to 'cps-service') diff --git a/cps-service/src/main/java/org/onap/cps/utils/CmHandleQueryRestParametersValidator.java b/cps-service/src/main/java/org/onap/cps/utils/CmHandleQueryRestParametersValidator.java index c3811eb485..7fe47be2da 100644 --- a/cps-service/src/main/java/org/onap/cps/utils/CmHandleQueryRestParametersValidator.java +++ b/cps-service/src/main/java/org/onap/cps/utils/CmHandleQueryRestParametersValidator.java @@ -22,18 +22,17 @@ package org.onap.cps.utils; import com.google.common.base.Strings; import java.util.Arrays; -import java.util.List; import java.util.Map; import lombok.AccessLevel; import lombok.NoArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.onap.cps.spi.exceptions.DataValidationException; import org.onap.cps.spi.model.CmHandleQueryServiceParameters; +@Slf4j @NoArgsConstructor(access = AccessLevel.PRIVATE) public class CmHandleQueryRestParametersValidator { - private static final List VALID_PROPERTY_NAMES = Arrays.asList("hasAllProperties", "hasAllModules"); - /** * Validate cm handle query parameters. * @param cmHandleQueryServiceParameters name of data to be validated @@ -45,7 +44,8 @@ public class CmHandleQueryRestParametersValidator { if (Strings.isNullOrEmpty(conditionApiProperty.getConditionName())) { throwDataValidationException("Missing 'conditionName' - please supply a valid name."); } - if (!VALID_PROPERTY_NAMES.contains(conditionApiProperty.getConditionName())) { + if (Arrays.stream(ValidQueryProperties.values()).noneMatch(validQueryProperty -> + validQueryProperty.getQueryProperty().equals(conditionApiProperty.getConditionName()))) { throwDataValidationException( String.format("Wrong 'conditionName': %s - please supply a valid name.", conditionApiProperty.getConditionName())); @@ -89,6 +89,34 @@ public class CmHandleQueryRestParametersValidator { throwDataValidationException("Wrong module condition property. - please supply a valid condition property."); } + /** + * Validate CPS path condition properties. + * @param conditionProperty name of data to be validated + */ + public static boolean validateCpsPathConditionProperties(final Map conditionProperty) { + if (conditionProperty.isEmpty()) { + return true; + } + if (conditionProperty.size() > 1) { + throwDataValidationException("Only one condition property is allowed for the CPS path query."); + } + if (!conditionProperty.containsKey("cpsPath")) { + throwDataValidationException( + "Wrong CPS path condition property. - expecting \"cpsPath\" as the condition property."); + } + final String cpsPath = conditionProperty.get("cpsPath"); + if (cpsPath.isBlank()) { + throwDataValidationException( + "Wrong CPS path. - please supply a valid CPS path."); + } + if (cpsPath.contains("/additional-properties")) { + log.debug("{} - Private metadata cannot be queried. Nothing to be returned", + cpsPath); + return false; + } + return true; + } + private static void throwDataValidationException(final String details) { throw new DataValidationException("Invalid Query Parameter.", details); } diff --git a/cps-service/src/main/java/org/onap/cps/utils/ValidQueryProperties.java b/cps-service/src/main/java/org/onap/cps/utils/ValidQueryProperties.java new file mode 100644 index 0000000000..1d7ccb91df --- /dev/null +++ b/cps-service/src/main/java/org/onap/cps/utils/ValidQueryProperties.java @@ -0,0 +1,36 @@ +/* + * ============LICENSE_START======================================================= + * Copyright (C) 2022 Nordix Foundation + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + * ============LICENSE_END========================================================= + */ + +package org.onap.cps.utils; + +import lombok.Getter; + +@Getter +public enum ValidQueryProperties { + HAS_ALL_PROPERTIES("hasAllProperties"), + HAS_ALL_MODULES("hasAllModules"), + WITH_CPS_PATH("cmHandleWithCpsPath"); + + private final String queryProperty; + + ValidQueryProperties(final String queryProperty) { + this.queryProperty = queryProperty; + } +} diff --git a/cps-service/src/test/groovy/org/onap/cps/utils/CmHandleQueryRestParametersValidatorSpec.groovy b/cps-service/src/test/groovy/org/onap/cps/utils/CmHandleQueryRestParametersValidatorSpec.groovy index a9b04c1ced..d5dcb7fc5c 100644 --- a/cps-service/src/test/groovy/org/onap/cps/utils/CmHandleQueryRestParametersValidatorSpec.groovy +++ b/cps-service/src/test/groovy/org/onap/cps/utils/CmHandleQueryRestParametersValidatorSpec.groovy @@ -88,4 +88,29 @@ class CmHandleQueryRestParametersValidatorSpec extends Specification { 'invalid value' | [moduleName: ''] 'invalid name' | [wrongName: 'value'] } + + def 'Validate CmHandle where an exception is thrown due to #scenario.'() { + when: 'the validator is called on a cps path condition property' + CmHandleQueryRestParametersValidator.validateCpsPathConditionProperties(conditionProperty) + then: 'a data validation exception is thrown' + def e = thrown(DataValidationException) + and: 'exception message matches the expected message' + e.details.contains(exceptionMessage) + where: + scenario | conditionProperty || exceptionMessage + 'more than one condition is supplied' | ['cpsPath':'some-path', 'cpsPath2':'some-path'] || 'Only one condition property is allowed for the CPS path query.' + 'cpsPath key not supplied' | ['wrong-key':'some-path'] || 'Wrong CPS path condition property. - expecting "cpsPath" as the condition property.' + 'cpsPath not supplied' | ['cpsPath':''] || 'Wrong CPS path. - please supply a valid CPS path.' + } + + def 'Validate CmHandle where #scenario.'() { + when: 'the validator is called on a cps path condition property' + def result = CmHandleQueryRestParametersValidator.validateCpsPathConditionProperties(['cpsPath':cpsPath]) + then: 'the expected boolean value is returned' + result == expectedBoolean + where: + scenario | cpsPath || expectedBoolean + 'cpsPath is valid' | '/some/valid/path' || true + 'cpsPath attempts to query private properties' | "//additional-properties[@some-property='some-value']" || false + } } -- cgit 1.2.3-korg