Skip to content

Commit

Permalink
Use stricter pattern to check if an index is managed by Graylog
Browse files Browse the repository at this point in the history
Fixes #1882
  • Loading branch information
Jochen Schalanda committed Mar 3, 2016
1 parent 1846952 commit 1728772
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
Expand Up @@ -34,10 +34,12 @@


import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Named;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;


import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.common.base.Strings.isNullOrEmpty;


Expand All @@ -61,23 +63,25 @@ public class Deflector { // extends Ablenkblech
private final String deflectorName; private final String deflectorName;
private final Indices indices; private final Indices indices;
private final SetIndexReadOnlyJob.Factory indexReadOnlyJobFactory; private final SetIndexReadOnlyJob.Factory indexReadOnlyJobFactory;
private final Pattern indexPattern;


@Inject @Inject
public Deflector(final SystemJobManager systemJobManager, public Deflector(final SystemJobManager systemJobManager,
final ElasticsearchConfiguration configuration, @Named("elasticsearch_index_prefix") final String indexPrefix,
final ActivityWriter activityWriter, final ActivityWriter activityWriter,
final SetIndexReadOnlyJob.Factory indexReadOnlyJobFactory, final SetIndexReadOnlyJob.Factory indexReadOnlyJobFactory,
final CreateNewSingleIndexRangeJob.Factory createNewSingleIndexRangeJobFactory, final CreateNewSingleIndexRangeJob.Factory createNewSingleIndexRangeJobFactory,
final Indices indices) { final Indices indices) {
this.indexPrefix = configuration.getIndexPrefix(); this.indexPrefix = indexPrefix;


this.systemJobManager = systemJobManager; this.systemJobManager = systemJobManager;
this.activityWriter = activityWriter; this.activityWriter = activityWriter;
this.indexReadOnlyJobFactory = indexReadOnlyJobFactory; this.indexReadOnlyJobFactory = indexReadOnlyJobFactory;
this.createNewSingleIndexRangeJobFactory = createNewSingleIndexRangeJobFactory; this.createNewSingleIndexRangeJobFactory = createNewSingleIndexRangeJobFactory;


this.deflectorName = buildName(configuration.getIndexPrefix()); this.deflectorName = buildName(indexPrefix);
this.indices = indices; this.indices = indices;
this.indexPattern = Pattern.compile("^" + indexPrefix + SEPARATOR + "\\d+");
} }


public boolean isUp() { public boolean isUp() {
Expand Down Expand Up @@ -283,6 +287,6 @@ public boolean isDeflectorAlias(final String indexName) {
} }


public boolean isGraylogIndex(final String indexName) { public boolean isGraylogIndex(final String indexName) {
return !isNullOrEmpty(indexName) && !isDeflectorAlias(indexName) && indexName.startsWith(indexPrefix + SEPARATOR); return !isNullOrEmpty(indexName) && !isDeflectorAlias(indexName) && indexPattern.matcher(indexName).matches();
} }
} }
Expand Up @@ -21,14 +21,14 @@
package org.graylog2.indexer; package org.graylog2.indexer;


import org.elasticsearch.action.admin.indices.stats.IndexStats; import org.elasticsearch.action.admin.indices.stats.IndexStats;
import org.graylog2.configuration.ElasticsearchConfiguration;
import org.graylog2.indexer.indices.Indices; import org.graylog2.indexer.indices.Indices;
import org.graylog2.indexer.ranges.CreateNewSingleIndexRangeJob; import org.graylog2.indexer.ranges.CreateNewSingleIndexRangeJob;
import org.graylog2.system.activities.SystemMessageActivityWriter; import org.graylog2.system.activities.SystemMessageActivityWriter;
import org.graylog2.system.jobs.SystemJobManager; import org.graylog2.system.jobs.SystemJobManager;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner; import org.mockito.runners.MockitoJUnitRunner;


import java.util.Map; import java.util.Map;
Expand All @@ -41,17 +41,21 @@


@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class DeflectorTest { public class DeflectorTest {
@Mock
private SystemJobManager systemJobManager;
@Mock
private SystemMessageActivityWriter activityWriter;
@Mock
private SetIndexReadOnlyJob.Factory indexReadOnlyJobFactory;
@Mock
private CreateNewSingleIndexRangeJob.Factory singleIndexRangeJobFactory;
@Mock
private Indices indices;
private Deflector deflector; private Deflector deflector;


@Before @Before
public void setUp() { public void setUp() {
deflector = new Deflector( deflector = new Deflector(systemJobManager, "graylog", activityWriter, indexReadOnlyJobFactory, singleIndexRangeJobFactory, indices);
mock(SystemJobManager.class),
new ElasticsearchConfiguration(),
mock(SystemMessageActivityWriter.class),
mock(SetIndexReadOnlyJob.Factory.class),
mock(CreateNewSingleIndexRangeJob.Factory.class),
mock(Indices.class));
} }


@Test @Test
Expand All @@ -75,14 +79,6 @@ public void testExtractIndexNumberWithMalformedFormatThrowsException() {


@Test @Test
public void testBuildIndexName() { public void testBuildIndexName() {

Deflector d = new Deflector(mock(SystemJobManager.class),
mock(ElasticsearchConfiguration.class),
mock(SystemMessageActivityWriter.class),
mock(SetIndexReadOnlyJob.Factory.class),
mock(CreateNewSingleIndexRangeJob.Factory.class),
mock(Indices.class));

assertEquals("graylog2_0", Deflector.buildIndexName("graylog2", 0)); assertEquals("graylog2_0", Deflector.buildIndexName("graylog2", 0));
assertEquals("graylog2_1", Deflector.buildIndexName("graylog2", 1)); assertEquals("graylog2_1", Deflector.buildIndexName("graylog2", 1));
assertEquals("graylog2_9001", Deflector.buildIndexName("graylog2", 9001)); assertEquals("graylog2_9001", Deflector.buildIndexName("graylog2", 9001));
Expand All @@ -95,27 +91,14 @@ public void testBuildDeflectorNameWithCustomIndexPrefix() {


@Test @Test
public void nullIndexerDoesNotThrow() { public void nullIndexerDoesNotThrow() {
Deflector d = new Deflector(mock(SystemJobManager.class), final Map<String, IndexStats> deflectorIndices = deflector.getAllDeflectorIndices();
mock(ElasticsearchConfiguration.class),
mock(SystemMessageActivityWriter.class),
mock(SetIndexReadOnlyJob.Factory.class),
mock(CreateNewSingleIndexRangeJob.Factory.class),
mock(Indices.class));

final Map<String, IndexStats> deflectorIndices = d.getAllDeflectorIndices();
assertNotNull(deflectorIndices); assertNotNull(deflectorIndices);
assertEquals(0, deflectorIndices.size()); assertEquals(0, deflectorIndices.size());
} }


@Test @Test
public void nullIndexerDoesNotThrowOnIndexName() { public void nullIndexerDoesNotThrowOnIndexName() {
Deflector d = new Deflector(mock(SystemJobManager.class), final String[] deflectorIndices = deflector.getAllDeflectorIndexNames();
mock(ElasticsearchConfiguration.class),
mock(SystemMessageActivityWriter.class),
mock(SetIndexReadOnlyJob.Factory.class),
mock(CreateNewSingleIndexRangeJob.Factory.class),
mock(Indices.class));
final String[] deflectorIndices = d.getAllDeflectorIndexNames();
assertNotNull(deflectorIndices); assertNotNull(deflectorIndices);
assertEquals(0, deflectorIndices.length); assertEquals(0, deflectorIndices.length);
} }
Expand All @@ -133,8 +116,11 @@ public void testIsGraylogIndex() {
assertTrue(deflector.isGraylogIndex("graylog_1")); assertTrue(deflector.isGraylogIndex("graylog_1"));
assertTrue(deflector.isGraylogIndex("graylog_42")); assertTrue(deflector.isGraylogIndex("graylog_42"));
assertTrue(deflector.isGraylogIndex("graylog_100000000")); assertTrue(deflector.isGraylogIndex("graylog_100000000"));
assertFalse(deflector.isGraylogIndex(null));
assertFalse(deflector.isGraylogIndex(""));
assertFalse(deflector.isGraylogIndex("graylog_deflector")); assertFalse(deflector.isGraylogIndex("graylog_deflector"));
assertFalse(deflector.isGraylogIndex("graylog2beta_1")); assertFalse(deflector.isGraylogIndex("graylog2beta_1"));
assertFalse(deflector.isGraylogIndex("graylog_1_suffix"));
assertFalse(deflector.isGraylogIndex("HAHA")); assertFalse(deflector.isGraylogIndex("HAHA"));
} }
} }
Expand Up @@ -86,7 +86,7 @@ public EsIndexRangeServiceTest() {
public void setUp() throws Exception { public void setUp() throws Exception {
final Messages messages = new Messages(client, ELASTICSEARCH_CONFIGURATION); final Messages messages = new Messages(client, ELASTICSEARCH_CONFIGURATION);
indices = new Indices(client, ELASTICSEARCH_CONFIGURATION, new IndexMapping(), messages); indices = new Indices(client, ELASTICSEARCH_CONFIGURATION, new IndexMapping(), messages);
final Deflector deflector = new Deflector(null, ELASTICSEARCH_CONFIGURATION, new NullActivityWriter(), null, null, indices); final Deflector deflector = new Deflector(null, ELASTICSEARCH_CONFIGURATION.getIndexPrefix(), new NullActivityWriter(), null, null, indices);
indexRangeService = new EsIndexRangeService(client, deflector, localEventBus, clusterEventBus, new MetricRegistry()); indexRangeService = new EsIndexRangeService(client, deflector, localEventBus, clusterEventBus, new MetricRegistry());
} }


Expand Down

0 comments on commit 1728772

Please sign in to comment.