Skip to content

Commit 6e93b5a

Browse files
committed
Merge remote-tracking branch 'upstream/6.x' into index-lifecycle-6.x
2 parents d95ac27 + c4bb2cc commit 6e93b5a

File tree

6 files changed

+109
-6
lines changed

6 files changed

+109
-6
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/doc/RestTestsFromSnippetsTask.groovy

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,11 @@ public class RestTestsFromSnippetsTask extends SnippetsTask {
197197
previousTest = snippet
198198
return
199199
}
200+
if (snippet.testTearDown) {
201+
testTearDown(snippet)
202+
previousTest = snippet
203+
return
204+
}
200205
if (snippet.testResponse) {
201206
response(snippet)
202207
return
@@ -223,6 +228,10 @@ public class RestTestsFromSnippetsTask extends SnippetsTask {
223228
throw new InvalidUserDataException("// TEST[continued] " +
224229
"cannot immediately follow // TESTSETUP: $test")
225230
}
231+
if (previousTest != null && previousTest.testTearDown) {
232+
throw new InvalidUserDataException("// TEST[continued] " +
233+
"cannot immediately follow // TEARDOWN: $test")
234+
}
226235
} else {
227236
current.println('---')
228237
current.println("\"line_$test.start\":")
@@ -354,6 +363,16 @@ public class RestTestsFromSnippetsTask extends SnippetsTask {
354363
body(snippet, true)
355364
}
356365

366+
private void testTearDown(Snippet snippet) {
367+
if (previousTest.testSetup == false && lastDocsPath == snippet.path) {
368+
throw new InvalidUserDataException("$snippet must follow test setup or be first")
369+
}
370+
setupCurrent(snippet)
371+
current.println('---')
372+
current.println('teardown:')
373+
body(snippet, true)
374+
}
375+
357376
private void body(Snippet snippet, boolean inSetup) {
358377
parse("$snippet", snippet.contents, SYNTAX) { matcher, last ->
359378
if (matcher.group("comment") != null) {

buildSrc/src/main/groovy/org/elasticsearch/gradle/doc/SnippetsTask.groovy

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,10 @@ public class SnippetsTask extends DefaultTask {
273273
snippet.testSetup = true
274274
return
275275
}
276+
if (line ==~ /\/\/\s*TEARDOWN\s*/) {
277+
snippet.testTearDown = true
278+
return
279+
}
276280
if (snippet == null) {
277281
// Outside
278282
return
@@ -317,6 +321,7 @@ public class SnippetsTask extends DefaultTask {
317321
boolean test = false
318322
boolean testResponse = false
319323
boolean testSetup = false
324+
boolean testTearDown = false
320325
String skip = null
321326
boolean continued = false
322327
String language = null

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesAction.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.security.rest.action.user;
77

8+
import org.elasticsearch.ElasticsearchSecurityException;
89
import org.elasticsearch.client.node.NodeClient;
910
import org.elasticsearch.common.settings.Settings;
1011
import org.elasticsearch.common.xcontent.ToXContent;
@@ -24,6 +25,7 @@
2425
import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivilege;
2526
import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivileges;
2627
import org.elasticsearch.xpack.core.security.client.SecurityClient;
28+
import org.elasticsearch.xpack.core.security.user.User;
2729
import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler;
2830

2931
import java.io.IOException;
@@ -52,7 +54,11 @@ public String getName() {
5254

5355
@Override
5456
public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException {
55-
final String username = securityContext.getUser().principal();
57+
final User user = securityContext.getUser();
58+
if (user == null) {
59+
return restChannel -> { throw new ElasticsearchSecurityException("there is no authenticated user"); };
60+
}
61+
final String username = user.principal();
5662
final GetUserPrivilegesRequestBuilder requestBuilder = new SecurityClient(client).prepareGetUserPrivileges(username);
5763
return channel -> requestBuilder.execute(new RestListener(channel));
5864
}

x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/rest/action/user/RestHasPrivilegesAction.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
package org.elasticsearch.xpack.security.rest.action.user;
77

8+
import org.elasticsearch.ElasticsearchSecurityException;
89
import org.elasticsearch.client.node.NodeClient;
910
import org.elasticsearch.common.bytes.BytesReference;
1011
import org.elasticsearch.common.collect.Tuple;
@@ -24,6 +25,7 @@
2425
import org.elasticsearch.xpack.core.security.action.user.HasPrivilegesResponse;
2526
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor;
2627
import org.elasticsearch.xpack.core.security.client.SecurityClient;
28+
import org.elasticsearch.xpack.core.security.user.User;
2729
import org.elasticsearch.xpack.security.rest.action.SecurityBaseRestHandler;
2830

2931
import java.io.IOException;
@@ -59,6 +61,9 @@ public String getName() {
5961
@Override
6062
public RestChannelConsumer innerPrepareRequest(RestRequest request, NodeClient client) throws IOException {
6163
final String username = getUsername(request);
64+
if (username == null) {
65+
return restChannel -> { throw new ElasticsearchSecurityException("there is no authenticated user"); };
66+
}
6267
final Tuple<XContentType, BytesReference> content = request.contentOrSourceParam();
6368
HasPrivilegesRequestBuilder requestBuilder = new SecurityClient(client).prepareHasPrivileges(username, content.v2(), content.v1());
6469
return channel -> requestBuilder.execute(new HasPrivilegesRestResponseBuilder(username, channel));
@@ -69,7 +74,11 @@ private String getUsername(RestRequest request) {
6974
if (username != null) {
7075
return username;
7176
}
72-
return securityContext.getUser().principal();
77+
final User user = securityContext.getUser();
78+
if (user == null) {
79+
return null;
80+
}
81+
return user.principal();
7382
}
7483

7584
static class HasPrivilegesRestResponseBuilder extends RestBuilderListener<HasPrivilegesResponse> {

x-pack/plugin/security/src/test/java/org/elasticsearch/xpack/security/rest/action/user/RestGetUserPrivilegesActionTests.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,52 @@
66

77
package org.elasticsearch.xpack.security.rest.action.user;
88

9+
import org.elasticsearch.client.node.NodeClient;
910
import org.elasticsearch.common.Strings;
1011
import org.elasticsearch.common.bytes.BytesArray;
12+
import org.elasticsearch.common.settings.Settings;
1113
import org.elasticsearch.common.util.set.Sets;
1214
import org.elasticsearch.common.xcontent.XContentBuilder;
15+
import org.elasticsearch.license.XPackLicenseState;
16+
import org.elasticsearch.rest.RestController;
17+
import org.elasticsearch.rest.RestStatus;
1318
import org.elasticsearch.test.ESTestCase;
19+
import org.elasticsearch.test.rest.FakeRestChannel;
20+
import org.elasticsearch.test.rest.FakeRestRequest;
21+
import org.elasticsearch.xpack.core.security.SecurityContext;
1422
import org.elasticsearch.xpack.core.security.action.user.GetUserPrivilegesResponse;
1523
import org.elasticsearch.xpack.core.security.authz.RoleDescriptor.ApplicationResourcePrivileges;
1624
import org.elasticsearch.xpack.core.security.authz.permission.FieldPermissionsDefinition;
1725
import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivilege;
1826
import org.elasticsearch.xpack.core.security.authz.privilege.ConditionalClusterPrivileges;
19-
import org.hamcrest.Matchers;
2027

2128
import java.util.Arrays;
2229
import java.util.Collections;
2330
import java.util.LinkedHashSet;
2431
import java.util.Set;
2532

2633
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
34+
import static org.hamcrest.Matchers.containsString;
35+
import static org.hamcrest.Matchers.equalTo;
36+
import static org.hamcrest.Matchers.notNullValue;
37+
import static org.mockito.Mockito.mock;
38+
import static org.mockito.Mockito.when;
2739

2840
public class RestGetUserPrivilegesActionTests extends ESTestCase {
2941

42+
public void testBasicLicense() throws Exception {
43+
final XPackLicenseState licenseState = mock(XPackLicenseState.class);
44+
final RestGetUserPrivilegesAction action = new RestGetUserPrivilegesAction(Settings.EMPTY, mock(RestController.class),
45+
mock(SecurityContext.class), licenseState);
46+
when(licenseState.isSecurityAvailable()).thenReturn(false);
47+
final FakeRestRequest request = new FakeRestRequest();
48+
final FakeRestChannel channel = new FakeRestChannel(request, true, 1);
49+
action.handleRequest(request, channel, mock(NodeClient.class));
50+
assertThat(channel.capturedResponse(), notNullValue());
51+
assertThat(channel.capturedResponse().status(), equalTo(RestStatus.FORBIDDEN));
52+
assertThat(channel.capturedResponse().content().utf8ToString(), containsString("current license is non-compliant for [security]"));
53+
}
54+
3055
public void testBuildResponse() throws Exception {
3156
final RestGetUserPrivilegesAction.RestListener listener = new RestGetUserPrivilegesAction.RestListener(null);
3257

@@ -37,8 +62,8 @@ public void testBuildResponse() throws Exception {
3762
final Set<GetUserPrivilegesResponse.Indices> index = new LinkedHashSet<>(Arrays.asList(
3863
new GetUserPrivilegesResponse.Indices(Arrays.asList("index-1", "index-2", "index-3-*"), Arrays.asList("read", "write"),
3964
new LinkedHashSet<>(Arrays.asList(
40-
new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[]{ "public.*" }, new String[0]),
41-
new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[]{ "*" }, new String[]{ "private.*" })
65+
new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[]{"public.*"}, new String[0]),
66+
new FieldPermissionsDefinition.FieldGrantExcludeGroup(new String[]{"*"}, new String[]{"private.*"})
4267
)),
4368
new LinkedHashSet<>(Arrays.asList(
4469
new BytesArray("{ \"term\": { \"access\": \"public\" } }"),
@@ -60,7 +85,7 @@ public void testBuildResponse() throws Exception {
6085
listener.buildResponse(response, builder);
6186

6287
String json = Strings.toString(builder);
63-
assertThat(json, Matchers.equalTo("{" +
88+
assertThat(json, equalTo("{" +
6489
"\"cluster\":[\"monitor\",\"manage_ml\",\"manage_watcher\"]," +
6590
"\"global\":[" +
6691
"{\"application\":{\"manage\":{\"applications\":[\"app01\",\"app02\"]}}}" +
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.security.rest.action.user;
7+
8+
import org.elasticsearch.client.node.NodeClient;
9+
import org.elasticsearch.common.settings.Settings;
10+
import org.elasticsearch.license.XPackLicenseState;
11+
import org.elasticsearch.rest.RestController;
12+
import org.elasticsearch.rest.RestStatus;
13+
import org.elasticsearch.test.ESTestCase;
14+
import org.elasticsearch.test.rest.FakeRestChannel;
15+
import org.elasticsearch.test.rest.FakeRestRequest;
16+
import org.elasticsearch.xpack.core.security.SecurityContext;
17+
18+
import static org.hamcrest.Matchers.containsString;
19+
import static org.hamcrest.Matchers.equalTo;
20+
import static org.hamcrest.Matchers.notNullValue;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.when;
23+
24+
public class RestHasPrivilegesActionTests extends ESTestCase {
25+
26+
public void testBasicLicense() throws Exception {
27+
final XPackLicenseState licenseState = mock(XPackLicenseState.class);
28+
final RestHasPrivilegesAction action = new RestHasPrivilegesAction(Settings.EMPTY, mock(RestController.class),
29+
mock(SecurityContext.class), licenseState);
30+
when(licenseState.isSecurityAvailable()).thenReturn(false);
31+
final FakeRestRequest request = new FakeRestRequest();
32+
final FakeRestChannel channel = new FakeRestChannel(request, true, 1);
33+
action.handleRequest(request, channel, mock(NodeClient.class));
34+
assertThat(channel.capturedResponse(), notNullValue());
35+
assertThat(channel.capturedResponse().status(), equalTo(RestStatus.FORBIDDEN));
36+
assertThat(channel.capturedResponse().content().utf8ToString(), containsString("current license is non-compliant for [security]"));
37+
}
38+
39+
}

0 commit comments

Comments
 (0)