|
11 | 11 |
|
12 | 12 | import org.elasticsearch.common.bytes.BytesArray;
|
13 | 13 | import org.elasticsearch.common.bytes.BytesReference;
|
| 14 | +import org.elasticsearch.common.xcontent.XContentHelper; |
14 | 15 | import org.elasticsearch.index.reindex.AbstractBulkByScrollRequest;
|
15 | 16 | import org.elasticsearch.index.reindex.ReindexRequest;
|
| 17 | +import org.elasticsearch.rest.RestRequest; |
16 | 18 | import org.elasticsearch.test.rest.FakeRestRequest;
|
17 | 19 | import org.elasticsearch.test.rest.RestActionTestCase;
|
18 | 20 | import org.elasticsearch.xcontent.XContentBuilder;
|
|
21 | 23 | import org.junit.Before;
|
22 | 24 |
|
23 | 25 | import java.io.IOException;
|
| 26 | +import java.util.List; |
| 27 | +import java.util.Map; |
24 | 28 |
|
25 | 29 | import static java.util.Collections.singletonMap;
|
| 30 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent; |
| 31 | +import static org.hamcrest.Matchers.aMapWithSize; |
| 32 | +import static org.hamcrest.Matchers.equalTo; |
| 33 | +import static org.hamcrest.Matchers.hasEntry; |
| 34 | +import static org.hamcrest.Matchers.hasKey; |
| 35 | +import static org.hamcrest.Matchers.notNullValue; |
26 | 36 |
|
27 | 37 | public class RestReindexActionTests extends RestActionTestCase {
|
28 | 38 |
|
@@ -74,4 +84,150 @@ public void testSetScrollTimeout() throws IOException {
|
74 | 84 | assertEquals("10m", request.getScrollTime().toString());
|
75 | 85 | }
|
76 | 86 | }
|
| 87 | + |
| 88 | + public void testFilterSource() throws IOException { |
| 89 | + final FakeRestRequest.Builder requestBuilder = new FakeRestRequest.Builder(xContentRegistry()); |
| 90 | + final var body = """ |
| 91 | + { |
| 92 | + "source" : { |
| 93 | + "index": "photos", |
| 94 | + "remote" : { |
| 95 | + "host": "https://bugle.example.net:2400/", |
| 96 | + "username": "peter.parker", |
| 97 | + "password": "mj4ever!", |
| 98 | + "headers": { |
| 99 | + "X-Hero-Name": "spiderman" |
| 100 | + } |
| 101 | + } |
| 102 | + }, |
| 103 | + "dest": { |
| 104 | + "index": "webshots" |
| 105 | + } |
| 106 | + } |
| 107 | + """; |
| 108 | + requestBuilder.withContent(new BytesArray(body), XContentType.JSON); |
| 109 | + |
| 110 | + final FakeRestRequest restRequest = requestBuilder.build(); |
| 111 | + ReindexRequest request = action.buildRequest(restRequest); |
| 112 | + |
| 113 | + // Check that the request parsed correctly |
| 114 | + assertThat(request.getRemoteInfo().getScheme(), equalTo("https")); |
| 115 | + assertThat(request.getRemoteInfo().getHost(), equalTo("bugle.example.net")); |
| 116 | + assertThat(request.getRemoteInfo().getPort(), equalTo(2400)); |
| 117 | + assertThat(request.getRemoteInfo().getUsername(), equalTo("peter.parker")); |
| 118 | + assertThat(request.getRemoteInfo().getPassword(), equalTo("mj4ever!")); |
| 119 | + assertThat(request.getRemoteInfo().getHeaders(), hasEntry("X-Hero-Name", "spiderman")); |
| 120 | + assertThat(request.getRemoteInfo().getHeaders(), aMapWithSize(1)); |
| 121 | + |
| 122 | + final RestRequest filtered = action.getFilteredRequest(restRequest); |
| 123 | + assertToXContentEquivalent(new BytesArray(""" |
| 124 | + { |
| 125 | + "source" : { |
| 126 | + "index": "photos", |
| 127 | + "remote" : { |
| 128 | + "host": "https://bugle.example.net:2400/", |
| 129 | + "username": "peter.parker", |
| 130 | + "password": "::es-redacted::", |
| 131 | + "headers": { |
| 132 | + "X-Hero-Name": "::es-redacted::" |
| 133 | + } |
| 134 | + } |
| 135 | + }, |
| 136 | + "dest": { |
| 137 | + "index": "webshots" |
| 138 | + } |
| 139 | + } |
| 140 | + """), filtered.content(), XContentType.JSON); |
| 141 | + } |
| 142 | + |
| 143 | + public void testUnfilteredSource() throws IOException { |
| 144 | + final FakeRestRequest.Builder requestBuilder = new FakeRestRequest.Builder(xContentRegistry()); |
| 145 | + final var empty1 = ""; |
| 146 | + final var empty2 = "{}"; |
| 147 | + final var nonRemote = """ |
| 148 | + { |
| 149 | + "source" : { "index": "your-index" }, |
| 150 | + "dest" : { "index": "my-index" } |
| 151 | + } |
| 152 | + """; |
| 153 | + final var noCredentials = """ |
| 154 | + { |
| 155 | + "source" : { |
| 156 | + "index": "remote-index", |
| 157 | + "remote" : { |
| 158 | + "host": "https://es.example.net:12345/", |
| 159 | + "headers": {} |
| 160 | + } |
| 161 | + }, |
| 162 | + "dest": { |
| 163 | + "index": "my-index" |
| 164 | + } |
| 165 | + } |
| 166 | + """; |
| 167 | + for (String body : List.of(empty1, empty2, nonRemote, noCredentials)) { |
| 168 | + final BytesArray bodyAsBytes = new BytesArray(body); |
| 169 | + requestBuilder.withContent(bodyAsBytes, XContentType.JSON); |
| 170 | + final FakeRestRequest restRequest = requestBuilder.build(); |
| 171 | + final RestRequest filtered = action.getFilteredRequest(restRequest); |
| 172 | + assertToXContentEquivalent(bodyAsBytes, filtered.content(), XContentType.JSON); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + public void testFilteringBadlyStructureSourceIsSafe() throws IOException { |
| 177 | + final FakeRestRequest.Builder requestBuilder = new FakeRestRequest.Builder(xContentRegistry()); |
| 178 | + final var remoteAsString = """ |
| 179 | + { |
| 180 | + "source" : { |
| 181 | + "index": "remote-index", |
| 182 | + "remote" : "https://es.example.net:12345/" |
| 183 | + }, |
| 184 | + "dest": { |
| 185 | + "index": "my-index" |
| 186 | + } |
| 187 | + } |
| 188 | + """; |
| 189 | + final var passwordAsNumber = """ |
| 190 | + { |
| 191 | + "source" : { |
| 192 | + "index": "remote-index", |
| 193 | + "remote" : { |
| 194 | + "host": "https://es.example.net:12345/", |
| 195 | + "username": "skroob", |
| 196 | + "password": 12345 |
| 197 | + } |
| 198 | + }, |
| 199 | + "dest": { |
| 200 | + "index": "my-index" |
| 201 | + } |
| 202 | + } |
| 203 | + """; |
| 204 | + final var headersAsList = """ |
| 205 | + { |
| 206 | + "source" : { |
| 207 | + "index": "remote-index", |
| 208 | + "remote" : { |
| 209 | + "host": "https://es.example.net:12345/", |
| 210 | + "headers": [ "bogus" ] |
| 211 | + } |
| 212 | + }, |
| 213 | + "dest": { |
| 214 | + "index": "my-index" |
| 215 | + } |
| 216 | + } |
| 217 | + """; |
| 218 | + for (String body : List.of(remoteAsString, passwordAsNumber, headersAsList)) { |
| 219 | + final BytesArray bodyAsBytes = new BytesArray(body); |
| 220 | + requestBuilder.withContent(bodyAsBytes, XContentType.JSON); |
| 221 | + final FakeRestRequest restRequest = requestBuilder.build(); |
| 222 | + |
| 223 | + final RestRequest filtered = action.getFilteredRequest(restRequest); |
| 224 | + assertThat(filtered, notNullValue()); |
| 225 | + |
| 226 | + // We will redacted some parts of these bodies, so just check that they end up as valid JSON with the right top level fields |
| 227 | + final Map<String, Object> filteredMap = XContentHelper.convertToMap(filtered.content(), false, XContentType.JSON).v2(); |
| 228 | + assertThat(filteredMap, notNullValue()); |
| 229 | + assertThat(filteredMap, hasKey("source")); |
| 230 | + assertThat(filteredMap, hasKey("dest")); |
| 231 | + } |
| 232 | + } |
77 | 233 | }
|
0 commit comments