|
7 | 7 |
|
8 | 8 | import org.apache.http.HttpEntity; |
9 | 9 | import org.apache.http.StatusLine; |
10 | | -import org.apache.http.entity.ContentType; |
11 | | -import org.apache.http.entity.StringEntity; |
12 | | -import org.apache.http.message.BasicHeader; |
13 | 10 | import org.apache.http.util.EntityUtils; |
| 11 | +import org.elasticsearch.client.Request; |
| 12 | +import org.elasticsearch.client.RequestOptions; |
14 | 13 | import org.elasticsearch.client.Response; |
15 | 14 | import org.elasticsearch.client.ResponseException; |
16 | 15 | import org.elasticsearch.common.settings.SecureString; |
17 | 16 | import org.elasticsearch.test.SecuritySingleNodeTestCase; |
18 | 17 | import org.elasticsearch.xpack.core.security.authc.support.UsernamePasswordToken; |
19 | 18 |
|
20 | 19 | import java.io.IOException; |
21 | | -import java.util.HashMap; |
22 | 20 | import java.util.Locale; |
23 | | -import java.util.Map; |
24 | 21 |
|
25 | 22 | import static org.hamcrest.Matchers.containsString; |
26 | 23 | import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
27 | 24 | import static org.hamcrest.Matchers.is; |
28 | 25 | import static org.hamcrest.Matchers.not; |
29 | 26 |
|
30 | 27 | /** |
31 | | - * a helper class that contains a couple of HTTP helper methods |
| 28 | + * A helper class that contains a couple of HTTP helper methods. |
32 | 29 | */ |
33 | 30 | public abstract class AbstractPrivilegeTestCase extends SecuritySingleNodeTestCase { |
34 | 31 |
|
35 | | - protected void assertAccessIsAllowed(String user, String method, String uri, String body, |
36 | | - Map<String, String> params) throws IOException { |
37 | | - Response response = getRestClient().performRequest(method, uri, params, entityOrNull(body), |
38 | | - new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, |
39 | | - UsernamePasswordToken.basicAuthHeaderValue(user, new SecureString("passwd".toCharArray())))); |
| 32 | + protected void assertAccessIsAllowed(String user, Request request) throws IOException { |
| 33 | + setUser(request, user); |
| 34 | + Response response = getRestClient().performRequest(request); |
40 | 35 | StatusLine statusLine = response.getStatusLine(); |
41 | | - String message = String.format(Locale.ROOT, "%s %s: Expected no error got %s %s with body %s", method, uri, |
42 | | - statusLine.getStatusCode(), statusLine.getReasonPhrase(), EntityUtils.toString(response.getEntity())); |
| 36 | + String message = String.format(Locale.ROOT, "%s %s: Expected no error got %s %s with body %s", |
| 37 | + request.getMethod(), request.getEndpoint(), statusLine.getStatusCode(), |
| 38 | + statusLine.getReasonPhrase(), EntityUtils.toString(response.getEntity())); |
43 | 39 | assertThat(message, statusLine.getStatusCode(), is(not(greaterThanOrEqualTo(400)))); |
44 | 40 | } |
45 | 41 |
|
46 | 42 | protected void assertAccessIsAllowed(String user, String method, String uri, String body) throws IOException { |
47 | | - assertAccessIsAllowed(user, method, uri, body, new HashMap<>()); |
| 43 | + Request request = new Request(method, uri); |
| 44 | + request.setJsonEntity(body); |
| 45 | + assertAccessIsAllowed(user, request); |
48 | 46 | } |
49 | 47 |
|
50 | 48 | protected void assertAccessIsAllowed(String user, String method, String uri) throws IOException { |
51 | | - assertAccessIsAllowed(user, method, uri, null, new HashMap<>()); |
| 49 | + assertAccessIsAllowed(user, new Request(method, uri)); |
52 | 50 | } |
53 | 51 |
|
54 | | - protected void assertAccessIsDenied(String user, String method, String uri, String body) throws IOException { |
55 | | - assertAccessIsDenied(user, method, uri, body, new HashMap<>()); |
56 | | - } |
57 | | - |
58 | | - protected void assertAccessIsDenied(String user, String method, String uri) throws IOException { |
59 | | - assertAccessIsDenied(user, method, uri, null, new HashMap<>()); |
60 | | - } |
61 | | - |
62 | | - protected void assertAccessIsDenied(String user, String method, String uri, String body, |
63 | | - Map<String, String> params) throws IOException { |
64 | | - ResponseException responseException = expectThrows(ResponseException.class, |
65 | | - () -> getRestClient().performRequest(method, uri, params, entityOrNull(body), |
66 | | - new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, |
67 | | - UsernamePasswordToken.basicAuthHeaderValue(user, new SecureString("passwd".toCharArray()))))); |
| 52 | + protected void assertAccessIsDenied(String user, Request request) throws IOException { |
| 53 | + setUser(request, user); |
| 54 | + ResponseException responseException = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request)); |
68 | 55 | StatusLine statusLine = responseException.getResponse().getStatusLine(); |
69 | | - String message = String.format(Locale.ROOT, "%s %s body %s: Expected 403, got %s %s with body %s", method, uri, body, |
| 56 | + String requestBody = request.getEntity() == null ? "" : "with body " + EntityUtils.toString(request.getEntity()); |
| 57 | + String message = String.format(Locale.ROOT, "%s %s body %s: Expected 403, got %s %s with body %s", |
| 58 | + request.getMethod(), request.getEndpoint(), requestBody, |
70 | 59 | statusLine.getStatusCode(), statusLine.getReasonPhrase(), |
71 | 60 | EntityUtils.toString(responseException.getResponse().getEntity())); |
72 | 61 | assertThat(message, statusLine.getStatusCode(), is(403)); |
73 | 62 | } |
74 | 63 |
|
| 64 | + protected void assertAccessIsDenied(String user, String method, String uri, String body) throws IOException { |
| 65 | + Request request = new Request(method, uri); |
| 66 | + request.setJsonEntity(body); |
| 67 | + assertAccessIsDenied(user, request); |
| 68 | + } |
75 | 69 |
|
76 | | - protected void assertBodyHasAccessIsDenied(String user, String method, String uri, String body) throws IOException { |
77 | | - assertBodyHasAccessIsDenied(user, method, uri, body, new HashMap<>()); |
| 70 | + protected void assertAccessIsDenied(String user, String method, String uri) throws IOException { |
| 71 | + assertAccessIsDenied(user, new Request(method, uri)); |
78 | 72 | } |
79 | 73 |
|
80 | 74 | /** |
81 | 75 | * Like {@code assertAcessIsDenied}, but for _bulk requests since the entire |
82 | 76 | * request will not be failed, just the individual ones |
83 | 77 | */ |
84 | | - protected void assertBodyHasAccessIsDenied(String user, String method, String uri, String body, |
85 | | - Map<String, String> params) throws IOException { |
86 | | - Response resp = getRestClient().performRequest(method, uri, params, entityOrNull(body), |
87 | | - new BasicHeader(UsernamePasswordToken.BASIC_AUTH_HEADER, |
88 | | - UsernamePasswordToken.basicAuthHeaderValue(user, new SecureString("passwd".toCharArray())))); |
| 78 | + protected void assertBodyHasAccessIsDenied(String user, Request request) throws IOException { |
| 79 | + setUser(request, user); |
| 80 | + Response resp = getRestClient().performRequest(request); |
89 | 81 | StatusLine statusLine = resp.getStatusLine(); |
90 | 82 | assertThat(statusLine.getStatusCode(), is(200)); |
91 | 83 | HttpEntity bodyEntity = resp.getEntity(); |
92 | 84 | String bodyStr = EntityUtils.toString(bodyEntity); |
93 | 85 | assertThat(bodyStr, containsString("unauthorized for user [" + user + "]")); |
94 | 86 | } |
95 | 87 |
|
96 | | - private static HttpEntity entityOrNull(String body) { |
97 | | - HttpEntity entity = null; |
98 | | - if (body != null) { |
99 | | - entity = new StringEntity(body, ContentType.APPLICATION_JSON); |
100 | | - } |
101 | | - return entity; |
| 88 | + protected void assertBodyHasAccessIsDenied(String user, String method, String uri, String body) throws IOException { |
| 89 | + Request request = new Request(method, uri); |
| 90 | + request.setJsonEntity(body); |
| 91 | + assertBodyHasAccessIsDenied(user, request); |
| 92 | + } |
| 93 | + |
| 94 | + private void setUser(Request request, String user) { |
| 95 | + RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder(); |
| 96 | + options.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(user, new SecureString("passwd".toCharArray()))); |
| 97 | + request.setOptions(options); |
102 | 98 | } |
103 | 99 | } |
0 commit comments