Skip to content

Commit

Permalink
Block field type changes in Illuminate index sets (#19923)
Browse files Browse the repository at this point in the history
  • Loading branch information
luk-kaminski authored Jul 17, 2024
1 parent eb473ca commit 942827f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

public interface IndexTemplateProvider<T extends IndexMappingTemplate> {

String FAILURE_TEMPLATE_TYPE = "failures";
String ILLUMINATE_INDEX_TEMPLATE_TYPE = "illuminate_content";

@Nonnull
T create(@Nonnull SearchVersion searchVersion, @Nonnull IndexSetConfig indexSetConfig)
throws IgnoreIndexTemplate;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.graylog.autovalue.WithBeanGetter;
import org.graylog2.database.DbEntity;
import org.graylog2.datatiering.DataTieringConfig;
import org.graylog2.indexer.IndexTemplateProvider;
import org.graylog2.indexer.MessageIndexTemplateProvider;
import org.graylog2.plugin.indexer.retention.RetentionStrategyConfig;
import org.graylog2.plugin.indexer.rotation.RotationStrategyConfig;
Expand All @@ -41,6 +42,7 @@
import javax.annotation.Nullable;
import java.time.ZonedDateTime;
import java.util.Optional;
import java.util.Set;

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.graylog2.indexer.EventIndexTemplateProvider.EVENT_TEMPLATE_TYPE;
Expand All @@ -60,6 +62,12 @@ public abstract class IndexSetConfig implements Comparable<IndexSetConfig>, Simp
public static final String FIELD_INDEX_TEMPLATE_NAME = "index_template_name";
public static final String FIELD_CUSTOM_FIELD_MAPPINGS = "custom_field_mappings";

private static final Set<String> TEMPLATE_TYPES_FOR_INDEX_SETS_WITH_IMMUTABLE_FIELD_TYPES = Set.of(
EVENT_TEMPLATE_TYPE,
IndexTemplateProvider.FAILURE_TEMPLATE_TYPE,
IndexTemplateProvider.ILLUMINATE_INDEX_TEMPLATE_TYPE
);

@JsonCreator
public static IndexSetConfig create(@Id @ObjectId @JsonProperty("_id") @Nullable String id,
@JsonProperty(FIELD_TITLE) @NotBlank String title,
Expand Down Expand Up @@ -254,20 +262,12 @@ public boolean isRegularIndex() {

@JsonIgnore
public boolean canHaveCustomFieldMappings() {
final String indexTemplateType = this.indexTemplateType().orElse(null);
if (EVENT_TEMPLATE_TYPE.equals(indexTemplateType) || "failures".equals(indexTemplateType)) {
return false;
}
return true;
return !this.indexTemplateType().map(TEMPLATE_TYPES_FOR_INDEX_SETS_WITH_IMMUTABLE_FIELD_TYPES::contains).orElse(false);
}

@JsonIgnore
public boolean canHaveProfile() {
final String indexTemplateType = this.indexTemplateType().orElse(null);
if (EVENT_TEMPLATE_TYPE.equals(indexTemplateType) || "failures".equals(indexTemplateType)) {
return false;
}
return true;
return !this.indexTemplateType().map(TEMPLATE_TYPES_FOR_INDEX_SETS_WITH_IMMUTABLE_FIELD_TYPES::contains).orElse(false);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.graylog2.indexer.indexset;

import org.graylog2.indexer.IndexTemplateProvider;
import org.graylog2.indexer.MessageIndexTemplateProvider;
import org.graylog2.indexer.retention.strategies.NoopRetentionStrategy;
import org.graylog2.indexer.retention.strategies.NoopRetentionStrategyConfig;
Expand Down Expand Up @@ -299,14 +300,28 @@ public void testEventIndexWithProfileSetIsIllegal() {

@Test
public void testFailureIndexWithProfileSetIsIllegal() {
assertFalse(testIndexSetConfig("failures",
assertFalse(testIndexSetConfig(IndexTemplateProvider.FAILURE_TEMPLATE_TYPE,
null,
"profile").canHaveProfile());
}

@Test
public void testFailureIndexWithChangedFieldMappingsIsIllegal() {
assertFalse(testIndexSetConfig("failures",
assertFalse(testIndexSetConfig(IndexTemplateProvider.FAILURE_TEMPLATE_TYPE,
new CustomFieldMappings(List.of(new CustomFieldMapping("john", "long"))),
null).canHaveCustomFieldMappings());
}

@Test
public void testIlluminateIndexWithProfileSetIsIllegal() {
assertFalse(testIndexSetConfig(IndexTemplateProvider.ILLUMINATE_INDEX_TEMPLATE_TYPE,
null,
"profile").canHaveProfile());
}

@Test
public void testIlluminateIndexWithChangedFieldMappingsIsIllegal() {
assertFalse(testIndexSetConfig(IndexTemplateProvider.ILLUMINATE_INDEX_TEMPLATE_TYPE,
new CustomFieldMappings(List.of(new CustomFieldMapping("john", "long"))),
null).canHaveCustomFieldMappings());
}
Expand Down

0 comments on commit 942827f

Please sign in to comment.