Skip to content
This repository has been archived by the owner on Aug 20, 2021. It is now read-only.

Commit

Permalink
issue(management): #4087 - remove null field of PolicyConfig before v…
Browse files Browse the repository at this point in the history
…alidation.

  Remove null field on import api and after policy generation on OAI/Swagger specifications.
  This "cleaning" step avoid modifying all policies module and allow to clean policies
  and avoid unmodifiable policy screens without changing all schema-form.json

fix gravitee-io/issues#4087
  • Loading branch information
leleueri committed Jul 24, 2020
1 parent 8109996 commit 3443dd4
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 6 deletions.
Expand Up @@ -353,6 +353,9 @@ private ApiEntity create0(UpdateApiEntity api, String userId, boolean createSyst
// check if there is regex errors in plaintext fields
validateRegexfields(api);

// check policy configurations.
checkPolicyConfigurations(api);

Api repoApi = convert(id, api);

if (repoApi != null) {
Expand Down
Expand Up @@ -41,6 +41,8 @@
import java.util.Set;
import java.util.stream.Collectors;

import static io.gravitee.rest.api.service.validator.PolicyCleaner.clearNullValues;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
Expand Down Expand Up @@ -105,7 +107,8 @@ public void validatePolicyConfiguration(Policy policy) {

try {
// At least, validate json.
JsonNode jsonConfiguration = JsonLoader.fromString(policy.getConfiguration());
String safePolicyConfiguration = clearNullValues(policy.getConfiguration());
JsonNode jsonConfiguration = JsonLoader.fromString(safePolicyConfiguration);

if (schema != null && !schema.equals("")) {
// Validate json against schema when defined.
Expand All @@ -119,6 +122,9 @@ public void validatePolicyConfiguration(Policy policy) {
throw new InvalidDataException("Invalid policy configuration" + msg);
}
}

policy.setConfiguration(safePolicyConfiguration);

} catch (IOException | ProcessingException e) {
throw new InvalidDataException("Unable to validate policy configuration", e);
}
Expand Down
Expand Up @@ -18,12 +18,11 @@
import io.gravitee.common.http.HttpMethod;
import io.gravitee.definition.model.Path;
import io.gravitee.definition.model.Rule;
import io.gravitee.policy.api.swagger.Policy;
import io.gravitee.rest.api.model.api.SwaggerApiEntity;
import io.gravitee.rest.api.model.api.SwaggerPath;
import io.gravitee.rest.api.service.impl.swagger.visitor.v3.OAIDescriptorVisitor;
import io.gravitee.rest.api.service.impl.swagger.visitor.v3.OAIOperationVisitor;
import io.gravitee.rest.api.service.swagger.OAIDescriptor;
import io.gravitee.policy.api.swagger.Policy;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.PathItem;
Expand All @@ -41,7 +40,8 @@

import static java.util.Collections.singleton;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;

import static io.gravitee.rest.api.service.validator.PolicyCleaner.clearNullValues;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
Expand Down Expand Up @@ -114,7 +114,7 @@ public void accept(OAIOperationVisitor oaiOperationVisitor) {

io.gravitee.definition.model.Policy defPolicy = new io.gravitee.definition.model.Policy();
defPolicy.setName(policy.get().getName());
defPolicy.setConfiguration(policy.get().getConfiguration());
defPolicy.setConfiguration(clearNullValues(policy.get().getConfiguration()));
rule.setPolicy(defPolicy);
rules.add(rule);
}
Expand Down
Expand Up @@ -32,6 +32,8 @@
import static java.util.Collections.singleton;
import static java.util.stream.Collectors.toMap;

import static io.gravitee.rest.api.service.validator.PolicyCleaner.clearNullValues;

/**
* @author David BRASSELY (david.brassely at graviteesource.com)
* @author GraviteeSource Team
Expand Down Expand Up @@ -98,7 +100,7 @@ public void accept(SwaggerOperationVisitor operationVisitor) {

io.gravitee.definition.model.Policy defPolicy = new io.gravitee.definition.model.Policy();
defPolicy.setName(policy.get().getName());
defPolicy.setConfiguration(policy.get().getConfiguration());
defPolicy.setConfiguration(clearNullValues(policy.get().getConfiguration()));
rule.setPolicy(defPolicy);
rules.add(rule);
}
Expand Down
@@ -0,0 +1,49 @@
/**
* Copyright (C) 2015 The Gravitee team (http://gravitee.io)
*
* 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.
*/
package io.gravitee.rest.api.service.validator;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
* @author Eric LELEU (eric.leleu at graviteesource.com)
* @author GraviteeSource Team
*/
public class PolicyCleaner {
private static Logger LOGGER = LoggerFactory.getLogger(PolicyCleaner.class);

private static ObjectMapper objectMapper = new ObjectMapper();

static {
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
}

public static String clearNullValues(String jsonPayload){
try {
// #4087 - ugly fix to remove null entries in the PolicyConfiguration
// otherwise updating the API is impossible.
Object staleObject = objectMapper.readValue(jsonPayload, Object.class);
return objectMapper.writeValueAsString(staleObject);
} catch (IOException e) {
LOGGER.debug("Unable to remove 'null' values from policy configuration, return the original value", e);
return jsonPayload;
}
}
}
Expand Up @@ -100,6 +100,8 @@ public class ApiService_CreateWithDefinitionTest {
private ApiMetadataService apiMetadataService;
@Mock
private AlertService alertService;
@Spy
private PolicyService policyService;

@Before
public void init() {
Expand Down

0 comments on commit 3443dd4

Please sign in to comment.