Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed aliases being returned in unmapped_index_fields #147

Merged
merged 4 commits into from
Dec 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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