Skip to content

Commit

Permalink
Logging: restrict files loaded as logging configuration based on thei…
Browse files Browse the repository at this point in the history
…r 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 elastic#7457
  • Loading branch information
mfussenegger authored and javanna committed Nov 19, 2014
1 parent 1ef1f01 commit 61fc401
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
Expand Up @@ -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;
Expand All @@ -33,6 +34,7 @@
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;

Expand All @@ -43,6 +45,8 @@
*/
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>()
Expand Down Expand Up @@ -115,8 +119,14 @@ public static void resolveConfig(Environment env, final ImmutableSettings.Builde
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;
}
Expand Down
Expand Up @@ -19,29 +19,39 @@

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;
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;

/**
*
*/
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())
Expand All @@ -64,6 +74,71 @@ public void testMultipleConfigs() throws Exception {
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());
Expand Down

0 comments on commit 61fc401

Please sign in to comment.