Skip to content

Commit

Permalink
- Fix for (GEOS-7810) GeoFence Embedded Server should not overwrite …
Browse files Browse the repository at this point in the history
…configuration custom parameters

 - Fix for (GEOS-7810) GeoFence Embedded Server should not overwrite configuration custom parameters
  • Loading branch information
afabiani committed Oct 24, 2016
1 parent cbaef95 commit 5742988
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 31 deletions.
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -77,54 +78,75 @@ public void setCacheConfiguration(CacheConfiguration cacheConfiguration) {
public void storeConfiguration() throws IOException {
Resource configurationFile = configurer.getConfigFile();

Properties oldServerProps = new Properties();
BufferedWriter writer = null;
final String comments =
"### GeoFence Module configuration file\n" +
"### \n" +
"### GeoServer will read this file at boot time.\n" +
"### This file may be automatically regenerated by GeoServer, so any changes beside the property values may be lost.\n\n";

try {
// Store the current property values
if (configurationFile.file().exists() && configurationFile.file().canRead()) {
oldServerProps.load(configurationFile.in());
}

writer = new BufferedWriter(new OutputStreamWriter(configurationFile.out()));

writer.write("### GeoFence Module configuration file\n");
writer.write("### \n");
writer.write("### GeoServer will read this file at boot time.\n");
writer.write("### This file may be automatically regenerated by GeoServer, so any changes beside the property values may be lost.\n\n");
// writer.write(comments);

saveConfiguration(writer, oldServerProps, geofenceConfiguration);
saveConfiguration(writer, oldServerProps, cacheConfiguration);

saveConfiguration(writer, geofenceConfiguration);
saveConfiguration(writer, cacheConfiguration);

oldServerProps.store(writer, comments);
} finally {
IOUtils.closeQuietly(writer);
}
}

/**
* Saves current configuration to disk.
* @param oldServerProps
*/
protected void saveConfiguration(Writer writer, GeoFenceConfiguration configuration)
protected void saveConfiguration(Writer writer, Properties oldServerProps, GeoFenceConfiguration configuration)
throws IOException {

writer.write("### GeoFence main configuration\n\n");

saveConfig(writer, "instanceName", configuration.getInstanceName());
saveConfig(writer, "servicesUrl", configuration.getServicesUrl());
saveConfig(writer, "allowRemoteAndInlineLayers", configuration.isAllowRemoteAndInlineLayers());
saveConfig(writer, "allowDynamicStyles", configuration.isAllowDynamicStyles());
saveConfig(writer, "grantWriteToWorkspacesToAuthenticatedUsers", configuration.isGrantWriteToWorkspacesToAuthenticatedUsers());
saveConfig(writer, "useRolesToFilter", configuration.isUseRolesToFilter());
saveConfig(writer, "acceptedRoles", configuration.getAcceptedRoles());
// writer.write("### GeoFence main configuration\n\n");

oldServerProps.setProperty("instanceName", configuration.getInstanceName());
oldServerProps.setProperty("servicesUrl", configuration.getServicesUrl());
oldServerProps.setProperty("allowRemoteAndInlineLayers", String.valueOf(configuration.isAllowRemoteAndInlineLayers()));
oldServerProps.setProperty("allowDynamicStyles", String.valueOf(configuration.isAllowDynamicStyles()));
oldServerProps.setProperty("grantWriteToWorkspacesToAuthenticatedUsers", String.valueOf(configuration.isGrantWriteToWorkspacesToAuthenticatedUsers()));
oldServerProps.setProperty("useRolesToFilter", String.valueOf(configuration.isUseRolesToFilter()));
oldServerProps.setProperty("acceptedRoles", configuration.getAcceptedRoles());

// saveConfig(writer, "instanceName", configuration.getInstanceName());
// saveConfig(writer, "servicesUrl", configuration.getServicesUrl());
// saveConfig(writer, "allowRemoteAndInlineLayers", configuration.isAllowRemoteAndInlineLayers());
// saveConfig(writer, "allowDynamicStyles", configuration.isAllowDynamicStyles());
// saveConfig(writer, "grantWriteToWorkspacesToAuthenticatedUsers", configuration.isGrantWriteToWorkspacesToAuthenticatedUsers());
// saveConfig(writer, "useRolesToFilter", configuration.isUseRolesToFilter());
// saveConfig(writer, "acceptedRoles", configuration.getAcceptedRoles());
}

protected void saveConfig(Writer writer, String name, Object value) throws IOException {
writer.write(name + "=" + String.valueOf(value) + "\n");
}
public void saveConfiguration(Writer writer, Properties oldServerProps, CacheConfiguration params) throws IOException {

public void saveConfiguration(Writer writer, CacheConfiguration params) throws IOException {
// writer.write("\n\n### Cache configuration\n\n");

writer.write("\n\n### Cache configuration\n\n");

saveConfig(writer, "cacheSize", params.getSize());
saveConfig(writer, "cacheRefresh", params.getRefreshMilliSec());
saveConfig(writer, "cacheExpire", params.getExpireMilliSec());
oldServerProps.setProperty("cacheSize", String.valueOf(params.getSize()));
oldServerProps.setProperty("cacheRefresh", String.valueOf(params.getRefreshMilliSec()));
oldServerProps.setProperty("cacheExpire", String.valueOf(params.getExpireMilliSec()));

// saveConfig(writer, "cacheSize", params.getSize());
// saveConfig(writer, "cacheRefresh", params.getRefreshMilliSec());
// saveConfig(writer, "cacheExpire", params.getExpireMilliSec());
}

protected void saveConfig(Writer writer, String name, Object value) throws IOException {
writer.write(name + "=" + String.valueOf(value) + "\n");
}

/**
* Returns a copy of the configuration.
Expand Down
Expand Up @@ -19,20 +19,25 @@
*/
package org.geoserver.geofence;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

import org.apache.commons.io.IOUtils;
import org.custommonkey.xmlunit.SimpleNamespaceContext;
import org.custommonkey.xmlunit.XMLUnit;
import org.geoserver.geofence.config.GeoFenceConfiguration;
import org.geoserver.geofence.config.GeoFenceConfigurationManager;
import org.geoserver.geofence.config.GeoFencePropertyPlaceholderConfigurer;
import org.geoserver.geofence.utils.GeofenceTestUtils;
import org.geoserver.platform.resource.Resource;
import org.geoserver.test.GeoServerTestSupport;
import org.junit.Test;
import org.springframework.core.io.UrlResource;

public class AccessManagerConfigTest extends GeoServerTestSupport {
Expand Down Expand Up @@ -75,9 +80,10 @@ protected void setUpInternal() throws Exception {
}


@Test
public void testSave() throws IOException, URISyntaxException {
GeofenceTestUtils.emptyFile("test-config.properties");

GeoFenceConfiguration config = new GeoFenceConfiguration();
config.setInstanceName("TEST_INSTANCE");
config.setServicesUrl("http://fakeservice");
Expand All @@ -88,6 +94,18 @@ public void testSave() throws IOException, URISyntaxException {
config.setAcceptedRoles("A,B");

manager.setConfiguration(config);

Resource configurationFile = configurer.getConfigFile();

BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(configurationFile.out()));

writer.write("newUserProperty=custom_property_value\n");
} finally {
IOUtils.closeQuietly(writer);
}

manager.storeConfiguration();

File configFile = configurer.getConfigFile().file();
Expand All @@ -96,9 +114,6 @@ public void testSave() throws IOException, URISyntaxException {
String content = GeofenceTestUtils.readConfig(configFile);
assertTrue(content.contains("fakeservice"));
assertTrue(content.contains("TEST_INSTANCE"));

assertTrue(content.contains("custom_property_value"));
}



}

0 comments on commit 5742988

Please sign in to comment.