11
11
import org .elasticsearch .action .admin .indices .template .get .GetIndexTemplatesResponse ;
12
12
import org .elasticsearch .action .search .SearchResponse ;
13
13
import org .elasticsearch .client .Client ;
14
+ import org .elasticsearch .client .Request ;
15
+ import org .elasticsearch .client .RequestOptions ;
14
16
import org .elasticsearch .client .Response ;
15
17
import org .elasticsearch .cluster .ClusterState ;
16
18
import org .elasticsearch .cluster .metadata .IndexTemplateMetaData ;
17
19
import org .elasticsearch .common .io .stream .NamedWriteableRegistry ;
18
20
import org .elasticsearch .common .network .NetworkModule ;
19
21
import org .elasticsearch .common .settings .SecureString ;
20
22
import org .elasticsearch .common .settings .Settings ;
23
+ import org .elasticsearch .index .query .QueryBuilder ;
21
24
import org .elasticsearch .index .query .QueryBuilders ;
22
25
import org .elasticsearch .plugins .Plugin ;
26
+ import org .elasticsearch .tasks .Task ;
23
27
import org .elasticsearch .test .ESIntegTestCase ;
24
28
import org .elasticsearch .test .TestCluster ;
25
29
import org .elasticsearch .xpack .core .XPackClientPlugin ;
@@ -112,8 +116,53 @@ public void testIndexAuditTrailWorking() throws Exception {
112
116
UsernamePasswordToken .basicAuthHeaderValue (USER , new SecureString (PASS .toCharArray ()))));
113
117
assertThat (response .getStatusLine ().getStatusCode (), is (200 ));
114
118
final AtomicReference <ClusterState > lastClusterState = new AtomicReference <>();
119
+ final boolean found = awaitSecurityAuditIndex (lastClusterState , QueryBuilders .matchQuery ("principal" , USER ));
120
+
121
+ assertTrue ("Did not find security audit index. Current cluster state:\n " + lastClusterState .get ().toString (), found );
122
+
123
+ SearchResponse searchResponse = client ().prepareSearch (".security_audit_log*" ).setQuery (
124
+ QueryBuilders .matchQuery ("principal" , USER )).get ();
125
+ assertThat (searchResponse .getHits ().getHits ().length , greaterThan (0 ));
126
+ assertThat (searchResponse .getHits ().getAt (0 ).getSourceAsMap ().get ("principal" ), is (USER ));
127
+ }
128
+
129
+ public void testAuditTrailTemplateIsRecreatedAfterDelete () throws Exception {
130
+ // this is already "tested" by the test framework since we wipe the templates before and after,
131
+ // but lets be explicit about the behavior
132
+ awaitIndexTemplateCreation ();
133
+
134
+ // delete the template
135
+ DeleteIndexTemplateResponse deleteResponse = client ().admin ().indices ()
136
+ .prepareDeleteTemplate (IndexAuditTrail .INDEX_TEMPLATE_NAME ).execute ().actionGet ();
137
+ assertThat (deleteResponse .isAcknowledged (), is (true ));
138
+ awaitIndexTemplateCreation ();
139
+ }
140
+
141
+ public void testOpaqueIdWorking () throws Exception {
142
+ Request request = new Request ("GET" , "/" );
143
+ RequestOptions .Builder options = request .getOptions ().toBuilder ();
144
+ options .addHeader (Task .X_OPAQUE_ID , "foo" );
145
+ options .addHeader (UsernamePasswordToken .BASIC_AUTH_HEADER ,
146
+ UsernamePasswordToken .basicAuthHeaderValue (USER , new SecureString (PASS .toCharArray ())));
147
+ request .setOptions (options );
148
+ Response response = getRestClient ().performRequest (request );
149
+ assertThat (response .getStatusLine ().getStatusCode (), is (200 ));
150
+ final AtomicReference <ClusterState > lastClusterState = new AtomicReference <>();
151
+ final boolean found = awaitSecurityAuditIndex (lastClusterState , QueryBuilders .matchQuery ("opaque_id" , "foo" ));
152
+
153
+ assertTrue ("Did not find security audit index. Current cluster state:\n " + lastClusterState .get ().toString (), found );
154
+
155
+ SearchResponse searchResponse = client ().prepareSearch (".security_audit_log*" ).setQuery (
156
+ QueryBuilders .matchQuery ("opaque_id" , "foo" )).get ();
157
+ assertThat (searchResponse .getHits ().getHits ().length , greaterThan (0 ));
158
+
159
+ assertThat (searchResponse .getHits ().getAt (0 ).getSourceAsMap ().get ("opaque_id" ), is ("foo" ));
160
+ }
161
+
162
+ private boolean awaitSecurityAuditIndex (AtomicReference <ClusterState > lastClusterState ,
163
+ QueryBuilder query ) throws InterruptedException {
115
164
final AtomicBoolean indexExists = new AtomicBoolean (false );
116
- final boolean found = awaitBusy (() -> {
165
+ return awaitBusy (() -> {
117
166
if (indexExists .get () == false ) {
118
167
ClusterState state = client ().admin ().cluster ().prepareState ().get ().getState ();
119
168
lastClusterState .set (state );
@@ -138,28 +187,9 @@ public void testIndexAuditTrailWorking() throws Exception {
138
187
logger .info ("refreshing audit indices" );
139
188
client ().admin ().indices ().prepareRefresh (".security_audit_log*" ).get ();
140
189
logger .info ("refreshed audit indices" );
141
- return client ().prepareSearch (".security_audit_log*" ).setQuery (QueryBuilders . matchQuery ( "principal" , USER ) )
142
- .get ().getHits ().getTotalHits () > 0 ;
190
+ return client ().prepareSearch (".security_audit_log*" ).setQuery (query )
191
+ .get ().getHits ().getTotalHits () > 0 ;
143
192
}, 60L , TimeUnit .SECONDS );
144
-
145
- assertTrue ("Did not find security audit index. Current cluster state:\n " + lastClusterState .get ().toString (), found );
146
-
147
- SearchResponse searchResponse = client ().prepareSearch (".security_audit_log*" ).setQuery (
148
- QueryBuilders .matchQuery ("principal" , USER )).get ();
149
- assertThat (searchResponse .getHits ().getHits ().length , greaterThan (0 ));
150
- assertThat (searchResponse .getHits ().getAt (0 ).getSourceAsMap ().get ("principal" ), is (USER ));
151
- }
152
-
153
- public void testAuditTrailTemplateIsRecreatedAfterDelete () throws Exception {
154
- // this is already "tested" by the test framework since we wipe the templates before and after,
155
- // but lets be explicit about the behavior
156
- awaitIndexTemplateCreation ();
157
-
158
- // delete the template
159
- DeleteIndexTemplateResponse deleteResponse = client ().admin ().indices ()
160
- .prepareDeleteTemplate (IndexAuditTrail .INDEX_TEMPLATE_NAME ).execute ().actionGet ();
161
- assertThat (deleteResponse .isAcknowledged (), is (true ));
162
- awaitIndexTemplateCreation ();
163
193
}
164
194
165
195
private void awaitIndexTemplateCreation () throws InterruptedException {
0 commit comments