4343import org .opensearch .security .test .helper .rest .RestHelper .HttpResponse ;
4444
4545import static org .hamcrest .MatcherAssert .assertThat ;
46+ import static org .hamcrest .Matchers .containsString ;
47+ import static org .hamcrest .Matchers .not ;
4648import static org .hamcrest .core .AnyOf .anyOf ;
4749import static org .hamcrest .core .IsEqual .equalTo ;
4850import static org .junit .Assert .assertThrows ;
@@ -90,10 +92,11 @@ public void testSourceFilter() throws Exception {
9092 Assert .assertEquals (HttpStatus .SC_OK , response .getStatusCode ());
9193 });
9294
93- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("COMPLIANCE_DOC_READ" ));
94- Assert .assertFalse (TestAuditlogImpl .sb .toString ().contains ("Designation" ));
95- Assert .assertFalse (TestAuditlogImpl .sb .toString ().contains ("Salary" ));
96- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("Gender" ));
95+ assertThat (message .getCategory (), equalTo (AuditCategory .COMPLIANCE_DOC_READ ));
96+ assertThat (message .getRequestBody (), not (containsString ("Designation" )));
97+ assertThat (message .getRequestBody (), not (containsString ("Salary" )));
98+ assertThat (message .getRequestBody (), containsString ("Gender" ));
99+
97100 Assert .assertTrue (validateMsgs (TestAuditlogImpl .messages ));
98101 }
99102
@@ -200,17 +203,24 @@ public void testSourceFilterMsearch() throws Exception {
200203 " }" +
201204 "}" +System .lineSeparator ();
202205
203- TestAuditlogImpl .doThenWaitForMessages (() -> {
206+ final List < AuditMessage > messages = TestAuditlogImpl .doThenWaitForMessages (() -> {
204207 HttpResponse response = rh .executePostRequest ("_msearch?pretty" , search , encodeBasicHeader ("admin" , "admin" ));
205208 assertNotContains (response , "*exception*" );
206209 Assert .assertEquals (HttpStatus .SC_OK , response .getStatusCode ());
207210 }, 2 );
208- System .out .println (TestAuditlogImpl .sb .toString ());
209- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("COMPLIANCE_DOC_READ" ));
210- Assert .assertFalse (TestAuditlogImpl .sb .toString ().contains ("Salary" ));
211- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("Gender" ));
212- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("Designation" ));
213- Assert .assertTrue (validateMsgs (TestAuditlogImpl .messages ));
211+
212+
213+ final AuditMessage desginationMsg = messages .stream ().filter (msg -> msg .getRequestBody ().contains ("Designation" )).findFirst ().orElseThrow ();
214+ assertThat (desginationMsg .getCategory (), equalTo (AuditCategory .COMPLIANCE_DOC_READ ));
215+ assertThat (desginationMsg .getRequestBody (), containsString ("Designation" ));
216+ assertThat (desginationMsg .getRequestBody (), not (containsString ("Salary" )));
217+
218+ final AuditMessage genderMsg = messages .stream ().filter (msg -> msg .getRequestBody ().contains ("Gender" )).findFirst ().orElseThrow ();
219+ assertThat (genderMsg .getCategory (), equalTo (AuditCategory .COMPLIANCE_DOC_READ ));
220+ assertThat (genderMsg .getRequestBody (), containsString ("Gender" ));
221+ assertThat (genderMsg .getRequestBody (), not (containsString ("Salary" )));
222+
223+ Assert .assertTrue (validateMsgs (messages ));
214224 }
215225
216226 @ Test
@@ -230,6 +240,7 @@ public void testInternalConfig() throws Exception {
230240
231241 setup (additionalSettings );
232242
243+ final List <String > expectedDocumentsTypes = List .of ("config" , "actiongroups" , "internalusers" , "roles" , "rolesmapping" , "tenants" , "audit" );
233244 final List <AuditMessage > messages = TestAuditlogImpl .doThenWaitForMessages (() -> {
234245 try (RestHighLevelClient restHighLevelClient = getRestClient (clusterInfo , "kirk-keystore.jks" , "truststore.jks" )) {
235246 for (IndexRequest ir : new DynamicSecurityConfig ().setSecurityRoles ("roles_2.yml" ).getDynamicConfig (getResourceFolder ())) {
@@ -245,21 +256,19 @@ public void testInternalConfig() throws Exception {
245256 assertThat (response .getStatusCode (), equalTo (HttpStatus .SC_OK ));
246257 }, 14 );
247258
248- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("COMPLIANCE_INTERNAL_CONFIG_READ" ));
249- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("COMPLIANCE_INTERNAL_CONFIG_WRITE" ));
250- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("anonymous_auth_enabled" ));
251- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("indices:data/read/suggest" ));
252- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("internalusers" ));
253- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("opendistro_security_all_access" ));
254- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("indices:data/read/suggest" ));
255- Assert .assertFalse (TestAuditlogImpl .sb .toString ().contains ("eyJzZWFyY2hndWFy" ));
256- Assert .assertFalse (TestAuditlogImpl .sb .toString ().contains ("eyJBTEwiOlsiaW" ));
257- Assert .assertFalse (TestAuditlogImpl .sb .toString ().contains ("eyJhZG1pbiI6e" ));
258- Assert .assertFalse (TestAuditlogImpl .sb .toString ().contains ("eyJzZ19hb" ));
259- Assert .assertFalse (TestAuditlogImpl .sb .toString ().contains ("eyJzZ19hbGx" ));
260- Assert .assertFalse (TestAuditlogImpl .sb .toString ().contains ("dvcmYiOnsiY2x" ));
261- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("\\ \" op\\ \" :\\ \" remove\\ \" ,\\ \" path\\ \" :\\ \" /opendistro_security_worf\\ \" " ));
262- Assert .assertTrue (validateMsgs (TestAuditlogImpl .messages ));
259+ final List <String > documentIds = messages .stream ().map (AuditMessage ::getDocId ).distinct ().collect (Collectors .toList ());
260+ assertThat (documentIds , equalTo (expectedDocumentsTypes ));
261+
262+ messages .stream ().collect (Collectors .groupingBy (AuditMessage ::getDocId )).entrySet ().forEach ((e ) -> {
263+ final String docId = e .getKey ();
264+ final List <AuditMessage > messagesByDocId = e .getValue ();
265+ assertThat ("Doc " + docId + " should have a read/write config message" ,
266+ messagesByDocId .stream ().map (AuditMessage ::getCategory ).collect (Collectors .toList ()),
267+ equalTo (List .of (AuditCategory .COMPLIANCE_INTERNAL_CONFIG_WRITE , AuditCategory .COMPLIANCE_INTERNAL_CONFIG_READ ))
268+ );
269+ });
270+
271+ Assert .assertTrue (validateMsgs (messages ));
263272 }
264273
265274 @ Test
@@ -276,7 +285,7 @@ public void testExternalConfig() throws Exception {
276285 .put (ConfigConstants .OPENDISTRO_SECURITY_AUDIT_CONFIG_DISABLED_REST_CATEGORIES , "authenticated,GRANTED_PRIVILEGES" )
277286 .build ();
278287
279- TestAuditlogImpl .doThenWaitForMessages (() -> {
288+ final List < AuditMessage > messages = TestAuditlogImpl .doThenWaitForMessages (() -> {
280289 try {
281290 setup (additionalSettings );
282291 } catch (final Exception ex ) {
@@ -293,10 +302,17 @@ public void testExternalConfig() throws Exception {
293302 Assert .assertEquals (HttpStatus .SC_OK , response .getStatusCode ());
294303 }, 4 );
295304
296- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("external_configuration" ));
297- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("COMPLIANCE_EXTERNAL_CONFIG" ));
298- Assert .assertTrue (TestAuditlogImpl .sb .toString ().contains ("opensearch_yml" ));
299- Assert .assertTrue (validateMsgs (TestAuditlogImpl .messages ));
305+ // Record the updated config, and then for each node record that the config was updated
306+ assertThat (messages .get (0 ).getCategory (), equalTo (AuditCategory .COMPLIANCE_INTERNAL_CONFIG_WRITE ));
307+ assertThat (messages .get (1 ).getCategory (), equalTo (AuditCategory .COMPLIANCE_EXTERNAL_CONFIG ));
308+ assertThat (messages .get (2 ).getCategory (), equalTo (AuditCategory .COMPLIANCE_EXTERNAL_CONFIG ));
309+ assertThat (messages .get (3 ).getCategory (), equalTo (AuditCategory .COMPLIANCE_EXTERNAL_CONFIG ));
310+
311+ // Make sure that the config update messsages are for each node in the cluster
312+ assertThat (messages .get (1 ).getNodeId (), not (equalTo (messages .get (2 ).getNodeId ())));
313+ assertThat (messages .get (2 ).getNodeId (), not (equalTo (messages .get (3 ).getNodeId ())));
314+
315+ Assert .assertTrue (validateMsgs (messages ));
300316 }
301317
302318 @ Test
0 commit comments