use of io.gravitee.rest.api.service.exceptions.InvalidDataException in project gravitee-management-rest-api by gravitee-io.
the class PolicyServiceTest method shouldRejectInvalidJsonConfigurationWithCustomJavaRegexFormat.
@Test(expected = InvalidDataException.class)
public void shouldRejectInvalidJsonConfigurationWithCustomJavaRegexFormat() throws Exception {
try {
String schemaWithJavaRegexFormat = JSON_SCHEMA.replace("\"type\": \"string\"\n", "\"type\": \"string\"\n,\"format\": \"java-regex\"");
Policy policy = new Policy();
policy.setName("my-policy");
policy.setConfiguration("{ \"name\": \"( INVALID regex\", \"valid\": true }");
when(policyManager.getSchema("my-policy")).thenReturn(schemaWithJavaRegexFormat);
policyService.validatePolicyConfiguration(policy);
} catch (InvalidDataException e) {
assertEquals("Invalid policy configuration : Invalid java regular expression [( INVALID regex]", e.getMessage());
throw e;
}
}
use of io.gravitee.rest.api.service.exceptions.InvalidDataException in project gravitee-management-rest-api by gravitee-io.
the class APIV1toAPIV2Converter method configurePolicies.
/**
* Configure Flow's Steps from Rule.
* @param policies, the list of available policies, containing available scopes
* @param rule, the rule to transform into Step
* @param flow, the current Flow
*/
private void configurePolicies(Set<PolicyEntity> policies, Rule rule, Flow flow) {
policies.stream().filter(policy -> policy.getId().equals(rule.getPolicy().getName())).findFirst().ifPresent(policy -> {
String rulePolicyConfiguration = rule.getPolicy().getConfiguration();
String safeRulePolicyConfiguration = clearNullValues(rulePolicyConfiguration);
if (policy.getDevelopment().getOnRequestMethod() != null && policy.getDevelopment().getOnResponseMethod() != null) {
try {
JsonNode jsonRulePolicyConfiguration = JsonLoader.fromString(safeRulePolicyConfiguration);
JsonNode scope = jsonRulePolicyConfiguration.get("scope");
if (scope != null) {
switch(scope.asText()) {
case "REQUEST":
case "REQUEST_CONTENT":
{
final Step step = createStep(rule, policy, safeRulePolicyConfiguration);
flow.getPre().add(step);
break;
}
case "RESPONSE":
case "RESPONSE_CONTENT":
{
final Step step = createStep(rule, policy, safeRulePolicyConfiguration);
flow.getPost().add(step);
break;
}
}
}
} catch (IOException e) {
throw new InvalidDataException("Unable to validate policy configuration", e);
}
} else if (policy.getDevelopment().getOnRequestMethod() != null) {
final Step step = createStep(rule, policy, safeRulePolicyConfiguration);
flow.getPre().add(step);
} else if (policy.getDevelopment().getOnResponseMethod() != null) {
final Step step = createStep(rule, policy, safeRulePolicyConfiguration);
flow.getPost().add(step);
}
});
}
use of io.gravitee.rest.api.service.exceptions.InvalidDataException in project gravitee-management-rest-api by gravitee-io.
the class PolicyServiceImpl method validatePolicyConfiguration.
private String validatePolicyConfiguration(String policyName, String configuration) {
if (policyName != null && configuration != null) {
String schema = getSchema(policyName);
try {
// At least, validate json.
String safePolicyConfiguration = clearNullValues(configuration);
JsonNode jsonConfiguration = JsonLoader.fromString(safePolicyConfiguration);
if (schema != null && !schema.equals("")) {
// Validate json against schema when defined.
JsonNode jsonSchema = JsonLoader.fromString(schema);
ListProcessingReport report = (ListProcessingReport) jsonSchemaFactory.getValidator().validate(jsonSchema, jsonConfiguration, true);
if (!report.isSuccess()) {
boolean hasDefaultValue = false;
String msg = "";
if (report.iterator().hasNext()) {
msg = " : " + report.iterator().next().getMessage();
Pattern pattern = Pattern.compile("\\(\\[\\\"(.*?)\\\"\\]\\)");
Matcher matcher = pattern.matcher(msg);
if (matcher.find()) {
String field = matcher.group(1);
JsonNode properties = jsonSchema.get("properties");
hasDefaultValue = properties != null && properties.get(field) != null && properties.get(field).get("default") != null;
}
}
if (!hasDefaultValue) {
throw new InvalidDataException("Invalid policy configuration" + msg);
}
}
}
return safePolicyConfiguration;
} catch (IOException | ProcessingException e) {
throw new InvalidDataException("Unable to validate policy configuration", e);
}
}
return configuration;
}
Aggregations