Skip to content

Commit

Permalink
validate deploy variable value schema validation
Browse files Browse the repository at this point in the history
  • Loading branch information
swaroopar committed Oct 20, 2023
1 parent d81ff9e commit 41a407e
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 121 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.eclipse.xpanse.modules.models.response.Response;
import org.eclipse.xpanse.modules.models.response.ResultType;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.IconProcessingFailedException;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.InvalidValueSchemaException;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.ServiceTemplateAlreadyRegistered;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.ServiceTemplateNotRegistered;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.ServiceTemplateUpdateNotAllowed;
Expand Down Expand Up @@ -81,7 +82,7 @@ public Response handleTerraformScriptFormatInvalidException(
}

/**
* Exception handler for handleServiceUpdateNotAllowed.
* Exception handler for ServiceTemplateUpdateNotAllowed.
*/
@ExceptionHandler({ServiceTemplateUpdateNotAllowed.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
Expand All @@ -91,4 +92,16 @@ public Response handleServiceTemplateUpdateNotAllowed(
return Response.errorResponse(ResultType.SERVICE_TEMPLATE_UPDATE_NOT_ALLOWED,
Collections.singletonList(ex.getMessage()));
}

/**
* Exception handler for ServiceTemplateUpdateNotAllowed.
*/
@ExceptionHandler({InvalidValueSchemaException.class})
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public Response handleInvalidValueSchemaException(
InvalidValueSchemaException ex) {
return Response.errorResponse(ResultType.VARIABLE_SCHEMA_DEFINITION_INVALID,
ex.getInvalidValueSchemaKeys());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.List;
import org.eclipse.xpanse.api.controllers.ServiceTemplateApi;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.IconProcessingFailedException;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.InvalidValueSchemaException;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.ServiceTemplateAlreadyRegistered;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.ServiceTemplateNotRegistered;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.ServiceTemplateUpdateNotAllowed;
Expand Down Expand Up @@ -105,4 +106,15 @@ void testServiceTemplateUpdateNotAllowed() throws Exception {
.andExpect(jsonPath("$.resultType").value("Service Template Update Not Allowed"))
.andExpect(jsonPath("$.details[0]").value("test error"));
}

@Test
void testInvalidValueSchemaException() throws Exception {
when(serviceTemplateManage.listServiceTemplates(any(ServiceTemplateQueryModel.class)))
.thenThrow(new InvalidValueSchemaException(List.of("test error")));

this.mockMvc.perform(get("/xpanse/service_templates"))
.andExpect(status().is(400))
.andExpect(jsonPath("$.resultType").value("Variable Schema Definition Invalid"))
.andExpect(jsonPath("$.details[0]").value("test error"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public enum ResultType {
UNSUPPORTED_ENUM_VALUE("Unsupported Enum Value"),
TERRAFORM_BOOT_REQUEST_FAILED("Terraform Boot Request Failed"),
METRICS_DATA_NOT_READY("Metrics Data Not Ready"),
VARIABLE_VALIDATION_FAILED("Variable Validation Failed");
VARIABLE_VALIDATION_FAILED("Variable Validation Failed"),
VARIABLE_SCHEMA_DEFINITION_INVALID("Variable Schema Definition Invalid");

private final String value;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@

package org.eclipse.xpanse.modules.models.service.utils;

import com.networknt.schema.JsonMetaSchema;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.xpanse.modules.models.servicetemplate.DeployVariable;
import org.eclipse.xpanse.modules.models.servicetemplate.enums.DeployVariableKind;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.InvalidValueSchemaException;
import org.eclipse.xpanse.modules.models.servicetemplate.utils.JsonObjectSchema;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
Expand All @@ -27,7 +30,7 @@ public class ServiceVariablesJsonSchemaGenerator {

private static final String VARIABLE_TYPE_KEY = "type";
private static final String VARIABLE_DESCRIPTION_KEY = "description";
private static final String VARIABLE_EXAMPLE_KEY = "example";
private static final String VARIABLE_EXAMPLE_KEY = "examples";

/**
* Generate JsonSchema objects to describe validation rules and metadata information for a set
Expand All @@ -50,9 +53,7 @@ public JsonObjectSchema buildJsonObjectSchema(List<DeployVariable> deployVariabl
}

if (!CollectionUtils.isEmpty(variable.getValueSchema())) {
variable.getValueSchema().forEach((validator, value) ->
validationProperties.put(validator.toValue(), value)
);
validationProperties.putAll(variable.getValueSchema());
}

if (Objects.nonNull(variable.getDataType())) {
Expand All @@ -75,7 +76,24 @@ public JsonObjectSchema buildJsonObjectSchema(List<DeployVariable> deployVariabl

jsonObjectSchema.setRequired(requiredList);
jsonObjectSchema.setProperties(jsonObjectSchemaProperties);
validateSchemaDefinition(jsonObjectSchema);
return jsonObjectSchema;
}

private void validateSchemaDefinition(JsonObjectSchema jsonObjectSchema) {
Set<String> allowedKeys = JsonMetaSchema.getV202012().getKeywords().keySet();
List<String> invalidKeys = new ArrayList<>();
jsonObjectSchema.getProperties().forEach((deployVariable, variableValueSchema) ->
variableValueSchema.forEach((schemaDefKey, schemaDefValue) -> {
if (!allowedKeys.contains(schemaDefKey)) {
invalidKeys.add(String.format(
"Value schema key %s in deploy variable %s is invalid",
schemaDefKey, deployVariable));
}
}));
if (!invalidKeys.isEmpty()) {
throw new InvalidValueSchemaException(invalidKeys);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import java.io.Serializable;
import java.util.Map;
import lombok.Data;
import org.eclipse.xpanse.modules.models.service.deploy.enums.VariableValidator;
import org.eclipse.xpanse.modules.models.servicetemplate.enums.DeployVariableDataType;
import org.eclipse.xpanse.modules.models.servicetemplate.enums.DeployVariableKind;
import org.eclipse.xpanse.modules.models.servicetemplate.enums.SensitiveScope;
Expand Down Expand Up @@ -55,8 +54,10 @@ public class DeployVariable implements Serializable {
@Schema(description = "Indicates if the variable is mandatory")
private Boolean mandatory;

@Schema(description = "valueSchema of the variable")
private Map<VariableValidator, Object> valueSchema;
@Schema(description = "valueSchema of the variable. "
+ "The key be any keyword that is part of the JSON schema definition which can be "
+ "found here https://json-schema.org/draft/2020-12/schema")
private Map<String, Object> valueSchema;

@Schema(description = "Sensitive scope of variable storage")
private SensitiveScope sensitiveScope = SensitiveScope.NONE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* SPDX-License-Identifier: Apache-2.0
* SPDX-FileCopyrightText: Huawei Inc.
*/

package org.eclipse.xpanse.modules.models.servicetemplate.exceptions;

import java.util.List;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
* Exception thrown when a deployment variable schema definition is invalid.
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class InvalidValueSchemaException extends RuntimeException {

private final List<String> invalidValueSchemaKeys;

public InvalidValueSchemaException(List<String> invalidValueSchemaKeys) {
this.invalidValueSchemaKeys = invalidValueSchemaKeys;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import lombok.EqualsAndHashCode;

/**
* Exception thrown when Terraform script is invalid.
* Exception thrown when a Terraform script is invalid.
*/
@EqualsAndHashCode(callSuper = true)
@Data
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.eclipse.xpanse.modules.models.service.deploy.exceptions.VariableInvalidException;
import org.eclipse.xpanse.modules.models.servicetemplate.DeployVariable;
import org.eclipse.xpanse.modules.models.servicetemplate.Ocl;
import org.eclipse.xpanse.modules.models.servicetemplate.exceptions.InvalidValueSchemaException;
import org.eclipse.xpanse.modules.models.servicetemplate.utils.JsonObjectSchema;
import org.eclipse.xpanse.modules.models.servicetemplate.utils.OclLoader;
import org.junit.jupiter.api.BeforeEach;
Expand All @@ -31,7 +32,7 @@
@ExtendWith(SpringExtension.class)
@ContextConfiguration(classes = {ServiceVariablesJsonSchemaGenerator.class,
ServiceVariablesJsonSchemaValidator.class})
public class ServiceVariablesJsonSchemaGeneratorAndValidatorTest {
class ServiceVariablesJsonSchemaGeneratorAndValidatorTest {

@Autowired
ServiceVariablesJsonSchemaGenerator serviceVariablesJsonSchemaGenerator;
Expand Down Expand Up @@ -146,4 +147,12 @@ void validateWithDataType_test() {
}
}

@Test
void throwExceptionWhenValueSchemaIsInvalid() {
variables.get(0).getValueSchema().put("enums", List.of(1, 2, 3));
assertThrows(InvalidValueSchemaException.class, () -> {
serviceVariablesJsonSchemaGenerator.buildJsonObjectSchema(variables);
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import java.util.Map;
import org.eclipse.xpanse.modules.models.service.deploy.enums.VariableValidator;
import org.eclipse.xpanse.modules.models.servicetemplate.enums.DeployVariableDataType;
import org.eclipse.xpanse.modules.models.servicetemplate.enums.DeployVariableKind;
import org.eclipse.xpanse.modules.models.servicetemplate.enums.SensitiveScope;
Expand All @@ -29,7 +28,7 @@ class DeployVariableTest {
private static final String description = "description";
private static final String value = "value";
private static final Boolean mandatory = true;
private static final Map<VariableValidator,Object> validatorMap = Map.of(VariableValidator.MINLENGTH,"10");
private static final Map<String,Object> validatorMap = Map.of("minLength","10");
private static final SensitiveScope sensitiveScope = SensitiveScope.ONCE;
private static DeployVariable deployVariable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,15 @@ public ServiceTemplateEntity registerServiceTemplate(Ocl ocl) {
log.error(errorMsg);
throw new ServiceTemplateAlreadyRegistered(errorMsg);
}
JsonObjectSchema jsonObjectSchema =
serviceVariablesJsonSchemaGenerator.buildJsonObjectSchema(
newEntity.getOcl().getDeployment().getVariables());
validateTerraformScript(ocl);
if (StringUtils.isNotBlank(getUserNamespace())) {
newEntity.setNamespace(getUserNamespace());
} else {
newEntity.setNamespace(ocl.getNamespace());
}
JsonObjectSchema jsonObjectSchema =
serviceVariablesJsonSchemaGenerator.buildJsonObjectSchema(
newEntity.getOcl().getDeployment().getVariables());
newEntity.setJsonObjectSchema(jsonObjectSchema);
storage.store(newEntity);
serviceTemplateOpenApiGenerator.generateServiceApi(newEntity);
Expand Down

0 comments on commit 41a407e

Please sign in to comment.