Add patch endpoint for global config (#1629)

Co-authored-by: Sebastian Sdorra <sebastian.sdorra@cloudogu.com>
This commit is contained in:
Eduard Heimbuch
2021-04-28 08:47:29 +02:00
committed by GitHub
parent 9782fd2e8f
commit 8f91c217fc
9 changed files with 633 additions and 120 deletions

View File

@@ -24,6 +24,7 @@
package sonia.scm.api.v2.resources;
import com.fasterxml.jackson.databind.JsonNode;
import com.google.common.annotations.VisibleForTesting;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Operation;
@@ -36,6 +37,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import sonia.scm.config.ConfigurationPermissions;
import sonia.scm.config.ScmConfiguration;
import sonia.scm.repository.NamespaceStrategyValidator;
import sonia.scm.util.JsonMerger;
import sonia.scm.util.ScmConfigurationUtil;
import sonia.scm.web.VndMediaType;
@@ -43,6 +45,7 @@ import javax.inject.Inject;
import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PATCH;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@@ -63,17 +66,21 @@ public class ConfigResource {
private final ScmConfigurationToConfigDtoMapper configToDtoMapper;
private final ScmConfiguration configuration;
private final NamespaceStrategyValidator namespaceStrategyValidator;
private final JsonMerger jsonMerger;
private Consumer<ScmConfiguration> store = (config) -> ScmConfigurationUtil.getInstance().store(config);
private Consumer<ScmConfiguration> store = config -> ScmConfigurationUtil.getInstance().store(config);
@Inject
public ConfigResource(ConfigDtoToScmConfigurationMapper dtoToConfigMapper,
ScmConfigurationToConfigDtoMapper configToDtoMapper,
ScmConfiguration configuration, NamespaceStrategyValidator namespaceStrategyValidator) {
ScmConfiguration configuration,
NamespaceStrategyValidator namespaceStrategyValidator,
JsonMerger jsonMerger) {
this.dtoToConfigMapper = dtoToConfigMapper;
this.configToDtoMapper = configToDtoMapper;
this.configuration = configuration;
this.namespaceStrategyValidator = namespaceStrategyValidator;
this.jsonMerger = jsonMerger;
}
@VisibleForTesting
@@ -107,7 +114,6 @@ public class ConfigResource {
)
)
public Response get() {
// We do this permission check in Resource and not in ScmConfiguration, because it must be available for reading
// from within the code (plugins, etc.), but not for the whole anonymous world outside.
ConfigurationPermissions.read(configuration).check();
@@ -151,21 +157,73 @@ public class ConfigResource {
)
)
public Response update(@Valid ConfigDto configDto) {
// This *could* be moved to ScmConfiguration or ScmConfigurationUtil classes.
// But to where to check? load() or store()? Leave it for now, SCMv1 legacy that can be cleaned up later.
ConfigurationPermissions.write(configuration).check();
// ensure the namespace strategy is valid
namespaceStrategyValidator.check(configDto.getNamespaceStrategy());
ScmConfiguration config = dtoToConfigMapper.map(configDto);
synchronized (ScmConfiguration.class) {
configuration.load(config);
store.accept(configuration);
}
updateConfig(configDto);
return Response.noContent().build();
}
/**
* Modifies the global scm config partially.
*
* @param updateNode json object which contains changed fields
*/
@PATCH
@Path("")
@Consumes(VndMediaType.CONFIG)
@Operation(
summary = "Update instance configuration partially",
description = "Modifies the instance configuration partially.",
tags = "Instance configuration",
requestBody = @RequestBody(
content = @Content(
mediaType = VndMediaType.CONFIG,
schema = @Schema(implementation = UpdateConfigDto.class),
examples = @ExampleObject(
name = "Overwrites the provided fields of the current configuration.",
value = "{\n \"realmDescription\":\"SCM-Manager Realm\" \n}",
summary = "Update configuration partially"
)
)
)
)
@ApiResponse(responseCode = "204", description = "update success")
@ApiResponse(responseCode = "401", description = "not authenticated / invalid credentials")
@ApiResponse(responseCode = "403", description = "not authorized, the current user does not have the \"configuration:write\" privilege")
@ApiResponse(
responseCode = "500",
description = "internal server error",
content = @Content(
mediaType = VndMediaType.ERROR_TYPE,
schema = @Schema(implementation = ErrorDto.class)
)
)
public Response updatePartially(JsonNode updateNode) {
ConfigurationPermissions.write(configuration).check();
ConfigDto updatedConfigDto = jsonMerger
.fromObject(configToDtoMapper.map(configuration))
.mergeWithJson(updateNode)
.toObject(ConfigDto.class)
.withValidation()
.build();
updateConfig(updatedConfigDto);
return Response.noContent().build();
}
private void updateConfig(ConfigDto updatedConfigDto) {
// This *could* be moved to ScmConfiguration or ScmConfigurationUtil classes.
// But to where to check? load() or store()? Leave it for now, SCMv1 legacy that can be cleaned up later.
// ensure the namespace strategy is valid
namespaceStrategyValidator.check(updatedConfigDto.getNamespaceStrategy());
ScmConfiguration config = dtoToConfigMapper.map(updatedConfigDto);
synchronized (ScmConfiguration.class) {
configuration.load(config);
store.accept(configuration);
}
}
}

View File

@@ -27,7 +27,6 @@ package sonia.scm.web.i18n;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.legman.Subscribe;
import com.google.common.annotations.VisibleForTesting;
import com.google.inject.Singleton;
@@ -40,6 +39,7 @@ import sonia.scm.cache.CacheManager;
import sonia.scm.filter.WebElement;
import sonia.scm.lifecycle.RestartEvent;
import sonia.scm.plugin.PluginLoader;
import sonia.scm.util.JsonMerger;
import javax.inject.Inject;
import javax.servlet.http.HttpServlet;
@@ -49,7 +49,6 @@ import java.io.IOException;
import java.io.PrintWriter;
import java.net.URL;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Optional;
@@ -70,13 +69,15 @@ public class I18nServlet extends HttpServlet {
private final ClassLoader classLoader;
private final Cache<String, JsonNode> cache;
private final ObjectMapper objectMapper = new ObjectMapper();
private final JsonMerger jsonMerger;
@Inject
public I18nServlet(SCMContextProvider context, PluginLoader pluginLoader, CacheManager cacheManager) {
public I18nServlet(SCMContextProvider context, PluginLoader pluginLoader, CacheManager cacheManager, JsonMerger jsonMerger) {
this.context = context;
this.classLoader = pluginLoader.getUberClassLoader();
this.cache = cacheManager.getCache(CACHE_NAME);
this.jsonMerger = jsonMerger;
}
@Subscribe(async = false)
@@ -146,7 +147,7 @@ public class I18nServlet extends HttpServlet {
URL url = resources.nextElement();
JsonNode jsonNode = objectMapper.readTree(url);
if (mergedJsonNode != null) {
merge(mergedJsonNode, jsonNode);
mergedJsonNode = jsonMerger.fromJson(mergedJsonNode).mergeWithJson(jsonNode).toJsonNode();
} else {
mergedJsonNode = jsonNode;
}
@@ -155,44 +156,4 @@ public class I18nServlet extends HttpServlet {
return Optional.ofNullable(mergedJsonNode);
}
private JsonNode merge(JsonNode mainNode, JsonNode updateNode) {
Iterator<String> fieldNames = updateNode.fieldNames();
while (fieldNames.hasNext()) {
String fieldName = fieldNames.next();
JsonNode jsonNode = mainNode.get(fieldName);
if (jsonNode != null) {
mergeNode(updateNode, fieldName, jsonNode);
} else {
mergeField(mainNode, updateNode, fieldName);
}
}
return mainNode;
}
private void mergeField(JsonNode mainNode, JsonNode updateNode, String fieldName) {
if (mainNode instanceof ObjectNode) {
JsonNode value = updateNode.get(fieldName);
if (value.isNull()) {
return;
}
if (value.isIntegralNumber() && value.toString().equals("0")) {
return;
}
if (value.isFloatingPointNumber() && value.toString().equals("0.0")) {
return;
}
((ObjectNode) mainNode).set(fieldName, value);
}
}
private void mergeNode(JsonNode updateNode, String fieldName, JsonNode jsonNode) {
if (jsonNode.isObject()) {
merge(jsonNode, updateNode.get(fieldName));
} else if (jsonNode.isArray()) {
for (int i = 0; i < jsonNode.size(); i++) {
merge(jsonNode.get(i), updateNode.get(fieldName).get(i));
}
}
}
}