From 263cbf2331685a999819f7215b660fe70eac07d8 Mon Sep 17 00:00:00 2001 From: Carl Harris Date: Thu, 14 Dec 2017 06:14:15 -0500 Subject: [PATCH 1/4] GUACAMOLE-464: configuration properties from OS environment --- .../environment/LocalEnvironment.java | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java b/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java index f45bcaa1a..2d8b0cfd4 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java @@ -304,9 +304,41 @@ public class LocalEnvironment implements Environment { return guacHome; } + /** + * Gets the string value for a property name. + * + * The value may come from either the OS environment or the Properties + * collection that was loaded from guacamole.properties. When checking the + * environment for the named property, the name is first transformed by + * converting all hyphens to underscores and converting the string to + * upper case letter, in accordance with common convention for environment + * strings. + * + * @param name + * The name of the property value to retrieve. + * + * @return + * The corresponding value for the property. If the value is found in + * the OS environment, any corresponding value from the Properties + * collection containing properties from guacamole.properties is + * ignored. + */ + private String getPropertyValue(String name) { + + // transform the name according to common convention + final String envName = name.replace('-', '_').toUpperCase(); + final String envValue = System.getenv(envName); + + if (envValue != null) { + return envValue; + } + + return properties.getProperty(name); + } + @Override public Type getProperty(GuacamoleProperty property) throws GuacamoleException { - return property.parseValue(properties.getProperty(property.getName())); + return property.parseValue(getPropertyValue(property.getName())); } @Override From cc99584802857db17816e15bada54210b90fdefb Mon Sep 17 00:00:00 2001 From: Carl Harris Date: Tue, 19 Dec 2017 05:22:23 -0500 Subject: [PATCH 2/4] GUACAMOLE-464: explicitly enable property overrides from environment --- .../environment/LocalEnvironment.java | 79 +++++++++++++++---- 1 file changed, 64 insertions(+), 15 deletions(-) diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java b/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java index 2d8b0cfd4..61ec6adf3 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java @@ -27,6 +27,8 @@ import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; + +import org.apache.guacamole.properties.BooleanGuacamoleProperty; import org.codehaus.jackson.map.ObjectMapper; import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleServerException; @@ -72,6 +74,18 @@ public class LocalEnvironment implements Environment { */ 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. */ @@ -87,6 +101,11 @@ public class LocalEnvironment implements Environment { */ private final Map availableProtocols; + /** + * Flag indicating whether environment variables can override properties. + */ + private final boolean environmentPropertyOverride; + /** * The Jackson parser for parsing JSON files. */ @@ -141,6 +160,8 @@ public class LocalEnvironment implements Environment { // Read all protocols 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 public File getGuacamoleHome() { return guacHome; @@ -307,30 +352,34 @@ public class LocalEnvironment implements Environment { /** * Gets the string value for a property name. * - * The value may come from either the OS environment or the Properties - * collection that was loaded from guacamole.properties. When checking the - * environment for the named property, the name is first transformed by - * converting all hyphens to underscores and converting the string to - * upper case letter, in accordance with common convention for environment - * strings. + * The value may come from either the OS environment (if property override + * is enabled) or the Properties collection that was loaded from + * guacamole.properties. When checking the environment for the named + * property, the name is first transformed by converting all hyphens to + * underscores and converting the string to upper case letter, in accordance + * with common convention for environment strings. * * @param name * The name of the property value to retrieve. * * @return - * The corresponding value for the property. If the value is found in - * the OS environment, any corresponding value from the Properties - * collection containing properties from guacamole.properties is - * ignored. + * The corresponding value for the property. If property override + * is enabled and the value is found in the OS environment, the value + * from the environment is returned. Otherwise, the value from + * guacamole.properties, if any, is returned. */ private String getPropertyValue(String name) { - // transform the name according to common convention - final String envName = name.replace('-', '_').toUpperCase(); - final String envValue = System.getenv(envName); + // Check for corresponding environment variable if overrides enabled + if (environmentPropertyOverride) { - if (envValue != null) { - return envValue; + // Transform the name according to common convention + final String envName = name.replace('-', '_').toUpperCase(); + final String envValue = System.getenv(envName); + + if (envValue != null) { + return envValue; + } } return properties.getProperty(name); From 718663d2c585630914b41625095819737faa7913 Mon Sep 17 00:00:00 2001 From: Carl Harris Date: Tue, 16 Jan 2018 11:49:15 -0500 Subject: [PATCH 3/4] GUACAMOLE-464: use enable-environment-properties as the property name --- .../environment/LocalEnvironment.java | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java b/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java index 61ec6adf3..8faee7a16 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java @@ -78,11 +78,11 @@ public class LocalEnvironment implements Environment { * A property that determines whether environment variables are evaluated * to override properties specified in guacamole.properties. */ - private static final BooleanGuacamoleProperty ENVIRONMENT_PROPERTY_OVERRIDE = + private static final BooleanGuacamoleProperty ENABLE_ENVIRONMENT_PROPERTIES = new BooleanGuacamoleProperty() { @Override public String getName() { - return "enable-environment-overrides"; + return "enable-environment-properties"; } }; @@ -104,7 +104,7 @@ public class LocalEnvironment implements Environment { /** * Flag indicating whether environment variables can override properties. */ - private final boolean environmentPropertyOverride; + private final boolean environmentPropertiesEnabled; /** * The Jackson parser for parsing JSON files. @@ -161,7 +161,7 @@ public class LocalEnvironment implements Environment { availableProtocols = readProtocols(); // Should environment variables override configuration properties? - environmentPropertyOverride = propertyOverrideEnabled(properties); + environmentPropertiesEnabled = environmentPropertiesEnabled(properties); } /** @@ -321,7 +321,7 @@ public class LocalEnvironment implements Environment { } /** - * Checks for the presence of the {@link #ENVIRONMENT_PROPERTY_OVERRIDE} + * Checks for the presence of the {@link #ENABLE_ENVIRONMENT_PROPERTIES} * property in the given properties collection. * * @param properties @@ -335,13 +335,13 @@ public class LocalEnvironment implements Environment { * cannot be successfully parsed as a Boolean * */ - private static boolean propertyOverrideEnabled(Properties properties) + private static boolean environmentPropertiesEnabled(Properties properties) throws GuacamoleException { - final Boolean enableOverrides = ENVIRONMENT_PROPERTY_OVERRIDE.parseValue( - properties.getProperty(ENVIRONMENT_PROPERTY_OVERRIDE.getName())); + final Boolean enabled = ENABLE_ENVIRONMENT_PROPERTIES.parseValue( + properties.getProperty(ENABLE_ENVIRONMENT_PROPERTIES.getName())); - return enableOverrides != null && enableOverrides; + return enabled != null && enabled; } @Override @@ -371,7 +371,7 @@ public class LocalEnvironment implements Environment { private String getPropertyValue(String name) { // Check for corresponding environment variable if overrides enabled - if (environmentPropertyOverride) { + if (environmentPropertiesEnabled) { // Transform the name according to common convention final String envName = name.replace('-', '_').toUpperCase(); From 9691bc0c86caa33d76eba11e4b570d3f2d6e83ba Mon Sep 17 00:00:00 2001 From: Carl Harris Date: Tue, 16 Jan 2018 12:15:45 -0500 Subject: [PATCH 4/4] GUACAMOLE-464: fix import statement order and spacing --- .../org/apache/guacamole/environment/LocalEnvironment.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java b/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java index 8faee7a16..1b45c9521 100644 --- a/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java +++ b/guacamole-ext/src/main/java/org/apache/guacamole/environment/LocalEnvironment.java @@ -27,12 +27,11 @@ import java.io.InputStream; import java.util.HashMap; import java.util.Map; import java.util.Properties; - -import org.apache.guacamole.properties.BooleanGuacamoleProperty; import org.codehaus.jackson.map.ObjectMapper; import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleServerException; import org.apache.guacamole.net.auth.GuacamoleProxyConfiguration; +import org.apache.guacamole.properties.BooleanGuacamoleProperty; import org.apache.guacamole.properties.GuacamoleProperty; import org.apache.guacamole.protocols.ProtocolInfo; import org.slf4j.Logger;