Skip to content

Commit

Permalink
fixed aliases being returned in unmapped_index_fields (#147)
Browse files Browse the repository at this point in the history
Signed-off-by: Petar Dzepina <petar.dzepina@gmail.com>
(cherry picked from commit 1c798dc)
  • Loading branch information
petardz authored and github-actions[bot] committed Dec 24, 2022
1 parent 276d4bb commit e8533f0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,6 @@ public void addListener(MappingsTraverserListener l) {
this.mappingsTraverserListeners.add(l);
}

/**
* Sets set of property "type" values to skip during traversal.
* @param types Set of strings representing property "type"
*/
public void setTypesToSkip(Set<String> types) {
this.typesToSkip = types;
}

/**
* Traverses mappings tree and collects all fields that are not of type "alias".
* Nested fields are flattened.
Expand All @@ -136,7 +128,7 @@ public void setTypesToSkip(Set<String> types) {
public List<String> extractFlatNonAliasFields() {
List<String> flatProperties = new ArrayList<>();
// Setup
this.typesToSkip.add(ALIAS);
this.propertiesToSkip.add(Pair.of(TYPE, ALIAS));
this.mappingsTraverserListeners.add(new MappingsTraverserListener() {
@Override
public void onLeafVisited(Node node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,18 @@ public void testCreateMappingSuccess() throws IOException {
" \"partial\":true" +
"}"
);
// request.addParameter("indexName", testIndexName);
// request.addParameter("ruleTopic", "netflow");
Response response = client().performRequest(request);
assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());

// Verify mappings
GetMappingsResponse getMappingsResponse = SecurityAnalyticsClientUtils.executeGetMappingsRequest(testIndexName);
MappingsTraverser mappingsTraverser = new MappingsTraverser(getMappingsResponse.getMappings().iterator().next().value);
// After applying netflow aliases, our index will have 4 alias mappings
List<String> flatProperties = mappingsTraverser.extractFlatNonAliasFields();
assertTrue(flatProperties.contains("source.ip"));
assertTrue(flatProperties.contains("destination.ip"));
assertTrue(flatProperties.contains("source.port"));
assertTrue(flatProperties.contains("destination.port"));
assertFalse(flatProperties.contains("source.ip"));
assertFalse(flatProperties.contains("destination.ip"));
assertFalse(flatProperties.contains("source.port"));
assertFalse(flatProperties.contains("destination.port"));
// Try searching by alias field
String query = "{" +
" \"query\": {" +
Expand Down Expand Up @@ -110,8 +109,8 @@ public void testCreateMappingWithAliasesSuccess() throws IOException {
GetMappingsResponse getMappingsResponse = SecurityAnalyticsClientUtils.executeGetMappingsRequest(testIndexName);
MappingsTraverser mappingsTraverser = new MappingsTraverser(getMappingsResponse.getMappings().iterator().next().value);
List<String> flatProperties = mappingsTraverser.extractFlatNonAliasFields();
assertTrue(flatProperties.contains("source.ip"));
assertTrue(flatProperties.contains("source.port"));
assertFalse(flatProperties.contains("source.ip"));
assertFalse(flatProperties.contains("source.port"));
// Try searching by alias field
String query = "{" +
" \"query\": {" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ public void testValidateIndexMappingsNoMissing() throws IOException {
assertEquals(0, missingFields.size());
}

public void testGetAllNonAliasFieldsFromIndex_success() throws IOException {
// Create index mappings
Map<String, Object> m = new HashMap<>();
m.put("netflow.event_data.SourceAddress", Map.of("type", "ip"));
m.put("alias_123", Map.of("type", "alias", "path", "netflow.event_data.SourceAddress"));
Map<String, Object> properties = Map.of("properties", m);
Map<String, Object> root = Map.of(MapperService.SINGLE_MAPPING_NAME, properties);
MappingMetadata mappingMetadata = new MappingMetadata(MapperService.SINGLE_MAPPING_NAME, root);

List<String> fields = MapperUtils.getAllNonAliasFieldsFromIndex(mappingMetadata);
assertEquals(1, fields.size());
assertEquals("netflow.event_data.SourceAddress", fields.get(0));
}

public void testGetAllPathsFromAliasMappingsSuccess() throws IOException {
MapperTopicStore.putAliasMappings("test123", "testValidAliasMappingsSimple.json");

Expand Down

0 comments on commit e8533f0

Please sign in to comment.