|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.plugin.correlation; |
| 10 | + |
| 11 | +import org.apache.lucene.search.join.ScoreMode; |
| 12 | +import org.junit.Assert; |
| 13 | +import org.opensearch.action.admin.cluster.node.info.NodeInfo; |
| 14 | +import org.opensearch.action.admin.cluster.node.info.NodesInfoRequest; |
| 15 | +import org.opensearch.action.admin.cluster.node.info.NodesInfoResponse; |
| 16 | +import org.opensearch.action.admin.cluster.node.info.PluginsAndModules; |
| 17 | +import org.opensearch.action.search.SearchRequest; |
| 18 | +import org.opensearch.action.search.SearchResponse; |
| 19 | +import org.opensearch.index.query.NestedQueryBuilder; |
| 20 | +import org.opensearch.index.query.QueryBuilders; |
| 21 | +import org.opensearch.plugin.correlation.rules.action.IndexCorrelationRuleAction; |
| 22 | +import org.opensearch.plugin.correlation.rules.action.IndexCorrelationRuleRequest; |
| 23 | +import org.opensearch.plugin.correlation.rules.action.IndexCorrelationRuleResponse; |
| 24 | +import org.opensearch.plugin.correlation.rules.model.CorrelationQuery; |
| 25 | +import org.opensearch.plugin.correlation.rules.model.CorrelationRule; |
| 26 | +import org.opensearch.plugin.correlation.utils.CorrelationConstants; |
| 27 | +import org.opensearch.plugins.Plugin; |
| 28 | +import org.opensearch.plugins.PluginInfo; |
| 29 | +import org.opensearch.rest.RestRequest; |
| 30 | +import org.opensearch.rest.RestStatus; |
| 31 | +import org.opensearch.search.builder.SearchSourceBuilder; |
| 32 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 33 | + |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.List; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.concurrent.ExecutionException; |
| 39 | +import java.util.function.Function; |
| 40 | +import java.util.stream.Collectors; |
| 41 | +import java.util.stream.Stream; |
| 42 | + |
| 43 | +public class EventsCorrelationPluginTransportIT extends OpenSearchIntegTestCase { |
| 44 | + |
| 45 | + @Override |
| 46 | + protected Collection<Class<? extends Plugin>> nodePlugins() { |
| 47 | + return Arrays.asList(EventsCorrelationPlugin.class); |
| 48 | + } |
| 49 | + |
| 50 | + public void testPluginsAreInstalled() { |
| 51 | + NodesInfoRequest nodesInfoRequest = new NodesInfoRequest(); |
| 52 | + nodesInfoRequest.addMetric(NodesInfoRequest.Metric.PLUGINS.metricName()); |
| 53 | + NodesInfoResponse nodesInfoResponse = OpenSearchIntegTestCase.client().admin().cluster().nodesInfo(nodesInfoRequest).actionGet(); |
| 54 | + List<PluginInfo> pluginInfos = nodesInfoResponse.getNodes() |
| 55 | + .stream() |
| 56 | + .flatMap( |
| 57 | + (Function<NodeInfo, Stream<PluginInfo>>) nodeInfo -> nodeInfo.getInfo(PluginsAndModules.class).getPluginInfos().stream() |
| 58 | + ) |
| 59 | + .collect(Collectors.toList()); |
| 60 | + Assert.assertTrue( |
| 61 | + pluginInfos.stream() |
| 62 | + .anyMatch(pluginInfo -> pluginInfo.getName().equals("org.opensearch.plugin.correlation.EventsCorrelationPlugin")) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + public void testCreatingACorrelationRule() throws ExecutionException, InterruptedException { |
| 67 | + List<CorrelationQuery> correlationQueries = Arrays.asList( |
| 68 | + new CorrelationQuery("s3_access_logs", "aws.cloudtrail.eventName:ReplicateObject", "@timestamp", List.of("s3")), |
| 69 | + new CorrelationQuery("app_logs", "keywords:PermissionDenied", "@timestamp", List.of("others_application")) |
| 70 | + ); |
| 71 | + CorrelationRule correlationRule = new CorrelationRule( |
| 72 | + CorrelationConstants.NO_ID, |
| 73 | + CorrelationConstants.NO_VERSION, |
| 74 | + "s3 to app logs", |
| 75 | + correlationQueries |
| 76 | + ); |
| 77 | + IndexCorrelationRuleRequest request = new IndexCorrelationRuleRequest( |
| 78 | + CorrelationConstants.NO_ID, |
| 79 | + correlationRule, |
| 80 | + RestRequest.Method.POST |
| 81 | + ); |
| 82 | + |
| 83 | + IndexCorrelationRuleResponse response = client().execute(IndexCorrelationRuleAction.INSTANCE, request).get(); |
| 84 | + Assert.assertEquals(RestStatus.CREATED, response.getStatus()); |
| 85 | + |
| 86 | + NestedQueryBuilder queryBuilder = QueryBuilders.nestedQuery( |
| 87 | + "correlate", |
| 88 | + QueryBuilders.matchQuery("correlate.index", "s3_access_logs"), |
| 89 | + ScoreMode.None |
| 90 | + ); |
| 91 | + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
| 92 | + searchSourceBuilder.query(queryBuilder); |
| 93 | + searchSourceBuilder.fetchSource(true); |
| 94 | + |
| 95 | + SearchRequest searchRequest = new SearchRequest(); |
| 96 | + searchRequest.indices(CorrelationRule.CORRELATION_RULE_INDEX); |
| 97 | + searchRequest.source(searchSourceBuilder); |
| 98 | + |
| 99 | + SearchResponse searchResponse = client().search(searchRequest).get(); |
| 100 | + Assert.assertEquals(1L, searchResponse.getHits().getTotalHits().value); |
| 101 | + } |
| 102 | + |
| 103 | + public void testFilteringCorrelationRules() throws ExecutionException, InterruptedException { |
| 104 | + List<CorrelationQuery> correlationQueries1 = Arrays.asList( |
| 105 | + new CorrelationQuery("s3_access_logs", "aws.cloudtrail.eventName:ReplicateObject", "@timestamp", List.of("s3")), |
| 106 | + new CorrelationQuery("app_logs", "keywords:PermissionDenied", "@timestamp", List.of("others_application")) |
| 107 | + ); |
| 108 | + CorrelationRule correlationRule1 = new CorrelationRule( |
| 109 | + CorrelationConstants.NO_ID, |
| 110 | + CorrelationConstants.NO_VERSION, |
| 111 | + "s3 to app logs", |
| 112 | + correlationQueries1 |
| 113 | + ); |
| 114 | + IndexCorrelationRuleRequest request1 = new IndexCorrelationRuleRequest( |
| 115 | + CorrelationConstants.NO_ID, |
| 116 | + correlationRule1, |
| 117 | + RestRequest.Method.POST |
| 118 | + ); |
| 119 | + client().execute(IndexCorrelationRuleAction.INSTANCE, request1).get(); |
| 120 | + |
| 121 | + List<CorrelationQuery> correlationQueries2 = Arrays.asList( |
| 122 | + new CorrelationQuery("windows", "host.hostname:EC2AMAZ*", "@timestamp", List.of("windows")), |
| 123 | + new CorrelationQuery("app_logs", "endpoint:/customer_records.txt", "@timestamp", List.of("others_application")) |
| 124 | + ); |
| 125 | + CorrelationRule correlationRule2 = new CorrelationRule( |
| 126 | + CorrelationConstants.NO_ID, |
| 127 | + CorrelationConstants.NO_VERSION, |
| 128 | + "windows to app logs", |
| 129 | + correlationQueries2 |
| 130 | + ); |
| 131 | + IndexCorrelationRuleRequest request2 = new IndexCorrelationRuleRequest( |
| 132 | + CorrelationConstants.NO_ID, |
| 133 | + correlationRule2, |
| 134 | + RestRequest.Method.POST |
| 135 | + ); |
| 136 | + client().execute(IndexCorrelationRuleAction.INSTANCE, request2).get(); |
| 137 | + |
| 138 | + NestedQueryBuilder queryBuilder = QueryBuilders.nestedQuery( |
| 139 | + "correlate", |
| 140 | + QueryBuilders.matchQuery("correlate.index", "s3_access_logs"), |
| 141 | + ScoreMode.None |
| 142 | + ); |
| 143 | + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
| 144 | + searchSourceBuilder.query(queryBuilder); |
| 145 | + searchSourceBuilder.fetchSource(true); |
| 146 | + |
| 147 | + SearchRequest searchRequest = new SearchRequest(); |
| 148 | + searchRequest.indices(CorrelationRule.CORRELATION_RULE_INDEX); |
| 149 | + searchRequest.source(searchSourceBuilder); |
| 150 | + |
| 151 | + SearchResponse searchResponse = client().search(searchRequest).get(); |
| 152 | + Assert.assertEquals(1L, searchResponse.getHits().getTotalHits().value); |
| 153 | + } |
| 154 | + |
| 155 | + @SuppressWarnings("unchecked") |
| 156 | + public void testCreatingACorrelationRuleWithNoTimestampField() throws ExecutionException, InterruptedException { |
| 157 | + List<CorrelationQuery> correlationQueries = Arrays.asList( |
| 158 | + new CorrelationQuery("s3_access_logs", "aws.cloudtrail.eventName:ReplicateObject", null, List.of("s3")), |
| 159 | + new CorrelationQuery("app_logs", "keywords:PermissionDenied", null, List.of("others_application")) |
| 160 | + ); |
| 161 | + CorrelationRule correlationRule = new CorrelationRule( |
| 162 | + CorrelationConstants.NO_ID, |
| 163 | + CorrelationConstants.NO_VERSION, |
| 164 | + "s3 to app logs", |
| 165 | + correlationQueries |
| 166 | + ); |
| 167 | + IndexCorrelationRuleRequest request = new IndexCorrelationRuleRequest( |
| 168 | + CorrelationConstants.NO_ID, |
| 169 | + correlationRule, |
| 170 | + RestRequest.Method.POST |
| 171 | + ); |
| 172 | + |
| 173 | + IndexCorrelationRuleResponse response = client().execute(IndexCorrelationRuleAction.INSTANCE, request).get(); |
| 174 | + Assert.assertEquals(RestStatus.CREATED, response.getStatus()); |
| 175 | + |
| 176 | + NestedQueryBuilder queryBuilder = QueryBuilders.nestedQuery( |
| 177 | + "correlate", |
| 178 | + QueryBuilders.matchQuery("correlate.index", "s3_access_logs"), |
| 179 | + ScoreMode.None |
| 180 | + ); |
| 181 | + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); |
| 182 | + searchSourceBuilder.query(queryBuilder); |
| 183 | + searchSourceBuilder.fetchSource(true); |
| 184 | + |
| 185 | + SearchRequest searchRequest = new SearchRequest(); |
| 186 | + searchRequest.indices(CorrelationRule.CORRELATION_RULE_INDEX); |
| 187 | + searchRequest.source(searchSourceBuilder); |
| 188 | + |
| 189 | + SearchResponse searchResponse = client().search(searchRequest).get(); |
| 190 | + Assert.assertEquals(1L, searchResponse.getHits().getTotalHits().value); |
| 191 | + Assert.assertEquals( |
| 192 | + "_timestamp", |
| 193 | + ((List<Map<String, Object>>) (searchResponse.getHits().getHits()[0].getSourceAsMap().get("correlate"))).get(0) |
| 194 | + .get("timestampField") |
| 195 | + ); |
| 196 | + } |
| 197 | +} |
0 commit comments