mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-17 18:35:25 +00:00
Logging: restrict files loaded as logging configuration based on their suffix
Make sure that files such as logging.yml.rpmnew or logging.yml.bak are not loaded as logging configuration. Only files that start with the "logging." prefix and end with ".yaml", ".yml", ".json" and ".properties" suffix get loaded. Closes #7457
This commit is contained in:
parent
314da4ec9e
commit
2dcb1f503d
@ -19,6 +19,7 @@
|
||||
|
||||
package org.elasticsearch.common.logging.log4j;
|
||||
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.apache.log4j.PropertyConfigurator;
|
||||
import org.elasticsearch.ElasticsearchException;
|
||||
@ -33,6 +34,7 @@ import java.net.MalformedURLException;
|
||||
import java.nio.file.*;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.util.EnumSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Properties;
|
||||
|
||||
@ -43,6 +45,8 @@ import static org.elasticsearch.common.settings.ImmutableSettings.settingsBuilde
|
||||
*/
|
||||
public class LogConfigurator {
|
||||
|
||||
private static final List<String> ALLOWED_SUFFIXES = ImmutableList.of(".yml", ".yaml", ".json", ".properties");
|
||||
|
||||
private static boolean loaded;
|
||||
|
||||
private static ImmutableMap<String, String> replacements = new MapBuilder<String, String>()
|
||||
@ -118,8 +122,14 @@ public class LogConfigurator {
|
||||
Files.walkFileTree(env.configFile().toPath(), EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() {
|
||||
@Override
|
||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
||||
if (file.getFileName().toString().startsWith("logging.")) {
|
||||
loadConfig(file, settingsBuilder);
|
||||
String fileName = file.getFileName().toString();
|
||||
if (fileName.startsWith("logging.")) {
|
||||
for (String allowedSuffix : ALLOWED_SUFFIXES) {
|
||||
if (fileName.endsWith(allowedSuffix)) {
|
||||
loadConfig(file, settingsBuilder);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return FileVisitResult.CONTINUE;
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
package org.elasticsearch.common.logging;
|
||||
|
||||
import com.google.common.io.Files;
|
||||
import org.apache.log4j.Appender;
|
||||
import org.apache.log4j.Logger;
|
||||
import org.elasticsearch.common.logging.log4j.Log4jESLogger;
|
||||
@ -26,12 +27,17 @@ import org.elasticsearch.common.logging.log4j.Log4jESLoggerFactory;
|
||||
import org.elasticsearch.common.logging.log4j.LogConfigurator;
|
||||
import org.elasticsearch.common.settings.ImmutableSettings;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.env.Environment;
|
||||
import org.elasticsearch.test.ElasticsearchTestCase;
|
||||
import org.hamcrest.Matchers;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
/**
|
||||
@ -39,9 +45,13 @@ import static org.hamcrest.Matchers.notNullValue;
|
||||
*/
|
||||
public class LoggingConfigurationTests extends ElasticsearchTestCase {
|
||||
|
||||
@Before
|
||||
public void before() throws Exception {
|
||||
LogConfigurator.reset();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleConfigs() throws Exception {
|
||||
LogConfigurator.reset();
|
||||
File configDir = resolveConfigDir();
|
||||
Settings settings = ImmutableSettings.builder()
|
||||
.put("path.conf", configDir.getAbsolutePath())
|
||||
@ -64,6 +74,71 @@ public class LoggingConfigurationTests extends ElasticsearchTestCase {
|
||||
assertThat(appender, notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveJsonLoggingConfig() throws Exception {
|
||||
File tmpDir = newTempDir();
|
||||
File tmpFile = File.createTempFile("logging.", ".json", tmpDir);
|
||||
Files.write("{\"json\": \"foo\"}", tmpFile, StandardCharsets.UTF_8);
|
||||
Environment environment = new Environment(
|
||||
ImmutableSettings.builder().put("path.conf", tmpDir.getAbsolutePath()).build());
|
||||
|
||||
ImmutableSettings.Builder builder = ImmutableSettings.builder();
|
||||
LogConfigurator.resolveConfig(environment, builder);
|
||||
|
||||
Settings logSettings = builder.build();
|
||||
assertThat(logSettings.get("json"), is("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolvePropertiesLoggingConfig() throws Exception {
|
||||
File tmpDir = newTempDir();
|
||||
File tmpFile = File.createTempFile("logging.", ".properties", tmpDir);
|
||||
Files.write("key: value", tmpFile, StandardCharsets.UTF_8);
|
||||
Environment environment = new Environment(
|
||||
ImmutableSettings.builder().put("path.conf", tmpDir.getAbsolutePath()).build());
|
||||
|
||||
ImmutableSettings.Builder builder = ImmutableSettings.builder();
|
||||
LogConfigurator.resolveConfig(environment, builder);
|
||||
|
||||
Settings logSettings = builder.build();
|
||||
assertThat(logSettings.get("key"), is("value"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveConfigValidFilename() throws Exception {
|
||||
File tmpDir = newTempDir();
|
||||
File tempFileYml = File.createTempFile("logging.", ".yml", tmpDir);
|
||||
File tempFileYaml = File.createTempFile("logging.", ".yaml", tmpDir);
|
||||
|
||||
Files.write("yml: bar", tempFileYml, StandardCharsets.UTF_8);
|
||||
Files.write("yaml: bar", tempFileYaml, StandardCharsets.UTF_8);
|
||||
Environment environment = new Environment(
|
||||
ImmutableSettings.builder().put("path.conf", tmpDir.getAbsolutePath()).build());
|
||||
|
||||
ImmutableSettings.Builder builder = ImmutableSettings.builder();
|
||||
LogConfigurator.resolveConfig(environment, builder);
|
||||
|
||||
Settings logSettings = builder.build();
|
||||
assertThat(logSettings.get("yml"), is("bar"));
|
||||
assertThat(logSettings.get("yaml"), is("bar"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveConfigInvalidFilename() throws Exception {
|
||||
File tmpDir = newTempDir();
|
||||
File tempFile = File.createTempFile("logging.yml.", ".bak", tmpDir);
|
||||
|
||||
Files.write("yml: bar", tempFile, StandardCharsets.UTF_8);
|
||||
Environment environment = new Environment(
|
||||
ImmutableSettings.builder().put("path.conf", tempFile.getAbsolutePath()).build());
|
||||
|
||||
ImmutableSettings.Builder builder = ImmutableSettings.builder();
|
||||
LogConfigurator.resolveConfig(environment, builder);
|
||||
|
||||
Settings logSettings = builder.build();
|
||||
assertThat(logSettings.get("yml"), Matchers.nullValue());
|
||||
}
|
||||
|
||||
private static File resolveConfigDir() throws Exception {
|
||||
URL url = LoggingConfigurationTests.class.getResource("config");
|
||||
return new File(url.toURI());
|
||||
|
Loading…
x
Reference in New Issue
Block a user