forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzureHttpHandler.java
More file actions
324 lines (290 loc) · 16 KB
/
AzureHttpHandler.java
File metadata and controls
324 lines (290 loc) · 16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/
package fixture.azure;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.SuppressForbidden;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.RestUtils;
import org.elasticsearch.test.fixture.HttpHeaderParser;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentType;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import static org.elasticsearch.repositories.azure.AzureFixtureHelper.assertValidBlockId;
/**
* Minimal HTTP handler that acts as an Azure compliant server
*/
@SuppressForbidden(reason = "Uses a HttpServer to emulate an Azure endpoint")
public class AzureHttpHandler implements HttpHandler {
private final Map<String, BytesReference> blobs;
private final String account;
private final String container;
private final Predicate<String> authHeaderPredicate;
public AzureHttpHandler(final String account, final String container, @Nullable Predicate<String> authHeaderPredicate) {
this.account = Objects.requireNonNull(account);
this.container = Objects.requireNonNull(container);
this.authHeaderPredicate = authHeaderPredicate;
this.blobs = new ConcurrentHashMap<>();
}
private static List<String> getAuthHeader(HttpExchange exchange) {
return exchange.getRequestHeaders().get("Authorization");
}
private boolean isValidAuthHeader(HttpExchange exchange) {
if (authHeaderPredicate == null) {
return true;
}
final var authHeader = getAuthHeader(exchange);
if (authHeader == null) {
return false;
}
if (authHeader.size() != 1) {
return false;
}
return authHeaderPredicate.test(authHeader.get(0));
}
@Override
public void handle(final HttpExchange exchange) throws IOException {
if (isValidAuthHeader(exchange) == false) {
try (exchange; var builder = XContentBuilder.builder(XContentType.JSON.xContent())) {
builder.startObject();
builder.field("method", exchange.getRequestMethod());
builder.field("uri", exchange.getRequestURI().toString());
builder.field("predicate", authHeaderPredicate.toString());
builder.field("authorization", Objects.toString(getAuthHeader(exchange)));
builder.startObject("headers");
for (final var header : exchange.getRequestHeaders().entrySet()) {
if (header.getValue() == null) {
builder.nullField(header.getKey());
} else {
builder.startArray(header.getKey());
for (final var value : header.getValue()) {
builder.value(value);
}
builder.endArray();
}
}
builder.endObject();
builder.endObject();
final var responseBytes = BytesReference.bytes(builder);
exchange.getResponseHeaders().add("Content-Type", "application/json; charset=utf-8");
exchange.sendResponseHeaders(RestStatus.FORBIDDEN.getStatus(), responseBytes.length());
responseBytes.writeTo(exchange.getResponseBody());
return;
}
}
final String request = exchange.getRequestMethod() + " " + exchange.getRequestURI().toString();
if (request.startsWith("GET") || request.startsWith("HEAD") || request.startsWith("DELETE")) {
int read = exchange.getRequestBody().read();
assert read == -1 : "Request body should have been empty but saw [" + read + "]";
}
try {
if (Regex.simpleMatch("PUT /" + account + "/" + container + "/*blockid=*", request)) {
// Put Block (https://docs.microsoft.com/en-us/rest/api/storageservices/put-block)
final Map<String, String> params = new HashMap<>();
RestUtils.decodeQueryString(exchange.getRequestURI().getRawQuery(), 0, params);
final String blockId = params.get("blockid");
assert assertValidBlockId(blockId);
blobs.put(blockId, Streams.readFully(exchange.getRequestBody()));
exchange.sendResponseHeaders(RestStatus.CREATED.getStatus(), -1);
} else if (Regex.simpleMatch("PUT /" + account + "/" + container + "/*comp=blocklist*", request)) {
// Put Block List (https://docs.microsoft.com/en-us/rest/api/storageservices/put-block-list)
final String blockList = Streams.copyToString(new InputStreamReader(exchange.getRequestBody(), StandardCharsets.UTF_8));
final List<String> blockIds = Arrays.stream(blockList.split("<Latest>"))
.filter(line -> line.contains("</Latest>"))
.map(line -> line.substring(0, line.indexOf("</Latest>")))
.toList();
final ByteArrayOutputStream blob = new ByteArrayOutputStream();
for (String blockId : blockIds) {
BytesReference block = blobs.remove(blockId);
assert block != null;
block.writeTo(blob);
}
blobs.put(exchange.getRequestURI().getPath(), new BytesArray(blob.toByteArray()));
exchange.getResponseHeaders().add("x-ms-request-server-encrypted", "false");
exchange.sendResponseHeaders(RestStatus.CREATED.getStatus(), -1);
} else if (Regex.simpleMatch("PUT /" + account + "/" + container + "/*", request)) {
// PUT Blob (see https://docs.microsoft.com/en-us/rest/api/storageservices/put-blob)
final String ifNoneMatch = exchange.getRequestHeaders().getFirst("If-None-Match");
if ("*".equals(ifNoneMatch)) {
if (blobs.putIfAbsent(exchange.getRequestURI().getPath(), Streams.readFully(exchange.getRequestBody())) != null) {
sendError(exchange, RestStatus.CONFLICT);
return;
}
} else {
blobs.put(exchange.getRequestURI().getPath(), Streams.readFully(exchange.getRequestBody()));
}
exchange.getResponseHeaders().add("x-ms-request-server-encrypted", "false");
exchange.sendResponseHeaders(RestStatus.CREATED.getStatus(), -1);
} else if (Regex.simpleMatch("HEAD /" + account + "/" + container + "/*", request)) {
// Get Blob Properties (see https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob-properties)
final BytesReference blob = blobs.get(exchange.getRequestURI().getPath());
if (blob == null) {
sendError(exchange, RestStatus.NOT_FOUND);
return;
}
exchange.getResponseHeaders().add("x-ms-blob-content-length", String.valueOf(blob.length()));
exchange.getResponseHeaders().add("Content-Length", String.valueOf(blob.length()));
exchange.getResponseHeaders().add("x-ms-blob-type", "BlockBlob");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), -1);
} else if (Regex.simpleMatch("GET /" + account + "/" + container + "/*", request)) {
// GET Object (https://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectGET.html)
final BytesReference blob = blobs.get(exchange.getRequestURI().getPath());
if (blob == null) {
sendError(exchange, RestStatus.NOT_FOUND);
return;
}
// see Constants.HeaderConstants.STORAGE_RANGE_HEADER
final String rangeHeader = exchange.getRequestHeaders().getFirst("x-ms-range");
if (rangeHeader == null) {
throw new AssertionError("Range header is required");
}
final HttpHeaderParser.Range range = HttpHeaderParser.parseRangeHeader(rangeHeader);
if (range == null) {
throw new AssertionError("Range header does not match expected format: " + rangeHeader);
}
if (blob.length() <= range.start()) {
exchange.getResponseHeaders().add("Content-Type", "application/octet-stream");
exchange.sendResponseHeaders(RestStatus.REQUESTED_RANGE_NOT_SATISFIED.getStatus(), -1);
return;
}
var responseBlob = blob.slice(
Math.toIntExact(range.start()),
Math.toIntExact(Math.min(range.end() - range.start() + 1, blob.length() - range.start()))
);
exchange.getResponseHeaders().add("Content-Type", "application/octet-stream");
exchange.getResponseHeaders().add("x-ms-blob-content-length", String.valueOf(responseBlob.length()));
exchange.getResponseHeaders().add("x-ms-blob-type", "blockblob");
exchange.getResponseHeaders().add("ETag", "\"blockblob\"");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), responseBlob.length());
responseBlob.writeTo(exchange.getResponseBody());
} else if (Regex.simpleMatch("DELETE /" + account + "/" + container + "/*", request)) {
// Delete Blob (https://docs.microsoft.com/en-us/rest/api/storageservices/delete-blob)
final boolean deleted = blobs.entrySet().removeIf(blob -> blob.getKey().startsWith(exchange.getRequestURI().getPath()));
if (deleted) {
exchange.sendResponseHeaders(RestStatus.ACCEPTED.getStatus(), -1);
} else {
exchange.sendResponseHeaders(RestStatus.NOT_FOUND.getStatus(), -1);
}
} else if (Regex.simpleMatch("GET /" + account + "/" + container + "?*restype=container*comp=list*", request)) {
// List Blobs (https://docs.microsoft.com/en-us/rest/api/storageservices/list-blobs)
final Map<String, String> params = new HashMap<>();
RestUtils.decodeQueryString(exchange.getRequestURI().getQuery(), 0, params);
final StringBuilder list = new StringBuilder();
list.append("""
<?xml version="1.0" encoding="UTF-8"?>
<EnumerationResults>""");
final String prefix = params.get("prefix");
final Set<String> blobPrefixes = new HashSet<>();
final String delimiter = params.get("delimiter");
if (delimiter != null) {
list.append("<Delimiter>").append(delimiter).append("</Delimiter>");
}
list.append("<Blobs>");
for (Map.Entry<String, BytesReference> blob : blobs.entrySet()) {
if (prefix != null && blob.getKey().startsWith("/" + account + "/" + container + "/" + prefix) == false) {
continue;
}
String blobPath = blob.getKey().replace("/" + account + "/" + container + "/", "");
if (delimiter != null) {
int fromIndex = (prefix != null ? prefix.length() : 0);
int delimiterPosition = blobPath.indexOf(delimiter, fromIndex);
if (delimiterPosition > 0) {
blobPrefixes.add(blobPath.substring(0, delimiterPosition) + delimiter);
continue;
}
}
list.append(String.format(Locale.ROOT, """
<Blob>
<Name>%s</Name>
<Properties>
<Content-Length>%s</Content-Length>
<BlobType>BlockBlob</BlobType>
</Properties>
</Blob>""", blobPath, blob.getValue().length()));
}
if (blobPrefixes.isEmpty() == false) {
blobPrefixes.forEach(p -> list.append("<BlobPrefix><Name>").append(p).append("</Name></BlobPrefix>"));
}
list.append("""
</Blobs>
<NextMarker/>
</EnumerationResults>""");
byte[] response = list.toString().getBytes(StandardCharsets.UTF_8);
exchange.getResponseHeaders().add("Content-Type", "application/xml");
exchange.sendResponseHeaders(RestStatus.OK.getStatus(), response.length);
exchange.getResponseBody().write(response);
} else {
sendError(exchange, RestStatus.BAD_REQUEST);
}
} finally {
exchange.close();
}
}
public Map<String, BytesReference> blobs() {
return blobs;
}
public static void sendError(final HttpExchange exchange, final RestStatus status) throws IOException {
final Headers headers = exchange.getResponseHeaders();
headers.add("Content-Type", "application/xml");
// see Constants.HeaderConstants.CLIENT_REQUEST_ID_HEADER
final String requestId = exchange.getRequestHeaders().getFirst("x-ms-client-request-id");
if (requestId != null) {
// see Constants.HeaderConstants.STORAGE_RANGE_HEADER
headers.add("x-ms-request-id", requestId);
}
final String errorCode = toAzureErrorCode(status);
// see Constants.HeaderConstants.ERROR_CODE
headers.add("x-ms-error-code", errorCode);
if ("HEAD".equals(exchange.getRequestMethod())) {
exchange.sendResponseHeaders(status.getStatus(), -1L);
} else {
final byte[] response = (String.format(Locale.ROOT, """
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>%s</Code>
<Message>%s</Message>
</Error>""", errorCode, status)).getBytes(StandardCharsets.UTF_8);
exchange.sendResponseHeaders(status.getStatus(), response.length);
exchange.getResponseBody().write(response);
}
}
// See https://docs.microsoft.com/en-us/rest/api/storageservices/common-rest-api-error-codes
private static String toAzureErrorCode(final RestStatus status) {
assert status.getStatus() >= 400;
return switch (status) {
case BAD_REQUEST -> "InvalidMetadata";
case NOT_FOUND -> "BlobNotFound";
case INTERNAL_SERVER_ERROR -> "InternalError";
case SERVICE_UNAVAILABLE -> "ServerBusy";
case CONFLICT -> "BlobAlreadyExists";
default -> throw new IllegalArgumentException(
"Error code [" + status.getStatus() + "] is not mapped to an existing Azure code"
);
};
}
}