GUACAMOLE-464: explicitly enable property overrides from environment

This commit is contained in:
Carl Harris
2017-12-19 05:22:23 -05:00
parent 263cbf2331
commit cc99584802

View File

@@ -27,6 +27,8 @@ import java.io.InputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import org.apache.guacamole.properties.BooleanGuacamoleProperty;
import org.codehaus.jackson.map.ObjectMapper; import org.codehaus.jackson.map.ObjectMapper;
import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleServerException; import org.apache.guacamole.GuacamoleServerException;
@@ -72,6 +74,18 @@ public class LocalEnvironment implements Environment {
*/ */
private static final boolean DEFAULT_GUACD_SSL = false; private static final boolean DEFAULT_GUACD_SSL = false;
/**
* A property that determines whether environment variables are evaluated
* to override properties specified in guacamole.properties.
*/
private static final BooleanGuacamoleProperty ENVIRONMENT_PROPERTY_OVERRIDE =
new BooleanGuacamoleProperty() {
@Override
public String getName() {
return "enable-environment-overrides";
}
};
/** /**
* All properties read from guacamole.properties. * All properties read from guacamole.properties.
*/ */
@@ -87,6 +101,11 @@ public class LocalEnvironment implements Environment {
*/ */
private final Map<String, ProtocolInfo> availableProtocols; private final Map<String, ProtocolInfo> availableProtocols;
/**
* Flag indicating whether environment variables can override properties.
*/
private final boolean environmentPropertyOverride;
/** /**
* The Jackson parser for parsing JSON files. * The Jackson parser for parsing JSON files.
*/ */
@@ -141,6 +160,8 @@ public class LocalEnvironment implements Environment {
// Read all protocols // Read all protocols
availableProtocols = readProtocols(); availableProtocols = readProtocols();
// Should environment variables override configuration properties?
environmentPropertyOverride = propertyOverrideEnabled(properties);
} }
/** /**
@@ -299,6 +320,30 @@ public class LocalEnvironment implements Environment {
} }
/**
* Checks for the presence of the {@link #ENVIRONMENT_PROPERTY_OVERRIDE}
* property in the given properties collection.
*
* @param properties
* The properties collection to check.
*
* @return
* true if the property is present in the given properties collection
* and its parsed value is true
*
* @throws GuacamoleException If the value specified for the property
* cannot be successfully parsed as a Boolean
*
*/
private static boolean propertyOverrideEnabled(Properties properties)
throws GuacamoleException {
final Boolean enableOverrides = ENVIRONMENT_PROPERTY_OVERRIDE.parseValue(
properties.getProperty(ENVIRONMENT_PROPERTY_OVERRIDE.getName()));
return enableOverrides != null && enableOverrides;
}
@Override @Override
public File getGuacamoleHome() { public File getGuacamoleHome() {
return guacHome; return guacHome;
@@ -307,31 +352,35 @@ public class LocalEnvironment implements Environment {
/** /**
* Gets the string value for a property name. * Gets the string value for a property name.
* *
* The value may come from either the OS environment or the Properties * The value may come from either the OS environment (if property override
* collection that was loaded from guacamole.properties. When checking the * is enabled) or the Properties collection that was loaded from
* environment for the named property, the name is first transformed by * guacamole.properties. When checking the environment for the named
* converting all hyphens to underscores and converting the string to * property, the name is first transformed by converting all hyphens to
* upper case letter, in accordance with common convention for environment * underscores and converting the string to upper case letter, in accordance
* strings. * with common convention for environment strings.
* *
* @param name * @param name
* The name of the property value to retrieve. * The name of the property value to retrieve.
* *
* @return * @return
* The corresponding value for the property. If the value is found in * The corresponding value for the property. If property override
* the OS environment, any corresponding value from the Properties * is enabled and the value is found in the OS environment, the value
* collection containing properties from guacamole.properties is * from the environment is returned. Otherwise, the value from
* ignored. * guacamole.properties, if any, is returned.
*/ */
private String getPropertyValue(String name) { private String getPropertyValue(String name) {
// transform the name according to common convention // Check for corresponding environment variable if overrides enabled
if (environmentPropertyOverride) {
// Transform the name according to common convention
final String envName = name.replace('-', '_').toUpperCase(); final String envName = name.replace('-', '_').toUpperCase();
final String envValue = System.getenv(envName); final String envValue = System.getenv(envName);
if (envValue != null) { if (envValue != null) {
return envValue; return envValue;
} }
}
return properties.getProperty(name); return properties.getProperty(name);
} }