Search in sources :

Example 1 with InvalidDataException

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;
    }
}
Also used : Policy(io.gravitee.definition.model.Policy) InvalidDataException(io.gravitee.rest.api.service.exceptions.InvalidDataException) Test(org.junit.Test)

Example 2 with InvalidDataException

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);
        }
    });
}
Also used : InvalidDataException(io.gravitee.rest.api.service.exceptions.InvalidDataException) JsonNode(com.fasterxml.jackson.databind.JsonNode) Step(io.gravitee.definition.model.flow.Step) IOException(java.io.IOException)

Example 3 with InvalidDataException

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;
}
Also used : ListProcessingReport(com.github.fge.jsonschema.core.report.ListProcessingReport) Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) InvalidDataException(io.gravitee.rest.api.service.exceptions.InvalidDataException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ProcessingException(com.github.fge.jsonschema.core.exceptions.ProcessingException)

Aggregations

InvalidDataException (io.gravitee.rest.api.service.exceptions.InvalidDataException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 IOException (java.io.IOException)2 ProcessingException (com.github.fge.jsonschema.core.exceptions.ProcessingException)1 ListProcessingReport (com.github.fge.jsonschema.core.report.ListProcessingReport)1 Policy (io.gravitee.definition.model.Policy)1 Step (io.gravitee.definition.model.flow.Step)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Test (org.junit.Test)1