|
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 | + RequestOptions.Builder options = request.getOptions().toBuilder(); |
| 34 | + options.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(user, new SecureString("passwd".toCharArray()))); |
| 35 | + request.setOptions(options); |
| 36 | + Response response = getRestClient().performRequest(request); |
40 | 37 | 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())); |
| 38 | + String message = String.format(Locale.ROOT, "%s %s: Expected no error got %s %s with body %s", |
| 39 | + request.getMethod(), request.getEndpoint(), statusLine.getStatusCode(), |
| 40 | + statusLine.getReasonPhrase(), EntityUtils.toString(response.getEntity())); |
43 | 41 | assertThat(message, statusLine.getStatusCode(), is(not(greaterThanOrEqualTo(400)))); |
44 | 42 | } |
45 | 43 |
|
46 | 44 | protected void assertAccessIsAllowed(String user, String method, String uri, String body) throws IOException { |
47 | | - assertAccessIsAllowed(user, method, uri, body, new HashMap<>()); |
| 45 | + Request request = new Request(method, uri); |
| 46 | + request.setJsonEntity(body); |
| 47 | + assertAccessIsAllowed(user, request); |
48 | 48 | } |
49 | 49 |
|
50 | 50 | protected void assertAccessIsAllowed(String user, String method, String uri) throws IOException { |
51 | | - assertAccessIsAllowed(user, method, uri, null, new HashMap<>()); |
| 51 | + assertAccessIsAllowed(user, new Request(method, uri)); |
52 | 52 | } |
53 | 53 |
|
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()))))); |
| 54 | + protected void assertAccessIsDenied(String user, Request request) throws IOException { |
| 55 | + RequestOptions.Builder options = request.getOptions().toBuilder(); |
| 56 | + options.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(user, new SecureString("passwd".toCharArray()))); |
| 57 | + request.setOptions(options); |
| 58 | + ResponseException responseException = expectThrows(ResponseException.class, () -> getRestClient().performRequest(request)); |
68 | 59 | 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, |
| 60 | + String message = String.format(Locale.ROOT, "%s %s body %s: Expected 403, got %s %s with body %s", |
| 61 | + request.getMethod(), request.getEndpoint(), EntityUtils.toString(request.getEntity()), |
70 | 62 | statusLine.getStatusCode(), statusLine.getReasonPhrase(), |
71 | 63 | EntityUtils.toString(responseException.getResponse().getEntity())); |
72 | 64 | assertThat(message, statusLine.getStatusCode(), is(403)); |
73 | 65 | } |
74 | 66 |
|
| 67 | + protected void assertAccessIsDenied(String user, String method, String uri, String body) throws IOException { |
| 68 | + Request request = new Request(method, uri); |
| 69 | + request.setJsonEntity(body); |
| 70 | + assertAccessIsDenied(user, request); |
| 71 | + } |
75 | 72 |
|
76 | | - protected void assertBodyHasAccessIsDenied(String user, String method, String uri, String body) throws IOException { |
77 | | - assertBodyHasAccessIsDenied(user, method, uri, body, new HashMap<>()); |
| 73 | + protected void assertAccessIsDenied(String user, String method, String uri) throws IOException { |
| 74 | + assertAccessIsDenied(user, new Request(method, uri)); |
78 | 75 | } |
79 | 76 |
|
80 | 77 | /** |
81 | 78 | * Like {@code assertAcessIsDenied}, but for _bulk requests since the entire |
82 | 79 | * request will not be failed, just the individual ones |
83 | 80 | */ |
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())))); |
| 81 | + protected void assertBodyHasAccessIsDenied(String user, Request request) throws IOException { |
| 82 | + RequestOptions.Builder options = request.getOptions().toBuilder(); |
| 83 | + options.addHeader("Authorization", UsernamePasswordToken.basicAuthHeaderValue(user, new SecureString("passwd".toCharArray()))); |
| 84 | + request.setOptions(options); |
| 85 | + Response resp = getRestClient().performRequest(request); |
89 | 86 | StatusLine statusLine = resp.getStatusLine(); |
90 | 87 | assertThat(statusLine.getStatusCode(), is(200)); |
91 | 88 | HttpEntity bodyEntity = resp.getEntity(); |
92 | 89 | String bodyStr = EntityUtils.toString(bodyEntity); |
93 | 90 | assertThat(bodyStr, containsString("unauthorized for user [" + user + "]")); |
94 | 91 | } |
95 | 92 |
|
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; |
| 93 | + protected void assertBodyHasAccessIsDenied(String user, String method, String uri, String body) throws IOException { |
| 94 | + Request request = new Request(method, uri); |
| 95 | + request.setJsonEntity(body); |
| 96 | + assertBodyHasAccessIsDenied(user, request); |
102 | 97 | } |
103 | 98 | } |
0 commit comments