GUACAMOLE-374: Automatically read properties from files pointed to by "*_FILE" environment variables.

This commit is contained in:
Michael Jumper
2024-02-18 23:18:43 -08:00
parent 9580dd4f82
commit d6a491f933
3 changed files with 97 additions and 2 deletions

View File

@@ -97,7 +97,8 @@ RUN useradd --system --create-home --shell /usr/sbin/nologin --uid $UID --gid $G
USER guacamole USER guacamole
# Environment variable defaults # Environment variable defaults
ENV GUACAMOLE_HOME=/etc/guacamole ENV GUACAMOLE_HOME=/etc/guacamole \
ENABLE_FILE_ENVIRONMENT_PROPERTIES=true
# Start Guacamole under Tomcat, listening on 0.0.0.0:8080 # Start Guacamole under Tomcat, listening on 0.0.0.0:8080
EXPOSE 8080 EXPOSE 8080

View File

@@ -95,7 +95,7 @@ public class GuacamoleServletContextListener extends GuiceServletContextListener
/** /**
* A property that determines whether environment variables are evaluated * A property that determines whether environment variables are evaluated
* to override properties specified in guacamole.properties. * to supply properties not specified in guacamole.properties.
*/ */
private static final BooleanGuacamoleProperty ENABLE_ENVIRONMENT_PROPERTIES = private static final BooleanGuacamoleProperty ENABLE_ENVIRONMENT_PROPERTIES =
new BooleanGuacamoleProperty() { new BooleanGuacamoleProperty() {
@@ -105,6 +105,19 @@ public class GuacamoleServletContextListener extends GuiceServletContextListener
} }
}; };
/**
* A property that determines whether environment variables of the form
* "*_FILE" are evaluated to supply properties not specified in
* guacamole.properties nor in environment variables.
*/
private static final BooleanGuacamoleProperty ENABLE_FILE_ENVIRONMENT_PROPERTIES =
new BooleanGuacamoleProperty() {
@Override
public String getName() {
return "enable-file-environment-properties";
}
};
/** /**
* The Guacamole server environment. * The Guacamole server environment.
*/ */
@@ -172,6 +185,23 @@ public class GuacamoleServletContextListener extends GuiceServletContextListener
logger.debug("Error reading \"{}\" property from guacamole.properties.", ENABLE_ENVIRONMENT_PROPERTIES.getName(), e); logger.debug("Error reading \"{}\" property from guacamole.properties.", ENABLE_ENVIRONMENT_PROPERTIES.getName(), e);
} }
// For any values not defined in GUACAMOLE_HOME/guacamole.properties
// nor in the system environment, read from files pointed to by
// corresponding "*_FILE" variables in the system environment if
// "enable-file-environment-properties" is set to "true"
try {
if (environment.getProperty(ENABLE_FILE_ENVIRONMENT_PROPERTIES, false)) {
environment.addGuacamoleProperties(new SystemFileEnvironmentGuacamoleProperties());
logger.info("Additional configuration parameters may be read "
+ "from files pointed to by \"*_FILE\" environment "
+ "variables.");
}
}
catch (GuacamoleException e) {
logger.error("Unable to configure support for file environment properties: {}", e.getMessage());
logger.debug("Error reading \"{}\" property from guacamole.properties.", ENABLE_FILE_ENVIRONMENT_PROPERTIES.getName(), e);
}
// Now that at least the main guacamole.properties source of // Now that at least the main guacamole.properties source of
// configuration information is available, initialize the session map // configuration information is available, initialize the session map
sessionMap = new HashTokenSessionMap(environment); sessionMap = new HashTokenSessionMap(environment);

View File

@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 org.apache.guacamole;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.guacamole.properties.GuacamoleProperties;
import org.apache.guacamole.token.TokenName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* GuacamoleProperties implementation which reads all properties from files
* whose filenames are stored in environment variables. The name of the
* environment variable corresponding to the filename is determined from the
* original property using {@link TokenName#canonicalize(java.lang.String)}
* with an additional "_FILE" suffix.
*/
public class SystemFileEnvironmentGuacamoleProperties implements GuacamoleProperties {
/**
* Logger for this class.
*/
private static final Logger logger = LoggerFactory.getLogger(SystemFileEnvironmentGuacamoleProperties.class);
@Override
public String getProperty(String name) {
String filename = System.getenv(TokenName.canonicalize(name) + "_FILE");
if (filename != null) {
try {
return Files.asCharSource(new File(filename), StandardCharsets.UTF_8).read();
}
catch (IOException e) {
logger.error("Property \"{}\" could not be read from file \"{}\": {}", name, filename, e.getMessage());
logger.debug("Error reading property value from file.", e);
}
}
return null;
}
}